Decode a Base64 string to a 7-Bit ASCII string

Return TypeFunction nameArguments
hzEcodeBase64Decode(hzChain&,const char*,)

Declared in file: hzCodec.h
Defined in file : hzCodec.cpp

Function Logic:

0:START 1:items nBytes 2:unknown 3:Return hzerr(E_FORMAT,Supplied string is not an exact multiple of 4 bytes in length) 4:unknown 5:a items b items c items d items 6:unknown 7:Return E_FORMAT 8:val 9:unknown 10:val 11:unknown 12:val 13:unknown 14:val 15:unknown 16:val 17:items items val c items 18:unknown 19:items 20:unknown 21:items 22:unknown 23:items 24:unknown 25:items 26:Return E_OK

Function body:

hzEcode Base64Decode (hzChain& Decoded)const char* s, 
{
   //  Category: Codec
   //  
   //  Decode a Base64 string to a 7-Bit ASCII string
   //  
   //  Arguments: 1) Decoded The decoded string (in hzChain)
   //     2) s  The Base64 data to decode (as char string)
   //  
   //  Returns: Instance of hzString by value being the decoded data
   _hzfunc("Base64Decode(cstr)") ;
   const char* i ;         //  Cstr iterator
   int32_t     nBytes ;    //  Length of input string
   int32_t     val ;       //  Holds 24 bit value to be written as 3 bytes
   int32_t     a ;         //  1st value
   int32_t     b ;         //  2nd value
   int32_t     c ;         //  3rd value
   int32_t     d ;         //  4th value
   //  Clear the supplied result chain
   Decoded.Clear() ;
   //  Not valid unless the string is an exact multiple of 4 bytes
   nBytes = strlen(s) ;
   if (nBytes % 4)
       return hzerr(E_FORMAT, "Supplied string is not an exact multiple of 4 bytes in length") ;
   //  Loop through collecting sets of 4 chars and writing out sets of 3 chars
   for (i = s ; *i ;)
   {
       a = _get6bit(*i) ; i++ ;
       b = _get6bit(*i) ; i++ ;
       c = _get6bit(*i) ; i++ ;
       d = _get6bit(*i) ; i++ ;
       if (a == 101||b== 101||c== 101||d== 101)
           return E_FORMAT ;
       val = 0;
       if (a < 100)val += (a << 18);
       if (b < 100)val += (b << 12);
       if (c < 100)val += (c << 6);
       if (d < 100)val += d ;
       a = (val & 0xff0000)>>16;
       b = (val & 0xff00)>>8;
       c = val & 0xff;
       Decoded.AddByte(a) ;
       if (b)
           Decoded.AddByte(b) ;
       if (c)
           Decoded.AddByte(c) ;
       if (*i == CHAR_NL)  i++ ;
       if (*i == CHAR_CR)  i++ ;
   }
   return E_OK ;
}