?? hex2bin.cpp
字號:
// NAME: Hex2Bin.cpp
//--------------------------------------------------------------------------------------------------
//
// GENERAL DESCRIPTION
//
// This module provides the utility that converts the phone S/W to binary from hex format.
//--------------------------------------------------------------------------------------------------
//
// Revision History:
// Modification Tracking
// Author Date Number Description of Changes
// ------------------------- ------------ ---------- ----------------------------------------
//
//
// Portability:
// This module can be portable to other C++ compilers or Win32 platforms.
//
//--------------------------------------------------------------------------------------------------
// INCLUDE FILES
//--------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "Hex2Bin.h"
#include "common.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std;
//--------------------------------------------------------------------------------------------------
// LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
//--------------------------------------------------------------------------------------------------
typedef struct {
BYTE mark;
BYTE len[2];
BYTE offset[4];
BYTE type[2];
} HEADER;
//--------------------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------------------------------------------
CHex2Bin theCnvrt; //the unique instance for the application
//--------------------------------------------------------------------------------------------------
// CLASS IMPLEMENTATION
//--------------------------------------------------------------------------------------------------
//
// Construction/Destruction
//--------------------------------------------------------------------------------------------------
CHex2Bin::CHex2Bin()
{
m_bType = FALSE;
}
CHex2Bin::~CHex2Bin()
{
}
//--------------------------------------------------------------------------------------------------
// External Member Fucntions
//--------------------------------------------------------------------------------------------------
// FUNCTION:
// CHex2Bin::GoGoGo
// DESCRIPTION:
// This function converts the phone hex file to the binary file and save with the same file name
// except for the extension name replaces with "bin".
// ARGUMENTS PASSED:
// strFile the name of the hex file
// RETURN VALUE:
// TRUE
// FALSE
// PRE-CONDITIONS:
// None
// POST-CONDITIONS:
// None
// IMPORTANT NOTES:
// None
//--------------------------------------------------------------------------------------------------
BOOL
CHex2Bin::GoGoGo(CString strHexName)
{
char pBinName[200];
strcpy(pBinName, strHexName);
BYTE cLen = strlen(pBinName);
pBinName[cLen -3] = 'b';
pBinName[cLen -2] = 'i';
pBinName[cLen -1] = 'n';
FILE* pHexFile;
if ( (pHexFile = fopen((LPCTSTR)strHexName, "rt")) == NULL )
return FALSE;
DWORD dwHexSize = FileSize(pHexFile);
FILE* pBinFile;
if ( (pBinFile = fopen(pBinName, "wb")) == NULL )
{
fclose(pHexFile);
return FALSE;
}
m_segment = CODE_BASE;
BYTE* filePtr = new BYTE[dwHexSize];
if(filePtr == NULL)
{
cout << "memory new error in CHex2Bin::GoGoGo!" << endl;
exit(1);
}
LONG size = fread(filePtr, 1, dwHexSize, pHexFile);
HEADER header;
for (LONG i=0; i<size; )
{
strncpy((char*)(&header), (char*)(filePtr+i), sizeof(HEADER));
if (header.mark != ':')
{
delete filePtr;
fclose(pHexFile);
fclose(pBinFile);
return FALSE;
}
BYTE len = htoi(header.len);
BYTE offstr[2];
offstr[0] = htoi(header.offset);
offstr[1] = htoi(&(header.offset[2]));
LONG offset = (((LONG)(offstr[0]))<<8) | offstr[1];
BYTE type = htoi(header.type);
LONG checksum = 0x00000000;
for (unsigned j=1; j<sizeof(HEADER)+2*(len+1); j+=2)
{
BYTE val = htoi(filePtr+i+j);
checksum += val;
}
if (checksum & 0x000000FF)
{
fclose(pHexFile);
fclose(pBinFile);
delete filePtr;
return FALSE;
}
switch (type)
{
case 0:
{
unsigned long segment;
segment = m_segment;
if (m_bType == TRUE)
segment >>= 12;
for (unsigned k=sizeof(HEADER); k<sizeof(HEADER)+2*len; k+=2)
{
LONG addr = segment + offset + (k-sizeof(HEADER))/2;
BYTE tmp = htoi(filePtr + i + k);
fwrite( &tmp,1, 1, pBinFile);
}
}
break;
case 1:
{
fclose(pHexFile);
fclose(pBinFile);
delete filePtr;
return TRUE;
}
case 2:
case 4:
Record_ExtAdd(filePtr+i, len, type);
break;
case 3:
case 5:
break;
default:
{
fclose(pHexFile);
fclose(pBinFile);
delete filePtr;
return FALSE;
}
}
i += sizeof(HEADER)+2*(len+1)+1;
}
delete filePtr;
fclose(pHexFile);
fclose(pBinFile);
return TRUE;
}
//--------------------------------------------------------------------------------------------------
// Internal Member Fucntions
//--------------------------------------------------------------------------------------------------
void
CHex2Bin::Record_ExtAdd(BYTE* ptr, int len, BYTE type)
{
m_bType = (type == 2)? TRUE: FALSE;
BYTE segstr[2];
segstr[0] = htoi(ptr+sizeof(HEADER));
segstr[1] = htoi(ptr+sizeof(HEADER)+2);
m_segment = 0x0;
}
BYTE
CHex2Bin::htoi(BYTE *str)
{
BYTE rtn[2];
rtn[1] = ((str[1] > '9')? str[1]-'A'+10: str[1]-'0');
rtn[0] = ((str[0] > '9')? str[0]-'A'+10: str[0]-'0');
return ((rtn[0]<<4) | rtn[1]);
}
LONG
CHex2Bin::FileSize(FILE *file)
{
LONG curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
return length;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -