Skip to content

Instantly share code, notes, and snippets.

@Andersama
Created August 4, 2022 06:13
Show Gist options
  • Select an option

  • Save Andersama/dd214ee85c8fefb9cb7f36f3b03c11d2 to your computer and use it in GitHub Desktop.

Select an option

Save Andersama/dd214ee85c8fefb9cb7f36f3b03c11d2 to your computer and use it in GitHub Desktop.
#pragma once
#include <stdint.h>
// for reference see: https://github.com/jxskiss/base62
// quick_base62_encoding.h
const uint32_t base62_high_bitmask = (62 & 63); //
const uint32_t base62_low_bitmask = (30 & 31); //
const uint32_t base62_common_bitmask = (base62_high_bitmask & base62_low_bitmask); //00011110
const uint32_t bits6_mask = (64-1); //
const uint32_t bits5_mask = (bits6_mask >> 1); //
struct base62_encode_result_t {
size_t char_count;
size_t required_count;
};
struct base62_decode_result_t {
size_t char_count;
size_t required_count;
};
char* decode_table_from_alphabet(char *table, char* alphabet, size_t len) {
//invalidate all values
for (size_t i = 0; i < len; i++) {
table[i] = 255;
}
//mark each from the alphabet
for (size_t i = 0; i < len; i++)
table[alphabet[i]] = i;
return table;
}
struct base62_encode_result_t encode_base62(char *dst, size_t dst_len, const char *input, size_t in_len, char *alphabet) {
base62_encode_result_t r = { 0 };
size_t i = 0;
uint32_t tmp_buf = 0;
uint32_t tmp_buf_bits = 0;
size_t encoding_bits = in_len * 8;
size_t processed_bits = 0;
for (;i < in_len; i++) {
//load a byte into the buffer (8 bits)
tmp_buf = (tmp_buf << 8) | input[i];
tmp_buf_bits += 8;
//encode the bits
do {
uint32_t bits_6 = (tmp_buf >> (tmp_buf_bits - 6)) & bits6_mask;
uint32_t bits_5 = bits_6 >> 1;
uint8_t e6_byte = alphabet[bits_6];
uint8_t e5_byte = alphabet[bits_5];
uint32_t is_common_mask = (bits_6 & base62_common_bitmask) == base62_common_bitmask;
size_t rem_bits = (encoding_bits - processed_bits);
uint32_t encoded_bits = is_common_mask && (rem_bits > 6 || e6_byte > bits5_mask) ? 5 : 6;
tmp_buf_bits -= encoded_bits;
processed_bits += encoded_bits;
uint8_t out_byte = is_common_mask ? e5_byte : e6_byte;
if (r.char_count < dst_len) {
dst[r.char_count] = out_byte;
r.char_count++;
}
r.required_count++;
//should take a while before this gets closed to being filled
} while (tmp_buf_bits > 24);
}
for (; tmp_buf_bits > 0;) {
uint32_t bits_6 = (tmp_buf >> (tmp_buf_bits - 6)) & bits6_mask;
uint32_t bits_5 = bits_6 >> 1;
uint8_t e6_byte = alphabet[bits_6];
uint8_t e5_byte = alphabet[bits_5];
uint32_t is_common_mask = (bits_6 & base62_common_bitmask) == base62_common_bitmask;
size_t rem_bits = (encoding_bits - processed_bits);
uint32_t encoded_bits = is_common_mask && (rem_bits > 6 || e6_byte > bits5_mask) ? 5 : 6;
tmp_buf_bits -= encoded_bits;
processed_bits += encoded_bits;
uint8_t out_byte = is_common_mask ? e5_byte : e6_byte;
if (r.char_count < dst_len) {
dst[r.char_count] = out_byte;
r.char_count++;
}
r.required_count++;
}
return r;
}
struct base62_decode_result_t decode_base62(char* dst, size_t dst_len, const char* input, size_t in_len, char *decode_table) {
struct base62_decode_result_t r = { 0 };
uint32_t tmp_buf = 0;
uint32_t tmp_buf_bits = 0;
size_t decoded_bits = 0;
size_t ndecoded_bits = 0;
size_t i = 0;
for (; i < in_len; i++) {
uint8_t decode_value = decode_table[input[i]];
if (decode_value > 62) {
return r;
}
tmp_buf = (tmp_buf << ndecoded_bits) | decode_value;
if (i == in_len - 1) {
ndecoded_bits = (decode_value > bits5_mask) ? 5 : 6;
} else if ((decode_value & base62_common_bitmask) == base62_common_bitmask) {
ndecoded_bits = 5;
} else {
ndecoded_bits = 6;
}
decoded_bits += ndecoded_bits;
tmp_buf_bits += ndecoded_bits;
if (tmp_buf_bits >= 8) {
if (r.char_count < dst_len) {
dst[r.char_count] = (tmp_buf >> (tmp_buf_bits - 8)) & 0xff;
r.char_count++;
}
r.required_count++;
tmp_buf_bits -= 8;
}
}
if (tmp_buf_bits > 0) {
if (r.char_count < dst_len) {
dst[r.char_count] = tmp_buf & 0xff;
//(tmp_buf >> (tmp_buf_bits - 8)) & 0xff;
r.char_count++;
}
r.required_count++;
//tmp_buf_bits = 0;
}
return r;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment