forked from mstone/vscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.cc
169 lines (137 loc) · 2.69 KB
/
decode.cc
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
/* Copyright (c) 2011 Akamai Technologies, Inc. */
#include "system.hh"
#include "decode.h"
static char decode_hex_char(char c)
{
if (c <= 'f' && c >= 'a')
return c - 'a' + 10;
else if (c <= 'F' && c >= 'A')
return c - 'A' + 10;
else
return c - '0';
}
int uri_decode(const StringPiece& sp, ostream& os)
{
for (StringPiece::const_iterator it = sp.begin(), ie = sp.end();
it != ie;
++it)
{
if (!os.good())
return EIO;
char c = *it;
if (c != '%')
os << c;
else
{
++it;
if (it == sp.end())
return EINVAL;
char c1 = *it;
if (!isxdigit(c1))
return EINVAL;
++it;
if (it == sp.end())
return EINVAL;
char c2 = *it;
if (!isxdigit(c2))
return EINVAL;
char d1 = decode_hex_char(c1);
char d2 = decode_hex_char(c2);
char d3 = d1 << 4;
char d4 = d3 | d2;
os << d4;
}
}
if (os.bad())
return EIO;
return 0;
}
static char isb64digit(char c)
{
if (c <= 'Z' && c >= 'A')
return 1;
else if (c <= 'z' && c >= 'a')
return 1;
else if (c <= '9' && c >= '0')
return 1;
else if (c == '+')
return 1;
else if (c == '-')
return 1;
return 0;
}
static char unsafe_decode_b64_char(char c)
{
if (c <= 'Z' && c >= 'A')
return c - 'A';
else if (c <= 'z' && c >= 'a')
return c - 'a' + 26;
else if (c <= '9' && c >= '0')
return c - '0' + 52;
else if (c == '+')
return 62;
else if (c == '-')
return 63;
return 0;
}
#define READCHAR(v) \
char c##v = *it++; \
if (!isb64digit(c##v)) { \
if (len == 1 && (v == 2 || v == 3) && c##v == '=') {} \
else if (len == 2 && (v == 3) && c##v == '=') {} \
else return EINVAL; \
} \
char d##v = unsafe_decode_b64_char(c##v);
#define TESTITER(v) \
if (it == sp.end()) {\
return EINVAL;\
}
#define MAYBE_OUTPUT(v) \
os << v; \
if (len == 1) { \
break;\
} \
--len;
int b64_decode(const StringPiece& sp, ostream& os)
{
size_t len = sp.size();
size_t pad_len = 0;
if (len == 0)
return 0;
if (len % 4 != 0)
return EINVAL;
if (sp[len-1] == '=' && sp[len-2] == '=')
pad_len = 2;
else if (sp[len-1] == '=')
pad_len = 1;
len /= 4;
len *= 3;
len -= pad_len;
for (StringPiece::const_iterator it = sp.begin(), ie = sp.end();
it != ie;
/**/)
{
if (!os.good())
return EIO;
READCHAR(0); TESTITER(0);
READCHAR(1); TESTITER(1);
READCHAR(2); TESTITER(2);
READCHAR(3);
/*
|......|.. ....|.... ..|......|
|...... ..|.... ....|.. ......|
*/
char m0 = (d0 & 0x3F) << 2 | ((d1 & 0x30) >> 4);
char m1 = (d1 & 0x0F) << 4 | ((d2 & 0x3C) >> 2);
char m2 = (d2 & 0x03) << 6 | ((d3 & 0x3F) >> 0);
MAYBE_OUTPUT(m0);
MAYBE_OUTPUT(m1);
MAYBE_OUTPUT(m2);
}
if (os.bad())
return EIO;
return 0;
}
#undef READCHAR
#undef TESTITER
#undef MAYBE_OUTPUT