annotate modules/base64/base64.c @ 754:8661af9617f8

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