16 #ifndef INCLUDE_NN_NLIB_BASE64_H_ 17 #define INCLUDE_NN_NLIB_BASE64_H_ 26 BASE64_PLUS_SLASH = 0,
35 BASE64_DEFAULT = BASE64_PLUS_SLASH,
36 BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
41 return ((srcsize + 2) / 3) * 4 + 1;
43 static errno_t Encode(
char* dst,
55 return Encode(dst, N, src, srcsize, char_option, padding);
57 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 58 template<
class DUMMY =
void>
66 size_t dstsize = GetRequiredSize(srcsize);
68 char* p =
new (std::nothrow)
char[dstsize];
69 if (!p)
return ENOMEM;
70 errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
94 size_t srcsize) NLIB_NOEXCEPT {
95 return StepEncode(written, dst, N, src, srcsize);
108 bool padding) NLIB_NOEXCEPT {
109 return Close(written, dst, N, padding);
122 BASE64_PLUS_SLASH = 0,
124 BASE64_MINUS_UNDERSCORE,
126 BASE64_UNDERSCORE_COLON,
127 BASE64_UNDERSCORE_MINUS,
128 BASE64_DOT_UNDERSCORE,
129 BASE64_EXCLAMATION_MINUS,
131 BASE64_DEFAULT = BASE64_PLUS_SLASH,
132 BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
137 return ((srcsize + 3) / 4) * 3;
143 CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
144 return Decode_(written, dst, dstsize, src,
nlib_strlen(src), char_option);
150 CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
151 return Decode_(written, dst, N, src,
nlib_strlen(src), char_option);
153 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 154 template<
class DUMMY =
void>
157 size_t* dstsize,
const char* src,
158 CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
161 size_t dstsize_ = GetRequiredSize(srcsize);
163 uint8_t* p =
new (std::nothrow) uint8_t[dstsize_];
164 if (!p)
return ENOMEM;
166 errno_t e = Decode_(&written, p, dstsize_, src, srcsize, char_option);
195 size_t srcsize) NLIB_NOEXCEPT {
196 return StepDecode(written, dst, N, src, srcsize);
205 uint8_t (&dst)[N]) NLIB_NOEXCEPT {
206 return Close(written, dst, N);
211 static errno_t Decode_(
size_t* written,
220 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 229 #endif // INCLUDE_NN_NLIB_BASE64_H_ static errno_t Decode(size_t *written, uint8_t(&dst)[N], const char *src, CharOption char_option=BASE64_DEFAULT) noexcept
Runs Decode(written, dst, N, src, srcsize, char_option).
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.
~Base64Encoder() noexcept
Destructor.
Specifies '!' for the 62nd character and '-' for the 63rd character.
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(char(&dst)[N], const void *src, size_t srcsize, CharOption char_option=BASE64_DEFAULT, bool padding=false) noexcept
Runs Encode(dst, N, src, srcsize, char_option, padding).
static errno_t Decode(UniquePtr< uint8_t[]> &dst, size_t *dstsize, const char *src, CharOption char_option=BASE64_DEFAULT) noexcept
Decodes data in batch.
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).
Specifies '.' for the 62nd character and '_' for the 63rd character.
Encodes Base64. This class supports various variants of Base64.
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.
#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.
static errno_t Decode(size_t *written, uint8_t *dst, size_t dstsize, const char *src, CharOption char_option=BASE64_DEFAULT) noexcept
Decodes data in batch.
static errno_t Encode(UniquePtr< char[]> &dst, const void *src, size_t srcsize, CharOption char_option=BASE64_DEFAULT, bool padding=false) noexcept
Encodes data in batch.
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.
Specifies '.' for the 62nd character and '-' for the 63rd character.
Base64Encoder() noexcept
Instantiates the object with default parameters (default constructor).