annotate modules/base64/base64.c @ 296:5a79be0ef6a8 r21-0b46

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