2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2007-05-25 23:22:22 +04:00
|
|
|
#include "cmHexFileConverter.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
#define INTEL_HEX_MIN_LINE_LENGTH (1 + 8 + 2)
|
|
|
|
#define INTEL_HEX_MAX_LINE_LENGTH (1 + 8 + (256 * 2) + 2)
|
|
|
|
#define MOTOROLA_SREC_MIN_LINE_LENGTH (2 + 2 + 4 + 2)
|
|
|
|
#define MOTOROLA_SREC_MAX_LINE_LENGTH (2 + 2 + 8 + (256 * 2) + 2)
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// might go to SystemTools ?
|
|
|
|
static bool cm_IsHexChar(char c)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
|
|
|
|
((c >= 'A') && (c <= 'F')));
|
2007-05-25 23:22:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int ChompStrlen(const char* line)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (line == 0) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
unsigned int length = static_cast<unsigned int>(strlen(line));
|
|
|
|
if ((line[length - 1] == '\n') || (line[length - 1] == '\r')) {
|
2007-05-25 23:22:22 +04:00
|
|
|
length--;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if ((line[length - 1] == '\n') || (line[length - 1] == '\r')) {
|
2007-05-25 23:22:22 +04:00
|
|
|
length--;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
static bool OutputBin(FILE* file, const char* buf, unsigned int startIndex,
|
|
|
|
unsigned int stopIndex)
|
2007-05-25 23:22:22 +04:00
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
char hexNumber[3];
|
|
|
|
hexNumber[2] = '\0';
|
|
|
|
char outBuf[256];
|
2007-05-26 00:23:57 +04:00
|
|
|
unsigned int outBufCount = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int i = startIndex; i < stopIndex; i += 2) {
|
2007-05-25 23:22:22 +04:00
|
|
|
hexNumber[0] = buf[i];
|
2016-05-16 17:34:04 +03:00
|
|
|
hexNumber[1] = buf[i + 1];
|
2007-05-25 23:22:22 +04:00
|
|
|
unsigned int convertedByte = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (sscanf(hexNumber, "%x", &convertedByte) != 1) {
|
2007-05-25 23:22:22 +04:00
|
|
|
success = false;
|
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-09-30 19:42:40 +04:00
|
|
|
outBuf[outBufCount] = static_cast<char>(convertedByte & 0xff);
|
2007-05-25 23:22:22 +04:00
|
|
|
outBufCount++;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
success = (fwrite(outBuf, 1, outBufCount, file) == outBufCount);
|
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see http://www.die.net/doc/linux/man/man5/srec.5.html
|
|
|
|
static bool ConvertMotorolaSrecLine(const char* buf, FILE* outFile)
|
|
|
|
{
|
|
|
|
unsigned int slen = ChompStrlen(buf);
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((slen < MOTOROLA_SREC_MIN_LINE_LENGTH) ||
|
|
|
|
(slen > MOTOROLA_SREC_MAX_LINE_LENGTH)) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// line length must be even
|
2016-05-16 17:34:04 +03:00
|
|
|
if (slen % 2 == 1) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (buf[0] != 'S') {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
unsigned int dataStart = 0;
|
|
|
|
// ignore extra address records
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((buf[1] == '5') || (buf[1] == '7') || (buf[1] == '8') ||
|
|
|
|
(buf[1] == '9')) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (buf[1] == '1') {
|
2007-05-25 23:22:22 +04:00
|
|
|
dataStart = 8;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (buf[1] == '2') {
|
2007-05-25 23:22:22 +04:00
|
|
|
dataStart = 10;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (buf[1] == '3') {
|
2007-05-25 23:22:22 +04:00
|
|
|
dataStart = 12;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else // unknown record type
|
|
|
|
{
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// ignore the last two bytes (checksum)
|
|
|
|
return OutputBin(outFile, buf, dataStart, slen - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// see http://en.wikipedia.org/wiki/Intel_hex
|
|
|
|
static bool ConvertIntelHexLine(const char* buf, FILE* outFile)
|
|
|
|
{
|
|
|
|
unsigned int slen = ChompStrlen(buf);
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((slen < INTEL_HEX_MIN_LINE_LENGTH) ||
|
|
|
|
(slen > INTEL_HEX_MAX_LINE_LENGTH)) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// line length must be odd
|
2016-05-16 17:34:04 +03:00
|
|
|
if (slen % 2 == 0) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((buf[0] != ':') || (buf[7] != '0')) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
unsigned int dataStart = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((buf[8] == '0') || (buf[8] == '1')) {
|
2007-05-25 23:22:22 +04:00
|
|
|
dataStart = 9;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
// ignore extra address records
|
2016-05-16 17:34:04 +03:00
|
|
|
else if ((buf[8] == '2') || (buf[8] == '3') || (buf[8] == '4') ||
|
|
|
|
(buf[8] == '5')) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else // unknown record type
|
|
|
|
{
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// ignore the last two bytes (checksum)
|
2007-05-25 23:22:22 +04:00
|
|
|
return OutputBin(outFile, buf, dataStart, slen - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmHexFileConverter::FileType cmHexFileConverter::DetermineFileType(
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* inFileName)
|
2007-05-25 23:22:22 +04:00
|
|
|
{
|
|
|
|
char buf[1024];
|
2014-01-04 09:47:13 +04:00
|
|
|
FILE* inFile = cmsys::SystemTools::Fopen(inFileName, "rb");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (inFile == 0) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return Binary;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!fgets(buf, 1024, inFile)) {
|
2009-10-02 23:30:01 +04:00
|
|
|
buf[0] = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
fclose(inFile);
|
|
|
|
FileType type = Binary;
|
|
|
|
unsigned int minLineLength = 0;
|
|
|
|
unsigned int maxLineLength = 0;
|
|
|
|
if (buf[0] == ':') // might be an intel hex file
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2007-05-25 23:22:22 +04:00
|
|
|
type = IntelHex;
|
|
|
|
minLineLength = INTEL_HEX_MIN_LINE_LENGTH;
|
|
|
|
maxLineLength = INTEL_HEX_MAX_LINE_LENGTH;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (buf[0] == 'S') // might be a motorola srec file
|
|
|
|
{
|
2007-05-25 23:22:22 +04:00
|
|
|
type = MotorolaSrec;
|
|
|
|
minLineLength = MOTOROLA_SREC_MIN_LINE_LENGTH;
|
|
|
|
maxLineLength = MOTOROLA_SREC_MAX_LINE_LENGTH;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2007-05-25 23:22:22 +04:00
|
|
|
return Binary;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2007-05-26 00:23:57 +04:00
|
|
|
unsigned int slen = ChompStrlen(buf);
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((slen < minLineLength) || (slen > maxLineLength)) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return Binary;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int i = 1; i < slen; i++) {
|
|
|
|
if (!cm_IsHexChar(buf[i])) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return Binary;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmHexFileConverter::TryConvert(const char* inFileName,
|
|
|
|
const char* outFileName)
|
|
|
|
{
|
|
|
|
FileType type = DetermineFileType(inFileName);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (type == Binary) {
|
2007-05-25 23:22:22 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// try to open the file
|
2014-01-04 09:47:13 +04:00
|
|
|
FILE* inFile = cmsys::SystemTools::Fopen(inFileName, "rb");
|
|
|
|
FILE* outFile = cmsys::SystemTools::Fopen(outFileName, "wb");
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((inFile == 0) || (outFile == 0)) {
|
|
|
|
if (inFile != 0) {
|
2007-05-25 23:22:22 +04:00
|
|
|
fclose(inFile);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (outFile != 0) {
|
2007-05-25 23:22:22 +04:00
|
|
|
fclose(outFile);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// convert them line by line
|
|
|
|
bool success = false;
|
|
|
|
char buf[1024];
|
2016-05-16 17:34:04 +03:00
|
|
|
while (fgets(buf, 1024, inFile) != 0) {
|
|
|
|
if (type == MotorolaSrec) {
|
2007-05-25 23:22:22 +04:00
|
|
|
success = ConvertMotorolaSrecLine(buf, outFile);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (type == IntelHex) {
|
2007-05-25 23:22:22 +04:00
|
|
|
success = ConvertIntelHexLine(buf, outFile);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (success == false) {
|
2007-05-25 23:22:22 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-05-25 23:22:22 +04:00
|
|
|
|
|
|
|
// close them again
|
|
|
|
fclose(inFile);
|
|
|
|
fclose(outFile);
|
|
|
|
return success;
|
|
|
|
}
|