Encode 7-Bit ASCII in the input chain to Base64 in the output chain. Returns: None

Return TypeFunction nameArguments
voidBase64Encode(hzChain&,hzChain&,)

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

Function Logic:

0:START 1:unknown 2:bits 3:c 4:b 5:a a items items 6:unknown 7:b items items 8:unknown 9:c items D C 10:B 11:A a A a B b B b C c C c D items items items items buf Encoded items 12:unknown 13:Encoded nSeg 14: No text

Function body:

void Base64Encode (hzChain& Encoded)hzChain& Raw, 
{
   //  Category: Codec
   //  
   //  Encode 7-Bit ASCII in the input chain to Base64 in the output chain.
   //  
   //  Arguments: 1) Encoded The chain to contain the encoded data
   //     2) Raw  The chain containing the 7-Bit ASCII data to encode
   //  
   //  Returns: None
   _hzfunc("Base64Encode(hzChain)") ;
   chIter      r ;             //  Raw input chain iterator
   int32_t     A ;             //  A is top 6 bits of byte 1 of 3 shifted down by 2
   int32_t     B ;             //  B is low 2 bits of byte 2 of 3 shifted up by 4 and top 4 bits of 'b' shifted down by 4
   int32_t     C ;             //  C is low 4 bits of byte 3 of 3 shifted up by 2 and top 2 bits of 'c' shifted down by 6
   int32_t     D ;             //  D is low 6 bits of byte 3
   int32_t     nSeg = 0;       //  Newline marker
   uchar       a ;             //  1st byte of 3-byte segment
   uchar       b ;             //  2nd byte of 3-byte segment
   uchar       c ;             //  3rd byte of 3-byte segment
   uchar       bits ;          //  Count of bits in segment (at end can be less than 3)
   char        buf [8];        //  Export encoded set of 4 bytes
   for (r = Raw ; !r.eof() ; r++)
   {
       //  Fill the 3 bytes of source
       a = b = c = bits = 0;
       a = *r ;
       r++ ;
       bits++ ;
       if (!r.eof())
       {
           b = *r ;
           r++ ;
           bits++ ;
           if (!r.eof())
           {
               c = *r ;
               bits++ ;
           }
       }
       //  Then convert to 4 bytes.
       A  = B = C = D = 0;
       A  = ((a & 0xfc)>>2);
       B  = ((a & 0x03)<<4);
       B |= ((b & 0xf0)>>4);
       C  = ((b & 0x0f)<<2);
       C |= ((c & 0xc0)>>6);
       D  = c & 0x3f;
       buf[0]= bits ? s_base64_array[A] : ''='';
       buf[1]= bits ? s_base64_array[B] : ''='';
       buf[2]= bits > 1? s_base64_array[C] : ''='';
       buf[3]= bits > 2? s_base64_array[D] : ''='';
       buf[4]= 0;
       Encoded += buf ;
       nSeg++ ;
       if (nSeg == 19)
       {
           Encoded += "\r\n" ;
           nSeg = 0;
       }
   }
}