nlib
nn::nlib::Base64Decoder Class Referencefinal

Decodes Base64. This class supports various variants of Base64. More...

#include "nn/nlib/Base64.h"

Public Types

enum  CharOption {
  kBase64PlusSlash = 0,
  kBase64PlusMinus,
  kBase64MinusUnderscore,
  kBase64DotMinus,
  kBase64UnderscoreColon,
  kBase64UnderscoreMinus,
  kBase64DotUnderscore,
  kBase64ExclamationMinus ,
  kBase64Default = kBase64PlusSlash,
  kBase64UrlSafe = kBase64MinusUnderscore
}
 Variations for the 62nd and 63rd characters in Base64 can be specified. More...
 

Public Member Functions

errno_t StepDecode (size_t *written, void *dst, size_t dstsize, const char *src, size_t srcsize) noexcept
 Runs split decoding of Base64. More...
 
template<size_t N>
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.
 
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 dst.
 
template<size_t N>
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.
 
errno_t Close (size_t *written, void *dst, size_t dstsize) noexcept
 Finishes split decoding of Base64. More...
 
template<size_t N>
errno_t Close (size_t *written, nlib_byte_t(&dst)[N]) 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 to dst.
 
template<size_t N>
std::pair< errno_t, size_t > Close (nlib_byte_t(&dst)[N]) noexcept
 A template overload of the above function.
 
 operator bool () const
 Returns true if the object has been initialized and an error has not occurred inside, or returns false if an error has occurred inside.
 
Constructor, Destructor, and Initialization
constexpr Base64Decoder () noexcept
 Instantiates the object with default parameters (default constructor).
 
errno_t Init (CharOption char_option=kBase64Default) noexcept
 Initializes the object by specifying the 62nd and 63rd characters. More...
 

Static Public Member Functions

static size_t GetRequiredSize (size_t srcsize) noexcept
 Calculates the size of the memory space required for decoding the data. More...
 
static errno_t Decode (size_t *written, void *dst, size_t dstsize, const char *src, CharOption char_option=kBase64Default) noexcept
 Decodes data in batch. More...
 
template<size_t N>
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.
 
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.
 
template<size_t N>
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.
 
static errno_t Decode (UniquePtr< uint8_t[]> &dst, size_t *dstsize, const char *src, CharOption char_option=kBase64Default) noexcept
 Decodes data in batch. More...
 
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 DecodeInplace (size_t *written, char *src, CharOption char_option=kBase64Default) noexcept
 Decodes data immediately. More...
 

Detailed Description

Decodes Base64. This class supports various variants of Base64.

Description
This class supports batch decoding, split decoding and in-place batch decoding of Base64. Various variants of Base64 can be supported by specifying CharOption type values. Data containing newline and/or padding characters can also be decoded.
See also
https://www.ietf.org/rfc/rfc2045.txt
The transition of the object state.
The overview of the object state transitions is described below:
dot_inline_dotgraph_1.png

Definition at line 140 of file Base64.h.

Member Enumeration Documentation

◆ CharOption

Variations for the 62nd and 63rd characters in Base64 can be specified.

Enumerator
kBase64PlusSlash 

Specifies '+' for the 62nd character and '/' for the 63rd character.

kBase64PlusMinus 

Specifies '+' for the 62nd character and '-' for the 63rd character.

kBase64MinusUnderscore 

Specifies '-' for the 62nd character and '_' for the 63rd character.

kBase64DotMinus 

Specifies '.' for the 62nd character and '-' for the 63rd character.

kBase64UnderscoreColon 

Specifies '_' for the 62nd character and ':' for the 63rd character.

kBase64UnderscoreMinus 

Specifies '_' for the 62nd character and '-' for the 63rd character.

kBase64DotUnderscore 

Specifies '.' for the 62nd character and '_' for the 63rd character.

kBase64ExclamationMinus 

Specifies '!' for the 62nd character and '-' for the 63rd character.

kBase64Default 

Provides the same result as kBase64PlusSlash.

kBase64UrlSafe 

Provides the same result as kBase64MinusUnderscore.

Definition at line 142 of file Base64.h.

Member Function Documentation

◆ Close()

nn::nlib::Base64Decoder::Close ( size_t *  written,
void *  dst,
size_t  dstsize 
)
noexcept

Finishes split decoding of Base64.

Parameters
[out]writtenThe number of bytes written to dst.
[out]dstThe buffer that stores the decoded data.
[in]dstsizeSize of the buffer specified by dst.
Return values
0Success.
EINVALwritten or dst was NULL.
EINVALIndicates that an error has previously occurred.
ERANGEdstsize was smaller than the necessary size.
Description
dst requires a space of at least 2 bytes.

◆ Decode() [1/2]

nn::nlib::Base64Decoder::Decode ( size_t *  written,
void *  dst,
size_t  dstsize,
const char *  src,
CharOption  char_option = kBase64Default 
)
inlinestaticnoexcept

Decodes data in batch.

