unsigned long Synta::hexToLong(char* hex){ // char *boo; //waste point for strtol // char str[7]; //Destination of rearranged hex // strncpy(str,&hex[4],2); //Lower Byte // strncpy(str+2,&hex[2],2); //Middle Byte // strncpy(str+4,hex,2); //Upper Byte // str[6] = 0; // return strtol(str,&boo,16); //convert hex to long integer Inter inter = Inter(0,hexToByte(hex+4),hexToByte(hex+2),hexToByte(hex)); //create an inter return inter.integer; //and convert it to an integer }
// {{{ load_key_line int load_key_line( FILE*file, unsigned char ** key, unsigned int *key_len, unsigned char ** nonce, unsigned int *nonce_len ) { int c; unsigned int i; char buf[2*RDRAND_MAX_KEY_LENGTH] = {}; i=0; while ( (c=getc(file)) != EOF && c != '\n') { if ( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9') ) { if(i > RDRAND_MAX_KEY_LENGTH*2) return E_KEY_NONCE_BAD_LENGTH; buf[i] = c; } else { printf("Unknown: %c (%02x)\n",c,c); return E_KEY_INVALID_CHARACTER; } i++; } // nonce is half of key, so key is first 2/3 of line and nonce the last 1/3 *key_len = (i/3)*2; *nonce_len = i/3; // special cases, when there is no need for malloc if(*key_len + *nonce_len != i) return E_KEY_NONCE_BAD_LENGTH; if(c == EOF) return E_EOF; if(*key_len == 0) return E_OK; // convert to bytes hexToByte(buf, *key_len, *key, *key_len/2); hexToByte(buf+*key_len, *nonce_len, *nonce, *nonce_len/2); // set key/nonce length to bytes *nonce_len=*nonce_len/2; *key_len = *key_len/2; return E_OK; }
// stores bit vector in hex string format - to be stored in the file // bitvec_buf - works on 2*BIT_VECTOR_LEN bytes void bitvec::convertFromHex(unsigned char bitvec_buf[]) { for(int i=0; i<BIT_VECTOR_LEN; i++) { unsigned char byte1 = hexToByte(bitvec_buf[2*i]); unsigned char byte2 = hexToByte(bitvec_buf[2*i+1]); bitvector[i] = (byte1 << 4) + byte2; } return; } // convertFromHex
// Read a uint8_t in HEX uint8_t UniversalRF::readByte() { uint8_t c[2]; uint8_t i; for(i=0; i<2; i++) { if(*hexPtr == 0) { if (debug) Serial.println("Not enough data"); longjmp(env, 1); } c[i] = *hexPtr; hexPtr++; } return hexToByte(c[0]) << 4 | hexToByte(c[1]); }
void FCEU_Guid::scan(std::string& str) { char* endptr = (char*)str.c_str(); FCEU_en32lsb(data,strtoul(endptr,&endptr,16)); FCEU_en16lsb(data+4,strtoul(endptr+1,&endptr,16)); FCEU_en16lsb(data+6,strtoul(endptr+1,&endptr,16)); FCEU_en16lsb(data+8,strtoul(endptr+1,&endptr,16)); endptr++; for(int i=0;i<6;i++) data[10+i] = hexToByte(&endptr); }
uint8_t listen_from_firectl(char result[5]) { uint8_t addrCount = 0; uint8_t bufferCount = 0; uint8_t listeningState = 0; char incomingByte; char addressBytes[2]; char buffer[10]; uint8_t resultLength = 0; while (Serial.available() > 0) { incomingByte = Serial.read(); if (incomingByte == '!'){ addrCount = 0; listeningState = 0; } else if (listeningState == 0){ addressBytes[addrCount++] = incomingByte; if (addrCount == 2){ if (hexToByte(addressBytes[0], addressBytes[1]) == BOARDADDRESS){ listeningState = 1; //for us, start saving command data } else{ listeningState = 2; //not for us, ignore } } } else if (listeningState == 1){ if (incomingByte == '.'){ listeningState = 2; } else { buffer[bufferCount++] = incomingByte; } } } uint8_t i = 0; while (i < bufferCount - 1){ result[resultLength++] = hexToByte(buffer[i++], buffer[i++]); } return resultLength; }
char *cdwCalcValidationKey(char *md5Hex, long long fileSize) /* calculate validation key to discourage faking of validation. Do freeMem on *result when done. */ { if (strlen(md5Hex) != 32) errAbort("Invalid md5Hex string: %s\n", md5Hex); long long sum = 0; sum += fileSize; while (*md5Hex) { unsigned char n = hexToByte(md5Hex); sum += n; md5Hex += 2; } int vNum = sum % 10000; char buf[256]; safef(buf, sizeof buf, "V%d", vNum); return cloneString(buf); }
bool xpcc::CanLawicelFormatter::convertToCanMessage(const char* in,can::Message& out) { uint8_t dlc_pos; if (in[0] == 'R' || in[0] == 'T') { out.flags.extended = true; dlc_pos = 9; } else { out.flags.extended = false; dlc_pos = 4; } if (std::strlen(in) < dlc_pos + 1U) return false; // get the number of data-bytes for this message out.length = in[dlc_pos] - '0'; if (out.length > 8) return false; // too many data-bytes if (in[0] == 'r' || in[0] == 'R') { out.flags.rtr = true; if (std::strlen(in) != (dlc_pos + 1U)) return false; } else { out.flags.rtr = false; if (std::strlen(in) != (out.length * 2 + dlc_pos + 1U)) return false; } // read the messge-identifier if (out.flags.extended) { uint16_t id; uint16_t id2; id = hexToByte(&in[1]) << 8; id |= hexToByte(&in[3]); id2 = hexToByte(&in[5]) << 8; id2 |= hexToByte(&in[7]); out.identifier = (uint32_t) id << 16 | id2; } else { uint16_t id; id = charToByte(&in[1]) << 8; id |= hexToByte(&in[2]); out.identifier = id; } // read data if the message is no rtr-frame if (!out.flags.rtr) { const char *buf = &in[dlc_pos + 1]; uint8_t i; for (i=0; i < out.length; i++) { out.data[i] = hexToByte(buf); buf += 2; } } return true; }
char * ICACHE_FLASH_ATTR parse_params_pins_set(const char *params, unsigned int paramslen, gpio_command_params *out, unsigned int all, unsigned int timeout, ALLOWED_FIELDS fields, ALLOWED_FIELDS *readedfields) { struct jsonparse_state jparser; if(paramslen) jsonparse_setup(&jparser, params, paramslen); else if(fields) return "No parameters specified"; int type; int pinnum; unsigned int pins_found = 0; unsigned int pinmask; *readedfields = 0; load_defaults(out, timeout); while (jparser.pos < jparser.len) { type = jsonparse_next(&jparser); if (type == JSON_TYPE_PAIR_NAME) { if(strcmp_value(&jparser, "mode") == 0) { if((fields & AF_UARTMODE) == 0 && (fields & AF_SPIMODE) == 0) return UNEXPECTED; jsonparse_next(&jparser); if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) { int val; if(strcmp_value(&jparser, "disable") == 0 && (fields & AF_UARTMODE)) { *readedfields |= AF_UARTMODE; out->uart_speed = 0; continue; } int res = strToUInt(&jparser.json[jparser.vstart], &val); if(!res) return "Wrong mode integer value"; if(fields & AF_UARTMODE) { *readedfields |= AF_UARTMODE; out->uart_speed = val; if(res < jparser.vlen && jparser.json[jparser.vstart + res] == ' ') { while(res < jparser.vlen && jparser.json[jparser.vstart + res] == ' ') res++; if(res + 3 == jparser.vlen) { const unsigned char b = jparser.json[jparser.vstart + res] - '0'; const unsigned char p = jparser.json[jparser.vstart + res + 1]; const unsigned char s = jparser.json[jparser.vstart + res + 2] - '0'; if(b < 10 && s < 10) { out->uart_bits = b; out->uart_partity = p; out->uart_stopbits = s; } else { return "Wrong mode framing"; } } } } if(fields & AF_SPIMODE) { *readedfields |= AF_SPIMODE; out->spi_mode = val; } } continue; } else if(strcmp_value(&jparser, "count") == 0) { char * res = readUIntField(&jparser, AF_COUNT, &out->count, fields, readedfields, 0); if(res) return res; continue; } else if(strcmp_value(&jparser, "timeout") == 0) { char * res = readUIntField(&jparser, AF_TIMEOUT, &out->timeout, fields, readedfields, 0); if(res) return res; continue; } else if(strcmp_value(&jparser, "frequency") == 0) { if((fields & AF_PERIOD) == 0) return UNEXPECTED; float frequncy; jsonparse_next(&jparser); if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) { const int res = strToFloat(&jparser.json[jparser.vstart], &frequncy); if(!res) return "Wrong frequency float value"; if(frequncy < 0.000499999f) out->periodus = 2000004000; else out->periodus = 1000000.0f / frequncy; *readedfields |= AF_PERIOD; } continue; } else if(strcmp_value(&jparser, "address") == 0) { if((fields & AF_ADDRESS) == 0) return UNEXPECTED; jsonparse_next(&jparser); if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) { char c; int p; if(jparser.json[jparser.vstart] == '0' && jparser.json[jparser.vstart + 1] == 'x') p = 2; else p = 0; const int res = hexToByte(&jparser.json[jparser.vstart + p], &c); if(res != (jparser.vlen - p)) return "Address is wrong"; out->address = c; *readedfields |= AF_ADDRESS; } continue; } else if(strcmp_value(&jparser, "SDA") == 0) { char * res = readUIntField(&jparser, AF_SDA, &out->SDA, fields, readedfields, 0); if(res) return res; continue; } else if(strcmp_value(&jparser, "SCL") == 0) { char * res = readUIntField(&jparser, AF_SCL, &out->SCL, fields, readedfields, 0); if(res) return res; continue; } else if(strcmp_value(&jparser, "CS") == 0) { char * res = readUIntField(&jparser, AF_CS, &out->CS, fields, readedfields, ~(uint32_t)0U); if(res) return res; continue; } else if(strcmp_value(&jparser, "pin") == 0) { char * res = readUIntField(&jparser, AF_PIN, &out->pin, fields, readedfields, 0); if(res) return res; continue; } else if(strcmp_value(&jparser, "data") == 0) { if((fields & AF_DATA) == 0 || out->data_len) return UNEXPECTED; jsonparse_next(&jparser); if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) { out->data_len = dhdata_decode(&jparser.json[jparser.vstart], jparser.vlen, out->data, sizeof(out->data)); if(out->data_len == 0) return "Data is broken"; *readedfields |= AF_DATA; } continue; } else if(strcmp_value(&jparser, "text") == 0) { if((fields & AF_TEXT_DATA) == 0 || out->data_len) return UNEXPECTED; jsonparse_next(&jparser); if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) { if (jparser.vlen > sizeof(out->data) - 1) return "Text is too long"; os_memcpy(out->data, &jparser.json[jparser.vstart], jparser.vlen); out->data[jparser.vlen] = 0; out->data_len = jparser.vlen; *readedfields |= AF_TEXT_DATA; } continue; } else if(strcmp_value(&jparser, "all") == 0) { if(pins_found) return "Wrong argument"; pins_found = ~(unsigned int)0; pinmask = all; pinnum = -1; } else { const int res = strToUInt(&jparser.json[jparser.vstart], &pinnum); if(!res || pinnum < 0 || pinnum > DHGPIO_MAXGPIONUM || (pins_found & (1 << pinnum))) return "Wrong argument"; pins_found |= (1 << pinnum); pinmask = (1 << pinnum); } jsonparse_next(&jparser); if(jsonparse_next(&jparser) != JSON_TYPE_ERROR) { if(strcmp_value(&jparser, "x") == 0) continue; else if(strcmp_value(&jparser, "init") == 0) { if((fields & AF_INIT) == 0) return UNEXPECTED; out->pins_to_init |= pinmask; *readedfields |= AF_INIT; } else if(strcmp_value(&jparser, "pullup") == 0) { if((fields & AF_PULLUP) == 0) return UNEXPECTED; out->pins_to_pullup |= pinmask; *readedfields |= AF_PULLUP; } else if(strcmp_value(&jparser, "nopull") == 0) { if((fields & AF_NOPULLUP) == 0) return UNEXPECTED; out->pins_to_nopull |= pinmask; *readedfields |= AF_NOPULLUP; } else if(strcmp_value(&jparser, "disable") == 0) { if((fields & AF_VALUES) == 0 && (fields & AF_DISABLE) == 0) { return UNEXPECTED; } if (fields & AF_VALUES) { int i; if(pinnum > 0 ) out->pin_value[pinnum] = 0; else for(i = 0; i <= DHGPIO_MAXGPIONUM; i++) out->pin_value[i] = 0; out->pin_value_readed |= pinmask; *readedfields |= AF_VALUES; } if(fields & AF_DISABLE) { out->pins_to_disable |= pinmask; *readedfields |= AF_DISABLE; } } else if(strcmp_value(&jparser, "rising") == 0) { if((fields & AF_RISING)== 0) return UNEXPECTED; out->pins_to_rising |= pinmask; *readedfields |= AF_RISING; } else if(strcmp_value(&jparser, "falling") == 0) { if((fields & AF_FALLING) == 0) return UNEXPECTED; out->pins_to_falling |= pinmask; *readedfields |= AF_FALLING; } else if(strcmp_value(&jparser, "both") == 0) { if((fields & AF_BOTH) == 0) return UNEXPECTED; out->pins_to_both |= pinmask; *readedfields |= AF_BOTH; } else if(strcmp_value(&jparser, "read") == 0) { if((fields & AF_READ) == 0) return UNEXPECTED; out->pins_to_read |= pinmask; *readedfields |= AF_READ; } else if(strcmp_value(&jparser, "presence") == 0) { if((fields & AF_PRESENCE) == 0) return UNEXPECTED; out->pins_to_presence |= pinmask; *readedfields |= AF_PRESENCE; } else if((fields & AF_VALUES)) { // BE CAREFULL, all digits values have to be under this if int value, i; if(!strToUInt(&jparser.json[jparser.vstart], &value)) return NONINTEGER; if(pinnum > 0 ) out->pin_value[pinnum] = value; else for(i = 0; i <= DHGPIO_MAXGPIONUM; i++) out->pin_value[i] = value; out->pin_value_readed |= pinmask; *readedfields |= AF_VALUES; if(value == 1 && (fields & AF_SET)) { out->pins_to_set |= pinmask; *readedfields |= AF_SET; } else if(value == 0 && (fields & AF_CLEAR)) { out->pins_to_clear |= pinmask; *readedfields |= AF_CLEAR; } } else if(strcmp_value(&jparser, "1") == 0) { if((fields & AF_SET) == 0) return UNEXPECTED; out->pins_to_set |= pinmask; *readedfields |= AF_SET; } else if(strcmp_value(&jparser, "0") == 0) { if((fields & AF_CLEAR) == 0) return UNEXPECTED; out->pins_to_clear |= pinmask; *readedfields |= AF_CLEAR; } else { return "Unsupported action"; } } } else if(type == JSON_TYPE_ERROR) { return "Broken json"; } } return NULL; }