📄 ltkchecksumgenerate.cpp
字号:
/*****************************************************************************************
* Copyright (c) 2007 Hewlett-Packard Development Company, L.P.
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************************/
/************************************************************************
* FILE DESCR: Definitions of Checksum generate module
*
* CONTENTS:
* LTKCheckSumGenerate
* initCRC32Table
* reflect
* getCRC
* addHeaderInfo
* readMDTHeader
*
* AUTHOR: Vijayakumara M
*
* DATE: Aug 02, 2005
* CHANGE HISTORY:
* Author Date Description
************************************************************************/
/************************************************************************
* SVN MACROS
*
* $LastChangedDate: 2008-08-12 14:19:20 +0530 (Tue, 12 Aug 2008) $
* $Revision: 613 $
* $Author: sharmnid $
*
************************************************************************/
#include "LTKCheckSumGenerate.h"
#include "LTKMacros.h"
#include "LTKLoggerUtil.h"
#include "LTKConfigFileReader.h"
#include "LTKException.h"
#include "LTKOSUtil.h"
#include "LTKOSUtilFactory.h"
/*****************************************************************************
* AUTHOR : Vijayakumara M
* DATE : 26 July 2005
* NAME : LTKCheckSumGenerate
* DESCRIPTION : Constractor.
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*****************************************************************************/
LTKCheckSumGenerate::LTKCheckSumGenerate():
m_OSUtilPtr(LTKOSUtilFactory::getInstance())
{
initCRC32Table();
}
/*****************************************************************************
* AUTHOR : Nidhi Sharma
* DATE : 26 June 2008
* NAME : ~LTKCheckSumGenerate
* DESCRIPTION : Desctructor.
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*****************************************************************************/
LTKCheckSumGenerate::~LTKCheckSumGenerate()
{
delete m_OSUtilPtr;
}
/****************************************************************************
* AUTHOR : Vijayakumara M
* DATE : 26 July 2005
* NAME : initCRC32Table
* DESCRIPTION : Call this function only once to initialize the CRC table.
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
****************************************************************************/
void LTKCheckSumGenerate::initCRC32Table()
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKCheckSumGenerate::initCRC32Table()" << endl;
unsigned long ulPolynomial = 0x04c11db7;
// 256 values representing ASCII character codes.
for(int i = 0; i <= 0xFF; i++)
{
m_CRC32Table[i]=reflect(i, 8) << 24;
for (int j = 0; j < 8; j++)
m_CRC32Table[i] = (m_CRC32Table[i] << 1) ^ (m_CRC32Table[i] & (1 << 31) ? ulPolynomial : 0);
m_CRC32Table[i] = reflect(m_CRC32Table[i], 32);
}
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKCheckSumGenerate::initCRC32Table()" << endl;
}
/**********************************************************************************
* AUTHOR : Vijayakumara M
* DATE : 26 July 2005
* NAME : reflect
* DESCRIPTION : reflection is a requirement for the official CRC-32 standard.
* we can create CRCs without it, but they won't conform to the standard.
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
unsigned long LTKCheckSumGenerate::reflect(unsigned long ref, char ch)
{// Used only by initCRC32Table()
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKCheckSumGenerate::reflect()" << endl;
unsigned long value(0);
// Swap bit 0 for bit 7
// bit 1 for bit 6, etc.
for(int i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKCheckSumGenerate::reflect()" << endl;
return value;
}
/**********************************************************************************
* AUTHOR : Vijayakumara M
* DATE : 26 July 2005
* NAME : getCRC
* DESCRIPTION : Function to generate checkSum.
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int LTKCheckSumGenerate::getCRC(char* text)
{
// Pass a text string to this function and it will return the CRC.
// Once the lookup table has been filled in by the two functions above,
// this function creates all CRCs using only the lookup table.
// Be sure to use unsigned variables,
// because negative values introduce high bits
// where zero bits are required.
// Start out with all bits set high.
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKCheckSumGenerate::getCRC()" << endl;
unsigned int ulCRC(0xffffffff);
// Get the length.
int len = strlen(text);
// Save the text in the buffer.
unsigned char* buffer = (unsigned char*)text;
// Perform the algorithm on each character
// in the string, using the lookup table values.
while(len--)
{
ulCRC = (ulCRC >> 8) ^ m_CRC32Table[(ulCRC & 0xFF) ^ *buffer++];
}
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKCheckSumGenerate::getCRC()" << endl;
// Exclusive OR the result with the beginning value.
return ulCRC ^ 0xffffffff;
}
/**********************************************************************************
* AUTHOR : Srinivasa Vithal
* DATE : 21 November 2007
* NAME : addHeaderInfo
* DESCRIPTION : This function adds the Header information to the Model Data file.
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int LTKCheckSumGenerate::addHeaderInfo(const string& modelDataHeaderInfoFilePath,
const string& mdtFilePath,
const stringStringMap& headerInfo)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKCheckSumGenerate::addHeaderInfo()" << endl;
int nCRC; // Holds checks sum in decimal format
long testEndian = 1;
char *modelFileData = NULL; // Model File header Data.
//unsigned int hdLen[CKSUM_HDR_STR_LEN], offsetLen[CKSUM_HDR_STR_LEN];
char chSum[CKSUM_HDR_STR_LEN];
long modelFileInfoSize ;
string comment, heder; //Header comment,and the header data
stringVector strTokens;
ostringstream strHeaderContents1;
ostringstream strHeaderContents2;
string::size_type indx=0;
// Add the mandatory fields to the header Info
stringStringMap tempHeaderInfo = updateHeaderWithMandatoryFields(headerInfo);
ostringstream headInfo, newHeadInfo;
ifstream readFile(mdtFilePath.c_str(), ios::in | ios::binary);
if(!readFile )
{
LOG(LTKLogger::LTK_LOGLEVEL_ERR)<<"Error : "<< EMODEL_DATA_FILE_OPEN
<<": "<< "Error opening mdt file "<< mdtFilePath <<
" LTKCheckSumGenerate::addHeaderInfo()"<<endl;
LTKReturnError(EMODEL_DATA_FILE_OPEN);
}
// get fle size
readFile.seekg(0, ios::beg);
readFile.seekg(0, ios::end);
modelFileInfoSize = readFile.tellg();
readFile.seekg(0, ios::beg);
try
{
if(!modelDataHeaderInfoFilePath.empty())
{
LTKConfigFileReader inputHeaderFileReader(modelDataHeaderInfoFilePath);
const stringStringMap& tempCfgFileMap = inputHeaderFileReader.getCfgFileMap();
stringStringMap::const_iterator tempCfgFileIter = tempCfgFileMap.begin();
stringStringMap::const_iterator tempCfgFileIterEnd = tempCfgFileMap.end();
for(; tempCfgFileIter != tempCfgFileIterEnd ; ++tempCfgFileIter)
{
if(tempHeaderInfo.find(tempCfgFileIter->first) == tempHeaderInfo.end())
{
//inserting user-defined key-value pair
tempHeaderInfo[tempCfgFileIter->first] = tempCfgFileIter->second;
}
}
}
//Read Model Data File.
modelFileData = new char[modelFileInfoSize+1];
memset(modelFileData, '\0', modelFileInfoSize+1);
readFile.read(modelFileData, modelFileInfoSize+1);
readFile.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -