|
GNU libmicrohttpd
0.9.5
|
00001 /* 00002 * This code implements the BASE64 algorithm. 00003 * This code is in the public domain; do with it what you wish. 00004 * 00005 * @file base64.c 00006 * @brief This code implements the BASE64 algorithm 00007 * @author Matthieu Speder 00008 */ 00009 #include "base64.h" 00010 00011 static const char base64_chars[] = 00012 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00013 00014 static const char base64_digits[] = 00015 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00016 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00017 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 00018 0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 00019 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 00020 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 00021 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00022 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00023 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00024 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00025 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00026 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 00027 00028 00029 char * 00030 BASE64Decode(const char* src) 00031 { 00032 size_t in_len = strlen (src); 00033 char* dest; 00034 char* result; 00035 00036 if (in_len % 4) 00037 { 00038 /* Wrong base64 string length */ 00039 return NULL; 00040 } 00041 result = dest = malloc(in_len / 4 * 3 + 1); 00042 if (result == NULL) 00043 return NULL; /* out of memory */ 00044 while (*src) { 00045 char a = base64_digits[(unsigned char)*(src++)]; 00046 char b = base64_digits[(unsigned char)*(src++)]; 00047 char c = base64_digits[(unsigned char)*(src++)]; 00048 char d = base64_digits[(unsigned char)*(src++)]; 00049 *(dest++) = (a << 2) | ((b & 0x30) >> 4); 00050 if (c == (char)-1) 00051 break; 00052 *(dest++) = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2); 00053 if (d == (char)-1) 00054 break; 00055 *(dest++) = ((c & 0x03) << 6) | d; 00056 } 00057 *dest = 0; 00058 return result; 00059 } 00060 00061 00062 /* end of base64.c */