1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#include "ace/Codecs.h"
#include "ace/Log_Category.h"
#include "ace/OS_Memory.h"
#include "ace/OS_NS_ctype.h"
#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */
namespace
{
// Just in case ...
#undef alphabet
#undef pad
#undef max_columns
// Symbols which form the Base64 alphabet (Defined as per RFC 2045)
ACE_Byte const alphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// The padding character used in the encoding
ACE_Byte const pad = '=';
// Number of columns per line of encoded output (Can have a maximum
// value of 76).
int const max_columns = 72;
}
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
bool ACE_Base64::init_ = false;
ACE_Byte ACE_Base64::decoder_[256];
ACE_Byte ACE_Base64::member_[256];
ACE_Byte*
ACE_Base64::encode (const ACE_Byte* input,
const size_t input_len,
size_t* output_len,
bool is_chunked)
{
if (!ACE_Base64::init_)
ACE_Base64::init();
if (!input)
return 0;
ACE_Byte* result = 0;
size_t length = ((input_len + 2) / 3) * 4;
size_t num_lines = length / max_columns + 1;
length += num_lines + 1;
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_ALLOCATOR_RETURN (result, static_cast<ACE_Byte*> (ACE_Allocator::instance()->malloc(sizeof (ACE_Byte) * length)), 0);
#else
ACE_NEW_RETURN (result, ACE_Byte[length], 0);
#endif /* ACE_HAS_ALLOC_HOOKS */
int char_count = 0;
int bits = 0;
size_t pos = 0;
int cols = 0;
for (size_t i = 0; i < input_len; ++i)
{
bits += input[i];
++char_count;
if (char_count == 3)
{
result[pos++] = alphabet[bits >> 18];
result[pos++] = alphabet[(bits >> 12) & 0x3f];
result[pos++] = alphabet[(bits >> 6) & 0x3f];
result[pos++] = alphabet[bits & 0x3f];
cols += 4;
if (cols == max_columns) {
if (is_chunked)
result[pos++] = '\n';
cols = 0;
}
bits = 0;
char_count = 0;
}
else
{
bits <<= 8;
}
}
if (char_count != 0)
{
bits <<= (16 - (8 * char_count));
result[pos++] = alphabet[bits >> 18];
result[pos++] = alphabet[(bits >> 12) & 0x3f];
cols += 2;
if (char_count == 1)
{
result[pos++] = pad;
result[pos++] = pad;
cols += 2;
}
else
{
result[pos++] = alphabet[(bits >> 6) & 0x3f];
result[pos++] = pad;
cols += 2;
}
}
if (cols > 0 && is_chunked)
result[pos++] = '\n';
result[pos] = 0;
*output_len = pos;
return result;
}
size_t
ACE_Base64::length (const ACE_Byte* input)
{
if (!ACE_Base64::init_)
ACE_Base64::init();
ACE_Byte* ptr = const_cast<ACE_Byte*> (input);
while (*ptr != 0 &&
(member_[*(ptr)] == 1 || *ptr == pad
|| ACE_OS::ace_isspace (*ptr)))
++ptr;
size_t len = ptr - input;
len = ((len + 3) / 4) * 3 + 1 ;
return len;
}
ACE_Byte*
ACE_Base64::decode (const ACE_Byte* input, size_t* output_len)
{
if (!ACE_Base64::init_)
ACE_Base64::init();
if (!input)
return 0;
size_t result_len = ACE_Base64::length (input);
ACE_Byte* result = 0;
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_ALLOCATOR_RETURN (result, static_cast<ACE_Byte*> (ACE_Allocator::instance()->malloc(sizeof (ACE_Byte) * result_len)), 0);
#else
ACE_NEW_RETURN (result, ACE_Byte[result_len], 0);
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_Byte* ptr = const_cast<ACE_Byte*> (input);
while (*ptr != 0 &&
(member_[*(ptr)] == 1 || *ptr == pad
|| ACE_OS::ace_isspace (*ptr)))
++ptr;
size_t input_len = ptr - input;
int char_count = 0;
int bits = 0;
size_t pos = 0;
size_t i = 0;
for (; i < input_len; ++i)
{
if (input[i] == pad)
break;
if (!ACE_Base64::member_[input[i]])
continue;
bits += decoder_[input[i]];
++char_count;
if (char_count == 4)
{
result[pos++] = static_cast<ACE_Byte> (bits >> 16);
result[pos++] = static_cast<ACE_Byte> ((bits >> 8) & 0xff);
result[pos++] = static_cast<ACE_Byte> (bits & 0xff);
bits = 0;
char_count = 0;
}
else
{
bits <<= 6;
}
}
int errors = 0;
if ( i == input_len)
{
if (char_count)
{
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("Decoding incomplete: atleast %d bits truncated\n"),
(4 - char_count) * 6));
++errors;
}
}
else
{
switch (char_count)
{
case 1:
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("Decoding incomplete: atleast 2 bits missing\n")));
++errors;
break;
case 2:
result[pos++] = static_cast<ACE_Byte> (bits >> 10);
break;
case 3:
result[pos++] = static_cast<ACE_Byte> (bits >> 16);
result[pos++] = static_cast<ACE_Byte> ((bits >> 8) & 0xff);
break;
}
}
if (errors)
{
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free(result);
#else
delete[] result;
#endif /* ACE_HAS_ALLOC_HOOKS */
return 0;
}
result[pos] = 0;
*output_len = pos;
return result;
}
void
ACE_Base64::init ()
{
if (!ACE_Base64::init_)
{
for (ACE_Byte i = 0; i < sizeof (alphabet); ++i)
{
ACE_Base64::decoder_[alphabet[i]] = i;
ACE_Base64::member_ [alphabet[i]] = 1;
}
ACE_Base64::init_ = true;
}
return;
}
ACE_END_VERSIONED_NAMESPACE_DECL
|