nlib
nn::nlib::Base64Encoder Class Referencefinal

Encodes 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

constexpr Base64Encoder () 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...
 
errno_t StepEncode (size_t *written, 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. More...
 
template<size_t N>
errno_t StepEncode (size_t *written, char(&dst)[N], const void *src, size_t srcsize) noexcept
 Runs StepEncode(written, dst, N, src, srcsize).
 
errno_t Close (size_t *written, char *dst, size_t dstsize, bool padding) noexcept
 Finishes split encoding of Base64. No null character will be appended to the end. More...
 
template<size_t N>
errno_t Close (size_t *written, char(&dst)[N], bool padding) noexcept
 Runs Base64Encoder::Close(written, dst, N, padding).
 
 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 encoding the data. More...
 
static errno_t Encode (char *dst, size_t dstsize, const void *src, size_t srcsize, CharOption char_option=kBase64Default, bool padding=false) noexcept
 Encodes data in batch. More...
 
template<size_t N>
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).
 
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. More...
 

Detailed Description

Encodes Base64. This class supports various variants of Base64.

Description
This class supports batch encoding and split encoding of Base64. Various variants of Base64 can be supported by specifying CharOption type values. You can also specify whether or not to output with characters padded. However, no newline character will be output.
Batch encoding is available with the following code.
UniquePtr<char[]> dst;
errno_t e = Base64Encoder::Encode(dst, src, srcsize);
if (nlib_is_error(e)) { ERROR; }
Split encoding can be performed with the following code. Note that generated strings will not be null-terminated.
Base64Encoder encoder;
....
errno_t MyStepEncode(char** dst, size_t* dstsize, uint8_t* src, size_t srcsize) {
size_t written;
errno_t e = encoder.StepEncode(&written, *dst, *dstsize, src, srcsize)
if (nlib_is_error(e)) { ERROR; }
*dst += written;
*dstsize -= written;
return 0;
}
errno_t MyCloseEncode(char** dst, size_t* dstsize) {
errno_t e = encoder.Close(&written, *dst, *dstsize, false);
if (nlib_is_error(e)) { ERROR; }
*dst += written;
*dstsize -= written;
// not null terminated yet
if (dstsize == 0) { ERROR; }
*dst = '\0';
return 0;
}
The transition of the object state.
The overview of the object state transitions is described below:
dot_inline_dotgraph_2.png
See also
https://www.ietf.org/rfc/rfc2045.txt

Definition at line 27 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 29 of file Base64.h.

Member Function Documentation

◆ Close()

nn::nlib::Base64Encoder::Close ( size_t *  written,
char *  dst,
size_t  dstsize,
bool  padding 
)
noexcept

Finishes split encoding of Base64. No null character will be appended to the end.

Parameters
[out]writtenThe number of bytes written to dst.
[out]dstThe buffer that stores the encoded data.
[in]dstsizeSize of the buffer specified by dst.
[in]paddingIf true, outputs zero to two '=' characters that are padded at the end.
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 4 bytes.

◆ Encode() [1/2]

nn::nlib::Base64Encoder::Encode ( char *  dst,
size_t  dstsize,
const void *  src,
size_t  srcsize,
CharOption  char_option = kBase64Default,
bool  padding = false 
)
staticnoexcept

Encodes data in batch.

Parameters
[out]dstThe buffer that the encoded string is output to.
[in]dstsizeSize of the buffer specified by dst.
[in]srcData to be encoded.
[in]srcsizeSize of the data specified by src.
[in]char_optionSpecifies the 62nd and 63rd characters.
[in]paddingIf true, outputs zero to two '=' characters that are padded at the end.
Returns
Returns 0 on success.

◆ Encode() [2/2]

nn::nlib::Base64Encoder::Encode ( UniquePtr< char[]> &  dst,
const void *  src,
size_t  srcsize,
CharOption  char_option = kBase64Default,
bool  padding = false 
)
inlinestaticnoexcept

Encodes data in batch.

Parameters
[out]dstEncoded string.
[in]srcData to be encoded.
[in]srcsizeSize of the data specified by src.
[in]char_optionSpecifies the 62nd and 63rd characters.
[in]paddingIf true, outputs zero to two '=' characters that are padded at the end.
Return values
0Success.
EINVALWhen src was NULL.
ENOMEMMemory allocation has failed.

Definition at line 71 of file Base64.h.

◆ GetRequiredSize()

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

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

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

Definition at line 55 of file Base64.h.

◆ Init()

nn::nlib::Base64Encoder::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.

◆ StepEncode()

nn::nlib::Base64Encoder::StepEncode ( size_t *  written,
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.

Parameters
[out]writtenThe number of bytes written to dst.
[out]dstThe buffer that stores the encoded data.
[in]dstsizeSize of the buffer specified by dst.
[in]srcData to be encoded.
[in]srcsizeSize of the data specified by src.
Return values
0Success.
EINVALwritten, dst or src is NULL.
EINVALIndicates that an error has previously occurred.
ERANGEdstsize was smaller than the necessary size.
Description
The buffer size necessary for dst can be calculated with GetRequiredSize(srcsize).

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