From ea3e71fa37f5e9934cb9222809d8a425303f28b3 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Tue, 18 Apr 2023 04:39:12 -0400 Subject: [PATCH] Further refactor IPv4 parsing Make it more readable Signed-off-by: Andrzej Kurek --- library/x509_crt.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/library/x509_crt.c b/library/x509_crt.c index 980858a58..d094ae0f8 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -2638,20 +2638,28 @@ static int x509_inet_pton_ipv4(const char *src, void *dst) /* note: allows leading 0's, e.g. 000.000.000.000 */ const unsigned char *p = (const unsigned char *) src; uint8_t *res = (uint8_t *) dst; - uint8_t digit1, digit2, digit3, num_octets = 0; + uint8_t digit, num_digits = 0; + uint8_t num_octets = 0; uint16_t octet; do { - if ((digit1 = *(uint8_t *) p - '0') > 9) { + octet = num_digits = 0; + do { + digit = *p - '0'; + if (digit > 9) { + break; + } + octet = octet * 10 + digit; + num_digits++; + p++; + } while (num_digits < 3); + + if (octet >= 256 || num_digits > 3 || num_digits == 0) { break; - } else if ((digit2 = *(uint8_t *) ++p - '0') > 9) { - *res++ = digit1; - } else if ((digit3 = *(uint8_t *) ++p - '0') > 9) { - *res++ = digit1 * 10 + digit2; - } else if ((octet = digit1 * 100 + digit2 * 10 + digit3) < 256) { - *res++ = (uint8_t) octet, ++p; } - } while (++num_octets < 4 && *p++ == '.'); + *res++ = (uint8_t) octet; + num_octets++; + } while (num_octets < 4 && *p++ == '.'); return num_octets == 4 && *p == '\0' ? 0 : -1; }