file_buffer文件缓存,file_size文件大小bbuf为目标缓存
ReadFile(hFile, file_buffer, file_size, &number, NULL);先将hex内容读入到文件缓存中,此时全是ASCII码,***号以前为统计所需缓存大小
和校验文件的正确性。
***以后为创建缓存,缓存。。。
unsigned int Begin_addr_seg = 0;
unsigned int Begin_addr_line = 0;
unsigned int Max_addr_seg = 0;
unsigned int Max_addr_line = 0;
bool SkipSeg = 0;
bool SkipLine = 0;
unsigned int Now_addr = 0;
unsigned int start_addr;
char Now_len;
unsigned char len;
unsigned char addr1=0, addr2=0, addr3=0, addr4=0;
char sum, rbyte;
CString pBuffer;
pBuffer = file_buffer;
unsigned int nEnd, n;
int fail = 0;
nEnd = pBuffer.Find(":00000001FF");
if(nEnd == 0) fail = 2; // find file end
nEnd--;
bin_length=0;
n = 0;
char Fill=0xff;
while(n < nEnd){
if(fail) break;
if(file_buffer[n] == ':'){
CharToByte(&file_buffer[n+1], &len);
CharToByte(&file_buffer[n+3], &addr1);
CharToByte(&file_buffer[n+5], &addr2);
start_addr = (addr1<<8) + addr2;
//-----checksum start
sum = 0;
for(i=0; i<(len+5); i++) { //5: length,addr1,addr2,label,checksum
CharToByte(&file_buffer[n+1+(i*2)], (unsigned char *)&rbyte);
sum += rbyte;
}
if(sum != 0) fail = 3;
//-----checksum end
switch(file_buffer[n+8]){
case '0': //Data record
if((SkipSeg == 0) && (SkipLine == 0)) {
if(start_addr >= Now_addr){
Now_addr = start_addr;
Now_len = len;
}
bin_length += len;
}
break;
case '1': //End of file record
fail = 4;
break;
case '2': //Extended segment address record
CharToByte(&file_buffer[n+9], &addr1);
CharToByte(&file_buffer[n+11], &addr2);
if(Max_addr_seg <= (((addr1<<8) + addr2) <<4)){
Max_addr_seg = ((addr1<<8) + addr2) <<4;
SkipSeg = 0;
Now_addr = 0;
}else{
SkipSeg = 1;
}
break;
case '4': //Extended linear address record
CharToByte(&file_buffer[n+9], &addr1);
CharToByte(&file_buffer[n+11], &addr2);
if(Max_addr_line <=((addr1<<8)+addr2) << 16){
Max_addr_line = ((addr1<<8) + addr2) << 16;
SkipLine = 0;
Now_addr = 0;
}else{
SkipLine = 1;
}
break;
case '3': //Start segment address record
CharToByte(&file_buffer[n+9], &addr1);
CharToByte(&file_buffer[n+11], &addr2);
CharToByte(&file_buffer[n+13], &addr3);
CharToByte(&file_buffer[n+15], &addr4);
Begin_addr_seg = (addr1<<24) + (addr2<<16) +(addr3<<8) + addr4;
break;
case '5': //Start linear address record
CharToByte(&file_buffer[n+9], &addr1);
CharToByte(&file_buffer[n+11], &addr2);
Begin_addr_line = ((addr1<<8) + addr2) << 16;
break;
default:
fail = 4;
break;
}
n += len*2 + 11;
}else{
n++;
}
}
***************************************************************************
if(fail == 0){
Now_addr += Now_len;
if(Now_addr >= bin_length)
bin_length = Now_addr;
#if 0
this->BinLength = bin_length;
if((dfile == NULL) && (dbuf == NULL)){
fail = 8; // no output only read bin file length
}
#endif
bbuf = new char [bin_length];
for(i=0; i<bin_length; i++)
bbuf[i] = Fill;
i=0;
Max_addr_seg = 0;
Max_addr_line = 0;
n = 0;
while(n < nEnd){
if(fail) break;
if(file_buffer[n] == ':'){
CharToByte(&file_buffer[n+1], &len);
CharToByte(&file_buffer[n+3], &addr1);
CharToByte(&file_buffer[n+5], &addr2);
start_addr = (addr1<<8) + addr2;
switch(file_buffer[n+8]){
case '0': //Data record
//start_addr += Max_addr_seg + 16;
for(i=0; i<len; i++){
CharToByte(&file_buffer[n + 9 + (i*2)], (unsigned char *)&rbyte);
bbuf[start_addr+i] = rbyte;
//??? Begin_addr_seg Begin_addr_line
}
break;
case '1': //End of file record
fail = 4;
break;
case '2': //Extended segment address record
CharToByte(&file_buffer[n+9], &addr1);
CharToByte(&file_buffer[n+11], &addr2);
Max_addr_seg = ((addr1<<8) + addr2) <<4;
break;
case '4': //Extended linear address record
CharToByte(&file_buffer[n+9], &addr1);
CharToByte(&file_buffer[n+11], &addr2);
Max_addr_line = ((addr1<<8) + addr2) << 16;
break;
case '3': //Start segment address record
case '5': //Start linear address record
break;
default:
fail = 4;
break;
}
n += len*2+11;
}else{
n++;
}
}
}
delete[] file_buffer;
ADDLog("文件读取到缓存");