Branch data Line data Source code
1 : : // $Id: Base64.cc 6024 2008-07-26 19:20:47Z vern $
2 : :
3 : : #include "config.h"
4 : : #include "Base64.h"
5 : :
6 : : static int base64_table[256];
7 : :
8 : 0 : static void init_base64_table()
9 : : {
10 : : static int table_initialized = 0;
11 : :
12 [ # # ]: 0 : if ( ++table_initialized > 1 )
13 : 0 : return;
14 : :
15 : : int i;
16 [ # # ]: 0 : for ( i = 0; i < 256; ++i )
17 : 0 : base64_table[i] = -1;
18 : :
19 [ # # ]: 0 : for ( i = 0; i < 26; ++i )
20 : : {
21 : 0 : base64_table['A' + i] = i;
22 : 0 : base64_table['a' + i] = i + 26;
23 : : }
24 : :
25 [ # # ]: 0 : for ( i = 0; i < 10; ++i )
26 : 0 : base64_table['0' + i] = i + 52;
27 : :
28 : : // Casts to avoid compiler warnings.
29 : 0 : base64_table[int('+')] = 62;
30 : 0 : base64_table[int('/')] = 63;
31 : 0 : base64_table[int('=')] = 0;
32 : : }
33 : :
34 : 0 : Base64Decoder::Base64Decoder(Analyzer* arg_analyzer)
35 : : {
36 : 0 : init_base64_table();
37 : 0 : base64_group_next = 0;
38 : 0 : base64_padding = base64_after_padding = 0;
39 : 0 : errored = 0;
40 : 0 : analyzer = arg_analyzer;
41 : 0 : }
42 : :
43 : 0 : int Base64Decoder::Decode(int len, const char* data, int* pblen, char** pbuf)
44 : : {
45 : : int blen;
46 : : char* buf;
47 : :
48 [ # # ]: 0 : if ( ! pbuf )
49 : 0 : internal_error("nil pointer to decoding result buffer");
50 : :
51 [ # # ]: 0 : if ( *pbuf )
52 : : {
53 : 0 : buf = *pbuf;
54 : 0 : blen = *pblen;
55 : : }
56 : : else
57 : : {
58 : : // Estimate the maximal number of 3-byte groups needed,
59 : : // plus 1 byte for the optional ending NUL.
60 : 0 : blen = int((len + base64_group_next + 3) / 4) * 3 + 1;
61 : 0 : *pbuf = buf = new char[blen];
62 : : }
63 : :
64 : 0 : int dlen = 0;
65 : :
66 : 0 : while ( 1 )
67 : : {
68 [ # # ]: 0 : if ( base64_group_next == 4 )
69 : : {
70 : : // For every group of 4 6-bit numbers,
71 : : // write the decoded 3 bytes to the buffer.
72 [ # # ]: 0 : if ( base64_after_padding )
73 : : {
74 [ # # ]: 0 : if ( ++errored == 1 )
75 : 0 : IllegalEncoding("extra base64 groups after '=' padding are ignored");
76 : 0 : base64_group_next = 0;
77 : 0 : continue;
78 : : }
79 : :
80 : 0 : int num_octets = 3 - base64_padding;
81 : :
82 [ # # ]: 0 : if ( buf + num_octets > *pbuf + blen )
83 : 0 : break;
84 : :
85 : : uint32 bit32 =
86 : : ((base64_group[0] & 0x3f) << 18) |
87 : : ((base64_group[1] & 0x3f) << 12) |
88 : : ((base64_group[2] & 0x3f) << 6) |
89 : 0 : ((base64_group[3] & 0x3f));
90 : :
91 [ # # ]: 0 : if ( --num_octets >= 0 )
92 : 0 : *buf++ = char((bit32 >> 16) & 0xff);
93 : :
94 [ # # ]: 0 : if ( --num_octets >= 0 )
95 : 0 : *buf++ = char((bit32 >> 8) & 0xff);
96 : :
97 [ # # ]: 0 : if ( --num_octets >= 0 )
98 : 0 : *buf++ = char((bit32) & 0xff);
99 : :
100 [ # # ]: 0 : if ( base64_padding > 0 )
101 : 0 : base64_after_padding = 1;
102 : :
103 : 0 : base64_group_next = 0;
104 : 0 : base64_padding = 0;
105 : : }
106 : :
107 [ # # ]: 0 : if ( dlen >= len )
108 : 0 : break;
109 : :
110 [ # # ]: 0 : if ( data[dlen] == '=' )
111 : 0 : ++base64_padding;
112 : :
113 : 0 : int k = base64_table[(unsigned char) data[dlen]];
114 [ # # ]: 0 : if ( k >= 0 )
115 : 0 : base64_group[base64_group_next++] = k;
116 : : else
117 : : {
118 [ # # ]: 0 : if ( ++errored == 1 )
119 : 0 : IllegalEncoding(fmt("character %d ignored by Base64 decoding", (int) (data[dlen])));
120 : : }
121 : :
122 : 0 : ++dlen;
123 : : }
124 : :
125 : 0 : *pblen = buf - *pbuf;
126 : 0 : return dlen;
127 : : }
128 : :
129 : 0 : int Base64Decoder::Done(int* pblen, char** pbuf)
130 : : {
131 : 0 : const char* padding = "===";
132 : :
133 [ # # ]: 0 : if ( base64_group_next != 0 )
134 : : {
135 [ # # ]: 0 : if ( base64_group_next < 4 )
136 : 0 : IllegalEncoding(fmt("incomplete base64 group, padding with %d bits of 0", (4-base64_group_next) * 6));
137 : 0 : Decode(4 - base64_group_next, padding, pblen, pbuf);
138 : 0 : return -1;
139 : : }
140 : :
141 [ # # ]: 0 : if ( pblen )
142 : 0 : *pblen = 0;
143 : :
144 : 0 : return 0;
145 : : }
146 : :
147 : 0 : BroString* decode_base64(const BroString* s)
148 : : {
149 : 0 : int buf_len = int((s->Len() + 3) / 4) * 3 + 1;
150 : 0 : int rlen2, rlen = buf_len;
151 : 0 : char* rbuf2, *rbuf = new char[rlen];
152 : :
153 : 0 : Base64Decoder dec(0);
154 [ # # ]: 0 : if ( dec.Decode(s->Len(), (const char*) s->Bytes(), &rlen, &rbuf) == -1 )
155 : 0 : goto err;
156 : :
157 : 0 : rlen2 = buf_len - rlen;
158 : 0 : rbuf2 = rbuf + rlen;
159 : : // Done() returns -1 if there isn't enough padding, but we just ignore
160 : : // it.
161 : 0 : dec.Done(&rlen2, &rbuf2);
162 : 0 : rlen += rlen2;
163 : :
164 : 0 : rbuf[rlen] = '\0';
165 : 0 : return new BroString(1, (u_char*) rbuf, rlen);
166 : :
167 : 0 : err:
168 [ # # ]: 0 : delete [] rbuf;
169 : 0 : return 0;
170 [ + - ][ + - ]: 6 : }
|