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