From eb330f77deda4a6c2e0e4e3e4c1fc0b9860e838d Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Thu, 12 Sep 2024 23:52:24 -0300 Subject: [PATCH] Add editorconfig --- .editorconfig | 7 +++ src/iscidraddr4.c | 52 ++++++++++---------- src/isinetaddr4.c | 56 ++++++++++----------- src/isinetaddr6.c | 122 +++++++++++++++++++++++----------------------- 4 files changed, 122 insertions(+), 115 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2b2b81e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +[*.c] +indent_style = space +indent_size = 4 + +[*.h] +indent_style = space +indent_size = 4 diff --git a/src/iscidraddr4.c b/src/iscidraddr4.c index 806b6ee..6ec6f49 100644 --- a/src/iscidraddr4.c +++ b/src/iscidraddr4.c @@ -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); + } } diff --git a/src/isinetaddr4.c b/src/isinetaddr4.c index 0a5e092..13de124 100644 --- a/src/isinetaddr4.c +++ b/src/isinetaddr4.c @@ -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; } diff --git a/src/isinetaddr6.c b/src/isinetaddr6.c index 036b48d..0a0a968 100644 --- a/src/isinetaddr6.c +++ b/src/isinetaddr6.c @@ -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; }