version 1.12, 2002/08/21 22:45:50
|
version 1.13, 2003/03/06 23:05:28
|
|
|
#include "Base64.h" | #include "Base64.h" |
#include <Pegasus/Common/ArrayInternal.h> | #include <Pegasus/Common/ArrayInternal.h> |
| |
|
#ifdef PEGASUS_OS_OS400 |
|
// OS400 is EBCDIC so it needs to convert string and characters to ASCII |
|
// prior to decode and encode. The encoding and decoding are done in ASCII. |
|
#include <Pegasus/Common/OS400ConvertChar.h> |
|
#endif |
|
|
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
PEGASUS_USING_STD; | PEGASUS_USING_STD; |
| |
|
|
return retArray; | 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 |
| |
|
#ifdef PEGASUS_OS_OS400 |
|
//For OS400 convert from ebcdic to ascii |
|
EtoA((char *)vby.getData(), vby.size()); |
|
#endif |
|
|
for (Uint32 i=0; i < vby.size(); i+=3) | for (Uint32 i=0; i < vby.size(); i+=3) |
{ | { |
| |
|
|
if (i+1<vby.size()) | if (i+1<vby.size()) |
retArray.append( _Encode(by6)); | retArray.append( _Encode(by6)); |
else | else |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#ifdef PEGASUS_OS_OS400 |
|
retArray.append(0x3D); /* ascii '=' */ |
|
#else |
retArray.append('='); | retArray.append('='); |
|
#endif |
|
|
| |
if (i+2<vby.size()) | if (i+2<vby.size()) |
retArray.append( _Encode(by7)); | retArray.append( _Encode(by7)); |
else | else |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#ifdef PEGASUS_OS_OS400 |
|
retArray.append(0x3D); /* ascii '=' */ |
|
#else |
retArray.append('='); | retArray.append('='); |
|
#endif |
| |
/* ATTN: Need to fix this. It adds unwanted cr-lf after 4 chars. | /* ATTN: Need to fix this. It adds unwanted cr-lf after 4 chars. |
| |
|
|
*/ | */ |
}; | }; |
| |
|
#ifdef PEGASUS_OS_OS400 |
|
//For OS400 convert from ascii to ebcdic |
|
AtoE((char *)retArray.getData(), retArray.size()); |
|
#endif |
|
|
return retArray; | 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. |
|
|
*/ | */ |
Array<Uint8> Base64::decode(const Array<Sint8> strInput) | Array<Uint8> Base64::decode(const Array<Sint8> strInput) |
{ | { |
|
#ifdef PEGASUS_OS_OS400 |
|
//For OS400 convert from ebcdic to ascii |
|
EtoA((char *)strInput.getData(), strInput.size()); |
|
#endif |
|
|
//Strip any non-base64 characters from the input | //Strip any non-base64 characters from the input |
PEGASUS_STD(string) str; | PEGASUS_STD(string) str; |
for (Uint32 j=0;j<strInput.size();j++) | for (Uint32 j=0;j<strInput.size();j++) |
|
|
// comment | // comment |
for (size_t i=0; i < str.length();i+=4) | for (size_t i=0; i < str.length();i+=4) |
{ | { |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#ifdef PEGASUS_OS_OS400 |
|
char c1=0x41,c2=0x41,c3=0x41,c4=0x41; /* ascii 'A' */ |
|
#else |
char c1='A',c2='A',c3='A',c4='A'; | char c1='A',c2='A',c3='A',c4='A'; |
|
#endif |
| |
c1 = str[i]; | c1 = str[i]; |
if (i+1<str.length()) | if (i+1<str.length()) |
|
|
retArray.append( (by1<<2)|(by2>>4) ); | retArray.append( (by1<<2)|(by2>>4) ); |
| |
// append second byte if not padding | // append second byte if not padding |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#ifdef PEGASUS_OS_OS400 |
|
if (c3 != 0x3d) /* ascii '=' */ |
|
#else |
if (c3 != '=') | if (c3 != '=') |
|
#endif |
retArray.append( ((by2&0xf)<<4)|(by3>>2) ); | retArray.append( ((by2&0xf)<<4)|(by3>>2) ); |
| |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#ifdef PEGASUS_OS_OS400 |
|
if (c4 != 0x3d) /* ascii '=' */ |
|
#else |
if (c4 != '=') | if (c4 != '=') |
|
#endif |
retArray.append( ((by3&0x3)<<6)|by4 ); | retArray.append( ((by3&0x3)<<6)|by4 ); |
} | } |
| |
|
#ifdef PEGASUS_OS_OS400 |
|
//For OS400 convert from ascii to ebcdic |
|
AtoE((char *)retArray.getData(), retArray.size()); |
|
#endif |
|
|
return retArray; | return retArray; |
}; | }; |
| |
|
|
*/ | */ |
inline PEGASUS_COMMON_LINKAGE char Base64::_Encode(Uint8 uc) | inline PEGASUS_COMMON_LINKAGE char Base64::_Encode(Uint8 uc) |
{ | { |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#if !defined(PEGASUS_OS_OS400) |
if (uc < 26) | if (uc < 26) |
return 'A'+uc; | return 'A'+uc; |
| |
|
|
return '+'; | return '+'; |
| |
return '/'; | return '/'; |
|
#else |
|
if (uc < 26) |
|
return 0x41+uc; /* ascii 'A' */ |
|
|
|
if (uc < 52) |
|
return 0x61+(uc-26); /* ascii 'a' */ |
|
|
|
if (uc < 62) |
|
return 0x30+(uc-52); /* ascii '0' */ |
|
|
|
if (uc == 62) |
|
return 0x2B; /* ascii '+' */ |
|
|
|
return 0x2F; /* ascii '/' */ |
|
#endif |
}; | }; |
| |
//Helper function returns true is a character is a valid base-64 character and false otherwise. | //Helper function returns true is a character is a valid base-64 character and false otherwise. |
|
|
inline Boolean Base64::_IsBase64(char c) | inline Boolean Base64::_IsBase64(char c) |
{ | { |
| |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#if !defined(PEGASUS_OS_OS400) |
if (c >= 'A' && c <= 'Z') | if (c >= 'A' && c <= 'Z') |
return true; | return true; |
| |
|
|
| |
if (c == '=') | if (c == '=') |
return true; | return true; |
|
#else |
|
if (c >= 0x41 && c <= 0x5A) /* ascii 'A' & 'Z' */ |
|
return true; |
|
|
|
if (c >= 0x61 && c <= 0x7A) /* ascii 'a' & 'z' */ |
|
return true; |
|
|
|
if (c >= 0x30 && c <= 0x39) /* ascii '0' & '9' */ |
|
return true; |
|
|
|
if (c == 0x2B) /* ascii '+' */ |
|
return true; |
|
|
|
if (c == 0x2F) /* ascii '/' */ |
|
return true; |
|
|
|
if (c == 0x3D) /* ascii '=' */ |
|
return true; |
|
#endif |
| |
return false; | return false; |
}; | }; |
|
|
// Translate one base-64 character into a six bit pattern | // Translate one base-64 character into a six bit pattern |
inline Uint8 Base64::_Decode(char c) | inline Uint8 Base64::_Decode(char c) |
{ | { |
|
// NOTE: If this code changes the OS400 code below may also need applicable changes |
|
#if !defined(PEGASUS_OS_OS400) |
if (c >= 'A' && c <= 'Z') | if (c >= 'A' && c <= 'Z') |
return c - 'A'; | return c - 'A'; |
if (c >= 'a' && c <= 'z') | if (c >= 'a' && c <= 'z') |
|
|
| |
if (c == '+') | if (c == '+') |
return 62; | return 62; |
|
#else |
|
if (c >= 0x41 && c <= 0x5A) /* ascii 'A' & 'Z' */ |
|
return c - 0x41; /* ascii 'A' */ |
|
if (c >= 0x61 && c <= 0x7A) /* ascii 'a' & 'z' */ |
|
return c - 0x61 + 26; /* ascii 'a' */ |
|
|
|
if (c >= 0x30 && c <= 0x39) /* ascii '0' & '9' */ |
|
return c - 0x30 + 52; /* ascii '0' */ |
|
|
|
if (c == 0x2B) /* ascii '+' */ |
|
return 62; |
|
#endif |
| |
return 63; | return 63; |
}; | }; |