root/capture-mod/trunk/b64.c

Revision 1779, 19.9 kB (checked in by xkovah, 3 months ago)

forgot b64.* files

  • Property svn:executable set to *
Line 
1 /* /////////////////////////////////////////////////////////////////////////////
2  * File:        b64.c
3  *
4  * Purpose:     Implementation file for the b64 library
5  *
6  * Created:     18th October 2004
7  * Updated:     3rd May 2008
8  *
9  * Home:        http://synesis.com.au/software/
10  *
11  * Copyright (c) 2004-2008, Matthew Wilson and Synesis Software
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are met:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this
18  *   list of conditions and the following disclaimer.
19  * - Redistributions in binary form must reproduce the above copyright notice,
20  *   this list of conditions and the following disclaimer in the documentation
21  *   and/or other materials provided with the distribution.
22  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
23  *   any contributors may be used to endorse or promote products derived from
24  *   this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * ////////////////////////////////////////////////////////////////////////// */
39
40
41 /** \file b64.c Implementation file for the b64 library
42  */
43
44 /* /////////////////////////////////////////////////////////////////////////////
45  * Version information
46  */
47
48 #ifndef B64_DOCUMENTATION_SKIP_SECTION
49 # define B64_VER_C_B64_MAJOR    1
50 # define B64_VER_C_B64_MINOR    2
51 # define B64_VER_C_B64_REVISION 3
52 # define B64_VER_C_B64_EDIT     17
53 #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
54
55 /* /////////////////////////////////////////////////////////////////////////////
56  * Includes
57  */
58
59 #include "b64.h"
60
61 #include <assert.h>
62 #include <string.h>
63
64 /* /////////////////////////////////////////////////////////////////////////////
65  * Constants and definitions
66  */
67
68 #ifndef B64_DOCUMENTATION_SKIP_SECTION
69 # define NUM_PLAIN_DATA_BYTES        (3)
70 # define NUM_ENCODED_DATA_BYTES      (4)
71 #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
72
73 /* /////////////////////////////////////////////////////////////////////////////
74  * Macros
75  */
76
77 #ifndef NUM_ELEMENTS
78 # define NUM_ELEMENTS(x)        (sizeof(x) / sizeof(x[0]))
79 #endif /* !NUM_ELEMENTS */
80
81 /* /////////////////////////////////////////////////////////////////////////////
82  * Warnings
83  */
84
85 #if defined(_MSC_VER) && \
86     _MSC_VER < 1000
87 # pragma warning(disable : 4705)
88 #endif /* _MSC_VER < 1000 */
89
90 /* /////////////////////////////////////////////////////////////////////////////
91  * Data
92  */
93
94 static const char           b64_chars[] =   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
95
96 static const signed char    b64_indexes[]   =   
97 {
98     /* 0 - 31 / 0x00 - 0x1f */
99         -1, -1, -1, -1, -1, -1, -1, -1 
100     ,   -1, -1, -1, -1, -1, -1, -1, -1 
101     ,   -1, -1, -1, -1, -1, -1, -1, -1 
102     ,   -1, -1, -1, -1, -1, -1, -1, -1
103     /* 32 - 63 / 0x20 - 0x3f */
104     ,   -1, -1, -1, -1, -1, -1, -1, -1 
105     ,   -1, -1, -1, 62, -1, -1, -1, 63  /* ... , '+', ... '/' */
106     ,   52, 53, 54, 55, 56, 57, 58, 59  /* '0' - '7' */
107     ,   60, 61, -1, -1, -1, -1, -1, -1  /* '8', '9', ... */
108     /* 64 - 95 / 0x40 - 0x5f */
109     ,   -1, 0,  1,  2,  3,  4,  5,  6   /* ..., 'A' - 'G' */
110     ,   7,  8,  9,  10, 11, 12, 13, 14  /* 'H' - 'O' */
111     ,   15, 16, 17, 18, 19, 20, 21, 22  /* 'P' - 'W' */
112     ,   23, 24, 25, -1, -1, -1, -1, -1  /* 'X', 'Y', 'Z', ... */
113     /* 96 - 127 / 0x60 - 0x7f */
114     ,   -1, 26, 27, 28, 29, 30, 31, 32  /* ..., 'a' - 'g' */
115     ,   33, 34, 35, 36, 37, 38, 39, 40  /* 'h' - 'o' */
116     ,   41, 42, 43, 44, 45, 46, 47, 48  /* 'p' - 'w' */
117     ,   49, 50, 51, -1, -1, -1, -1, -1  /* 'x', 'y', 'z', ... */
118
119     ,   -1, -1, -1, -1, -1, -1, -1, -1 
120     ,   -1, -1, -1, -1, -1, -1, -1, -1 
121     ,   -1, -1, -1, -1, -1, -1, -1, -1 
122     ,   -1, -1, -1, -1, -1, -1, -1, -1 
123
124     ,   -1, -1, -1, -1, -1, -1, -1, -1 
125     ,   -1, -1, -1, -1, -1, -1, -1, -1 
126     ,   -1, -1, -1, -1, -1, -1, -1, -1 
127     ,   -1, -1, -1, -1, -1, -1, -1, -1 
128
129     ,   -1, -1, -1, -1, -1, -1, -1, -1 
130     ,   -1, -1, -1, -1, -1, -1, -1, -1 
131     ,   -1, -1, -1, -1, -1, -1, -1, -1 
132     ,   -1, -1, -1, -1, -1, -1, -1, -1 
133
134     ,   -1, -1, -1, -1, -1, -1, -1, -1 
135     ,   -1, -1, -1, -1, -1, -1, -1, -1 
136     ,   -1, -1, -1, -1, -1, -1, -1, -1 
137     ,   -1, -1, -1, -1, -1, -1, -1, -1 
138 };
139
140 /* /////////////////////////////////////////////////////////////////////////////
141  * Helper functions
142  */
143
144 /** This function reads in 3 bytes at a time, and translates them into 4
145  * characters.
146  */
147 static size_t b64_encode_unsigned char const *src
148                         ,   size_t              srcSize
149                         ,   char *const         dest
150                         ,   size_t              destLen
151                         ,   unsigned            lineLen
152                         ,   B64_RC              *rc)
153 {
154     size_t total = ((srcSize + (NUM_PLAIN_DATA_BYTES - 1)) / NUM_PLAIN_DATA_BYTES) * NUM_ENCODED_DATA_BYTES;
155
156     assert(NULL != rc);
157     *rc = B64_RC_OK;
158
159     if(lineLen > 0)
160     {
161         size_t numLines = (total + (lineLen - 1)) / lineLen;
162
163         total += 2 * (numLines - 1);
164     }
165
166     if(NULL == dest)
167     {
168         return total;
169     }
170     else if(destLen < total)
171     {
172         *rc = B64_RC_INSUFFICIENT_BUFFER;
173
174         return 0;
175     }
176     else
177     {
178         char    *p      =   dest;
179         char    *end    =   dest + destLen;
180         size_t  len     =   0;
181
182         for(; NUM_PLAIN_DATA_BYTES <= srcSize; srcSize -= NUM_PLAIN_DATA_BYTES)
183         {
184             char    characters[NUM_ENCODED_DATA_BYTES];
185
186             /*
187              *
188              * |       0       |       1       |       2       |
189              *
190              * |               |               |               |
191              * |       |       |       |       |       |       |
192              * |   |   |   |   |   |   |   |   |   |   |   |   |
193              * | | | | | | | | | | | | | | | | | | | | | | | | |
194              *
195              * |     0     |     1     |     2     |     3     |
196              *
197              */
198
199             /* characters[0] is the 6 left-most bits of src[0] */
200             characters[0] = (char)((src[0] & 0xfc) >> 2);
201             /* characters[0] is the right-most 2 bits of src[0] and the left-most 4 bits of src[1] */
202             characters[1] = (char)(((src[0] & 0x03) << 4) + ((src[1] & 0xf0) >> 4));
203             /* characters[0] is the right-most 4 bits of src[1] and the 2 left-most bits of src[2] */
204             characters[2] = (char)(((src[1] & 0x0f) << 2) + ((src[2] & 0xc0) >> 6));
205             /* characters[3] is the right-most 6 bits of src[2] */
206             characters[3] = (char)(src[2] & 0x3f);
207
208 #ifndef __WATCOMC__
209             assert(characters[0] >= 0 && characters[0] < 64);
210             assert(characters[1] >= 0 && characters[1] < 64);
211             assert(characters[2] >= 0 && characters[2] < 64);
212             assert(characters[3] >= 0 && characters[3] < 64);
213 #endif /* __WATCOMC__ */
214
215             src += NUM_PLAIN_DATA_BYTES;
216             *p++ = b64_chars[(unsigned char)characters[0]];
217             assert(NULL != strchr(b64_chars, *(p-1)));
218             ++len;
219             assert(len != lineLen);
220
221             *p++ = b64_chars[(unsigned char)characters[1]];
222             assert(NULL != strchr(b64_chars, *(p-1)));
223             ++len;
224             assert(len != lineLen);
225
226             *p++ = b64_chars[(unsigned char)characters[2]];
227             assert(NULL != strchr(b64_chars, *(p-1)));
228             ++len;
229             assert(len != lineLen);
230
231             *p++ = b64_chars[(unsigned char)characters[3]];
232             assert(NULL != strchr(b64_chars, *(p-1)));
233
234             if( ++len == lineLen &&
235                 p != end)
236             {
237                 *p++ = '\r';
238                 *p++ = '\n';
239                 len = 0;
240             }
241         }
242
243         if(0 != srcSize)
244         {
245             /* Deal with the overspill, by boosting it up to three bytes (using 0s)
246              * and then appending '=' for any missing characters.
247              *
248              * This is done into a temporary buffer, so we can call ourselves and
249              * have the output continue to be written direct to the destination.
250              */
251
252             unsigned char   dummy[NUM_PLAIN_DATA_BYTES];
253             size_t          i;
254
255             for(i = 0; i < srcSize; ++i)
256             {
257                 dummy[i] = *src++;
258             }
259
260             for(; i < NUM_PLAIN_DATA_BYTES; ++i)
261             {
262                 dummy[i] = '\0';
263             }
264
265             b64_encode_(&dummy[0], NUM_PLAIN_DATA_BYTES, p, NUM_ENCODED_DATA_BYTES * (1 + 2), 0, rc);
266
267             for(p += 1 + srcSize; srcSize++ < NUM_PLAIN_DATA_BYTES; )
268             {
269                 *p++ = '=';
270             }
271         }
272
273         return total;
274     }
275 }
276
277 /** This function reads in a character string in 4-character chunks, and writes
278  * out the converted form in 3-byte chunks to the destination.
279  */
280 static size_t b64_decode_char const      *src
281                         ,   size_t          srcLen
282                         ,   unsigned char   *dest
283                         ,   size_t          destSize
284                         ,   unsigned        flags
285                         ,   char const      **badChar
286                         ,   B64_RC          *rc)
287 {
288     const size_t    wholeChunks     =   (srcLen / NUM_ENCODED_DATA_BYTES);
289     const size_t    remainderBytes  =   (srcLen % NUM_ENCODED_DATA_BYTES);
290     size_t          maxTotal        =   (wholeChunks + (0 != remainderBytes)) * NUM_PLAIN_DATA_BYTES;
291     unsigned char   *dest_          =   dest;
292
293     ((void)remainderBytes);
294
295     assert(NULL != badChar);
296     assert(NULL != rc);
297
298     *badChar    =   NULL;
299     *rc         =   B64_RC_OK;
300
301     if(NULL == dest)
302     {
303         return maxTotal;
304     }
305     else if(destSize < maxTotal)
306     {
307         *rc = B64_RC_INSUFFICIENT_BUFFER;
308
309         return 0;
310     }
311     else
312     {
313         /* Now we iterate through the src, collecting together four characters
314          * at a time from the Base-64 alphabet, until the end-point is reached.
315          *
316          *
317          */
318
319         char const          *begin      =   src;
320         char const  *const  end         =   begin + srcLen;
321         size_t              currIndex   =   0;
322         size_t              numPads     =   0;
323         signed char         indexes[NUM_ENCODED_DATA_BYTES];    /* 4 */
324
325         for(; begin != end; ++begin)
326         {
327             const char  ch  =   *begin;
328
329             if('=' == ch)
330             {
331                 assert(currIndex < NUM_ENCODED_DATA_BYTES);
332
333                 indexes[currIndex++] = '\0';
334
335                 ++numPads;
336             }
337             else
338             {
339                 /* NOTE: Had to rename 'index' to 'ix', due to name clash with GCC on 64-bit Linux. */
340                 signed char ix   =   b64_indexes[(unsigned char)ch];
341
342                 if(-1 == ix)
343                 {
344                     switch(ch)
345                     {
346                         case    ' ':
347                         case    '\t':
348                         case    '\b':
349                         case    '\v':
350                             if(B64_F_STOP_ON_UNEXPECTED_WS & flags)
351                             {
352                                 *rc         =   B64_RC_DATA_ERROR;
353                                 *badChar    =   begin;
354                                 return 0;
355                             }
356                             else
357                             {
358                                 /* Fall through */
359                             }
360                         case    '\r':
361                         case    '\n':
362                             continue;
363                         default:
364                             if(B64_F_STOP_ON_UNKNOWN_CHAR & flags)
365                             {
366                                 *rc         =   B64_RC_DATA_ERROR;
367                                 *badChar    =   begin;
368                                 return 0;
369                             }
370                             else
371                             {
372                                 continue;
373                             }
374                     }
375                 }
376                 else
377                 {
378                     numPads = 0;
379
380                     assert(currIndex < NUM_ENCODED_DATA_BYTES);
381
382                     indexes[currIndex++] = ix;
383                 }
384             }
385
386             if(NUM_ENCODED_DATA_BYTES == currIndex)
387             {
388                 unsigned char   bytes[NUM_PLAIN_DATA_BYTES];        /* 3 */
389
390                 bytes[0] = (unsigned char)((indexes[0] << 2) + ((indexes[1] & 0x30) >> 4));
391
392                 currIndex = 0;
393
394                 *dest++ = bytes[0];
395                 if(2 != numPads)
396                 {
397                     bytes[1] = (unsigned char)(((indexes[1] & 0xf) << 4) + ((indexes[2] & 0x3c) >> 2));
398
399                     *dest++ = bytes[1];
400
401                     if(1 != numPads)
402                     {
403                         bytes[2] = (unsigned char)(((indexes[2] & 0x3) << 6) + indexes[3]);
404
405                         *dest++ = bytes[2];
406                     }
407                 }
408                 if(0 != numPads)
409                 {
410                     break;
411                 }
412             }
413         }
414
415         return (size_t)(dest - dest_);
416     }
417 }
418
419 /* /////////////////////////////////////////////////////////////////////////////
420  * API functions
421  */
422
423 size_t b64_encode(void const *src, size_t srcSize, char *dest, size_t destLen)
424 {
425     /* Use Null Object (Variable) here for rc, so do not need to check
426      * elsewhere.
427      */
428     B64_RC  rc_;
429
430     return b64_encode_((unsigned char const*)src, srcSize, dest, destLen, 0, &rc_);
431 }
432
433 size_t b64_encode2( void const  *src
434                 ,   size_t      srcSize
435                 ,   char        *dest
436                 ,   size_t      destLen
437                 ,   unsigned    flags
438                 ,   int         lineLen /* = -1 */
439                 ,   B64_RC      *rc     /* = NULL */)
440 {
441     /* Use Null Object (Variable) here for rc, so do not need to check
442      * elsewhere
443      */
444     B64_RC  rc_;
445     if(NULL == rc)
446     {
447         rc = &rc_;
448     }
449
450     switch(B64_F_LINE_LEN_MASK & flags)
451     {
452         case    B64_F_LINE_LEN_USE_PARAM:
453             if(lineLen >= 0)
454             {
455                 break;
456             }
457             /* Fall through to 64 */
458         case    B64_F_LINE_LEN_64:
459             lineLen = 64;
460             break;
461         case    B64_F_LINE_LEN_76:
462             lineLen = 76;
463             break;
464         default:
465             assert(!"Bad line length flag specified to b64_encode2()");
466         case    B64_F_LINE_LEN_INFINITE:
467             lineLen = 0;
468             break;
469     }
470
471     assert(0 == (lineLen % 4));
472
473     return b64_encode_((unsigned char const*)src, srcSize, dest, destLen, (unsigned)lineLen, rc);
474 }
475
476 size_t b64_decode(char const *src, size_t srcLen, void *dest, size_t destSize)
477 {
478     /* Use Null Object (Variable) here for rc and badChar, so do not need to
479      * check elsewhere.
480      */
481     char const  *badChar_;
482     B64_RC      rc_;
483
484     return b64_decode_(src, srcLen, (unsigned char*)dest, destSize, B64_F_STOP_ON_NOTHING, &badChar_, &rc_);
485 }
486
487 size_t b64_decode2( char const  *src
488                 ,   size_t      srcLen
489                 ,   void        *dest
490                 ,   size_t      destSize
491                 ,   unsigned    flags
492                 ,   char const  **badChar   /* = NULL */
493                 ,   B64_RC      *rc         /* = NULL */)
494 {
495     char const      *badChar_;
496     B64_RC          rc_;
497
498     /* Use Null Object (Variable) here for rc and badChar, so do not need to
499      * check elsewhere.
500      */
501     if(NULL == badChar)
502     {
503         badChar = &badChar_;
504     }
505     if(NULL == rc)
506     {
507         rc = &rc_;
508     }
509
510     return b64_decode_(src, srcLen, (unsigned char*)dest, destSize, flags, badChar, rc);
511 }
512
513 /* ////////////////////////////////////////////////////////////////////////// */
514
515 #ifdef B64_DOCUMENTATION_SKIP_SECTION
516 struct b64ErrorString_t_
517 #else /* !B64_DOCUMENTATION_SKIP_SECTION */
518 typedef struct b64ErrorString_t_    b64ErrorString_t_;
519 struct b64ErrorString_t_
520 #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
521 {
522     int         code;   /*!< The error code. */
523     char const  *str;   /*!< The string.        */
524     size_t      len;    /*!< The string length. */
525 };
526
527
528
529 #define SEVERITY_STR_DECL(rc, desc)                                                         \
530                                                                                             \
531     static const char               s_str##rc[] =   desc;                                   \
532     static const b64ErrorString_t_  s_rct##rc = { rc, s_str##rc, NUM_ELEMENTS(s_str##rc) - 1 }
533
534
535 #define SEVERITY_STR_ENTRY(rc)                                                          \
536                                                                                         \
537     &s_rct##rc
538
539
540 static char const *b64_LookupCodeA_(int code, b64ErrorString_t_ const **mappings, size_t cMappings, size_t *len)
541 {
542     /* Use Null Object (Variable) here for len, so do not need to check
543      * elsewhere.
544      */
545     size_t  len_;
546
547     if(NULL == len)
548     {
549         len = &len_;
550     }
551
552     /* Checked, indexed search. */
553     if( code >= 0 &&
554         code < B64_max_RC_value)
555     {
556         if(code == mappings[code]->code)
557         {
558             return (*len = mappings[code]->len, mappings[code]->str);
559         }
560     }
561
562     /* Linear search. Should only be needed if order in
563      * b64_LookupErrorStringA_() messed up.
564      */
565     { size_t i; for(i = 0; i < cMappings; ++i)
566     {
567         if(code == mappings[i]->code)
568         {
569             return (*len = mappings[i]->len, mappings[i]->str);
570         }
571     }}
572
573     return (*len = 0, "");
574 }
575
576 static char const *b64_LookupErrorStringA_(int error, size_t *len)
577 {
578     SEVERITY_STR_DECL(B64_RC_OK                     ,   "Operation was successful"                                              );
579     SEVERITY_STR_DECL(B64_RC_INSUFFICIENT_BUFFER    ,   "The given translation buffer was not of sufficient size"               );
580     SEVERITY_STR_DECL(B64_RC_TRUNCATED_INPUT        ,   "The input did not represent a fully formed stream of octet couplings"  );
581     SEVERITY_STR_DECL(B64_RC_DATA_ERROR             ,   "Invalid data"                                                          );
582
583     static const b64ErrorString_t_    *s_strings[] =
584     {
585         SEVERITY_STR_ENTRY(B64_RC_OK),
586         SEVERITY_STR_ENTRY(B64_RC_INSUFFICIENT_BUFFER),
587         SEVERITY_STR_ENTRY(B64_RC_TRUNCATED_INPUT),
588         SEVERITY_STR_ENTRY(B64_RC_DATA_ERROR),
589     };
590
591     return b64_LookupCodeA_(error, s_strings, NUM_ELEMENTS(s_strings), len);
592 }
593
594 char const *b64_getErrorString(B64_RC code)
595 {
596     return b64_LookupErrorStringA_((int)code, NULL);
597 }
598
599 size_t b64_getErrorStringLength(B64_RC code)
600 {
601     size_t  len;
602
603     return (b64_LookupErrorStringA_((int)code, &len), len);
604 }
605
606 /* ////////////////////////////////////////////////////////////////////////// */
Note: See TracBrowser for help on using the browser.