"Fossies" - the Fresh Open Source Software Archive

Member "irods-4.2.8/lib/core/src/base64.cpp" (14 May 2020, 4751 Bytes) of package /linux/misc/irods-4.2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "base64.cpp" see the Fossies "Dox" file reference documentation.

    1 /* base64.cpp - subroutine to do base64 encoding and decoding. The code came from
    2  * SiFang
    3  */
    4 
    5 #include "rodsClient.h"
    6 #include "base64.h"
    7 #define OK 0
    8 
    9 static const char *codes =
   10     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
   11 
   12 static const unsigned char mymap[256] = {
   13     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   14     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   15     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   16     255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
   17     52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
   18     255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
   19     7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
   20     19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
   21     255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
   22     37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
   23     49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   24     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   25     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   26     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   27     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   28     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   29     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   30     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   31     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   32     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   33     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
   34     255, 255, 255, 255
   35 };
   36 
   37 /**
   38    base64 Encode a buffer (NUL terminated)
   39    @param in      The input buffer to encode
   40    @param inlen   The length of the input buffer
   41    @param out     [out] The destination of the base64 encoded data
   42    @param outlen  [in/out] The max size and resulting size
   43    @return OK if successful
   44 */
   45 int base64_encode( const unsigned char *in,  unsigned long inlen,
   46                    unsigned char *out, unsigned long *outlen ) {
   47     unsigned long i, len2, leven;
   48     unsigned char *p;
   49 
   50     /* valid output size ? */
   51     len2 = 4 * ( ( inlen + 2 ) / 3 );
   52     if ( *outlen < len2 + 1 ) {
   53         return BASE64_BUFFER_OVERFLOW;
   54     }
   55     p = out;
   56     leven = 3 * ( inlen / 3 );
   57     for ( i = 0; i < leven; i += 3 ) {
   58         *p++ = codes[( in[0] >> 2 ) & 0x3F];
   59         *p++ = codes[( ( ( in[0] & 3 ) << 4 ) + ( in[1] >> 4 ) ) & 0x3F];
   60         *p++ = codes[( ( ( in[1] & 0xf ) << 2 ) + ( in[2] >> 6 ) ) & 0x3F];
   61         *p++ = codes[in[2] & 0x3F];
   62         in += 3;
   63     }
   64     /* Pad it if necessary...  */
   65     if ( i < inlen ) {
   66         unsigned a = in[0];
   67         unsigned b = ( i + 1 < inlen ) ? in[1] : 0;
   68 
   69         *p++ = codes[( a >> 2 ) & 0x3F];
   70         *p++ = codes[( ( ( a & 3 ) << 4 ) + ( b >> 4 ) ) & 0x3F];
   71         *p++ = ( i + 1 < inlen ) ? codes[( ( ( b & 0xf ) << 2 ) ) & 0x3F] : '=';
   72         *p++ = '=';
   73     }
   74 
   75     /* append a NULL byte */
   76     *p = '\0';
   77 
   78     /* return ok */
   79     *outlen = p - out;
   80     return OK;
   81 }
   82 
   83 /**
   84    base64 decode a block of memory
   85    @param in       The base64 data to decode
   86    @param inlen    The length of the base64 data
   87    @param out      [out] The destination of the binary decoded data
   88    @param outlen   [in/out] The max size and resulting size of the decoded data
   89    @return OK if successful
   90 */
   91 int base64_decode( const unsigned char *in,  unsigned long inlen,
   92                    unsigned char *out, unsigned long *outlen ) {
   93     unsigned long t, x, y, z;
   94     unsigned char c;
   95     int           g;
   96 
   97     g = 3;
   98     for ( x = y = z = t = 0; x < inlen; x++ ) {
   99         c = mymap[in[x] & 0xFF];
  100         if ( c == 255 ) {
  101             continue;
  102         }
  103         /* the final = symbols are read and used to trim the remaining bytes */
  104         if ( c == 254 ) {
  105             c = 0;
  106             /* prevent g < 0 which would potentially allow an overflow later */
  107             if ( --g < 0 ) {
  108                 return BASE64_INVALID_PACKET;
  109             }
  110         }
  111         else if ( g != 3 ) {
  112             /* we only allow = to be at the end */
  113             return BASE64_INVALID_PACKET;
  114         }
  115 
  116         t = ( t << 6 ) | c;
  117 
  118         if ( ++y == 4 ) {
  119             if ( z + g > *outlen ) {
  120                 return BASE64_BUFFER_OVERFLOW;
  121             }
  122             out[z++] = ( unsigned char )( ( t >> 16 ) & 255 );
  123             if ( g > 1 ) {
  124                 out[z++] = ( unsigned char )( ( t >> 8 ) & 255 );
  125             }
  126             if ( g > 2 ) {
  127                 out[z++] = ( unsigned char )( t & 255 );
  128             }
  129             y = t = 0;
  130         }
  131     }
  132     if ( y != 0 ) {
  133         return BASE64_INVALID_PACKET;
  134     }
  135     *outlen = z;
  136     return OK;
  137 }
  138