Base64 Library

Base64 is an encoding for representing arbitrary data as a printable ASCII string. 4 Bytes in base64 encoding represent 3 Bytes of the data.

File information

Filecommon/ilib/base64.h

Public functions encode_base64
decode_base64
decode_base64_bin
Examples encode_base64
decode_base64
decode_base64_bin

The same functions exist with name ..._base64url for base 64 encoding with URL and filename safe alphabet, see RFC 4648 section 5.

Public functions

encode_base64
Encodes arbitrary data into a null-terminated base64 string. This increases the needed space by 4/3 plus one byte for the null-termination.

Parameters

const byte * in The data that shall be encoded.
char * out The function writes the null-terminated base64 string to this buffer. The buffer has to be large enough to contain 4/3 * len + 1 bytes.
int len The number of bytes that shall be encoded.

Return value

dword The number of bytes that have been written to the out buffer.
decode_base64
Decodes a base64 string into a null-terminated plain string.

Parameters

const char * in The base64 string to be decoded.
byte * out The function writes the decoded data to this buffer and places an additional null byte at the end.
int n The number of bytes in the in buffer to be decoded.

Return value

dword The number of bytes that have been written to the out buffer.
decode_base64_bin
Decodes a null-terminated base64 string.

Parameters

const char * in The null-terminated base64 string to be decoded.
byte * out The function writes the decoded data to this buffer.
unsigned size_of_out The size of the out buffer.

Return value

dword The number of bytes that have been written to the out buffer.

Code Example

encode_base64

const char * in = "This is an example text";
char out[64];
dword out_len = encode_base64(in, out, strlen(in));

decode_base64

const char * in = "VGhpcyBpcyBhbiBleGFtcGxlIHRleHQ=";
char out[64];
dword out_len = decode_base64(in, out, strlen(in));

decode_base64_bin

const char * in = "VGhpcyBpcyBhbiBleGFtcGxlIHRleHQ=";
char out[64];
dword out_len = decode_base64_bin(in, out, sizeof(out));