int hexStringToHex(char* in, int length, unsigned char* out) { int i=0; int char1, char2; char *input=in; unsigned char *output=out; if(length < 1) return 1; for(i=0; i<length; i+=2) { if(input[i] == '-' || input[i] == ':' || input[i] == '_' || input[i] == ' ') { input++; length--; } char1 = hexCharToInt(input[i]); char2 = hexCharToInt(input[i+1]); if(char1 < 0 || char1 > 15) return -1; output[i/2] = ((char1 << 4) + char2) & 0xFF; } return (i/2); }
int hexStringToByte(const char *hexString) { int l = strlen(hexString); if (l == 0) return 0; if (l == 1) return hexCharToInt(hexString[0]); return 16*hexCharToInt(hexString[0]) + hexCharToInt(hexString[1]); }
int hexToInt(char s[], int len) { int i = 0; int convert = -1; int value = 0; // Remove leading 0 (and also the second char that can be x or X) while (i < len) { if (s[i] != '0' || (i == 1 && toupper((int)s[i]) != 'X')) break; ++i; } // Convert to hex while (i < len) { convert = hexCharToInt((unsigned char)s[i]); // If conversion failed, return -1 if (convert == -1) return -1; value = (value * HEX_BASE) + convert; ++i; } return value; }
void fill_with_string(void) { char *msg = hexOrAscii ? "Hexa string to fill with: " : "Ascii string to fill with: "; char **last = hexOrAscii ? &lastFillWithStringHexa : &lastFillWithStringAscii; char tmp2[BLOCK_SEARCH_SIZE]; unsigned char *tmp1; size_t i, l1, l2; if (!mark_set) return; if (isReadOnly) { displayMessageAndWaitForKey("File is read-only!"); return; } if (sizeCopyBuffer > BIGGEST_COPYING) { displayTwoLineMessage("Hey, don't you think that's too big?!", "Really fill (Yes/No)"); if (tolower(getch()) != 'y') return; } if (!displayMessageAndGetString(msg, last, tmp2, sizeof(tmp2))) return; l1 = mark_max - mark_min + 1; l2 = strlen(tmp2); if (hexOrAscii) { if (l2 == 1) { if (!isxdigit(*tmp2)) { displayMessageAndWaitForKey("Invalid hexa string"); return; } *tmp2 = hexCharToInt(*tmp2); } else if (!hexStringToBinString(tmp2, &l2)) return; } tmp1 = malloc(l1); if(!tmp1) return; for (i = 0; i < l1 - l2 + 1; i += l2) memcpy(tmp1 + i, tmp2, l2); memcpy(tmp1 + i, tmp2, l1 - i); addToEdited(mark_min, l1, tmp1); readFile(); free(tmp1); }
// in: input string // in_length: length of the string // out: output string (needs to be already allocated). // out_length: length of the array // returns amount of bytes saved to 'out' or -1 if an error happened int hexStringToArray(char* in, int in_length, unsigned char* out, int out_length) { int i, out_pos; int chars[2]; char *input=in; unsigned char *output=out; if (in_length < 2 || out_length < (in_length / 3) + 1 || input == NULL || output == NULL) return -1; out_pos = 0; for (i = 0; i < in_length - 1; ++i) { if(input[i] == '-' || input[i] == ':' || input[i] == '_' || input[i] == ' ' || input[i] == '.') { continue; } // Check output array is big enough if(out_pos >= out_length) { return -1; } chars[0] = hexCharToInt(input[i]); // If first char is invalid (or '\0'), don't bother continuing (and you really shouldn't). if(chars[0] < 0 || chars[0] > 15) return -1; chars[1] = hexCharToInt(input[++i]); // It should always be a multiple of 2 hex characters with or without separator if(chars[1] < 0 || chars[1] > 15) return -1; output[out_pos++] = ((chars[0] << 4) + chars[1]) & 0xFF; } return out_pos; }
int hexToInt(std::string hexString) { int pos; int value = 0; bool isNeg = (hexString[0] == '-'); if (isNeg) hexString = hexString.substr(1); for (int i = hexString.size() - 1; i>= 0; i--) { pos = hexString.size() - i - 1; char ch = hexString[i]; value += hexCharToInt(ch) * (int)(pow(16, pos)); } if (isNeg) value *= -1; return value; }
void toDec(char *in, char form, int base, LLBin **list) { int i; for (i = strlen(in) - 1; i >= 0; i--) { int x = 0; /* Convert A-Fa-f to 10-15. */ if (base == HEX_BASE && !isdigit(in[i])) { x = hexCharToInt(in[i]); } else { x = in[i] - '0'; } toBin(x, form, list); } }
//Return the mac address bytes (or null if it's not a mac address) int getmac(char * macAddress, int strict, unsigned char * mac) { char byte[3]; int i, nbElem, n; if (macAddress == NULL) return 1; /* Minimum length */ if ((int)strlen(macAddress) < 12) return 1; memset(mac, 0, 6); byte[2] = 0; i = nbElem = 0; while (macAddress[i] != 0) { if (macAddress[i] == '\n' || macAddress[i] == '\r') break; byte[0] = macAddress[i]; byte[1] = macAddress[i+1]; if (sscanf( byte, "%x", &n ) != 1 && strlen(byte) == 2) return 1; if (hexCharToInt(byte[1]) < 0) return 1; mac[nbElem] = n; i+=2; nbElem++; if (macAddress[i] == ':' || macAddress[i] == '-' || macAddress[i] == '_') i++; } if ((strict && nbElem != 6) || (!strict && nbElem > 6)) return 1; return 0; }
int toDec(char *in, int base) { int result = 0; int power = 0; int i; for (i = strlen(in) - 1; i >= 0; i--) { int x; /* Convert A-Fa-f to 10-15. */ if (base == HEX_BASE && !isdigit(in[i])) { x = hexCharToInt(in[i]); } else { x = in[i] - '0'; } result += x * (pow(base, power++)); } return result; }
int main(int argc,char** argv){ FILE* inputHex; int fileSize; int byteCount; int addr; int highLowToggle = LOW_BYTE; int ndx; uint16_t data; char hexData[128]; inputHex = fopen(argv[1],"r"); setATmega(ATMEGA_328P); initializeSPI(); if(programmingEnable() != 0x53){ printf("Programmer Out of Sync!!!\n"); return -1; } else { printf("In sync!\n"); } //printf("Erasing chip\n"); if(argv[2][0] == 'e'){ chipErase(); return 1; } //writeProgramMemoryByte(0,0xFF,LOW_BYTE); //chipErase(); usleep(25000); //printf("Fuse check: %hhX\n",readFuseBits()); //return 1; /*if(programmingEnable() != 0x53){ printf("Out of Sync!\n"); return -1; } else { printf("In sync!\n"); }*/ while(feof(inputHex) == 0){ fgets(hexData,128,inputHex); //printf("parsing %s\n",hexData); if(hexData[0] != ':'){ printf("Error in hex file!\n"); } byteCount = hexCharToInt(hexData[1]) << 4; byteCount |= hexCharToInt(hexData[2]); addr = hexCharToInt(hexData[3]) << 12; addr |= hexCharToInt(hexData[4]) << 8; addr |= hexCharToInt(hexData[5]) << 4; addr |= hexCharToInt(hexData[6]); addr /= 2; if(hexData[8] == '1'){ break; } else if(hexData[8] != '0'){ printf("Unsupported Hex file\n"); break; } ndx = 9; while(byteCount){ data = hexCharToInt(hexData[ndx]) << 4; ndx++; data |= hexCharToInt(hexData[ndx]); ndx++; data |= hexCharToInt(hexData[ndx]) << 12; ndx++; data |= hexCharToInt(hexData[ndx]) << 8; //printf("Writing byte %hX to address %hX\n",data,addr); writeProgramMemory(addr,data); usleep(4000); ndx++; addr++; byteCount -= 2; } } finishProgramming(); byteCount = 0; while(byteCount < 0x88){ printf("%X: %hX\n",byteCount,readProgramMemory(byteCount)); //printf("%X: %hhX %hhX\n",byteCount,readProgramMemoryByte(byteCount,HIGH_BYTE),readProgramMemoryByte(byteCount,LOW_BYTE)); byteCount++; } fclose(inputHex); }
//! \param encodedByte Must point to at least two ASCII hex characters. //! uint8_t hexByteToInt(const char *encodedByte) { return (hexCharToInt(encodedByte[0]) << 4) | hexCharToInt(encodedByte[1]); }
// // convert a hex byte (two characters) into 8 bit unsigned integer // uint8_t hexByteToInt(char hi, char lo) { return ( (hexCharToInt(hi) << 4) + hexCharToInt(lo) ); // return the unsigned integer }
// // parse the current s-record in the buffer // uint8_t parseSrecBuffer(char *thisBuffer) { uint8_t srecBytecount, srecChecksum, srecType; uint32_t srecAddress; uint8_t tmpAddress; char hi, lo; char c1, c2; uint16_t i; // check if current buffer is a data record (starts with S1, S2 or S3) if(*thisBuffer++ == 'S' ) { // get s-record type srecType = hexCharToInt(*thisBuffer++); // only process the record in case it's a data record if((srecType == 1) || (srecType == 2) || (srecType == 3)) { // get the byte count hi = *thisBuffer++; lo = *thisBuffer++; srecBytecount = hexByteToInt(hi, lo); // one could directly put *thisBuffer++ into the arguments, // but the arguments are put on stack last first, i.e. the // lo character is fetched from the *thisBuffer first and // this changes lo and hi character! Using seperate variables // hi and lo is more clear and readable than changing the // sequence in the hexByteToInt function. // check if byte count is larger than 0x43, i.e. we have more // than 64 bytes in the record -> not allowed if(srecBytecount > (0x43 + srecType - 1)) { uartPutChar('B'); // 'B' indicates this error return FALSE; } srecChecksum = srecBytecount; // add byte count to checksum srecAddress = 0; // reset s-record address // extract the address depending of s-record type for(i = 0; i <= srecType; ++i) { hi = *thisBuffer++; lo = *thisBuffer++; tmpAddress = hexByteToInt(hi, lo); // get next address byte srecAddress <<= 8; // shift existing address one byte left srecAddress += tmpAddress; // add new lower address byte srecChecksum += tmpAddress; // add address portion to checksum } // read all data bytes for(i = 0; i < (srecBytecount - 3 - (srecType - 1)); i += 2) { // assemble a 16 bit little endian data word and calculate checksum hi = *thisBuffer++; lo = *thisBuffer++; c1 = hexByteToInt(hi, lo); srecChecksum += c1; hi = *thisBuffer++; lo = *thisBuffer++; c2 = hexByteToInt(hi, lo); srecChecksum += c2; #ifdef EEPROM_CODE if(programThisMemory == FLASH) { #endif // write word to flash write buffer boot_page_fill(srecAddress + i, (c2 << 8) + c1); #ifdef EEPROM_CODE } else { // write hi and lo byte into eeprom buffer eepromWriteBuffer[srecAddress + i] = c1; eepromWriteBuffer[srecAddress + i + 1] = c2; } #endif // This counter decrements from SPM_PAGESIZE to two, // when two is reached, the flash page may be written. // If it goes below two, the bytecount of an s-record // was not a integer factor of the flash page size. // See description on top of this file. if((flashPageSizeCounter -= 2) == 0) { uartPutChar('P'); // 'P' indicates this error return FALSE; } } // get checksum and compare to 0xff hi = *thisBuffer++; lo = *thisBuffer++; srecChecksum += hexByteToInt(hi, lo); // compare checksum to 0xff if(srecChecksum != 0xff) { uartPutChar('C'); // if checksum is wrong, 'C' indicates this error return FALSE; } uartPutChar('.'); // '.' indicates some progress } // check if end of file record if((srecType == 9) || (srecType == 8) || (srecType == 7)) { srecEndOfFile = TRUE; } } // check if either page size counter is two (i.e. buffer is full) // or end of file was reached (i.e. the previously received // bytes must be written to flash) if((flashPageSizeCounter == 2) || (srecEndOfFile == TRUE)){ #ifdef EEPROM_CODE if(programThisMemory == FLASH) { #endif boot_page_erase(writeBaseAddress); // do a page erase boot_spm_busy_wait(); // wait for page erase done boot_page_write(writeBaseAddress); // do a page write boot_spm_busy_wait(); // wait for write completed boot_rww_enable(); // reenable rww section again #ifdef EEPROM_CODE } if(programThisMemory == EEPROM) { eeprom_busy_wait(); // wait for eeprom no longer busy eeprom_write_block(eepromWriteBuffer, (void *)(uint16_t)writeBaseAddress, SPM_PAGESIZE); // write the block to eeprom } #endif flashPageSizeCounter = SPM_PAGESIZE + 2; // set byte counter to correct value (see declaration at beginning) writeBaseAddress += SPM_PAGESIZE; } return TRUE; }
bool EdifyTokenString::unescape(const std::string &str, std::string *out) { std::string output; for (std::size_t i = 0; i < str.size();) { char c = str[i]; if (c == '\\') { if (i == str.size() - 1) { // Escape character is last character return false; } std::size_t new_i = i + 2; if (str[i + 1] == 'a') { output += '\a'; } else if (str[i + 1] == 'b') { output += '\b'; } else if (str[i + 1] == 'f') { output += '\f'; } else if (str[i + 1] == 'n') { output += '\n'; } else if (str[i + 1] == 'r') { output += '\r'; } else if (str[i + 1] == 't') { output += '\t'; } else if (str[i + 1] == 'v') { output += '\v'; } else if (str[i + 1] == '\\') { output += '\\'; } else if (str[i + 1] == 'x') { if (str.size() - i < 4) { // Need 4 chars: \xYY return false; } int digit1 = hexCharToInt(str[i + 2]); int digit2 = hexCharToInt(str[i + 3]); if (digit1 < 0 || digit2 < 0) { // One of the chars is not a valid hex character return false; } char val = (digit1 << 4) & digit2; output += val; new_i += 2; } else { // Invalid escape char return false; } i = new_i; } else { output += c; i += 1; } } out->swap(output); return true; }