Skip to content

Commit 2725466

Browse files
committed
Fix bugs in punycode and IP address processing
1 parent 3305f17 commit 2725466

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

include/skyr/domain/punycode.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,13 @@ constexpr inline auto punycode_decode(StringView input, std::u32string* output)
164164
constexpr auto zero = '0';
165165
constexpr auto upper_a = 'A';
166166
constexpr auto lower_a = 'a';
167+
constexpr auto alphabet_size = 0x1aul;
167168

168169
if ((cp - zero) < 0x10) {
169-
return static_cast<std::uint32_t>(cp - zero);
170-
} else if ((cp - upper_a) < 0x1a) {
170+
return static_cast<std::uint32_t>(cp - zero + alphabet_size);
171+
} else if ((cp - upper_a) < alphabet_size) {
171172
return static_cast<std::uint32_t>(cp - upper_a);
172-
} else if ((cp - lower_a) < 0x1a) {
173+
} else if ((cp - lower_a) < alphabet_size) {
173174
return static_cast<std::uint32_t>(cp - lower_a);
174175
}
175176
return base;

include/skyr/network/ipv4_address.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ class ipv4_address {
5858
/// The address in bytes in network byte order
5959
/// \returns The address in bytes
6060
[[nodiscard]] constexpr auto to_bytes() const noexcept -> std::array<unsigned char, 4> {
61-
return {{static_cast<unsigned char>(address_ >> 24u), static_cast<unsigned char>(address_ >> 16u),
62-
static_cast<unsigned char>(address_ >> 8u), static_cast<unsigned char>(address_)}};
61+
auto addr = address();
62+
return {{static_cast<unsigned char>(addr >> 24u), static_cast<unsigned char>(addr >> 16u),
63+
static_cast<unsigned char>(addr >> 8u), static_cast<unsigned char>(addr)}};
6364
}
6465

6566
/// \returns The address as a string
@@ -70,7 +71,7 @@ class ipv4_address {
7071
constexpr auto separator = [](auto i) { return (i < 4) ? "."sv : ""sv; };
7172

7273
auto output = ""s;
73-
auto n = address_;
74+
auto n = address();
7475
for (auto i = 1U; i <= 4U; ++i) {
7576
output = std::format("{}{}{}", separator(i), n % 256, output);
7677
n >>= 8;

include/skyr/network/ipv6_address.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ class ipv6_address {
5757
[[nodiscard]] constexpr auto to_bytes() const noexcept -> std::array<unsigned char, 16> {
5858
std::array<unsigned char, 16> bytes{};
5959
for (auto i = 0UL; i < address_.size(); ++i) {
60-
bytes[i * 2] = static_cast<unsigned char>(address_[i] >> 8u); // NOLINT
61-
bytes[i * 2 + 1] = static_cast<unsigned char>(address_[i]); // NOLINT
60+
auto piece = from_network_byte_order(address_[i]);
61+
bytes[i * 2] = static_cast<unsigned char>(piece >> 8u); // NOLINT
62+
bytes[i * 2 + 1] = static_cast<unsigned char>(piece); // NOLINT
6263
}
6364
return bytes;
6465
}
@@ -68,13 +69,19 @@ class ipv6_address {
6869
using namespace std::string_literals;
6970
using namespace std::string_view_literals;
7071

72+
// Convert address to host byte order for processing
73+
auto address = std::array<unsigned short, 8>{};
74+
for (auto i = 0UL; i < address_.size(); ++i) {
75+
address[i] = from_network_byte_order(address_[i]); // NOLINT
76+
}
77+
7178
auto output = ""s;
7279
auto compress = std::optional<std::size_t>();
7380

7481
auto sequences = static_vector<std::pair<std::size_t, std::size_t>, 8>{};
7582
auto in_sequence = false;
7683

77-
auto first = std::cbegin(address_), last = std::cend(address_);
84+
auto first = std::cbegin(address), last = std::cend(address);
7885
auto it = first;
7986
while (true) {
8087
if (*it == 0) {
@@ -114,7 +121,7 @@ class ipv6_address {
114121

115122
auto ignore0 = false;
116123
for (auto i = 0UL; i <= 7UL; ++i) {
117-
if (ignore0 && (address_[i] == 0)) { // NOLINT
124+
if (ignore0 && (address[i] == 0)) { // NOLINT
118125
continue;
119126
} else if (ignore0) {
120127
ignore0 = false;
@@ -129,7 +136,7 @@ class ipv6_address {
129136

130137
constexpr auto separator = [](auto i) { return (i != 7) ? ":"sv : ""sv; };
131138

132-
output += std::format("{:x}{}", address_[i], separator(i)); // NOLINT
139+
output += std::format("{:x}{}", address[i], separator(i)); // NOLINT
133140
}
134141

135142
return output;

0 commit comments

Comments
 (0)