-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-token.cpp
157 lines (153 loc) · 5.45 KB
/
decode-token.cpp
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
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include "http.hpp"
#include "decode-lookup-cls.hpp"
namespace http {
// RFC 7230 HTTP/1.1
//
// fields-value: [,\s]* token (\s* ';' \s* parameter)*
// (\s* ',' (\s* token (\s* ';' \s* parameter)*)?)* \s*
// parameter: token \s* '=' \s* (token | '"' ('\\' qdesc | qdtext)* '"'
// token: tchar+
// tchar: [A-Za-z0-9!#$%&'*+\-.^_`|~]
// qdesc: qdtext | '\\' | '"'
// qdtext: [\t \x21\x23-\x5b\x5d-\x7e]
//
// S1: tchar S2 A1{ item.token.push_back (octet); }
// | [,] S1
// | [\t ] S1
//
// S2: tchar S2 A1{ item.token.push_back (octet); }
// | [\t ] S3
// | [;] S4
// | [,] Sc A5{ fields.push_back (item); }
// | $ Sd A5{ fields.push_back (item); }
//
// S3: [\t ] S3
// | [;] S4
// | [,] Sc A5{ fields.push_back (item); }
// | $ Sd A5{ fields.push_back (item); }
//
// S4: tchar S5 A2{ name.push_back (std::tolower (octet)); }
// | [\t ] S4
//
// S5: tchar S5 A2{ name.push_back (std::tolower (octet)); }
// | [\t ] S6
// | [=] S7
//
// S6: [\t ] S6
// | [=] S7
//
// S7: tchar S8 A3{ value.push_back (octet); }
// | [\t ] S7
// | ["] Sa
//
// S8: tchar S8 A3{ value.push_back (octet); }
// | [\t ] Sb A4{ item.parameter.push_back (name, value); }
// | [;] S4 A4{ item.parameter.push_back (name, value); }
// | [,] Sc A6{ A4 (); A5 (); }
// | $ Sd A6{ A4 (); A5 (); }
//
// S9: qdtext Sa A3{ value.push_back (octet); }
// | [\\] Sa A3{ value.push_back (octet); }
// | ["] Sa A3{ value.push_back (octet); }
//
// Sa: qdtext Sa A3{ value.push_back (octet); }
// | [\\] S9
// | ["] Sb A4{ item.parameter.push_back (name, value); }
//
// Sb: [\t ] Sb
// | [;] S4
// | [,] Sc A5{ fields.push_back (item); }
// | $ Sd A5{ fields.push_back (item); }
//
// Sc: tchar S2 A1{ item.token.push_back (octet); }
// | [\t ] Sc
// | [,] Sc
// | $ Sd
//
// Sd: MATCH
bool
decode (std::vector<token_type>& fields, std::string const& src, int const lowerlimit)
{
static const int SHIFT[14][10] = {
// qdtext tchar [\t ] [;] [=] [,] [\\] ["] $
{0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0, 0x00, 0x12, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, // S1
{0, 0x00, 0x12, 0x03, 0x04, 0x00, 0x5c, 0x00, 0x00, 0x5d}, // S2
{0, 0x00, 0x00, 0x03, 0x04, 0x00, 0x5c, 0x00, 0x00, 0x5d}, // S3
{0, 0x00, 0x25, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // S4
{0, 0x00, 0x25, 0x06, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00}, // S5
{0, 0x00, 0x00, 0x06, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00}, // S6
{0, 0x00, 0x38, 0x07, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00}, // S7
{0, 0x00, 0x38, 0x4b, 0x44, 0x00, 0x6c, 0x00, 0x00, 0x6d}, // S8
{0, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x00}, // S9
{0, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x09, 0x4b, 0x00}, // Sa
{0, 0x00, 0x00, 0x0b, 0x04, 0x00, 0x5c, 0x00, 0x00, 0x5d}, // Sb
{0, 0x00, 0x12, 0x0c, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x0d}, // Sc
{1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Sd
};
static const uint32_t CCLASS[16] = {
// tn r
0x00000000, 0x03000000, 0x00000000, 0x00000000,
// !"#$%&' ()*+,-./ 01234567 89:;<=>?
0x32822222, 0x11226221, 0x22222222, 0x22141511,
// @ABCDEFG HIJKLMNO PQRSTUVW XYZ[\]^_
0x12222222, 0x22222222, 0x22222222, 0x22217122,
// `abcdefg hijklmno pqrstuvw xyz{|}~
0x22222222, 0x22222222, 0x22222222, 0x22212120,
};
std::vector<token_type> list;
token_type item;
std::string::const_iterator s = src.cbegin ();
std::string::const_iterator const e = src.cend ();
std::string name;
std::string value;
bool matched = false;
int next_state = 1 == lowerlimit ? 1 : 12;
for (; s <= e; ++s) {
uint32_t octet = s == e ? '\0' : static_cast<uint8_t> (*s);
int cls = s == e ? 9 : lookup_cls (CCLASS, octet);
int prev_state = next_state;
next_state = 0 == cls ? 0 : SHIFT[prev_state][cls] & 0x0f;
if (! next_state)
break;
switch (SHIFT[prev_state][cls] & 0xf0) {
case 0x10:
item.token.push_back (octet);
break;
case 0x20:
name.push_back (std::tolower (octet));
break;
case 0x30:
value.push_back (octet);
break;
case 0x40:
item.parameter.push_back (name);
item.parameter.push_back (value);
name.clear ();
value.clear ();
break;
case 0x50:
list.push_back (item);
item.clear ();
break;
case 0x60:
item.parameter.push_back (name);
item.parameter.push_back (value);
list.push_back (item);
name.clear ();
value.clear ();
item.clear ();
break;
}
if (SHIFT[next_state][0] & 1)
matched = true;
}
if (matched)
std::swap (fields, list);
return matched;
}
}//namespace http