diff --git a/programs/develop/libraries/base64/base64.c b/programs/develop/libraries/base64/base64.c new file mode 100644 index 0000000000..ca2efa2e55 --- /dev/null +++ b/programs/develop/libraries/base64/base64.c @@ -0,0 +1,123 @@ + +#include "base64.h" + +///=============== + +char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + +///=============== + +char* strchr (const char *s, int c) +{ + do { + if (*s == c) + { + return (char*)s; + } + } while (*s++); + return (0); +} + +///=============== + +int base64_encode(char inp[], char outp[], int len) +{ +int i, j; +unsigned char chr[3]; + +for (i = 0, j=-1; i < len; i+=3) + { + chr[0] = (unsigned char) inp[i]; + if (i < len) + { + chr[1] = (unsigned char) inp[i+1]; + chr[2] = (unsigned char) inp[i+2]; + } + + outp[++j] = base64_table[ chr[0]>>2 ]; + outp[++j] = base64_table[ ((chr[0] & 3) << 4) | (chr[1] >> 4) ]; + + outp[++j] = base64_table[ ((chr[1] & 15) << 2) | (chr[2] >> 6) ]; + outp[++j] = base64_table[ chr[2] & 63 ]; + } + +switch (len%3) + { + case 1: + outp[j-1] = outp[j] = '='; + break; + + case 2: + outp[j] = '='; + break; + + default: + break; + + }; + +outp[j+1] = '\0'; + +return j+1; +} + +///=============== + +int base64_decode(char inp[], char outp[], int len) +{ +int i, j, k; +char chr[4]; + +for (i = 0, j=-1, k=0; i < len; i+=4) + { + chr[0] = strchr(base64_table, inp[i]) - base64_table; + + chr[1] = strchr(base64_table, inp[i+1]) - base64_table; + + if (inp[i+2] == '=') + { + chr[2] = 0; + k++; + } + else + chr[2] = strchr(base64_table, inp[i+2]) - base64_table; + + if (inp[i+3] == '=') + { + chr[3] = 0; + k++; + } + else + chr[3] = strchr(base64_table, inp[i+3]) - base64_table; + + outp[++j] = ((chr[0] << 2) | (chr[1] >> 4)); + outp[++j] = ((chr[1] << 4) | (chr[2] >> 2)); + outp[++j] = ((chr[2] & 0x03 )<< 6) | (chr[3] & 0x3f); + } + +outp[j+1-k] = '\0'; + +return j+1-k; +} + +///=============== + +#define NULL ((void*)0) + +typedef struct +{ +void *name; +void *function; +} export_t; + +char szbase64_encode[]={"base64_encode"}; +char szbase64_decode[]={"base64_decode"}; + +export_t EXPORTS[] = +{ +{szbase64_encode, (void*) base64_encode}, +{szbase64_decode, (void*) base64_decode}, +{ NULL, NULL }, +}; + +///=============== diff --git a/programs/develop/libraries/base64/base64.h b/programs/develop/libraries/base64/base64.h new file mode 100644 index 0000000000..71baa1fcea --- /dev/null +++ b/programs/develop/libraries/base64/base64.h @@ -0,0 +1,4 @@ + +char* my_strchr (const char *s, int c); +int base64_encode(char inp[], char outp[], int len); +int base64_decode(char inp[], char outp[], int len); diff --git a/programs/develop/libraries/base64/compile.bat b/programs/develop/libraries/base64/compile.bat new file mode 100644 index 0000000000..e44a296e38 --- /dev/null +++ b/programs/develop/libraries/base64/compile.bat @@ -0,0 +1,4 @@ +del base64.obj +gcc -c -o base64.obj base64.c +kpack base64.obj +pause \ No newline at end of file diff --git a/programs/develop/libraries/base64/readme.txt b/programs/develop/libraries/base64/readme.txt new file mode 100644 index 0000000000..d33b786069 --- /dev/null +++ b/programs/develop/libraries/base64/readme.txt @@ -0,0 +1,11 @@ +Кодирование/декодирование base64 +================================ + +int base64_encode(char inp[], char outp[], int len); +Кодирование массива inp длиной len в массив outp (строку с '\0'). Функция возвращает длину outp. + +int base64_decode(char inp[], char outp[], int len); +Декодирование массива inp длиной len в массив outp (строку с '\0'). Функция возвращает длину outp. + +================================ +Albom, 2010