]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/base64.c
ANSIFY
[thirdparty/squid.git] / lib / base64.c
CommitLineData
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
10static int base64_initialized = 0;
11int base64_value[256];
12char base64_code[] = "ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13
14static void base64_init()
15{
16 int i;
17
18 for (i = 0; i < 256; i++)
19 base64_value[i] = -1;
20
21 for (i = 0; i < 64; i++)
22 base64_value[(int) base64_code[i]] = i;
23 base64_value['='] = 0;
24
25 base64_initialized = 1;
26}
27
28char *base64_decode(p)
b44c0fb4 29 char *p;
0c05d982 30{
31 static char result[8192];
32 int c;
33 long val;
34 int i;
35 char *d;
36
37 if (!p)
38 return p;
39
40 if (!base64_initialized)
41 base64_init();
42
43 val = c = 0;
44 d = result;
45 while (*p) {
46 i = base64_value[(int) *p++];
47 if (i >= 0) {
48 val = val * 64 + i;
49 c++;
50 }
51 if (c == 4) { /* One quantum of four encoding characters/24 bit */
52 *d++ = val >> 16; /* High 8 bits */
53 *d++ = (val >> 8) & 0xff; /* Mid 8 bits */
54 *d++ = val & 0xff; /* Low 8 bits */
55 val = c = 0;
56 }
57 }
58
59 return *result ? result : NULL;
60}