Base64
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.
Header
#include <encode/base64.h>
Functions
uint32_t | b64_encoded_size (...) |
uint32_t | b64_decoded_size (...) |
uint32_t | b64_encode (...) |
uint32_t | b64_decode (...) |
String* | b64_encode_from_stm (...) |
String* | b64_encode_from_file (...) |
String* | b64_encode_from_data (...) |
String* | b64_encode_from_str (...) |
String* | b64_encode_from_cstr (...) |
Buffer* | b64_decode_from_str (...) |
Buffer* | b64_decode_from_data (...) |
Base64 is a coding system that converts binary data (such as images or files) into a text string composed of ASCII characters for transmission and storage, especially in environments that only admit text (such as emails, URLs, JSON, XML). As the name implies, 64 symbols are used for coding that are A-Z, a-z, 0-9 in this order for the first 62 digits, plus two additional characters +/. The protocol is relatively simple and can be summarized in:
- Divide the binary data into blocks of 3 bytes (24 bits).
- Each block is divided into 4 groups of 6 bits.
- Each 6-bit group translates to a character of the 64 allowed.
- Base64 encoding increases data size by approximately 33%, since every 3 bytes become 4 characters.
- It allows to send and store binary data in systems that only accept flat text.
- Does not encrypt or protect the data, it only represents them in a textual way.
b64_encoded_size ()
Get the number of bytes needed to encode a memory block in format base64.
uint32_t b64_encoded_size(const uint32_t data_size);
data_size | The original block size. |
Return
Base64 size.
b64_decoded_size ()
Get the number of bytes needed to decode a block of memory in base64 format.
uint32_t b64_decoded_size(const uint32_t data_size);
data_size | The block size encoded in base64. |
Return
The size in bytes.
b64_encode ()
Encode a block of memory in base64.
uint32_t b64_encode(const byte_t *data, const uint32_t size, char_t *base64);
data | The data block. |
size | Block size. |
base64 | The buffer where to store the result. |
Return
The size in bytes.
Remarks
The buffer base64
must be at least the size returned by b64_encoded_size.
b64_decode ()
Decodes a block base64.
uint32_t b64_decode(const char_t *base64, const uint32_t size, byte_t *data);
base64 | The base64 block. |
size | Block size. |
data | The buffer where to store the result. |
Return
The size in bytes.
Remarks
The buffer data
must be at least the size returned by b64_decoded_size.
b64_encode_from_stm ()
Create a base64 with the contents of an input stream.
String* b64_encode_from_stm(Stream *stm);
stm | Input stream. |
Return
Base64 text string.
b64_encode_from_file ()
Create a base64 with the contents of a file.
String* b64_encode_from_file(const char_t *pathname, ferror_t *error);
pathname | File path. |
error | Error code file opening fails. It can be |
Return
Base64 text string.
Remarks
Will return an empty string (not NULL
) if it cannot open the file.
b64_encode_from_data ()
Create a base64 from an in-memory buffer.
String* b64_encode_from_data(const byte_t *data, const uint32_t size);
data | Data block. |
size | Data block size. |
Return
Base64 text string.
b64_encode_from_str ()
Create a base64 from a String.
String* b64_encode_from_str(const String *str);
str | String. |
Return
Base64 text string.
b64_encode_from_cstr ()
Create a base64 from a C string.
String* b64_encode_from_cstr(const char_t *str);
str | Char string in UTF8, finished in a null character ( |
Return
Base64 text string.
b64_decode_from_str ()
Decodes a text in base64.
Buffer* b64_decode_from_str(const String *base64);
base64 | Base64 string. |
Return
Buffer with the result.
b64_decode_from_data ()
Decodes a text in base64.
Buffer* b64_decode_from_data(const byte_t *data, const uint32_t size);
data | Data buffer containing base64 text. |
size | Size in bytes of |
Return
Buffer with the result.