int cli_realhex2ui(const char *hex, uint16_t *ptr, unsigned int len) { uint16_t val; unsigned int i; int c; for(i = 0; i < len; i += 2) { val = 0; if(hex[i] == '?' && hex[i + 1] == '?') { val |= CLI_MATCH_IGNORE; } else if(hex[i + 1] == '?') { if((c = cli_hex2int(hex[i])) >= 0) { val = c << 4; } else { return 0; } val |= CLI_MATCH_NIBBLE_HIGH; } else if(hex[i] == '?') { if((c = cli_hex2int(hex[i + 1])) >= 0) { val = c; } else { return 0; } val |= CLI_MATCH_NIBBLE_LOW; } else if(hex[i] == '(') { val |= CLI_MATCH_SPECIAL; } else { if((c = cli_hex2int(hex[i])) >= 0) { val = c; if((c = cli_hex2int(hex[i+1])) >= 0) { val = (val << 4) + c; } else { return 0; } } else { return 0; } } *ptr++ = val; } return 1; }
short int *cli_hex2si(const char *hex) { short int *str, *ptr, val, c; int i, len; len = strlen(hex); if(len % 2 != 0) { cli_errmsg("cli_hex2si(): Malformed hexstring: %s (length: %d)\n", hex, len); return NULL; } str = cli_calloc((len / 2) + 1, sizeof(short int)); if(!str) return NULL; ptr = str; for(i = 0; i < len; i += 2) { if(hex[i] == '?') { val = CLI_IGN; } else if(hex[i] == '@') { val = CLI_ALT; } else { if((c = cli_hex2int(hex[i])) >= 0) { val = c; if((c = cli_hex2int(hex[i+1])) >= 0) { val = (val << 4) + c; } else { free(str); return NULL; } } else { free(str); return NULL; } } *ptr++ = val; } return str; }
int cli_hex2str_to(const char *hex, char *ptr, size_t len) { size_t i; int c; char val; for(i = 0; i < len; i += 2) { if((c = cli_hex2int(hex[i])) >= 0) { val = c; if((c = cli_hex2int(hex[i+1])) >= 0) { val = (val << 4) + c; } else { return -1; } } else { return -1; } *ptr++ = val; } return 0; }
int cli_hex2num(const char *hex) { int hexval, ret = 0, len, i; len = strlen(hex); if(len % 2 != 0) { cli_errmsg("cli_hex2num(): Malformed hexstring: %s (length: %d)\n", hex, len); return -1; } for(i = 0; i < len; i++) { if((hexval = cli_hex2int(hex[i])) < 0) break; ret = (ret << 4) | hexval; } return ret; }