-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix64.h
179 lines (156 loc) · 4.12 KB
/
radix64.h
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
#pragma once
#include <string>
#include <string.h>
#include <vector>
class Radix64
{
public:
static void decode(const std::string& buffer,unsigned char *& out, size_t& len)
{
char val;
int c = 0, c2;/* init c because gcc is not clever
enough for the continue */
int idx;
size_t buffer_pos = 0;
radix64_init();
std::vector<char> buf ;
idx = 0;
val = 0;
for (buffer_pos = 0; buffer_pos < buffer.length(); buffer_pos++)
{
c = buffer[buffer_pos];
again:
if (c == '\n' || c == ' ' || c == '\r' || c == '\t')
continue;
else if (c == '=')
{
/* pad character: stop */
/* some mailers leave quoted-printable
* encoded characters so we try to
* workaround this */
if (buffer_pos + 2 < buffer.length())
{
int cc1, cc2, cc3;
cc1 = buffer[buffer_pos];
cc2 = buffer[buffer_pos + 1];
cc3 = buffer[buffer_pos + 2];
if (isxdigit((unsigned char)cc1) && isxdigit((unsigned char)cc2) && strchr("=\n\r\t ", cc3))
{
/* well it seems to be the case -
* adjust */
c =
isdigit((unsigned char)cc1) ? (cc1 -
'0')
: (toupper((unsigned char)cc1) - 'A' + 10);
c <<= 4;
c |=
isdigit((unsigned char)cc2) ? (cc2 -
'0')
: (toupper((unsigned char)cc2) - 'A' + 10);
buffer_pos += 2;
goto again;
}
}
if (idx == 1)
buf.push_back(val) ;// buf[n++] = val;
break;
}
else if ((c = asctobin()[(c2 = c)]) == 255)
{
/* invalid radix64 character %02x skipped\n", c2; */
continue;
}
switch (idx)
{
case 0:
val = c << 2;
break;
case 1:
val |= (c >> 4) & 3;
buf.push_back(val);//buf[n++] = val;
val = (c << 4) & 0xf0;
break;
case 2:
val |= (c >> 2) & 15;
buf.push_back(val);//buf[n++] = val;
val = (c << 6) & 0xc0;
break;
case 3:
val |= c & 0x3f;
buf.push_back(val);//buf[n++] = val;
break;
}
idx = (idx + 1) % 4;
}
idx = idx;
len = buf.size() ;
out = (unsigned char*)malloc(len) ;
memcpy(out,&buf[0],len) ;
}
/****************
* create a radix64 encoded string.
*/
static void encode(const unsigned char *data,int len,std::string& out_string)
{
unsigned char *buffer, *p;
radix64_init();
int size = (len + 2) / 3 * 4 +1;
buffer = p = (unsigned char*)malloc(size) ;
for (; len >= 3; len -= 3, data += 3)
{
*p++ = bintoasc()[(data[0] >> 2) & 077];
*p++ =
bintoasc()[
(((data[0] << 4) & 060) |
((data[1] >> 4) & 017)) & 077];
*p++ =
bintoasc()[
(((data[1] << 2) & 074) |
((data[2] >> 6) & 03)) & 077];
*p++ = bintoasc()[data[2] & 077];
}
if (len == 2)
{
*p++ = bintoasc()[(data[0] >> 2) & 077];
*p++ =
bintoasc()[
(((data[0] << 4) & 060) |
((data[1] >> 4) & 017)) & 077];
*p++ = bintoasc()[((data[1] << 2) & 074)];
*p++ = '=' ;
}
else if (len == 1)
{
*p++ = bintoasc()[(data[0] >> 2) & 077];
*p++ = bintoasc()[(data[0] << 4) & 060];
*p++ = '=' ;
*p++ = '=' ;
}
//*p = 0;
out_string = std::string((char*)buffer,p-buffer) ;
delete[] buffer ;
}
private:
static inline char *bintoasc() { static char bta[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; return bta ; }
static inline char *asctobin() { static char s[256]; return s ; } /* runtime radix64_initd */
static int& is_radix64_initd() { static int is_inited = false ; return is_inited ; }
/* hey, guess what: this is a read-only table.
* we don't _care_ if multiple threads get to initialise it
* at the same time, _except_ that is_radix64_initd=1 _must_
* be done at the end...
*/
static bool radix64_init()
{
if (is_radix64_initd())
return true;
int i;
char *s;
/* build the helpapr_table_t for radix64 to bin conversion */
for (i = 0; i < 256; i++)
asctobin()[i] = 255; /* used to detect invalid characters */
for (s = bintoasc(), i = 0; *s; s++, i++)
asctobin()[(int)*s] = i;
is_radix64_initd() = 1;
return true ;
}
};