std::string readkey(const char* keyfile){ if(keyread){return "";} keyread=true; std::ifstream keyin(keyfile); // Open for reading if(!keyin.is_open()){ return "key.lmk not found"; } Array<std::string> f; std::string st; while(getline(keyin,st)){ f.pushBack(st); } for(int i=0; i<RSA_KEY_BYTES; i++){ std::string t=f[0].substr(i*3,2); file_public_modulus[i]=hexdec(t); } for(int i=0; i<RSA_KEY_BYTES; i++){ std::string t=f[1].substr(i*3,2); file_private_exponent[i]=hexdec(t); } keyin.close(); return ""; }
string csstidy::unicode(string& istring,int& i) { ++i; string add = ""; bool replaced = false; while(i < istring.length() && (ctype_xdigit(istring[i]) || ctype_space(istring[i])) && add.length()< 6) { add += istring[i]; if(ctype_space(istring[i])) { break; } i++; } if(hexdec(add) > 47 && hexdec(add) < 58 || hexdec(add) > 64 && hexdec(add) < 91 || hexdec(add) > 96 && hexdec(add) < 123) { string msg = "Replaced unicode notation: Changed \\" + rtrim(add) + " to "; add = static_cast<int>(hexdec(add)); msg += add; log(msg,Information); replaced = true; } else { add = trim("\\" + add); } if(ctype_xdigit(istring[i+1]) && ctype_space(istring[i]) && !replaced || !ctype_space(istring[i])) { i--; } if(add != "\\" || !settings["remove_bslash"] || in_str_array(tokens,istring[i+1])) { return add; } if(add == "\\") { log("Removed unnecessary backslash",Information); } return ""; }
int readHEX(const char* file, uint8* bout, unsigned long max_length, uint8* pages_used) { static const uint32 HEX_DATA_OFFSET = 4; uint8 linebin[256] = {0}; uint8* data = (linebin + HEX_DATA_OFFSET); uint8 hex_crc, hex_type, hex_len; uint32 hex_addr; uint32 hex_base_addr = 0; uint32 hex_words = 0; uint32 f_addr = 0; uint32 o_addr = 0; uint32 num_words = 0; char line[512] = {0}; char *pc; char *pline = line + 1; int res = 0; int binlen = 0; int line_no = 0; int i = 0; FILE* fp = fopen(file, "rb"); if( !fp ) { return -1; } while( !feof(fp) && fgets(line, sizeof(line) - 1, fp) ) { line_no++; if( line[0] != ':' ) { break; } res = strlen(pline); pc = pline + res - 1; while( pc > pline && *pc <= ' ' ) { *pc-- = 0; res--; } if( res & 0x01 || res > 512 || res < 10) { fprintf(stderr, "Incorrect number of characters on line %d:%d\n", line_no, res); return -1; } hex_crc = 0; for( pc = pline, i = 0; i<res; i+=2, pc+=2 ) { linebin[i >> 1] = hexdec(pc); hex_crc += linebin[i >> 1]; } binlen = res / 2; if( hex_crc != 0 ) { fprintf(stderr, "Checksum does not match, line %d\n", line_no); return -1; } hex_addr = (linebin[1] << 8) | linebin[2]; hex_len = linebin[0]; hex_type = linebin[3]; if( binlen - (1 + 2 + 1 + hex_len + 1) != 0 ) { fprintf(stderr, "Incorrect number of bytes, line %d\n", line_no); return -1; } if( hex_type == 0x00 ) { f_addr = (hex_base_addr | (hex_addr)) / 2; //PCU if( hex_len % 4 ) { fprintf(stderr, "Misaligned data, line %d\n", line_no); return -1; } else if( f_addr >= flashsize ) { fprintf(stderr, "Current record address is higher than maximum allowed, line %d\n", line_no); return -1; } hex_words = hex_len / 4; o_addr = (f_addr / 2) * PIC_WORD_SIZE; //BYTES for( i=0; i<hex_words; i++) { bout[o_addr + 0] = data[(i*4) + 2]; bout[o_addr + 1] = data[(i*4) + 0]; bout[o_addr + 2] = data[(i*4) + 1]; pages_used[ (o_addr / PIC_PAGE_SIZE) ] = 1; o_addr += PIC_WORD_SIZE; num_words ++; } } else if ( hex_type == 0x04 && hex_len == 2) { hex_base_addr = (linebin[4] << 24) | (linebin[5] << 16); } else if ( hex_type == 0x01 ) { break; //EOF } else { fprintf(stderr, "Unsupported record type %02x, line %d\n", hex_type, line_no); return -1; } } fclose(fp); return num_words; }
int readHEX(const char* file, uint8* bout, unsigned long max_length, uint8* pages_used) { static const uint32 HEX_DATA_OFFSET = 4; uint8 linebin[256] = {0}; uint8* data = (linebin + HEX_DATA_OFFSET); uint8 hex_crc, hex_type, hex_len; uint32 hex_addr; uint32 hex_base_addr = 0; uint32 hex_words = 0; uint32 f_addr = 0; uint32 o_addr = 0; uint32 num_words = 0; char line[512] = {0}; char *pc; char *pline = line + 1; //location in the line of the start of the length???? int res = 0; int binlen = 0; int line_no = 0; int i = 0; FILE* fp = fopen(file, "rb"); if( !fp ) { return -1; } while( !feof(fp) && fgets(line, sizeof(line) - 1, fp) ) { line_no++;//increment line counter if( line[0] != ':' ) {//if the line doesn't start with : then the HEX is mangled somehow break; } res = strlen(pline); pc = pline + res - 1; while( pc > pline && *pc <= ' ' ) { //???get address or length???? *pc-- = 0; res--; } if( res & 0x01 || res > 512 || res < 10) { fprintf(stderr, "Incorrect number of characters on line %d:%d\n", line_no, res); return -1; } hex_crc = 0; for( pc = pline, i = 0; i<res; i+=2, pc+=2 ) { linebin[i >> 1] = hexdec(pc);//crawl line two bytes at a time and suck up the HEX digits, put in linebin[i/2] hex_crc += linebin[i >> 1];//handle CRC } binlen = res / 2; if( hex_crc != 0 ) { //each byte plus the checksum (final hex number) is 0 fprintf(stderr, "Checksum does not match, line %d\n", line_no); return -1; } hex_addr = (linebin[1] << 8) | linebin[2]; //this line address in program space hex_len = linebin[0]; //length of the data hex_type = linebin[3]; //HEX record type if( binlen - (1 + 2 + 1 + hex_len + 1) != 0 ) { fprintf(stderr, "Incorrect number of bytes, line %d\n", line_no); return -1; } if( hex_type == 0x00 ) { f_addr = (hex_base_addr | (hex_addr)) / 2; //PCU if( hex_len % 4 ) { fprintf(stderr, "Misaligned data, line %d\n", line_no); return -1; } else if( f_addr >= PIC_FLASHSIZE ) { fprintf(stderr, "Current record address is higher than maximum allowed, line %d\n", line_no); return -1; } hex_words = hex_len / 2; //not 4... o_addr = (f_addr) * PIC_WORD_SIZE; //BYTES for( i=0; i<hex_words; i++) { bout[o_addr + 0] = data[(i*2) + 0];//2 not 4 bout[o_addr + 1] = data[(i*2) + 1];//2 not 4 //bout[o_addr + 2] = data[(i*4) + 1]; // printf("Fad: %d Oad: %d HW: %d W1: %X W2: %X \n", f_addr, o_addr,hex_words,bout[o_addr + 0],bout[o_addr + 1] ); pages_used[ (o_addr / PIC_PAGE_SIZE) ] = 1; o_addr += PIC_WORD_SIZE; num_words ++; } } else if ( hex_type == 0x04 && hex_len == 2) { hex_base_addr = (linebin[4] << 24) | (linebin[5] << 16); } else if ( hex_type == 0x01 ) { break; //EOF } else { fprintf(stderr, "Unsupported record type %02x, line %d\n", hex_type, line_no); return -1; } } fclose(fp); //puts("Raw HEX..."); //dumpHex(bout,64); return num_words; }