]>
Commit | Line | Data |
---|---|---|
0c05d982 | 1 | #include "config.h" |
2 | ||
3 | #if HAVE_STDIO_H | |
4 | #include <stdio.h> | |
5 | #endif | |
6 | #if HAVE_STDLIB_H | |
7 | #include <stdlib.h> | |
8 | #endif | |
9 | ||
f606f345 | 10 | #include "ansiproto.h" |
11 | ||
67508012 | 12 | static void base64_init _PARAMS((void)); |
0673c0ba | 13 | |
0c05d982 | 14 | static int base64_initialized = 0; |
15 | int base64_value[256]; | |
d5aa0e3b | 16 | const char base64_code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
0c05d982 | 17 | |
b8d8561b | 18 | static void |
0673c0ba | 19 | base64_init(void) |
0c05d982 | 20 | { |
21 | int i; | |
22 | ||
23 | for (i = 0; i < 256; i++) | |
24 | base64_value[i] = -1; | |
25 | ||
26 | for (i = 0; i < 64; i++) | |
27 | base64_value[(int) base64_code[i]] = i; | |
28 | base64_value['='] = 0; | |
29 | ||
30 | base64_initialized = 1; | |
31 | } | |
32 | ||
b8d8561b | 33 | char * |
0ee4272b | 34 | base64_decode(const char *p) |
0c05d982 | 35 | { |
36 | static char result[8192]; | |
37 | int c; | |
38 | long val; | |
39 | int i; | |
40 | char *d; | |
41 | ||
42 | if (!p) | |
0ee4272b | 43 | return NULL; |
0c05d982 | 44 | |
45 | if (!base64_initialized) | |
46 | base64_init(); | |
47 | ||
48 | val = c = 0; | |
49 | d = result; | |
50 | while (*p) { | |
51 | i = base64_value[(int) *p++]; | |
52 | if (i >= 0) { | |
53 | val = val * 64 + i; | |
54 | c++; | |
55 | } | |
56 | if (c == 4) { /* One quantum of four encoding characters/24 bit */ | |
57 | *d++ = val >> 16; /* High 8 bits */ | |
58 | *d++ = (val >> 8) & 0xff; /* Mid 8 bits */ | |
59 | *d++ = val & 0xff; /* Low 8 bits */ | |
60 | val = c = 0; | |
61 | } | |
62 | } | |
becf2977 | 63 | *d = 0; |
0c05d982 | 64 | return *result ? result : NULL; |
65 | } |