Utilize stdbool.h
This commit is contained in:
parent
58193daba1
commit
8b94548472
5 changed files with 34 additions and 27 deletions
|
@ -10,8 +10,8 @@ extend [testcases](test/) that help verify safety and correctness.
|
||||||
### IPv4
|
### IPv4
|
||||||
|
|
||||||
The following example demonstrates the `isinetaddr4` function with
|
The following example demonstrates the `isinetaddr4` function with
|
||||||
both valid and invalid inputs. The `isinetaddr4` function returns 1
|
both valid and invalid inputs. The `isinetaddr4` function returns `true`
|
||||||
when the input given is a valid IPv4 address, and otherwise returns 0.
|
when the input given is a valid IPv4 address, and otherwise returns `false`.
|
||||||
|
|
||||||
```C
|
```C
|
||||||
#include <isinetaddr.h>
|
#include <isinetaddr.h>
|
||||||
|
@ -121,8 +121,8 @@ foobar is an invalid IPv4 address.
|
||||||
### IPv6
|
### IPv6
|
||||||
|
|
||||||
The following example demonstrates the `isinetaddr6` function with
|
The following example demonstrates the `isinetaddr6` function with
|
||||||
both valid and invalid inputs. The `isinetaddr6` function returns 1
|
both valid and invalid inputs. The `isinetaddr6` function returns `true`
|
||||||
when the input given is a valid IPv6 address, and otherwise returns 0.
|
when the input given is a valid IPv6 address, and otherwise returns `false`.
|
||||||
|
|
||||||
```C
|
```C
|
||||||
#include <isinetaddr.h>
|
#include <isinetaddr.h>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
int isinetaddr4(const char *str);
|
#include <stdbool.h>
|
||||||
int iscidraddr4(const char *str);
|
bool isinetaddr4(const char *str);
|
||||||
int isinetaddr6(const char *str);
|
bool iscidraddr4(const char *str);
|
||||||
|
bool isinetaddr6(const char *str);
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
static int in_range(char buf[4], int min, int max);
|
static bool in_range(char buf[4], int min, int max);
|
||||||
|
|
||||||
int
|
bool
|
||||||
iscidraddr4(const char *str)
|
iscidraddr4(const char *str)
|
||||||
{
|
{
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
@ -29,14 +30,14 @@ iscidraddr4(const char *str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static bool
|
||||||
in_range(char buf[4], int min, int max)
|
in_range(char buf[4], int min, int max)
|
||||||
{
|
{
|
||||||
char *err;
|
char *err;
|
||||||
long r;
|
long r;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (strnlen(buf, 1) == 0) {
|
if (strnlen(buf, 1) == 0) {
|
||||||
return 0;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
r = strtol(buf, &err, 10);
|
r = strtol(buf, &err, 10);
|
||||||
return *err == '\0' && errno == 0 && (r >= min && r <= max);
|
return *err == '\0' && errno == 0 && (r >= min && r <= max);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <isinetaddr.h>
|
#include <isinetaddr.h>
|
||||||
|
|
||||||
static const int MAX_BUFLEN = 3;
|
static const int MAX_BUFLEN = 3;
|
||||||
|
@ -8,9 +9,9 @@ static const int MAX_OCTETS = 4;
|
||||||
static const int MAX_DIGITLEN = 12;
|
static const int MAX_DIGITLEN = 12;
|
||||||
static const int MAX_STRLEN = 16;
|
static const int MAX_STRLEN = 16;
|
||||||
static const char SEP = '.';
|
static const char SEP = '.';
|
||||||
static int within_range(char buf[MAX_BUFLEN]);
|
static bool within_range(char buf[MAX_BUFLEN]);
|
||||||
|
|
||||||
int
|
bool
|
||||||
isinetaddr4(const char *str)
|
isinetaddr4(const char *str)
|
||||||
{
|
{
|
||||||
char buf[MAX_BUFLEN + 1];
|
char buf[MAX_BUFLEN + 1];
|
||||||
|
@ -21,31 +22,31 @@ isinetaddr4(const char *str)
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
if (str[i] == SEP) {
|
if (str[i] == SEP) {
|
||||||
if (octets == MAX_OCTETS || buflen == 0) {
|
if (octets == MAX_OCTETS || buflen == 0) {
|
||||||
return 0;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
buflen = 0;
|
buflen = 0;
|
||||||
bzero(buf, MAX_BUFLEN);
|
bzero(buf, MAX_BUFLEN);
|
||||||
}
|
}
|
||||||
} else if (isdigit(str[i])) {
|
} else if (isdigit(str[i])) {
|
||||||
if (buflen == MAX_BUFLEN) {
|
if (buflen == MAX_BUFLEN) {
|
||||||
return 0;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
buf[buflen++] = str[i];
|
buf[buflen++] = str[i];
|
||||||
digits++;
|
digits++;
|
||||||
if (!within_range(buf)) {
|
if (!within_range(buf)) {
|
||||||
return 0;
|
return false;
|
||||||
} else if (str[i-1] == SEP) {
|
} else if (str[i-1] == SEP) {
|
||||||
octets++;
|
octets++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return octets == MAX_OCTETS && digits <= MAX_DIGITLEN;
|
return octets == MAX_OCTETS && digits <= MAX_DIGITLEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static bool
|
||||||
within_range(char buf[MAX_BUFLEN])
|
within_range(char buf[MAX_BUFLEN])
|
||||||
{
|
{
|
||||||
int n = atoi(buf);
|
int n = atoi(buf);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <isinetaddr.h>
|
#include <isinetaddr.h>
|
||||||
|
|
||||||
static const int MAX_DIGITLEN = 4;
|
static const int MAX_DIGITLEN = 4;
|
||||||
|
@ -9,24 +10,27 @@ static const int MAX_HEXDIGITS = 32;
|
||||||
static const int MAX_STRLEN = 40;
|
static const int MAX_STRLEN = 40;
|
||||||
static const char SEP = ':';
|
static const char SEP = ':';
|
||||||
|
|
||||||
static int has_consecutive_chars(const char *str, char c, int n);
|
static bool has_consecutive_chars(const char *str, char c, int n);
|
||||||
static char* expand(const char *str, size_t strlen, char *new_str, size_t headlen);
|
static char* expand(const char *str, size_t strlen, char *new_str, size_t headlen);
|
||||||
static size_t get_offset(char *tail);
|
static size_t get_offset(char *tail);
|
||||||
|
|
||||||
int
|
bool
|
||||||
isinetaddr6(const char *str)
|
isinetaddr6(const char *str)
|
||||||
{
|
{
|
||||||
int hextets = 1, digitlen = 0, hexdigits = 0;
|
int hextets = 1, digitlen = 0, hexdigits = 0;
|
||||||
size_t len = (str == NULL ? 0 : strnlen(str, MAX_STRLEN));
|
size_t len = (str == NULL ? 0 : strnlen(str, MAX_STRLEN));
|
||||||
|
|
||||||
|
if (len == 0) {
|
||||||
|
return false;
|
||||||
|
} else if (strncasecmp(str, "::ffff", 6) == 0) {
|
||||||
|
return isinetaddr4(&str[7]);
|
||||||
|
}
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
if (has_consecutive_chars(&str[i], SEP, 3)) {
|
if (has_consecutive_chars(&str[i], SEP, 3)) {
|
||||||
return 0;
|
return false;
|
||||||
} else if (i == 0 && strncasecmp(str, "::ffff", 6) == 0) {
|
|
||||||
return isinetaddr4(&str[7]);
|
|
||||||
} else if (has_consecutive_chars(&str[i], SEP, 2)) {
|
} else if (has_consecutive_chars(&str[i], SEP, 2)) {
|
||||||
if (i == 0 && isinetaddr4(&str[3])) {
|
if (i == 0 && isinetaddr4(&str[3])) {
|
||||||
return 1;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
char new_str[MAX_STRLEN];
|
char new_str[MAX_STRLEN];
|
||||||
return isinetaddr6(expand(str, len, new_str, i));
|
return isinetaddr6(expand(str, len, new_str, i));
|
||||||
|
@ -37,18 +41,18 @@ isinetaddr6(const char *str)
|
||||||
digitlen++;
|
digitlen++;
|
||||||
hexdigits++;
|
hexdigits++;
|
||||||
if (digitlen > MAX_DIGITLEN) {
|
if (digitlen > MAX_DIGITLEN) {
|
||||||
return 0;
|
return false;
|
||||||
} else if (str[i-1] == SEP) {
|
} else if (str[i-1] == SEP) {
|
||||||
hextets++;
|
hextets++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hextets == MAX_HEXTETS && hexdigits <= MAX_HEXDIGITS;
|
return hextets == MAX_HEXTETS && hexdigits <= MAX_HEXDIGITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static bool
|
||||||
has_consecutive_chars(const char *str, char c, int n)
|
has_consecutive_chars(const char *str, char c, int n)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
|
|
Loading…
Reference in a new issue