• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/350788890): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "url/url_canon_ip.h"
11 
12 #include <stdint.h>
13 #include <stdlib.h>
14 
15 #include <limits>
16 
17 #include "base/check.h"
18 #include "url/url_canon_internal.h"
19 #include "url/url_features.h"
20 
21 namespace url {
22 
23 namespace {
24 
25 // Converts one of the character types that represent a numerical base to the
26 // corresponding base.
BaseForType(SharedCharTypes type)27 int BaseForType(SharedCharTypes type) {
28   switch (type) {
29     case CHAR_HEX:
30       return 16;
31     case CHAR_DEC:
32       return 10;
33     case CHAR_OCT:
34       return 8;
35     default:
36       return 0;
37   }
38 }
39 
40 // Converts an IPv4 component to a 32-bit number, while checking for overflow.
41 //
42 // Possible return values:
43 // - IPV4    - The number was valid, and did not overflow.
44 // - BROKEN  - The input was numeric, but too large for a 32-bit field.
45 // - NEUTRAL - Input was not numeric.
46 //
47 // The input is assumed to be ASCII. The components are assumed to be non-empty.
48 template<typename CHAR>
IPv4ComponentToNumber(const CHAR * spec,const Component & component,uint32_t * number)49 CanonHostInfo::Family IPv4ComponentToNumber(const CHAR* spec,
50                                             const Component& component,
51                                             uint32_t* number) {
52   // Empty components are considered non-numeric.
53   if (component.is_empty())
54     return CanonHostInfo::NEUTRAL;
55 
56   // Figure out the base
57   SharedCharTypes base;
58   int base_prefix_len = 0;  // Size of the prefix for this base.
59   if (spec[component.begin] == '0') {
60     // Either hex or dec, or a standalone zero.
61     if (component.len == 1) {
62       base = CHAR_DEC;
63     } else if (spec[component.begin + 1] == 'X' ||
64                spec[component.begin + 1] == 'x') {
65       base = CHAR_HEX;
66       base_prefix_len = 2;
67     } else {
68       base = CHAR_OCT;
69       base_prefix_len = 1;
70     }
71   } else {
72     base = CHAR_DEC;
73   }
74 
75   // Extend the prefix to consume all leading zeros.
76   while (base_prefix_len < component.len &&
77          spec[component.begin + base_prefix_len] == '0')
78     base_prefix_len++;
79 
80   // Put the component, minus any base prefix, into a NULL-terminated buffer so
81   // we can call the standard library. Because leading zeros have already been
82   // discarded, filling the entire buffer is guaranteed to trigger the 32-bit
83   // overflow check.
84   const int kMaxComponentLen = 16;
85   char buf[kMaxComponentLen + 1];  // digits + '\0'
86   int dest_i = 0;
87   bool may_be_broken_octal = false;
88   for (int i = component.begin + base_prefix_len; i < component.end(); i++) {
89     if (spec[i] >= 0x80)
90       return CanonHostInfo::NEUTRAL;
91 
92     // We know the input is 7-bit, so convert to narrow (if this is the wide
93     // version of the template) by casting.
94     char input = static_cast<char>(spec[i]);
95 
96     // Validate that this character is OK for the given base.
97     if (!IsCharOfType(input, base)) {
98       if (IsCharOfType(input, CHAR_DEC)) {
99         // Entirely numeric components with leading 0s that aren't octal are
100         // considered broken.
101         may_be_broken_octal = true;
102       } else {
103         return CanonHostInfo::NEUTRAL;
104       }
105     }
106 
107     // Fill the buffer, if there's space remaining. This check allows us to
108     // verify that all characters are numeric, even those that don't fit.
109     if (dest_i < kMaxComponentLen)
110       buf[dest_i++] = input;
111   }
112 
113   if (may_be_broken_octal)
114     return CanonHostInfo::BROKEN;
115 
116   buf[dest_i] = '\0';
117 
118   // Use the 64-bit strtoi so we get a big number (no hex, decimal, or octal
119   // number can overflow a 64-bit number in <= 16 characters).
120   uint64_t num = _strtoui64(buf, NULL, BaseForType(base));
121 
122   // Check for 32-bit overflow.
123   if (num > std::numeric_limits<uint32_t>::max())
124     return CanonHostInfo::BROKEN;
125 
126   // No overflow. Success!
127   *number = static_cast<uint32_t>(num);
128   return CanonHostInfo::IPV4;
129 }
130 
131 // See declaration of IPv4AddressToNumber for documentation.
132 template <typename CHAR, typename UCHAR>
DoIPv4AddressToNumber(const CHAR * spec,Component host,unsigned char address[4],int * num_ipv4_components)133 CanonHostInfo::Family DoIPv4AddressToNumber(const CHAR* spec,
134                                             Component host,
135                                             unsigned char address[4],
136                                             int* num_ipv4_components) {
137   // Ignore terminal dot, if present.
138   if (host.is_nonempty() && spec[host.end() - 1] == '.')
139     --host.len;
140 
141   // Do nothing if empty.
142   if (host.is_empty())
143     return CanonHostInfo::NEUTRAL;
144 
145   // Read component values.  The first `existing_components` of them are
146   // populated front to back, with the first one corresponding to the last
147   // component, which allows for early exit if the last component isn't a
148   // number.
149   uint32_t component_values[4];
150   int existing_components = 0;
151 
152   int current_component_end = host.end();
153   int current_position = current_component_end;
154   while (true) {
155     // If this is not the first character of a component, go to the next
156     // component.
157     if (current_position != host.begin && spec[current_position - 1] != '.') {
158       --current_position;
159       continue;
160     }
161 
162     CanonHostInfo::Family family = IPv4ComponentToNumber(
163         spec,
164         Component(current_position, current_component_end - current_position),
165         &component_values[existing_components]);
166 
167     // If `family` is NEUTRAL and this is the last component, return NEUTRAL. If
168     // `family` is NEUTRAL but not the last component, this is considered a
169     // BROKEN IPv4 address, as opposed to a non-IPv4 hostname.
170     if (family == CanonHostInfo::NEUTRAL && existing_components == 0)
171       return CanonHostInfo::NEUTRAL;
172 
173     if (family != CanonHostInfo::IPV4)
174       return CanonHostInfo::BROKEN;
175 
176     ++existing_components;
177 
178     // If this is the final component, nothing else to do.
179     if (current_position == host.begin)
180       break;
181 
182     // If there are more than 4 components, fail.
183     if (existing_components == 4)
184       return CanonHostInfo::BROKEN;
185 
186     current_component_end = current_position - 1;
187     --current_position;
188   }
189 
190   // Use `component_values` to fill out the 4-component IP address.
191 
192   // First, process all components but the last, while making sure each fits
193   // within an 8-bit field.
194   for (int i = existing_components - 1; i > 0; i--) {
195     if (component_values[i] > std::numeric_limits<uint8_t>::max())
196       return CanonHostInfo::BROKEN;
197     address[existing_components - i - 1] =
198         static_cast<unsigned char>(component_values[i]);
199   }
200 
201   uint32_t last_value = component_values[0];
202   for (int i = 3; i >= existing_components - 1; i--) {
203     address[i] = static_cast<unsigned char>(last_value);
204     last_value >>= 8;
205   }
206 
207   // If the last component has residual bits, report overflow.
208   if (last_value != 0)
209     return CanonHostInfo::BROKEN;
210 
211   // Tell the caller how many components we saw.
212   *num_ipv4_components = existing_components;
213 
214   // Success!
215   return CanonHostInfo::IPV4;
216 }
217 
218 // Return true if we've made a final IPV4/BROKEN decision, false if the result
219 // is NEUTRAL, and we could use a second opinion.
220 template<typename CHAR, typename UCHAR>
DoCanonicalizeIPv4Address(const CHAR * spec,const Component & host,CanonOutput * output,CanonHostInfo * host_info)221 bool DoCanonicalizeIPv4Address(const CHAR* spec,
222                                const Component& host,
223                                CanonOutput* output,
224                                CanonHostInfo* host_info) {
225   host_info->family = IPv4AddressToNumber(
226       spec, host, host_info->address, &host_info->num_ipv4_components);
227 
228   switch (host_info->family) {
229     case CanonHostInfo::IPV4:
230       // Definitely an IPv4 address.
231       host_info->out_host.begin = output->length();
232       AppendIPv4Address(host_info->address, output);
233       host_info->out_host.len = output->length() - host_info->out_host.begin;
234       return true;
235     case CanonHostInfo::BROKEN:
236       // Definitely broken.
237       return true;
238     default:
239       // Could be IPv6 or a hostname.
240       return false;
241   }
242 }
243 
244 // Helper class that describes the main components of an IPv6 input string.
245 // See the following examples to understand how it breaks up an input string:
246 //
247 // [Example 1]: input = "[::aa:bb]"
248 //  ==> num_hex_components = 2
249 //  ==> hex_components[0] = Component(3,2) "aa"
250 //  ==> hex_components[1] = Component(6,2) "bb"
251 //  ==> index_of_contraction = 0
252 //  ==> ipv4_component = Component(0, -1)
253 //
254 // [Example 2]: input = "[1:2::3:4:5]"
255 //  ==> num_hex_components = 5
256 //  ==> hex_components[0] = Component(1,1) "1"
257 //  ==> hex_components[1] = Component(3,1) "2"
258 //  ==> hex_components[2] = Component(6,1) "3"
259 //  ==> hex_components[3] = Component(8,1) "4"
260 //  ==> hex_components[4] = Component(10,1) "5"
261 //  ==> index_of_contraction = 2
262 //  ==> ipv4_component = Component(0, -1)
263 //
264 // [Example 3]: input = "[::ffff:192.168.0.1]"
265 //  ==> num_hex_components = 1
266 //  ==> hex_components[0] = Component(3,4) "ffff"
267 //  ==> index_of_contraction = 0
268 //  ==> ipv4_component = Component(8, 11) "192.168.0.1"
269 //
270 // [Example 4]: input = "[1::]"
271 //  ==> num_hex_components = 1
272 //  ==> hex_components[0] = Component(1,1) "1"
273 //  ==> index_of_contraction = 1
274 //  ==> ipv4_component = Component(0, -1)
275 //
276 // [Example 5]: input = "[::192.168.0.1]"
277 //  ==> num_hex_components = 0
278 //  ==> index_of_contraction = 0
279 //  ==> ipv4_component = Component(8, 11) "192.168.0.1"
280 //
281 struct IPv6Parsed {
282   // Zero-out the parse information.
reseturl::__anon2b11fe760111::IPv6Parsed283   void reset() {
284     num_hex_components = 0;
285     index_of_contraction = -1;
286     ipv4_component.reset();
287   }
288 
289   // There can be up to 8 hex components (colon separated) in the literal.
290   Component hex_components[8];
291 
292   // The count of hex components present. Ranges from [0,8].
293   int num_hex_components;
294 
295   // The index of the hex component that the "::" contraction precedes, or
296   // -1 if there is no contraction.
297   int index_of_contraction;
298 
299   // The range of characters which are an IPv4 literal.
300   Component ipv4_component;
301 };
302 
303 // Parse the IPv6 input string. If parsing succeeded returns true and fills
304 // |parsed| with the information. If parsing failed (because the input is
305 // invalid) returns false.
306 template<typename CHAR, typename UCHAR>
DoParseIPv6(const CHAR * spec,const Component & host,IPv6Parsed * parsed)307 bool DoParseIPv6(const CHAR* spec, const Component& host, IPv6Parsed* parsed) {
308   // Zero-out the info.
309   parsed->reset();
310 
311   if (host.is_empty())
312     return false;
313 
314   // The index for start and end of address range (no brackets).
315   int begin = host.begin;
316   int end = host.end();
317 
318   int cur_component_begin = begin;  // Start of the current component.
319 
320   // Scan through the input, searching for hex components, "::" contractions,
321   // and IPv4 components.
322   for (int i = begin; /* i <= end */; i++) {
323     bool is_colon = spec[i] == ':';
324     bool is_contraction = is_colon && i < end - 1 && spec[i + 1] == ':';
325 
326     // We reached the end of the current component if we encounter a colon
327     // (separator between hex components, or start of a contraction), or end of
328     // input.
329     if (is_colon || i == end) {
330       int component_len = i - cur_component_begin;
331 
332       // A component should not have more than 4 hex digits.
333       if (component_len > 4)
334         return false;
335 
336       // Don't allow empty components.
337       if (component_len == 0) {
338         // The exception is when contractions appear at beginning of the
339         // input or at the end of the input.
340         if (!((is_contraction && i == begin) || (i == end &&
341             parsed->index_of_contraction == parsed->num_hex_components)))
342           return false;
343       }
344 
345       // Add the hex component we just found to running list.
346       if (component_len > 0) {
347         // Can't have more than 8 components!
348         if (parsed->num_hex_components >= 8)
349           return false;
350 
351         parsed->hex_components[parsed->num_hex_components++] =
352             Component(cur_component_begin, component_len);
353       }
354     }
355 
356     if (i == end)
357       break;  // Reached the end of the input, DONE.
358 
359     // We found a "::" contraction.
360     if (is_contraction) {
361       // There can be at most one contraction in the literal.
362       if (parsed->index_of_contraction != -1)
363         return false;
364       parsed->index_of_contraction = parsed->num_hex_components;
365       ++i;  // Consume the colon we peeked.
366     }
367 
368     if (is_colon) {
369       // Colons are separators between components, keep track of where the
370       // current component started (after this colon).
371       cur_component_begin = i + 1;
372     } else {
373       if (static_cast<UCHAR>(spec[i]) >= 0x80)
374         return false;  // Not ASCII.
375 
376       if (!IsHexChar(static_cast<unsigned char>(spec[i]))) {
377         // Regular components are hex numbers. It is also possible for
378         // a component to be an IPv4 address in dotted form.
379         if (IsIPv4Char(static_cast<unsigned char>(spec[i]))) {
380           // Since IPv4 address can only appear at the end, assume the rest
381           // of the string is an IPv4 address. (We will parse this separately
382           // later).
383           parsed->ipv4_component =
384               Component(cur_component_begin, end - cur_component_begin);
385           break;
386         } else {
387           // The character was neither a hex digit, nor an IPv4 character.
388           return false;
389         }
390       }
391     }
392   }
393 
394   return true;
395 }
396 
397 // Verifies the parsed IPv6 information, checking that the various components
398 // add up to the right number of bits (hex components are 16 bits, while
399 // embedded IPv4 formats are 32 bits, and contractions are placeholdes for
400 // 16 or more bits). Returns true if sizes match up, false otherwise. On
401 // success writes the length of the contraction (if any) to
402 // |out_num_bytes_of_contraction|.
CheckIPv6ComponentsSize(const IPv6Parsed & parsed,int * out_num_bytes_of_contraction)403 bool CheckIPv6ComponentsSize(const IPv6Parsed& parsed,
404                              int* out_num_bytes_of_contraction) {
405   // Each group of four hex digits contributes 16 bits.
406   int num_bytes_without_contraction = parsed.num_hex_components * 2;
407 
408   // If an IPv4 address was embedded at the end, it contributes 32 bits.
409   if (parsed.ipv4_component.is_valid())
410     num_bytes_without_contraction += 4;
411 
412   // If there was a "::" contraction, its size is going to be:
413   // MAX([16bits], [128bits] - num_bytes_without_contraction).
414   int num_bytes_of_contraction = 0;
415   if (parsed.index_of_contraction != -1) {
416     num_bytes_of_contraction = 16 - num_bytes_without_contraction;
417     if (num_bytes_of_contraction < 2)
418       num_bytes_of_contraction = 2;
419   }
420 
421   // Check that the numbers add up.
422   if (num_bytes_without_contraction + num_bytes_of_contraction != 16)
423     return false;
424 
425   *out_num_bytes_of_contraction = num_bytes_of_contraction;
426   return true;
427 }
428 
429 // Converts a hex component into a number. This cannot fail since the caller has
430 // already verified that each character in the string was a hex digit, and
431 // that there were no more than 4 characters.
432 template <typename CHAR>
IPv6HexComponentToNumber(const CHAR * spec,const Component & component)433 uint16_t IPv6HexComponentToNumber(const CHAR* spec,
434                                   const Component& component) {
435   DCHECK(component.len <= 4);
436 
437   // Copy the hex string into a C-string.
438   char buf[5];
439   for (int i = 0; i < component.len; ++i)
440     buf[i] = static_cast<char>(spec[component.begin + i]);
441   buf[component.len] = '\0';
442 
443   // Convert it to a number (overflow is not possible, since with 4 hex
444   // characters we can at most have a 16 bit number).
445   return static_cast<uint16_t>(_strtoui64(buf, NULL, 16));
446 }
447 
448 // Converts an IPv6 address to a 128-bit number (network byte order), returning
449 // true on success. False means that the input was not a valid IPv6 address.
450 template<typename CHAR, typename UCHAR>
DoIPv6AddressToNumber(const CHAR * spec,const Component & host,unsigned char address[16])451 bool DoIPv6AddressToNumber(const CHAR* spec,
452                            const Component& host,
453                            unsigned char address[16]) {
454   // Make sure the component is bounded by '[' and ']'.
455   int end = host.end();
456   if (host.is_empty() || spec[host.begin] != '[' || spec[end - 1] != ']')
457     return false;
458 
459   // Exclude the square brackets.
460   Component ipv6_comp(host.begin + 1, host.len - 2);
461 
462   // Parse the IPv6 address -- identify where all the colon separated hex
463   // components are, the "::" contraction, and the embedded IPv4 address.
464   IPv6Parsed ipv6_parsed;
465   if (!DoParseIPv6<CHAR, UCHAR>(spec, ipv6_comp, &ipv6_parsed))
466     return false;
467 
468   // Do some basic size checks to make sure that the address doesn't
469   // specify more than 128 bits or fewer than 128 bits. This also resolves
470   // how may zero bytes the "::" contraction represents.
471   int num_bytes_of_contraction;
472   if (!CheckIPv6ComponentsSize(ipv6_parsed, &num_bytes_of_contraction))
473     return false;
474 
475   int cur_index_in_address = 0;
476 
477   // Loop through each hex components, and contraction in order.
478   for (int i = 0; i <= ipv6_parsed.num_hex_components; ++i) {
479     // Append the contraction if it appears before this component.
480     if (i == ipv6_parsed.index_of_contraction) {
481       for (int j = 0; j < num_bytes_of_contraction; ++j)
482         address[cur_index_in_address++] = 0;
483     }
484     // Append the hex component's value.
485     if (i != ipv6_parsed.num_hex_components) {
486       // Get the 16-bit value for this hex component.
487       uint16_t number = IPv6HexComponentToNumber<CHAR>(
488           spec, ipv6_parsed.hex_components[i]);
489       // Append to |address|, in network byte order.
490       address[cur_index_in_address++] = (number & 0xFF00) >> 8;
491       address[cur_index_in_address++] = (number & 0x00FF);
492     }
493   }
494 
495   // If there was an IPv4 section, convert it into a 32-bit number and append
496   // it to |address|.
497   if (ipv6_parsed.ipv4_component.is_valid()) {
498     // Append the 32-bit number to |address|.
499     int num_ipv4_components = 0;
500     // IPv4AddressToNumber will remove the trailing dot from the component.
501     bool trailing_dot = ipv6_parsed.ipv4_component.is_nonempty() &&
502                         spec[ipv6_parsed.ipv4_component.end() - 1] == '.';
503     // The URL standard requires the embedded IPv4 address to be concisely
504     // composed of 4 parts and disallows terminal dots.
505     // See https://url.spec.whatwg.org/#concept-ipv6-parser
506     if (CanonHostInfo::IPV4 !=
507             IPv4AddressToNumber(spec, ipv6_parsed.ipv4_component,
508                                 &address[cur_index_in_address],
509                                 &num_ipv4_components)) {
510       return false;
511     }
512     if ((num_ipv4_components != 4 || trailing_dot)) {
513       return false;
514     }
515   }
516 
517   return true;
518 }
519 
520 // Searches for the longest sequence of zeros in |address|, and writes the
521 // range into |contraction_range|. The run of zeros must be at least 16 bits,
522 // and if there is a tie the first is chosen.
ChooseIPv6ContractionRange(const unsigned char address[16],Component * contraction_range)523 void ChooseIPv6ContractionRange(const unsigned char address[16],
524                                 Component* contraction_range) {
525   // The longest run of zeros in |address| seen so far.
526   Component max_range;
527 
528   // The current run of zeros in |address| being iterated over.
529   Component cur_range;
530 
531   for (int i = 0; i < 16; i += 2) {
532     // Test for 16 bits worth of zero.
533     bool is_zero = (address[i] == 0 && address[i + 1] == 0);
534 
535     if (is_zero) {
536       // Add the zero to the current range (or start a new one).
537       if (!cur_range.is_valid())
538         cur_range = Component(i, 0);
539       cur_range.len += 2;
540     }
541 
542     if (!is_zero || i == 14) {
543       // Just completed a run of zeros. If the run is greater than 16 bits,
544       // it is a candidate for the contraction.
545       if (cur_range.len > 2 && cur_range.len > max_range.len) {
546         max_range = cur_range;
547       }
548       cur_range.reset();
549     }
550   }
551   *contraction_range = max_range;
552 }
553 
554 // Return true if we've made a final IPV6/BROKEN decision, false if the result
555 // is NEUTRAL, and we could use a second opinion.
556 template<typename CHAR, typename UCHAR>
DoCanonicalizeIPv6Address(const CHAR * spec,const Component & host,CanonOutput * output,CanonHostInfo * host_info)557 bool DoCanonicalizeIPv6Address(const CHAR* spec,
558                                const Component& host,
559                                CanonOutput* output,
560                                CanonHostInfo* host_info) {
561   // Turn the IP address into a 128 bit number.
562   if (!IPv6AddressToNumber(spec, host, host_info->address)) {
563     // If it's not an IPv6 address, scan for characters that should *only*
564     // exist in an IPv6 address.
565     for (int i = host.begin; i < host.end(); i++) {
566       switch (spec[i]) {
567         case '[':
568         case ']':
569         case ':':
570           host_info->family = CanonHostInfo::BROKEN;
571           return true;
572       }
573     }
574 
575     // No invalid characters. Could still be IPv4 or a hostname.
576     host_info->family = CanonHostInfo::NEUTRAL;
577     return false;
578   }
579 
580   host_info->out_host.begin = output->length();
581   output->push_back('[');
582   AppendIPv6Address(host_info->address, output);
583   output->push_back(']');
584   host_info->out_host.len = output->length() - host_info->out_host.begin;
585 
586   host_info->family = CanonHostInfo::IPV6;
587   return true;
588 }
589 
590 }  // namespace
591 
AppendIPv4Address(const unsigned char address[4],CanonOutput * output)592 void AppendIPv4Address(const unsigned char address[4], CanonOutput* output) {
593   for (int i = 0; i < 4; i++) {
594     char str[16];
595     _itoa_s(address[i], str, 10);
596 
597     for (int ch = 0; str[ch] != 0; ch++)
598       output->push_back(str[ch]);
599 
600     if (i != 3)
601       output->push_back('.');
602   }
603 }
604 
AppendIPv6Address(const unsigned char address[16],CanonOutput * output)605 void AppendIPv6Address(const unsigned char address[16], CanonOutput* output) {
606   // We will output the address according to the rules in:
607   // http://tools.ietf.org/html/draft-kawamura-ipv6-text-representation-01#section-4
608 
609   // Start by finding where to place the "::" contraction (if any).
610   Component contraction_range;
611   ChooseIPv6ContractionRange(address, &contraction_range);
612 
613   for (int i = 0; i <= 14;) {
614     // We check 2 bytes at a time, from bytes (0, 1) to (14, 15), inclusive.
615     DCHECK(i % 2 == 0);
616     if (i == contraction_range.begin && contraction_range.len > 0) {
617       // Jump over the contraction.
618       if (i == 0)
619         output->push_back(':');
620       output->push_back(':');
621       i = contraction_range.end();
622     } else {
623       // Consume the next 16 bits from |address|.
624       int x = address[i] << 8 | address[i + 1];
625 
626       i += 2;
627 
628       // Stringify the 16 bit number (at most requires 4 hex digits).
629       char str[5];
630       _itoa_s(x, str, 16);
631       for (int ch = 0; str[ch] != 0; ++ch)
632         output->push_back(str[ch]);
633 
634       // Put a colon after each number, except the last.
635       if (i < 16)
636         output->push_back(':');
637     }
638   }
639 }
640 
CanonicalizeIPAddress(const char * spec,const Component & host,CanonOutput * output,CanonHostInfo * host_info)641 void CanonicalizeIPAddress(const char* spec,
642                            const Component& host,
643                            CanonOutput* output,
644                            CanonHostInfo* host_info) {
645   if (DoCanonicalizeIPv4Address<char, unsigned char>(
646           spec, host, output, host_info))
647     return;
648   if (DoCanonicalizeIPv6Address<char, unsigned char>(
649           spec, host, output, host_info))
650     return;
651 }
652 
CanonicalizeIPAddress(const char16_t * spec,const Component & host,CanonOutput * output,CanonHostInfo * host_info)653 void CanonicalizeIPAddress(const char16_t* spec,
654                            const Component& host,
655                            CanonOutput* output,
656                            CanonHostInfo* host_info) {
657   if (DoCanonicalizeIPv4Address<char16_t, char16_t>(spec, host, output,
658                                                     host_info))
659     return;
660   if (DoCanonicalizeIPv6Address<char16_t, char16_t>(spec, host, output,
661                                                     host_info))
662     return;
663 }
664 
CanonicalizeIPv6Address(const char * spec,const Component & host,CanonOutput & output,CanonHostInfo & host_info)665 void CanonicalizeIPv6Address(const char* spec,
666                              const Component& host,
667                              CanonOutput& output,
668                              CanonHostInfo& host_info) {
669   DoCanonicalizeIPv6Address<char, unsigned char>(spec, host, &output,
670                                                  &host_info);
671 }
672 
CanonicalizeIPv6Address(const char16_t * spec,const Component & host,CanonOutput & output,CanonHostInfo & host_info)673 void CanonicalizeIPv6Address(const char16_t* spec,
674                              const Component& host,
675                              CanonOutput& output,
676                              CanonHostInfo& host_info) {
677   DoCanonicalizeIPv6Address<char16_t, char16_t>(spec, host, &output,
678                                                 &host_info);
679 }
680 
IPv4AddressToNumber(const char * spec,const Component & host,unsigned char address[4],int * num_ipv4_components)681 CanonHostInfo::Family IPv4AddressToNumber(const char* spec,
682                                           const Component& host,
683                                           unsigned char address[4],
684                                           int* num_ipv4_components) {
685   return DoIPv4AddressToNumber<char, unsigned char>(spec, host, address,
686                                                     num_ipv4_components);
687 }
688 
IPv4AddressToNumber(const char16_t * spec,const Component & host,unsigned char address[4],int * num_ipv4_components)689 CanonHostInfo::Family IPv4AddressToNumber(const char16_t* spec,
690                                           const Component& host,
691                                           unsigned char address[4],
692                                           int* num_ipv4_components) {
693   return DoIPv4AddressToNumber<char16_t, char16_t>(spec, host, address,
694                                                    num_ipv4_components);
695 }
696 
IPv6AddressToNumber(const char * spec,const Component & host,unsigned char address[16])697 bool IPv6AddressToNumber(const char* spec,
698                          const Component& host,
699                          unsigned char address[16]) {
700   return DoIPv6AddressToNumber<char, unsigned char>(spec, host, address);
701 }
702 
IPv6AddressToNumber(const char16_t * spec,const Component & host,unsigned char address[16])703 bool IPv6AddressToNumber(const char16_t* spec,
704                          const Component& host,
705                          unsigned char address[16]) {
706   return DoIPv6AddressToNumber<char16_t, char16_t>(spec, host, address);
707 }
708 
709 }  // namespace url
710