CMake/Source/cmHexFileConverter.cxx

217 lines
5.3 KiB
C++
Raw Permalink Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmHexFileConverter.h"
#include <stdio.h>
#include <string.h>
#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)
// might go to SystemTools ?
static bool cm_IsHexChar(char c)
{
return (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
((c >= 'A') && (c <= 'F')));
}
static unsigned int ChompStrlen(const char* line)
{
2016-06-27 23:44:16 +03:00
if (line == CM_NULLPTR) {
return 0;
}
unsigned int length = static_cast<unsigned int>(strlen(line));
if ((line[length - 1] == '\n') || (line[length - 1] == '\r')) {
length--;
}
if ((line[length - 1] == '\n') || (line[length - 1] == '\r')) {
length--;
}
return length;
}
static bool OutputBin(FILE* file, const char* buf, unsigned int startIndex,
unsigned int stopIndex)
{
bool success = true;
char hexNumber[3];
hexNumber[2] = '\0';
char outBuf[256];
unsigned int outBufCount = 0;
for (unsigned int i = startIndex; i < stopIndex; i += 2) {
hexNumber[0] = buf[i];
hexNumber[1] = buf[i + 1];
unsigned int convertedByte = 0;
if (sscanf(hexNumber, "%x", &convertedByte) != 1) {
success = false;
break;
}
2009-09-30 19:42:40 +04:00
outBuf[outBufCount] = static_cast<char>(convertedByte & 0xff);
outBufCount++;
}
if (success) {
success = (fwrite(outBuf, 1, outBufCount, file) == outBufCount);
}
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);
if ((slen < MOTOROLA_SREC_MIN_LINE_LENGTH) ||
(slen > MOTOROLA_SREC_MAX_LINE_LENGTH)) {
return false;
}
// line length must be even
if (slen % 2 == 1) {
return false;
}
if (buf[0] != 'S') {
return false;
}
unsigned int dataStart = 0;
// ignore extra address records
if ((buf[1] == '5') || (buf[1] == '7') || (buf[1] == '8') ||
(buf[1] == '9')) {
return true;
2016-09-16 23:45:24 +03:00
}
if (buf[1] == '1') {
dataStart = 8;
} else if (buf[1] == '2') {
dataStart = 10;
} else if (buf[1] == '3') {
dataStart = 12;
} else // unknown record type
{
return false;
}
// 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);
if ((slen < INTEL_HEX_MIN_LINE_LENGTH) ||
(slen > INTEL_HEX_MAX_LINE_LENGTH)) {
return false;
}
// line length must be odd
if (slen % 2 == 0) {
return false;
}
if ((buf[0] != ':') || (buf[7] != '0')) {
return false;
}
unsigned int dataStart = 0;
if ((buf[8] == '0') || (buf[8] == '1')) {
dataStart = 9;
}
// ignore extra address records
else if ((buf[8] == '2') || (buf[8] == '3') || (buf[8] == '4') ||
(buf[8] == '5')) {
return true;
} else // unknown record type
{
return false;
}
// ignore the last two bytes (checksum)
return OutputBin(outFile, buf, dataStart, slen - 2);
}
cmHexFileConverter::FileType cmHexFileConverter::DetermineFileType(
const char* inFileName)
{
char buf[1024];
FILE* inFile = cmsys::SystemTools::Fopen(inFileName, "rb");
2016-06-27 23:44:16 +03:00
if (inFile == CM_NULLPTR) {
return Binary;
}
if (!fgets(buf, 1024, inFile)) {
2009-10-02 23:30:01 +04:00
buf[0] = 0;
}
fclose(inFile);
FileType type = Binary;
unsigned int minLineLength = 0;
unsigned int maxLineLength = 0;
if (buf[0] == ':') // might be an intel hex file
{
type = IntelHex;
minLineLength = INTEL_HEX_MIN_LINE_LENGTH;
maxLineLength = INTEL_HEX_MAX_LINE_LENGTH;
} else if (buf[0] == 'S') // might be a motorola srec file
{
type = MotorolaSrec;
minLineLength = MOTOROLA_SREC_MIN_LINE_LENGTH;
maxLineLength = MOTOROLA_SREC_MAX_LINE_LENGTH;
} else {
return Binary;
}
unsigned int slen = ChompStrlen(buf);
if ((slen < minLineLength) || (slen > maxLineLength)) {
return Binary;
}
for (unsigned int i = 1; i < slen; i++) {
if (!cm_IsHexChar(buf[i])) {
return Binary;
}
}
return type;
}
bool cmHexFileConverter::TryConvert(const char* inFileName,
const char* outFileName)
{
FileType type = DetermineFileType(inFileName);
if (type == Binary) {
return false;
}
// try to open the file
FILE* inFile = cmsys::SystemTools::Fopen(inFileName, "rb");
FILE* outFile = cmsys::SystemTools::Fopen(outFileName, "wb");
2016-06-27 23:44:16 +03:00
if ((inFile == CM_NULLPTR) || (outFile == CM_NULLPTR)) {
if (inFile != CM_NULLPTR) {
fclose(inFile);
}
2016-06-27 23:44:16 +03:00
if (outFile != CM_NULLPTR) {
fclose(outFile);
}
return false;
}
// convert them line by line
bool success = false;
char buf[1024];
2016-06-27 23:44:16 +03:00
while (fgets(buf, 1024, inFile) != CM_NULLPTR) {
if (type == MotorolaSrec) {
success = ConvertMotorolaSrecLine(buf, outFile);
} else if (type == IntelHex) {
success = ConvertIntelHexLine(buf, outFile);
}
if (success == false) {
break;
}
}
// close them again
fclose(inFile);
fclose(outFile);
return success;
}