Last active
August 29, 2015 14:18
-
-
Save cia48621793/a58d899bfb52184816b5 to your computer and use it in GitHub Desktop.
based on http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c, UTF-8 is not supported. (require libc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "base64.h" | |
void build_decoding_table() | |
{ | |
int i; | |
decode_table = (char*) malloc(128); | |
for (i = 0; i < 64; i++) | |
decode_table[encode_table[i]] = i; | |
} | |
char* base64_encode(const char* src) | |
{ | |
int len, outLen, a, b, c, triple, i, j; | |
len = strlen(src); | |
outLen = 4 * ((len+2) / 3); | |
char* pool = (char*) malloc(outLen + 1); | |
if (!pool) | |
return NULL; | |
for (i = 0; i < outLen + 1; i++) | |
pool[i] = 0; | |
for (i = 0, j = 0; i < len; ) | |
{ | |
a = i < len ? src[i++] : 0; | |
b = i < len ? src[i++] : 0; | |
c = i < len ? src[i++] : 0; | |
triple = (a << 0x10) + (b << 0x08) + c; | |
pool[j++] = encode_table[(triple >> 3 * 6) & 0x3F]; | |
pool[j++] = encode_table[(triple >> 2 * 6) & 0x3F]; | |
pool[j++] = encode_table[(triple >> 1 * 6) & 0x3F]; | |
pool[j++] = encode_table[(triple >> 0 * 6) & 0x3F]; | |
} | |
for (i = 0; i < mod_table[len % 3]; i++) | |
pool[outLen - 1 - i] = '='; | |
return pool; | |
} | |
char* base64_decode(const char* src) | |
{ | |
if (!decode_table) | |
build_decoding_table(); | |
int len, outLen, a, b, c, d, triple, i, j; | |
len = strlen(src); | |
outLen = len / 4 * 3; | |
if (len % 4 != 0) | |
return NULL; | |
if (src[len - 1] == '=' || src[len - 2] == '=') | |
outLen--; | |
char* pool = (char*) malloc(outLen + 1); | |
if (!pool) | |
return NULL; | |
for (i = 0; i < outLen + 1; i++) | |
pool[i] = 0; | |
for (i = 0, j = 0; i < len;) | |
{ | |
a = src[i] == '=' ? 0 & i++ : decode_table[src[i++]]; | |
b = src[i] == '=' ? 0 & i++ : decode_table[src[i++]]; | |
c = src[i] == '=' ? 0 & i++ : decode_table[src[i++]]; | |
d = src[i] == '=' ? 0 & i++ : decode_table[src[i++]]; | |
triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); | |
if (j < outLen) pool[j++] = (triple >> 2 * 8) & 0xFF; | |
if (j < outLen) pool[j++] = (triple >> 1 * 8) & 0xFF; | |
if (j < outLen) pool[j++] = (triple >> 0 * 8) & 0xFF; | |
} | |
return pool; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _BASE64_H | |
#define _BASE64_H_ | |
#include <string.h> | |
#include <stdlib.h> | |
static char encode_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
static char* decode_table = NULL; | |
static int mod_table[] = {0, 2, 1}; | |
void build_decoding_table(); | |
char* base64_encode(const char* src); | |
char* base64_decode(const char* src); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment