]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/base64.c
massive const patch from Markus Gyger
[thirdparty/squid.git] / lib / base64.c
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
10 #include "ansiproto.h"
11
12 static void base64_init _PARAMS((void));
13
14 static int base64_initialized = 0;
15 int base64_value[256];
16 char base64_code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17
18 static void
19 base64_init(void)
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
33 char *
34 base64_decode(const char *p)
35 {
36 static char result[8192];
37 int c;
38 long val;
39 int i;
40 char *d;
41
42 if (!p)
43 return NULL;
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 }
63 *d = 0;
64 return *result ? result : NULL;
65 }