Protected Member Functions | Private Member Functions | Static Private Member Functions | Static Private Attributes

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.

{}

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.

{
  if (!ACE_Base64::init_)
    ACE_Base64::init();

  if (!input)
    return 0;

  size_t result_len = ACE_Base64::length (input);
  ACE_Byte* result = 0;
  ACE_NEW_RETURN (result, ACE_Byte[result_len], 0);

  ACE_Byte* ptr = const_cast<ACE_Byte*> (input);
  while (*ptr != 0 &&
         (member_[*(ptr)] == 1 || *ptr == pad
          || ACE_OS::ace_isspace (*ptr)))
    ++ptr;
  size_t input_len = ptr - input;

  int char_count = 0;
  int bits = 0;
  size_t pos = 0;

  size_t i = 0;
  for (; i < input_len; ++i)
    {
      if (input[i] == pad)
        break;
      if (!ACE_Base64::member_[input[i]])
        continue;
      bits += decoder_[input[i]];
      ++char_count;

      if (char_count == 4)
        {
          result[pos++] = static_cast<ACE_Byte> (bits >> 16);
          result[pos++] = static_cast<ACE_Byte> ((bits >> 8) & 0xff);
          result[pos++] = static_cast<ACE_Byte> (bits & 0xff);
          bits = 0;
          char_count = 0;
        }
      else
        {
          bits <<= 6;
        }
    }

  int errors = 0;
  if ( i == input_len)
    {
      if (char_count)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Decoding incomplete: atleast %d bits truncated\n"),
                      (4 - char_count) * 6));
          ++errors;
        }
    }
  else
    {
      switch (char_count)
        {
        case 1:
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Decoding incomplete: atleast 2 bits missing\n")));
          ++errors;
          break;
        case 2:
          result[pos++] = static_cast<ACE_Byte> (bits >> 10);
          break;
        case 3:
          result[pos++] = static_cast<ACE_Byte> (bits >> 16);
          result[pos++] = static_cast<ACE_Byte> ((bits >> 8) & 0xff);
          break;
        }
    }

  if (errors)
    {
      delete[] result;
      return 0;
    }
  result[pos] = 0;
  *output_len = pos;
  return result;
}

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.

{
  if (!ACE_Base64::init_)
    ACE_Base64::init();

  if (!input)
    return 0;

  ACE_Byte* result = 0;

  size_t length = ((input_len + 2) / 3) * 4;
  size_t num_lines = length / max_columns + 1;
  length += num_lines + 1;
  ACE_NEW_RETURN (result, ACE_Byte[length], 0);

  int char_count = 0;
  int bits = 0;
  size_t pos = 0;
  int cols = 0;

  for (size_t i = 0; i < input_len; ++i)
    {
      bits += input[i];
      ++char_count;

      if (char_count == 3)
        {
          result[pos++] = alphabet[bits >> 18];
          result[pos++] = alphabet[(bits >> 12) & 0x3f];
          result[pos++] = alphabet[(bits >> 6) & 0x3f];
          result[pos++] = alphabet[bits & 0x3f];
          cols += 4;
          if (cols == max_columns) {
            if (is_chunked) 
              result[pos++] = '\n';
            cols = 0;
          }
          bits = 0;
          char_count = 0;
        }
      else
        {
          bits <<= 8;
        }
    }

  if (char_count != 0)
    {
      bits <<= (16 - (8 * char_count));
      result[pos++] = alphabet[bits >> 18];
      result[pos++] = alphabet[(bits >> 12) & 0x3f];
      cols += 2;
      if (char_count == 1)
        {
          result[pos++] = pad;
          result[pos++] = pad;
          cols += 2;
        }
      else
        {
          result[pos++] = alphabet[(bits >> 6) & 0x3f];
          result[pos++] = pad;
          cols += 2;
        }
    }

  if (cols > 0 && is_chunked)
    result[pos++] = '\n';

  result[pos] = 0;
  *output_len = pos;
  return result;
}

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

Initialize the tables for encoding/decoding.

Definition at line 220 of file Codecs.cpp.

{
  if (!ACE_Base64::init_)
    {
      for (ACE_Byte i = 0; i < sizeof (alphabet); ++i)
        {
          ACE_Base64::decoder_[alphabet[i]] = i;
          ACE_Base64::member_ [alphabet[i]] = 1;
        }
      ACE_Base64::init_ = true;
    }
  return;
}

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.

{
  if (!ACE_Base64::init_)
    ACE_Base64::init();

  ACE_Byte* ptr = const_cast<ACE_Byte*> (input);
  while (*ptr != 0 &&
         (member_[*(ptr)] == 1 || *ptr == pad
          || ACE_OS::ace_isspace (*ptr)))
    ++ptr;
  size_t len = ptr - input;
  len = ((len + 3) / 4) * 3 + 1 ;
  return len;
}

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.

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

Boolean to denote whether initialization is complete.

Definition at line 113 of file Codecs.h.

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.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines