123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include <string.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <math.h>
- #include <stddef.h>
- #include "utils.h"
- uint8_t ICACHE_FLASH_ATTR UTILS_IsIPV4 (int8_t *str)
- {
- uint8_t segs = 0;
- uint8_t chcnt = 0;
- uint8_t accum = 0;
-
- if (str == 0)
- return 0;
-
- while (*str != '\0') {
-
- if (*str == '.') {
-
- if (chcnt == 0)
- return 0;
-
- if (++segs == 4)
- return 0;
-
- chcnt = accum = 0;
- str++;
- continue;
- }
-
- if ((*str < '0') || (*str > '9'))
- return 0;
-
- if ((accum = accum * 10 + *str - '0') > 255)
- return 0;
-
- chcnt++;
- str++;
- }
-
- if (segs != 3)
- return 0;
- if (chcnt == 0)
- return 0;
-
- return 1;
- }
- uint8_t ICACHE_FLASH_ATTR UTILS_StrToIP(const int8_t* str, void *ip)
- {
-
- int i;
-
- const char * start;
- start = str;
- for (i = 0; i < 4; i++) {
-
- char c;
-
- int n = 0;
- while (1) {
- c = * start;
- start++;
- if (c >= '0' && c <= '9') {
- n *= 10;
- n += c - '0';
- }
-
- else if ((i < 3 && c == '.') || i == 3) {
- break;
- }
- else {
- return 0;
- }
- }
- if (n >= 256) {
- return 0;
- }
- ((uint8_t*)ip)[i] = n;
- }
- return 1;
- }
- uint32_t ICACHE_FLASH_ATTR UTILS_Atoh(const int8_t *s)
- {
- uint32_t value = 0, digit;
- int8_t c;
- while ((c = *s++)) {
- if ('0' <= c && c <= '9')
- digit = c - '0';
- else if ('A' <= c && c <= 'F')
- digit = c - 'A' + 10;
- else if ('a' <= c && c <= 'f')
- digit = c - 'a' + 10;
- else break;
- value = (value << 4) | digit;
- }
- return value;
- }
|