src/corosio/src/ipv4_address.cpp
99.1% Lines (108 / 109)
85.9% Branches (55 / 64)
100.0% Functions (15 / 15)
Functions (15)
Function
Calls
Lines
Branches
Blocks
boost::corosio::ipv4_address::ipv4_address(unsigned int)
:17
23736x
100.0%
–
100.0%
boost::corosio::ipv4_address::ipv4_address(std::__1::array<unsigned char, 4ul> const&)
:19
79763x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_bytes() const
:28
18688x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_uint() const
:39
25x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_string() const
:45
24x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_buffer(char*, unsigned long) const
:53
8x
83.3%
75.0%
66.0%
boost::corosio::ipv4_address::is_loopback() const
:62
9x
100.0%
–
100.0%
boost::corosio::ipv4_address::is_unspecified() const
:68
8x
100.0%
–
100.0%
boost::corosio::ipv4_address::is_multicast() const
:74
6x
100.0%
–
100.0%
boost::corosio::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, boost::corosio::ipv4_address const&)
:80
2x
100.0%
–
100.0%
boost::corosio::ipv4_address::print_impl(char*) const
:88
31x
100.0%
–
83.0%
boost::corosio::ipv4_address::print_impl(char*) const::$_0::operator()(char*&, unsigned char) const
:91
124x
100.0%
66.7%
100.0%
boost::corosio::(anonymous namespace)::parse_dec_octet(char const*&, char const*, unsigned char&)
:121
470x
100.0%
88.2%
100.0%
boost::corosio::parse_ipv4_impl(std::__1::basic_string_view<char, std::__1::char_traits<char>>, boost::corosio::ipv4_address&)
:169
164x
100.0%
100.0%
100.0%
boost::corosio::make_ipv4_address(std::__1::basic_string_view<char, std::__1::char_traits<char>>)
:202
164x
100.0%
100.0%
100.0%
| 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/ipv4_address.hpp> | |||
| 11 | ||||
| 12 | #include <ostream> | |||
| 13 | #include <stdexcept> | |||
| 14 | ||||
| 15 | namespace boost::corosio { | |||
| 16 | ||||
| 17 | 23736x | ipv4_address::ipv4_address(uint_type u) noexcept : addr_(u) {} | ||
| 18 | ||||
| 19 | 79763x | ipv4_address::ipv4_address(bytes_type const& bytes) noexcept | ||
| 20 | 39881x | { | ||
| 21 | 119646x | addr_ = (static_cast<std::uint32_t>(bytes[0]) << 24) | | ||
| 22 | 79764x | (static_cast<std::uint32_t>(bytes[1]) << 16) | | ||
| 23 | 79764x | (static_cast<std::uint32_t>(bytes[2]) << 8) | | ||
| 24 | 39882x | (static_cast<std::uint32_t>(bytes[3])); | ||
| 25 | 39881x | } | ||
| 26 | ||||
| 27 | auto | |||
| 28 | 18688x | ipv4_address::to_bytes() const noexcept -> bytes_type | ||
| 29 | { | |||
| 30 | bytes_type bytes; | |||
| 31 | 18688x | bytes[0] = static_cast<unsigned char>((addr_ >> 24) & 0xff); | ||
| 32 | 18688x | bytes[1] = static_cast<unsigned char>((addr_ >> 16) & 0xff); | ||
| 33 | 18688x | bytes[2] = static_cast<unsigned char>((addr_ >> 8) & 0xff); | ||
| 34 | 18688x | bytes[3] = static_cast<unsigned char>(addr_ & 0xff); | ||
| 35 | 18688x | return bytes; | ||
| 36 | } | |||
| 37 | ||||
| 38 | auto | |||
| 39 | 25x | ipv4_address::to_uint() const noexcept -> uint_type | ||
| 40 | { | |||
| 41 | 25x | return addr_; | ||
| 42 | } | |||
| 43 | ||||
| 44 | std::string | |||
| 45 | 24x | ipv4_address::to_string() const | ||
| 46 | { | |||
| 47 | char buf[max_str_len]; | |||
| 48 | 24x | auto n = print_impl(buf); | ||
| 49 | 24x | return std::string(buf, n); | ||
| 50 | } | |||
| 51 | ||||
| 52 | std::string_view | |||
| 53 | 8x | ipv4_address::to_buffer(char* dest, std::size_t dest_size) const | ||
| 54 | { | |||
| 55 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
|
8x | if (dest_size < max_str_len) | |
| 56 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | throw std::length_error("buffer too small for IPv4 address"); | |
| 57 | 7x | auto n = print_impl(dest); | ||
| 58 | 7x | return std::string_view(dest, n); | ||
| 59 | ✗ | } | ||
| 60 | ||||
| 61 | bool | |||
| 62 | 9x | ipv4_address::is_loopback() const noexcept | ||
| 63 | { | |||
| 64 | 9x | return (addr_ & 0xFF000000) == 0x7F000000; | ||
| 65 | } | |||
| 66 | ||||
| 67 | bool | |||
| 68 | 8x | ipv4_address::is_unspecified() const noexcept | ||
| 69 | { | |||
| 70 | 8x | return addr_ == 0; | ||
| 71 | } | |||
| 72 | ||||
| 73 | bool | |||
| 74 | 6x | ipv4_address::is_multicast() const noexcept | ||
| 75 | { | |||
| 76 | 6x | return (addr_ & 0xF0000000) == 0xE0000000; | ||
| 77 | } | |||
| 78 | ||||
| 79 | std::ostream& | |||
| 80 | 2x | operator<<(std::ostream& os, ipv4_address const& addr) | ||
| 81 | { | |||
| 82 | char buf[ipv4_address::max_str_len]; | |||
| 83 | 2x | os << addr.to_buffer(buf, sizeof(buf)); | ||
| 84 | 2x | return os; | ||
| 85 | } | |||
| 86 | ||||
| 87 | std::size_t | |||
| 88 | 31x | ipv4_address::print_impl(char* dest) const noexcept | ||
| 89 | { | |||
| 90 | 31x | auto const start = dest; | ||
| 91 | 155x | auto const write = [](char*& dest, unsigned char v) { | ||
| 92 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 80 times.
|
124x | if (v >= 100) | |
| 93 | { | |||
| 94 | 44x | *dest++ = '0' + v / 100; | ||
| 95 | 44x | v %= 100; | ||
| 96 | 44x | *dest++ = '0' + v / 10; | ||
| 97 | 44x | v %= 10; | ||
| 98 | 44x | } | ||
| 99 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 5 times.
|
80x | else if (v >= 10) | |
| 100 | { | |||
| 101 | 5x | *dest++ = '0' + v / 10; | ||
| 102 | 5x | v %= 10; | ||
| 103 | 5x | } | ||
| 104 | 124x | *dest++ = '0' + v; | ||
| 105 | 124x | }; | ||
| 106 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>((addr_ >> 24) & 0xff)); | |
| 107 | 31x | *dest++ = '.'; | ||
| 108 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>((addr_ >> 16) & 0xff)); | |
| 109 | 31x | *dest++ = '.'; | ||
| 110 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>((addr_ >> 8) & 0xff)); | |
| 111 | 31x | *dest++ = '.'; | ||
| 112 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>(addr_ & 0xff)); | |
| 113 | 31x | return static_cast<std::size_t>(dest - start); | ||
| 114 | } | |||
| 115 | ||||
| 116 | namespace { | |||
| 117 | ||||
| 118 | // Parse a decimal octet (0-255), no leading zeros except "0" | |||
| 119 | // Returns true on success, advances `it` | |||
| 120 | bool | |||
| 121 | 470x | parse_dec_octet(char const*& it, char const* end, unsigned char& octet) noexcept | ||
| 122 | { | |||
| 123 |
2/2✓ Branch 0 taken 466 times.
✓ Branch 1 taken 4 times.
|
470x | if (it == end) | |
| 124 | 4x | return false; | ||
| 125 | ||||
| 126 | 466x | char c = *it; | ||
| 127 |
4/4✓ Branch 0 taken 461 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 421 times.
|
466x | if (c < '0' || c > '9') | |
| 128 | 45x | return false; | ||
| 129 | ||||
| 130 | 421x | unsigned v = static_cast<unsigned>(c - '0'); | ||
| 131 | 421x | ++it; | ||
| 132 | ||||
| 133 |
2/2✓ Branch 0 taken 363 times.
✓ Branch 1 taken 58 times.
|
421x | if (v == 0) | |
| 134 | { | |||
| 135 | // "0" is valid, but "00", "01", etc. are not | |||
| 136 |
5/6✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 53 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
58x | if (it != end && *it >= '0' && *it <= '9') | |
| 137 | 3x | return false; | ||
| 138 | 55x | octet = 0; | ||
| 139 | 55x | return true; | ||
| 140 | } | |||
| 141 | ||||
| 142 | // First digit was 1-9, can have more | |||
| 143 |
6/6✓ Branch 0 taken 280 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 156 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 118 times.
|
363x | if (it != end && *it >= '0' && *it <= '9') | |
| 144 | { | |||
| 145 | 118x | v = v * 10 + static_cast<unsigned>(*it - '0'); | ||
| 146 | 118x | ++it; | ||
| 147 | ||||
| 148 |
4/6✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 115 times.
|
118x | if (it != end && *it >= '0' && *it <= '9') | |
| 149 | { | |||
| 150 | 115x | v = v * 10 + static_cast<unsigned>(*it - '0'); | ||
| 151 | 115x | ++it; | ||
| 152 | ||||
| 153 | // Can't have more than 3 digits | |||
| 154 |
5/6✓ Branch 0 taken 109 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 106 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
115x | if (it != end && *it >= '0' && *it <= '9') | |
| 155 | 3x | return false; | ||
| 156 | 112x | } | ||
| 157 | 115x | } | ||
| 158 | ||||
| 159 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 351 times.
|
360x | if (v > 255) | |
| 160 | 9x | return false; | ||
| 161 | ||||
| 162 | 351x | octet = static_cast<unsigned char>(v); | ||
| 163 | 351x | return true; | ||
| 164 | 470x | } | ||
| 165 | ||||
| 166 | } // namespace | |||
| 167 | ||||
| 168 | static std::error_code | |||
| 169 | 164x | parse_ipv4_impl(std::string_view s, ipv4_address& addr) noexcept | ||
| 170 | { | |||
| 171 | 164x | auto it = s.data(); | ||
| 172 | 164x | auto const end = it + s.size(); | ||
| 173 | ||||
| 174 | unsigned char octets[4]; | |||
| 175 | ||||
| 176 | // Parse first octet | |||
| 177 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 109 times.
|
164x | if (!parse_dec_octet(it, end, octets[0])) | |
| 178 | 55x | return std::make_error_code(std::errc::invalid_argument); | ||
| 179 | ||||
| 180 | // Parse remaining octets | |||
| 181 |
2/2✓ Branch 0 taken 315 times.
✓ Branch 1 taken 91 times.
|
406x | for (int i = 1; i < 4; ++i) | |
| 182 | { | |||
| 183 |
4/4✓ Branch 0 taken 307 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 306 times.
|
315x | if (it == end || *it != '.') | |
| 184 | 9x | return std::make_error_code(std::errc::invalid_argument); | ||
| 185 | 306x | ++it; // skip '.' | ||
| 186 | ||||
| 187 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 297 times.
|
306x | if (!parse_dec_octet(it, end, octets[i])) | |
| 188 | 9x | return std::make_error_code(std::errc::invalid_argument); | ||
| 189 | 297x | } | ||
| 190 | ||||
| 191 | // Must have consumed entire string | |||
| 192 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 81 times.
|
91x | if (it != end) | |
| 193 | 10x | return std::make_error_code(std::errc::invalid_argument); | ||
| 194 | ||||
| 195 | 81x | addr = ipv4_address( | ||
| 196 | 81x | ipv4_address::bytes_type{{octets[0], octets[1], octets[2], octets[3]}}); | ||
| 197 | ||||
| 198 | 81x | return {}; | ||
| 199 | 164x | } | ||
| 200 | ||||
| 201 | capy::results<ipv4_address> | |||
| 202 | 164x | make_ipv4_address(std::string_view s) noexcept | ||
| 203 | { | |||
| 204 | 164x | ipv4_address addr; | ||
| 205 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 81 times.
|
164x | if (auto ec = parse_ipv4_impl(s, addr)) | |
| 206 | 83x | return {ec, ipv4_address{}}; | ||
| 207 | 81x | return {std::error_code{}, addr}; | ||
| 208 | 164x | } | ||
| 209 | ||||
| 210 | } // namespace boost::corosio | |||
| 211 |