428
|
1 /* base64 interface for XEmacs.
|
|
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
|
5137
|
3 Copyright (C) 2010 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* Author: William Perry <wmperry@aventail.com> */
|
|
25
|
|
26 #include <emodules.h>
|
|
27
|
|
28 unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
29
|
|
30 DEFUN ("base64-encode", Fbase64_encode, 1, 5, 0, /*
|
|
31 Return the base64 encoding of an object.
|
|
32 OBJECT is either a string or a buffer.
|
|
33 Optional arguments START and END denote buffer positions for computing the
|
|
34 hash of a portion of OBJECT. The optional CODING argument specifies the coding
|
|
35 system the text is to be represented in while computing the digest. This only
|
|
36 has meaning with MULE, and defaults to the current format of the data.
|
|
37 If ERROR-ME-NOT is nil, report an error if the coding system can't be
|
|
38 determined. Else assume binary coding if all else fails.
|
|
39 */
|
|
40 (object, start, end, coding, error_me_not))
|
|
41 {
|
5137
|
42 int cols,bits,char_count;
|
|
43 Lisp_Object instream, outstream,deststream;
|
|
44 Lstream *istr, *ostr, *dstr;
|
|
45 static Extbyte_dynarr *conversion_out_dynarr;
|
|
46 static Extbyte_dynarr *out_dynarr;
|
|
47 char tempbuf[1024]; /* some random amount */
|
|
48 struct gcpro gcpro1, gcpro2;
|
|
49 Lisp_Object conv_out_stream, coding_system;
|
|
50 Lstream *costr;
|
|
51 struct gcpro gcpro3;
|
428
|
52
|
5137
|
53 if (!conversion_out_dynarr)
|
|
54 conversion_out_dynarr = Dynarr_new (Extbyte);
|
|
55 else
|
|
56 Dynarr_reset (conversion_out_dynarr);
|
|
57
|
|
58 if (!out_dynarr)
|
|
59 out_dynarr = Dynarr_new (Extbyte);
|
|
60 else
|
|
61 Dynarr_reset (out_dynarr);
|
|
62
|
|
63 char_count = bits = cols = 0;
|
|
64
|
|
65 /* set up the in stream */
|
|
66 if (BUFFERP (object))
|
|
67 {
|
|
68 struct buffer *b = XBUFFER (object);
|
|
69 Charbpos begv, endv;
|
|
70 /* Figure out where we need to get info from */
|
|
71 get_buffer_range_char (b, start, end, &begv, &endv, GB_ALLOW_NIL);
|
428
|
72
|
5137
|
73 instream = make_lisp_buffer_input_stream (b, begv, endv, 0);
|
|
74 }
|
|
75 else
|
|
76 {
|
|
77 Bytecount bstart, bend;
|
|
78 CHECK_STRING (object);
|
|
79 get_string_range_byte (object, start, end, &bstart, &bend,
|
|
80 GB_HISTORICAL_STRING_BEHAVIOR);
|
|
81 instream = make_lisp_string_input_stream (object, bstart, bend);
|
|
82 }
|
|
83 istr = XLSTREAM (instream);
|
428
|
84
|
5137
|
85 /* Find out what format the buffer will be saved in, so we can make
|
|
86 the digest based on what it will look like on disk */
|
|
87 if (NILP (coding))
|
|
88 {
|
|
89 if (BUFFERP (object))
|
428
|
90 {
|
5137
|
91 /* Use the file coding for this buffer by default */
|
|
92 coding_system = XBUFFER (object)->buffer_file_coding_system;
|
428
|
93 }
|
5137
|
94 else
|
|
95 {
|
|
96 /* attempt to autodetect the coding of the string. Note: this VERY hit-and-miss */
|
|
97 enum eol_type eol = EOL_AUTODETECT;
|
|
98 coding_system = Fget_coding_system (Qundecided);
|
|
99 determine_real_coding_system (istr, &coding_system, &eol);
|
|
100 }
|
|
101 if (NILP (coding_system))
|
|
102 coding_system = Fget_coding_system (Qbinary);
|
|
103 else
|
428
|
104 {
|
5137
|
105 coding_system = Ffind_coding_system (coding_system);
|
|
106 if (NILP (coding_system))
|
|
107 coding_system = Fget_coding_system (Qbinary);
|
428
|
108 }
|
5137
|
109 }
|
|
110 else
|
|
111 {
|
|
112 coding_system = Ffind_coding_system (coding);
|
|
113 if (NILP (coding_system))
|
|
114 {
|
|
115 if (NILP (error_me_not))
|
|
116 signal_simple_error ("No such coding system", coding);
|
|
117 else
|
|
118 coding_system = Fget_coding_system (Qbinary); /* default to binary */
|
|
119 }
|
|
120 }
|
428
|
121
|
5137
|
122 /* setup the out stream */
|
|
123 outstream = make_dynarr_output_stream ((unsigned_char_dynarr *)conversion_out_dynarr);
|
|
124 ostr = XLSTREAM (outstream);
|
|
125 deststream = make_dynarr_output_stream ((unsigned_char_dynarr *)out_dynarr);
|
|
126 dstr = XLSTREAM (deststream);
|
|
127 /* setup the conversion stream */
|
|
128 conv_out_stream = make_encoding_output_stream (ostr, coding_system);
|
|
129 costr = XLSTREAM (conv_out_stream);
|
|
130 GCPRO3 (instream, outstream, conv_out_stream);
|
|
131
|
|
132 /* Get the data while doing the conversion */
|
|
133 while (1)
|
|
134 {
|
|
135 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
136 int l;
|
|
137 if (!size_in_bytes)
|
|
138 break;
|
|
139 /* It does seem the flushes are necessary... */
|
|
140 Lstream_write (costr, tempbuf, size_in_bytes);
|
|
141 Lstream_flush (costr);
|
|
142 Lstream_flush (ostr);
|
|
143
|
|
144 /* Update the base64 output buffer */
|
|
145 for (l = 0; l < size_in_bytes; l++)
|
428
|
146 {
|
5137
|
147 bits += Dynarr_at (conversion_out_dynarr,l);
|
|
148 char_count++;
|
|
149 if (char_count == 3)
|
428
|
150 {
|
5137
|
151 static char obuf[4];
|
|
152 obuf[0] = alphabet[(bits >> 18)];
|
|
153 obuf[1] = alphabet[(bits >> 12) & 0x3f];
|
|
154 obuf[2] = alphabet[(bits >> 6) & 0x3f];
|
|
155 obuf[3] = alphabet[bits & 0x3f];
|
|
156
|
|
157 Lstream_write (dstr,obuf,sizeof (obuf));
|
|
158 cols += 4;
|
|
159 if (cols == 72)
|
|
160 {
|
|
161 Lstream_write (dstr,"\n",sizeof (unsigned char));
|
|
162 cols = 0;
|
|
163 }
|
|
164 bits = char_count = 0;
|
428
|
165 }
|
5137
|
166 else
|
428
|
167 {
|
5137
|
168 bits <<= 8;
|
428
|
169 }
|
|
170 }
|
5137
|
171 /* reset the dynarr */
|
|
172 Lstream_rewind (ostr);
|
|
173 }
|
|
174 Lstream_close (istr);
|
|
175 Lstream_close (costr);
|
|
176 Lstream_close (ostr);
|
428
|
177
|
5137
|
178 if (char_count != 0)
|
|
179 {
|
|
180 bits <<= 16 - (8 * char_count);
|
|
181 Lstream_write (dstr,&alphabet[bits >> 18],sizeof (unsigned char));
|
|
182 Lstream_write (dstr,&alphabet[(bits >> 12) & 0x3f],sizeof (unsigned char));
|
|
183 if (char_count == 1)
|
|
184 {
|
|
185 Lstream_write (dstr,"==",2 * sizeof (unsigned char));
|
|
186 } else
|
|
187 {
|
|
188 Lstream_write (dstr,&alphabet[(bits >> 6) & 0x3f],sizeof (unsigned char));
|
|
189 Lstream_write (dstr,"=",sizeof (unsigned char));
|
428
|
190 }
|
5137
|
191 }
|
|
192 #if 0
|
|
193 if (cols > 0)
|
|
194 {
|
|
195 Lstream_write (dstr,"\n",sizeof (unsigned char));
|
|
196 }
|
428
|
197 #endif
|
5137
|
198 UNGCPRO;
|
|
199 Lstream_delete (istr);
|
|
200 Lstream_delete (ostr);
|
|
201 Lstream_delete (costr);
|
|
202 Lstream_flush (dstr);
|
|
203 Lstream_delete (dstr);
|
428
|
204
|
5137
|
205 return (make_string (Dynarr_atp (out_dynarr,0),Dynarr_length (out_dynarr)));
|
428
|
206 }
|
|
207
|
|
208 DEFUN ("base64-decode", Fbase64_decode, 1, 5, 0, /*
|
|
209 Undo the base64 encoding of an object.
|
|
210 OBJECT is either a string or a buffer.
|
|
211 Optional arguments START and END denote buffer positions for computing the
|
|
212 hash of a portion of OBJECT. The optional CODING argument specifies the coding
|
|
213 system the text is to be represented in while computing the digest. This only
|
|
214 has meaning with MULE, and defaults to the current format of the data.
|
|
215 If ERROR-ME-NOT is nil, report an error if the coding system can't be
|
|
216 determined. Else assume binary coding if all else fails.
|
|
217 */
|
|
218 (object, start, end, coding, error_me_not))
|
|
219 {
|
5137
|
220 static char inalphabet[256], decoder[256];
|
|
221 int i,cols,bits,char_count,hit_eof;
|
|
222 Lisp_Object instream, outstream,deststream;
|
|
223 Lstream *istr, *ostr, *dstr;
|
|
224 static Extbyte_dynarr *conversion_out_dynarr;
|
|
225 static Extbyte_dynarr *out_dynarr;
|
|
226 char tempbuf[1024]; /* some random amount */
|
|
227 struct gcpro gcpro1, gcpro2;
|
|
228 Lisp_Object conv_out_stream, coding_system;
|
|
229 Lstream *costr;
|
|
230 struct gcpro gcpro3;
|
428
|
231
|
5137
|
232 for (i = (sizeof alphabet) - 1; i >= 0 ; i--)
|
|
233 {
|
|
234 inalphabet[alphabet[i]] = 1;
|
|
235 decoder[alphabet[i]] = i;
|
428
|
236 }
|
|
237
|
5137
|
238 if (!conversion_out_dynarr)
|
|
239 conversion_out_dynarr = Dynarr_new (Extbyte);
|
|
240 else
|
|
241 Dynarr_reset (conversion_out_dynarr);
|
428
|
242
|
5137
|
243 if (!out_dynarr)
|
|
244 out_dynarr = Dynarr_new (Extbyte);
|
|
245 else
|
|
246 Dynarr_reset (out_dynarr);
|
428
|
247
|
5137
|
248 char_count = bits = cols = hit_eof = 0;
|
428
|
249
|
5137
|
250 /* set up the in stream */
|
|
251 if (BUFFERP (object))
|
|
252 {
|
|
253 struct buffer *b = XBUFFER (object);
|
|
254 Charbpos begv, endv;
|
|
255 /* Figure out where we need to get info from */
|
|
256 get_buffer_range_char (b, start, end, &begv, &endv, GB_ALLOW_NIL);
|
428
|
257
|
5137
|
258 instream = make_lisp_buffer_input_stream (b, begv, endv, 0);
|
|
259 }
|
|
260 else
|
|
261 {
|
|
262 Bytecount bstart, bend;
|
|
263 CHECK_STRING (object);
|
|
264 get_string_range_byte (object, start, end, &bstart, &bend,
|
|
265 GB_HISTORICAL_STRING_BEHAVIOR);
|
|
266 instream = make_lisp_string_input_stream (object, bstart, bend);
|
|
267 }
|
|
268 istr = XLSTREAM (instream);
|
428
|
269
|
5137
|
270 /* Find out what format the buffer will be saved in, so we can make
|
|
271 the digest based on what it will look like on disk */
|
|
272 if (NILP (coding))
|
|
273 {
|
|
274 if (BUFFERP (object))
|
|
275 {
|
|
276 /* Use the file coding for this buffer by default */
|
|
277 coding_system = XBUFFER (object)->buffer_file_coding_system;
|
|
278 }
|
|
279 else
|
|
280 {
|
|
281 /* attempt to autodetect the coding of the string. Note: this VERY hit-and-miss */
|
|
282 enum eol_type eol = EOL_AUTODETECT;
|
|
283 coding_system = Fget_coding_system (Qundecided);
|
|
284 determine_real_coding_system (istr, &coding_system, &eol);
|
|
285 }
|
|
286 if (NILP (coding_system))
|
|
287 coding_system = Fget_coding_system (Qbinary);
|
|
288 else
|
|
289 {
|
|
290 coding_system = Ffind_coding_system (coding_system);
|
|
291 if (NILP (coding_system))
|
|
292 coding_system = Fget_coding_system (Qbinary);
|
|
293 }
|
|
294 }
|
|
295 else
|
|
296 {
|
|
297 coding_system = Ffind_coding_system (coding);
|
|
298 if (NILP (coding_system))
|
428
|
299 {
|
5137
|
300 if (NILP (error_me_not))
|
|
301 signal_simple_error ("No such coding system", coding);
|
|
302 else
|
|
303 coding_system = Fget_coding_system (Qbinary); /* default to binary */
|
|
304 }
|
|
305 }
|
|
306
|
|
307 /* setup the out stream */
|
|
308 outstream = make_dynarr_output_stream ((unsigned_char_dynarr *)conversion_out_dynarr);
|
|
309 ostr = XLSTREAM (outstream);
|
|
310 deststream = make_dynarr_output_stream ((unsigned_char_dynarr *)out_dynarr);
|
|
311 dstr = XLSTREAM (deststream);
|
|
312 /* setup the conversion stream */
|
|
313 conv_out_stream = make_encoding_output_stream (ostr, coding_system);
|
|
314 costr = XLSTREAM (conv_out_stream);
|
|
315 GCPRO3 (instream, outstream, conv_out_stream);
|
|
316
|
|
317 /* Get the data while doing the conversion */
|
|
318 while (1)
|
|
319 {
|
|
320 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
321 int l;
|
|
322 if (!size_in_bytes)
|
|
323 {
|
|
324 hit_eof = 1;
|
|
325 break;
|
|
326 }
|
|
327 /* It does seem the flushes are necessary... */
|
|
328 Lstream_write (costr, tempbuf, size_in_bytes);
|
|
329 Lstream_flush (costr);
|
|
330 Lstream_flush (ostr);
|
|
331
|
|
332 /* Update the base64 output buffer */
|
|
333 for (l = 0; l < size_in_bytes; l++)
|
|
334 {
|
|
335 if (Dynarr_at (conversion_out_dynarr,l) == '=')
|
|
336 goto decoder_out;
|
|
337 bits += decoder[Dynarr_at (conversion_out_dynarr,l)];
|
|
338 fprintf (stderr,"%d\n",bits);
|
|
339 char_count++;
|
|
340 if (char_count == 4)
|
428
|
341 {
|
5137
|
342 static unsigned char obuf[3];
|
|
343 obuf[0] = (bits >> 16);
|
|
344 obuf[1] = (bits >> 8) & 0xff;
|
|
345 obuf[2] = (bits & 0xff);
|
|
346
|
|
347 Lstream_write (dstr,obuf,sizeof (obuf));
|
|
348 bits = char_count = 0;
|
428
|
349 }
|
5137
|
350 else
|
428
|
351 {
|
5137
|
352 bits <<= 6;
|
428
|
353 }
|
|
354 }
|
5137
|
355 /* reset the dynarr */
|
|
356 Lstream_rewind (ostr);
|
|
357 }
|
|
358 decoder_out:
|
|
359 Lstream_close (istr);
|
|
360 Lstream_close (costr);
|
|
361 Lstream_close (ostr);
|
428
|
362
|
5137
|
363 if (hit_eof)
|
|
364 {
|
|
365 if (char_count)
|
|
366 {
|
|
367 error_with_frob (object,"base64-decode failed: at least %d bits truncated",((4 - char_count) * 6));
|
428
|
368 }
|
5137
|
369 }
|
|
370 switch (char_count)
|
|
371 {
|
|
372 case 1:
|
|
373 error_with_frob (object, "base64 encoding incomplete: at least 2 bits missing");
|
|
374 break;
|
|
375 case 2:
|
|
376 char_count = bits >> 10;
|
|
377 Lstream_write (dstr,&char_count,sizeof (char_count));
|
|
378 break;
|
|
379 case 3:
|
|
380 {
|
|
381 unsigned char buf[2];
|
|
382 buf[0] = (bits >> 16);
|
|
383 buf[1] = (bits >> 8) & 0xff;
|
|
384 Lstream_write (dstr,buf,sizeof (buf));
|
|
385 break;
|
|
386 }
|
|
387 }
|
428
|
388
|
5137
|
389 UNGCPRO;
|
|
390 Lstream_delete (istr);
|
|
391 Lstream_delete (ostr);
|
|
392 Lstream_delete (costr);
|
|
393 Lstream_flush (dstr);
|
|
394 Lstream_delete (dstr);
|
428
|
395
|
5137
|
396 return (make_string (Dynarr_atp (out_dynarr,0),Dynarr_length (out_dynarr)));
|
428
|
397 }
|
|
398
|
|
399 void
|
|
400 syms_of_base64 (void)
|
|
401 {
|
5137
|
402 DEFSUBR (Fbase64_encode);
|
|
403 DEFSUBR (Fbase64_decode);
|
428
|
404 }
|
|
405
|
|
406 void
|
|
407 vars_of_base64 (void)
|
|
408 {
|
|
409 Fprovide (intern ("base64"));
|
|
410 }
|