Cross-Platform C++

ot::util
class Base64

#include "ot/util/Base64.h"

Class module for encoding and decoding byte arrays to and from Base64. The Base64 Content-Transfer-Encoding is described in RFC 2045 section 6.8. Briefly, the Base64 encoding transforms each 3 bytes (24 bits) of input into 4 ASCII bytes, each one encoding 6 bits of the input. The output bytes only require 65 unique values (taken from 2^6 + 1 terminator) which means that a subset of printable ASCII can be used.

Each 6-bit group is used as an index into an array of 64 printable characters. The character referenced by the index is placed in the output string. These characters, identified in Table 1, below, are selected so as to be universally representable, and the set excludes characters with particular significance to SMTP (e.g., ".", CR, LF) and to the multi-part boundary delimiters defined in RFC 2046 (e.g., "-").

                    Table 1: The Base64 Alphabet

     Value Encoding  Value Encoding  Value Encoding  Value Encoding
         0 A            17 R            34 i            51 z
         1 B            18 S            35 j            52 0
         2 C            19 T            36 k            53 1
         3 D            20 U            37 l            54 2
         4 E            21 V            38 m            55 3
         5 F            22 W            39 n            56 4
         6 G            23 X            40 o            57 5
         7 H            24 Y            41 p            58 6
         8 I            25 Z            42 q            59 7
         9 J            26 a            43 r            60 8
        10 K            27 b            44 s            61 9
        11 L            28 c            45 t            62 +
        12 M            29 d            46 u            63 /
        13 N            30 e            47 v
        14 O            31 f            48 w         (pad) =
        15 P            32 g            49 x
        16 Q            33 h            50 y




Method Summary
static Result Decode(const Byte* pFrom, const Byte* pFromEnd, Byte* pTo, const Byte* pToLimit, Byte*& pToNext)
         Decodes an array of base64-encoded bytes.
static Result Encode(const Byte* pFrom, const Byte* pFromEnd, Byte* pTo, const Byte* pToLimit, Byte*& pToNext)
         Encodes an array of bytes into base64.
static size_t GetEncodedLength(const Byte* pFrom, const Byte* pFromEnd)
         Returns the maximum number of bytes that a byte sequence will occupy once it has been encoded into base64.
static size_t GetMaxDecodedLength(const Byte* pFrom, const Byte* pFromEnd)
         Returns the maximum number of bytes that a base64-encoded sequence will occupy once it has been decoded.

Enumerations

enum Result { ok /* Operation successful */ 
  outputExhausted /* Output buffer too small for to accept result */ 
  inputExhausted /* Input buffer contains insufficient data */ 


Method Detail

Decode

static Result Decode(const Byte* pFrom,
                     const Byte* pFromEnd,
                     Byte* pTo,
                     const Byte* pToLimit,
                     Byte*& pToNext)
Decodes an array of base64-encoded bytes.

Parameters:
pFrom - pointer to the start of an array of bytes to decode
pFromEnd - pointer to the next byte position after the end of the array
pTo - pointer to the start of an array of bytes to receive the result
pToLimit - pointer to the next byte position after the end of the result array
pToNext - return parameter which will contain a pointer to the first unused byte in the result array after decoding is complete.
Exceptions:
NullPointerException - if pFrom, pFromEnd, pTo or pToLimit are null.
IllegalArgumentException - if pFromEnd is less than pFrom or pToLimit is less than pTo

Encode

static Result Encode(const Byte* pFrom,
                     const Byte* pFromEnd,
                     Byte* pTo,
                     const Byte* pToLimit,
                     Byte*& pToNext)
Encodes an array of bytes into base64.

Parameters:
pFrom - pointer to the start of an array of bytes to encode
pFromEnd - pointer to the next byte position after the end of the array
pTo - pointer to the start of an array of bytes to receive the result
pToLimit - pointer to the next byte position after the end of the result array
pToNext - return parameter which will contain a pointer to the first unused byte in the result array after encoding is complete.
Exceptions:
NullPointerException - if pFrom, pFromEnd, pTo or pToLimit are null.
IllegalArgumentException - if pFromEnd is less than pFrom or pToLimit is less than pTo

GetEncodedLength

static size_t GetEncodedLength(const Byte* pFrom,
                               const Byte* pFromEnd)
Returns the maximum number of bytes that a byte sequence will occupy once it has been encoded into base64.

Parameters:
pFrom - pointer to the start of an array of bytes to encode
pFromEnd - pointer to the next byte position after the end of the array

GetMaxDecodedLength

static size_t GetMaxDecodedLength(const Byte* pFrom,
                                  const Byte* pFromEnd)
Returns the maximum number of bytes that a base64-encoded sequence will occupy once it has been decoded.

Parameters:
pFrom - pointer to the start of an array of bytes to decode
pFromEnd - pointer to the next byte position after the end of the array


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements