16 #ifndef INCLUDE_NN_NLIB_BASE64_H_ 17 #define INCLUDE_NN_NLIB_BASE64_H_ 21 #ifdef __cpp_rvalue_references 39 kBase64Default = kBase64PlusSlash,
40 kBase64UrlSafe = kBase64MinusUnderscore
45 return ((srcsize + 2) / 3) * 4 + 1;
48 Encode(
char* dst,
size_t dstsize,
const void* src,
size_t srcsize,
49 CharOption char_option = kBase64Default,
bool padding =
false)
NLIB_NOEXCEPT;
52 Encode(
char (&dst)[N], const
void* src,
size_t srcsize,
CharOption char_option = kBase64Default,
54 return Encode(dst, N, src, srcsize, char_option, padding);
57 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 58 template<
class DUMMY =
void>
64 size_t dstsize = GetRequiredSize(srcsize);
66 char* p =
new (std::nothrow)
char[dstsize];
67 if (!p)
return ENOMEM;
68 errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
75 #ifdef __cpp_rvalue_references 76 static std::pair<errno_t, std::unique_ptr<char[]> >
79 size_t dstsize = GetRequiredSize(srcsize);
80 char* p =
new (std::nothrow)
char[dstsize];
81 if (!p) return ::std::make_pair(ENOMEM,
nullptr);
82 errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
84 return ::std::make_pair(0,
UniquePtr<
char[]>(p));
86 return ::std::make_pair(e,
nullptr);
94 errno_t StepEncode(
size_t* written,
char* dst,
size_t dstsize,
const void* src,
100 return StepEncode(written, dst, N, src, srcsize);
102 std::pair<errno_t, size_t>
105 errno_t e = StepEncode(&written, dst, dstsize, src, srcsize);
106 return std::make_pair(e, written);
109 std::pair<errno_t, size_t>
111 return StepEncode(&dst[0], N, src, srcsize);
120 return Close(written, dst, N, padding);
124 errno_t e = Close(&written, dst, dstsize, padding);
125 return std::make_pair(e, written);
129 return Close(dst, N, padding);
143 kBase64PlusSlash = 0,
152 kBase64Default = kBase64PlusSlash,
153 kBase64UrlSafe = kBase64MinusUnderscore
158 static errno_t Decode(
size_t* written,
void* dst,
size_t dstsize,
const char* src,
160 return Decode_(written, static_cast<nlib_byte_t*>(dst), dstsize, src,
nlib_strlen(src),
166 return Decode_(written, dst, N, src,
nlib_strlen(src), char_option);
168 static std::pair<errno_t, size_t>
169 Decode(
void* dst,
size_t dstsize,
const char* src,
172 errno_t e = Decode(&written, dst, dstsize, src, char_option);
173 return std::make_pair(e, written);
176 static std::pair<errno_t, size_t>
179 return Decode(dst, N, src, char_option);
181 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES 182 template<
class DUMMY =
void>
188 size_t dstsize_ = GetRequiredSize(srcsize);
190 uint8_t* p =
new (std::nothrow) uint8_t[dstsize_];
191 if (!p)
return ENOMEM;
193 errno_t e = Decode_(&written, reinterpret_cast<nlib_byte_t*>(p), dstsize_, src, srcsize,
203 #ifdef __cpp_rvalue_references 204 static std::tuple<errno_t, std::unique_ptr<nlib_byte_t[]>,
size_t>
207 size_t dstsize = GetRequiredSize(srcsize);
209 if (!p) return ::std::make_tuple(ENOMEM,
nullptr, 0);
211 errno_t e = Decode_(&written, p, dstsize, src, srcsize, char_option);
216 return ::std::make_tuple(e,
nullptr, 0);
221 DecodeInplace(
size_t* written,
char* src,
224 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 231 errno_t StepDecode(
size_t* written,
void* dst,
size_t dstsize,
const char* src,
236 return StepDecode(written, dst, N, src, srcsize);
238 std::pair<errno_t, size_t>
241 errno_t e = StepDecode(&written, dst, dstsize, src, srcsize);
242 return std::make_pair(e, written);
245 std::pair<errno_t, size_t>
247 return StepDecode(dst, N, src, srcsize);
252 return Close(written, dst, N);
256 errno_t e = Close(&written, dst, dstsize);
257 return std::make_pair(e, written);
261 return Close(&dst[0], N);
266 static
errno_t Decode_(
size_t* written,
nlib_byte_t* dst,
size_t dstsize, const
char* src,
271 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN) 281 #endif // INCLUDE_NN_NLIB_BASE64_H_ errno_t StepEncode(size_t *written, char(&dst)[N], const void *src, size_t srcsize) noexcept
A template overload of the above function.
CharOption
Variations for the 62nd and 63rd characters in Base64 can be specified.
Specifies '-' for the 62nd character and '_' for the 63rd character.
Specifies '.' for the 62nd character and '-' for the 63rd character.
std::pair< errno_t, size_t > StepDecode(nlib_byte_t(&dst)[N], const char *src, size_t srcsize) noexcept
A template overload of the above function.
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 std::tuple< errno_t, std::unique_ptr< nlib_byte_t[]>, size_t > Decode(const char *src, CharOption char_option=kBase64Default) noexcept
Decodes the data at a time and returns them after allocating memory inside.
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.
std::pair< errno_t, size_t > Close(char(&dst)[N], bool padding) noexcept
A template overload of the above function.
std::pair< errno_t, size_t > StepEncode(char *dst, size_t dstsize, const void *src, size_t srcsize) noexcept
Runs split encoding of Base64. No null character will be appended to the end. Returns a pair of the e...
Specifies '.' for the 62nd character and '_' for the 63rd character.
Specifies '!' for the 62nd character and '-' for the 63rd character.
In the C++11 environment (which supports alias templates), std::unique_ptr is made an alias template...
Defines that class that is corresponding to std::unique_ptr.
Specifies '_' for the 62nd character and '-' for the 63rd character.
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
A template overload of the above function.
errno_t Close(size_t *written, char(&dst)[N], bool padding) noexcept
A template overload of the above function.
static std::pair< errno_t, size_t > Decode(nlib_byte_t(&dst)[N], const char *src, CharOption char_option=kBase64Default) noexcept
A template overload of the above function.
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
A template overload of the above function.
Specifies '_' for the 62nd character and ':' for the 63rd character.
static errno_t Decode(size_t *written, nlib_byte_t(&dst)[N], const char *src, CharOption char_option=kBase64Default) noexcept
A template overload of the above function.
std::pair< errno_t, size_t > Close(void *dst, size_t dstsize) noexcept
Finishes split decoding of Base64. Returns a pair of the error value and the number of bytes written ...
Specifies '.' for the 62nd character and '-' for the 63rd character.
std::pair< errno_t, size_t > StepEncode(char(&dst)[N], const void *src, size_t srcsize) noexcept
A template overload of the above function.
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.
static std::pair< errno_t, std::unique_ptr< char[]> > Encode(const void *src, size_t srcsize, CharOption char_option=kBase64Default, bool padding=false) noexcept
Encodes the data at a time and returns them after allocating memory inside.
Specifies '+' for the 62nd character and '-' for the 63rd character.
static std::pair< errno_t, size_t > Decode(void *dst, size_t dstsize, const char *src, CharOption char_option=kBase64Default) noexcept
Decodes data in batch. Returns a pair of the error value and the output data size.
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).
std::pair< errno_t, size_t > Close(nlib_byte_t(&dst)[N]) noexcept
A template overload of the above function.
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).
std::pair< errno_t, size_t > StepDecode(void *dst, size_t dstsize, const char *src, size_t srcsize) noexcept
Runs split decoding of Base64. Returns a pair of the error value and the number of bytes written to d...
std::pair< errno_t, size_t > Close(char *dst, size_t dstsize, bool padding) noexcept
Finishes split encoding of Base64. No null character will be appended to the end. Returns a pair of t...