16 #ifndef INCLUDE_NN_NLIB_BASE64_H_ 17 #define INCLUDE_NN_NLIB_BASE64_H_ 21 #ifdef NLIB_CXX11_STDLIB_TUPLE 39 kBase64Default = kBase64PlusSlash,
40 kBase64UrlSafe = kBase64MinusUnderscore,
41 BASE64_PLUS_SLASH = kBase64PlusSlash,
42 BASE64_PLUS_MINUS = kBase64PlusMinus,
43 BASE64_MINUS_UNDERSCORE = kBase64MinusUnderscore,
44 BASE64_DOT_MINUS = kBase64DotMinus,
45 BASE64_UNDERSCORE_COLON = kBase64UnderscoreColon,
46 BASE64_UNDERSCORE_MINUS = kBase64UnderscoreMinus,
47 BASE64_DOT_UNDERSCORE = kBase64DotUnderscore,
48 BASE64_EXCLAMATION_MINUS = kBase64ExclamationMinus,
49 _BASE64_MAX = kBase64Max,
50 BASE64_DEFAULT = kBase64PlusSlash,
51 BASE64_URL_SAFE = kBase64MinusUnderscore
56 return ((srcsize + 2) / 3) * 4 + 1;
59 Encode(
char* dst,
size_t dstsize,
const void* src,
size_t srcsize,
63 Encode(
char (&dst)[N],
const void* src,
size_t srcsize,
64 CharOption char_option = kBase64Default,
bool padding =
false) NLIB_NOEXCEPT {
65 return Encode(dst, N, src, srcsize, char_option, padding);
68 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 69 template<
class DUMMY =
void>
75 bool padding =
false) NLIB_NOEXCEPT {
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);
88 #ifdef __cpp_rvalue_references 89 static ::std::pair<errno_t, UniquePtr<char[]> >
90 Encode(
const void* src,
size_t srcsize,
91 CharOption char_option = kBase64Default,
bool padding =
false) NLIB_NOEXCEPT {
92 size_t dstsize = GetRequiredSize(srcsize);
93 char* p =
new (std::nothrow)
char[dstsize];
94 if (!p) return ::std::make_pair(ENOMEM,
nullptr);
95 errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
97 return ::std::make_pair(0,
UniquePtr<
char[]>(p));
99 return ::std::make_pair(e,
nullptr);
107 errno_t StepEncode(
size_t* written,
char* dst,
size_t dstsize,
const void* src,
112 size_t srcsize) NLIB_NOEXCEPT {
113 return StepEncode(written, dst, N, src, srcsize);
115 std::pair<errno_t, size_t>
116 StepEncode(
char* dst,
size_t dstsize,
const void* src,
size_t srcsize) NLIB_NOEXCEPT {
118 errno_t e = StepEncode(&written, dst, dstsize, src, srcsize);
119 return std::make_pair(e, written);
122 std::pair<errno_t, size_t>
123 StepEncode(
char (&dst)[N],
const void* src,
size_t srcsize) NLIB_NOEXCEPT {
124 return StepEncode(&dst[0], N, src, srcsize);
128 errno_t Close(
size_t* written,
char* dst,
size_t dstsize,
132 errno_t Close(
size_t* written,
char (&dst)[N],
bool padding) NLIB_NOEXCEPT {
133 return Close(written, dst, N, padding);
135 std::pair<errno_t, size_t>
136 Close(
char* dst,
size_t dstsize,
bool padding) NLIB_NOEXCEPT {
138 errno_t e = Close(&written, dst, dstsize, padding);
139 return std::make_pair(e, written);
142 std::pair<errno_t, size_t>
143 Close(
char (&dst)[N],
bool padding) NLIB_NOEXCEPT {
return Close(dst, N, padding); }
156 kBase64PlusSlash = 0,
158 kBase64MinusUnderscore,
160 kBase64UnderscoreColon,
161 kBase64UnderscoreMinus,
162 kBase64DotUnderscore,
163 kBase64ExclamationMinus,
165 kBase64Default = kBase64PlusSlash,
166 kBase64UrlSafe = kBase64MinusUnderscore,
167 BASE64_PLUS_SLASH = kBase64PlusSlash,
168 BASE64_PLUS_MINUS = kBase64PlusMinus,
169 BASE64_MINUS_UNDERSCORE = kBase64MinusUnderscore,
170 BASE64_DOT_MINUS = kBase64DotMinus,
171 BASE64_UNDERSCORE_COLON = kBase64UnderscoreColon,
172 BASE64_UNDERSCORE_MINUS = kBase64UnderscoreMinus,
173 BASE64_DOT_UNDERSCORE = kBase64DotUnderscore,
174 BASE64_EXCLAMATION_MINUS = kBase64ExclamationMinus,
175 _BASE64_MAX = kBase64Max,
176 BASE64_DEFAULT = kBase64PlusSlash,
177 BASE64_URL_SAFE = kBase64MinusUnderscore
182 return ((srcsize + 3) / 4) * 3;
184 static errno_t Decode(
size_t* written,
void* dst,
size_t dstsize,
const char* src,
185 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
186 return Decode_(written, static_cast<nlib_byte_t*>(dst), dstsize,
191 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
192 return Decode_(written, dst, N, src,
nlib_strlen(src), char_option);
194 static std::pair<errno_t, size_t>
195 Decode(
void* dst,
size_t dstsize,
const char* src,
196 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
198 errno_t e = Decode(&written, dst, dstsize, src, char_option);
199 return std::make_pair(e, written);
202 static std::pair<errno_t, size_t>
204 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
205 return Decode(dst, N, src, char_option);
207 #if defined(_MSC_VER) || __cplusplus >= 201103L 209 Decode(
size_t* written, uint8_t* dst,
size_t dstsize,
const char* src,
210 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
211 return Decode_(written, reinterpret_cast<nlib_byte_t*>(dst),
216 Decode(
size_t* written, uint8_t (&dst)[N],
const char* src,
217 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
218 return Decode_(written, reinterpret_cast<nlib_byte_t*>(dst),
222 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 223 template<
class DUMMY =
void>
226 size_t* dstsize,
const char* src,
227 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
230 size_t dstsize_ = GetRequiredSize(srcsize);
233 if (!p)
return ENOMEM;
235 errno_t e = Decode_(&written, p, dstsize_, src, srcsize, char_option);
244 #if defined(_MSC_VER) || __cplusplus >= 201103L 245 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 246 template<
class DUMMY =
void>
249 size_t* dstsize,
const char* src,
250 CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
253 size_t dstsize_ = GetRequiredSize(srcsize);
255 uint8_t* p =
new (std::nothrow) uint8_t[dstsize_];
256 if (!p)
return ENOMEM;
258 errno_t e = Decode_(&written, reinterpret_cast<nlib_byte_t*>(p),
259 dstsize_, src, srcsize, char_option);
269 #ifdef NLIB_CXX11_STDLIB_TUPLE 270 static ::std::tuple<errno_t, UniquePtr<nlib_byte_t[]>,
size_t>
271 Decode(
const char* src,
CharOption char_option = kBase64Default) NLIB_NOEXCEPT {
273 size_t dstsize = GetRequiredSize(srcsize);
275 if (!p) return ::std::make_tuple(ENOMEM,
nullptr, 0);
277 errno_t e = Decode_(&written, p, dstsize, src, srcsize, char_option);
282 return ::std::make_tuple(e,
nullptr, 0);
291 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 298 errno_t StepDecode(
size_t* written,
void* dst,
size_t dstsize,
const char* src,
302 size_t srcsize) NLIB_NOEXCEPT {
303 return StepDecode(written, dst, N, src, srcsize);
305 std::pair<errno_t, size_t>
306 StepDecode(
void* dst,
size_t dstsize,
const char* src,
size_t srcsize) NLIB_NOEXCEPT {
308 errno_t e = StepDecode(&written, dst, dstsize, src, srcsize);
309 return std::make_pair(e, written);
312 std::pair<errno_t, size_t>
313 StepDecode(
nlib_byte_t (&dst)[N],
const char* src,
size_t srcsize) NLIB_NOEXCEPT {
314 return StepDecode(dst, N, src, srcsize);
316 #if defined(_MSC_VER) || __cplusplus >= 201103L 319 size_t srcsize) NLIB_NOEXCEPT {
320 return StepDecode(written, static_cast<nlib_byte_t*>(dst), N, src, srcsize);
327 return Close(written, dst, N);
329 std::pair<errno_t, size_t>
330 Close(
void* dst,
size_t dstsize) NLIB_NOEXCEPT {
332 errno_t e = Close(&written, dst, dstsize);
333 return std::make_pair(e, written);
336 std::pair<errno_t, size_t>
337 Close(
nlib_byte_t (&dst)[N]) NLIB_NOEXCEPT {
return Close(&dst[0], N); }
338 #if defined(_MSC_VER) || __cplusplus >= 201103L 341 return Close(written, reinterpret_cast<nlib_byte_t*>(&dst[0]), N);
347 static errno_t Decode_(
size_t* written,
nlib_byte_t* dst,
size_t dstsize,
const char* src,
352 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 362 #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.
Decodes Base64. This class supports various variants of Base64.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#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...
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 StepDecode(size_t *written, nlib_byte_t(&dst)[N], const char *src, size_t srcsize) noexcept
Runs StepDecode(written, dst, N, src, srcsize).
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.
errno_t Close(size_t *written, nlib_byte_t(&dst)[N]) noexcept
Runs Close(written, dst, N).
static errno_t Decode(size_t *written, nlib_byte_t(&dst)[N], const char *src, CharOption char_option=kBase64Default) noexcept
Runs Decode(written, dst, N, src, srcsize, char_option).
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.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
static errno_t Decode(size_t *written, void *dst, size_t dstsize, const char *src, CharOption char_option=kBase64Default) noexcept
Decodes data in batch.
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.
constexpr 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.
constexpr Base64Encoder() noexcept
Instantiates the object with default parameters (default constructor).