ipv4_address.ipp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/url
  8. //
  9. #ifndef BOOST_URL_IMPL_IPV4_ADDRESS_IPP
  10. #define BOOST_URL_IMPL_IPV4_ADDRESS_IPP
  11. #include <boost/url/ipv4_address.hpp>
  12. #include <boost/url/detail/except.hpp>
  13. #include <boost/url/rfc/ipv4_address_rule.hpp>
  14. #include <cstring>
  15. namespace boost {
  16. namespace urls {
  17. ipv4_address::
  18. ipv4_address(
  19. uint_type addr) noexcept
  20. : addr_(addr)
  21. {
  22. }
  23. ipv4_address::
  24. ipv4_address(
  25. bytes_type const& bytes) noexcept
  26. {
  27. addr_ =
  28. (static_cast<unsigned long>(bytes[0]) << 24) |
  29. (static_cast<unsigned long>(bytes[1]) << 16) |
  30. (static_cast<unsigned long>(bytes[2]) << 8) |
  31. (static_cast<unsigned long>(bytes[3]));
  32. }
  33. ipv4_address::
  34. ipv4_address(
  35. string_view s)
  36. : ipv4_address(
  37. parse_ipv4_address(s
  38. ).value(BOOST_URL_POS))
  39. {
  40. }
  41. auto
  42. ipv4_address::
  43. to_bytes() const noexcept ->
  44. bytes_type
  45. {
  46. bytes_type bytes;
  47. bytes[0] = (addr_ >> 24) & 0xff;
  48. bytes[1] = (addr_ >> 16) & 0xff;
  49. bytes[2] = (addr_ >> 8) & 0xff;
  50. bytes[3] = addr_ & 0xff;
  51. return bytes;
  52. }
  53. auto
  54. ipv4_address::
  55. to_uint() const noexcept ->
  56. uint_type
  57. {
  58. return addr_;
  59. }
  60. string_view
  61. ipv4_address::
  62. to_buffer(
  63. char* dest,
  64. std::size_t dest_size) const
  65. {
  66. if(dest_size < max_str_len)
  67. detail::throw_length_error();
  68. auto n = print_impl(dest);
  69. return string_view(dest, n);
  70. }
  71. bool
  72. ipv4_address::
  73. is_loopback() const noexcept
  74. {
  75. return (to_uint() & 0xFF000000) ==
  76. 0x7F000000;
  77. }
  78. bool
  79. ipv4_address::
  80. is_unspecified() const noexcept
  81. {
  82. return to_uint() == 0;
  83. }
  84. bool
  85. ipv4_address::
  86. is_multicast() const noexcept
  87. {
  88. return (to_uint() & 0xF0000000) ==
  89. 0xE0000000;
  90. }
  91. std::size_t
  92. ipv4_address::
  93. print_impl(
  94. char* dest) const noexcept
  95. {
  96. auto const start = dest;
  97. auto const write =
  98. []( char*& dest,
  99. unsigned char v)
  100. {
  101. if(v >= 100)
  102. {
  103. *dest++ = '0' +
  104. v / 100;
  105. v %= 100;
  106. *dest++ = '0' +
  107. v / 10;
  108. v %= 10;
  109. }
  110. else if(v >= 10)
  111. {
  112. *dest++ = '0' +
  113. v / 10;
  114. v %= 10;
  115. }
  116. *dest++ = '0' + v;
  117. };
  118. auto const v = to_uint();
  119. write(dest, (v >> 24) & 0xff);
  120. *dest++ = '.';
  121. write(dest, (v >> 16) & 0xff);
  122. *dest++ = '.';
  123. write(dest, (v >> 8) & 0xff);
  124. *dest++ = '.';
  125. write(dest, (v ) & 0xff);
  126. return dest - start;
  127. }
  128. void
  129. ipv4_address::
  130. to_string_impl(
  131. string_token::arg& t) const
  132. {
  133. char buf[max_str_len];
  134. auto const n = print_impl(buf);
  135. char* dest = t.prepare(n);
  136. std::memcpy(dest, buf, n);
  137. }
  138. //------------------------------------------------
  139. auto
  140. parse_ipv4_address(
  141. string_view s) noexcept ->
  142. result<ipv4_address>
  143. {
  144. return grammar::parse(
  145. s, ipv4_address_rule);
  146. }
  147. } // urls
  148. } // boost
  149. #endif