annotate modules/base64/base64.c @ 5663:0df4d95bd98a

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