src/corosio/src/ipv6_address.cpp
89.8% Lines (221/246)
100.0% Functions (17/17)
77.5% Branches (117/151)
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Vinnie Falco ([email protected]) | |||
| 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/cppalliance/corosio | |||
| 8 | // | |||
| 9 | ||||
| 10 | #include <boost/corosio/ipv6_address.hpp> | |||
| 11 | #include <boost/corosio/ipv4_address.hpp> | |||
| 12 | ||||
| 13 | #include <cstring> | |||
| 14 | #include <ostream> | |||
| 15 | #include <stdexcept> | |||
| 16 | ||||
| 17 | namespace boost::corosio { | |||
| 18 | ||||
| 19 | 45 | ipv6_address::ipv6_address(bytes_type const& bytes) noexcept | ||
| 20 | { | |||
| 21 | 45 | std::memcpy(addr_.data(), bytes.data(), 16); | ||
| 22 | 45 | } | ||
| 23 | ||||
| 24 | 3 | ipv6_address::ipv6_address(ipv4_address const& addr) noexcept | ||
| 25 | { | |||
| 26 | 3 | auto const v = addr.to_bytes(); | ||
| 27 | 12 | addr_ = { | ||
| 28 | 3 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, v[0], v[1], v[2], v[3]}}; | ||
| 29 | 3 | } | ||
| 30 | ||||
| 31 | 3 | ipv6_address::ipv6_address(std::string_view s) | ||
| 32 | { | |||
| 33 | 3 | auto ec = parse_ipv6_address(s, *this); | ||
| 34 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 8 taken 1 time.
|
3 | if (ec) | |
| 35 |
1/1✓ Branch 6 → 7 taken 2 times.
|
2 | throw std::invalid_argument("invalid IPv6 address"); | |
| 36 | 1 | } | ||
| 37 | ||||
| 38 | std::string | |||
| 39 | 6 | ipv6_address::to_string() const | ||
| 40 | { | |||
| 41 | char buf[max_str_len]; | |||
| 42 | 6 | auto n = print_impl(buf); | ||
| 43 |
1/1✓ Branch 5 → 6 taken 6 times.
|
12 | return std::string(buf, n); | |
| 44 | } | |||
| 45 | ||||
| 46 | std::string_view | |||
| 47 | 2 | ipv6_address::to_buffer(char* dest, std::size_t dest_size) const | ||
| 48 | { | |||
| 49 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 2 times.
|
2 | if (dest_size < max_str_len) | |
| 50 | ✗ | throw std::length_error("buffer too small for IPv6 address"); | ||
| 51 | 2 | auto n = print_impl(dest); | ||
| 52 | 2 | return std::string_view(dest, n); | ||
| 53 | } | |||
| 54 | ||||
| 55 | bool | |||
| 56 | 3 | ipv6_address::is_unspecified() const noexcept | ||
| 57 | { | |||
| 58 | 3 | return *this == ipv6_address(); | ||
| 59 | } | |||
| 60 | ||||
| 61 | bool | |||
| 62 | 10 | ipv6_address::is_loopback() const noexcept | ||
| 63 | { | |||
| 64 | 10 | return *this == loopback(); | ||
| 65 | } | |||
| 66 | ||||
| 67 | bool | |||
| 68 | 12 | ipv6_address::is_v4_mapped() const noexcept | ||
| 69 | { | |||
| 70 |
4/6✓ Branch 5 → 6 taken 10 times.
✓ Branch 5 → 27 taken 2 times.
✓ Branch 7 → 8 taken 10 times.
✗ Branch 7 → 27 not taken.
✓ Branch 9 → 10 taken 10 times.
✗ Branch 9 → 27 not taken.
|
24 | return addr_[0] == 0 && addr_[1] == 0 && addr_[2] == 0 && addr_[3] == 0 && | |
| 71 |
4/8✓ Branch 11 → 12 taken 10 times.
✗ Branch 11 → 27 not taken.
✓ Branch 13 → 14 taken 10 times.
✗ Branch 13 → 27 not taken.
✓ Branch 15 → 16 taken 10 times.
✗ Branch 15 → 27 not taken.
✓ Branch 17 → 18 taken 10 times.
✗ Branch 17 → 27 not taken.
|
10 | addr_[4] == 0 && addr_[5] == 0 && addr_[6] == 0 && addr_[7] == 0 && | |
| 72 |
5/8✓ Branch 3 → 4 taken 12 times.
✗ Branch 3 → 27 not taken.
✓ Branch 19 → 20 taken 10 times.
✗ Branch 19 → 27 not taken.
✓ Branch 21 → 22 taken 10 times.
✗ Branch 21 → 27 not taken.
✓ Branch 23 → 24 taken 4 times.
✓ Branch 23 → 27 taken 6 times.
|
28 | addr_[8] == 0 && addr_[9] == 0 && addr_[10] == 0xff && | |
| 73 |
1/2✓ Branch 25 → 26 taken 4 times.
✗ Branch 25 → 27 not taken.
|
16 | addr_[11] == 0xff; | |
| 74 | } | |||
| 75 | ||||
| 76 | ipv6_address | |||
| 77 | 23 | ipv6_address::loopback() noexcept | ||
| 78 | { | |||
| 79 | 23 | ipv6_address a; | ||
| 80 | 23 | a.addr_[15] = 1; | ||
| 81 | 23 | return a; | ||
| 82 | } | |||
| 83 | ||||
| 84 | std::ostream& | |||
| 85 | 1 | operator<<(std::ostream& os, ipv6_address const& addr) | ||
| 86 | { | |||
| 87 | char buf[ipv6_address::max_str_len]; | |||
| 88 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
|
1 | os << addr.to_buffer(buf, sizeof(buf)); | |
| 89 | 1 | return os; | ||
| 90 | } | |||
| 91 | ||||
| 92 | std::size_t | |||
| 93 | 8 | ipv6_address::print_impl(char* dest) const noexcept | ||
| 94 | { | |||
| 95 | 27 | auto const count_zeroes = [](unsigned char const* first, | ||
| 96 | unsigned char const* const last) { | |||
| 97 | 27 | std::size_t n = 0; | ||
| 98 |
2/2✓ Branch 6 → 3 taken 65 times.
✓ Branch 6 → 7 taken 1 time.
|
66 | while (first != last) | |
| 99 | { | |||
| 100 |
4/4✓ Branch 3 → 4 taken 61 times.
✓ Branch 3 → 7 taken 4 times.
✓ Branch 4 → 5 taken 39 times.
✓ Branch 4 → 7 taken 22 times.
|
65 | if (first[0] != 0 || first[1] != 0) | |
| 101 | break; | |||
| 102 | 39 | n += 2; | ||
| 103 | 39 | first += 2; | ||
| 104 | } | |||
| 105 | 27 | return n; | ||
| 106 | }; | |||
| 107 | ||||
| 108 | 21 | auto const print_hex = [](char* dest, unsigned short v) { | ||
| 109 | 21 | char const* const dig = "0123456789abcdef"; | ||
| 110 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 19 times.
|
21 | if (v >= 0x1000) | |
| 111 | { | |||
| 112 | 2 | *dest++ = dig[v >> 12]; | ||
| 113 | 2 | v &= 0x0fff; | ||
| 114 | 2 | *dest++ = dig[v >> 8]; | ||
| 115 | 2 | v &= 0x0ff; | ||
| 116 | 2 | *dest++ = dig[v >> 4]; | ||
| 117 | 2 | v &= 0x0f; | ||
| 118 | 2 | *dest++ = dig[v]; | ||
| 119 | } | |||
| 120 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 19 times.
|
19 | else if (v >= 0x100) | |
| 121 | { | |||
| 122 | ✗ | *dest++ = dig[v >> 8]; | ||
| 123 | ✗ | v &= 0x0ff; | ||
| 124 | ✗ | *dest++ = dig[v >> 4]; | ||
| 125 | ✗ | v &= 0x0f; | ||
| 126 | ✗ | *dest++ = dig[v]; | ||
| 127 | } | |||
| 128 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 19 times.
|
19 | else if (v >= 0x10) | |
| 129 | { | |||
| 130 | ✗ | *dest++ = dig[v >> 4]; | ||
| 131 | ✗ | v &= 0x0f; | ||
| 132 | ✗ | *dest++ = dig[v]; | ||
| 133 | } | |||
| 134 | else | |||
| 135 | { | |||
| 136 | 19 | *dest++ = dig[v]; | ||
| 137 | } | |||
| 138 | 21 | return dest; | ||
| 139 | }; | |||
| 140 | ||||
| 141 | 8 | auto const dest0 = dest; | ||
| 142 | // find longest run of zeroes | |||
| 143 | 8 | std::size_t best_len = 0; | ||
| 144 | 8 | int best_pos = -1; | ||
| 145 | 8 | auto it = addr_.data(); | ||
| 146 | 8 | auto const v4 = is_v4_mapped(); | ||
| 147 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 8 taken 6 times.
|
16 | auto const end = v4 ? (it + addr_.size() - 4) : it + addr_.size(); | |
| 148 | ||||
| 149 |
2/2✓ Branch 19 → 12 taken 27 times.
✓ Branch 19 → 20 taken 8 times.
|
35 | while (it != end) | |
| 150 | { | |||
| 151 | 27 | auto n = count_zeroes(it, end); | ||
| 152 |
2/2✓ Branch 13 → 14 taken 21 times.
✓ Branch 13 → 15 taken 6 times.
|
27 | if (n == 0) | |
| 153 | { | |||
| 154 | 21 | it += 2; | ||
| 155 | 21 | continue; | ||
| 156 | } | |||
| 157 |
1/2✓ Branch 15 → 16 taken 6 times.
✗ Branch 15 → 18 not taken.
|
6 | if (n > best_len) | |
| 158 | { | |||
| 159 | 6 | best_pos = static_cast<int>(it - addr_.data()); | ||
| 160 | 6 | best_len = n; | ||
| 161 | } | |||
| 162 | 6 | it += n; | ||
| 163 | } | |||
| 164 | ||||
| 165 | 8 | it = addr_.data(); | ||
| 166 |
2/2✓ Branch 21 → 22 taken 2 times.
✓ Branch 21 → 24 taken 6 times.
|
8 | if (best_pos != 0) | |
| 167 | { | |||
| 168 | 2 | unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]); | ||
| 169 | 2 | dest = print_hex(dest, v); | ||
| 170 | 2 | it += 2; | ||
| 171 | } | |||
| 172 | else | |||
| 173 | { | |||
| 174 | 6 | *dest++ = ':'; | ||
| 175 | 6 | it += best_len; | ||
| 176 |
2/2✓ Branch 24 → 25 taken 1 time.
✓ Branch 24 → 26 taken 5 times.
|
6 | if (it == end) | |
| 177 | 1 | *dest++ = ':'; | ||
| 178 | } | |||
| 179 | ||||
| 180 |
2/2✓ Branch 34 → 27 taken 19 times.
✓ Branch 34 → 35 taken 8 times.
|
27 | while (it != end) | |
| 181 | { | |||
| 182 | 19 | *dest++ = ':'; | ||
| 183 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 32 taken 19 times.
|
19 | if (it - addr_.data() == best_pos) | |
| 184 | { | |||
| 185 | ✗ | it += best_len; | ||
| 186 | ✗ | if (it == end) | ||
| 187 | ✗ | *dest++ = ':'; | ||
| 188 | ✗ | continue; | ||
| 189 | } | |||
| 190 | 19 | unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]); | ||
| 191 | 19 | dest = print_hex(dest, v); | ||
| 192 | 19 | it += 2; | ||
| 193 | } | |||
| 194 | ||||
| 195 |
2/2✓ Branch 35 → 36 taken 2 times.
✓ Branch 35 → 46 taken 6 times.
|
8 | if (v4) | |
| 196 | { | |||
| 197 | ipv4_address::bytes_type bytes; | |||
| 198 | 2 | bytes[0] = it[0]; | ||
| 199 | 2 | bytes[1] = it[1]; | ||
| 200 | 2 | bytes[2] = it[2]; | ||
| 201 | 2 | bytes[3] = it[3]; | ||
| 202 | 2 | ipv4_address a(bytes); | ||
| 203 | 2 | *dest++ = ':'; | ||
| 204 | char buf[ipv4_address::max_str_len]; | |||
| 205 | 2 | auto sv = a.to_buffer(buf, sizeof(buf)); | ||
| 206 | 2 | std::memcpy(dest, sv.data(), sv.size()); | ||
| 207 | 2 | dest += sv.size(); | ||
| 208 | } | |||
| 209 | ||||
| 210 | 8 | return static_cast<std::size_t>(dest - dest0); | ||
| 211 | } | |||
| 212 | ||||
| 213 | namespace { | |||
| 214 | ||||
| 215 | // Convert hex character to value (0-15), or -1 if not hex | |||
| 216 | inline int | |||
| 217 | 304 | hexdig_value(char c) noexcept | ||
| 218 | { | |||
| 219 |
4/4✓ Branch 2 → 3 taken 299 times.
✓ Branch 2 → 5 taken 5 times.
✓ Branch 3 → 4 taken 199 times.
✓ Branch 3 → 5 taken 100 times.
|
304 | if (c >= '0' && c <= '9') | |
| 220 | 199 | return c - '0'; | ||
| 221 |
4/4✓ Branch 5 → 6 taken 27 times.
✓ Branch 5 → 8 taken 78 times.
✓ Branch 6 → 7 taken 23 times.
✓ Branch 6 → 8 taken 4 times.
|
105 | if (c >= 'a' && c <= 'f') | |
| 222 | 23 | return c - 'a' + 10; | ||
| 223 |
3/4✓ Branch 8 → 9 taken 4 times.
✓ Branch 8 → 11 taken 78 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 4 times.
|
82 | if (c >= 'A' && c <= 'F') | |
| 224 | ✗ | return c - 'A' + 10; | ||
| 225 | 82 | return -1; | ||
| 226 | } | |||
| 227 | ||||
| 228 | // Parse h16 (1-4 hex digits) returning 16-bit value | |||
| 229 | // Returns true on success, advances `it` | |||
| 230 | bool | |||
| 231 | 120 | parse_h16( | ||
| 232 | char const*& it, | |||
| 233 | char const* end, | |||
| 234 | unsigned char& hi, | |||
| 235 | unsigned char& lo) noexcept | |||
| 236 | { | |||
| 237 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 120 times.
|
120 | if (it == end) | |
| 238 | ✗ | return false; | ||
| 239 | ||||
| 240 | 120 | int d = hexdig_value(*it); | ||
| 241 |
2/2✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 118 times.
|
120 | if (d < 0) | |
| 242 | 2 | return false; | ||
| 243 | ||||
| 244 | 118 | unsigned v = static_cast<unsigned>(d); | ||
| 245 | 118 | ++it; | ||
| 246 | ||||
| 247 |
4/4✓ Branch 12 → 13 taken 155 times.
✓ Branch 12 → 14 taken 9 times.
✓ Branch 13 → 8 taken 124 times.
✓ Branch 13 → 14 taken 31 times.
|
164 | for (int i = 0; i < 3 && it != end; ++i) | |
| 248 | { | |||
| 249 | 124 | d = hexdig_value(*it); | ||
| 250 |
2/2✓ Branch 9 → 10 taken 78 times.
✓ Branch 9 → 11 taken 46 times.
|
124 | if (d < 0) | |
| 251 | 78 | break; | ||
| 252 | 46 | v = (v << 4) | static_cast<unsigned>(d); | ||
| 253 | 46 | ++it; | ||
| 254 | } | |||
| 255 | ||||
| 256 | 118 | hi = static_cast<unsigned char>((v >> 8) & 0xff); | ||
| 257 | 118 | lo = static_cast<unsigned char>(v & 0xff); | ||
| 258 | 118 | return true; | ||
| 259 | } | |||
| 260 | ||||
| 261 | // Check if a hex word could be 0..255 if interpreted as decimal | |||
| 262 | bool | |||
| 263 | 4 | maybe_octet(unsigned char const* p) noexcept | ||
| 264 | { | |||
| 265 | 4 | unsigned short word = static_cast<unsigned short>(p[0]) * 256 + | ||
| 266 | 4 | static_cast<unsigned short>(p[1]); | ||
| 267 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 4 times.
|
4 | if (word > 0x255) | |
| 268 | ✗ | return false; | ||
| 269 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 4 times.
|
4 | if (((word >> 4) & 0xf) > 9) | |
| 270 | ✗ | return false; | ||
| 271 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 4 times.
|
4 | if ((word & 0xf) > 9) | |
| 272 | ✗ | return false; | ||
| 273 | 4 | return true; | ||
| 274 | } | |||
| 275 | ||||
| 276 | } // namespace | |||
| 277 | ||||
| 278 | std::error_code | |||
| 279 | 52 | parse_ipv6_address(std::string_view s, ipv6_address& addr) noexcept | ||
| 280 | { | |||
| 281 | 52 | auto it = s.data(); | ||
| 282 | 52 | auto const end = it + s.size(); | ||
| 283 | ||||
| 284 | 52 | int n = 8; // words needed | ||
| 285 | 52 | int b = -1; // value of n when '::' seen | ||
| 286 | 52 | bool c = false; // need colon | ||
| 287 | 52 | auto prev = it; | ||
| 288 | 52 | ipv6_address::bytes_type bytes{}; | ||
| 289 | unsigned char hi, lo; | |||
| 290 | ||||
| 291 | for (;;) | |||
| 292 | { | |||
| 293 |
2/2✓ Branch 5 → 6 taken 32 times.
✓ Branch 5 → 9 taken 173 times.
|
205 | if (it == end) | |
| 294 | { | |||
| 295 |
2/2✓ Branch 6 → 7 taken 28 times.
✓ Branch 6 → 8 taken 4 times.
|
32 | if (b != -1) | |
| 296 | { | |||
| 297 | // end in "::" | |||
| 298 | 28 | break; | ||
| 299 | } | |||
| 300 | // not enough words | |||
| 301 | 4 | return std::make_error_code(std::errc::invalid_argument); | ||
| 302 | } | |||
| 303 | ||||
| 304 |
2/2✓ Branch 9 → 10 taken 109 times.
✓ Branch 9 → 28 taken 64 times.
|
173 | if (*it == ':') | |
| 305 | { | |||
| 306 | 109 | ++it; | ||
| 307 |
2/2✓ Branch 10 → 11 taken 3 times.
✓ Branch 10 → 12 taken 106 times.
|
109 | if (it == end) | |
| 308 | { | |||
| 309 | // expected ':' | |||
| 310 | 3 | return std::make_error_code(std::errc::invalid_argument); | ||
| 311 | } | |||
| 312 |
2/2✓ Branch 12 → 13 taken 42 times.
✓ Branch 12 → 18 taken 64 times.
|
106 | if (*it == ':') | |
| 313 | { | |||
| 314 |
2/2✓ Branch 13 → 14 taken 41 times.
✓ Branch 13 → 17 taken 1 time.
|
42 | if (b == -1) | |
| 315 | { | |||
| 316 | // first "::" | |||
| 317 | 41 | ++it; | ||
| 318 | 41 | --n; | ||
| 319 | 41 | b = n; | ||
| 320 |
1/2✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 41 times.
|
41 | if (n == 0) | |
| 321 | ✗ | break; | ||
| 322 | 41 | c = false; | ||
| 323 | 41 | continue; | ||
| 324 | } | |||
| 325 | // extra "::" found | |||
| 326 | 1 | return std::make_error_code(std::errc::invalid_argument); | ||
| 327 | } | |||
| 328 |
2/2✓ Branch 18 → 19 taken 61 times.
✓ Branch 18 → 27 taken 3 times.
|
64 | if (c) | |
| 329 | { | |||
| 330 | 61 | prev = it; | ||
| 331 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 61 times.
|
61 | if (!parse_h16(it, end, hi, lo)) | |
| 332 | ✗ | return std::make_error_code(std::errc::invalid_argument); | ||
| 333 | 61 | bytes[2 * (8 - n) + 0] = hi; | ||
| 334 | 61 | bytes[2 * (8 - n) + 1] = lo; | ||
| 335 | 61 | --n; | ||
| 336 |
2/2✓ Branch 24 → 25 taken 5 times.
✓ Branch 24 → 26 taken 56 times.
|
61 | if (n == 0) | |
| 337 | 5 | break; | ||
| 338 | 56 | continue; | ||
| 339 | } | |||
| 340 | // expected h16 | |||
| 341 | 3 | return std::make_error_code(std::errc::invalid_argument); | ||
| 342 | } | |||
| 343 | ||||
| 344 |
2/2✓ Branch 28 → 29 taken 4 times.
✓ Branch 28 → 63 taken 60 times.
|
64 | if (*it == '.') | |
| 345 | { | |||
| 346 |
1/4✗ Branch 29 → 30 not taken.
✓ Branch 29 → 32 taken 4 times.
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
|
4 | if (b == -1 && n > 1) | |
| 347 | { | |||
| 348 | // not enough h16 | |||
| 349 | ✗ | return std::make_error_code(std::errc::invalid_argument); | ||
| 350 | } | |||
| 351 |
1/2✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 4 times.
|
4 | if (!maybe_octet(&bytes[std::size_t(2) * std::size_t(7 - n)])) | |
| 352 | { | |||
| 353 | // invalid octet | |||
| 354 | ✗ | return std::make_error_code(std::errc::invalid_argument); | ||
| 355 | } | |||
| 356 | // rewind the h16 and parse it as IPv4 | |||
| 357 | 4 | it = prev; | ||
| 358 | 4 | ipv4_address v4; | ||
| 359 | 4 | auto ec = parse_ipv4_address( | ||
| 360 | 4 | std::string_view(it, static_cast<std::size_t>(end - it)), v4); | ||
| 361 |
1/2✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 4 times.
|
4 | if (ec) | |
| 362 | ✗ | return ec; | ||
| 363 | // Must consume exactly the IPv4 address portion | |||
| 364 | // Re-parse to find where it ends | |||
| 365 | 4 | auto v4_it = it; | ||
| 366 |
2/2✓ Branch 43 → 44 taken 41 times.
✓ Branch 43 → 47 taken 4 times.
|
45 | while (v4_it != end && | |
| 367 |
4/6✓ Branch 44 → 42 taken 12 times.
✓ Branch 44 → 45 taken 29 times.
✓ Branch 45 → 46 taken 29 times.
✗ Branch 45 → 47 not taken.
✓ Branch 46 → 42 taken 29 times.
✗ Branch 46 → 47 not taken.
|
41 | (*v4_it == '.' || (*v4_it >= '0' && *v4_it <= '9'))) | |
| 368 | 41 | ++v4_it; | ||
| 369 | // Verify it parsed correctly by re-parsing the exact substring | |||
| 370 | 4 | ipv4_address v4_check; | ||
| 371 | 4 | ec = parse_ipv4_address( | ||
| 372 | 4 | std::string_view(it, static_cast<std::size_t>(v4_it - it)), | ||
| 373 | v4_check); | |||
| 374 |
1/2✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 4 times.
|
4 | if (ec) | |
| 375 | ✗ | return ec; | ||
| 376 | 4 | it = v4_it; | ||
| 377 | 4 | auto const b4 = v4_check.to_bytes(); | ||
| 378 | 4 | bytes[2 * (7 - n) + 0] = b4[0]; | ||
| 379 | 4 | bytes[2 * (7 - n) + 1] = b4[1]; | ||
| 380 | 4 | bytes[2 * (7 - n) + 2] = b4[2]; | ||
| 381 | 4 | bytes[2 * (7 - n) + 3] = b4[3]; | ||
| 382 | 4 | --n; | ||
| 383 | 4 | break; | ||
| 384 | } | |||
| 385 | ||||
| 386 | 60 | auto d = hexdig_value(*it); | ||
| 387 |
3/4✓ Branch 64 → 65 taken 34 times.
✓ Branch 64 → 67 taken 26 times.
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 34 times.
|
60 | if (b != -1 && d < 0) | |
| 388 | { | |||
| 389 | // ends in "::" | |||
| 390 | ✗ | break; | ||
| 391 | } | |||
| 392 | ||||
| 393 |
2/2✓ Branch 67 → 68 taken 59 times.
✓ Branch 67 → 76 taken 1 time.
|
60 | if (!c) | |
| 394 | { | |||
| 395 | 59 | prev = it; | ||
| 396 |
2/2✓ Branch 69 → 70 taken 2 times.
✓ Branch 69 → 71 taken 57 times.
|
59 | if (!parse_h16(it, end, hi, lo)) | |
| 397 | 2 | return std::make_error_code(std::errc::invalid_argument); | ||
| 398 | 57 | bytes[2 * (8 - n) + 0] = hi; | ||
| 399 | 57 | bytes[2 * (8 - n) + 1] = lo; | ||
| 400 | 57 | --n; | ||
| 401 |
2/2✓ Branch 73 → 74 taken 1 time.
✓ Branch 73 → 75 taken 56 times.
|
57 | if (n == 0) | |
| 402 | 1 | break; | ||
| 403 | 56 | c = true; | ||
| 404 | 56 | continue; | ||
| 405 | } | |||
| 406 | ||||
| 407 | // ':' divides a word | |||
| 408 | 1 | return std::make_error_code(std::errc::invalid_argument); | ||
| 409 | 153 | } | ||
| 410 | ||||
| 411 | // Must have consumed entire string | |||
| 412 |
2/2✓ Branch 78 → 79 taken 2 times.
✓ Branch 78 → 80 taken 36 times.
|
38 | if (it != end) | |
| 413 | 2 | return std::make_error_code(std::errc::invalid_argument); | ||
| 414 | ||||
| 415 |
2/2✓ Branch 80 → 81 taken 1 time.
✓ Branch 80 → 83 taken 35 times.
|
36 | if (b == -1) | |
| 416 | { | |||
| 417 | 1 | addr = ipv6_address{bytes}; | ||
| 418 | 1 | return {}; | ||
| 419 | } | |||
| 420 | ||||
| 421 |
2/2✓ Branch 83 → 84 taken 2 times.
✓ Branch 83 → 86 taken 33 times.
|
35 | if (b == n) | |
| 422 | { | |||
| 423 | // "::" last | |||
| 424 | 2 | auto const i = 2 * (7 - n); | ||
| 425 | 2 | std::memset(&bytes[i], 0, 16 - i); | ||
| 426 | } | |||
| 427 |
2/2✓ Branch 86 → 87 taken 19 times.
✓ Branch 86 → 91 taken 14 times.
|
33 | else if (b == 7) | |
| 428 | { | |||
| 429 | // "::" first | |||
| 430 | 19 | auto const i = 2 * (b - n); | ||
| 431 | 19 | std::memmove(&bytes[16 - i], &bytes[2], i); | ||
| 432 | 19 | std::memset(&bytes[0], 0, 16 - i); | ||
| 433 | } | |||
| 434 | else | |||
| 435 | { | |||
| 436 | // "::" in middle | |||
| 437 | 14 | auto const i0 = 2 * (7 - b); | ||
| 438 | 14 | auto const i1 = 2 * (b - n); | ||
| 439 | 14 | std::memmove(&bytes[16 - i1], &bytes[i0 + 2], i1); | ||
| 440 | 14 | std::memset(&bytes[i0], 0, 16 - (i0 + i1)); | ||
| 441 | } | |||
| 442 | ||||
| 443 | 35 | addr = ipv6_address{bytes}; | ||
| 444 | 35 | return {}; | ||
| 445 | } | |||
| 446 | ||||
| 447 | } // namespace boost::corosio | |||
| 448 |