-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
url_canon_internal.h
246 lines (211 loc) · 10.6 KB
/
url_canon_internal.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
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
// Envoy snapshot of Chromium URL path normalization, see README.md.
// NOLINT(namespace-envoy)
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef URL_URL_CANON_INTERNAL_H_
#define URL_URL_CANON_INTERNAL_H_
// This file is intended to be included in another C++ file where the character
// types are defined. This allows us to write mostly generic code, but not have
// template bloat because everything is inlined when anybody calls any of our
// functions.
#include <stddef.h>
#include <stdlib.h>
#include "common/chromium_url/envoy_shim.h"
#include "common/chromium_url/url_canon.h"
namespace chromium_url {
// Character type handling -----------------------------------------------------
// Bits that identify different character types. These types identify different
// bits that are set for each 8-bit character in the kSharedCharTypeTable.
enum SharedCharTypes {
// Characters that do not require escaping in queries. Characters that do
// not have this flag will be escaped; see url_canon_query.cc
CHAR_QUERY = 1,
// Valid in the username/password field.
CHAR_USERINFO = 2,
// Valid in a IPv4 address (digits plus dot and 'x' for hex).
CHAR_IPV4 = 4,
// Valid in an ASCII-representation of a hex digit (as in %-escaped).
CHAR_HEX = 8,
// Valid in an ASCII-representation of a decimal digit.
CHAR_DEC = 16,
// Valid in an ASCII-representation of an octal digit.
CHAR_OCT = 32,
// Characters that do not require escaping in encodeURIComponent. Characters
// that do not have this flag will be escaped; see url_util.cc.
CHAR_COMPONENT = 64,
};
// This table contains the flags in SharedCharTypes for each 8-bit character.
// Some canonicalization functions have their own specialized lookup table.
// For those with simple requirements, we have collected the flags in one
// place so there are fewer lookup tables to load into the CPU cache.
//
// Using an unsigned char type has a small but measurable performance benefit
// over using a 32-bit number.
extern const unsigned char kSharedCharTypeTable[0x100];
// More readable wrappers around the character type lookup table.
inline bool IsCharOfType(unsigned char c, SharedCharTypes type) {
return !!(kSharedCharTypeTable[c] & type);
}
inline bool IsQueryChar(unsigned char c) { return IsCharOfType(c, CHAR_QUERY); }
inline bool IsIPv4Char(unsigned char c) { return IsCharOfType(c, CHAR_IPV4); }
inline bool IsHexChar(unsigned char c) { return IsCharOfType(c, CHAR_HEX); }
inline bool IsComponentChar(unsigned char c) { return IsCharOfType(c, CHAR_COMPONENT); }
// Maps the hex numerical values 0x0 to 0xf to the corresponding ASCII digit
// that will be used to represent it.
COMPONENT_EXPORT(URL) extern const char kHexCharLookup[0x10];
// This lookup table allows fast conversion between ASCII hex letters and their
// corresponding numerical value. The 8-bit range is divided up into 8
// regions of 0x20 characters each. Each of the three character types (numbers,
// uppercase, lowercase) falls into different regions of this range. The table
// contains the amount to subtract from characters in that range to get at
// the corresponding numerical value.
//
// See HexDigitToValue for the lookup.
extern const char kCharToHexLookup[8];
// Assumes the input is a valid hex digit! Call IsHexChar before using this.
inline unsigned char HexCharToValue(unsigned char c) { return c - kCharToHexLookup[c / 0x20]; }
// Indicates if the given character is a dot or dot equivalent, returning the
// number of characters taken by it. This will be one for a literal dot, 3 for
// an escaped dot. If the character is not a dot, this will return 0.
template <typename CHAR> inline int IsDot(const CHAR* spec, int offset, int end) {
if (spec[offset] == '.') {
return 1;
} else if (spec[offset] == '%' && offset + 3 <= end && spec[offset + 1] == '2' &&
(spec[offset + 2] == 'e' || spec[offset + 2] == 'E')) {
// Found "%2e"
return 3;
}
return 0;
}
// Write a single character, escaped, to the output. This always escapes: it
// does no checking that thee character requires escaping.
// Escaping makes sense only 8 bit chars, so code works in all cases of
// input parameters (8/16bit).
template <typename UINCHAR, typename OUTCHAR>
inline void AppendEscapedChar(UINCHAR ch, CanonOutputT<OUTCHAR>* output) {
output->push_back('%');
output->push_back(kHexCharLookup[(ch >> 4) & 0xf]);
output->push_back(kHexCharLookup[ch & 0xf]);
}
// UTF-8 functions ------------------------------------------------------------
// Reads one character in UTF-8 starting at |*begin| in |str| and places
// the decoded value into |*code_point|. If the character is valid, we will
// return true. If invalid, we'll return false and put the
// kUnicodeReplacementCharacter into |*code_point|.
//
// |*begin| will be updated to point to the last character consumed so it
// can be incremented in a loop and will be ready for the next character.
// (for a single-byte ASCII character, it will not be changed).
COMPONENT_EXPORT(URL)
bool ReadUTFChar(const char* str, int* begin, int length, unsigned* code_point_out);
// Generic To-UTF-8 converter. This will call the given append method for each
// character that should be appended, with the given output method. Wrappers
// are provided below for escaped and non-escaped versions of this.
//
// The char_value must have already been checked that it's a valid Unicode
// character.
template <class Output, void Appender(unsigned char, Output*)>
inline void DoAppendUTF8(unsigned char_value, Output* output) {
if (char_value <= 0x7f) {
Appender(static_cast<unsigned char>(char_value), output);
} else if (char_value <= 0x7ff) {
// 110xxxxx 10xxxxxx
Appender(static_cast<unsigned char>(0xC0 | (char_value >> 6)), output);
Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), output);
} else if (char_value <= 0xffff) {
// 1110xxxx 10xxxxxx 10xxxxxx
Appender(static_cast<unsigned char>(0xe0 | (char_value >> 12)), output);
Appender(static_cast<unsigned char>(0x80 | ((char_value >> 6) & 0x3f)), output);
Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), output);
} else if (char_value <= 0x10FFFF) { // Max Unicode code point.
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
Appender(static_cast<unsigned char>(0xf0 | (char_value >> 18)), output);
Appender(static_cast<unsigned char>(0x80 | ((char_value >> 12) & 0x3f)), output);
Appender(static_cast<unsigned char>(0x80 | ((char_value >> 6) & 0x3f)), output);
Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), output);
} else {
// Invalid UTF-8 character (>20 bits).
NOTREACHED();
}
}
// Helper used by AppendUTF8Value below. We use an unsigned parameter so there
// are no funny sign problems with the input, but then have to convert it to
// a regular char for appending.
inline void AppendCharToOutput(unsigned char ch, CanonOutput* output) {
output->push_back(static_cast<char>(ch));
}
// Writes the given character to the output as UTF-8. This does NO checking
// of the validity of the Unicode characters; the caller should ensure that
// the value it is appending is valid to append.
inline void AppendUTF8Value(unsigned char_value, CanonOutput* output) {
DoAppendUTF8<CanonOutput, AppendCharToOutput>(char_value, output);
}
// Writes the given character to the output as UTF-8, escaping ALL
// characters (even when they are ASCII). This does NO checking of the
// validity of the Unicode characters; the caller should ensure that the value
// it is appending is valid to append.
inline void AppendUTF8EscapedValue(unsigned char_value, CanonOutput* output) {
DoAppendUTF8<CanonOutput, AppendEscapedChar>(char_value, output);
}
// Escaping functions ---------------------------------------------------------
// Writes the given character to the output as UTF-8, escaped. Call this
// function only when the input is wide. Returns true on success. Failure
// means there was some problem with the encoding, we'll still try to
// update the |*begin| pointer and add a placeholder character to the
// output so processing can continue.
//
// We will append the character starting at ch[begin] with the buffer ch
// being |length|. |*begin| will be updated to point to the last character
// consumed (we may consume more than one for UTF-16) so that if called in
// a loop, incrementing the pointer will move to the next character.
//
// Every single output character will be escaped. This means that if you
// give it an ASCII character as input, it will be escaped. Some code uses
// this when it knows that a character is invalid according to its rules
// for validity. If you don't want escaping for ASCII characters, you will
// have to filter them out prior to calling this function.
//
// Assumes that ch[begin] is within range in the array, but does not assume
// that any following characters are.
inline bool AppendUTF8EscapedChar(const char* str, int* begin, int length, CanonOutput* output) {
// ReadUTF8Char will handle invalid characters for us and give us the
// kUnicodeReplacementCharacter, so we don't have to do special checking
// after failure, just pass through the failure to the caller.
unsigned ch;
bool success = ReadUTFChar(str, begin, length, &ch);
AppendUTF8EscapedValue(ch, output);
return success;
}
// Given a '%' character at |*begin| in the string |spec|, this will decode
// the escaped value and put it into |*unescaped_value| on success (returns
// true). On failure, this will return false, and will not write into
// |*unescaped_value|.
//
// |*begin| will be updated to point to the last character of the escape
// sequence so that when called with the index of a for loop, the next time
// through it will point to the next character to be considered. On failure,
// |*begin| will be unchanged.
inline bool Is8BitChar(char /*c*/) {
return true; // this case is specialized to avoid a warning
}
template <typename CHAR>
inline bool DecodeEscaped(const CHAR* spec, int* begin, int end, unsigned char* unescaped_value) {
if (*begin + 3 > end || !Is8BitChar(spec[*begin + 1]) || !Is8BitChar(spec[*begin + 2])) {
// Invalid escape sequence because there's not enough room, or the
// digits are not ASCII.
return false;
}
unsigned char first = static_cast<unsigned char>(spec[*begin + 1]);
unsigned char second = static_cast<unsigned char>(spec[*begin + 2]);
if (!IsHexChar(first) || !IsHexChar(second)) {
// Invalid hex digits, fail.
return false;
}
// Valid escape sequence.
*unescaped_value = (HexCharToValue(first) << 4) + HexCharToValue(second);
*begin += 2;
return true;
}
} // namespace chromium_url
#endif // URL_URL_CANON_INTERNAL_H_