mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-31 10:20:45 +00:00
Improve x509_inet_pton_ipv4 readability
Introduce descriptive variable names. Drop the table of tens. Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
parent
b255e21e48
commit
13b8b780fe
@ -2623,24 +2623,23 @@ static int x509_inet_pton_ipv6(const char *src, void *dst)
|
|||||||
static int x509_inet_pton_ipv4(const char *src, void *dst)
|
static int x509_inet_pton_ipv4(const char *src, void *dst)
|
||||||
{
|
{
|
||||||
/* note: allows leading 0's, e.g. 000.000.000.000 */
|
/* note: allows leading 0's, e.g. 000.000.000.000 */
|
||||||
const unsigned char *v = (const unsigned char *) src;
|
const unsigned char *character = (const unsigned char *) src;
|
||||||
uint8_t *res = (uint8_t *) dst;
|
uint8_t *res = (uint8_t *) dst;
|
||||||
uint8_t d1, d2, d3, i = 0;
|
uint8_t digit1, digit2, digit3, num_octets = 0;
|
||||||
int ii;
|
uint16_t octet;
|
||||||
const uint8_t tens[] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 };
|
|
||||||
do {
|
do {
|
||||||
if ((d1 = *(uint8_t *) v - '0') > 9) {
|
if ((digit1 = *(uint8_t *) character - '0') > 9) {
|
||||||
break;
|
break;
|
||||||
} else if ((d2 = *(uint8_t *) ++v - '0') > 9) {
|
} else if ((digit2 = *(uint8_t *) ++character - '0') > 9) {
|
||||||
*res++ = d1;
|
*res++ = digit1;
|
||||||
} else if ((d3 = *(uint8_t *) ++v - '0') > 9) {
|
} else if ((digit3 = *(uint8_t *) ++character - '0') > 9) {
|
||||||
*res++ = tens[d1] + d2;
|
*res++ = digit1 * 10 + digit2;
|
||||||
} else if ((ii = (d1 < 2 ? d1 == 1 ? 100 : 0 : d1 == 2 ? 200 : 999)
|
} else if ((octet = digit1 * 100 + digit2 * 10 + digit3) < 256) {
|
||||||
+ tens[d2] + d3) < 256) {
|
*res++ = (uint8_t) octet, ++character;
|
||||||
*res++ = (uint8_t) ii, ++v;
|
|
||||||
}
|
}
|
||||||
} while (++i < 4 && *v++ == '.');
|
} while (++num_octets < 4 && *character++ == '.');
|
||||||
return i == 4 && *v == '\0' ? 0 : -1;
|
return num_octets == 4 && *character == '\0' ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user