blob: 44ccea825fbeb83ebe7f39a21e1adfbaa539ef76 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// 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.
[email protected]e7bba5f82013-04-10 20:10:524
[email protected]318076b2013-04-18 21:19:455#include "url/url_canon_ip.h"
[email protected]e7bba5f82013-04-10 20:10:526
7#include <stdlib.h>
tfarina5595f3b2015-05-07 22:06:328#include <limits>
[email protected]e7bba5f82013-04-10 20:10:529
10#include "base/basictypes.h"
11#include "base/logging.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_canon_internal.h"
[email protected]e7bba5f82013-04-10 20:10:5213
[email protected]0318f922014-04-22 00:09:2314namespace url {
[email protected]e7bba5f82013-04-10 20:10:5215
16namespace {
17
18// Converts one of the character types that represent a numerical base to the
19// corresponding base.
20int BaseForType(SharedCharTypes type) {
21 switch (type) {
22 case CHAR_HEX:
23 return 16;
24 case CHAR_DEC:
25 return 10;
26 case CHAR_OCT:
27 return 8;
28 default:
29 return 0;
30 }
31}
32
33template<typename CHAR, typename UCHAR>
34bool DoFindIPv4Components(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:2335 const Component& host,
36 Component components[4]) {
[email protected]e7bba5f82013-04-10 20:10:5237 if (!host.is_nonempty())
38 return false;
39
40 int cur_component = 0; // Index of the component we're working on.
41 int cur_component_begin = host.begin; // Start of the current component.
42 int end = host.end();
43 for (int i = host.begin; /* nothing */; i++) {
44 if (i >= end || spec[i] == '.') {
45 // Found the end of the current component.
46 int component_len = i - cur_component_begin;
[email protected]0318f922014-04-22 00:09:2347 components[cur_component] = Component(cur_component_begin, component_len);
[email protected]e7bba5f82013-04-10 20:10:5248
49 // The next component starts after the dot.
50 cur_component_begin = i + 1;
51 cur_component++;
52
53 // Don't allow empty components (two dots in a row), except we may
54 // allow an empty component at the end (this would indicate that the
55 // input ends in a dot). We also want to error if the component is
56 // empty and it's the only component (cur_component == 1).
57 if (component_len == 0 && (i < end || cur_component == 1))
58 return false;
59
60 if (i >= end)
61 break; // End of the input.
62
63 if (cur_component == 4) {
64 // Anything else after the 4th component is an error unless it is a
65 // dot that would otherwise be treated as the end of input.
66 if (spec[i] == '.' && i + 1 == end)
67 break;
68 return false;
69 }
70 } else if (static_cast<UCHAR>(spec[i]) >= 0x80 ||
71 !IsIPv4Char(static_cast<unsigned char>(spec[i]))) {
72 // Invalid character for an IPv4 address.
73 return false;
74 }
75 }
76
77 // Fill in any unused components.
78 while (cur_component < 4)
[email protected]0318f922014-04-22 00:09:2379 components[cur_component++] = Component();
[email protected]e7bba5f82013-04-10 20:10:5280 return true;
81}
82
83// Converts an IPv4 component to a 32-bit number, while checking for overflow.
84//
85// Possible return values:
86// - IPV4 - The number was valid, and did not overflow.
87// - BROKEN - The input was numeric, but too large for a 32-bit field.
88// - NEUTRAL - Input was not numeric.
89//
90// The input is assumed to be ASCII. FindIPv4Components should have stripped
91// out any input that is greater than 7 bits. The components are assumed
92// to be non-empty.
93template<typename CHAR>
[email protected]0318f922014-04-22 00:09:2394CanonHostInfo::Family IPv4ComponentToNumber(const CHAR* spec,
95 const Component& component,
96 uint32* number) {
[email protected]e7bba5f82013-04-10 20:10:5297 // Figure out the base
98 SharedCharTypes base;
99 int base_prefix_len = 0; // Size of the prefix for this base.
100 if (spec[component.begin] == '0') {
101 // Either hex or dec, or a standalone zero.
102 if (component.len == 1) {
103 base = CHAR_DEC;
104 } else if (spec[component.begin + 1] == 'X' ||
105 spec[component.begin + 1] == 'x') {
106 base = CHAR_HEX;
107 base_prefix_len = 2;
108 } else {
109 base = CHAR_OCT;
110 base_prefix_len = 1;
111 }
112 } else {
113 base = CHAR_DEC;
114 }
115
116 // Extend the prefix to consume all leading zeros.
117 while (base_prefix_len < component.len &&
118 spec[component.begin + base_prefix_len] == '0')
119 base_prefix_len++;
120
121 // Put the component, minus any base prefix, into a NULL-terminated buffer so
122 // we can call the standard library. Because leading zeros have already been
123 // discarded, filling the entire buffer is guaranteed to trigger the 32-bit
124 // overflow check.
125 const int kMaxComponentLen = 16;
126 char buf[kMaxComponentLen + 1]; // digits + '\0'
127 int dest_i = 0;
128 for (int i = component.begin + base_prefix_len; i < component.end(); i++) {
129 // We know the input is 7-bit, so convert to narrow (if this is the wide
130 // version of the template) by casting.
131 char input = static_cast<char>(spec[i]);
132
133 // Validate that this character is OK for the given base.
134 if (!IsCharOfType(input, base))
135 return CanonHostInfo::NEUTRAL;
136
137 // Fill the buffer, if there's space remaining. This check allows us to
138 // verify that all characters are numeric, even those that don't fit.
139 if (dest_i < kMaxComponentLen)
140 buf[dest_i++] = input;
141 }
142
143 buf[dest_i] = '\0';
144
145 // Use the 64-bit strtoi so we get a big number (no hex, decimal, or octal
146 // number can overflow a 64-bit number in <= 16 characters).
147 uint64 num = _strtoui64(buf, NULL, BaseForType(base));
148
149 // Check for 32-bit overflow.
tfarina5595f3b2015-05-07 22:06:32150 if (num > std::numeric_limits<uint32_t>::max())
[email protected]e7bba5f82013-04-10 20:10:52151 return CanonHostInfo::BROKEN;
152
153 // No overflow. Success!
154 *number = static_cast<uint32>(num);
155 return CanonHostInfo::IPV4;
156}
157
158// See declaration of IPv4AddressToNumber for documentation.
159template<typename CHAR>
160CanonHostInfo::Family DoIPv4AddressToNumber(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:23161 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52162 unsigned char address[4],
163 int* num_ipv4_components) {
164 // The identified components. Not all may exist.
[email protected]0318f922014-04-22 00:09:23165 Component components[4];
[email protected]e7bba5f82013-04-10 20:10:52166 if (!FindIPv4Components(spec, host, components))
167 return CanonHostInfo::NEUTRAL;
168
169 // Convert existing components to digits. Values up to
170 // |existing_components| will be valid.
171 uint32 component_values[4];
172 int existing_components = 0;
173
174 // Set to true if one or more components are BROKEN. BROKEN is only
175 // returned if all components are IPV4 or BROKEN, so, for example,
176 // 12345678912345.de returns NEUTRAL rather than broken.
177 bool broken = false;
178 for (int i = 0; i < 4; i++) {
179 if (components[i].len <= 0)
180 continue;
181 CanonHostInfo::Family family = IPv4ComponentToNumber(
182 spec, components[i], &component_values[existing_components]);
183
184 if (family == CanonHostInfo::BROKEN) {
185 broken = true;
186 } else if (family != CanonHostInfo::IPV4) {
187 // Stop if we hit a non-BROKEN invalid non-empty component.
188 return family;
189 }
190
191 existing_components++;
192 }
193
194 if (broken)
195 return CanonHostInfo::BROKEN;
196
197 // Use that sequence of numbers to fill out the 4-component IP address.
198
199 // First, process all components but the last, while making sure each fits
200 // within an 8-bit field.
201 for (int i = 0; i < existing_components - 1; i++) {
tfarina5595f3b2015-05-07 22:06:32202 if (component_values[i] > std::numeric_limits<uint8_t>::max())
[email protected]e7bba5f82013-04-10 20:10:52203 return CanonHostInfo::BROKEN;
204 address[i] = static_cast<unsigned char>(component_values[i]);
205 }
206
207 // Next, consume the last component to fill in the remaining bytes.
[email protected]b6e05ccf2014-07-14 07:56:01208 // Work around a gcc 4.9 bug. crbug.com/392872
[email protected]85e09da0b2014-07-29 23:56:03209#if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 9) || __GNUC__ > 4)
[email protected]b6e05ccf2014-07-14 07:56:01210#pragma GCC diagnostic push
211#pragma GCC diagnostic ignored "-Warray-bounds"
212#endif
[email protected]e7bba5f82013-04-10 20:10:52213 uint32 last_value = component_values[existing_components - 1];
[email protected]85e09da0b2014-07-29 23:56:03214#if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 9) || __GNUC__ > 4)
[email protected]b6e05ccf2014-07-14 07:56:01215#pragma GCC diagnostic pop
216#endif
[email protected]e7bba5f82013-04-10 20:10:52217 for (int i = 3; i >= existing_components - 1; i--) {
218 address[i] = static_cast<unsigned char>(last_value);
219 last_value >>= 8;
220 }
221
222 // If the last component has residual bits, report overflow.
223 if (last_value != 0)
224 return CanonHostInfo::BROKEN;
225
226 // Tell the caller how many components we saw.
227 *num_ipv4_components = existing_components;
228
229 // Success!
230 return CanonHostInfo::IPV4;
231}
232
233// Return true if we've made a final IPV4/BROKEN decision, false if the result
234// is NEUTRAL, and we could use a second opinion.
235template<typename CHAR, typename UCHAR>
236bool DoCanonicalizeIPv4Address(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:23237 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52238 CanonOutput* output,
239 CanonHostInfo* host_info) {
240 host_info->family = IPv4AddressToNumber(
241 spec, host, host_info->address, &host_info->num_ipv4_components);
242
243 switch (host_info->family) {
244 case CanonHostInfo::IPV4:
245 // Definitely an IPv4 address.
246 host_info->out_host.begin = output->length();
247 AppendIPv4Address(host_info->address, output);
248 host_info->out_host.len = output->length() - host_info->out_host.begin;
249 return true;
250 case CanonHostInfo::BROKEN:
251 // Definitely broken.
252 return true;
253 default:
254 // Could be IPv6 or a hostname.
255 return false;
256 }
257}
258
259// Helper class that describes the main components of an IPv6 input string.
260// See the following examples to understand how it breaks up an input string:
261//
262// [Example 1]: input = "[::aa:bb]"
263// ==> num_hex_components = 2
264// ==> hex_components[0] = Component(3,2) "aa"
265// ==> hex_components[1] = Component(6,2) "bb"
266// ==> index_of_contraction = 0
267// ==> ipv4_component = Component(0, -1)
268//
269// [Example 2]: input = "[1:2::3:4:5]"
270// ==> num_hex_components = 5
271// ==> hex_components[0] = Component(1,1) "1"
272// ==> hex_components[1] = Component(3,1) "2"
273// ==> hex_components[2] = Component(6,1) "3"
274// ==> hex_components[3] = Component(8,1) "4"
275// ==> hex_components[4] = Component(10,1) "5"
276// ==> index_of_contraction = 2
277// ==> ipv4_component = Component(0, -1)
278//
279// [Example 3]: input = "[::ffff:192.168.0.1]"
280// ==> num_hex_components = 1
281// ==> hex_components[0] = Component(3,4) "ffff"
282// ==> index_of_contraction = 0
283// ==> ipv4_component = Component(8, 11) "192.168.0.1"
284//
285// [Example 4]: input = "[1::]"
286// ==> num_hex_components = 1
287// ==> hex_components[0] = Component(1,1) "1"
288// ==> index_of_contraction = 1
289// ==> ipv4_component = Component(0, -1)
290//
291// [Example 5]: input = "[::192.168.0.1]"
292// ==> num_hex_components = 0
293// ==> index_of_contraction = 0
294// ==> ipv4_component = Component(8, 11) "192.168.0.1"
295//
296struct IPv6Parsed {
297 // Zero-out the parse information.
298 void reset() {
299 num_hex_components = 0;
300 index_of_contraction = -1;
301 ipv4_component.reset();
302 }
303
304 // There can be up to 8 hex components (colon separated) in the literal.
[email protected]0318f922014-04-22 00:09:23305 Component hex_components[8];
[email protected]e7bba5f82013-04-10 20:10:52306
307 // The count of hex components present. Ranges from [0,8].
308 int num_hex_components;
309
310 // The index of the hex component that the "::" contraction precedes, or
311 // -1 if there is no contraction.
312 int index_of_contraction;
313
314 // The range of characters which are an IPv4 literal.
[email protected]0318f922014-04-22 00:09:23315 Component ipv4_component;
[email protected]e7bba5f82013-04-10 20:10:52316};
317
318// Parse the IPv6 input string. If parsing succeeded returns true and fills
319// |parsed| with the information. If parsing failed (because the input is
320// invalid) returns false.
321template<typename CHAR, typename UCHAR>
[email protected]0318f922014-04-22 00:09:23322bool DoParseIPv6(const CHAR* spec, const Component& host, IPv6Parsed* parsed) {
[email protected]e7bba5f82013-04-10 20:10:52323 // Zero-out the info.
324 parsed->reset();
325
326 if (!host.is_nonempty())
327 return false;
328
329 // The index for start and end of address range (no brackets).
330 int begin = host.begin;
331 int end = host.end();
332
333 int cur_component_begin = begin; // Start of the current component.
334
335 // Scan through the input, searching for hex components, "::" contractions,
336 // and IPv4 components.
337 for (int i = begin; /* i <= end */; i++) {
338 bool is_colon = spec[i] == ':';
339 bool is_contraction = is_colon && i < end - 1 && spec[i + 1] == ':';
340
341 // We reached the end of the current component if we encounter a colon
342 // (separator between hex components, or start of a contraction), or end of
343 // input.
344 if (is_colon || i == end) {
345 int component_len = i - cur_component_begin;
346
347 // A component should not have more than 4 hex digits.
348 if (component_len > 4)
349 return false;
350
351 // Don't allow empty components.
352 if (component_len == 0) {
353 // The exception is when contractions appear at beginning of the
354 // input or at the end of the input.
355 if (!((is_contraction && i == begin) || (i == end &&
356 parsed->index_of_contraction == parsed->num_hex_components)))
357 return false;
358 }
359
360 // Add the hex component we just found to running list.
361 if (component_len > 0) {
362 // Can't have more than 8 components!
363 if (parsed->num_hex_components >= 8)
364 return false;
365
366 parsed->hex_components[parsed->num_hex_components++] =
[email protected]0318f922014-04-22 00:09:23367 Component(cur_component_begin, component_len);
[email protected]e7bba5f82013-04-10 20:10:52368 }
369 }
370
371 if (i == end)
372 break; // Reached the end of the input, DONE.
373
374 // We found a "::" contraction.
375 if (is_contraction) {
376 // There can be at most one contraction in the literal.
377 if (parsed->index_of_contraction != -1)
378 return false;
379 parsed->index_of_contraction = parsed->num_hex_components;
380 ++i; // Consume the colon we peeked.
381 }
382
383 if (is_colon) {
384 // Colons are separators between components, keep track of where the
385 // current component started (after this colon).
386 cur_component_begin = i + 1;
387 } else {
388 if (static_cast<UCHAR>(spec[i]) >= 0x80)
389 return false; // Not ASCII.
390
391 if (!IsHexChar(static_cast<unsigned char>(spec[i]))) {
392 // Regular components are hex numbers. It is also possible for
393 // a component to be an IPv4 address in dotted form.
394 if (IsIPv4Char(static_cast<unsigned char>(spec[i]))) {
395 // Since IPv4 address can only appear at the end, assume the rest
396 // of the string is an IPv4 address. (We will parse this separately
397 // later).
[email protected]0318f922014-04-22 00:09:23398 parsed->ipv4_component =
399 Component(cur_component_begin, end - cur_component_begin);
[email protected]e7bba5f82013-04-10 20:10:52400 break;
401 } else {
402 // The character was neither a hex digit, nor an IPv4 character.
403 return false;
404 }
405 }
406 }
407 }
408
409 return true;
410}
411
412// Verifies the parsed IPv6 information, checking that the various components
413// add up to the right number of bits (hex components are 16 bits, while
414// embedded IPv4 formats are 32 bits, and contractions are placeholdes for
415// 16 or more bits). Returns true if sizes match up, false otherwise. On
416// success writes the length of the contraction (if any) to
417// |out_num_bytes_of_contraction|.
418bool CheckIPv6ComponentsSize(const IPv6Parsed& parsed,
419 int* out_num_bytes_of_contraction) {
420 // Each group of four hex digits contributes 16 bits.
421 int num_bytes_without_contraction = parsed.num_hex_components * 2;
422
423 // If an IPv4 address was embedded at the end, it contributes 32 bits.
424 if (parsed.ipv4_component.is_valid())
425 num_bytes_without_contraction += 4;
426
427 // If there was a "::" contraction, its size is going to be:
428 // MAX([16bits], [128bits] - num_bytes_without_contraction).
429 int num_bytes_of_contraction = 0;
430 if (parsed.index_of_contraction != -1) {
431 num_bytes_of_contraction = 16 - num_bytes_without_contraction;
432 if (num_bytes_of_contraction < 2)
433 num_bytes_of_contraction = 2;
434 }
435
436 // Check that the numbers add up.
437 if (num_bytes_without_contraction + num_bytes_of_contraction != 16)
438 return false;
439
440 *out_num_bytes_of_contraction = num_bytes_of_contraction;
441 return true;
442}
443
444// Converts a hex comonent into a number. This cannot fail since the caller has
445// already verified that each character in the string was a hex digit, and
446// that there were no more than 4 characters.
447template<typename CHAR>
[email protected]0318f922014-04-22 00:09:23448uint16 IPv6HexComponentToNumber(const CHAR* spec, const Component& component) {
[email protected]e7bba5f82013-04-10 20:10:52449 DCHECK(component.len <= 4);
450
451 // Copy the hex string into a C-string.
452 char buf[5];
453 for (int i = 0; i < component.len; ++i)
454 buf[i] = static_cast<char>(spec[component.begin + i]);
455 buf[component.len] = '\0';
456
457 // Convert it to a number (overflow is not possible, since with 4 hex
458 // characters we can at most have a 16 bit number).
459 return static_cast<uint16>(_strtoui64(buf, NULL, 16));
460}
461
462// Converts an IPv6 address to a 128-bit number (network byte order), returning
463// true on success. False means that the input was not a valid IPv6 address.
464template<typename CHAR, typename UCHAR>
465bool DoIPv6AddressToNumber(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:23466 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52467 unsigned char address[16]) {
468 // Make sure the component is bounded by '[' and ']'.
469 int end = host.end();
470 if (!host.is_nonempty() || spec[host.begin] != '[' || spec[end - 1] != ']')
471 return false;
472
473 // Exclude the square brackets.
[email protected]0318f922014-04-22 00:09:23474 Component ipv6_comp(host.begin + 1, host.len - 2);
[email protected]e7bba5f82013-04-10 20:10:52475
476 // Parse the IPv6 address -- identify where all the colon separated hex
477 // components are, the "::" contraction, and the embedded IPv4 address.
478 IPv6Parsed ipv6_parsed;
479 if (!DoParseIPv6<CHAR, UCHAR>(spec, ipv6_comp, &ipv6_parsed))
480 return false;
481
482 // Do some basic size checks to make sure that the address doesn't
483 // specify more than 128 bits or fewer than 128 bits. This also resolves
484 // how may zero bytes the "::" contraction represents.
485 int num_bytes_of_contraction;
486 if (!CheckIPv6ComponentsSize(ipv6_parsed, &num_bytes_of_contraction))
487 return false;
488
489 int cur_index_in_address = 0;
490
491 // Loop through each hex components, and contraction in order.
492 for (int i = 0; i <= ipv6_parsed.num_hex_components; ++i) {
493 // Append the contraction if it appears before this component.
494 if (i == ipv6_parsed.index_of_contraction) {
495 for (int j = 0; j < num_bytes_of_contraction; ++j)
496 address[cur_index_in_address++] = 0;
497 }
498 // Append the hex component's value.
499 if (i != ipv6_parsed.num_hex_components) {
500 // Get the 16-bit value for this hex component.
501 uint16 number = IPv6HexComponentToNumber<CHAR>(
502 spec, ipv6_parsed.hex_components[i]);
503 // Append to |address|, in network byte order.
504 address[cur_index_in_address++] = (number & 0xFF00) >> 8;
505 address[cur_index_in_address++] = (number & 0x00FF);
506 }
507 }
508
509 // If there was an IPv4 section, convert it into a 32-bit number and append
510 // it to |address|.
511 if (ipv6_parsed.ipv4_component.is_valid()) {
512 // Append the 32-bit number to |address|.
513 int ignored_num_ipv4_components;
514 if (CanonHostInfo::IPV4 !=
515 IPv4AddressToNumber(spec,
516 ipv6_parsed.ipv4_component,
517 &address[cur_index_in_address],
518 &ignored_num_ipv4_components))
519 return false;
520 }
521
522 return true;
523}
524
525// Searches for the longest sequence of zeros in |address|, and writes the
526// range into |contraction_range|. The run of zeros must be at least 16 bits,
527// and if there is a tie the first is chosen.
528void ChooseIPv6ContractionRange(const unsigned char address[16],
[email protected]0318f922014-04-22 00:09:23529 Component* contraction_range) {
[email protected]e7bba5f82013-04-10 20:10:52530 // The longest run of zeros in |address| seen so far.
[email protected]0318f922014-04-22 00:09:23531 Component max_range;
[email protected]e7bba5f82013-04-10 20:10:52532
533 // The current run of zeros in |address| being iterated over.
[email protected]0318f922014-04-22 00:09:23534 Component cur_range;
[email protected]e7bba5f82013-04-10 20:10:52535
536 for (int i = 0; i < 16; i += 2) {
537 // Test for 16 bits worth of zero.
538 bool is_zero = (address[i] == 0 && address[i + 1] == 0);
539
540 if (is_zero) {
541 // Add the zero to the current range (or start a new one).
542 if (!cur_range.is_valid())
[email protected]0318f922014-04-22 00:09:23543 cur_range = Component(i, 0);
[email protected]e7bba5f82013-04-10 20:10:52544 cur_range.len += 2;
545 }
546
547 if (!is_zero || i == 14) {
548 // Just completed a run of zeros. If the run is greater than 16 bits,
549 // it is a candidate for the contraction.
550 if (cur_range.len > 2 && cur_range.len > max_range.len) {
551 max_range = cur_range;
552 }
553 cur_range.reset();
554 }
555 }
556 *contraction_range = max_range;
557}
558
559// Return true if we've made a final IPV6/BROKEN decision, false if the result
560// is NEUTRAL, and we could use a second opinion.
561template<typename CHAR, typename UCHAR>
562bool DoCanonicalizeIPv6Address(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:23563 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52564 CanonOutput* output,
565 CanonHostInfo* host_info) {
566 // Turn the IP address into a 128 bit number.
567 if (!IPv6AddressToNumber(spec, host, host_info->address)) {
568 // If it's not an IPv6 address, scan for characters that should *only*
569 // exist in an IPv6 address.
570 for (int i = host.begin; i < host.end(); i++) {
571 switch (spec[i]) {
572 case '[':
573 case ']':
574 case ':':
575 host_info->family = CanonHostInfo::BROKEN;
576 return true;
577 }
578 }
579
580 // No invalid characters. Could still be IPv4 or a hostname.
581 host_info->family = CanonHostInfo::NEUTRAL;
582 return false;
583 }
584
585 host_info->out_host.begin = output->length();
586 output->push_back('[');
587 AppendIPv6Address(host_info->address, output);
588 output->push_back(']');
589 host_info->out_host.len = output->length() - host_info->out_host.begin;
590
591 host_info->family = CanonHostInfo::IPV6;
592 return true;
593}
594
595} // namespace
596
597void AppendIPv4Address(const unsigned char address[4], CanonOutput* output) {
598 for (int i = 0; i < 4; i++) {
599 char str[16];
600 _itoa_s(address[i], str, 10);
601
602 for (int ch = 0; str[ch] != 0; ch++)
603 output->push_back(str[ch]);
604
605 if (i != 3)
606 output->push_back('.');
607 }
608}
609
610void AppendIPv6Address(const unsigned char address[16], CanonOutput* output) {
611 // We will output the address according to the rules in:
612 // http://tools.ietf.org/html/draft-kawamura-ipv6-text-representation-01#section-4
613
614 // Start by finding where to place the "::" contraction (if any).
[email protected]0318f922014-04-22 00:09:23615 Component contraction_range;
[email protected]e7bba5f82013-04-10 20:10:52616 ChooseIPv6ContractionRange(address, &contraction_range);
617
618 for (int i = 0; i <= 14;) {
619 // We check 2 bytes at a time, from bytes (0, 1) to (14, 15), inclusive.
620 DCHECK(i % 2 == 0);
621 if (i == contraction_range.begin && contraction_range.len > 0) {
622 // Jump over the contraction.
623 if (i == 0)
624 output->push_back(':');
625 output->push_back(':');
626 i = contraction_range.end();
627 } else {
628 // Consume the next 16 bits from |address|.
629 int x = address[i] << 8 | address[i + 1];
630
631 i += 2;
632
633 // Stringify the 16 bit number (at most requires 4 hex digits).
634 char str[5];
635 _itoa_s(x, str, 16);
636 for (int ch = 0; str[ch] != 0; ++ch)
637 output->push_back(str[ch]);
638
639 // Put a colon after each number, except the last.
640 if (i < 16)
641 output->push_back(':');
642 }
643 }
644}
645
646bool FindIPv4Components(const char* spec,
[email protected]0318f922014-04-22 00:09:23647 const Component& host,
648 Component components[4]) {
[email protected]e7bba5f82013-04-10 20:10:52649 return DoFindIPv4Components<char, unsigned char>(spec, host, components);
650}
651
[email protected]3774f832013-06-11 21:21:57652bool FindIPv4Components(const base::char16* spec,
[email protected]0318f922014-04-22 00:09:23653 const Component& host,
654 Component components[4]) {
[email protected]3774f832013-06-11 21:21:57655 return DoFindIPv4Components<base::char16, base::char16>(
656 spec, host, components);
[email protected]e7bba5f82013-04-10 20:10:52657}
658
659void CanonicalizeIPAddress(const char* spec,
[email protected]0318f922014-04-22 00:09:23660 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52661 CanonOutput* output,
662 CanonHostInfo* host_info) {
663 if (DoCanonicalizeIPv4Address<char, unsigned char>(
664 spec, host, output, host_info))
665 return;
666 if (DoCanonicalizeIPv6Address<char, unsigned char>(
667 spec, host, output, host_info))
668 return;
669}
670
[email protected]3774f832013-06-11 21:21:57671void CanonicalizeIPAddress(const base::char16* spec,
[email protected]0318f922014-04-22 00:09:23672 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52673 CanonOutput* output,
674 CanonHostInfo* host_info) {
[email protected]3774f832013-06-11 21:21:57675 if (DoCanonicalizeIPv4Address<base::char16, base::char16>(
[email protected]e7bba5f82013-04-10 20:10:52676 spec, host, output, host_info))
677 return;
[email protected]3774f832013-06-11 21:21:57678 if (DoCanonicalizeIPv6Address<base::char16, base::char16>(
[email protected]e7bba5f82013-04-10 20:10:52679 spec, host, output, host_info))
680 return;
681}
682
683CanonHostInfo::Family IPv4AddressToNumber(const char* spec,
[email protected]0318f922014-04-22 00:09:23684 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52685 unsigned char address[4],
686 int* num_ipv4_components) {
687 return DoIPv4AddressToNumber<char>(spec, host, address, num_ipv4_components);
688}
689
[email protected]3774f832013-06-11 21:21:57690CanonHostInfo::Family IPv4AddressToNumber(const base::char16* spec,
[email protected]0318f922014-04-22 00:09:23691 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52692 unsigned char address[4],
693 int* num_ipv4_components) {
[email protected]3774f832013-06-11 21:21:57694 return DoIPv4AddressToNumber<base::char16>(
[email protected]e7bba5f82013-04-10 20:10:52695 spec, host, address, num_ipv4_components);
696}
697
698bool IPv6AddressToNumber(const char* spec,
[email protected]0318f922014-04-22 00:09:23699 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52700 unsigned char address[16]) {
701 return DoIPv6AddressToNumber<char, unsigned char>(spec, host, address);
702}
703
[email protected]3774f832013-06-11 21:21:57704bool IPv6AddressToNumber(const base::char16* spec,
[email protected]0318f922014-04-22 00:09:23705 const Component& host,
[email protected]e7bba5f82013-04-10 20:10:52706 unsigned char address[16]) {
[email protected]3774f832013-06-11 21:21:57707 return DoIPv6AddressToNumber<base::char16, base::char16>(spec, host, address);
[email protected]e7bba5f82013-04-10 20:10:52708}
709
[email protected]0318f922014-04-22 00:09:23710} // namespace url