3 * from http://base64.sourceforge.net/
4 * ********************************************************************\
8 AUTHOR: Bob Trower 08/04/01
10 PROJECT: Crypt Data Packaging
12 COPYRIGHT: Copyright (c) Trantor Standard Systems Inc., 2001
14 NOTE: This source code may be used as you wish, subject to
15 the MIT license. See the LICENCE section below.
18 This little utility implements the Base64
19 Content-Transfer-Encoding standard described in
20 RFC1113 (http://www.faqs.org/rfcs/rfc1113.html).
22 This is the coding scheme used by MIME to allow
23 binary data to be transferred by SMTP mail.
25 Groups of 3 bytes from a binary stream are coded as
26 groups of 4 bytes in a text stream.
28 The input stream is 'padded' with zeros to create
29 an input that is an even multiple of 3.
31 A special character ('=') is used to denote padding so
32 that the stream can be decoded back to its exact size.
34 Encoded output is formatted in lines which should
35 be a maximum of 72 characters to conform to the
36 specification. This program defaults to 72 characters,
37 but will allow more or less through the use of a
38 switch. The program enforces a minimum line size
43 The stream 'ABCD' is 32 bits long. It is mapped as
48 A (65) B (66) C (67) D (68) (None) (None)
49 01000001 01000010 01000011 01000100
51 16 (Q) 20 (U) 9 (J) 3 (D) 17 (R) 0 (A) NA (=) NA (=)
52 010000 010100 001001 000011 010001 000000 000000 000000
57 Decoding is the process in reverse. A 'decode' lookup
58 table has been created to avoid string scans.
60 DESIGN GOALS: Specifically:
61 Code is a stand-alone utility to perform base64
62 encoding/decoding. It should be genuinely useful
63 when the need arises and it meets a need that is
64 likely to occur for some users.
65 Code acts as sample code to show the author's
66 design and coding style.
69 This program is designed to survive:
70 Everything you need is in a single source file.
71 It compiles cleanly using a vanilla ANSI C compiler.
72 It does its job correctly with a minimum of fuss.
73 The code is not overly clever, not overly simplistic
74 and not overly verbose.
75 Access is 'cut and paste' from a web page.
76 Terms of use are reasonable.
78 VALIDATION: Non-trivial code is never without errors. This
79 file likely has some problems, since it has only
80 been tested by the author. It is expected with most
81 source code that there is a period of 'burn-in' when
82 problems are identified and corrected. That being
83 said, it is possible to have 'reasonably correct'
84 code by following a regime of unit test that covers
85 the most likely cases and regression testing prior
86 to release. This has been done with this code and
87 it has a good probability of performing as expected.
93 (Zero length target file created
94 on both encode and decode.)
96 case 1:One input character:
97 CASE1.DAT A -> QQ== -> A
99 case 2:Two input characters:
100 CASE2.DAT AB -> QUJD -> AB
102 case 3:Three input characters:
103 CASE3.DAT ABC -> QUJD -> ABC
105 case 4:Four input characters:
106 case4.dat ABCD -> QUJDRA== -> ABCD
108 case 5:All chars from 0 to ff, LINE_SIZE set to 50:
110 AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj
111 JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH
112 SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr
113 bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P
114 kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz
115 tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX
116 2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7
119 case 6:Mime Block from e-mail:
120 (Data same as test case 5)
123 Tested 28 MB file in/out.
125 case 8: Random Binary Integrity:
126 This binary program (b64.exe) was encoded to base64,
127 back to binary and then executed.
130 All files in a working directory encoded/decoded
131 and compared with file comparison utility to
132 ensure that multiple runs do not cause problems
133 such as exhausting file handles, tmp storage, etc.
137 Syntax, operation and failure:
138 All options/switches tested. Performs as
142 No Args -- Shows Usage Screen
143 Return Code 1 (Invalid Syntax)
145 One Arg (invalid) -- Shows Usage Screen
146 Return Code 1 (Invalid Syntax)
148 One Arg Help (-?) -- Shows detailed Usage Screen.
149 Return Code 0 (Success -- help request is valid).
151 One Arg Help (-h) -- Shows detailed Usage Screen.
152 Return Code 0 (Success -- help request is valid).
154 One Arg (valid) -- Uses stdin/stdout (filter)
155 Return Code 0 (Sucess)
157 Two Args (invalid file) -- shows system error.
158 Return Code 2 (File Error)
160 Encode non-existent file -- shows system error.
161 Return Code 2 (File Error)
163 Out of disk space -- shows system error.
164 Return Code 3 (File I/O Error)
168 Compile/Regression test:
169 gcc compiled binary under Cygwin
170 Microsoft Visual Studio under Windows 2000
171 Microsoft Version 6.0 C under Windows 2000
175 LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
177 Permission is hereby granted, free of charge, to any person
178 obtaining a copy of this software and associated
179 documentation files (the "Software"), to deal in the
180 Software without restriction, including without limitation
181 the rights to use, copy, modify, merge, publish, distribute,
182 sublicense, and/or sell copies of the Software, and to
183 permit persons to whom the Software is furnished to do so,
184 subject to the following conditions:
186 The above copyright notice and this permission notice shall
187 be included in all copies or substantial portions of the
190 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
191 KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
192 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
193 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
194 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
195 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
196 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
197 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
200 Bob Trower 08/04/01 -- Create Version 0.00.00B
202 \******************************************************************* */
207 #include <sysexits.h>
212 ** Translation Table as described in RFC1113
214 static const char cb64
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
217 ** Translation Table to decode (created by author)
219 static const char cd64
[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
224 ** encode 3 8-bit binary bytes as 4 '6-bit' characters
226 void encodeblock( unsigned char in
[3], unsigned char out
[4], int len
)
228 out
[0] = cb64
[ in
[0] >> 2 ];
229 out
[1] = cb64
[ ((in
[0] & 0x03) << 4) | ((in
[1] & 0xf0) >> 4) ];
230 out
[2] = (unsigned char) (len
> 1 ? cb64
[ ((in
[1] & 0x0f) << 2) | ((in
[2] & 0xc0) >> 6) ] : '=');
231 out
[3] = (unsigned char) (len
> 2 ? cb64
[ in
[2] & 0x3f ] : '=');
237 ** decode 4 '6-bit' characters into 3 8-bit binary bytes
239 void decodeblock( unsigned char in
[4], unsigned char out
[3] )
241 out
[ 0 ] = (unsigned char ) (in
[0] << 2 | in
[1] >> 4);
242 out
[ 1 ] = (unsigned char ) (in
[1] << 4 | in
[2] >> 2);
243 out
[ 2 ] = (unsigned char ) (((in
[2] << 6) & 0xc0) | in
[3]);
246 /* Decode base64. Caller frees. */
247 char *b64_decode(char *in
)
250 unsigned int at
, out_at
;
253 /* Get some space for the output. Needs less, but keep it simple. */
254 out
= malloc(strlen(in
));
257 exit(EX_UNAVAILABLE
);
260 memset(out
, 0, strlen(in
));
265 unsigned char in4
[4];
267 /* Read 4 base64-encoded characters */
274 if (isalpha(c
) || isdigit(c
))
276 printf("in4 = %c\n", c
);
280 decodeblock(in4
, (unsigned char *)(out
+ out_at
));
283 } while(at
< strlen(in
));
293 /* TODO: fix this! */
294 printf("|%s|\n", b64_decode("aXQgd29ya3M="));