Parameters
[out]writtenSize of the data output to dst.
[out]dstThe buffer that the decoded data is output to.
[in]dstsizeSize of the buffer specified by dst.
[in]srcEncoded string.
[in]char_optionSpecifies the 62nd and 63rd characters.
Returns
Returns 0 on success.
Description
Sample code is provided below.
const char* base64 = "RGF0YSB0byBlbmNvZGU";
auto result = nlib_ns::Base64Decoder::Decode(base64);
SUCCEED_IF(std::get<0>(result) == 0);
auto data = std::move(std::get<1>(result));
size_t size = std::get<2>(result);
const char* p = reinterpret_cast<const char*>(data.get());
std::string str(p, p + size);
nlib_printf("decoded: %s\n", str.c_str());
/*
Output:
decoded: Data to encode
*/

Definition at line 158 of file Base64.h.

◆ Decode() [2/2]

nn::nlib::Base64Decoder::Decode ( UniquePtr< uint8_t[]> &  dst,
size_t *  dstsize,
const char *  src,
CharOption  char_option = kBase64Default 
)
inlinestaticnoexcept

Decodes data in batch.

Parameters
[out]dstDecoded data.
[out]dstsizeSize of the data specified by dst.
[in]srcEncoded string.
[in]char_optionSpecifies the 62nd and 63rd characters.
Return values
0Success.
EINVALdstsize or src was NULL.
ENOMEMMemory allocation has failed.
EILSEQOne or more invalid characters were found.

Definition at line 184 of file Base64.h.

◆ DecodeInplace()

nn::nlib::Base64Decoder::DecodeInplace ( size_t *  written,
char *  src,
CharOption  char_option = kBase64Default 
)
staticnoexcept

Decodes data immediately.

Parameters
[out]writtenThe size of data written to src.
[in,out]srcEncoded string and its decoded data.
[in]char_optionSpecifies the 62nd and 63rd characters.
Return values
0Success.
EINVALwritten or src was NULL.
EILSEQOne or more invalid characters were found.
Description
If EILSEQ is returned, zero is written to written even if the decoding has partially completed.
Sample code is provided below.
char base64[] = "RGF0YSB0byBlbmNvZGUgc3RlcDEKRGF0YSB0byBlbmNvZGUgc3RlcDIK";
size_t written;
SUCCEED_IF(e == 0);
base64[written] = '\0';
nlib_printf("decoded:\n%s", base64);
/*
Output:
decoded:
Data to encode step1
Data to encode step2
*/

◆ GetRequiredSize()

nn::nlib::Base64Decoder::GetRequiredSize ( size_t  srcsize)
inlinestaticnoexcept

Calculates the size of the memory space required for decoding the data.

Parameters
[in]srcsizeSize of the data to be decoded.
Returns
Returns the value of ((srcsize + 3) / 4) * 3.

Definition at line 157 of file Base64.h.

◆ Init()

nn::nlib::Base64Decoder::Init ( CharOption  char_option = kBase64Default)
noexcept

Initializes the object by specifying the 62nd and 63rd characters.

Parameters
[in]char_optionSpecifies the 62nd and 63rd characters.
Return values
0Success.
EALREADYIndicates that the initialization has already completed and no error has occurred.

◆ StepDecode()

nn::nlib::Base64Decoder::StepDecode ( size_t *  written,
void *  dst,
size_t  dstsize,
const char *  src,
size_t  srcsize 
)
noexcept

Runs split decoding of Base64.

Parameters
[out]writtenThe number of bytes written to dst.
[out]dstThe buffer that stores the decoded data.
[in]dstsizeSize of the buffer specified by dst.
[in]srcData to be decoded.
[in]srcsizeSize of the data specified by src.
Return values
0Success.
EINVALwritten, dst or src was NULL.
EINVALIndicates that an error has previously occurred.
ERANGEdstsize was smaller than the necessary size.
EILSEQOne or more invalid characters were found.
Description
The buffer size necessary for dst can be calculated with GetRequiredSize(srcsize). If EILSEQ is returned, zero is written to written even if the decoding has partially completed.
Sample code is provided below.
char dst[256];
char* p = &dst[0];
size_t dstsize = 256;
const char* base64[] = {
"RGF0YS", "B0byBlbmNvZ", "GU", "gc3RlcDEKRGF0YSB", "0byBlbm", "NvZGUgc3RlcDIK"
};
for (auto& str : base64) {
auto result = decoder.StepDecode(p, dstsize, str, nlib_strlen(str));
SUCCEED_IF(result.first == 0);
p += result.second;
dstsize -= result.second;
}
auto result = decoder.Close(p, dstsize);
SUCCEED_IF(result.first == 0);
p += result.second;
dstsize -= result.second;
SUCCEED_IF(dstsize > 0);
*p = '\0';
nlib_printf("decoded:\n%s", dst);
/*
Output:
decoded:
Data to encode step1
Data to encode step2
*/

The documentation for this class was generated from the following files: