/* deletes @what from the string @begin. * @what_len: length of @what, excluding the terminating \0 */ static void str_hex_to_char(char **begin, const char **end) { char *sbegin = *begin; const char *str_end = *end; if(str_end <= sbegin) return; if(strlen(sbegin) <= 2) return; /* convert leading %xx*/ if (sbegin[0] == '%') { sbegin[2] = hex2int((unsigned char*)sbegin+1); sbegin += 2; } *begin = sbegin++; while(sbegin+3 < str_end) { while(sbegin+3<str_end && sbegin[0]=='%') { const char* src = sbegin+3; *sbegin = hex2int((unsigned char*)sbegin+1); /* move string */ memmove(sbegin+1,src,str_end-src+1); str_end -= 2; } sbegin++; } *end = str_end; }
//Functions to encrypt 2 Hex messages with XOR //Take in 2 Hex arguments //Xor them together to produce the encrypt message //Algorithm: //First compare size between msg and key //If msg > key, repeat the key until msg = key //encrypt every bytes then concatenate the out result char* hex_xor( char *msg, char *key) { char* out; int i; char a[2]; char b[2]; int len = strlen(msg) + 1; out = malloc((len)*sizeof(char)); memset(out, 0, (len)*sizeof(char)); //Functions to compare msg and key then modify key if needed // //Xor every char while(msg[0]!='\0') { for(i=0; i<2; i++) { a[i] = msg[i]; b[i] = key[i]; } a[1] = '\0'; b[1] = '\0'; out = strcat(out, dec_to_hex(int_xor(hex2int(a) ,hex2int(b)))); msg = msg + 1; key = key + 1; } return out; }
int rt_eth_aton(char *addr_buf, const char *mac) { int i = 0; int nibble; while (1) { if (*mac == 0) return -EINVAL; if ((nibble = hex2int(*mac++)) < 0) return nibble; *addr_buf = nibble << 4; if (*mac == 0) return -EINVAL; if ((nibble = hex2int(*mac++)) < 0) return nibble; *addr_buf++ |= nibble; if (++i == 6) break; if ((*mac == 0) || (*mac++ != ':')) return -EINVAL; } return 0; }
// http://www.dragonwins.com/domains/getteched/bmp/bmpfileformat.htm // http://www.fileformat.info/format/bmp/egff.htm // https://en.wikipedia.org/wiki/BMP_file_format void ImageReader::read_image(string filepath) { fstream reader; reader.open(filepath.c_str(), ios::in | ios::binary); if (reader.is_open()) { // Wczytanie nag³ówka pliku - 14 bajtów (nic z tym nie trzeba robiæ) reader.read( this->header, BMP_HEADER_SIZE); // Wczytanie nag³ówka obrazu (tutaj bêdzie rozmiar) reader.read( &this->header[BMP_HEADER_SIZE], BMP_IMAGE_HEADER_SIZE ); this->width = hex2int( &this->header[BMP_HEADER_SIZE + BMP_WIDTH_OFFSET], BMP_WIDTH_SIZE); this->height = hex2int( &this->header[BMP_HEADER_SIZE + BMP_HEIGHT_OFFSET], BMP_HEIGHT_SIZE); this->data = new unsigned char[this->width*this->height*COLORSIZE]; char byte; int counter = 0; while( reader.get(byte) ) { unsigned char color = (unsigned char)byte; this->data[counter++] = color; } } else { cout << "Error! No image" << endl; } reader.close(); }
void decode_bearer_data(char *msg, int length, char *message, int *is_vm) { int i=0,j; int code,sublength; while(i<length) { get_code_and_length(msg+i*2,&code,&sublength); if(code==1) { int encoding=getbits(msg+i*2+4,0,5); int nchars=getbits(msg+i*2+4,5,8); if(encoding==2 || encoding==3) { for(j=0;j<nchars;j++) *message++=getbits(msg+i*2+4,13+7*j,7); } else if(encoding==8 || encoding==0) { for(j=0;j<nchars;j++) *message++=getbits(msg+i*2+4,13+8*j,8); } else { strcpy(message,"bad SMS encoding"); message+=16; } *message=0; } else if (code == 11 && sublength == 1) { int msgs; if (is_vm) { *is_vm = 1; msgs = hex2int(msg[i*2+4])+16*hex2int(msg[i*2+5]); if (msgs) *is_vm |= 0x10; } } i+=sublength+2; } }
static DWORD string2hex16( const char* str ) { int i1 = hex2int( str[0] ); int i2 = hex2int( str[1] ); if ( ( i1 >= 0 ) && ( i2 >= 0 ) ) return i1*16+i2; return -1; }
static int set_path(PyObject *env, char *buf, int len) { int c, c1, slen; char *s0, *t; PyObject *obj; t = s0 = buf; while(len > 0){ c = *buf++; if(c == '%' && len > 2){ c = *buf++; c1 = c; c = *buf++; c = hex2int(c1) * 16 + hex2int(c); len -= 2; }else if(c == '?'){ //stop if(set_query(env, buf, len) == -1){ //Error return -1; } break; }else if(c == '#'){ //stop //ignore fragment break; } *t++ = c; len--; } //*t = 0; slen = t - s0; slen = urldecode(s0, slen); obj = PyBytes_FromStringAndSize(s0, slen); /* DEBUG("path:%.*s", (int)slen, PyBytes_AS_STRING(obj)); */ if(likely(obj != NULL)){ #ifdef PY3 //TODO CHECK ERROR char *c2 = PyBytes_AS_STRING(obj); PyObject *v = PyUnicode_DecodeUTF8(c2, strlen(c2), NULL); PyDict_SetItem(env, path_info_key, v); Py_DECREF(v); #else PyDict_SetItem(env, path_info_key, obj); #endif Py_DECREF(obj); return slen; }else{ return -1; } }
int unpack_hex(char* src, ut64 len, char* dst) { int i = 0; while (i < (len / 2)) { int val = hex2int (src[(i*2)]); val <<= 4; val |= hex2int (src[(i*2)+1]); dst[i++] = val; } dst[i] = '\0'; return len; }
string url_decode(const char* url, int is_query) { unsigned char high, low; if(!url) return ""; char* str = (char*)strdup(url); const char* src = (const char*)str; char* dst = (char*)str; while((*src) != '\0') { if(is_query && *src == '+') { *dst = ' '; } else if (*src == '%') { *dst = '%'; high = hex2int(*(src + 1)); if (high != 0xFF) { low = hex2int(*(src + 2)); if (low != 0xFF) { high = (high << 4) | low; /* map control-characters out */ //if (high < 32 || high == 127) high = '_'; *dst = high; src += 2; } } } else { *dst = *src; } dst++; src++; } *dst = '\0'; string decoded = str; free((void*)str); return decoded; }
static void handle_rfid_input(void) { static char buf[14]; static uint8_t idx = 0; int c; uint8_t checksum; uint8_t i; for (;;) { c = softserial_getchar(); switch (c) { case SOFTSERIAL_EOF: return; case 2: idx = 0; break; case 3: if (idx == 14 && cnt == 0) { /* Check for correct checksum and CR / LF */ checksum = 0; for (i = 0; i < 12; i += 2) checksum ^= ((hex2int(buf[i]) << 4) | hex2int(buf[i+1])); if (checksum) break; if (buf[12] != 13 || buf[13] != 10) break; /* We got an RFID tag. Copy it into the card reader buffer to emulate a read card data string. */ for (i = 0; i < 10 && cnt < 255; ++i, ++cnt) data[cnt] = buf[i]; for (i = 0; i < 3; i++) { pin_low(PIN_YELLOW_LED); _delay_ms(80); pin_high(PIN_YELLOW_LED); _delay_ms(80); } } default: if (idx < 14) { buf[idx++] = c; second = 0; } break; } } }
float string2float(const std::string& str) { assert(str.size() == 2*sizeof(float)); float value; for(size_t i = 0; i < sizeof(float); ++i) { char& v = reinterpret_cast<char*>(&value)[i]; v = static_cast<char>((hex2int(str[2*i+0]) << 4) | hex2int(str[2*i+1])); } return value; }
int main(int argc, char** argv) { char buff[3]; int d, u; printf("Enter Hex number: "); fgets(buff, 3, stdin); d = hex2int(buff[0]); u = hex2int(buff[1]); printf("That equals %d in decimal.\n", d * 16 + u); return 0; }
static int set_path(PyObject *env, char *buf, int len) { int c, c1, slen; char *s0, *t; PyObject *obj; t = s0 = buf; while(len > 0){ c = *buf++; if(c == '%' && len > 2){ c = *buf++; c1 = c; c = *buf++; c = hex2int(c1) * 16 + hex2int(c); len -= 2; }else if(c == '?'){ //stop if(set_query(env, buf, len) == -1){ //Error return -1; } break; }else if(c == '#'){ //stop //ignore fragment break; } *t++ = c; len--; } //*t = 0; slen = t - s0; slen = urldecode(s0, slen); #ifdef PY3 obj = PyUnicode_DecodeLatin1(s0, slen, "strict"); #else obj = PyBytes_FromStringAndSize(s0, slen); #endif if (likely(obj != NULL)) { PyDict_SetItem(env, path_info_key, obj); Py_DECREF(obj); return slen; } else { return -1; } }
void med_print_string_tlv(u16 len, char *info) { int i; for (i = 0; i < 2*len; i+=2) printf("%c", hex2int(info+i)); printf("\n"); }
bool t_digest_response::set_attr(const t_parameter &p) { if (p.name == "username") username = p.value; else if (p.name == "realm") realm = p.value; else if (p.name == "nonce") nonce = p.value; else if (p.name == "digest_uri") { digest_uri.set_url(p.value); if (!digest_uri.is_valid()) return false; } else if (p.name == "response") dresponse = p.value; else if (p.name == "cnonce") cnonce = p.value; else if (p.name == "opaque") opaque = p.value; else if (p.name == "algorithm") algorithm = p.value; else if (p.name == "qop") message_qop = p.value; else if (p.name == "nc") nonce_count = hex2int(p.value); else auth_params.push_back(p); return true; }
int main(int argc, char **argv) { int i, nf; printf("ROMIDENT v2.1\nThierry Lescot, 1998/99.\n\n"); load_datafile(argv[0]); printf("DAT file revision %d.\n", g_entry); if (argc<2) { printf("Error, specify at least one file name !\n"); return 1; }; for (nf=1;nf<argc;nf++) { if (argv[nf][0] == '-') { switch(argv[nf][1]) { case '&': ident_crc(hex2int(&argv[nf][2])); break; }; } else { if ((i = checkfile(argv[nf])) == -1) { printf("Error, '%s' doesn't exist !\n", argv[1]); return 1; }; ident(argv[nf]); }; }; return unk; }
HRESULT envGetEenvironmentLong (const char * varname, uint32 * pvalue) { char * val; int vlen; uint8 type; if (findEnvString (varname, &val, &vlen)) { type = is_number (val, vlen); if (type == 1) { *pvalue = (uint32) dec2int(val, vlen); return NO_ERROR; } else if (type == 2) { *pvalue = (uint32) hex2int(val, vlen); return NO_ERROR; } else { return E_GEN_ILLEGAL_PARAM; } } return E_GEN_NOMATCH; }
static HRESULT cliBuiltInToolGetNumber(uint32 index, char** argv, CLIDescriptor* variable, uint32* value) { HRESULT hResult = NO_ERROR; if (!is_number(argv[index],strlen(argv[index]))) { if (variable) { hResult = cliLookUpConstant(variable, argv[index], value); } else { hResult = E_CLI_BADARGUMENTS; } } if (hResult != NO_ERROR) { cliPrintf("SET ERROR: argument #%i must be a number\n\r", index+1); return hResult; } if (is_number(argv[index],strlen(argv[index]))==2) { *value = hex2int(argv[index],strlen(argv[index])); } else { *value = (uint32) dec2int(argv[index],strlen(argv[index])); } return hResult; }
int str2byte( unsigned char *str, int len, unsigned char *byte_out) { int i, val, val2; char *pos = (char *)str; for (i = 0; i < len/2; i++) { val = hex2int(*pos++); if (val < 0) return -1; val2 = hex2int(*pos++); if (val2 < 0) return -1; byte_out[i] = (val * 16 + val2) & 0xff; } return 0; }
int hexpair2bin(const char *arg) // (0A) => 10 || -1 (on error) { unsigned char *ptr; unsigned char c = '\0'; unsigned char d = '\0'; unsigned int j = 0; for (ptr = (unsigned char *)arg; ;ptr = ptr + 1) { if (ptr[0]==' '||ptr[0]=='\t'||ptr[0]=='\n'||ptr[0]=='\r') continue; if (!is_printable(ptr[0])) continue; if (ptr[0]=='\0'||ptr[0]==' ' || j==2) break; d = c; if (hex2int(&c, ptr[0]) == 0) { fprintf(stderr, "Invalid hexa string at char '%c'.\n", ptr[0]); return -1; } c |= d; if (j++ == 0) c <<= 4; } return (int)c; }
void setbit(char *s,int b, int val) { int byte=b/4; int bit=b%4; s[byte]=hextable[hex2int(s[byte]) | (val<<(3-bit))] ; }
int htoi(const char s[]) { int i = 0; int ans = 0; int valid = 1; int hexit; // skip over 0x or 0X if(s[i] == '0') { ++i; if(s[i] == 'x' || s[i] == 'X'){ ++i; } } while(valid && s[i] != '\0') { ans = ans * 16; if(s[i] >= '0' && s[i] <= '9') { ans = ans + (s[i] - '0'); } else { hexit = hex2int(s[i]); if(hexit == 0){ valid = 0; } else { ans = ans + hexit; } } ++i; } if(!valid) { ans = 0; } return ans; }
int unescape_component(char *comp) // inplace, returns len after shrinking { char *in = comp, *out = comp; int len; for (len = 0; *in; len++) { if (in[0] != '%' || !in[1] || !in[2]) { *out++ = *in++; continue; } *out++ = hex2int(in[1])*16 + hex2int(in[2]); in += 3; } return len; }
void Graph::set_colorlines(const std::vector<std::string> &_colorlines) { this->colorlines = _colorlines; std::string delimiter = "#"; for(std::vector<std::string>::const_iterator it = this->colorlines.begin(); it != this->colorlines.end(); ++it) { std::string value = it->substr(0, it->find(delimiter)); std::string rgb = it->substr(it->find(delimiter) + 1); this->colorline_values.push_back( str2float(value) ); Color clr(hex2int(rgb.substr(0,2)), hex2int(rgb.substr(2,2)), hex2int(rgb.substr(4,2)) ); this->colorline_colors.push_back(clr); } }
int getbit(char *s,int b) { int byte=b/4; int bit=b%4; int data=hex2int(s[byte]); if(data&(1<<(3-bit))) return 1; else return 0; }
gunichar UnicodeRange::sample_glyph(){ //This could be better if (!unichars.empty()) return unichars[0]; if (!range.empty()) return hex2int(range[0].start); return (gunichar) ' '; }
int str2int(char *hex) { if(hex[1] == 'x' || hex[1] == 'X') { return hex2int(&hex[2]); } return atoi(hex); }
std::string decodeStepString( std::string arg_str ) { char* stream_pos = (char*)arg_str.c_str(); std::string result_str; while( *stream_pos != '\0' ) { if( *stream_pos == '\\' ) { ++stream_pos; if( *stream_pos == 'S' ) { ++stream_pos; if( *stream_pos == '\\' ) { ++stream_pos; char char_from_8859 = *(stream_pos)+128; result_str.push_back( char_from_8859 ); ++stream_pos; continue; } } else if( *stream_pos == 'X' ) { ++stream_pos; if( *stream_pos == '\\' ) { ++stream_pos; char c = *stream_pos; int result_ascii = hex2int( c ); char char_ascii = result_ascii; result_str.push_back( char_ascii ); continue; } else if( *stream_pos == '0' ) { ++stream_pos; if( *stream_pos == '\\' ) { } } else if( *stream_pos == '2' ) { ++stream_pos; if( *stream_pos == '\\' ) { // the following sequence of multiples of four hexadecimal characters shall be interpreted as encoding the // two-octet representation of characters from the BMP in ISO 10646 } } } } result_str.push_back( *stream_pos ); ++stream_pos; } return result_str; }
int parseUserData(unsigned char *buf, int bufsize, PGconn *db){ if(buf[2]!=0x14 && buf[3]!=0x0e){ printf("Buffer does not consist of user data!\n"); return 1; } sUserData ud; ud.weight=(float)((buf[6]<<8)+buf[5])*0.045359; ud.height=buf[7]; struct tm time; time.tm_sec=1; time.tm_min=0; time.tm_hour=0; time.tm_mday=buf[8]; time.tm_mon=buf[9]-1; time.tm_year=buf[10]; time.tm_isdst = -1; ud.birthdate=(long) mktime(&time); ud.sex=buf[11]; ud.activity=buf[12]; ud.vo2max=buf[13]; ud.HRMax=buf[14]; time.tm_sec=0; time.tm_min=hex2int(buf[18]); time.tm_hour=hex2int(buf[19]); time.tm_mday=buf[20]; time.tm_mon=buf[21]-1; time.tm_year=buf[22]+100; time.tm_isdst = -1; ud.editdate=(long) mktime(&time); if(db!=NULL){ db_insert_udata(db,&ud); } printf("weight=%.1f kg (%.1f lbs)\n", (double)((buf[6]<<8)+buf[5])*0.045359,(double)((buf[6]<<8)+buf[5])/10 ); printf("height=%d cm\n", buf[7]); printf("birthdate=%d.%d.%d\n",buf[8],buf[9],1900+buf[10]); printf("sex=%s\n", buf[11]?"Female":"Male"); printf("Activity=%d\n",buf[12]); printf("VO2max=%d\n",buf[13]); printf("HRmax=%d\n",buf[14]); printf("Information last edited=%d.%d.%d %x:%x\n",buf[20],buf[21],buf[22],buf[19],buf[18]); return 0; }
int asn_object::read(std::istream& istr){ char c1, c2; if(!(istr>>c1)){ //istr.unget(); return -1; } if(!(istr>>c2)){ //istr.unget(); istr.unget(); return -1; } if(!isxdigit(c1) || !isxdigit(c2)){ istr.unget(); istr.unget(); return -1; } return hex2int(c1)*16 + hex2int(c2); }