Encode 7-Bit ASCII in the input chain to Base64 in the output chain. Returns: None
| Return Type | Function name | Arguments |
|---|---|---|
| void | Base64Encode | (hzChain&,hzString&,) |
Declared in file: hzCodec.h
Defined in file : hzCodec.cpp
Function Logic:
Function body:
void Base64Encode (hzChain& Encoded)hzString& 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(hzString)") ;
const char* 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
Encoded.Clear() ;
if (!Raw)
return ;
for (r = *Raw ; *r ; r++)
{
// Fill the 3 bytes of source
a = b = c = bits = 0;
a = *r ;
r++ ;
bits++ ;
if (*r)
{
b = *r ;
r++ ;
bits++ ;
if (*r)
{
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;
}
}
}