1 /*!
2  * \file l4util/lib/src/base64.c
3  * \ingroup utils
4  *
5  * \date    04/26/2002
6  * \author  Joerg Nothnagel <jn6@os.inf.tu-dresden.de>
7  */
8 
9 //code adapted from b64.c
10 
11 /*********************************************************************\
12 
13 MODULE NAME:    b64.c
14 
15 AUTHOR:         Bob Trower 08/04/01
16 
17 PROJECT:        Crypt Data Packaging
18 
19 COPYRIGHT:      Copyright (c) Trantor Standard Systems Inc., 2001
20 
21 NOTE:           This source code may be used as you wish, subject to
22                 the MIT license.  See the LICENCE section below.
23 
24 DESCRIPTION:
25                 This little utility implements the Base64
26                 Content-Transfer-Encoding standard described in
27                 RFC1113 (http://www.faqs.org/rfcs/rfc1113.html).
28 
29                 This is the coding scheme used by MIME to allow
30                 binary data to be transferred by SMTP mail.
31 
32                 Groups of 3 bytes from a binary stream are coded as
33                 groups of 4 bytes in a text stream.
34 
35                 The input stream is 'padded' with zeros to create
36                 an input that is an even multiple of 3.
37 
38                 A special character ('=') is used to denote padding so
39                 that the stream can be decoded back to its exact size.
40 
41                 Encoded output is formatted in lines which should
42                 be a maximum of 72 characters to conform to the
43                 specification.  This program defaults to 72 characters,
44                 but will allow more or less through the use of a
45                 switch.  The program enforces a minimum line size
46                 of 4 characters.
47 
48                 Example encoding:
49 
50                 The stream 'ABCD' is 32 bits long.  It is mapped as
51                 follows:
52 
53                 ABCD
54 
55                  A (65)     B (66)     C (67)     D (68)   (None) (None)
56                 01000001   01000010   01000011   01000100
57 
58                 16 (Q)  20 (U)  9 (J)   3 (D)    17 (R) 0 (A)  NA (=) NA (=)
59                 010000  010100  001001  000011   010001 000000 000000 000000
60 
61 
62                 QUJDRA==
63 
64                 Decoding is the process in reverse.  A 'decode' lookup
65                 table has been created to avoid string scans.
66 
67 DESIGN GOALS:	Specifically:
68 		Code is a stand-alone utility to perform base64
69 		encoding/decoding. It should be genuinely useful
70 		when the need arises and it meets a need that is
71 		likely to occur for some users.
72 		Code acts as sample code to show the author's
73 		design and coding style.
74 
75 		Generally:
76 		This program is designed to survive:
77 		Everything you need is in a single source file.
78 		It compiles cleanly using a vanilla ANSI C compiler.
79 		It does its job correctly with a minimum of fuss.
80 		The code is not overly clever, not overly simplistic
81 		and not overly verbose.
82 		Access is 'cut and paste' from a web page.
83 		Terms of use are reasonable.
84 
85 VALIDATION:     Non-trivial code is never without errors.  This
86                 file likely has some problems, since it has only
87                 been tested by the author.  It is expected with most
88                 source code that there is a period of 'burn-in' when
89                 problems are identified and corrected.  That being
90                 said, it is possible to have 'reasonably correct'
91                 code by following a regime of unit test that covers
92                 the most likely cases and regression testing prior
93                 to release.  This has been done with this code and
94                 it has a good probability of performing as expected.
95 
96                 Unit Test Cases:
97 
98                 case 0:empty file:
99                     CASE0.DAT  ->  ->
100                     (Zero length target file created
101                     on both encode and decode.)
102 
103                 case 1:One input character:
104                     CASE1.DAT A -> QQ== -> A
105 
106                 case 2:Two input characters:
107                     CASE2.DAT AB -> QUJD -> AB
108 
109                 case 3:Three input characters:
110                     CASE3.DAT ABC -> QUJD -> ABC
111 
112                 case 4:Four input characters:
113                     case4.dat ABCD -> QUJDRA== -> ABCD
114 
115                 case 5:All chars from 0 to ff, linesize set to 50:
116 
117                     AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj
118                     JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH
119                     SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr
120                     bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P
121                     kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz
122                     tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX
123                     2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7
124                     /P3+/w==
125 
126                 case 6:Mime Block from e-mail:
127                     (Data same as test case 5)
128 
129                 case 7: Large files:
130                     Tested 28 MB file in/out.
131 
132                 case 8: Random Binary Integrity:
133                     This binary program (b64.exe) was encoded to base64,
134                     back to binary and then executed.
135 
136                 case 9 Stress:
137                     All files in a working directory encoded/decoded
138                     and compared with file comparison utility to
139                     ensure that multiple runs do not cause problems
140                     such as exhausting file handles, tmp storage, etc.
141 
142                 -------------
143 
144                 Syntax, operation and failure:
145                     All options/switches tested.  Performs as
146                     expected.
147 
148                 case 10:
149                     No Args -- Shows Usage Screen
150                     Return Code 1 (Invalid Syntax)
151                 case 11:
152                     One Arg (invalid) -- Shows Usage Screen
153                     Return Code 1 (Invalid Syntax)
154                 case 12:
155                     One Arg Help (-?) -- Shows detailed Usage Screen.
156                     Return Code 0 (Success -- help request is valid).
157                 case 13:
158                     One Arg Help (-h) -- Shows detailed Usage Screen.
159                     Return Code 0 (Success -- help request is valid).
160                 case 14:
161                     One Arg (valid) -- Uses stdin/stdout (filter)
162                     Return Code 0 (Sucess)
163                 case 15:
164                     Two Args (invalid file) -- shows system error.
165                     Return Code 2 (File Error)
166                 case 16:
167                     Encode non-existent file -- shows system error.
168                     Return Code 2 (File Error)
169                 case 17:
170                     Out of disk space -- shows system error.
171                     Return Code 3 (File I/O Error)
172 
173                 -------------
174 
175                 Compile/Regression test:
176                     gcc compiled binary under Cygwin
177                     Microsoft Visual Studio under Windows 2000
178                     Microsoft Version 6.0 C under Windows 2000
179 
180 DEPENDENCIES:   None
181 
182 LICENCE:        Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
183 
184                 Permission is hereby granted, free of charge, to any person
185                 obtaining a copy of this software and associated
186                 documentation files (the "Software"), to deal in the
187                 Software without restriction, including without limitation
188                 the rights to use, copy, modify, merge, publish, distribute,
189                 sublicense, and/or sell copies of the Software, and to
190                 permit persons to whom the Software is furnished to do so,
191                 subject to the following conditions:
192 
193                 The above copyright notice and this permission notice shall
194                 be included in all copies or substantial portions of the
195                 Software.
196 
197                 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
198                 KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
199                 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
200                 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
201                 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
202                 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
203                 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
204                 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
205 
206 VERSION HISTORY:
207                 Bob Trower 08/04/01 -- Create Version 0.00.00B
208 
209 \******************************************************************* */
210 
211 
212 // local includes
213 
214 #include <l4/util/base64.h> // we implement these functions
215 
216 // global includes
217 
218 #include <stdlib.h>
219 
220 // private variables
221 
222 /*!
223  * \ingroup utils_internal
224  * Translation Table as described in RFC1113
225  */
226 static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
227 
228 /*!
229  * \ingroup utils_internal
230  * Translation Table to decode (created by Bob Trower)
231  */
232 static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
233 
234 //private functions
235 
236 /*!
237  * \brief encode 3 8-bit binary bytes as 4 '6-bit' characters
238  * \ingroup utils_internal
239  *
240  *
241  * \param in array of bytes to be encoded
242  * \param len real number of bytes to encode
243  * \retval out encoded representation of \a in
244  * encode 3 8-bit binary bytes as 4 '6-bit' characters
245  */
246 static void base64_encodeblock(unsigned char in[3], unsigned char out[4], int len);
247 
248 /*!
249  * \brief decode 4 '6-bit' characters into 3 8-bit binary bytes
250  * \ingroup utils_internal
251  *
252  *
253  * \param in array of bytes to be decoded
254  * \retval out decoded representation of \a in
255  * decode 4 '6-bit' characters into 3 8-bit binary bytes
256  */
257 static void base64_decodeblock(unsigned char in[4], unsigned char out[3]);
258 
259 
260 // implementation of public functions
261 
base64_encode(const char * infile,unsigned int in_size,char ** outfile)262 L4_CV void base64_encode( const char *infile, unsigned int in_size, char **outfile)
263 {
264   unsigned char in[3], out[4];
265   int i, len = 0;
266   unsigned int in_count=0, out_count=0;
267   char *temp=malloc(in_size*2);//to be on the safe side;
268   if (!temp)
269     {
270       *outfile = NULL;
271       return;
272     }
273 
274   while(in_count<in_size)
275     {
276       len = 0;
277       for( i = 0; i < 3; i++ )
278 	{
279 	  if(in_count<in_size)
280 	    {
281 	      in[i] = (unsigned char) infile[in_count++];
282 	      len++;
283 	    }
284 	  else
285 	    {
286 	      in[i] = 0;
287 	    }
288 	}
289       if( len )
290 	{
291 	  base64_encodeblock( in, out, len );
292 	  for( i = 0; i < 4; i++ )
293 	    {
294 	      temp[out_count++]=out[i];
295 	    }
296 	}
297     }
298   temp[out_count]=0; //null-terminate string
299   *outfile=temp;
300 }
301 
base64_decode(const char * infile,unsigned int in_size,char ** outfile)302 L4_CV void base64_decode( const char*infile, unsigned int in_size, char **outfile )
303 {
304   unsigned char in[4], out[3], v;
305   int i, len;
306   unsigned int in_count=0, out_count=0;
307   char *temp =malloc(in_size); //to be on the safe side;
308   if (!temp)
309     {
310       *outfile = NULL;
311       return;
312     }
313 
314   while( in_count<in_size)
315     {
316       for( len = 0, i = 0; i < 4 && in_count<in_size; i++ )
317 	{
318 	  v = 0;
319 	  while( in_count<in_size && v == 0 )
320 	    {
321 	      v = (unsigned char) infile[in_count++];
322 	      v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
323 	      if( v )
324 		{
325 		  v = (unsigned char) ((v == '$') ? 0 : v - 61);
326 		}
327 	    }
328 	  if( in_count<in_size)
329 	    {
330 	      len++;
331 	      if( v )
332 		{
333 		  in[ i ] = (unsigned char) (v - 1);
334 		}
335 	    }
336 	  else
337 	    {
338 	      in[i] = 0;
339 	    }
340 	}
341       if( len )
342 	{
343 	  base64_decodeblock( in, out );
344 	  for( i = 0; i < len - 1; i++ )
345 	    {
346 	      temp[out_count++]=out[i];
347 	    }
348 	}
349     }
350   temp[out_count]=0;
351   *outfile=temp;
352 }
353 
354 
355 // implementation of private functions
356 
base64_encodeblock(unsigned char in[3],unsigned char out[4],int len)357 void base64_encodeblock( unsigned char in[3], unsigned char out[4], int len )
358 {
359   out[0] = cb64[ in[0] >> 2 ];
360   out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
361   out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
362   out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
363 }
364 
base64_decodeblock(unsigned char in[4],unsigned char out[3])365 static void base64_decodeblock( unsigned char in[4], unsigned char out[3] )
366 {
367   out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
368   out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
369   out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
370 }
371 
372