unsigned char* hex2bin(char *hex, unsigned char *bin, int iLen, int *oLen) { int bLen; int i; iLen = (iLen <= 0) ? StrLen(hex) : iLen; if(StrNCompare(hex, "0x", 2) == 0) { /* hex string has 0x prefix */ hex = hex + 2; iLen -= 2; } if(iLen%2 != 0) { /* hex string is not a multiple of 2 in length */ return NULL; } bLen = iLen / 2; MemSet(bin,0,bLen); for(i = 0; i < bLen; i++) { char hbyte = *hex++; char lbyte = *hex++; if(!is_hex(hbyte) || !is_hex(lbyte)) { /* invalid character */ return NULL; } *bin++ = (unsigned char) (hex2byte(hbyte)<<4 | hex2byte(lbyte)); } *oLen = i; return bin; }
static int decode_5(const char *url_data, char *dest) { char *pos = dest; for (size_t i = 0; url_data[i] != '\0'; i++) { if (url_data[i] == '+') // decode space *(pos++) = ' '; else if (url_data[i] == '%') { // decode hex value if (is_hex(url_data[i + 1]) && is_hex(url_data[i + 2])) { // this is a percent encoded value. *(pos++) = (hex_val(url_data[i + 1]) << 4) | hex_val(url_data[i + 2]); i += 2; } else { // there was an error in the URL encoding... return -1; } } else *(pos++) = url_data[i]; } *pos = '\0'; return pos - dest; }
/* Modify STR in-place. '%', CR and LF are always percent escaped, other characters may be percent escaped, always using uppercase hex, see https://www.gnupg.org/documentation/manuals/assuan.pdf */ static char * unescape_assuan(char *str) { char *s = str; while (s[0]) { if (s[0] == '%' && is_hex(s[1]) && is_hex(s[2])) { char *s2 = s; char val = hex_to_int(s[1]) * 16 + hex_to_int(s[2]); s2[0] = val; ++s2; while (s2[2]) { s2[0] = s2[2]; ++s2; } s2[0] = '\0'; } ++s; } return str; }
/* Scan a string for URI escape sequences, replacing them with the * decoded octet value in place. Replacing the encoded sequence will * cause the string to become shorter. The resulting string will * be NUL-terminated. */ static void replace_escape_sequences(char * s, size_t len) { size_t i, j, n; int c; for(i = j = n = 0 ; i < len ; i++, j++) { c = s[i]; if(c == '%' && i+2 < len && is_hex(s[i+1]) && is_hex(s[i+2])) { c = 16 * hex_to_dec(s[i+1]) + hex_to_dec(s[i+2]); i += 2; s[j] = c; } else if (i != j) { s[j] = c; } } s[j] = '\0'; }
int32_t urlDecodeNoZeroes ( char *dest , const char *s , int32_t slen ) { int32_t j = 0; for ( int32_t i = 0 ; i < slen ; i++ ) { if ( s[i] == '+' ) { dest[j++]=' '; continue; } dest[j++] = s[i]; if ( s[i] != '%' ) continue; if ( i + 2 >= slen ) continue; // if two chars after are not hex chars, it's not an encoding if ( ! is_hex ( s[i+1] ) ) continue; if ( ! is_hex ( s[i+2] ) ) continue; // convert hex chars to values unsigned char a = htob ( s[i+1] ) * 16; unsigned char b = htob ( s[i+2] ) ; // NO ZEROES! fixes &content= having decoded \0's in it // and setting our parms if ( a + b == 0 ) { log("fctypes: urlDecodeNoZeros encountered url " "encoded zero. truncating http request."); return j; } dest[j-1] = (char) (a + b); i += 2; } return j; }
/*------------------------------------------------------------------------ -- Name: replace_gaiji_with_utf8 -- -- Description: -- Replace all gaiji that have UTF-8 equivalents with those equivalents. -- -- The gaiji in the source string should be replresented as follows: -- {#[w or n][hex_gaiji_code]x4} (use this printf format: {#n%04X} or {#w%04X}) -- -- Parameters: -- (OUT) dest - Storage for the replaced text. -- (IN) src - The source text that contains the gaiji codes to replace. -- -- Returns: -- The replaced text. -- ------------------------------------------------------------------------*/ char * replace_gaiji_with_utf8(char *dest, char *src) { int i = 0; int dest_idx = 0; int src_len = strlen(src); char replacement[MAX_BYTES_UTF8_REPLACEMENT + 1]; /* Storage for the replacement character(s) */ dest[0] = '\0'; for(i = 0; i < src_len; i++) { /* Is a gaiji code encountered? */ if(((i + 7 < src_len) && src[i] == '{') && (src[i + 1] == '#') && is_gaiji_width_char(src[i + 2]) && is_hex(src[i + 3]) && is_hex(src[i + 4]) && is_hex(src[i + 5]) && is_hex(src[i + 6]) && (src[i + 7] == '}')) { /* Get the replacement character(s) for the gaiji code */ get_gaiji_replacement_text(replacement, subbook_directory, src[i + 2], src[i + 3], src[i + 4], src[i + 5], src[i + 6]); /* Add the replacement character(s) to dest */ dest[dest_idx] = '\0'; strcat(dest, replacement); dest_idx += strlen(replacement); i += 7; } else /* No gaiji code encountered */ { dest[dest_idx++] = src[i]; } } dest[dest_idx] = '\0'; return dest; } /* replace_gaiji_with_utf8 */
static int find_newc_header(struct archive_read *a) { const void *h; const char *p, *q; size_t skip, skipped = 0; ssize_t bytes; for (;;) { h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), &bytes); if (h == NULL) return (ARCHIVE_FATAL); p = h; q = p + bytes; /* Try the typical case first, then go into the slow search.*/ if (memcmp("07070", p, 5) == 0 && (p[5] == '1' || p[5] == '2') && is_hex(p, sizeof(struct cpio_newc_header))) return (ARCHIVE_OK); /* * Scan ahead until we find something that looks * like an odc header. */ while (p + sizeof(struct cpio_newc_header) < q) { switch (p[5]) { case '1': case '2': if (memcmp("07070", p, 5) == 0 && is_hex(p, sizeof(struct cpio_newc_header))) { skip = p - (const char *)h; __archive_read_consume(a, skip); skipped += skip; if (skipped > 0) { archive_set_error(&a->archive, 0, "Skipped %d bytes before " "finding valid header", (int)skipped); return (ARCHIVE_WARN); } return (ARCHIVE_OK); } p += 2; break; case '0': p++; break; default: p += 6; break; } } skip = p - (const char *)h; __archive_read_consume(a, skip); skipped += skip; } }
// pct-encoded = "%" HEXDIG HEXDIG static IteratorT parse_percent_encoded(IteratorT begin, IteratorT end) { if (count(begin, end) < 3) { return begin; } if (*begin == '%' && is_hex(begin+1) && is_hex(begin+2)) { return begin + 3; } else { return begin; } }
static void expand_backslashed (char *s, int len) { int backslashed = 0; exp_buffer_pos = 0; int i = 0; while (i < len) { assert (i + 3 <= (1 << 25)); if (backslashed) { backslashed = 0; switch (s[i ++]) { case 'n': exp_buffer[exp_buffer_pos ++] = '\n'; break; case 'r': exp_buffer[exp_buffer_pos ++] = '\r'; break; case 't': exp_buffer[exp_buffer_pos ++] = '\t'; break; case 'b': exp_buffer[exp_buffer_pos ++] = '\b'; break; case 'a': exp_buffer[exp_buffer_pos ++] = '\a'; break; case '\\': exp_buffer[exp_buffer_pos ++] = '\\'; break; case 'x': if (i + 2 > len || !is_hex (s[i]) || !is_hex (s[i + 1])) { exp_buffer_pos = -1; return; } exp_buffer[exp_buffer_pos ++] = hex2dec (s[i]) * 16 + hex2dec (s[i + 1]); i += 2; break; default: break; } } else { if (s[i] == '\\') { backslashed = 1; i ++; } else { exp_buffer[exp_buffer_pos ++] = s[i ++]; } } } }
static int is_number(const char * n) { if (n[0] == '0' && n[1] == 'x') return is_hex(n + 2); else return is_dec(n); }
static HRESULT parse_pubkey(IAssemblyNameImpl *name, LPCWSTR pubkey) { int i; BYTE val; static const WCHAR nullstr[] = {'n','u','l','l',0}; if(lstrcmpiW(pubkey, nullstr) == 0) return FUSION_E_PRIVATE_ASM_DISALLOWED; if (lstrlenW(pubkey) < CHARS_PER_PUBKEY) return FUSION_E_INVALID_NAME; for (i = 0; i < CHARS_PER_PUBKEY; i++) if (!is_hex(pubkey[i])) return FUSION_E_INVALID_NAME; name->haspubkey = TRUE; for (i = 0; i < CHARS_PER_PUBKEY; i += 2) { val = (hextobyte(pubkey[i]) << 4) + hextobyte(pubkey[i + 1]); name->pubkey[i / 2] = val; } return S_OK; }
int main(int argc, char **argv) { struct usb_device *usb_dev; struct usb_dev_handle *usb_handle; int retval = 1; usb_dev = device_init(); if (usb_dev == NULL) { fprintf(stderr, "Device not found!\n"); goto exit; } usb_handle = usb_open(usb_dev); if (usb_handle == NULL) { fprintf(stderr, "Unable to open USB device!\n"); goto exit; } if ((argc != 2) || (strlen(argv[1]) != 6) || !is_hex(argv[1])) { fprintf(stderr, "Specify color in RRGGBB hexa form!\n"); goto exit; } change_color(usb_handle, argv[1]); retval = 0; exit: usb_close(usb_handle); return retval; }
const char* SkParse::FindHex(const char str[], uint32_t* value) { SkASSERT(str); str = skip_ws(str); if (!is_hex(*str)) return NULL; uint32_t n = 0; int max_digits = 8; int digit; while ((digit = to_hex(*str)) >= 0) { if (--max_digits < 0) return NULL; n = (n << 4) | digit; str += 1; } if (*str == 0 || is_ws(*str)) { if (value) *value = n; return str; } return NULL; }
static void create_packet(char in_data[], struct pbuf **pbuf) { int i; int j; int len = 0; char num[3] = {0}; *pbuf = pbuf_alloc(BUF_SIZE); for (i = 0; i < (int) strlen(in_data) - 1; ) { j = 0; while (j < 2 && i < (int) strlen(in_data)) { if (is_hex(in_data[i])) { num[j] = in_data[i]; j++; } i++; } *((*pbuf)->putp) = (uint8_t) strtol(num, NULL, 16); (*pbuf)->putp++; (*pbuf)->plen--; len++; } (*pbuf)->plen = (size_t) len; }
void* read_pointer(){ void *ret; sscanf(str + poz, "%p", &ret); if(get_char(poz+1) == 'x') poz += 2; while(is_hex(get_char(poz))) ++poz; return ret; }
/** * bigobject_uri_string_unescape: * @str: the string to unescape * @len: the length in bytes to unescape (or <= 0 to indicate full string) * @target: optional destination buffer * * Unescaping routine, but does not check that the string is an URI. The * output is a direct unsigned char translation of %XX values (no encoding) * Note that the length of the result can only be smaller or same size as * the input string. * * Returns a copy of the string, but unescaped, will return NULL only in case * of error */ char * bigobject_uri_string_unescape(const char *str, int len, char *target) { char *ret, *out; const char *in; if (str == NULL) return(NULL); if (len <= 0) len = strlen(str); if (len < 0) return(NULL); if (target == NULL) { ret = malloc(len + 1); } else ret = target; in = str; out = ret; while(len > 0) { if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) { in++; if ((*in >= '0') && (*in <= '9')) *out = (*in - '0'); else if ((*in >= 'a') && (*in <= 'f')) *out = (*in - 'a') + 10; else if ((*in >= 'A') && (*in <= 'F')) *out = (*in - 'A') + 10; in++; if ((*in >= '0') && (*in <= '9')) *out = *out * 16 + (*in - '0'); else if ((*in >= 'a') && (*in <= 'f')) *out = *out * 16 + (*in - 'a') + 10; else if ((*in >= 'A') && (*in <= 'F')) *out = *out * 16 + (*in - 'A') + 10; in++; len -= 3; out++; } else { *out++ = *in++; len--; } } *out = 0; return ret; }
// . decodes "s/slen" and stores into "dest" // . returns the number of bytes stored into "dest" int32_t urlDecode ( char *dest , const char *s , int32_t slen ) { int32_t j = 0; for ( int32_t i = 0 ; i < slen ; i++ ) { if ( s[i] == '+' ) { dest[j++]=' '; continue; } dest[j++] = s[i]; if ( s[i] != '%' ) continue; if ( i + 2 >= slen ) continue; // if two chars after are not hex chars, it's not an encoding if ( ! is_hex ( s[i+1] ) ) continue; if ( ! is_hex ( s[i+2] ) ) continue; // convert hex chars to values unsigned char a = htob ( s[i+1] ) * 16; unsigned char b = htob ( s[i+2] ) ; dest[j-1] = (char) (a + b); i += 2; } return j; }
Type Lexer::get_hex(char &c, std::string &str) { while(is_hex(c)) { str += c; c = fgetc(input_file); } return HEX; }
static char * wpa_config_write_string(const u8 *value, size_t len) { if (value == NULL) return NULL; if (is_hex(value, len)) return wpa_config_write_string_hex(value, len); else return wpa_config_write_string_ascii(value, len); }
int main(int argc, char *argv[]) { plan(8); const uint8_t rdata[] = { 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; is_hex( 0x8899, knot_wire_read_u16(rdata), "16-bit read"); is_hex( 0x8899aabb, knot_wire_read_u32(rdata), "32-bit read"); is_hex( 0x8899aabbccdd, knot_wire_read_u48(rdata), "48-bit read"); is_hex(0x8899aabbccddeeff, knot_wire_read_u64(rdata), "64-bit read"); write_test(16, 0x1122, 0x11, 0x22); write_test(32, 0x66778899, 0x66, 0x77, 0x88, 0x99); write_test(48, 0xbbccdd778899, 0xbb, 0xcc, 0xdd, 0x77, 0x88, 0x99); write_test(64, 0xbbccddee66778899, 0xbb, 0xcc, 0xdd, 0xee, 0x66, 0x77, 0x88, 0x99); return 0; }
static std::string unescape(sci& i, sci end, bool (*is_valid)(char)) { auto j = i; size_t count = 0; while (end != i && (is_valid(*i) || ('%' == *i && 2 < end - i && is_hex(i[1]) && is_hex(i[2])))) { ++count; i += ('%' == *i ? 3 : 1); } std::string out; out.reserve(count); while (j != i) { out.push_back('%' == *j ? from_hex(j[1]) << 4 | from_hex(j[2]) : *j); j += ('%' == *j ? 3 : 1); } return out; }
static int is_afio_large(const char *h, size_t len) { if (len < afiol_header_size) return (0); if (h[afiol_ino_m_offset] != 'm' || h[afiol_mtime_n_offset] != 'n' || h[afiol_xsize_s_offset] != 's' || h[afiol_filesize_c_offset] != ':') return (0); if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset)) return (0); if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset)) return (0); if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset)) return (0); if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size)) return (0); return (1); }
static unsigned long __get_uint_hex(const char* s) { unsigned long res = 0; while (*s) { if (is_hex(*s)) res = (res << 4) + parse_hex(*s); else if (*s != '_') break; s++; } return res; }
/** Return true if the given string starts with a color. Ex: #ffdd00, #fd0 */ bool is_start_of_color(const char *str) { if (*str++ != '#') return false; int digit_count = 0; while (is_hex(*str)) { str++; digit_count++; } return digit_count == 8 || digit_count == 6 || digit_count == 4 || digit_count == 3; }
CMString WCPattern::parseHex() { #define to_low(x) (((x) >= 'A' && (x) <= 'Z') ? ((x) - 'A' + 'a') : (x)) #define is_dig(x) ((x) >= '0' && (x) <= '9') #define is_hex(x) (is_dig(x) || (to_low(x) >= 'a' && to_low(x) <= 'f')) #define to_int(x) ((is_dig(x)) ? ((x) - '0') : (to_low(x) - 'a' + 10)) int ci = curInd; wchar_t ch1 = (ci + 0 < pattern.GetLength()) ? pattern[ci + 0] : USHRT_MAX; wchar_t ch2 = (ci + 1 < pattern.GetLength()) ? pattern[ci + 1] : USHRT_MAX; wchar_t ch3 = (ci + 2 < pattern.GetLength()) ? pattern[ci + 2] : USHRT_MAX; wchar_t ch4 = (ci + 3 < pattern.GetLength()) ? pattern[ci + 3] : USHRT_MAX; CMString s = L" "; if (is_hex(ch1) && is_hex(ch2) && is_hex(ch3) && is_hex(ch4)) { curInd += 2; s.SetAt(0, (to_int(ch1) << 12 & 0xF000) | (to_int(ch2) << 8 & 0x0F00) | (to_int(ch3) << 4 & 0x0F00) | (to_int(ch4) & 0x000F)); } else if (is_hex(ch1) && is_hex(ch2)) { curInd += 2; s.SetAt(0, (to_int(ch1) << 4 & 0xF0) | (to_int(ch2) & 0x0F)); } return s; #undef to_low #undef is_dig #undef is_hex #undef to_int }
static uint32 parse_hex(char *&src, int max_count) { uint32 hex = 0; for (int i = 0; i < max_count; i++) { char c = *src; if (!is_hex(c)) break; hex <<= 4; hex |= isdigit(c) ? c - '0' : tolower(c) - 'a' + 10; src++; } return hex; }
static void parse_string(query_result r, char* buf) { if (r.start < r.end) { if (*r.start == '"') { for (const char* p = r.start + 1; p < r.end && *p != '"'; p++) { char ch = p[0]; if (ch == '\\' && p[1] == 'x' && is_hex(p[2])) { ch = parse_hex(p[2]); if (is_hex(p[3])) { ch = (ch << 4) + parse_hex(p[3]); p++; } p += 2; } *buf++ = ch; } } else { for (const char* p = r.start; p < r.end && *p > ' '; p++) *buf++ = *p; } } *buf = 0; }
int dump_mem(char *start, char *len){ if (!is_hex(start) || !is_number(len)) return -1; int st=hextoi(start),ed=st+atoi(len),i,total; char output[2]; total=0; memset(output,0,sizeof(output)); for (i=st; i<ed; i++){ u8 temp=read_mem_byte(i); display_byte_hex(temp); ++total; if (total%8==0 && total%16!=0) display_string("-"); else display_string(" "); if ((total)%16==0) display_string("\n"); } if (total%16!=0) display_string("\n"); return 0; }
int my_set_outputs(char *filename, int fd) { FILE *fp; char first; char buf[100]; char send[100]; int i, n; if ((fp = fopen(filename, "r")) == NULL) { return(-1); } if (fgets(buf, 99, fp) != NULL) { if ((i = is_hex(&buf[0])) >= 0) { /* try to send it */ n = i & 0xf; sprintf(send, "L%01x%01x\n", n, n); printf("%s", send); } } fclose(fp); return(0); }
static gboolean ignore_connection_name (const char *name) { gboolean result = FALSE; guint i = 0; /* check ignore_name list */ while (ignore_name[i] != NULL) { if (g_ascii_strncasecmp (name, ignore_name[i], strlen (ignore_name[i])) == 0) { return TRUE; } i++; } /* Ignore mac address based configuration */ if (strlen (name) == 12 && is_hex (name)) result = TRUE; return result; }