dmi_base64_decode

Synopsis

#include <opendmi/utils/base64.h>

dmi_data_t *dmi_base64_decode(
        const char *data,
        size_t      data_length,
        size_t     *poutput_length);

Description

The dmi_base64_decode function decodes a Base64-encoded string into binary data.

The function allocates and returns a buffer containing the decoded binary data corresponding to the given Base64-encoded string. The caller is responsible for freeing the returned buffer.

The string is required to be what dmi_base64_encode gives: whole groups of four characters of the alphabet of RFC 4648, the last one padded with = for the bytes it is short of. Anything else is invalid, including white space and the characters of the variant of the alphabet for file names (- and _).

Parameters

data

Pointer to the Base64-encoded string to decode.

data_length

Length of data in bytes, excluding any null terminator.

poutput_length

If non-nullptr, receives the number of bytes in the returned buffer.

Return value

On success, returns a newly allocated buffer containing the decoded binary data. On failure, returns nullptr (e.g., allocation error, invalid Base64 input, or invalid arguments).

Errors

None.

Examples

const char *input = "SGVsbG8=";
size_t output_length;

dmi_data_t *decoded = dmi_base64_decode(input, strlen(input), &output_length);
if (decoded != nullptr) {
    printf("decoded %zu bytes\n", output_length);
    free(decoded);
}