Encodes Base64. This class supports various variants of Base64.
More...
#include "nn/nlib/Base64.h"
|
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 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...
|
|
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.
- Split encoding can be performed with the following code. Note that generated strings will not be null-terminated.
....
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)
*dst += written;
*dstsize -= written;
return 0;
}
errno_t MyCloseEncode(
char** dst,
size_t* dstsize) {
errno_t e = encoder.Close(&written, *dst, *dstsize,
false);
*dst += written;
*dstsize -= written;
if (dstsize == 0) { ERROR; }
*dst = '\0';
return 0;
}
- The transition of the object state.
- The overview of the object state transitions is described below:
- See also
- https://www.ietf.org/rfc/rfc2045.txt
Definition at line 27 of file Base64.h.
◆ 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.
◆ 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] | written | The number of bytes written to dst. |
[out] | dst | The buffer that stores the encoded data. |
[in] | dstsize | Size of the buffer specified by dst. |
[in] | padding | If true , outputs zero to two '=' characters that are padded at the end. |
- Return values
-
0 | Success. |
EINVAL | written or dst was NULL . |
EINVAL | Indicates that an error has previously occurred. |
ERANGE | dstsize 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] | dst | The buffer that the encoded string is output to. |
[in] | dstsize | Size of the buffer specified by dst. |
[in] | src | Data to be encoded. |
[in] | srcsize | Size of the data specified by src. |
[in] | char_option | Specifies the 62nd and 63rd characters. |
[in] | padding | If true , outputs zero to two '=' characters that are padded at the end. |
- Returns
- Returns
0
on success.
◆ Encode() [2/2]
Encodes data in batch.
- Parameters
-
[out] | dst | Encoded string. |
[in] | src | Data to be encoded. |
[in] | srcsize | Size of the data specified by src. |
[in] | char_option | Specifies the 62nd and 63rd characters. |
[in] | padding | If true , outputs zero to two '=' characters that are padded at the end. |
- Return values
-
0 | Success. |
EINVAL | When src was NULL . |
ENOMEM | Memory 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] | srcsize | Size 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()
Initializes the object by specifying the 62nd and 63rd characters.
- Parameters
-
[in] | char_option | Specifies the 62nd and 63rd characters. |
- Return values
-
0 | Success. |
EALREADY | Indicates 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] | written | The number of bytes written to dst. |
[out] | dst | The buffer that stores the encoded data. |
[in] | dstsize | Size of the buffer specified by dst. |
[in] | src | Data to be encoded. |
[in] | srcsize | Size of the data specified by src. |
- Return values
-
0 | Success. |
EINVAL | written, dst or src is NULL . |
EINVAL | Indicates that an error has previously occurred. |
ERANGE | dstsize 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: