Skip to content

Instantly share code, notes, and snippets.

@gizlu
Last active June 4, 2022 13:45
Show Gist options
  • Save gizlu/14c5d930241244b8045f3043f8883d93 to your computer and use it in GitHub Desktop.
Save gizlu/14c5d930241244b8045f3043f8883d93 to your computer and use it in GitHub Desktop.
script generating lookup table for base64 decoding
#include <stdint.h>
#include <stdio.h>
void pprint(uint8_t* arr, int size)
{
printf("uint8_t lookup[%d] = {", size);
for(int i = 0; i < size-1; ++i) {
printf("%u,", arr[i]);
}
printf("%u};\n", arr[size - 1]);
}
#define MAX_VALID 63; // 0b00111111
#define PAD 128; // 0b10000000
#define INVALID 64; // 0b01000000
// PAD and INVALID bitflags are convienient for error checking
int main()
{
uint8_t lookup[256] = {0};
int n = 0;
for(int i = 'A'; i<='Z'; ++i) {
lookup[i] = n++;
}
for(int i = 'a'; i<='z'; ++i) {
lookup[i] = n++;
}
for(int i = '0'; i<='9'; ++i) {
lookup[i] = n++;
}
lookup['+'] = n++;
lookup['/'] = n++;
lookup['='] = PAD;
for(int i = 0; i<256; ++i) {
if(lookup[i] == 0 && i != 'A') lookup[i] = INVALID;
}
pprint(lookup, 256);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment