title: src/crelude/base64.h


src/crelude/base64.h

More...

Functions

Name
i8 *base64_decode_table(void )
boolis_base64_digit(byte d)
usizebase64_encoded_size(usize )
Calculate encoded size given original size.
usizebase64_decoded_size(MemSlice )
Calculate decoded size given the endcoded data.
MemSlicebase64_encode(MemSlice )
MemSlicebase64_decode(MemSlice )

Attributes

Name
byte *value
usizelen
const struct @16BASE64_DIGITS
Encoding characters / digits, each representing a value between 0..63.
const struct @17BASE64_INVERSES
Inverse/decoding table. (-1) represents absence from the table.

Detailed Description

Base-64 encoding and decoding.

Functions Documentation

function base64_decode_table

static i8 * base64_decode_table(
    void 
)

Generates the above inverses table (length 80). This could be a constexpr or something in a better language.

function is_base64_digit

static inline bool is_base64_digit(
    byte d
)

function base64_encoded_size

usize base64_encoded_size(
    usize 
)

Calculate encoded size given original size.

function base64_decoded_size

usize base64_decoded_size(
    MemSlice 
)

Calculate decoded size given the endcoded data.

function base64_encode

MemSlice base64_encode(
    MemSlice 
)

Return: A slice holding a heap-allocated free-able pointer to the encoded data.

Encode bytes into base-64.

function base64_decode

MemSlice base64_decode(
    MemSlice 
)

Return: A heap allocated slice holding the raw decoded data.

Decode from base-64 to raw bytes. If input is not a valid base-64 string, an empty nil-slice will be returned.

Attributes Documentation

variable value

byte * value;

variable len

usize len;

variable BASE64_DIGITS

static const struct @16 BASE64_DIGITS = 
{   .[len](/crelude/Files/base64_8h.md#variable-len) = sizeof(( [byte](/crelude/Files/common_8h.md#typedef-byte) []) { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" } )/sizeof( [byte](/crelude/Files/common_8h.md#typedef-byte) ),   .[value](/crelude/Files/common_8h.md#variable-value) = ( [byte](/crelude/Files/common_8h.md#typedef-byte) []) { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" }   };

Encoding characters / digits, each representing a value between 0..63.

variable BASE64_INVERSES

static const struct @17 BASE64_INVERSES = 









{   .[len](/crelude/Files/base64_8h.md#variable-len) = sizeof(( [i8](/crelude/Files/common_8h.md#typedef-i8) []) {      62, -1, -1, -1, 63, 52, 53, 54, 55, 56,      57, 58, 59, 60, 61, -1, -1, -1, -1, -1,      -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,       8,  9, 10, 11, 12, 13, 14, 15, 16, 17,      18, 19, 20, 21, 22, 23, 24, 25, -1, -1,      -1, -1, -1, -1, 26, 27, 28, 29, 30, 31,      32, 33, 34, 35, 36, 37, 38, 39, 40, 41,      42, 43, 44, 45, 46, 47, 48, 49, 50, 51   } )/sizeof( [i8](/crelude/Files/common_8h.md#typedef-i8) ),   .[value](/crelude/Files/common_8h.md#variable-value) = ( [i8](/crelude/Files/common_8h.md#typedef-i8) []) {      62, -1, -1, -1, 63, 52, 53, 54, 55, 56,      57, 58, 59, 60, 61, -1, -1, -1, -1, -1,      -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,       8,  9, 10, 11, 12, 13, 14, 15, 16, 17,      18, 19, 20, 21, 22, 23, 24, 25, -1, -1,      -1, -1, -1, -1, 26, 27, 28, 29, 30, 31,      32, 33, 34, 35, 36, 37, 38, 39, 40, 41,      42, 43, 44, 45, 46, 47, 48, 49, 50, 51   }   };

Inverse/decoding table. (-1) represents absence from the table.

Source code


#pragma once
#include "common.h"

static const sliceof(byte) BASE64_DIGITS = INIT(byte,
    { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" });

static const sliceof(i8) BASE64_INVERSES = INIT(i8, {
//  ------------------10------------------
    62, -1, -1, -1, 63, 52, 53, 54, 55, 56, // |
    57, 58, 59, 60, 61, -1, -1, -1, -1, -1, // |
    -1, -1,  0,  1,  2,  3,  4,  5,  6,  7, // |
     8,  9, 10, 11, 12, 13, 14, 15, 16, 17, // 8
    18, 19, 20, 21, 22, 23, 24, 25, -1, -1, // |
    -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, // |
    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, // |
    42, 43, 44, 45, 46, 47, 48, 49, 50, 51  // |
});

static i8 *base64_decode_table(void) __attribute__((unused));
static i8 *base64_decode_table()
{
    static i8 table[80];
    memset(table, -1, sizeof(table));

    for (usize i = 0; i < BASE64_DIGITS.len - 1; ++i)
        table[NTH(BASE64_DIGITS, i) - '+'] = i;

    return &*table;
}

static inline bool is_base64_digit(byte d)
{
    return (d >= '0' && d <= '9')
        || (d >= 'A' && d <= 'Z')
        || (d >= 'a' && d <= 'z')
        ||  d == '+' || d == '/'
        ||  d == '=';
}

usize base64_encoded_size(usize);
usize base64_decoded_size(MemSlice);

MemSlice base64_encode(MemSlice);

MemSlice base64_decode(MemSlice);

Updated on 23 August 2022 at 00:54:19 UTC