]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/negotiate_auth/wrapper/nw_base64.cc
Add external_acl_type %EXT_LOG and %EXT_TAG format options.
[thirdparty/squid.git] / helpers / negotiate_auth / wrapper / nw_base64.cc
1 /*
2 * Markus Moeller has modified the following code from Squid
3 */
4 #include "config.h"
5 #include "nw_base64.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10
11 static void nw_base64_init(void);
12
13 static int base64_initialized = 0;
14 #define BASE64_VALUE_SZ 256
15 int base64_value[BASE64_VALUE_SZ];
16 const char base64_code[] =
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
18
19
20 static void
21 nw_base64_init(void)
22 {
23 int i;
24
25 for (i = 0; i < BASE64_VALUE_SZ; i++)
26 base64_value[i] = -1;
27
28 for (i = 0; i < 64; i++)
29 base64_value[(int) base64_code[i]] = i;
30 base64_value[(int)'='] = 0;
31
32 base64_initialized = 1;
33 }
34
35 void
36 nw_base64_decode(char *result, const char *data, int result_size)
37 {
38 int j;
39 int c;
40 long val;
41 if (!data)
42 return;
43 if (!base64_initialized)
44 nw_base64_init();
45 val = c = 0;
46
47 for (j = 0; *data; data++) {
48 unsigned int k = ((unsigned char) *data) % BASE64_VALUE_SZ;
49 if (base64_value[k] < 0)
50 continue;
51 val <<= 6;
52 val += base64_value[k];
53 if (++c < 4)
54 continue;
55 /* One quantum of four encoding characters/24 bit */
56 if (j >= result_size)
57 break;
58 result[j++] = val >> 16; /* High 8 bits */
59 if (j >= result_size)
60 break;
61 result[j++] = (val >> 8) & 0xff; /* Mid 8 bits */
62 if (j >= result_size)
63 break;
64 result[j++] = val & 0xff; /* Low 8 bits */
65 val = c = 0;
66 }
67 return;
68 }
69
70 int
71 nw_base64_decode_len(const char *data)
72 {
73 int i, j;
74
75 j = 0;
76 for (i = strlen(data) - 1; i >= 0; i--) {
77 if (data[i] == '=')
78 j++;
79 if (data[i] != '=')
80 break;
81 }
82 return strlen(data) / 4 * 3 - j;
83 }