annotate modules/base64/base64.c @ 5182:2e528066e2fc

Move #'sort*, #'fill, #'merge to C from cl-seq.el. lisp/ChangeLog addition: 2010-04-01 Aidan Kehoe <kehoea@parhasard.net> * cl-seq.el (fill, sort*, merge): Move these functions to fns.c. (stable-sort): Make this docstring reflect the argument names used in the #'sort* docstring. * cl-macs.el (stable-sort): Make #'stable-sort exactly equivalent to #'sort* in compiled code. * bytecomp.el (byte-compile-maybe-add-*): New macro, for functions like #'sort and #'mapcar that, to be strictly compatible, should only take two args, but in our implementation can take more, because they're aliases of #'sort* and #'mapcar*. (byte-compile-mapcar, byte-compile-sort, byte-compile-fillarray): Use this new macro. (map-into): Add a byte-compile method for #'map-into in passing. * apropos.el (apropos-print): Use #'sort* with a :key argument, now it's in C. * compat.el (extent-at): Ditto. * register.el (list-registers): Ditto. * package-ui.el (pui-list-packages): Ditto. * help.el (sorted-key-descriptions): Ditto. src/ChangeLog addition: 2010-03-31 Aidan Kehoe <kehoea@parhasard.net> * fns.c (STRING_DATA_TO_OBJECT_ARRAY) (BIT_VECTOR_TO_OBJECT_ARRAY, c_merge_predicate_key) (c_merge_predicate_nokey, list_merge, array_merge) (list_array_merge_into_list, list_list_merge_into_array) (list_array_merge_into_array, CHECK_KEY_ARGUMENT, Fmerge) (list_sort, array_sort, FsortX): Move #'sort*, #'fill, #'merge from cl-seq.el to C, extending the implementations of Fsort, Ffillarray, and merge() to do so. * keymap.c (keymap_submaps, map_keymap_sort_predicate) (describe_map_sort_predicate): Change the calling semantics of the C sort predicates to return a non-nil Lisp object if the first argument is less than the second, rather than C integers. * fontcolor-msw.c (sort_font_list_function): * fileio.c (build_annotations): * dired.c (Fdirectory_files): * abbrev.c (Finsert_abbrev_table_description): Call list_sort instead of Fsort, list_merge instead of merge() in these functions. man/ChangeLog addition: 2010-04-01 Aidan Kehoe <kehoea@parhasard.net> * lispref/lists.texi (Rearrangement): Update the documentation of #'sort here, now that it accepts any type of sequence and the KEY keyword argument. (Though this is probably now the wrong place for this function, given that.)
author Aidan Kehoe <kehoea@parhasard.net>
date Thu, 01 Apr 2010 20:22:50 +0100
parents 0ac12485616c
children 308d34e9f07d
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
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 /* Synched up with: Not in FSF. */
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 /* Author: William Perry <wmperry@aventail.com> */
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 #include <emodules.h>
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 unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 DEFUN ("base64-encode", Fbase64_encode, 1, 5, 0, /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 Return the base64 encoding of an object.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 OBJECT is either a string or a buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 Optional arguments START and END denote buffer positions for computing the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 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
35 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
36 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
37 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
38 determined. Else assume binary coding if all else fails.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 (object, start, end, coding, error_me_not))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
42 int cols,bits,char_count;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
43 Lisp_Object instream, outstream,deststream;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
44 Lstream *istr, *ostr, *dstr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
45 static Extbyte_dynarr *conversion_out_dynarr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
46 static Extbyte_dynarr *out_dynarr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
47 char tempbuf[1024]; /* some random amount */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
48 struct gcpro gcpro1, gcpro2;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
49 Lisp_Object conv_out_stream, coding_system;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
50 Lstream *costr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
51 struct gcpro gcpro3;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
53 if (!conversion_out_dynarr)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
54 conversion_out_dynarr = Dynarr_new (Extbyte);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
55 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
56 Dynarr_reset (conversion_out_dynarr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
57
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
58 if (!out_dynarr)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
59 out_dynarr = Dynarr_new (Extbyte);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
60 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
61 Dynarr_reset (out_dynarr);
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 char_count = bits = cols = 0;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
64
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
65 /* set up the in stream */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
66 if (BUFFERP (object))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
67 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
68 struct buffer *b = XBUFFER (object);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
69 Charbpos begv, endv;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
70 /* Figure out where we need to get info from */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
71 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
72
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
73 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
74 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
75 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
76 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
77 Bytecount bstart, bend;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
78 CHECK_STRING (object);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
79 get_string_range_byte (object, start, end, &bstart, &bend,
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
80 GB_HISTORICAL_STRING_BEHAVIOR);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
81 instream = make_lisp_string_input_stream (object, bstart, bend);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
82 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
83 istr = XLSTREAM (instream);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
85 /* 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
86 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
87 if (NILP (coding))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
88 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
89 if (BUFFERP (object))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
91 /* Use the file coding for this buffer by default */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
92 coding_system = XBUFFER (object)->buffer_file_coding_system;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 }
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
94 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
95 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
96 /* 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
97 enum eol_type eol = EOL_AUTODETECT;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
98 coding_system = Fget_coding_system (Qundecided);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
99 determine_real_coding_system (istr, &coding_system, &eol);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
100 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
101 if (NILP (coding_system))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
102 coding_system = Fget_coding_system (Qbinary);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
103 else
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
105 coding_system = Ffind_coding_system (coding_system);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
106 if (NILP (coding_system))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
107 coding_system = Fget_coding_system (Qbinary);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 }
5137
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 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
111 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
112 coding_system = Ffind_coding_system (coding);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
113 if (NILP (coding_system))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
114 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
115 if (NILP (error_me_not))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
116 signal_simple_error ("No such coding system", coding);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
117 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
118 coding_system = Fget_coding_system (Qbinary); /* default to binary */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
119 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
120 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
122 /* setup the out stream */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
123 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
124 ostr = XLSTREAM (outstream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
125 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
126 dstr = XLSTREAM (deststream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
127 /* setup the conversion stream */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
128 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
129 costr = XLSTREAM (conv_out_stream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
130 GCPRO3 (instream, outstream, conv_out_stream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
131
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
132 /* Get the data while doing the conversion */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
133 while (1)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
134 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
135 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
136 int l;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
137 if (!size_in_bytes)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
138 break;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
139 /* It does seem the flushes are necessary... */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
140 Lstream_write (costr, tempbuf, size_in_bytes);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
141 Lstream_flush (costr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
142 Lstream_flush (ostr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
143
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
144 /* Update the base64 output buffer */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
145 for (l = 0; l < size_in_bytes; l++)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
147 bits += Dynarr_at (conversion_out_dynarr,l);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
148 char_count++;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
149 if (char_count == 3)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
151 static char obuf[4];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
152 obuf[0] = alphabet[(bits >> 18)];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
153 obuf[1] = alphabet[(bits >> 12) & 0x3f];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
154 obuf[2] = alphabet[(bits >> 6) & 0x3f];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
155 obuf[3] = alphabet[bits & 0x3f];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
156
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
157 Lstream_write (dstr,obuf,sizeof (obuf));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
158 cols += 4;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
159 if (cols == 72)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
160 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
161 Lstream_write (dstr,"\n",sizeof (unsigned char));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
162 cols = 0;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
163 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
164 bits = char_count = 0;
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 else
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
168 bits <<= 8;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 }
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
171 /* reset the dynarr */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
172 Lstream_rewind (ostr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
173 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
174 Lstream_close (istr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
175 Lstream_close (costr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
176 Lstream_close (ostr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
178 if (char_count != 0)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
179 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
180 bits <<= 16 - (8 * char_count);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
181 Lstream_write (dstr,&alphabet[bits >> 18],sizeof (unsigned char));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
182 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
183 if (char_count == 1)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
184 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
185 Lstream_write (dstr,"==",2 * sizeof (unsigned char));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
186 } else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
187 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
188 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
189 Lstream_write (dstr,"=",sizeof (unsigned char));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 }
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
191 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
192 #if 0
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
193 if (cols > 0)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
194 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
195 Lstream_write (dstr,"\n",sizeof (unsigned char));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
196 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 #endif
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
198 UNGCPRO;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
199 Lstream_delete (istr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
200 Lstream_delete (ostr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
201 Lstream_delete (costr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
202 Lstream_flush (dstr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
203 Lstream_delete (dstr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
205 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
206 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 DEFUN ("base64-decode", Fbase64_decode, 1, 5, 0, /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 Undo the base64 encoding of an object.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 OBJECT is either a string or a buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 Optional arguments START and END denote buffer positions for computing the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 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
213 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
214 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
215 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
216 determined. Else assume binary coding if all else fails.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 (object, start, end, coding, error_me_not))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
220 static char inalphabet[256], decoder[256];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
221 int i,cols,bits,char_count,hit_eof;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
222 Lisp_Object instream, outstream,deststream;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
223 Lstream *istr, *ostr, *dstr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
224 static Extbyte_dynarr *conversion_out_dynarr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
225 static Extbyte_dynarr *out_dynarr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
226 char tempbuf[1024]; /* some random amount */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
227 struct gcpro gcpro1, gcpro2;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
228 Lisp_Object conv_out_stream, coding_system;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
229 Lstream *costr;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
230 struct gcpro gcpro3;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
232 for (i = (sizeof alphabet) - 1; i >= 0 ; i--)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
233 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
234 inalphabet[alphabet[i]] = 1;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
235 decoder[alphabet[i]] = i;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
238 if (!conversion_out_dynarr)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
239 conversion_out_dynarr = Dynarr_new (Extbyte);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
240 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
241 Dynarr_reset (conversion_out_dynarr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
243 if (!out_dynarr)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
244 out_dynarr = Dynarr_new (Extbyte);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
245 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
246 Dynarr_reset (out_dynarr);
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 char_count = bits = cols = hit_eof = 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
250 /* set up the in stream */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
251 if (BUFFERP (object))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
252 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
253 struct buffer *b = XBUFFER (object);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
254 Charbpos begv, endv;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
255 /* Figure out where we need to get info from */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
256 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
257
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
258 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
259 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
260 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
261 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
262 Bytecount bstart, bend;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
263 CHECK_STRING (object);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
264 get_string_range_byte (object, start, end, &bstart, &bend,
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
265 GB_HISTORICAL_STRING_BEHAVIOR);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
266 instream = make_lisp_string_input_stream (object, bstart, bend);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
267 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
268 istr = XLSTREAM (instream);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
270 /* 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
271 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
272 if (NILP (coding))
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 if (BUFFERP (object))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
275 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
276 /* Use the file coding for this buffer by default */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
277 coding_system = XBUFFER (object)->buffer_file_coding_system;
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 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
280 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
281 /* 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
282 enum eol_type eol = EOL_AUTODETECT;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
283 coding_system = Fget_coding_system (Qundecided);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
284 determine_real_coding_system (istr, &coding_system, &eol);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
285 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
286 if (NILP (coding_system))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
287 coding_system = Fget_coding_system (Qbinary);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
288 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
289 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
290 coding_system = Ffind_coding_system (coding_system);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
291 if (NILP (coding_system))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
292 coding_system = Fget_coding_system (Qbinary);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
293 }
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 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
296 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
297 coding_system = Ffind_coding_system (coding);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
298 if (NILP (coding_system))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
300 if (NILP (error_me_not))
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
301 signal_simple_error ("No such coding system", coding);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
302 else
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
303 coding_system = Fget_coding_system (Qbinary); /* default to binary */
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 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
306
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
307 /* setup the out stream */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
308 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
309 ostr = XLSTREAM (outstream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
310 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
311 dstr = XLSTREAM (deststream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
312 /* setup the conversion stream */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
313 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
314 costr = XLSTREAM (conv_out_stream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
315 GCPRO3 (instream, outstream, conv_out_stream);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
316
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
317 /* Get the data while doing the conversion */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
318 while (1)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
319 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
320 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
321 int l;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
322 if (!size_in_bytes)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
323 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
324 hit_eof = 1;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
325 break;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
326 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
327 /* It does seem the flushes are necessary... */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
328 Lstream_write (costr, tempbuf, size_in_bytes);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
329 Lstream_flush (costr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
330 Lstream_flush (ostr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
331
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
332 /* Update the base64 output buffer */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
333 for (l = 0; l < size_in_bytes; l++)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
334 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
335 if (Dynarr_at (conversion_out_dynarr,l) == '=')
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
336 goto decoder_out;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
337 bits += decoder[Dynarr_at (conversion_out_dynarr,l)];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
338 fprintf (stderr,"%d\n",bits);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
339 char_count++;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
340 if (char_count == 4)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
342 static unsigned char obuf[3];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
343 obuf[0] = (bits >> 16);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
344 obuf[1] = (bits >> 8) & 0xff;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
345 obuf[2] = (bits & 0xff);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
346
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
347 Lstream_write (dstr,obuf,sizeof (obuf));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
348 bits = char_count = 0;
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 else
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
352 bits <<= 6;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 }
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
355 /* reset the dynarr */
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
356 Lstream_rewind (ostr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
357 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
358 decoder_out:
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
359 Lstream_close (istr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
360 Lstream_close (costr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
361 Lstream_close (ostr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
363 if (hit_eof)
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 if (char_count)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
366 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
367 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
368 }
5137
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 switch (char_count)
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
371 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
372 case 1:
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
373 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
374 break;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
375 case 2:
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
376 char_count = bits >> 10;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
377 Lstream_write (dstr,&char_count,sizeof (char_count));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
378 break;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
379 case 3:
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
380 {
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
381 unsigned char buf[2];
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
382 buf[0] = (bits >> 16);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
383 buf[1] = (bits >> 8) & 0xff;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
384 Lstream_write (dstr,buf,sizeof (buf));
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
385 break;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
386 }
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
387 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
389 UNGCPRO;
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
390 Lstream_delete (istr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
391 Lstream_delete (ostr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
392 Lstream_delete (costr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
393 Lstream_flush (dstr);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
394 Lstream_delete (dstr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
396 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
397 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 syms_of_base64 (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 {
5137
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
402 DEFSUBR (Fbase64_encode);
0ac12485616c reindent modules/base64/base64.c
Ben Wing <ben@xemacs.org>
parents: 754
diff changeset
403 DEFSUBR (Fbase64_decode);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 vars_of_base64 (void)
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 Fprovide (intern ("base64"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 }