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