etl/docs/codecs/base64.md
John Wellbelove 4a88884b39
Issue/add hugo support for documentation (#1449)
* Add ranges

* Initial Hugo setup

* Work in progress

* Added selection for local or remote site

* Updated to 'light' theme

* Changed to using Hextra Hugo theme

* Changed to using Hextra Hugo theme

* Changed to Hextra Hugo theme

* Change to Hextra Hugo theme

* Updated Hugo setup.

* Updated Hugo setup.

# Conflicts:
#	docs/releases/_index.md

* Work in progress

* Added new fonts

Added new documentation

* Latest documentation updates

* Latest documentation updates

# Conflicts:
#	docs/containers/array.md
#	docs/containers/array_view.md
#	docs/containers/array_wrapper.md
#	docs/containers/bip_buffer_spsc_atomic.md
#	docs/containers/bitset.md
#	docs/containers/indirect_vector.md
#	docs/containers/vector.md
#	docs/getting-started/compilers.md

* Added bloom_filter markdown doc

* Added more documentation

Updated CSS for light and dark modes

* Fixed some menus

Added mode documentation files

* Updated CSS rules

Added badges to home page
Added uniqur_ptr + pool tutorial

* Fixed formatting on the home page markdown

Modified light amd dark code formatting

* Updated unique_ptr-with-pool

* Added container and shared message tutorials

* Updates to documentation

* Added const_multimap

* Updated source-formatting.md

* Added initial raw text files form Web site editor

* Innore coverage build directory

* Exported raw text documentation files from the web site editor

* Hugo updates

* Added Hugo intalation and markdown descriptions

* More addition to the documentation

* Added closure.md and updates to delegate.md

* Added format.md

* Added documentation for etl::delegate_observable, etl::function, Base64 codec

* Added io_port documentation

* Added basic_format_spec

* Added documentation for string_stream and string utilities.

* Added more documentation

Updated the documentation CSS

* Added documentation for clocks, day, duration

* Added more documentation for chrono classes

Updated callouts

* More chrono documentation

* Completed chrono documentation

* Maths functions documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Added multiple documentation files

* Added iterator.md

* Added debug_count.md and versions.md

* Added debug_count.md and versions.md

* Added more documentation

* More documentation

* Added some design pattern documentation

Modified some of the layout files
Modified the About documentation

* Converted more documentation pages

Modified the site CSS

* Added more documentation

Moced some documentation files to new directories

* Added more documentation

Tweaks to CSS

* Added callback_timer_deferred_locked documentation

* Added callback_timer_locked documentation

* More documentation updates

* More documentation updates

* More documentation updates

* New documentation files.

Harmonised file name format

* New documentation files.

* Multiple document updates

* Multiple document updates

* Final conversion of web pages

* Updates before PR

* Updates before PR

* Updates before PR

# Conflicts:
#	docs/blog/_index.md

* Final pre PR updates

* Updates to message framework documentation

* Renamed directory

* Fix spelling

* Added author and date to blog files

Moved documentation files merged from development

* Fixed 'Description' typo

* Fix typos

# Conflicts:
#	docs/IO/io_port.md
#	docs/containers/sets/const-multiset.md
#	docs/containers/sets/const-set.md
#	docs/maths/correlation.md
#	docs/maths/gamma.md

* Renamed two files to lower case

* Minor renaming

* Added author and date

* Updated callout on bresenham_line.md

Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset.
Updated the Python 'update_release.py' to copy 'version.txt'

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

# Conflicts:
#	docs/getting-started/view-the-docs-locally/hugo-commands.md

* Added a link to pseudo_moving_average.md

* Updated title pages for groups

* Fixed missing 404 for non-existent pages

* Fixed coordinate variable names in the 'Calculating the intersection' example

---------

Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
2026-06-06 13:12:44 +01:00

314 lines
8.4 KiB
Markdown

---
title: "Base64"
---
{{< callout type="info">}}
Headers:
`base64.h` Common definitions
`base64_encoder.h` Encoder class
`base64_decoder.h` Decoder class
From: `20.38.4`
{{< /callout >}}
Encodes and decodes data to and from Base64 format.
## Encode
**Input -> pointer/length : Output -> pointer/length**
```cpp
template <typename T>
ETL_CONSTEXPR14
static
size_t encode(const T* input, size_t input_length,
char* output, size_t output_length)
```
**Description**
Transforms the input data described by input and input_length to Base64 format to be stored in output output_length.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
**Return**
The length of the encoded data.
**Error**
If output_length is not large enough to hold the encoded data then an `etl::base64_overflow` is raised.
---
**Input -> pointer/length : Output -> string**
```cpp
template <typename T>
ETL_CONSTEXPR14
static
size_t encode(const T* input, size_t input_length,
etl::istring& output)
```
**Description**
Transforms the input data described by input and input_length to Base64 format to be stored in output.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
The output string must have capacity of at least `etl::base64::encode_size(input)`.
**Return**
The length of the encoded data.
**Error**
If output_length is not large enough to hold the encoded data then an `etl::base64_overflow` is raised.
---
**Input -> pointer/pointer : Output -> pointer/pointer**
```cpp
template <typename T>
ETL_CONSTEXPR14
static
size_t encode(const T* input_begin, const T* input_end,
char* output_begin, char* output_end)
```
**Description**
Transforms the input data described by input_begin and input_end to Base64 format to be stored in output_begin to output_end.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
**Return**
The length of the encoded data.
**Error**
If the output buffer is not large enough to hold the encoded data then an `etl::base64_overflow` is raised.
---
**Input -> pointer/pointer : Output -> string**
```cpp
template <typename T>
ETL_CONSTEXPR14
static
size_t encode(const T* input_begin, const T* input_end,
etl::istring& output)
```
**Description**
Transforms the input data described by input_begin and input_end to Base64 format to be stored in output.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
The output string must have capacity of at least `etl::base64::encode_size(input)`.
**Return**
The length of the encoded data.
**Error**
If the output buffer is not large enough to hold the encoded data then an `etl::base64_overflow` is raised.
---
**Input -> span : Output -> span**
```cpp
template <typename T, size_t Length1, size_t Length2>
ETL_CONSTEXPR14
static
size_t encode(const etl::span<const T, Length1>& input_span,
const etl::span<char, Length2>& output_span)
```
**Description**
Transforms the input data described by input_span to Base64 format to be stored in output_span.
Enabled if etl::is_integral<T> is true and etl::integral_limits<T>::bits == 8.
**Return**
The length of the encoded data.
**Error**
If the output span is not large enough to hold the encoded data then an etl::base64_overflow is raised.
---
**Input -> span : Output -> string**
```cpp
template <typename T, size_t Length>
ETL_CONSTEXPR14
static
size_t encode(const etl::span<const T, Length>& input_span,
etl::istring& output)
```
**Description**
Transforms the input data described by input_span to Base64 format to be stored in output.
Enabled if etl::is_integral<T> is true and etl::integral_limits<T>::bits == 8
The output string must have capacity of at least etl::base64::encode_size(input).
**Return**
The length of the encoded data.
**Error**
If the output span is not large enough to hold the encoded data then an etl::base64_overflow is raised.
---
```cpp
ETL_NODISCARD
ETL_CONSTEXPR14
static
size_t encode_size(size_t input_length)
```
**Return**
Returns the size of the output buffer required to encode the specified input data length.
## Decode
**Input -> pointer/length : Output -> pointer/length**
```cpp
template <typename T>
ETL_CONSTEXPR14
static
size_t decode(const char* input, size_t input_length,
T* output, size_t output_length)
```
**Description**
Transforms the Base64 format input data described by input and input_length to by output and output_length.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
**Return**
The length of the decoded data.
**Error**
If `output_length` is not large enough to hold the decoded data then an `etl::base64_overflow` is raised.
---
**Input -> pointer/pointer: Output -> pointer/pointer**
```cpp
template <typename T>
ETL_CONSTEXPR14
static
size_t decode(const char* input_begin, const char* input_end,
T* output_begin, T* output_end)
```
**Description***
Transforms the Base64 format input data described by `input_begin` and `input_end` to `output_begin` and `output_end`.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
**Return**
The length of the decoded data.
**Error**
If the output buffer is not large enough to hold the decoded data then an `etl::base64_overflow` is raised.
---
**Input -> span: Output -> span**
```cpp
template <typename T, size_t Length1, size_t Length2>
ETL_CONSTEXPR14
static
size_t decode(const etl::span<const char, Length1>& input_span,
const etl::span<T, Length2>& output_span)
```
**Description**
Transforms the Base64 format input data described by input_span to output_span.
Enabled if `etl::is_integral<T>` is `true` and `etl::integral_limits<T>::bits == 8`.
**Return**
The length of the decoded data.
**Error**
If the output span is not large enough to hold the decoded data then an `etl::base64_overflow` is raised.
---
```cpp
ETL_NODISCARD
ETL_CONSTEXPR14
static
size_t decode_size(const char* input, size_t input_length)
```
**Return**
The size of the output buffer required to decode the specified input Base64 data length.
---
```cpp
ETL_NODISCARD
ETL_CONSTEXPR14
static
size_t decode_size(const char* input_begin, const char* input_end)
```
**Return**
The size of the output buffer required to decode the specified input Base64 data length.
---
```cpp
ETL_NODISCARD
ETL_CONSTEXPR14
static
size_t decode_size(etl::span<const char> input)
```
**Return**
The size of the output buffer required to decode the specified input Base64 data length.
## Examples
### Encode
```cpp
const etl::array<int8_t, 12> input =
{
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 // "Hello World!"
};
etl::string<20> encoded;
size_t encoded_size = etl::base64::encode(input.data(), input.size(),
encoded.data(), encoded.size());
encoded.resize(encoded_size);
// encoded == "OycDQy37KCphrg=="
```
---
*For C++14 or above*
```cpp
const etl::array<int8_t, 12> input =
{
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 // "Hello World!"
};
constexpr size_t length = etl::base64::encode_size(input.size());
etl::string<length> output;
output.uninitialized_resize(output.capacity());
etl::base64::encode(input.data(), input.size(), output.data(), output.size());
// output == "OycDQy37KCphrg=="
```
---
### Decode
```cpp
const etl::string<16> encoded = "OycDQy37KCphrg==";
etl::vector<int8_t, 20> output;
size_t decoded_size = etl::base64::decode(encoded.data(), encoded.size(),
output.data(), output.size());
output.resize(decoded_size);
// output == { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
// 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
```
---
*For C++14 or above*
```cpp
// "OycDQy37KCphrg=="
constexpr const etl::array<char, 16> input =
{
0x4F, 0x79, 0x63, 0x44, 0x51, 0x79, 0x33, 0x37,
0x4B, 0x43, 0x70, 0x68, 0x72, 0x67, 0x3D, 0x3D
};
constexpr size_t length = etl::base64::decode_size(input.data(), input.size());
etl::vector<int8_t, length> output;
etl::base64::decode(input.data(), input.size(), output.data(), output.size());
// output == { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
// 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
```