ACE_Base64 Class Reference

Encode/Decode a stream of bytes according to Base64 encoding. More...

#include <Codecs.h>

List of all members.

Static Public Member Functions

static ACE_Byteencode (const ACE_Byte *input, const size_t input_len, size_t *output_len, bool is_chunked=true)
static ACE_Bytedecode (const ACE_Byte *input, size_t *output_len)
static size_t length (const ACE_Byte *input)

Protected Member Functions

 ACE_Base64 (void)

Private Member Functions

 ACE_Base64 (ACE_Base64 const &)
ACE_Base64operator= (ACE_Base64 const &)

Static Private Member Functions

static void init (void)
 Initialize the tables for encoding/decoding.

Static Private Attributes

static ACE_Byte decoder_ []
 Alphabet used for decoding i.e decoder_[alphabet_[i = 0..63]] = i.
static ACE_Byte member_ []
static bool init_ = false
 Boolean to denote whether initialization is complete.


Detailed Description

Encode/Decode a stream of bytes according to Base64 encoding.

This class provides methods to encode or decode a stream of bytes to/from Base64 encoding. It doesn't convert the input stream to a canonical form before encoding.

Definition at line 46 of file Codecs.h.


Constructor & Destructor Documentation

ACE_Base64::ACE_Base64 ( void   )  [inline, protected]

Definition at line 92 of file Codecs.h.

00092 {}

ACE_Base64::ACE_Base64 ( ACE_Base64 const &   )  [private]


Member Function Documentation

ACE_Byte * ACE_Base64::decode ( const ACE_Byte input,
size_t *  output_len 
) [static]

Decodes a stream of Base64 to bytes data

Parameters:
input Encoded Base64 data in byte stream.
output_len Length of the binary byte stream.
Returns:
Binary data in byte stream or NULL if input data cannot be encoded.

Definition at line 132 of file Codecs.cpp.

References ACE_ERROR, ACE_OS::ace_isspace(), ACE_NEW_RETURN, ACE_TEXT, decoder_, init(), init_, length(), LM_ERROR, member_, and pad.

00133 {
00134   if (!ACE_Base64::init_)
00135     ACE_Base64::init();
00136 
00137   if (!input)
00138     return 0;
00139 
00140   size_t result_len = ACE_Base64::length (input);
00141   ACE_Byte* result = 0;
00142   ACE_NEW_RETURN (result, ACE_Byte[result_len], 0);
00143 
00144   ACE_Byte* ptr = const_cast<ACE_Byte*> (input);
00145   while (*ptr != 0 &&
00146          (member_[*(ptr)] == 1 || *ptr == pad
00147           || ACE_OS::ace_isspace (*ptr)))
00148     ++ptr;
00149   size_t input_len = ptr - input;
00150 
00151   int char_count = 0;
00152   int bits = 0;
00153   size_t pos = 0;
00154 
00155   size_t i = 0;
00156   for (; i < input_len; ++i)
00157     {
00158       if (input[i] == pad)
00159         break;
00160       if (!ACE_Base64::member_[input[i]])
00161         continue;
00162       bits += decoder_[input[i]];
00163       ++char_count;
00164 
00165       if (char_count == 4)
00166         {
00167           result[pos++] = static_cast<ACE_Byte> (bits >> 16);
00168           result[pos++] = static_cast<ACE_Byte> ((bits >> 8) & 0xff);
00169           result[pos++] = static_cast<ACE_Byte> (bits & 0xff);
00170           bits = 0;
00171           char_count = 0;
00172         }
00173       else
00174         {
00175           bits <<= 6;
00176         }
00177     }
00178 
00179   int errors = 0;
00180   if ( i == input_len)
00181     {
00182       if (char_count)
00183         {
00184           ACE_ERROR ((LM_ERROR,
00185                       ACE_TEXT ("Decoding incomplete: atleast %d bits truncated\n"),
00186                       (4 - char_count) * 6));
00187           ++errors;
00188         }
00189     }
00190   else
00191     {
00192       switch (char_count)
00193         {
00194         case 1:
00195           ACE_ERROR ((LM_ERROR,
00196                       ACE_TEXT ("Decoding incomplete: atleast 2 bits missing\n")));
00197           ++errors;
00198           break;
00199         case 2:
00200           result[pos++] = static_cast<ACE_Byte> (bits >> 10);
00201           break;
00202         case 3:
00203           result[pos++] = static_cast<ACE_Byte> (bits >> 16);
00204           result[pos++] = static_cast<ACE_Byte> ((bits >> 8) & 0xff);
00205           break;
00206         }
00207     }
00208 
00209   if (errors)
00210     {
00211       delete[] result;
00212       return 0;
00213     }
00214   result[pos] = 0;
00215   *output_len = pos;
00216   return result;
00217 }

ACE_Byte * ACE_Base64::encode ( const ACE_Byte input,
const size_t  input_len,
size_t *  output_len,
bool  is_chunked = true 
) [static]

Encodes a stream of bytes to Base64 data

Parameters:
input Binary data in byte stream.
input_len Length of the byte stream.
output_len Length of the encoded Base64 byte stream.
is_chunked If true, terminate 72 character blocks with newline
Returns:
Encoded Base64 data in byte stream or NULL if input data cannot be encoded.

Definition at line 38 of file Codecs.cpp.

References ACE_NEW_RETURN, alphabet, init(), init_, length(), max_columns, and pad.

00042 {
00043   if (!ACE_Base64::init_)
00044     ACE_Base64::init();
00045 
00046   if (!input)
00047     return 0;
00048 
00049   ACE_Byte* result = 0;
00050 
00051   size_t length = ((input_len + 2) / 3) * 4;
00052   size_t num_lines = length / max_columns + 1;
00053   length += num_lines + 1;
00054   ACE_NEW_RETURN (result, ACE_Byte[length], 0);
00055 
00056   int char_count = 0;
00057   int bits = 0;
00058   size_t pos = 0;
00059   int cols = 0;
00060 
00061   for (size_t i = 0; i < input_len; ++i)
00062     {
00063       bits += input[i];
00064       ++char_count;
00065 
00066       if (char_count == 3)
00067         {
00068           result[pos++] = alphabet[bits >> 18];
00069           result[pos++] = alphabet[(bits >> 12) & 0x3f];
00070           result[pos++] = alphabet[(bits >> 6) & 0x3f];
00071           result[pos++] = alphabet[bits & 0x3f];
00072           cols += 4;
00073           if (cols == max_columns) {
00074             if (is_chunked) 
00075               result[pos++] = '\n';
00076             cols = 0;
00077           }
00078           bits = 0;
00079           char_count = 0;
00080         }
00081       else
00082         {
00083           bits <<= 8;
00084         }
00085     }
00086 
00087   if (char_count != 0)
00088     {
00089       bits <<= (16 - (8 * char_count));
00090       result[pos++] = alphabet[bits >> 18];
00091       result[pos++] = alphabet[(bits >> 12) & 0x3f];
00092       cols += 2;
00093       if (char_count == 1)
00094         {
00095           result[pos++] = pad;
00096           result[pos++] = pad;
00097           cols += 2;
00098         }
00099       else
00100         {
00101           result[pos++] = alphabet[(bits >> 6) & 0x3f];
00102           result[pos++] = pad;
00103           cols += 2;
00104         }
00105     }
00106 
00107   if (cols > 0 && is_chunked)
00108     result[pos++] = '\n';
00109 
00110   result[pos] = 0;
00111   *output_len = pos;
00112   return result;
00113 }

void ACE_Base64::init ( void   )  [static, private]

Initialize the tables for encoding/decoding.

Definition at line 220 of file Codecs.cpp.

References alphabet, decoder_, init_, and member_.

Referenced by decode(), encode(), and length().

00221 {
00222   if (!ACE_Base64::init_)
00223     {
00224       for (ACE_Byte i = 0; i < sizeof (alphabet); ++i)
00225         {
00226           ACE_Base64::decoder_[alphabet[i]] = i;
00227           ACE_Base64::member_ [alphabet[i]] = 1;
00228         }
00229       ACE_Base64::init_ = true;
00230     }
00231   return;
00232 }

size_t ACE_Base64::length ( const ACE_Byte input  )  [static]

Return the length of the encoded input data

Parameters:
input Encoded Base64 data in byte stream.
Returns:
Length of the encoded Base64 data.

Definition at line 116 of file Codecs.cpp.

References ACE_OS::ace_isspace(), init(), init_, member_, and pad.

Referenced by decode(), and encode().

00117 {
00118   if (!ACE_Base64::init_)
00119     ACE_Base64::init();
00120 
00121   ACE_Byte* ptr = const_cast<ACE_Byte*> (input);
00122   while (*ptr != 0 &&
00123          (member_[*(ptr)] == 1 || *ptr == pad
00124           || ACE_OS::ace_isspace (*ptr)))
00125     ++ptr;
00126   size_t len = ptr - input;
00127   len = ((len + 3) / 4) * 3 + 1 ;
00128   return len;
00129 }

ACE_Base64& ACE_Base64::operator= ( ACE_Base64 const &   )  [private]


Member Data Documentation

ACE_Byte ACE_Base64::decoder_ [static, private]

Alphabet used for decoding i.e decoder_[alphabet_[i = 0..63]] = i.

Definition at line 106 of file Codecs.h.

Referenced by decode(), and init().

ACE_BEGIN_VERSIONED_NAMESPACE_DECL bool ACE_Base64::init_ = false [static, private]

Boolean to denote whether initialization is complete.

Definition at line 113 of file Codecs.h.

Referenced by decode(), encode(), init(), and length().

ACE_Byte ACE_Base64::member_ [static, private]

Alphabet used to check valid range of encoded input i.e member_[alphabet_[0..63]] = 1

Definition at line 110 of file Codecs.h.

Referenced by decode(), init(), and length().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:34:56 2010 for ACE by  doxygen 1.4.7