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