Add editorconfig
Some checks are pending
isinetaddr / tests (macos-latest) (push) Waiting to run
isinetaddr / tests (ubuntu-latest) (push) Waiting to run

This commit is contained in:
0x1eef 2024-09-12 23:52:24 -03:00
parent 7ac3d7c869
commit eb330f77de
4 changed files with 122 additions and 115 deletions

7
.editorconfig Normal file
View file

@ -0,0 +1,7 @@
[*.c]
indent_style = space
indent_size = 4
[*.h]
indent_style = space
indent_size = 4

View file

@ -9,37 +9,37 @@ static bool in_range(char buf[4], int min, int max);
bool bool
iscidraddr4(const char *str) iscidraddr4(const char *str)
{ {
size_t offset = 0; size_t offset = 0;
size_t len = (str == NULL ? 0 : strnlen(str, 16)); size_t len = (str == NULL ? 0 : strnlen(str, 16));
for(size_t i = 0; i < len; i++) { for(size_t i = 0; i < len; i++) {
if (str[i] == '/') { if (str[i] == '/') {
offset = i; offset = i;
break; break;
}
}
if (offset == 0) {
return isinetaddr4(str);
} else {
char addr[offset], cidr[3];
char *c = (char*)&str[offset + 1];
memcpy(addr, str, offset);
memcpy(cidr, c, 3);
addr[offset] = '\0';
return isinetaddr4(addr) && in_range(cidr, 0, 32);
} }
}
if (offset == 0) {
return isinetaddr4(str);
} else {
char addr[offset], cidr[3];
char *c = (char*)&str[offset + 1];
memcpy(addr, str, offset);
memcpy(cidr, c, 3);
addr[offset] = '\0';
return isinetaddr4(addr) && in_range(cidr, 0, 32);
}
} }
static bool 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 false; 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);
} }
} }

View file

@ -14,40 +14,40 @@ static bool within_range(char buf[MAX_BUFLEN]);
bool bool
isinetaddr4(const char *str) isinetaddr4(const char *str)
{ {
char buf[MAX_BUFLEN + 1]; char buf[MAX_BUFLEN + 1];
int digits = 0, octets = 1, buflen = 0; int digits = 0, octets = 1, buflen = 0;
size_t len = (str == NULL ? 0 : strnlen(str, MAX_STRLEN)); size_t len = (str == NULL ? 0 : strnlen(str, MAX_STRLEN));
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 false; return false;
} else { } else {
buflen = 0; buflen = 0;
} }
} else if (isdigit(str[i])) { } else if (isdigit(str[i])) {
if (buflen == MAX_BUFLEN) { if (buflen == MAX_BUFLEN) {
return false; return false;
} else { } else {
buf[buflen++] = str[i]; buf[buflen++] = str[i];
buf[buflen] = '\0'; buf[buflen] = '\0';
digits++; digits++;
if (!within_range(buf)) { if (!within_range(buf)) {
return false; return false;
} else if (i > 0 && str[i-1] == SEP) { } else if (i > 0 && str[i-1] == SEP) {
octets++; octets++;
}
}
} else {
return false;
} }
}
} else {
return false;
} }
} return octets == MAX_OCTETS && digits <= MAX_DIGITLEN;
return octets == MAX_OCTETS && digits <= MAX_DIGITLEN;
} }
static bool static bool
within_range(char buf[MAX_BUFLEN]) within_range(char buf[MAX_BUFLEN])
{ {
int n = atoi(buf); int n = atoi(buf);
return n >= 0 && n <= 255; return n >= 0 && n <= 255;
} }

View file

@ -17,87 +17,87 @@ static size_t get_offset(char *tail);
bool 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) { if (len == 0) {
return false;
} else if (strncasecmp(str, "::ffff", 6) == 0) {
return isinetaddr4(&str[7]);
}
for (size_t i = 0; i < len; i++) {
if (has_consecutive_chars(&str[i], SEP, 3)) {
return false;
} else if (has_consecutive_chars(&str[i], SEP, 2)) {
if (i == 0 && isinetaddr4(&str[3])) {
return true;
} else {
char new_str[MAX_STRLEN];
return isinetaddr6(expand(str, len, new_str, i));
}
} else if (str[i] == SEP) {
digitlen = 0;
} else if (isxdigit(str[i])) {
digitlen++;
hexdigits++;
if (digitlen > MAX_DIGITLEN) {
return false; return false;
} else if (str[i-1] == SEP) { } else if (strncasecmp(str, "::ffff", 6) == 0) {
hextets++; return isinetaddr4(&str[7]);
}
} else {
return false;
} }
} for (size_t i = 0; i < len; i++) {
return hextets == MAX_HEXTETS && hexdigits <= MAX_HEXDIGITS; if (has_consecutive_chars(&str[i], SEP, 3)) {
return false;
} else if (has_consecutive_chars(&str[i], SEP, 2)) {
if (i == 0 && isinetaddr4(&str[3])) {
return true;
} else {
char new_str[MAX_STRLEN];
return isinetaddr6(expand(str, len, new_str, i));
}
} else if (str[i] == SEP) {
digitlen = 0;
} else if (isxdigit(str[i])) {
digitlen++;
hexdigits++;
if (digitlen > MAX_DIGITLEN) {
return false;
} else if (str[i-1] == SEP) {
hextets++;
}
} else {
return false;
}
}
return hextets == MAX_HEXTETS && hexdigits <= MAX_HEXDIGITS;
} }
static bool 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++) {
if (*str++ != c) { if (*str++ != c) {
return 0; return 0;
}
} }
} return 1;
return 1;
} }
static char* static char*
expand(const char *str, size_t strlen, char *new_str, size_t headlen) expand(const char *str, size_t strlen, char *new_str, size_t headlen)
{ {
char *ptr = new_str; char *ptr = new_str;
char *tail = (char*) &str[headlen + 2]; char *tail = (char*) &str[headlen + 2];
size_t taillen = (strlen - headlen) - 2; size_t taillen = (strlen - headlen) - 2;
size_t bodylen = MAX_STRLEN - taillen; size_t bodylen = MAX_STRLEN - taillen;
size_t i = headlen + 2; size_t i = headlen + 2;
size_t j = headlen; size_t j = headlen;
size_t offset = get_offset(tail); size_t offset = get_offset(tail);
while (i++ < strlen) { while (i++ < strlen) {
if (has_consecutive_chars(&str[i], SEP, 2)) { if (has_consecutive_chars(&str[i], SEP, 2)) {
return NULL; return NULL;
}
} }
} memcpy(ptr, &str[0], headlen);
memcpy(ptr, &str[0], headlen); ptr += headlen;
ptr += headlen; while (++j < bodylen-offset) {
while (++j < bodylen-offset) { *ptr++ = j % 5 == 0 ? ':' : '0';
*ptr++ = j % 5 == 0 ? ':' : '0'; }
} memcpy(ptr, tail, taillen);
memcpy(ptr, tail, taillen); return new_str;
return new_str;
} }
static size_t static size_t
get_offset(char *tail) get_offset(char *tail)
{ {
size_t offset = 0; size_t offset = 0;
while (*tail++ != SEP) { while (*tail++ != SEP) {
offset++; offset++;
if (*tail == '\0' || offset == 4) { if (*tail == '\0' || offset == 4) {
offset = 0; offset = 0;
break; break;
}
} }
} return offset;
return offset;
} }