1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "url/url_canon_internal.h"
6 
7 #include <errno.h>
8 #include <stddef.h>
9 #include <stdlib.h>
10 
11 #include <cstdio>
12 #include <string>
13 
14 #include "base/strings/utf_string_conversion_utils.h"
15 
16 namespace url {
17 
18 namespace {
19 
20 template<typename CHAR, typename UCHAR>
DoAppendStringOfType(const CHAR * source,int length,SharedCharTypes type,CanonOutput * output)21 void DoAppendStringOfType(const CHAR* source, int length,
22                           SharedCharTypes type,
23                           CanonOutput* output) {
24   for (int i = 0; i < length; i++) {
25     if (static_cast<UCHAR>(source[i]) >= 0x80) {
26       // ReadChar will fill the code point with kUnicodeReplacementCharacter
27       // when the input is invalid, which is what we want.
28       unsigned code_point;
29       ReadUTFChar(source, &i, length, &code_point);
30       AppendUTF8EscapedValue(code_point, output);
31     } else {
32       // Just append the 7-bit character, possibly escaping it.
33       unsigned char uch = static_cast<unsigned char>(source[i]);
34       if (!IsCharOfType(uch, type))
35         AppendEscapedChar(uch, output);
36       else
37         output->push_back(uch);
38     }
39   }
40 }
41 
42 // This function assumes the input values are all contained in 8-bit,
43 // although it allows any type. Returns true if input is valid, false if not.
44 template<typename CHAR, typename UCHAR>
DoAppendInvalidNarrowString(const CHAR * spec,int begin,int end,CanonOutput * output)45 void DoAppendInvalidNarrowString(const CHAR* spec, int begin, int end,
46                                  CanonOutput* output) {
47   for (int i = begin; i < end; i++) {
48     UCHAR uch = static_cast<UCHAR>(spec[i]);
49     if (uch >= 0x80) {
50       // Handle UTF-8/16 encodings. This call will correctly handle the error
51       // case by appending the invalid character.
52       AppendUTF8EscapedChar(spec, &i, end, output);
53     } else if (uch <= ' ' || uch == 0x7f) {
54       // This function is for error handling, so we escape all control
55       // characters and spaces, but not anything else since we lack
56       // context to do something more specific.
57       AppendEscapedChar(static_cast<unsigned char>(uch), output);
58     } else {
59       output->push_back(static_cast<char>(uch));
60     }
61   }
62 }
63 
64 // Overrides one component, see the Replacements structure for
65 // what the various combionations of source pointer and component mean.
DoOverrideComponent(const char * override_source,const Component & override_component,const char ** dest,Component * dest_component)66 void DoOverrideComponent(const char* override_source,
67                          const Component& override_component,
68                          const char** dest,
69                          Component* dest_component) {
70   if (override_source) {
71     *dest = override_source;
72     *dest_component = override_component;
73   }
74 }
75 
76 // Similar to DoOverrideComponent except that it takes a UTF-16 input and does
77 // not actually set the output character pointer.
78 //
79 // The input is converted to UTF-8 at the end of the given buffer as a temporary
80 // holding place. The component identifying the portion of the buffer used in
81 // the |utf8_buffer| will be specified in |*dest_component|.
82 //
83 // This will not actually set any |dest| pointer like DoOverrideComponent
84 // does because all of the pointers will point into the |utf8_buffer|, which
85 // may get resized while we're overriding a subsequent component. Instead, the
86 // caller should use the beginning of the |utf8_buffer| as the string pointer
87 // for all components once all overrides have been prepared.
PrepareUTF16OverrideComponent(const base::char16 * override_source,const Component & override_component,CanonOutput * utf8_buffer,Component * dest_component)88 bool PrepareUTF16OverrideComponent(const base::char16* override_source,
89                                    const Component& override_component,
90                                    CanonOutput* utf8_buffer,
91                                    Component* dest_component) {
92   bool success = true;
93   if (override_source) {
94     if (!override_component.is_valid()) {
95       // Non-"valid" component (means delete), so we need to preserve that.
96       *dest_component = Component();
97     } else {
98       // Convert to UTF-8.
99       dest_component->begin = utf8_buffer->length();
100       success = ConvertUTF16ToUTF8(&override_source[override_component.begin],
101                                    override_component.len, utf8_buffer);
102       dest_component->len = utf8_buffer->length() - dest_component->begin;
103     }
104   }
105   return success;
106 }
107 
108 }  // namespace
109 
110 // See the header file for this array's declaration.
111 const unsigned char kSharedCharTypeTable[0x100] = {
112     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x00 - 0x0f
113     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x10 - 0x1f
114     0,                           // 0x20  ' ' (escape spaces in queries)
115     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x21  !
116     0,                           // 0x22  "
117     0,                           // 0x23  #  (invalid in query since it marks the ref)
118     CHAR_QUERY | CHAR_USERINFO,  // 0x24  $
119     CHAR_QUERY | CHAR_USERINFO,  // 0x25  %
120     CHAR_QUERY | CHAR_USERINFO,  // 0x26  &
121     0,                           // 0x27  '  (Try to prevent XSS.)
122     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x28  (
123     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x29  )
124     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x2a  *
125     CHAR_QUERY | CHAR_USERINFO,  // 0x2b  +
126     CHAR_QUERY | CHAR_USERINFO,  // 0x2c  ,
127     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x2d  -
128     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT,  // 0x2e  .
129     CHAR_QUERY,                  // 0x2f  /
130     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x30  0
131     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x31  1
132     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x32  2
133     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x33  3
134     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x34  4
135     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x35  5
136     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x36  6
137     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,  // 0x37  7
138     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_COMPONENT,             // 0x38  8
139     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_COMPONENT,             // 0x39  9
140     CHAR_QUERY,  // 0x3a  :
141     CHAR_QUERY,  // 0x3b  ;
142     0,           // 0x3c  <  (Try to prevent certain types of XSS.)
143     CHAR_QUERY,  // 0x3d  =
144     0,           // 0x3e  >  (Try to prevent certain types of XSS.)
145     CHAR_QUERY,  // 0x3f  ?
146     CHAR_QUERY,  // 0x40  @
147     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x41  A
148     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x42  B
149     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x43  C
150     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x44  D
151     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x45  E
152     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x46  F
153     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x47  G
154     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x48  H
155     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x49  I
156     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x4a  J
157     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x4b  K
158     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x4c  L
159     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x4d  M
160     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x4e  N
161     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x4f  O
162     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x50  P
163     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x51  Q
164     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x52  R
165     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x53  S
166     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x54  T
167     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x55  U
168     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x56  V
169     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x57  W
170     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT, // 0x58  X
171     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x59  Y
172     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x5a  Z
173     CHAR_QUERY,  // 0x5b  [
174     CHAR_QUERY,  // 0x5c  '\'
175     CHAR_QUERY,  // 0x5d  ]
176     CHAR_QUERY,  // 0x5e  ^
177     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x5f  _
178     CHAR_QUERY,  // 0x60  `
179     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x61  a
180     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x62  b
181     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x63  c
182     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x64  d
183     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x65  e
184     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,  // 0x66  f
185     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x67  g
186     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x68  h
187     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x69  i
188     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x6a  j
189     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x6b  k
190     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x6c  l
191     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x6d  m
192     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x6e  n
193     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x6f  o
194     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x70  p
195     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x71  q
196     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x72  r
197     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x73  s
198     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x74  t
199     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x75  u
200     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x76  v
201     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x77  w
202     CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT,  // 0x78  x
203     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x79  y
204     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x7a  z
205     CHAR_QUERY,  // 0x7b  {
206     CHAR_QUERY,  // 0x7c  |
207     CHAR_QUERY,  // 0x7d  }
208     CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,  // 0x7e  ~
209     0,           // 0x7f
210     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x80 - 0x8f
211     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0x90 - 0x9f
212     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xa0 - 0xaf
213     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xb0 - 0xbf
214     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xc0 - 0xcf
215     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xd0 - 0xdf
216     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xe0 - 0xef
217     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 0xf0 - 0xff
218 };
219 
220 const char kHexCharLookup[0x10] = {
221     '0', '1', '2', '3', '4', '5', '6', '7',
222     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
223 };
224 
225 const char kCharToHexLookup[8] = {
226     0,         // 0x00 - 0x1f
227     '0',       // 0x20 - 0x3f: digits 0 - 9 are 0x30 - 0x39
228     'A' - 10,  // 0x40 - 0x5f: letters A - F are 0x41 - 0x46
229     'a' - 10,  // 0x60 - 0x7f: letters a - f are 0x61 - 0x66
230     0,         // 0x80 - 0x9F
231     0,         // 0xA0 - 0xBF
232     0,         // 0xC0 - 0xDF
233     0,         // 0xE0 - 0xFF
234 };
235 
236 const base::char16 kUnicodeReplacementCharacter = 0xfffd;
237 
AppendStringOfType(const char * source,int length,SharedCharTypes type,CanonOutput * output)238 void AppendStringOfType(const char* source, int length,
239                         SharedCharTypes type,
240                         CanonOutput* output) {
241   DoAppendStringOfType<char, unsigned char>(source, length, type, output);
242 }
243 
AppendStringOfType(const base::char16 * source,int length,SharedCharTypes type,CanonOutput * output)244 void AppendStringOfType(const base::char16* source, int length,
245                         SharedCharTypes type,
246                         CanonOutput* output) {
247   DoAppendStringOfType<base::char16, base::char16>(
248       source, length, type, output);
249 }
250 
ReadUTFChar(const char * str,int * begin,int length,unsigned * code_point_out)251 bool ReadUTFChar(const char* str, int* begin, int length,
252                  unsigned* code_point_out) {
253   // This depends on ints and int32s being the same thing. If they're not, it
254   // will fail to compile.
255   // TODO(mmenke): This should probably be fixed.
256   if (!base::ReadUnicodeCharacter(str, length, begin, code_point_out) ||
257       !base::IsValidCharacter(*code_point_out)) {
258     *code_point_out = kUnicodeReplacementCharacter;
259     return false;
260   }
261   return true;
262 }
263 
ReadUTFChar(const base::char16 * str,int * begin,int length,unsigned * code_point_out)264 bool ReadUTFChar(const base::char16* str, int* begin, int length,
265                  unsigned* code_point_out) {
266   // This depends on ints and int32s being the same thing. If they're not, it
267   // will fail to compile.
268   // TODO(mmenke): This should probably be fixed.
269   if (!base::ReadUnicodeCharacter(str, length, begin, code_point_out) ||
270       !base::IsValidCharacter(*code_point_out)) {
271     *code_point_out = kUnicodeReplacementCharacter;
272     return false;
273   }
274   return true;
275 }
276 
AppendInvalidNarrowString(const char * spec,int begin,int end,CanonOutput * output)277 void AppendInvalidNarrowString(const char* spec, int begin, int end,
278                                CanonOutput* output) {
279   DoAppendInvalidNarrowString<char, unsigned char>(spec, begin, end, output);
280 }
281 
AppendInvalidNarrowString(const base::char16 * spec,int begin,int end,CanonOutput * output)282 void AppendInvalidNarrowString(const base::char16* spec, int begin, int end,
283                                CanonOutput* output) {
284   DoAppendInvalidNarrowString<base::char16, base::char16>(
285       spec, begin, end, output);
286 }
287 
ConvertUTF16ToUTF8(const base::char16 * input,int input_len,CanonOutput * output)288 bool ConvertUTF16ToUTF8(const base::char16* input, int input_len,
289                         CanonOutput* output) {
290   bool success = true;
291   for (int i = 0; i < input_len; i++) {
292     unsigned code_point;
293     success &= ReadUTFChar(input, &i, input_len, &code_point);
294     AppendUTF8Value(code_point, output);
295   }
296   return success;
297 }
298 
ConvertUTF8ToUTF16(const char * input,int input_len,CanonOutputT<base::char16> * output)299 bool ConvertUTF8ToUTF16(const char* input, int input_len,
300                         CanonOutputT<base::char16>* output) {
301   bool success = true;
302   for (int i = 0; i < input_len; i++) {
303     unsigned code_point;
304     success &= ReadUTFChar(input, &i, input_len, &code_point);
305     AppendUTF16Value(code_point, output);
306   }
307   return success;
308 }
309 
SetupOverrideComponents(const char * base,const Replacements<char> & repl,URLComponentSource<char> * source,Parsed * parsed)310 void SetupOverrideComponents(const char* base,
311                              const Replacements<char>& repl,
312                              URLComponentSource<char>* source,
313                              Parsed* parsed) {
314   // Get the source and parsed structures of the things we are replacing.
315   const URLComponentSource<char>& repl_source = repl.sources();
316   const Parsed& repl_parsed = repl.components();
317 
318   DoOverrideComponent(repl_source.scheme, repl_parsed.scheme,
319                       &source->scheme, &parsed->scheme);
320   DoOverrideComponent(repl_source.username, repl_parsed.username,
321                       &source->username, &parsed->username);
322   DoOverrideComponent(repl_source.password, repl_parsed.password,
323                       &source->password, &parsed->password);
324 
325   // Our host should be empty if not present, so override the default setup.
326   DoOverrideComponent(repl_source.host, repl_parsed.host,
327                       &source->host, &parsed->host);
328   if (parsed->host.len == -1)
329     parsed->host.len = 0;
330 
331   DoOverrideComponent(repl_source.port, repl_parsed.port,
332                       &source->port, &parsed->port);
333   DoOverrideComponent(repl_source.path, repl_parsed.path,
334                       &source->path, &parsed->path);
335   DoOverrideComponent(repl_source.query, repl_parsed.query,
336                       &source->query, &parsed->query);
337   DoOverrideComponent(repl_source.ref, repl_parsed.ref,
338                       &source->ref, &parsed->ref);
339 }
340 
SetupUTF16OverrideComponents(const char * base,const Replacements<base::char16> & repl,CanonOutput * utf8_buffer,URLComponentSource<char> * source,Parsed * parsed)341 bool SetupUTF16OverrideComponents(const char* base,
342                                   const Replacements<base::char16>& repl,
343                                   CanonOutput* utf8_buffer,
344                                   URLComponentSource<char>* source,
345                                   Parsed* parsed) {
346   bool success = true;
347 
348   // Get the source and parsed structures of the things we are replacing.
349   const URLComponentSource<base::char16>& repl_source = repl.sources();
350   const Parsed& repl_parsed = repl.components();
351 
352   success &= PrepareUTF16OverrideComponent(
353       repl_source.scheme, repl_parsed.scheme,
354       utf8_buffer, &parsed->scheme);
355   success &= PrepareUTF16OverrideComponent(
356       repl_source.username, repl_parsed.username,
357       utf8_buffer, &parsed->username);
358   success &= PrepareUTF16OverrideComponent(
359       repl_source.password, repl_parsed.password,
360       utf8_buffer, &parsed->password);
361   success &= PrepareUTF16OverrideComponent(
362       repl_source.host, repl_parsed.host,
363       utf8_buffer, &parsed->host);
364   success &= PrepareUTF16OverrideComponent(
365       repl_source.port, repl_parsed.port,
366       utf8_buffer, &parsed->port);
367   success &= PrepareUTF16OverrideComponent(
368       repl_source.path, repl_parsed.path,
369       utf8_buffer, &parsed->path);
370   success &= PrepareUTF16OverrideComponent(
371       repl_source.query, repl_parsed.query,
372       utf8_buffer, &parsed->query);
373   success &= PrepareUTF16OverrideComponent(
374       repl_source.ref, repl_parsed.ref,
375       utf8_buffer, &parsed->ref);
376 
377   // PrepareUTF16OverrideComponent will not have set the data pointer since the
378   // buffer could be resized, invalidating the pointers. We set the data
379   // pointers for affected components now that the buffer is finalized.
380   if (repl_source.scheme)   source->scheme = utf8_buffer->data();
381   if (repl_source.username) source->username = utf8_buffer->data();
382   if (repl_source.password) source->password = utf8_buffer->data();
383   if (repl_source.host)     source->host = utf8_buffer->data();
384   if (repl_source.port)     source->port = utf8_buffer->data();
385   if (repl_source.path)     source->path = utf8_buffer->data();
386   if (repl_source.query)    source->query = utf8_buffer->data();
387   if (repl_source.ref)      source->ref = utf8_buffer->data();
388 
389   return success;
390 }
391 
392 #ifndef WIN32
393 
_itoa_s(int value,char * buffer,size_t size_in_chars,int radix)394 int _itoa_s(int value, char* buffer, size_t size_in_chars, int radix) {
395   const char* format_str;
396   if (radix == 10)
397     format_str = "%d";
398   else if (radix == 16)
399     format_str = "%x";
400   else
401     return EINVAL;
402 
403   int written = snprintf(buffer, size_in_chars, format_str, value);
404   if (static_cast<size_t>(written) >= size_in_chars) {
405     // Output was truncated, or written was negative.
406     return EINVAL;
407   }
408   return 0;
409 }
410 
_itow_s(int value,base::char16 * buffer,size_t size_in_chars,int radix)411 int _itow_s(int value, base::char16* buffer, size_t size_in_chars, int radix) {
412   if (radix != 10)
413     return EINVAL;
414 
415   // No more than 12 characters will be required for a 32-bit integer.
416   // Add an extra byte for the terminating null.
417   char temp[13];
418   int written = snprintf(temp, sizeof(temp), "%d", value);
419   if (static_cast<size_t>(written) >= size_in_chars) {
420     // Output was truncated, or written was negative.
421     return EINVAL;
422   }
423 
424   for (int i = 0; i < written; ++i) {
425     buffer[i] = static_cast<base::char16>(temp[i]);
426   }
427   buffer[written] = '\0';
428   return 0;
429 }
430 
431 #endif  // !WIN32
432 
433 }  // namespace url
434