16 #ifndef INCLUDE_NN_NLIB_BASE64_H_ 17 #define INCLUDE_NN_NLIB_BASE64_H_ 35 kBase64Default = kBase64PlusSlash,
36 kBase64UrlSafe = kBase64MinusUnderscore,
37 BASE64_PLUS_SLASH = kBase64PlusSlash,
38 BASE64_PLUS_MINUS = kBase64PlusMinus,
39 BASE64_MINUS_UNDERSCORE = kBase64MinusUnderscore,
40 BASE64_DOT_MINUS = kBase64DotMinus,
41 BASE64_UNDERSCORE_COLON = kBase64UnderscoreColon,
42 BASE64_UNDERSCORE_MINUS = kBase64UnderscoreMinus,
43 BASE64_DOT_UNDERSCORE = kBase64DotUnderscore,
44 BASE64_EXCLAMATION_MINUS = kBase64ExclamationMinus,
45 _BASE64_MAX = kBase64Max,
46 BASE64_DEFAULT = kBase64PlusSlash,
47 BASE64_URL_SAFE = kBase64MinusUnderscore
52 return ((srcsize + 2) / 3) * 4 + 1;
54 static errno_t Encode(
char* dst,
66 return Encode(dst, N, src, srcsize, char_option, padding);
68 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 69 template<
class DUMMY =
void>
77 size_t dstsize = GetRequiredSize(srcsize);
79 char* p =
new (std::nothrow)
char[dstsize];
80 if (!p)
return ENOMEM;
81 errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
105 size_t srcsize) NLIB_NOEXCEPT {
106 return StepEncode(written, dst, N, src, srcsize);
119 bool padding) NLIB_NOEXCEPT {
120 return Close(written, dst, N, padding);
133 kBase64PlusSlash = 0,
135 kBase64MinusUnderscore,
137 kBase64UnderscoreColon,
138 kBase64UnderscoreMinus,
139 kBase64DotUnderscore,
140 kBase64ExclamationMinus,
142 kBase64Default = kBase64PlusSlash,
143 kBase64UrlSafe = kBase64MinusUnderscore,
144 BASE64_PLUS_SLASH = kBase64PlusSlash,
145 BASE64_PLUS_MINUS = kBase64PlusMinus,
146 BASE64_MINUS_UNDERSCORE = kBase64MinusUnderscore,
147 BASE64_DOT_MINUS = kBase64DotMinus,
148 BASE64_UNDERSCORE_COLON = kBase64UnderscoreColon,
149 BASE64_UNDERSCORE_MINUS = kBase64UnderscoreMinus,
150 BASE64_DOT_UNDERSCORE = kBase64DotUnderscore,
151 BASE64_EXCLAMATION_MINUS = kBase64ExclamationMinus,
152 _BASE64_MAX = kBase64Max,
153 BASE64_DEFAULT = kBase64PlusSlash,
154 BASE64_URL_SAFE = kBase64MinusUnderscore
159 return ((srcsize + 3) / 4) * 3;
165 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
166 return Decode_(written, dst, dstsize, src,
nlib_strlen(src), char_option);
172 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
173 return Decode_(written, dst, N, src,
nlib_strlen(src), char_option);
175 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 176 template<
class DUMMY =
void>
179 size_t* dstsize,
const char* src,
180 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
183 size_t dstsize_ = GetRequiredSize(srcsize);
185 uint8_t* p =
new (std::nothrow) uint8_t[dstsize_];
186 if (!p)
return ENOMEM;
188 errno_t e = Decode_(&written, p, dstsize_, src, srcsize, char_option);
217 size_t srcsize) NLIB_NOEXCEPT {
218 return StepDecode(written, dst, N, src, srcsize);
227 uint8_t (&dst)[N]) NLIB_NOEXCEPT {
228 return Close(written, dst, N);
233 static errno_t Decode_(
size_t* written,
242 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 251 #endif // INCLUDE_NN_NLIB_BASE64_H_ errno_t StepEncode(size_t *written, char(&dst)[N], const void *src, size_t srcsize) noexcept
Runs StepEncode(written, dst, N, src, srcsize).
CharOption
For more information, see Base64Encoder::CharOption.
Specifies '.' for the 62nd character and '-' for the 63rd character.
static errno_t Decode(size_t *written, uint8_t *dst, size_t dstsize, const char *src, CharOption char_option=kBase64Default) noexcept
Decodes data in batch.
~Base64Encoder() noexcept
Destructor.
Decodes Base64. This class supports various variants of Base64.
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
~Base64Decoder() noexcept
Destructor.
static errno_t Encode(UniquePtr< char[]> &dst, const void *src, size_t srcsize, CharOption char_option=kBase64Default, bool padding=false) noexcept
Encodes data in batch.
static errno_t Decode(UniquePtr< uint8_t[]> &dst, size_t *dstsize, const char *src, CharOption char_option=kBase64Default) noexcept
Decodes data in batch.
static errno_t Encode(char(&dst)[N], const void *src, size_t srcsize, CharOption char_option=kBase64Default, bool padding=false) noexcept
Runs Encode(dst, N, src, srcsize, char_option, padding).
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Defines that class that is corresponding to std::unique_ptr.
CharOption
Variations for the 62nd and 63rd characters in Base64 can be specified.
errno_t Close(size_t *written, char(&dst)[N], bool padding) noexcept
Runs Base64Encoder::Close(written, dst, N, padding).
Encodes Base64. This class supports various variants of Base64.
Specifies '.' for the 62nd character and '_' for the 63rd character.
static errno_t Decode(size_t *written, uint8_t(&dst)[N], const char *src, CharOption char_option=kBase64Default) noexcept
Runs Decode(written, dst, N, src, srcsize, char_option).
errno_t StepDecode(size_t *written, uint8_t(&dst)[N], const char *src, size_t srcsize) noexcept
Runs StepDecode(written, dst, N, src, srcsize).
Specifies '-' for the 62nd character and '_' for the 63rd character.
Specifies '_' for the 62nd character and ':' for the 63rd character.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
static size_t GetRequiredSize(size_t srcsize) noexcept
Calculates the size of the memory space required for decoding the data.
Specifies '+' for the 62nd character and '-' for the 63rd character.
Specifies '_' for the 62nd character and '-' for the 63rd character.
Specifies '!' for the 62nd character and '-' for the 63rd character.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Base64Decoder() noexcept
Instantiates the object with default parameters (default constructor).
static size_t GetRequiredSize(size_t srcsize) noexcept
Calculates the size of the memory space required for encoding the data.
Base64Encoder() noexcept
Instantiates the object with default parameters (default constructor).