2
//*********************************************************************
3
//* C_Base64 - a simple base64 encoder and decoder.
5
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
7
//* This code may be freely used for any purpose, either personal
8
//* or commercial, provided the authors copyright notice remains
10
//*********************************************************************
12
#ifndef TALK_BASE_BASE64_H__
13
#define TALK_BASE_BASE64_H__
24
DO_PARSE_STRICT = 1, // Parse only base64 characters
25
DO_PARSE_WHITE = 2, // Parse only base64 and whitespace characters
26
DO_PARSE_ANY = 3, // Parse all characters
29
DO_PAD_YES = 4, // Padding is required
30
DO_PAD_ANY = 8, // Padding is optional
31
DO_PAD_NO = 12, // Padding is disallowed
34
DO_TERM_BUFFER = 16, // Must termiante at end of buffer
35
DO_TERM_CHAR = 32, // May terminate at any character boundary
36
DO_TERM_ANY = 48, // May terminate at a sub-character bit offset
39
// Strictest interpretation
40
DO_STRICT = DO_PARSE_STRICT | DO_PAD_YES | DO_TERM_BUFFER,
42
DO_LAX = DO_PARSE_ANY | DO_PAD_ANY | DO_TERM_CHAR,
44
typedef int DecodeFlags;
46
static bool IsBase64Char(char ch);
48
// Determines whether the given string consists entirely of valid base64
49
// encoded characters.
50
static bool IsBase64Encoded(const std::string& str);
52
static void EncodeFromArray(const void* data, size_t len,
54
static bool DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
55
std::string* result, size_t* data_used);
56
static bool DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
57
std::vector<char>* result, size_t* data_used);
59
// Convenience Methods
60
static inline std::string Encode(const std::string& data) {
62
EncodeFromArray(data.data(), data.size(), &result);
65
static inline std::string Decode(const std::string& data, DecodeFlags flags) {
67
DecodeFromArray(data.data(), data.size(), flags, &result, NULL);
70
static inline bool Decode(const std::string& data, DecodeFlags flags,
71
std::string* result, size_t* data_used)
73
return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
75
static inline bool Decode(const std::string& data, DecodeFlags flags,
76
std::vector<char>* result, size_t* data_used)
78
return DecodeFromArray(data.data(), data.size(), flags, result, data_used);
82
static const char Base64Table[];
83
static const unsigned char DecodeTable[];
85
static size_t GetNextQuantum(DecodeFlags parse_flags, bool illegal_pads,
86
const char* data, size_t len, size_t* dpos,
87
unsigned char qbuf[4], bool* padded);
89
static bool DecodeFromArrayTemplate(const char* data, size_t len,
90
DecodeFlags flags, T* result,
94
} // namespace talk_base
96
#endif // TALK_BASE_BASE64_H__