Encoding Utilities

Headers: lwip/cryptography/base64.h, lwip/cryptography/bytes.h
References: RFC 4648 — Base64

lwip/cryptography/base64.h — Base64 encode/decode

Standard Base64 (RFC 4648) with +// alphabet and = padding. Both functions return the number of bytes written. Output buffer sizing: encode output is 4 * ((input_len + 2) / 3) bytes; decode output is at most input_len * 3 / 4 bytes.

size_t tls_base64_encode(const uint8_t *inbuf, size_t len, uint8_t *outbuf)

Encode binary data as Base64.

Parameters:
  • inbuf – Input binary data.

  • len – Length of input in bytes.

  • outbuf – Output buffer (at least 4 * ((len + 2) / 3) bytes).

Returns:

Number of bytes written to outbuf.

size_t tls_base64_decode(const uint8_t *inbuf, size_t len, uint8_t *outbuf)

Decode Base64 data to binary.

Parameters:
  • inbuf – Base64-encoded input.

  • len – Length of input in bytes.

  • outbuf – Output buffer (at least len * 3 / 4 bytes).

Returns:

Number of bytes written to outbuf.

uint8_t encoded[512];
size_t enc_len = tls_base64_encode(binary, bin_len, encoded);

uint8_t decoded[256];
size_t dec_len = tls_base64_decode(encoded, enc_len, decoded);