Skip to content

File syn_base64.c

FileList > src > syntropic > util > syn_base64.c

Go to the source code of this file

RFC 4648 Base64 and Base64URL codec implementation.

  • #include "syn_base64.h"
  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>
  • #include <string.h>

Public Static Attributes

Type Name
const char B64_STD_TABLE = /* multi line expression */
Standard RFC 4648 Base64 character encoding table.
const char B64_URL_TABLE = /* multi line expression */
URL and filename-safe RFC 4648 Base64URL character encoding table.

Public Functions

Type Name
bool syn_base64_decode (const char * src, size_t src_len, uint8_t * dst, size_t dst_size, size_t * out_len)
Decode an RFC 4648 standard Base64 string into binary data.
bool syn_base64_encode (const uint8_t * src, size_t src_len, char * dst, size_t dst_size, size_t * out_len)
Encode binary data to standard RFC 4648 Base64 string.
bool syn_base64url_decode (const char * src, size_t src_len, uint8_t * dst, size_t dst_size, size_t * out_len)
Decode an RFC 4648 Base64URL string into binary data.
bool syn_base64url_encode (const uint8_t * src, size_t src_len, char * dst, size_t dst_size, bool with_padding, size_t * out_len)
Encode binary data to RFC 4648 Base64URL string.

Public Static Functions

Type Name
bool base64_decode_internal (const char * src, size_t src_len, uint8_t * dst, size_t dst_size, size_t * out_len, bool url_mode)
Internal shared decoder for standard and URL-safe Base64.
uint8_t decode_char (char c, bool url_mode)
Decode single ASCII character into 6-bit value or control marker.

Macros

Type Name
define B64_VAL_INVALID 0xFFU
Marker value for non-base64 character.
define B64_VAL_PAD 0xFEU
Marker value for base64 padding '=' character.
define B64_VAL_WS 0xFDU
Marker value for whitespace character.

Public Static Attributes Documentation

variable B64_STD_TABLE

Standard RFC 4648 Base64 character encoding table.

const char B64_STD_TABLE[];


variable B64_URL_TABLE

URL and filename-safe RFC 4648 Base64URL character encoding table.

const char B64_URL_TABLE[];


Public Functions Documentation

function syn_base64_decode

Decode an RFC 4648 standard Base64 string into binary data.

bool syn_base64_decode (
    const char * src,
    size_t src_len,
    uint8_t * dst,
    size_t dst_size,
    size_t * out_len
) 

Automatically skips whitespace characters (spaces, tabs, CR, LF). Rejects invalid non-base64 characters and malformed padding.

Parameters:

  • src Input Base64 string (null-terminated or bounded by src_len).
  • src_len Length of input string (or 0 to determine via strlen).
  • dst [out] Output binary buffer.
  • dst_size Capacity of output buffer in bytes.
  • out_len [out] Optional pointer to receive actual binary bytes written.

Returns:

true on success, false if input contains invalid characters or destination is too small.


function syn_base64_encode

Encode binary data to standard RFC 4648 Base64 string.

bool syn_base64_encode (
    const uint8_t * src,
    size_t src_len,
    char * dst,
    size_t dst_size,
    size_t * out_len
) 

Uses alphabet [A-Za-z0-9+/] and adds = padding when needed. Always null-terminates the output if dst_size > 0.

Parameters:

  • src Input binary data.
  • src_len Length of input binary data in bytes.
  • dst [out] Output string buffer.
  • dst_size Capacity of output buffer in bytes (must be >= syn_base64_calc_encoded_size(src_len)).
  • out_len [out] Optional pointer to receive character length written (excluding null).

Returns:

true on success, false if parameters are invalid or buffer is too small.


function syn_base64url_decode

Decode an RFC 4648 Base64URL string into binary data.

bool syn_base64url_decode (
    const char * src,
    size_t src_len,
    uint8_t * dst,
    size_t dst_size,
    size_t * out_len
) 

Supports both padded (=) and unpadded URL-safe strings.

Parameters:

  • src Input Base64URL string.
  • src_len Length of input string (or 0 to determine via strlen).
  • dst [out] Output binary buffer.
  • dst_size Capacity of output buffer in bytes.
  • out_len [out] Optional pointer to receive actual binary bytes written.

Returns:

true on success, false on invalid input or insufficient buffer capacity.


function syn_base64url_encode

Encode binary data to RFC 4648 Base64URL string.

bool syn_base64url_encode (
    const uint8_t * src,
    size_t src_len,
    char * dst,
    size_t dst_size,
    bool with_padding,
    size_t * out_len
) 

Uses URL-safe alphabet [A-Za-z0-9-_].

Parameters:

  • src Input binary data.
  • src_len Length of input binary data in bytes.
  • dst [out] Output string buffer.
  • dst_size Capacity of output buffer in bytes.
  • with_padding If true, appends = padding; if false, omits padding (standard JWT format).
  • out_len [out] Optional pointer to receive character length written (excluding null).

Returns:

true on success, false if buffer is too small or parameters invalid.


Public Static Functions Documentation

function base64_decode_internal

Internal shared decoder for standard and URL-safe Base64.

static bool base64_decode_internal (
    const char * src,
    size_t src_len,
    uint8_t * dst,
    size_t dst_size,
    size_t * out_len,
    bool url_mode
) 

Parameters:

  • src Input Base64 string buffer.
  • src_len Length of input string (or 0 for strlen).
  • dst [out] Output destination binary buffer.
  • dst_size Capacity of destination buffer in bytes.
  • out_len [out] Pointer to receive decoded byte count.
  • url_mode True for URL-safe alphabet, false for standard RFC 4648.

Returns:

True on success, false on decode error or buffer overflow.


function decode_char

Decode single ASCII character into 6-bit value or control marker.

static uint8_t decode_char (
    char c,
    bool url_mode
) 

Parameters:

  • c ASCII character to decode.
  • url_mode True if decoding in URL-safe mode (- and _).

Returns:

6-bit value (0-63) or marker constant (B64_VAL_PAD, B64_VAL_WS, B64_VAL_INVALID).


Macro Definition Documentation

define B64_VAL_INVALID

Marker value for non-base64 character.

#define B64_VAL_INVALID `0xFFU`


define B64_VAL_PAD

Marker value for base64 padding '=' character.

#define B64_VAL_PAD `0xFEU`


define B64_VAL_WS

Marker value for whitespace character.

#define B64_VAL_WS `0xFDU`



The documentation for this class was generated from the following file src/syntropic/util/syn_base64.c