Skip to content

Instantly share code, notes, and snippets.

@opsJson
Created July 16, 2022 02:06
Show Gist options
  • Select an option

  • Save opsJson/79bbce20acc2393b3291864021f1cbab to your computer and use it in GitHub Desktop.

Select an option

Save opsJson/79bbce20acc2393b3291864021f1cbab to your computer and use it in GitHub Desktop.
Encode and decode base64
#include <stdio.h>
#include <string.h>
#define BASE64_SIZE(str) ((4 * strlen(str) / 3) + 3) & ~3
static char const *to_base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static char const from_base64_table[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 0, 64, 64,
64, 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, 64, 64, 64, 64, 64,
64, 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, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
void base64_encode(char *dest, char *src, int size) {
int i, j;
for (i=0, j=0; i<size; i+=3, j+=4)
{
dest[j] = to_base64_table[(src[i] & 252) >> 2];
dest[j+1] = to_base64_table[((src[i] & 3) << 4) | ((src[i+1] & 240) >> 4)];
dest[j+2] = to_base64_table[((src[i+1] & 15) << 2) | ((src[i+2] & 192) >> 6)];
dest[j+3] = to_base64_table[src[i+2] & 63];
}
if (i - size == 1) {
dest[j-1] = '=';
}
else if (i - size == 2) {
dest[j-1] = '=';
dest[j-2] = '=';
}
dest[j] = 0;
}
void base64_decode(char *dest, char *src, int size) {
int i, j;
unsigned char *ptr = (unsigned char *)src;
for (i=0, j=0; i<size; i+=4, j+=3)
{
dest[j] = (from_base64_table[ptr[i]] << 2) | ((from_base64_table[ptr[i+1]]) >> 4);
dest[j+1] = (from_base64_table[ptr[i+1]] << 4) | (from_base64_table[ptr[i+2]] >> 2);
dest[j+2] = (from_base64_table[ptr[i+2]] << 6) | (from_base64_table[ptr[i+3]]);
}
dest[j] = 0;
}
/*///////////////////////////////////
Testing:
///////////////////////////////////*/
int main(void) {
char encoded[50], decoded[50];
char *str = "Hello, world!";
base64_encode(encoded, str, strlen(str));
printf("base64 encoded: %s\n\n", encoded);
base64_decode(decoded, encoded, strlen(encoded));
printf("base64 decoded: %s\n\n", decoded);
printf("calculate size for base64 non-null terminator string: %i\n", BASE64_SIZE(str));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment