目录
前言
最近研究了一下hex文件的用法。主要用途是配合STVP进行量产工具的开发应用。因为涉及到常量的修改不想编译和修改或者应用再无法修改原始文件的地方 可以通过提前记录对应的常量偏移来进行修改。
需求:修改指定偏移位置的hex文件 并将此行的记录校验修正。
下面是我对网上提供的一些源码基础上进行的代码修改
这里是我的试验工程代码有兴趣可以查阅
一、Hex是什么?
网上对于hex文件的说明和分析已经很详细了。这里就不多啰嗦了。
二、使用步骤
1.引入HexLexer类
HexLexer.cpp
#pragma once
#include "stdafx.h"
#include "HexLexer.h"
//#include <iostream>
//using namespace std;
//获取记录标识
Hex::Hex(char mark)
{
this->m_cRecordMark = mark;
m_cBuffer[0] = '\0';
//m_pBuffer = NULL;
m_nRecordLength = 0;
m_pLoadOffset = NULL;
m_pRecordType = NULL;
m_pData = NULL;
m_pChecksum = NULL;
m_bRecvStatus = false;
//m_nIndex = 0;
}
Hex::~Hex()
{
delete m_pLoadOffset, m_pRecordType, m_pData, m_pChecksum;
}
char Hex::GetRecordMark()
{
return this->m_cRecordMark;
}
//获取每条记录的长度
size_t Hex::GetRecordLength()
{
//char *len = (char*)malloc(sizeof(char)* 3);
if (strlen(m_cBuffer) >= 2)
{
char len[3];
len[0] = m_cBuffer[0];
len[1] = m_cBuffer[1];
len[2] = '\0';
char *p = NULL;
return strtol(len, &p, 16);
}
else
{
return 0;
}
}
//获取装载偏移
char* Hex::GetLoadOffset()
{
if (strlen(m_cBuffer) == (GetRecordLength() + 5) * 2)
{
char *offset = (char*)malloc(sizeof(char) * 5);
for (int i = 0; i < 4; ++i)
{
offset[i] = m_cBuffer[i + 2];
}
offset[4] = '\0';
m_pLoadOffset = offset;
offset = NULL;
}
return m_pLoadOffset;
}
//获取记录类型
char* Hex::GetRecordType()
{
if (strlen(m_cBuffer) == (GetRecordLength() + 5) * 2)
{
char *type = (char*)malloc(sizeof(char) * 3);
type[0] = m_cBuffer[6];
type[1] = m_cBuffer[7];
type[2] = '\0';
m_pRecordType = type;
type = NULL;
}
return m_pRecordType;
}
//获取数据
char* Hex::GetData()
{
if (strlen(m_cBuffer) == (GetRecordLength() + 5) * 2)
{
int len = GetRecordLength();
char *data = (char*)malloc(sizeof(char)*(len * 2 + 1));
for (int i = 0; i < len * 2; ++i)
{
data[i] = m_cBuffer[i + 8];
}
data[len * 2] = '\0';
m_pData = data;
data = NULL;
}
return m_pData;
}
//获取校验和
char* Hex::GetChecksum()
{
int len = GetRecordLength();
if (strlen(m_cBuffer) == (len + 5) * 2)
{
char *checksum = (char*)malloc(sizeof(char) * 3);
checksum[0] = m_cBuffer[(len + 5) * 2 - 2];
checksum[1] = m_cBuffer[(len + 5) * 2 - 1];
checksum[2] = '\0';
m_pChecksum = checksum;
checksum = NULL;
}
return m_pChecksum;
}
//解析Hex文件中的每一条记录
void Hex::ParseRecord(char ch)
{
size_t buf_len = strlen(m_cBuffer);
if (GetRecordMark() == ch)
{
m_bRecvStatus = true;
m_cBuffer[0] = '\0';
//m_nIndex = 0;
return;
}
if ((buf_len == (GetRecordLength() + 5) * 2 - 1))
{
//接收最后一个字符
m_cBuffer[buf_len] = ch;
m_cBuffer[buf_len + 1] = '\0';
//检验接收的数据
char temp[3];
char *p = NULL;
long int checksum = 0;
for (size_t i = 0; i < strlen(m_cBuffer); i += 2)
{
temp[0] = m_cBuffer[i];
temp[1] = m_cBuffer[i + 1];
temp[2] = '\0';
checksum += strtol(temp, &p, 16);
temp[0] = '\0';
}
checksum &= 0x00ff;//取计算结果的低8位
if (checksum == 0)//checksum为0说明接收的数据无误
{
cout <<GetRecordMark();//输入记录标记
GetLoadOffset();//获取偏移项
//你可以在这个地方进新hex文件的修改
ModifyRecord(1, (char*)"C010", "66");
ModifyRecord(13, (char*)"C020","66");//修改指定的偏移数据
cout << m_cBuffer << endl;
}
else//否则接收数据有误
{
cout << "Error!" << endl;
}
m_cBuffer[0] = '\0';
m_bRecvStatus = false;
m_nRecordLength = 0;
m_pLoadOffset = NULL;
m_pRecordType = NULL;
m_pChecksum = NULL;
}
else if (m_bRecvStatus)
{
m_cBuffer[buf_len] = ch;
m_cBuffer[buf_len + 1] = '\0';
//m_nIndex++;
}
}
//解析Hex文件
void Hex::ParseHex(char *data)
{
for (size_t i = 0; i < strlen(data); ++i)
{
ParseRecord(data[i]);
}
}
//重新校验此行数据
char* Hex::GetChecksumEx()
{
//校验方法
//020000040003F7
//0x100 - ((0x02 + 0x00 + 0x00 + 0x04 + 0x00 + 0x03) % 256) = 0xF7
//检验接收的数据
char temp[3] = { '\0', '\0', '\0' };
char* p = NULL;
long int checksum = 0;
int len = strlen(m_cBuffer) - 2;
for (size_t i = 0; i < len; i += 2)
{
temp[0] = m_cBuffer[i];
temp[1] = m_cBuffer[i + 1];
temp[2] = '\0';
checksum += strtol(temp, &p, 16);
temp[0] = '\0';
}
checksum %= 256;
checksum = 0x100 - checksum;
itoa(checksum, temp, 16);
m_pChecksum = strupr(temp);
return m_pChecksum;
}
void Hex::ModifyRecord(int nModifyOffset, char* cOffsetData, char* cModifyData)//修改某一行指定的偏移的数据
{
if (nModifyOffset<0|| nModifyOffset>16)
{
return;//偏移位置有误则返回
}
//(13-1)*2 24+9
int nOffset = (nModifyOffset - 1) * 2 + 9;//计算偏移位置
if (!strcmp(m_pLoadOffset, cOffsetData))//对比偏移数据是否一致
{
//修改此行的记录数据
m_cBuffer[nOffset] = cModifyData[0];
m_cBuffer[nOffset-1] = cModifyData[1];
GetChecksumEx();//获取修改后的校验数据
//修正校验位
m_cBuffer[43 - 3] = m_pChecksum[0];
m_cBuffer[43 - 2] = m_pChecksum[1];
}
}
void Hex::ModifyHex(char* SrcPath, char* DesPath)
{
FILE* fp_in = freopen(SrcPath, "r", stdin);
rewind(fp_in);
FILE* fp_out = freopen(DesPath, "w", stdout);
rewind(fp_out);
char ch;
while (1)
{
ch = fgetc(fp_in);
ParseRecord(ch);
if (feof(fp_in))
{
break;
}
}
fclose(stdout);
fclose(stdin);
}
#if 0
int main(int argc, char *argv[])
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
Hex hex(':');
char ch;
while (cin >> ch)
{
hex.ParseRecord(ch);
}
fclose(stdout);
fclose(stdin);
return 0;
}
#endif
HexLexer.h
#pragma once
#ifndef _HEXLEXER_H_
#define _HEXLEXER_H_
#include <cstdio>
#include <cstring>
#include <cstdlib>
/*
Intel Hex文件解析器V1.0
Hex文件的格式如下:
RecordMark RecordLength LoadOffset RecordType Data Checksum
在Intel Hex文件中,RecordMark规定为“:”
*/
#pragma warning(disable:4996)
#define MAX_BUFFER_SIZE 43
class Hex
{
public:
Hex(char mark);
~Hex();
void ParseHex(char *data);//解析Hex文件
void ParseRecord(char ch);//解析每一条记录
size_t GetRecordLength();//获取记录长度
char GetRecordMark();//获取记录标识
char *GetLoadOffset();//获取内存装载偏移
char *GetRecordType();//获取记录类型
char *GetData();//获取数据
char *GetChecksum();//获取校验和
char* GetChecksumEx();//获取新的校验
void ModifyRecord(int nModifyOffset,char* cOffsetData, char* cModifyData);//修改某一行指定的偏移的数据
void ModifyHex(char* SrcPath, char* DesPath);//修改文件
private:
char m_cBuffer[MAX_BUFFER_SIZE];//存储待解析的记录
char m_cRecordMark;//记录标识
size_t m_nRecordLength;//记录长度
char *m_pLoadOffset;//装载偏移
char *m_pRecordType;//记录类型
char *m_pData;//数据字段
char *m_pChecksum;//校验和
bool m_bRecvStatus;//接收状态标识
//size_t m_nIndex;//缓存的字符索引值
};
#endif
2.调用类
Hex hex(':');
hex.ModifyHex("in.hex", "out.hex");