1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #ifndef URL_URL_CANON_INTERNAL_H_
6*6777b538SAndroid Build Coastguard Worker #define URL_URL_CANON_INTERNAL_H_
7*6777b538SAndroid Build Coastguard Worker
8*6777b538SAndroid Build Coastguard Worker // This file is intended to be included in another C++ file where the character
9*6777b538SAndroid Build Coastguard Worker // types are defined. This allows us to write mostly generic code, but not have
10*6777b538SAndroid Build Coastguard Worker // template bloat because everything is inlined when anybody calls any of our
11*6777b538SAndroid Build Coastguard Worker // functions.
12*6777b538SAndroid Build Coastguard Worker
13*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
14*6777b538SAndroid Build Coastguard Worker #include <stdlib.h>
15*6777b538SAndroid Build Coastguard Worker
16*6777b538SAndroid Build Coastguard Worker #include <string>
17*6777b538SAndroid Build Coastguard Worker
18*6777b538SAndroid Build Coastguard Worker #include "base/component_export.h"
19*6777b538SAndroid Build Coastguard Worker #include "base/notreached.h"
20*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_number_conversions.h"
21*6777b538SAndroid Build Coastguard Worker #include "base/third_party/icu/icu_utf.h"
22*6777b538SAndroid Build Coastguard Worker #include "url/url_canon.h"
23*6777b538SAndroid Build Coastguard Worker
24*6777b538SAndroid Build Coastguard Worker namespace url {
25*6777b538SAndroid Build Coastguard Worker
26*6777b538SAndroid Build Coastguard Worker // Character type handling -----------------------------------------------------
27*6777b538SAndroid Build Coastguard Worker
28*6777b538SAndroid Build Coastguard Worker // Bits that identify different character types. These types identify different
29*6777b538SAndroid Build Coastguard Worker // bits that are set for each 8-bit character in the kSharedCharTypeTable.
30*6777b538SAndroid Build Coastguard Worker enum SharedCharTypes {
31*6777b538SAndroid Build Coastguard Worker // Characters that do not require escaping in queries. Characters that do
32*6777b538SAndroid Build Coastguard Worker // not have this flag will be escaped; see url_canon_query.cc
33*6777b538SAndroid Build Coastguard Worker CHAR_QUERY = 1,
34*6777b538SAndroid Build Coastguard Worker
35*6777b538SAndroid Build Coastguard Worker // Valid in the username/password field.
36*6777b538SAndroid Build Coastguard Worker CHAR_USERINFO = 2,
37*6777b538SAndroid Build Coastguard Worker
38*6777b538SAndroid Build Coastguard Worker // Valid in a IPv4 address (digits plus dot and 'x' for hex).
39*6777b538SAndroid Build Coastguard Worker CHAR_IPV4 = 4,
40*6777b538SAndroid Build Coastguard Worker
41*6777b538SAndroid Build Coastguard Worker // Valid in an ASCII-representation of a hex digit (as in %-escaped).
42*6777b538SAndroid Build Coastguard Worker CHAR_HEX = 8,
43*6777b538SAndroid Build Coastguard Worker
44*6777b538SAndroid Build Coastguard Worker // Valid in an ASCII-representation of a decimal digit.
45*6777b538SAndroid Build Coastguard Worker CHAR_DEC = 16,
46*6777b538SAndroid Build Coastguard Worker
47*6777b538SAndroid Build Coastguard Worker // Valid in an ASCII-representation of an octal digit.
48*6777b538SAndroid Build Coastguard Worker CHAR_OCT = 32,
49*6777b538SAndroid Build Coastguard Worker
50*6777b538SAndroid Build Coastguard Worker // Characters that do not require escaping in encodeURIComponent. Characters
51*6777b538SAndroid Build Coastguard Worker // that do not have this flag will be escaped; see url_util.cc.
52*6777b538SAndroid Build Coastguard Worker CHAR_COMPONENT = 64,
53*6777b538SAndroid Build Coastguard Worker };
54*6777b538SAndroid Build Coastguard Worker
55*6777b538SAndroid Build Coastguard Worker // This table contains the flags in SharedCharTypes for each 8-bit character.
56*6777b538SAndroid Build Coastguard Worker // Some canonicalization functions have their own specialized lookup table.
57*6777b538SAndroid Build Coastguard Worker // For those with simple requirements, we have collected the flags in one
58*6777b538SAndroid Build Coastguard Worker // place so there are fewer lookup tables to load into the CPU cache.
59*6777b538SAndroid Build Coastguard Worker //
60*6777b538SAndroid Build Coastguard Worker // Using an unsigned char type has a small but measurable performance benefit
61*6777b538SAndroid Build Coastguard Worker // over using a 32-bit number.
62*6777b538SAndroid Build Coastguard Worker extern const unsigned char kSharedCharTypeTable[0x100];
63*6777b538SAndroid Build Coastguard Worker
64*6777b538SAndroid Build Coastguard Worker // More readable wrappers around the character type lookup table.
IsCharOfType(unsigned char c,SharedCharTypes type)65*6777b538SAndroid Build Coastguard Worker inline bool IsCharOfType(unsigned char c, SharedCharTypes type) {
66*6777b538SAndroid Build Coastguard Worker return !!(kSharedCharTypeTable[c] & type);
67*6777b538SAndroid Build Coastguard Worker }
IsQueryChar(unsigned char c)68*6777b538SAndroid Build Coastguard Worker inline bool IsQueryChar(unsigned char c) {
69*6777b538SAndroid Build Coastguard Worker return IsCharOfType(c, CHAR_QUERY);
70*6777b538SAndroid Build Coastguard Worker }
IsIPv4Char(unsigned char c)71*6777b538SAndroid Build Coastguard Worker inline bool IsIPv4Char(unsigned char c) {
72*6777b538SAndroid Build Coastguard Worker return IsCharOfType(c, CHAR_IPV4);
73*6777b538SAndroid Build Coastguard Worker }
IsHexChar(unsigned char c)74*6777b538SAndroid Build Coastguard Worker inline bool IsHexChar(unsigned char c) {
75*6777b538SAndroid Build Coastguard Worker return IsCharOfType(c, CHAR_HEX);
76*6777b538SAndroid Build Coastguard Worker }
IsComponentChar(unsigned char c)77*6777b538SAndroid Build Coastguard Worker inline bool IsComponentChar(unsigned char c) {
78*6777b538SAndroid Build Coastguard Worker return IsCharOfType(c, CHAR_COMPONENT);
79*6777b538SAndroid Build Coastguard Worker }
80*6777b538SAndroid Build Coastguard Worker
81*6777b538SAndroid Build Coastguard Worker // Appends the given string to the output, escaping characters that do not
82*6777b538SAndroid Build Coastguard Worker // match the given |type| in SharedCharTypes.
83*6777b538SAndroid Build Coastguard Worker void AppendStringOfType(const char* source,
84*6777b538SAndroid Build Coastguard Worker size_t length,
85*6777b538SAndroid Build Coastguard Worker SharedCharTypes type,
86*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
87*6777b538SAndroid Build Coastguard Worker void AppendStringOfType(const char16_t* source,
88*6777b538SAndroid Build Coastguard Worker size_t length,
89*6777b538SAndroid Build Coastguard Worker SharedCharTypes type,
90*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
91*6777b538SAndroid Build Coastguard Worker
92*6777b538SAndroid Build Coastguard Worker // This lookup table allows fast conversion between ASCII hex letters and their
93*6777b538SAndroid Build Coastguard Worker // corresponding numerical value. The 8-bit range is divided up into 8
94*6777b538SAndroid Build Coastguard Worker // regions of 0x20 characters each. Each of the three character types (numbers,
95*6777b538SAndroid Build Coastguard Worker // uppercase, lowercase) falls into different regions of this range. The table
96*6777b538SAndroid Build Coastguard Worker // contains the amount to subtract from characters in that range to get at
97*6777b538SAndroid Build Coastguard Worker // the corresponding numerical value.
98*6777b538SAndroid Build Coastguard Worker //
99*6777b538SAndroid Build Coastguard Worker // See HexDigitToValue for the lookup.
100*6777b538SAndroid Build Coastguard Worker extern const char kCharToHexLookup[8];
101*6777b538SAndroid Build Coastguard Worker
102*6777b538SAndroid Build Coastguard Worker // Assumes the input is a valid hex digit! Call IsHexChar before using this.
HexCharToValue(unsigned char c)103*6777b538SAndroid Build Coastguard Worker inline int HexCharToValue(unsigned char c) {
104*6777b538SAndroid Build Coastguard Worker return c - kCharToHexLookup[c / 0x20];
105*6777b538SAndroid Build Coastguard Worker }
106*6777b538SAndroid Build Coastguard Worker
107*6777b538SAndroid Build Coastguard Worker // Indicates if the given character is a dot or dot equivalent, returning the
108*6777b538SAndroid Build Coastguard Worker // number of characters taken by it. This will be one for a literal dot, 3 for
109*6777b538SAndroid Build Coastguard Worker // an escaped dot. If the character is not a dot, this will return 0.
110*6777b538SAndroid Build Coastguard Worker template <typename CHAR>
IsDot(const CHAR * spec,size_t offset,size_t end)111*6777b538SAndroid Build Coastguard Worker inline size_t IsDot(const CHAR* spec, size_t offset, size_t end) {
112*6777b538SAndroid Build Coastguard Worker if (spec[offset] == '.') {
113*6777b538SAndroid Build Coastguard Worker return 1;
114*6777b538SAndroid Build Coastguard Worker } else if (spec[offset] == '%' && offset + 3 <= end &&
115*6777b538SAndroid Build Coastguard Worker spec[offset + 1] == '2' &&
116*6777b538SAndroid Build Coastguard Worker (spec[offset + 2] == 'e' || spec[offset + 2] == 'E')) {
117*6777b538SAndroid Build Coastguard Worker // Found "%2e"
118*6777b538SAndroid Build Coastguard Worker return 3;
119*6777b538SAndroid Build Coastguard Worker }
120*6777b538SAndroid Build Coastguard Worker return 0;
121*6777b538SAndroid Build Coastguard Worker }
122*6777b538SAndroid Build Coastguard Worker
123*6777b538SAndroid Build Coastguard Worker // Returns the canonicalized version of the input character according to scheme
124*6777b538SAndroid Build Coastguard Worker // rules. This is implemented alongside the scheme canonicalizer, and is
125*6777b538SAndroid Build Coastguard Worker // required for relative URL resolving to test for scheme equality.
126*6777b538SAndroid Build Coastguard Worker //
127*6777b538SAndroid Build Coastguard Worker // Returns 0 if the input character is not a valid scheme character.
128*6777b538SAndroid Build Coastguard Worker char CanonicalSchemeChar(char16_t ch);
129*6777b538SAndroid Build Coastguard Worker
130*6777b538SAndroid Build Coastguard Worker // Write a single character, escaped, to the output. This always escapes: it
131*6777b538SAndroid Build Coastguard Worker // does no checking that thee character requires escaping.
132*6777b538SAndroid Build Coastguard Worker // Escaping makes sense only 8 bit chars, so code works in all cases of
133*6777b538SAndroid Build Coastguard Worker // input parameters (8/16bit).
134*6777b538SAndroid Build Coastguard Worker template <typename UINCHAR, typename OUTCHAR>
AppendEscapedChar(UINCHAR ch,CanonOutputT<OUTCHAR> * output)135*6777b538SAndroid Build Coastguard Worker inline void AppendEscapedChar(UINCHAR ch, CanonOutputT<OUTCHAR>* output) {
136*6777b538SAndroid Build Coastguard Worker output->push_back('%');
137*6777b538SAndroid Build Coastguard Worker std::string hex;
138*6777b538SAndroid Build Coastguard Worker base::AppendHexEncodedByte(static_cast<uint8_t>(ch), hex);
139*6777b538SAndroid Build Coastguard Worker output->push_back(static_cast<OUTCHAR>(hex[0]));
140*6777b538SAndroid Build Coastguard Worker output->push_back(static_cast<OUTCHAR>(hex[1]));
141*6777b538SAndroid Build Coastguard Worker }
142*6777b538SAndroid Build Coastguard Worker
143*6777b538SAndroid Build Coastguard Worker // The character we'll substitute for undecodable or invalid characters.
144*6777b538SAndroid Build Coastguard Worker extern const base_icu::UChar32 kUnicodeReplacementCharacter;
145*6777b538SAndroid Build Coastguard Worker
146*6777b538SAndroid Build Coastguard Worker // UTF-8 functions ------------------------------------------------------------
147*6777b538SAndroid Build Coastguard Worker
148*6777b538SAndroid Build Coastguard Worker // Reads one character in UTF-8 starting at |*begin| in |str|, places
149*6777b538SAndroid Build Coastguard Worker // the decoded value into |*code_point|, and returns true on success.
150*6777b538SAndroid Build Coastguard Worker // Otherwise, we'll return false and put the kUnicodeReplacementCharacter
151*6777b538SAndroid Build Coastguard Worker // into |*code_point|.
152*6777b538SAndroid Build Coastguard Worker //
153*6777b538SAndroid Build Coastguard Worker // |*begin| will be updated to point to the last character consumed so it
154*6777b538SAndroid Build Coastguard Worker // can be incremented in a loop and will be ready for the next character.
155*6777b538SAndroid Build Coastguard Worker // (for a single-byte ASCII character, it will not be changed).
156*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
157*6777b538SAndroid Build Coastguard Worker bool ReadUTFCharLossy(const char* str,
158*6777b538SAndroid Build Coastguard Worker size_t* begin,
159*6777b538SAndroid Build Coastguard Worker size_t length,
160*6777b538SAndroid Build Coastguard Worker base_icu::UChar32* code_point_out);
161*6777b538SAndroid Build Coastguard Worker
162*6777b538SAndroid Build Coastguard Worker // Generic To-UTF-8 converter. This will call the given append method for each
163*6777b538SAndroid Build Coastguard Worker // character that should be appended, with the given output method. Wrappers
164*6777b538SAndroid Build Coastguard Worker // are provided below for escaped and non-escaped versions of this.
165*6777b538SAndroid Build Coastguard Worker //
166*6777b538SAndroid Build Coastguard Worker // The char_value must have already been checked that it's a valid Unicode
167*6777b538SAndroid Build Coastguard Worker // character.
168*6777b538SAndroid Build Coastguard Worker template <class Output, void Appender(unsigned char, Output*)>
DoAppendUTF8(base_icu::UChar32 char_value,Output * output)169*6777b538SAndroid Build Coastguard Worker inline void DoAppendUTF8(base_icu::UChar32 char_value, Output* output) {
170*6777b538SAndroid Build Coastguard Worker DCHECK(char_value >= 0);
171*6777b538SAndroid Build Coastguard Worker DCHECK(char_value <= 0x10FFFF);
172*6777b538SAndroid Build Coastguard Worker if (char_value <= 0x7f) {
173*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(char_value), output);
174*6777b538SAndroid Build Coastguard Worker } else if (char_value <= 0x7ff) {
175*6777b538SAndroid Build Coastguard Worker // 110xxxxx 10xxxxxx
176*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0xC0 | (char_value >> 6)), output);
177*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), output);
178*6777b538SAndroid Build Coastguard Worker } else if (char_value <= 0xffff) {
179*6777b538SAndroid Build Coastguard Worker // 1110xxxx 10xxxxxx 10xxxxxx
180*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0xe0 | (char_value >> 12)), output);
181*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0x80 | ((char_value >> 6) & 0x3f)),
182*6777b538SAndroid Build Coastguard Worker output);
183*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), output);
184*6777b538SAndroid Build Coastguard Worker } else {
185*6777b538SAndroid Build Coastguard Worker // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
186*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0xf0 | (char_value >> 18)), output);
187*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0x80 | ((char_value >> 12) & 0x3f)),
188*6777b538SAndroid Build Coastguard Worker output);
189*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0x80 | ((char_value >> 6) & 0x3f)),
190*6777b538SAndroid Build Coastguard Worker output);
191*6777b538SAndroid Build Coastguard Worker Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), output);
192*6777b538SAndroid Build Coastguard Worker }
193*6777b538SAndroid Build Coastguard Worker }
194*6777b538SAndroid Build Coastguard Worker
195*6777b538SAndroid Build Coastguard Worker // Helper used by AppendUTF8Value below. We use an unsigned parameter so there
196*6777b538SAndroid Build Coastguard Worker // are no funny sign problems with the input, but then have to convert it to
197*6777b538SAndroid Build Coastguard Worker // a regular char for appending.
AppendCharToOutput(unsigned char ch,CanonOutput * output)198*6777b538SAndroid Build Coastguard Worker inline void AppendCharToOutput(unsigned char ch, CanonOutput* output) {
199*6777b538SAndroid Build Coastguard Worker output->push_back(static_cast<char>(ch));
200*6777b538SAndroid Build Coastguard Worker }
201*6777b538SAndroid Build Coastguard Worker
202*6777b538SAndroid Build Coastguard Worker // Writes the given character to the output as UTF-8. This does NO checking
203*6777b538SAndroid Build Coastguard Worker // of the validity of the Unicode characters; the caller should ensure that
204*6777b538SAndroid Build Coastguard Worker // the value it is appending is valid to append.
AppendUTF8Value(base_icu::UChar32 char_value,CanonOutput * output)205*6777b538SAndroid Build Coastguard Worker inline void AppendUTF8Value(base_icu::UChar32 char_value, CanonOutput* output) {
206*6777b538SAndroid Build Coastguard Worker DoAppendUTF8<CanonOutput, AppendCharToOutput>(char_value, output);
207*6777b538SAndroid Build Coastguard Worker }
208*6777b538SAndroid Build Coastguard Worker
209*6777b538SAndroid Build Coastguard Worker // Writes the given character to the output as UTF-8, escaping ALL
210*6777b538SAndroid Build Coastguard Worker // characters (even when they are ASCII). This does NO checking of the
211*6777b538SAndroid Build Coastguard Worker // validity of the Unicode characters; the caller should ensure that the value
212*6777b538SAndroid Build Coastguard Worker // it is appending is valid to append.
AppendUTF8EscapedValue(base_icu::UChar32 char_value,CanonOutput * output)213*6777b538SAndroid Build Coastguard Worker inline void AppendUTF8EscapedValue(base_icu::UChar32 char_value,
214*6777b538SAndroid Build Coastguard Worker CanonOutput* output) {
215*6777b538SAndroid Build Coastguard Worker DoAppendUTF8<CanonOutput, AppendEscapedChar>(char_value, output);
216*6777b538SAndroid Build Coastguard Worker }
217*6777b538SAndroid Build Coastguard Worker
218*6777b538SAndroid Build Coastguard Worker // UTF-16 functions -----------------------------------------------------------
219*6777b538SAndroid Build Coastguard Worker
220*6777b538SAndroid Build Coastguard Worker // Reads one character in UTF-16 starting at |*begin| in |str|, places
221*6777b538SAndroid Build Coastguard Worker // the decoded value into |*code_point|, and returns true on success.
222*6777b538SAndroid Build Coastguard Worker // Otherwise, we'll return false and put the kUnicodeReplacementCharacter
223*6777b538SAndroid Build Coastguard Worker // into |*code_point|.
224*6777b538SAndroid Build Coastguard Worker //
225*6777b538SAndroid Build Coastguard Worker // |*begin| will be updated to point to the last character consumed so it
226*6777b538SAndroid Build Coastguard Worker // can be incremented in a loop and will be ready for the next character.
227*6777b538SAndroid Build Coastguard Worker // (for a single-16-bit-word character, it will not be changed).
228*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
229*6777b538SAndroid Build Coastguard Worker bool ReadUTFCharLossy(const char16_t* str,
230*6777b538SAndroid Build Coastguard Worker size_t* begin,
231*6777b538SAndroid Build Coastguard Worker size_t length,
232*6777b538SAndroid Build Coastguard Worker base_icu::UChar32* code_point_out);
233*6777b538SAndroid Build Coastguard Worker
234*6777b538SAndroid Build Coastguard Worker // Equivalent to U16_APPEND_UNSAFE in ICU but uses our output method.
AppendUTF16Value(base_icu::UChar32 code_point,CanonOutputT<char16_t> * output)235*6777b538SAndroid Build Coastguard Worker inline void AppendUTF16Value(base_icu::UChar32 code_point,
236*6777b538SAndroid Build Coastguard Worker CanonOutputT<char16_t>* output) {
237*6777b538SAndroid Build Coastguard Worker if (code_point > 0xffff) {
238*6777b538SAndroid Build Coastguard Worker output->push_back(static_cast<char16_t>((code_point >> 10) + 0xd7c0));
239*6777b538SAndroid Build Coastguard Worker output->push_back(static_cast<char16_t>((code_point & 0x3ff) | 0xdc00));
240*6777b538SAndroid Build Coastguard Worker } else {
241*6777b538SAndroid Build Coastguard Worker output->push_back(static_cast<char16_t>(code_point));
242*6777b538SAndroid Build Coastguard Worker }
243*6777b538SAndroid Build Coastguard Worker }
244*6777b538SAndroid Build Coastguard Worker
245*6777b538SAndroid Build Coastguard Worker // Escaping functions ---------------------------------------------------------
246*6777b538SAndroid Build Coastguard Worker
247*6777b538SAndroid Build Coastguard Worker // Writes the given character to the output as UTF-8, escaped. Call this
248*6777b538SAndroid Build Coastguard Worker // function only when the input is wide. Returns true on success. Failure
249*6777b538SAndroid Build Coastguard Worker // means there was some problem with the encoding, we'll still try to
250*6777b538SAndroid Build Coastguard Worker // update the |*begin| pointer and add a placeholder character to the
251*6777b538SAndroid Build Coastguard Worker // output so processing can continue.
252*6777b538SAndroid Build Coastguard Worker //
253*6777b538SAndroid Build Coastguard Worker // We will append the character starting at ch[begin] with the buffer ch
254*6777b538SAndroid Build Coastguard Worker // being |length|. |*begin| will be updated to point to the last character
255*6777b538SAndroid Build Coastguard Worker // consumed (we may consume more than one for UTF-16) so that if called in
256*6777b538SAndroid Build Coastguard Worker // a loop, incrementing the pointer will move to the next character.
257*6777b538SAndroid Build Coastguard Worker //
258*6777b538SAndroid Build Coastguard Worker // Every single output character will be escaped. This means that if you
259*6777b538SAndroid Build Coastguard Worker // give it an ASCII character as input, it will be escaped. Some code uses
260*6777b538SAndroid Build Coastguard Worker // this when it knows that a character is invalid according to its rules
261*6777b538SAndroid Build Coastguard Worker // for validity. If you don't want escaping for ASCII characters, you will
262*6777b538SAndroid Build Coastguard Worker // have to filter them out prior to calling this function.
263*6777b538SAndroid Build Coastguard Worker //
264*6777b538SAndroid Build Coastguard Worker // Assumes that ch[begin] is within range in the array, but does not assume
265*6777b538SAndroid Build Coastguard Worker // that any following characters are.
AppendUTF8EscapedChar(const char16_t * str,size_t * begin,size_t length,CanonOutput * output)266*6777b538SAndroid Build Coastguard Worker inline bool AppendUTF8EscapedChar(const char16_t* str,
267*6777b538SAndroid Build Coastguard Worker size_t* begin,
268*6777b538SAndroid Build Coastguard Worker size_t length,
269*6777b538SAndroid Build Coastguard Worker CanonOutput* output) {
270*6777b538SAndroid Build Coastguard Worker // UTF-16 input. ReadUTFCharLossy will handle invalid characters for us and
271*6777b538SAndroid Build Coastguard Worker // give us the kUnicodeReplacementCharacter, so we don't have to do special
272*6777b538SAndroid Build Coastguard Worker // checking after failure, just pass through the failure to the caller.
273*6777b538SAndroid Build Coastguard Worker base_icu::UChar32 char_value;
274*6777b538SAndroid Build Coastguard Worker bool success = ReadUTFCharLossy(str, begin, length, &char_value);
275*6777b538SAndroid Build Coastguard Worker AppendUTF8EscapedValue(char_value, output);
276*6777b538SAndroid Build Coastguard Worker return success;
277*6777b538SAndroid Build Coastguard Worker }
278*6777b538SAndroid Build Coastguard Worker
279*6777b538SAndroid Build Coastguard Worker // Handles UTF-8 input. See the wide version above for usage.
AppendUTF8EscapedChar(const char * str,size_t * begin,size_t length,CanonOutput * output)280*6777b538SAndroid Build Coastguard Worker inline bool AppendUTF8EscapedChar(const char* str,
281*6777b538SAndroid Build Coastguard Worker size_t* begin,
282*6777b538SAndroid Build Coastguard Worker size_t length,
283*6777b538SAndroid Build Coastguard Worker CanonOutput* output) {
284*6777b538SAndroid Build Coastguard Worker // ReadUTFCharLossy will handle invalid characters for us and give us the
285*6777b538SAndroid Build Coastguard Worker // kUnicodeReplacementCharacter, so we don't have to do special checking
286*6777b538SAndroid Build Coastguard Worker // after failure, just pass through the failure to the caller.
287*6777b538SAndroid Build Coastguard Worker base_icu::UChar32 ch;
288*6777b538SAndroid Build Coastguard Worker bool success = ReadUTFCharLossy(str, begin, length, &ch);
289*6777b538SAndroid Build Coastguard Worker AppendUTF8EscapedValue(ch, output);
290*6777b538SAndroid Build Coastguard Worker return success;
291*6777b538SAndroid Build Coastguard Worker }
292*6777b538SAndroid Build Coastguard Worker
293*6777b538SAndroid Build Coastguard Worker // URL Standard: https://url.spec.whatwg.org/#c0-control-percent-encode-set
294*6777b538SAndroid Build Coastguard Worker template <typename CHAR>
IsInC0ControlPercentEncodeSet(CHAR ch)295*6777b538SAndroid Build Coastguard Worker bool IsInC0ControlPercentEncodeSet(CHAR ch) {
296*6777b538SAndroid Build Coastguard Worker return ch < 0x20 || ch > 0x7E;
297*6777b538SAndroid Build Coastguard Worker }
298*6777b538SAndroid Build Coastguard Worker
299*6777b538SAndroid Build Coastguard Worker // Given a '%' character at |*begin| in the string |spec|, this will decode
300*6777b538SAndroid Build Coastguard Worker // the escaped value and put it into |*unescaped_value| on success (returns
301*6777b538SAndroid Build Coastguard Worker // true). On failure, this will return false, and will not write into
302*6777b538SAndroid Build Coastguard Worker // |*unescaped_value|.
303*6777b538SAndroid Build Coastguard Worker //
304*6777b538SAndroid Build Coastguard Worker // |*begin| will be updated to point to the last character of the escape
305*6777b538SAndroid Build Coastguard Worker // sequence so that when called with the index of a for loop, the next time
306*6777b538SAndroid Build Coastguard Worker // through it will point to the next character to be considered. On failure,
307*6777b538SAndroid Build Coastguard Worker // |*begin| will be unchanged.
Is8BitChar(char c)308*6777b538SAndroid Build Coastguard Worker inline bool Is8BitChar(char c) {
309*6777b538SAndroid Build Coastguard Worker return true; // this case is specialized to avoid a warning
310*6777b538SAndroid Build Coastguard Worker }
Is8BitChar(char16_t c)311*6777b538SAndroid Build Coastguard Worker inline bool Is8BitChar(char16_t c) {
312*6777b538SAndroid Build Coastguard Worker return c <= 255;
313*6777b538SAndroid Build Coastguard Worker }
314*6777b538SAndroid Build Coastguard Worker
315*6777b538SAndroid Build Coastguard Worker template <typename CHAR>
DecodeEscaped(const CHAR * spec,size_t * begin,size_t end,unsigned char * unescaped_value)316*6777b538SAndroid Build Coastguard Worker inline bool DecodeEscaped(const CHAR* spec,
317*6777b538SAndroid Build Coastguard Worker size_t* begin,
318*6777b538SAndroid Build Coastguard Worker size_t end,
319*6777b538SAndroid Build Coastguard Worker unsigned char* unescaped_value) {
320*6777b538SAndroid Build Coastguard Worker if (*begin + 3 > end || !Is8BitChar(spec[*begin + 1]) ||
321*6777b538SAndroid Build Coastguard Worker !Is8BitChar(spec[*begin + 2])) {
322*6777b538SAndroid Build Coastguard Worker // Invalid escape sequence because there's not enough room, or the
323*6777b538SAndroid Build Coastguard Worker // digits are not ASCII.
324*6777b538SAndroid Build Coastguard Worker return false;
325*6777b538SAndroid Build Coastguard Worker }
326*6777b538SAndroid Build Coastguard Worker
327*6777b538SAndroid Build Coastguard Worker unsigned char first = static_cast<unsigned char>(spec[*begin + 1]);
328*6777b538SAndroid Build Coastguard Worker unsigned char second = static_cast<unsigned char>(spec[*begin + 2]);
329*6777b538SAndroid Build Coastguard Worker if (!IsHexChar(first) || !IsHexChar(second)) {
330*6777b538SAndroid Build Coastguard Worker // Invalid hex digits, fail.
331*6777b538SAndroid Build Coastguard Worker return false;
332*6777b538SAndroid Build Coastguard Worker }
333*6777b538SAndroid Build Coastguard Worker
334*6777b538SAndroid Build Coastguard Worker // Valid escape sequence.
335*6777b538SAndroid Build Coastguard Worker *unescaped_value = static_cast<unsigned char>((HexCharToValue(first) << 4) +
336*6777b538SAndroid Build Coastguard Worker HexCharToValue(second));
337*6777b538SAndroid Build Coastguard Worker *begin += 2;
338*6777b538SAndroid Build Coastguard Worker return true;
339*6777b538SAndroid Build Coastguard Worker }
340*6777b538SAndroid Build Coastguard Worker
341*6777b538SAndroid Build Coastguard Worker // Appends the given substring to the output, escaping "some" characters that
342*6777b538SAndroid Build Coastguard Worker // it feels may not be safe. It assumes the input values are all contained in
343*6777b538SAndroid Build Coastguard Worker // 8-bit although it allows any type.
344*6777b538SAndroid Build Coastguard Worker //
345*6777b538SAndroid Build Coastguard Worker // This is used in error cases to append invalid output so that it looks
346*6777b538SAndroid Build Coastguard Worker // approximately correct. Non-error cases should not call this function since
347*6777b538SAndroid Build Coastguard Worker // the escaping rules are not guaranteed!
348*6777b538SAndroid Build Coastguard Worker void AppendInvalidNarrowString(const char* spec,
349*6777b538SAndroid Build Coastguard Worker size_t begin,
350*6777b538SAndroid Build Coastguard Worker size_t end,
351*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
352*6777b538SAndroid Build Coastguard Worker void AppendInvalidNarrowString(const char16_t* spec,
353*6777b538SAndroid Build Coastguard Worker size_t begin,
354*6777b538SAndroid Build Coastguard Worker size_t end,
355*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
356*6777b538SAndroid Build Coastguard Worker
357*6777b538SAndroid Build Coastguard Worker // Misc canonicalization helpers ----------------------------------------------
358*6777b538SAndroid Build Coastguard Worker
359*6777b538SAndroid Build Coastguard Worker // Converts between UTF-8 and UTF-16, returning true on successful conversion.
360*6777b538SAndroid Build Coastguard Worker // The output will be appended to the given canonicalizer output (so make sure
361*6777b538SAndroid Build Coastguard Worker // it's empty if you want to replace).
362*6777b538SAndroid Build Coastguard Worker //
363*6777b538SAndroid Build Coastguard Worker // On invalid input, this will still write as much output as possible,
364*6777b538SAndroid Build Coastguard Worker // replacing the invalid characters with the "invalid character". It will
365*6777b538SAndroid Build Coastguard Worker // return false in the failure case, and the caller should not continue as
366*6777b538SAndroid Build Coastguard Worker // normal.
367*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
368*6777b538SAndroid Build Coastguard Worker bool ConvertUTF16ToUTF8(const char16_t* input,
369*6777b538SAndroid Build Coastguard Worker size_t input_len,
370*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
371*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
372*6777b538SAndroid Build Coastguard Worker bool ConvertUTF8ToUTF16(const char* input,
373*6777b538SAndroid Build Coastguard Worker size_t input_len,
374*6777b538SAndroid Build Coastguard Worker CanonOutputT<char16_t>* output);
375*6777b538SAndroid Build Coastguard Worker
376*6777b538SAndroid Build Coastguard Worker // Converts from UTF-16 to 8-bit using the character set converter. If the
377*6777b538SAndroid Build Coastguard Worker // converter is NULL, this will use UTF-8.
378*6777b538SAndroid Build Coastguard Worker void ConvertUTF16ToQueryEncoding(const char16_t* input,
379*6777b538SAndroid Build Coastguard Worker const Component& query,
380*6777b538SAndroid Build Coastguard Worker CharsetConverter* converter,
381*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
382*6777b538SAndroid Build Coastguard Worker
383*6777b538SAndroid Build Coastguard Worker // Applies the replacements to the given component source. The component source
384*6777b538SAndroid Build Coastguard Worker // should be pre-initialized to the "old" base. That is, all pointers will
385*6777b538SAndroid Build Coastguard Worker // point to the spec of the old URL, and all of the Parsed components will
386*6777b538SAndroid Build Coastguard Worker // be indices into that string.
387*6777b538SAndroid Build Coastguard Worker //
388*6777b538SAndroid Build Coastguard Worker // The pointers and components in the |source| for all non-NULL strings in the
389*6777b538SAndroid Build Coastguard Worker // |repl| (replacements) will be updated to reference those strings.
390*6777b538SAndroid Build Coastguard Worker // Canonicalizing with the new |source| and |parsed| can then combine URL
391*6777b538SAndroid Build Coastguard Worker // components from many different strings.
392*6777b538SAndroid Build Coastguard Worker void SetupOverrideComponents(const char* base,
393*6777b538SAndroid Build Coastguard Worker const Replacements<char>& repl,
394*6777b538SAndroid Build Coastguard Worker URLComponentSource<char>* source,
395*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
396*6777b538SAndroid Build Coastguard Worker
397*6777b538SAndroid Build Coastguard Worker // Like the above 8-bit version, except that it additionally converts the
398*6777b538SAndroid Build Coastguard Worker // UTF-16 input to UTF-8 before doing the overrides.
399*6777b538SAndroid Build Coastguard Worker //
400*6777b538SAndroid Build Coastguard Worker // The given utf8_buffer is used to store the converted components. They will
401*6777b538SAndroid Build Coastguard Worker // be appended one after another, with the parsed structure identifying the
402*6777b538SAndroid Build Coastguard Worker // appropriate substrings. This buffer is a parameter because the source has
403*6777b538SAndroid Build Coastguard Worker // no storage, so the buffer must have the same lifetime as the source
404*6777b538SAndroid Build Coastguard Worker // parameter owned by the caller.
405*6777b538SAndroid Build Coastguard Worker //
406*6777b538SAndroid Build Coastguard Worker // THE CALLER MUST NOT ADD TO THE |utf8_buffer| AFTER THIS CALL. Members of
407*6777b538SAndroid Build Coastguard Worker // |source| will point into this buffer, which could be invalidated if
408*6777b538SAndroid Build Coastguard Worker // additional data is added and the CanonOutput resizes its buffer.
409*6777b538SAndroid Build Coastguard Worker //
410*6777b538SAndroid Build Coastguard Worker // Returns true on success. False means that the input was not valid UTF-16,
411*6777b538SAndroid Build Coastguard Worker // although we will have still done the override with "invalid characters" in
412*6777b538SAndroid Build Coastguard Worker // place of errors.
413*6777b538SAndroid Build Coastguard Worker bool SetupUTF16OverrideComponents(const char* base,
414*6777b538SAndroid Build Coastguard Worker const Replacements<char16_t>& repl,
415*6777b538SAndroid Build Coastguard Worker CanonOutput* utf8_buffer,
416*6777b538SAndroid Build Coastguard Worker URLComponentSource<char>* source,
417*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
418*6777b538SAndroid Build Coastguard Worker
419*6777b538SAndroid Build Coastguard Worker // Implemented in url_canon_path.cc, these are required by the relative URL
420*6777b538SAndroid Build Coastguard Worker // resolver as well, so we declare them here.
421*6777b538SAndroid Build Coastguard Worker bool CanonicalizePartialPathInternal(const char* spec,
422*6777b538SAndroid Build Coastguard Worker const Component& path,
423*6777b538SAndroid Build Coastguard Worker size_t path_begin_in_output,
424*6777b538SAndroid Build Coastguard Worker CanonMode canon_mode,
425*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
426*6777b538SAndroid Build Coastguard Worker bool CanonicalizePartialPathInternal(const char16_t* spec,
427*6777b538SAndroid Build Coastguard Worker const Component& path,
428*6777b538SAndroid Build Coastguard Worker size_t path_begin_in_output,
429*6777b538SAndroid Build Coastguard Worker CanonMode canon_mode,
430*6777b538SAndroid Build Coastguard Worker CanonOutput* output);
431*6777b538SAndroid Build Coastguard Worker
432*6777b538SAndroid Build Coastguard Worker // Find the position of a bona fide Windows drive letter in the given path. If
433*6777b538SAndroid Build Coastguard Worker // no leading drive letter is found, -1 is returned. This function correctly
434*6777b538SAndroid Build Coastguard Worker // treats /c:/foo and /./c:/foo as having drive letters, and /def/c:/foo as not
435*6777b538SAndroid Build Coastguard Worker // having a drive letter.
436*6777b538SAndroid Build Coastguard Worker //
437*6777b538SAndroid Build Coastguard Worker // Exported for tests.
438*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
439*6777b538SAndroid Build Coastguard Worker int FindWindowsDriveLetter(const char* spec, int begin, int end);
440*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
441*6777b538SAndroid Build Coastguard Worker int FindWindowsDriveLetter(const char16_t* spec, int begin, int end);
442*6777b538SAndroid Build Coastguard Worker
443*6777b538SAndroid Build Coastguard Worker #ifndef WIN32
444*6777b538SAndroid Build Coastguard Worker
445*6777b538SAndroid Build Coastguard Worker // Implementations of Windows' int-to-string conversions
446*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
447*6777b538SAndroid Build Coastguard Worker int _itoa_s(int value, char* buffer, size_t size_in_chars, int radix);
448*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
449*6777b538SAndroid Build Coastguard Worker int _itow_s(int value, char16_t* buffer, size_t size_in_chars, int radix);
450*6777b538SAndroid Build Coastguard Worker
451*6777b538SAndroid Build Coastguard Worker // Secure template overloads for these functions
452*6777b538SAndroid Build Coastguard Worker template <size_t N>
_itoa_s(int value,char (& buffer)[N],int radix)453*6777b538SAndroid Build Coastguard Worker inline int _itoa_s(int value, char (&buffer)[N], int radix) {
454*6777b538SAndroid Build Coastguard Worker return _itoa_s(value, buffer, N, radix);
455*6777b538SAndroid Build Coastguard Worker }
456*6777b538SAndroid Build Coastguard Worker
457*6777b538SAndroid Build Coastguard Worker template <size_t N>
_itow_s(int value,char16_t (& buffer)[N],int radix)458*6777b538SAndroid Build Coastguard Worker inline int _itow_s(int value, char16_t (&buffer)[N], int radix) {
459*6777b538SAndroid Build Coastguard Worker return _itow_s(value, buffer, N, radix);
460*6777b538SAndroid Build Coastguard Worker }
461*6777b538SAndroid Build Coastguard Worker
462*6777b538SAndroid Build Coastguard Worker // _strtoui64 and strtoull behave the same
_strtoui64(const char * nptr,char ** endptr,int base)463*6777b538SAndroid Build Coastguard Worker inline unsigned long long _strtoui64(const char* nptr,
464*6777b538SAndroid Build Coastguard Worker char** endptr,
465*6777b538SAndroid Build Coastguard Worker int base) {
466*6777b538SAndroid Build Coastguard Worker return strtoull(nptr, endptr, base);
467*6777b538SAndroid Build Coastguard Worker }
468*6777b538SAndroid Build Coastguard Worker
469*6777b538SAndroid Build Coastguard Worker #endif // WIN32
470*6777b538SAndroid Build Coastguard Worker
471*6777b538SAndroid Build Coastguard Worker // The threshold we set to consider SIMD processing, in bytes; there is
472*6777b538SAndroid Build Coastguard Worker // no deep theory here, it's just set empirically to a value that seems
473*6777b538SAndroid Build Coastguard Worker // to be good. (We don't really know why there's a slowdown for zero;
474*6777b538SAndroid Build Coastguard Worker // but a guess would be that there's no need in going into a complex loop
475*6777b538SAndroid Build Coastguard Worker // with a lot of setup for a five-byte string.)
476*6777b538SAndroid Build Coastguard Worker static constexpr int kMinimumLengthForSIMD = 50;
477*6777b538SAndroid Build Coastguard Worker
478*6777b538SAndroid Build Coastguard Worker } // namespace url
479*6777b538SAndroid Build Coastguard Worker
480*6777b538SAndroid Build Coastguard Worker #endif // URL_URL_CANON_INTERNAL_H_
481