diff options
author | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-03-30 10:13:18 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-03-30 16:19:50 +0200 |
commit | 049fa77cc447fc4919df0fe36132b1fea95a9c77 (patch) | |
tree | e9ab6e5307f64f214a9ebfadfd855c6e4c58c969 | |
parent | f610c3ad12dedc04261ab4ff0a15798f0c22e65f (diff) | |
download | gnutls-049fa77cc447fc4919df0fe36132b1fea95a9c77.tar.gz |
x509/output: simplified cidr_to_string()
-rw-r--r-- | lib/x509/output.c | 37 |
1 files changed, 4 insertions, 33 deletions
diff --git a/lib/x509/output.c b/lib/x509/output.c index 7728bf5847..2ccb68d9dd 100644 --- a/lib/x509/output.c +++ b/lib/x509/output.c @@ -88,39 +88,10 @@ char *ip_to_string(void *_ip, int ip_size, char *string, #endif } -static int bit_count(uint32_t i) -{ - int c = 0; - unsigned int seen_one = 0; - - while (i > 0) { - if (i & 1) { - seen_one = 1; - c++; - } else { - if (seen_one) { - return -1; - } - } - i >>= 1; - } - - return c; -} - -static unsigned mask_to_prefix(uint8_t mask[4]) -{ - uint32_t m; - - memcpy(&m, mask, 4); - m = ntohl(m); - return bit_count(m); -} - -static unsigned mask6_to_prefix(uint8_t mask[16]) +static unsigned mask_to_prefix(const uint8_t *mask, unsigned mask_size) { unsigned i, c = 0; - for (i=0; i<16; i++) { + for (i=0; i<mask_size; i++) { if (mask[i] == 0xFF) { c += 8; } else { @@ -160,12 +131,12 @@ char *cidr_to_string(void *_ip, int ip_size, char *string, p = inet_ntop(AF_INET, ip, tmp, sizeof(tmp)); if (p) - snprintf(string, string_size, "%s/%u", tmp, mask_to_prefix(ip+4)); + snprintf(string, string_size, "%s/%u", tmp, mask_to_prefix(ip+4, 4)); } else { p = inet_ntop(AF_INET6, ip, tmp, sizeof(tmp)); if (p) - snprintf(string, string_size, "%s/%u", tmp, mask6_to_prefix(ip+16)); + snprintf(string, string_size, "%s/%u", tmp, mask_to_prefix(ip+16, 16)); } if (p == NULL) |