]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/base64.c
11 static void base64_init(void);
13 static int base64_initialized
= 0;
14 int base64_value
[256];
15 const char base64_code
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23 for (i
= 0; i
< 256; i
++)
26 for (i
= 0; i
< 64; i
++)
27 base64_value
[(int) base64_code
[i
]] = i
;
28 base64_value
['='] = 0;
30 base64_initialized
= 1;
34 base64_decode(const char *p
)
36 static char result
[8192];
45 if (!base64_initialized
)
51 i
= base64_value
[(int) *p
++];
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 */
64 return *result
? result
: NULL
;
67 /* adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments */
69 base64_encode(const char *decoded_str
)
71 static char result
[8192];
80 if (!base64_initialized
)
83 while ((c
= *decoded_str
++) && out_cnt
< sizeof(result
) - 1) {
86 if (char_count
== 3) {
87 result
[out_cnt
++] = base64_code
[bits
>> 18];
88 result
[out_cnt
++] = base64_code
[(bits
>> 12) & 0x3f];
89 result
[out_cnt
++] = base64_code
[(bits
>> 6) & 0x3f];
90 result
[out_cnt
++] = base64_code
[bits
& 0x3f];
97 if (char_count
!= 0) {
98 bits
<<= 16 - (8 * char_count
);
99 result
[out_cnt
++] = base64_code
[bits
>> 18];
100 result
[out_cnt
++] = base64_code
[(bits
>> 12) & 0x3f];
101 if (char_count
== 1) {
102 result
[out_cnt
++] = '=';
103 result
[out_cnt
++] = '=';
105 result
[out_cnt
++] = base64_code
[(bits
>> 6) & 0x3f];
106 result
[out_cnt
++] = '=';
109 result
[out_cnt
] = '\0'; /* terminate */