Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing shortened ipv4 addresses #218

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions groups/nts/ntsa/ntsa_ipv4address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
BSLS_IDENT_RCSID(ntsa_ipv4address_cpp, "$Id$ $CSID$")

#include <bslim_printer.h>
#include <bsl_cmath.h>
#include <bsl_cstdlib.h>
#include <bsl_cstring.h>
#include <bsl_limits.h>
#include <bsl_sstream.h>


namespace BloombergLP {
namespace ntsa {

Expand Down Expand Up @@ -90,22 +93,25 @@ bool Ipv4Address::parse(const bslstl::StringRef& text) NTSCFG_NOEXCEPT
Representation newValue;
newValue.d_asDword = 0;

bsl::uint32_t byteAtIndex = 0;
bsl::uint64_t numAtIndex = 0;

while (current != end) {
const char ch = *current;
if (NTSCFG_LIKELY(ch >= '0' && ch <= '9')) {
byteAtIndex *= 10;
byteAtIndex += ch - '0';
numAtIndex *= 10;
numAtIndex += ch - '0';
if (numAtIndex > bsl::numeric_limits<bsl::uint32_t>::max()) {
return false;
}
}
else {
if (ch == '.') {
if (byteAtIndex > 255) {
if (numAtIndex > 255) {
return false;
}
newValue.d_asBytes[index] =
NTSCFG_WARNING_NARROW(bsl::uint8_t, byteAtIndex);
byteAtIndex = 0;
NTSCFG_WARNING_NARROW(bsl::uint8_t, numAtIndex);
numAtIndex = 0;
++index;
if (index > 3) {
return false;
Expand All @@ -119,17 +125,18 @@ bool Ipv4Address::parse(const bslstl::StringRef& text) NTSCFG_NOEXCEPT
++current;
}

if (index != 3) {
bsl::uint64_t numOctetsLeft = 4 - index;
if (numAtIndex >= bsl::pow(256,numOctetsLeft)) {
che2 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

if (byteAtIndex > 255) {
return false;
for (size_t oct = 0; oct < numOctetsLeft; ++oct) {
bsl::uint8_t octet = NTSCFG_WARNING_NARROW(bsl::uint8_t,
numAtIndex % 256);
newValue.d_asBytes[3 - oct] = octet;
numAtIndex /= 256;
}

newValue.d_asBytes[index] =
NTSCFG_WARNING_NARROW(bsl::uint8_t, byteAtIndex);

d_value.d_asDword = newValue.d_asDword;

return true;
Expand Down
14 changes: 14 additions & 0 deletions groups/nts/ntsa/ntsa_ipv4address.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ void Ipv4AddressTest::verifyParsing()
{ "x.2.3.4.5", {0x00, 0x00, 0x00, 0x00}, false},
{ "256.256.256.256", {0x00, 0x00, 0x00, 0x00}, false},
{"9999.9999.9999.9999", {0x00, 0x00, 0x00, 0x00}, false},

{ "127.1", {0x7F, 0x00, 0x00, 0x01}, true},
{ "127.168.257", {0x7F, 0xA8, 0x01, 0x01}, true},
{ "127.2.1", {0x7F, 0x02, 0x00, 0x01}, true},
{ "199.11315476", {0xC7, 0xAC, 0xA9, 0x14}, true},
{ "255.16777215", {0xFF, 0xFF, 0xFF, 0xFF}, true},
{ "255.255.65535", {0xFF, 0xFF, 0xFF, 0xFF}, true},
{ "1", {0x00, 0x00, 0x00, 0x01}, true},
{ "4294967295", {0xFF, 0xFF, 0xFF, 0xFF}, true},
{ "4294967296", {0x00, 0x00, 0x00, 0x00}, false},
{ "256.1", {0x00, 0x00, 0x00, 0x00}, false},
{ "255.16777216", {0x00, 0x00, 0x00, 0x00}, false},
{ "255.255.65536", {0xFF, 0xFF, 0xFF, 0xFF}, false},
{ "99999999999999999", {0x00, 0x00, 0x00, 0x00}, false},
};

enum { NUM_DATA = sizeof(DATA) / sizeof(DATA[0]) };
Expand Down
Loading