version 1.1, 2002/02/07 15:05:54
|
version 1.2, 2002/02/07 15:21:57
|
|
|
//************************************************************* | //************************************************************* |
/* Encode static method takes an array of 8-bit values and | /* Encode static method takes an array of 8-bit values and |
returns a base-64 stream. | returns a base-64 stream. |
|
ATTN: KS feb 2002 - This is probably a very slow an inefficient |
|
implementation and could be improved if it is required for |
|
production. Today it is only for test programs. |
*/ | */ |
inline Array<Sint8> Base64::encode(const Array<Uint8>& vby) | inline Array<Sint8> Base64::encode(const Array<Uint8>& vby) |
{ | { |
Array<Sint8> retval; |
Array<Sint8> retArray; |
// set retval = zero |
|
// If nothing in input string, return empty string | // If nothing in input string, return empty string |
if (vby.size() == 0) | if (vby.size() == 0) |
return retval; |
return retArray; |
// for every character in the input array taken 3 bytes at a time | // for every character in the input array taken 3 bytes at a time |
| |
for (Uint32 i=0; i < vby.size(); i+=3) | for (Uint32 i=0; i < vby.size(); i+=3) |
|
|
by6 = ((by2&0xf)<<2)|(by3>>6); | by6 = ((by2&0xf)<<2)|(by3>>6); |
by7 = by3&0x3f; | by7 = by3&0x3f; |
| |
retval.append(_Encode(by4)); |
retArray.append(_Encode(by4)); |
retval.append(_Encode(by5)); |
retArray.append(_Encode(by5)); |
| |
if (i+1<vby.size()) | if (i+1<vby.size()) |
retval.append( _Encode(by6)); |
retArray.append( _Encode(by6)); |
else | else |
retval.append('='); |
retArray.append('='); |
| |
if (i+2<vby.size()) | if (i+2<vby.size()) |
retval.append( _Encode(by7)); |
retArray.append( _Encode(by7)); |
else | else |
retval.append('='); |
retArray.append('='); |
| |
if (i % (76/4*3) == 0) | if (i % (76/4*3) == 0) |
{ | { |
retval.append( '\r'); |
retArray.append( '\r'); |
retval.append( '\n'); |
retArray.append( '\n'); |
} | } |
}; | }; |
| |
return retval; |
return retArray; |
}; | }; |
/*I checked for the zero length. The algorithm would also work for zero length input stream, but I’m pretty adamant about handling border conditions. They are often the culprits of run-time production failures. | /*I checked for the zero length. The algorithm would also work for zero length input stream, but I’m pretty adamant about handling border conditions. They are often the culprits of run-time production failures. |
The algorithm goes thru each three bytes of data at a time. The first thing I do is to shift the bits around from three 8-bit values to four 6-bit values. Then I encode the 6-bit values and add then one at a time to the output stream. This is actually quite inefficient. The STL character array is being allocated one byte at a time. The algorithm would be much faster, if I pre-allocated that array. I’ll leave that as an optimization practical exercise for the reader. | The algorithm goes thru each three bytes of data at a time. The first thing I do is to shift the bits around from three 8-bit values to four 6-bit values. Then I encode the 6-bit values and add then one at a time to the output stream. This is actually quite inefficient. The STL character array is being allocated one byte at a time. The algorithm would be much faster, if I pre-allocated that array. I’ll leave that as an optimization practical exercise for the reader. |
|
|
if (_IsBase64(strInput[j])) | if (_IsBase64(strInput[j])) |
str += strInput[j]; | str += strInput[j]; |
} | } |
//cout << "base64::decode String = !" << strInput << "! Len = " |
|
// << strInput.size() << endl; |
Array<Uint8> retArray; |
//cout << "base64::decode stripped = !" << str.data() << "! Len = " |
|
// << str.length() << endl; |
|
Array<Uint8> retval; |
|
| |
// Return if the input is zero length | // Return if the input is zero length |
if (str.length() == 0) | if (str.length() == 0) |
return retval; |
return retArray; |
| |
// comment | // comment |
for (int i=0; i < str.length();i+=4) | for (int i=0; i < str.length();i+=4) |
|
|
// " 4 " << c4 << " " << by4 << endl; | // " 4 " << c4 << " " << by4 << endl; |
| |
// append first byte by shifting | // append first byte by shifting |
retval.append( (by1<<2)|(by2>>4) ); |
retArray.append( (by1<<2)|(by2>>4) ); |
| |
// append second byte if not padding | // append second byte if not padding |
if (c3 != '=') | if (c3 != '=') |
retval.append( ((by2&0xf)<<4)|(by3>>2) ); |
retArray.append( ((by2&0xf)<<4)|(by3>>2) ); |
| |
if (c4 != '=') | if (c4 != '=') |
retval.append( ((by3&0x3)<<6)|by4 ); |
retArray.append( ((by3&0x3)<<6)|by4 ); |
} | } |
| |
return retval; |
return retArray; |
}; | }; |
| |
//********************************************************** | //********************************************************** |