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
 For more information, see Base64Encoder::CharOption.
 

Public Member Functions

 Base64Decoder () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~Base64Decoder () noexcept
 Destructor.
 
errno_t Init (CharOption char_option=kBase64Default) noexcept
 Initializes the object by specifying the 62nd and 63rd characters. More...
 
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, uint8_t(&dst)[N], const char *src, size_t srcsize) noexcept
 Runs StepDecode(written, dst, N, src, srcsize).
 
errno_t Close (size_t *written, void *dst, size_t dstsize) noexcept
 Finishes split decoding of Base64. More...
 
 operator bool () const
 Returns true if the initialization has completed and no error has occurred.
 

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, uint8_t *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, uint8_t(&dst)[N], const char *src, CharOption char_option=kBase64Default) noexcept
 Runs Decode(written, dst, N, src, srcsize, char_option).
 
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 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.
Batch decoding is available with the following code.
UniquePtr<uint8_t[]> dst;
size_t dstsize;
errno_t e = Base64Decoder::Decode(dst, &dstsize, src);
if (nlib_is_error(e)) { ERROR; }
Split decoding can be performed with the following code.
Base64Decoder decoder;
....
errno_t MyStepDecode(uint8_t** dst, size_t* dstsize, const char* src, size_t srcsize) {
size_t written;
errno_t e = decoder.StepDecode(&written, *dst, *dstsize, src, srcsize);
if (nlib_is_error(e)) { ERROR; }
*dst += written;
*dstsize -= written;
return 0;
}
errno_t MyCloseDecode(char** dst, size_t* dstsize) {
size_t written;
errno_t e = decoder.Close(&written, *dst, *dstsize);
if (nlib_is_error(e)) { ERROR; }
*dst += written;
*dstsize -= written;
return 0;
}
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 130 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,
uint8_t *  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.

Definition at line 161 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 178 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.

◆ 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 158 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.

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