src/ipv4_address.cpp

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