END_TEST START_TEST(tc_pico_is_digit) { fail_if(pico_is_digit('a')); fail_if(pico_is_digit('Z')); fail_if(pico_is_digit('\0')); fail_if(pico_is_digit('\n')); fail_if(pico_is_digit('0' - 1)); fail_if(pico_is_digit('9' + 1)); fail_unless(pico_is_digit('0')); fail_unless(pico_is_digit('9')); }
static int pico_dns_client_mirror(char *ptr) { unsigned char buf[4] = {0}; char *m; int cnt = 0; int p, i; if (!ptr) return -1; m = ptr; while ((p = *ptr++) != 0) { if (pico_is_digit(p)) { buf[cnt] = (10 * buf[cnt]) + (p - '0'); } else if (p == '.') { cnt++; } else { return -1; } } /* Handle short notation */ if(cnt == 1){ buf[3] = buf[1]; buf[1] = 0; buf[2] = 0; }else if (cnt == 2){ buf[3] = buf[2]; buf[2] = 0; }else if(cnt != 3){ /* String could not be parsed, return error */ return -1; } ptr = m; for(i = 3; i >= 0; i--) { if(buf[i] > 99){ *ptr++ = '0' + (buf[i] / 100); *ptr++ = '0' + ((buf[i] % 100) / 10); *ptr++ = '0' + ((buf[i] % 100) % 10); }else if(buf[i] > 9){ *ptr++ = '0' + (buf[i] / 10); *ptr++ = '0' + (buf[i] % 10); }else{ *ptr++ = '0' + buf[i]; } if(i > 0) *ptr++ = '.'; } return 0; }
int pico_string_to_ipv4(const char *ipstr, uint32_t *ip) { unsigned char buf[PICO_SIZE_IP4] = { 0 }; int cnt = 0; char p; if (pico_string_check_null_args(ipstr, ip) < 0) return -1; while((p = *ipstr++) != 0 && cnt < PICO_SIZE_IP4) { if (pico_is_digit(p)) { buf[cnt] = (uint8_t)((10 * buf[cnt]) + (p - '0')); } else if (p == '.') { cnt++; } else { return -1; } } /* Handle short notation */ if (cnt == 1) { buf[3] = buf[1]; buf[1] = 0; buf[2] = 0; } else if (cnt == 2) { buf[3] = buf[2]; buf[2] = 0; } else if (cnt != 3) { /* String could not be parsed, return error */ return -1; } *ip = long_from(buf); return 0; }