3 #ifndef INCLUDE_NN_NLIB_BASE64_H_ 4 #define INCLUDE_NN_NLIB_BASE64_H_ 13 BASE64_PLUS_SLASH = 0,
22 BASE64_DEFAULT = BASE64_PLUS_SLASH,
23 BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
28 return ((srcsize + 2) / 3) * 4 + 1;
30 static errno_t Encode(
char* dst,
42 return Encode(dst, N, src, srcsize, char_option, padding);
44 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 45 template<
class DUMMY =
void>
53 size_t dstsize = GetRequiredSize(srcsize);
55 char* p =
new (std::nothrow)
char[dstsize];
56 if (!p)
return ENOMEM;
57 errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
81 size_t srcsize) NLIB_NOEXCEPT {
82 return StepEncode(written, dst, N, src, srcsize);
95 bool padding) NLIB_NOEXCEPT {
96 return Close(written, dst, N, padding);
109 BASE64_PLUS_SLASH = 0,
111 BASE64_MINUS_UNDERSCORE,
113 BASE64_UNDERSCORE_COLON,
114 BASE64_UNDERSCORE_MINUS,
115 BASE64_DOT_UNDERSCORE,
116 BASE64_EXCLAMATION_MINUS,
118 BASE64_DEFAULT = BASE64_PLUS_SLASH,
119 BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
124 return ((srcsize + 3) / 4) * 3;
130 CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
131 return Decode_(written, dst, dstsize, src,
nlib_strlen(src), char_option);
137 CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
138 return Decode_(written, dst, N, src,
nlib_strlen(src), char_option);
140 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 141 template<
class DUMMY =
void>
144 size_t* dstsize,
const char* src,
145 CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
148 size_t dstsize_ = GetRequiredSize(srcsize);
150 uint8_t* p =
new (std::nothrow) uint8_t[dstsize_];
151 if (!p)
return ENOMEM;
153 errno_t e = Decode_(&written, p, dstsize_, src, srcsize, char_option);
182 size_t srcsize) NLIB_NOEXCEPT {
183 return StepDecode(written, dst, N, src, srcsize);
192 uint8_t (&dst)[N]) NLIB_NOEXCEPT {
193 return Close(written, dst, N);
198 static errno_t Decode_(
size_t* written,
207 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 216 #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).