Provides methods for converting bytes sequences to Base64 character strings and vice versa. More...
#include <Base64.h>
Static Public Member Functions | |
static void | encodeBytes (const std::vector< uint8_t > &source, std::string &dest) |
Convert a sequence of bytes to a Base64 string. | |
static uint8_t | decode64 (char c) throw (std::invalid_argument) |
Convert a 6-bit Base64 character to an 8-bit byte. | |
static void | decodeString (const std::string &source, std::vector< uint8_t > &dest) throw (std::invalid_argument) |
Convert a Base64 string to a sequence of bytes. | |
Static Private Member Functions | |
static void | map3to4 (uint8_t A, uint8_t B, uint8_t C, std::string &abcd) |
Convert a sequence of three bytes to a sequence of four characters. | |
static void | map4to3 (char a, char b, char c, char d, std::vector< uint8_t > &ABC, int j, int n) throw (std::invalid_argument) |
Convert a sequence of four characters to a sequence of three bytes. | |
Static Private Attributes | |
static char | encode64 [] |
Base64 character set: A-Z, a-z, 0-9, +, /. |
Provides methods for converting bytes sequences to Base64 character strings and vice versa.
static void encodeBytes(const std::vector<uint8_t> &source, std::string &dest)
static void decodeString(const std::string &source, std::vector<uint8_t> &dest)
Be aware that there is not a one-to-one correspondence between byte sequences and Base64 character sequences. Given any character sequence C created by encodeBytes
, the call decodeString(C)
will return the original byte sequence. However, there exist character sequences which are never produced by encodeBytes
, and, hence, applying decodeString
to such character sequences produces byte sequences from which the original character sequences cannot be recovered. For example, decodeString
converts both AA== and AB== to the single byte 0, and encodeBytes
converts the single byte 0 to AA==. There does not exist a sequence of bytes which will cause encodeBytes
to return AB==.
Definition at line 39 of file Base64.h.
uint8_t Base64::decode64 | ( | char | c | ) | throw (std::invalid_argument) [static] |
Convert a 6-bit Base64 character to an 8-bit byte.
c | a 6-bit Base64 character. |
std::invalid_argument | if the input character is not a Base64 character. |
Definition at line 91 of file Base64.cpp.
void Base64::decodeString | ( | const std::string & | source, | |
std::vector< uint8_t > & | dest | |||
) | throw (std::invalid_argument) [static] |
Convert a Base64 string to a sequence of bytes.
source | is the input to this function, a string of zero or more 6-bit Base64 characters. | |
dest | is the output from this function, a a byte sequence containing the 8-bit bytes corresponding to the input characters. |
std::invalid_argument | if the length of the input string is not a multiple of four. | |
std::invalid_argument | if the input string contains a character which is not a Base64 character. |
Definition at line 154 of file Base64.cpp.
References ends_with().
void Base64::encodeBytes | ( | const std::vector< uint8_t > & | source, | |
std::string & | dest | |||
) | [static] |
Convert a sequence of bytes to a Base64 string.
source | is the input to this function, a sequence of zero or more 8-bit bytes. | |
dest | is the output from this function, a string containing the Base64 representation of the input bytes, 6-bits per character. |
Definition at line 63 of file Base64.cpp.
References map3to4().
void Base64::map3to4 | ( | uint8_t | A, | |
uint8_t | B, | |||
uint8_t | C, | |||
std::string & | abcd | |||
) | [static, private] |
Convert a sequence of three bytes to a sequence of four characters.
Map three 8-bit bytes to four 6-bit bytes:
765432 10 7654 3210 76 543210 +---------+---------+---------+ |AAAAAA AA|BBBB BBBB|CC CCCCCC| +------+-------+-------+------+ |aaaaaa|bb bbbb|cccc cc|dddddd| +------+-------+-------+------+ 543210 54 3210 5432 10 543210 *
A | the first byte. | |
B | the second byte. | |
C | the third byte. | |
abcd | the resulting four characters are appended to abcd. |
Definition at line 22 of file Base64.cpp.
References encode64.
Referenced by encodeBytes().
void Base64::map4to3 | ( | char | a, | |
char | b, | |||
char | c, | |||
char | d, | |||
std::vector< uint8_t > & | ABC, | |||
int | j, | |||
int | n | |||
) | throw (std::invalid_argument) [static, private] |
Convert a sequence of four characters to a sequence of three bytes.
See the comment for map3to4 for the exact mapping.
a | the first character. | |
b | the second character. | |
c | the third character. | |
d | the fourth character. | |
ABC | the resulting three bytes are stored here. | |
j | an index to the first byte in ABC to change. | |
n | 1, 2, or 3, indicating how many bytes to store in ABC. |
std::invalid_argument | if any input character is not a Base64 character. |
Definition at line 122 of file Base64.cpp.
char Base64::encode64 [static, private] |
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }
Base64 character set: A-Z, a-z, 0-9, +, /.
Definition at line 45 of file Base64.h.
Referenced by map3to4().