1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copyright 2007, Google Inc. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// All rights reserved. 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Redistribution and use in source and binary forms, with or without 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// modification, are permitted provided that the following conditions are 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// met: 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Redistributions of source code must retain the above copyright 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// notice, this list of conditions and the following disclaimer. 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Redistributions in binary form must reproduce the above 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// copyright notice, this list of conditions and the following disclaimer 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// in the documentation and/or other materials provided with the 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// distribution. 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Neither the name of Google Inc. nor the names of its 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// contributors may be used to endorse or promote products derived from 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// this software without specific prior written permission. 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This file is intended to be included in another C++ file where the character 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// types are defined. This allows us to write mostly generic code, but not have 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// templace bloat because everything is inlined when anybody calls any of our 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// functions. 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef GOOGLEURL_SRC_URL_CANON_INTERNAL_H__ 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define GOOGLEURL_SRC_URL_CANON_INTERNAL_H__ 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <stdlib.h> 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 403345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/logging.h" 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "googleurl/src/url_canon.h" 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace url_canon { 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Character type handling ----------------------------------------------------- 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Bits that identify different character types. These types identify different 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// bits that are set for each 8-bit character in the kSharedCharTypeTable. 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottenum SharedCharTypes { 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Characters that do not require escaping in queries. Characters that do 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // not have this flag will be escaped, see url_canon_query.cc 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CHAR_QUERY = 1, 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Valid in the username/password field. 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CHAR_USERINFO = 2, 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Valid in a IPv4 address (digits plus dot and 'x' for hex). 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CHAR_IPV4 = 4, 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Valid in an ASCII-representation of a hex digit (as in %-escaped). 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CHAR_HEX = 8, 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Valid in an ASCII-representation of a decimal digit. 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CHAR_DEC = 16, 65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Valid in an ASCII-representation of an octal digit. 67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CHAR_OCT = 32, 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This table contains the flags in SharedCharTypes for each 8-bit character. 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Some canonicalization functions have their own specialized lookup table. 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// For those with simple requirements, we have collected the flags in one 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// place so there are fewer lookup tables to load into the CPU cache. 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Using an unsigned char type has a small but measurable performance benefit 76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// over using a 32-bit number. 77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottextern const unsigned char kSharedCharTypeTable[0x100]; 78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// More readable wrappers around the character type lookup table. 80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool IsCharOfType(unsigned char c, SharedCharTypes type) { 81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return !!(kSharedCharTypeTable[c] & type); 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool IsQueryChar(unsigned char c) { 84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return IsCharOfType(c, CHAR_QUERY); 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool IsIPv4Char(unsigned char c) { 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return IsCharOfType(c, CHAR_IPV4); 88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool IsHexChar(unsigned char c) { 90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return IsCharOfType(c, CHAR_HEX); 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Appends the given string to the output, escaping characters that do not 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// match the given |type| in SharedCharTypes. 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid AppendStringOfType(const char* source, int length, 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SharedCharTypes type, 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid AppendStringOfType(const char16* source, int length, 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott SharedCharTypes type, 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Maps the hex numerical values 0x0 to 0xf to the corresponding ASCII digit 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// that will be used to represent it. 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottextern const char kHexCharLookup[0x10]; 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This lookup table allows fast conversion between ASCII hex letters and their 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// corresponding numerical value. The 8-bit range is divided up into 8 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// regions of 0x20 characters each. Each of the three character types (numbers, 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// uppercase, lowercase) falls into different regions of this range. The table 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// contains the amount to subtract from characters in that range to get at 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the corresponding numerical value. 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// See HexDigitToValue for the lookup. 114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottextern const char kCharToHexLookup[8]; 115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Assumes the input is a valid hex digit! Call IsHexChar before using this. 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline unsigned char HexCharToValue(unsigned char c) { 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return c - kCharToHexLookup[c / 0x20]; 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Indicates if the given character is a dot or dot equivalent, returning the 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// number of characters taken by it. This will be one for a literal dot, 3 for 123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// an escaped dot. If the character is not a dot, this will return 0. 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<typename CHAR> 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline int IsDot(const CHAR* spec, int offset, int end) { 126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (spec[offset] == '.') { 127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return 1; 128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } else if (spec[offset] == '%' && offset + 3 <= end && 129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott spec[offset + 1] == '2' && 130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott (spec[offset + 2] == 'e' || spec[offset + 2] == 'E')) { 131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Found "%2e" 132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return 3; 133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return 0; 135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the canonicalized version of the input character according to scheme 138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// rules. This is implemented alongside the scheme canonicalizer, and is 139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// required for relative URL resolving to test for scheme equality. 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns 0 if the input character is not a valid scheme character. 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottchar CanonicalSchemeChar(char16 ch); 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Write a single character, escaped, to the output. This always escapes: it 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// does no checking that thee character requires escaping. 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Escaping makes sense only 8 bit chars, so code works in all cases of 147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// input parameters (8/16bit). 148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<typename UINCHAR, typename OUTCHAR> 149c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline void AppendEscapedChar(UINCHAR ch, 150c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutputT<OUTCHAR>* output) { 151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back('%'); 152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back(kHexCharLookup[ch >> 4]); 153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back(kHexCharLookup[ch & 0xf]); 154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The character we'll substitute for undecodable or invalid characters. 157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottextern const char16 kUnicodeReplacementCharacter; 158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// UTF-8 functions ------------------------------------------------------------ 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Reads one character in UTF-8 starting at |*begin| in |str| and places 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the decoded value into |*code_point|. If the character is valid, we will 163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// return true. If invalid, we'll return false and put the 164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// kUnicodeReplacementCharacter into |*code_point|. 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |*begin| will be updated to point to the last character consumed so it 167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// can be incremented in a loop and will be ready for the next character. 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (for a single-byte ASCII character, it will not be changed). 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Implementation is in url_canon_icu.cc. 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ReadUTFChar(const char* str, int* begin, int length, 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned* code_point_out); 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Generic To-UTF-8 converter. This will call the given append method for each 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// character that should be appended, with the given output method. Wrappers 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// are provided below for escaped and non-escaped versions of this. 1773345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// 1783345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// The char_value must have already been checked that it's a valid Unicode 1793345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// character. 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<class Output, void Appender(unsigned char, Output*)> 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline void DoAppendUTF8(unsigned char_value, Output* output) { 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (char_value <= 0x7f) { 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(char_value), output); 184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } else if (char_value <= 0x7ff) { 185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 110xxxxx 10xxxxxx 186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0xC0 | (char_value >> 6)), 187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), 189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 190c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } else if (char_value <= 0xffff) { 191c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 1110xxxx 10xxxxxx 10xxxxxx 192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0xe0 | (char_value >> 12)), 193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0x80 | ((char_value >> 6) & 0x3f)), 195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), 197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 1983345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick } else if (char_value <= 0x10FFFF) { // Max unicode code point. 199c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0xf0 | (char_value >> 18)), 201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0x80 | ((char_value >> 12) & 0x3f)), 203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0x80 | ((char_value >> 6) & 0x3f)), 205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 206c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott Appender(static_cast<unsigned char>(0x80 | (char_value & 0x3f)), 207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output); 208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } else { 2093345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // Invalid UTF-8 character (>20 bits). 2103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick NOTREACHED(); 211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 212c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Helper used by AppendUTF8Value below. We use an unsigned parameter so there 215c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// are no funny sign problems with the input, but then have to convert it to 216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// a regular char for appending. 217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline void AppendCharToOutput(unsigned char ch, CanonOutput* output) { 218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back(static_cast<char>(ch)); 219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Writes the given character to the output as UTF-8. This does NO checking 222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// of the validity of the unicode characters; the caller should ensure that 223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the value it is appending is valid to append. 224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline void AppendUTF8Value(unsigned char_value, CanonOutput* output) { 225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott DoAppendUTF8<CanonOutput, AppendCharToOutput>(char_value, output); 226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Writes the given character to the output as UTF-8, escaping ALL 229c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// characters (even when they are ASCII). This does NO checking of the 230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// validity of the unicode characters; the caller should ensure that the value 231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// it is appending is valid to append. 232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline void AppendUTF8EscapedValue(unsigned char_value, CanonOutput* output) { 233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott DoAppendUTF8<CanonOutput, AppendEscapedChar>(char_value, output); 234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// UTF-16 functions ----------------------------------------------------------- 237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Reads one character in UTF-16 starting at |*begin| in |str| and places 239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the decoded value into |*code_point|. If the character is valid, we will 240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// return true. If invalid, we'll return false and put the 241c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// kUnicodeReplacementCharacter into |*code_point|. 242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |*begin| will be updated to point to the last character consumed so it 244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// can be incremented in a loop and will be ready for the next character. 245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (for a single-16-bit-word character, it will not be changed). 246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 247c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Implementation is in url_canon_icu.cc. 248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ReadUTFChar(const char16* str, int* begin, int length, 249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned* code_point); 250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Equivalent to U16_APPEND_UNSAFE in ICU but uses our output method. 252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline void AppendUTF16Value(unsigned code_point, 253c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutputT<char16>* output) { 254c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (code_point > 0xffff) { 255c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back(static_cast<char16>((code_point >> 10) + 0xd7c0)); 256c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back(static_cast<char16>((code_point & 0x3ff) | 0xdc00)); 257c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } else { 258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott output->push_back(static_cast<char16>(code_point)); 259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 262c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Escaping functions --------------------------------------------------------- 263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Writes the given character to the output as UTF-8, escaped. Call this 265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// function only when the input is wide. Returns true on success. Failure 266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// means there was some problem with the encoding, we'll still try to 267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// update the |*begin| pointer and add a placeholder character to the 268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// output so processing can continue. 269c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 270c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// We will append the character starting at ch[begin] with the buffer ch 271c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// being |length|. |*begin| will be updated to point to the last character 272c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// consumed (we may consume more than one for UTF-16) so that if called in 273c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// a loop, incrementing the pointer will move to the next character. 274c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 275c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Every single output character will be escaped. This means that if you 276c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// give it an ASCII character as input, it will be escaped. Some code uses 277c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// this when it knows that a character is invalid according to its rules 278c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// for validity. If you don't want escaping for ASCII characters, you will 279c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// have to filter them out prior to calling this function. 280c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 281c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Assumes that ch[begin] is within range in the array, but does not assume 282c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// that any following characters are. 283c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool AppendUTF8EscapedChar(const char16* str, int* begin, int length, 284c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output) { 285c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // UTF-16 input. Readchar16 will handle invalid characters for us and give 286c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // us the kUnicodeReplacementCharacter, so we don't have to do special 287c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // checking after failure, just pass through the failure to the caller. 288c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned char_value; 289c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool success = ReadUTFChar(str, begin, length, &char_value); 290c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott AppendUTF8EscapedValue(char_value, output); 291c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return success; 292c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 293c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 294c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Handles UTF-8 input. See the wide version above for usage. 295c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool AppendUTF8EscapedChar(const char* str, int* begin, int length, 296c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output) { 297c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // ReadUTF8Char will handle invalid characters for us and give us the 298c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // kUnicodeReplacementCharacter, so we don't have to do special checking 299c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // after failure, just pass through the failure to the caller. 300c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned ch; 301c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool success = ReadUTFChar(str, begin, length, &ch); 302c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott AppendUTF8EscapedValue(ch, output); 303c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return success; 304c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 305c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 306c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Given a '%' character at |*begin| in the string |spec|, this will decode 307c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the escaped value and put it into |*unescaped_value| on success (returns 308c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// true). On failure, this will return false, and will not write into 309c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |*unescaped_value|. 310c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 311c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |*begin| will be updated to point to the last character of the escape 312c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// sequence so that when called with the index of a for loop, the next time 313c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// through it will point to the next character to be considered. On failure, 314c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |*begin| will be unchanged. 315c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool Is8BitChar(char c) { 316c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return true; // this case is specialized to avoid a warning 317c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 318c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool Is8BitChar(char16 c) { 319c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return c <= 255; 320c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 321c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 322c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<typename CHAR> 323c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline bool DecodeEscaped(const CHAR* spec, int* begin, int end, 324c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned char* unescaped_value) { 325c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (*begin + 3 > end || 326c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott !Is8BitChar(spec[*begin + 1]) || !Is8BitChar(spec[*begin + 2])) { 327c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Invalid escape sequence because there's not enough room, or the 328c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // digits are not ASCII. 329c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return false; 330c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 331c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 332c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned char first = static_cast<unsigned char>(spec[*begin + 1]); 333c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott unsigned char second = static_cast<unsigned char>(spec[*begin + 2]); 334c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (!IsHexChar(first) || !IsHexChar(second)) { 335c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Invalid hex digits, fail. 336c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return false; 337c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 338c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 339c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Valid escape sequence. 340c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott *unescaped_value = (HexCharToValue(first) << 4) + HexCharToValue(second); 341c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott *begin += 2; 342c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return true; 343c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 344c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 345c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Appends the given substring to the output, escaping "some" characters that 346c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// it feels may not be safe. It assumes the input values are all contained in 347c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 8-bit although it allows any type. 348c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 349c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This is used in error cases to append invalid output so that it looks 350c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// approximately correct. Non-error cases should not call this function since 351c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the escaping rules are not guaranteed! 352c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid AppendInvalidNarrowString(const char* spec, int begin, int end, 353c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 354c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid AppendInvalidNarrowString(const char16* spec, int begin, int end, 355c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 356c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 357c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Misc canonicalization helpers ---------------------------------------------- 358c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 359c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts between UTF-8 and UTF-16, returning true on successful conversion. 360c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The output will be appended to the given canonicalizer output (so make sure 361c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// it's empty if you want to replace). 362c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 363c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// On invalid input, this will still write as much output as possible, 364c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// replacing the invalid characters with the "invalid character". It will 365c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// return false in the failure case, and the caller should not continue as 366c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// normal. 367c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ConvertUTF16ToUTF8(const char16* input, int input_len, 368c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 369c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ConvertUTF8ToUTF16(const char* input, int input_len, 370c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutputT<char16>* output); 371c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 372c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts from UTF-16 to 8-bit using the character set converter. If the 373c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// converter is NULL, this will use UTF-8. 374c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid ConvertUTF16ToQueryEncoding(const char16* input, 375c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const url_parse::Component& query, 376c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CharsetConverter* converter, 377c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 378c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 379c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Applies the replacements to the given component source. The component source 380c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// should be pre-initialized to the "old" base. That is, all pointers will 381c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// point to the spec of the old URL, and all of the Parsed components will 382c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// be indices into that string. 383c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 384c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The pointers and components in the |source| for all non-NULL strings in the 385c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |repl| (replacements) will be updated to reference those strings. 386c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Canonicalizing with the new |source| and |parsed| can then combine URL 387c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// components from many different strings. 388c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid SetupOverrideComponents(const char* base, 389c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const Replacements<char>& repl, 390c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott URLComponentSource<char>* source, 391c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott url_parse::Parsed* parsed); 392c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 393c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Like the above 8-bit version, except that it additionally converts the 394c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// UTF-16 input to UTF-8 before doing the overrides. 395c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 396c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The given utf8_buffer is used to store the converted components. They will 397c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// be appended one after another, with the parsed structure identifying the 398c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// appropriate substrings. This buffer is a parameter because the source has 399c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// no storage, so the buffer must have the same lifetime as the source 400c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// parameter owned by the caller. 401c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 402c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// THE CALLER MUST NOT ADD TO THE |utf8_buffer| AFTER THIS CALL. Members of 403c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |source| will point into this buffer, which could be invalidated if 404c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// additional data is added and the CanonOutput resizes its buffer. 405c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 406c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true on success. Fales means that the input was not valid UTF-16, 407c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// although we will have still done the override with "invalid characters" in 408c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// place of errors. 409c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool SetupUTF16OverrideComponents(const char* base, 410c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const Replacements<char16>& repl, 411c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* utf8_buffer, 412c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott URLComponentSource<char>* source, 413c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott url_parse::Parsed* parsed); 414c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 415c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Implemented in url_canon_path.cc, these are required by the relative URL 416c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// resolver as well, so we declare them here. 417c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool CanonicalizePartialPath(const char* spec, 418c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const url_parse::Component& path, 419c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int path_begin_in_output, 420c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 421c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool CanonicalizePartialPath(const char16* spec, 422c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const url_parse::Component& path, 423c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int path_begin_in_output, 424c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott CanonOutput* output); 425c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 426c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef WIN32 427c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 428c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Implementations of Windows' int-to-string conversions 429c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint _itoa_s(int value, char* buffer, size_t size_in_chars, int radix); 430c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint _itow_s(int value, char16* buffer, size_t size_in_chars, int radix); 431c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 432c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Secure template overloads for these functions 433c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<size_t N> 434c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline int _itoa_s(int value, char (&buffer)[N], int radix) { 435c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return _itoa_s(value, buffer, N, radix); 436c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 437c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 438c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate<size_t N> 439c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline int _itow_s(int value, char16 (&buffer)[N], int radix) { 440c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return _itow_s(value, buffer, N, radix); 441c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 442c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 443c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// _strtoui64 and strtoull behave the same 444c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline unsigned long long _strtoui64(const char* nptr, 445c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott char** endptr, int base) { 446c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return strtoull(nptr, endptr, base); 447c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 448c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 449c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // WIN32 450c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 451c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace url_canon 452c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 453c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // GOOGLEURL_SRC_URL_CANON_INTERNAL_H__ 454