⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hex2bin.cpp

📁 在高通的手机平台下,一个下载手机.bin文件到手机的flash中的工具,包含PC端的程序代码和运行在基带处理器中的代码.
💻 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 + -