CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/data/headers/windows/base64.h
Views: 1904
/* from https://github.com/mdornseif/didentd */1/* public domain2* BASE64 on stdin -> converted data on stdout */34/* arbitrary data on stdin -> BASE64 data on stdout5* UNIX's newline convention is used, i.e. one ASCII control-j (10 decimal).6*7* public domain8*/910/* Hacked by [email protected] to be a library function working on memory blocks11*12*/1314static unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";1516int base64decode(char *dest, const char *src, int l)17{18static char inalphabet[256], decoder[256];19static short table_initialized = 0;20int i, bits, c, char_count;21int rpos;22int wpos = 0;2324if (!table_initialized) {25for (i = (sizeof alphabet) - 1; i >= 0; i--) {26inalphabet[alphabet[i]] = 1;27decoder[alphabet[i]] = i;28}29table_initialized = 1;30}3132char_count = 0;33bits = 0;34for (rpos = 0; rpos < l; rpos++) {35c = src[rpos];3637if (c == '=') {38break;39}4041if (c > 255 || !inalphabet[c]) {42return -1;43}4445bits += decoder[c];46char_count++;47if (char_count < 4) {48bits <<= 6;49} else {50dest[wpos++] = bits >> 16;51dest[wpos++] = (bits >> 8) & 0xff;52dest[wpos++] = bits & 0xff;53bits = 0;54char_count = 0;55}56}5758switch (char_count) {59case 1:60return -1;61break;62case 2:63dest[wpos++] = bits >> 10;64break;65case 3:66dest[wpos++] = bits >> 16;67dest[wpos++] = (bits >> 8) & 0xff;68break;69}7071return wpos;72}7374int base64encode(char *dest, const char *src, int l)75{76int bits, c, char_count;77int rpos;78int wpos = 0;7980char_count = 0;81bits = 0;8283for (rpos = 0; rpos < l; rpos++) {84c = src[rpos];8586bits += c;87char_count++;88if (char_count < 3) {89bits <<= 8;90} else {91dest[wpos++] = alphabet[bits >> 18];92dest[wpos++] = alphabet[(bits >> 12) & 0x3f];93dest[wpos++] = alphabet[(bits >> 6) & 0x3f];94dest[wpos++] = alphabet[bits & 0x3f];95bits = 0;96char_count = 0;97}98}99100if (char_count != 0) {101bits <<= 16 - (8 * char_count);102dest[wpos++] = alphabet[bits >> 18];103dest[wpos++] = alphabet[(bits >> 12) & 0x3f];104if (char_count == 1) {105dest[wpos++] = '=';106dest[wpos++] = '=';107} else {108dest[wpos++] = alphabet[(bits >> 6) & 0x3f];109dest[wpos++] = '=';110}111}112return wpos;113}114115