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
iscidraddr4(const char *str)
{
size_t offset = 0;
size_t len = (str == NULL ? 0 : strnlen(str, 16));
size_t offset = 0;
size_t len = (str == NULL ? 0 : strnlen(str, 16));
for(size_t i = 0; i < len; i++) {
if (str[i] == '/') {
offset = i;
break;
for(size_t i = 0; i < len; i++) {
if (str[i] == '/') {
offset = i;
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
in_range(char buf[4], int min, int max)
{
char *err;
long r;
errno = 0;
if (strnlen(buf, 1) == 0) {
return false;
} else {
r = strtol(buf, &err, 10);
return *err == '\0' && errno == 0 && (r >= min && r <= max);
}
char *err;
long r;
errno = 0;
if (strnlen(buf, 1) == 0) {
return false;
} else {
r = strtol(buf, &err, 10);
return *err == '\0' && errno == 0 && (r >= min && r <= max);
}
}

View file

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

View file

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