Base64 module

base64_decode

procedure · 1 overloadSource

The bytes that input encodes, and whether all of it was valid.

base64_decode :: (
    input: string,
    table: [256] u8 = BASE64_DECODER_TABLE
) -> (string, success: bool)

Line feeds are skipped. A = ends the decoding and anything after it is ignored. Padding is optional. Input is invalid, and success is false, when it holds a byte that is not in table (a carriage return, a space or a byte outside the alphabet, for example) or when the symbols before the end or the first = leave a lone symbol, which cannot encode a byte ("T", "TWFuT").

When success is false the result is an empty string, never the part that decoded before the problem, so a caller that forgets to check success does not use truncated data. Otherwise the result is allocated with the context allocator and the caller frees it; freeing the empty failure result is harmless.

Parameters

input: string
table: [256] u8

Returns

string
success: bool
ExampleOpen in playground ↗
decoded, ok := base64_decode("TWFu\nTWE=");
defer free(decoded);
assert(ok && decoded == "ManMa");

failed, failed_ok := base64_decode("TW!u");
defer free(failed);
assert(!failed_ok && failed == "");

lone, lone_ok := base64_decode("T");
defer free(lone);
assert(!lone_ok);

See also