url_canon_ip.cc 23 KB

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