StIntelHexFile.h
/*
* Copyright (c) 2013-15, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(_StIntelHexFile_h_)
#define _StIntelHexFile_h_
#include "stdafx.h"
#include <istream>
#include <string>
#include <vector>
#include <stdexcept>
enum
{
INTELHEX_START_CHAR = ':',
INTELHEX_MIN_LENGTH = 11,
INTELHEX_ADDRESS_START_CHAR_INDEX = 3,
INTELHEX_TYPE_START_CHAR_INDEX = 7,
INTELHEX_DATA_START_CHAR_INDEX = 9
};
enum
{
INTELHEX_RECORD_DATA = 0x00,
INTELHEX_RECORD_END_OF_FILE = 0x01,
INTELHEX_RECORD_EXTENDED_SEGMENT_ADDRESS = 0x02,
INTELHEX_RECORD_START_SEGMENT_ADDRESS = 0x03,
INTELHEX_RECORD_EXTENDED_LINEAR_ADDRESS = 0x04,
INTELHEX_RECORD_START_LINEAR_ADDRESS = 0x05
};
class StIntelHexFile
{
public:
struct IntelHex
{
unsigned m_dataCount;
uint32_t m_address;
unsigned m_type;
uint8_t *m_data;
uint8_t m_checksum;
};
typedef std::vector<IntelHex>::const_iterator const_iterator;
public:
StIntelHexFile(std::istream &inStream);
virtual ~StIntelHexFile();
virtual void setName(const std::string &inName) { m_name = inName; }
virtual std::string getName() const { return m_name; }
virtual bool isIntelHexFile();
virtual void parse();
inline unsigned getRecordCount() const { return static_cast<unsigned>(m_records.size()); }
inline const_iterator getBegin() const { return m_records.begin(); }
inline const_iterator getEnd() const { return m_records.end(); }
inline const IntelHex &operator[](unsigned inIndex) { return m_records[inIndex]; }
protected:
std::istream &m_stream;
std::vector<IntelHex> m_records;
std::string m_name;
virtual void parseLine(std::string &inLine);
bool isHexDigit(char c);
int hexDigitToInt(char digit);
int readHexByte(std::string &inString, int inIndex);
};
class StIntelHexParseException : public std::runtime_error
{
public:
StIntelHexParseException(const std::string &inMessage)
: std::runtime_error(inMessage)
{
}
};
#endif // _StIntelHexFile_h_
StIntelHexFile::getRecordCount
unsigned getRecordCount() const
Definition: StIntelHexFile.h:138
StIntelHexFile::StIntelHexFile
StIntelHexFile(std::istream &inStream)
Constructor.
Definition: StIntelHexFile.cpp:37
StIntelHexFile::parseLine
virtual void parseLine(std::string &inLine)
Parses individual Intel Hex.
Definition: StIntelHexFile.cpp:153
std
STL namespace.
StIntelHexFile::IntelHex::m_checksum
uint8_t m_checksum
The checksum byte used to verify the record.
Definition: StIntelHexFile.h:107
StIntelHexFile::const_iterator
std::vector< IntelHex >::const_iterator const_iterator
Iterator type.
Definition: StIntelHexFile.h:111
StIntelHexFile::parse
virtual void parse()
Parse the entire IntelHex input stream.
Definition: StIntelHexFile.cpp:79
StIntelHexFile::m_stream
std::istream & m_stream
The input stream for the Intel Hex data.
Definition: StIntelHexFile.h:150
StIntelHexFile::~StIntelHexFile
virtual ~StIntelHexFile()
Destructor.
Definition: StIntelHexFile.cpp:43
StIntelHexFile::IntelHex::m_type
unsigned m_type
Definition: StIntelHexFile.h:100
StIntelHexParseException::StIntelHexParseException
StIntelHexParseException(const std::string &inMessage)
Default constructor.
Definition: StIntelHexFile.h:172
StIntelHexFile::m_name
std::string m_name
File name. (optional)
Definition: StIntelHexFile.h:153
StIntelHexFile::IntelHex
Definition: StIntelHexFile.h:96
StIntelHexFile::IntelHex::m_data
uint8_t * m_data
Pointer to data, or NULL if no data for this record.
Definition: StIntelHexFile.h:106
StIntelHexFile::readHexByte
int readHexByte(std::string &inString, int inIndex)
Definition: StIntelHexFile.cpp:133
StIntelHexFile::getBegin
const_iterator getBegin() const
Definition: StIntelHexFile.h:140
StIntelHexFile::isIntelHexFile
virtual bool isIntelHexFile()
Determine if the file is a Intel Hex file.
Definition: StIntelHexFile.cpp:58
StIntelHexParseException
Simple exception thrown to indicate an error in the input Intel Hex data format.
Definition: StIntelHexFile.h:168
StIntelHexFile::IntelHex::m_dataCount
unsigned m_dataCount
The number of bytes in the data field.
Definition: StIntelHexFile.h:98
StIntelHexFile
Intel Hex parser.
Definition: StIntelHexFile.h:90
StIntelHexFile::m_records
std::vector< IntelHex > m_records
Vector of Intel Hex in the input data.
Definition: StIntelHexFile.h:151
StIntelHexFile::IntelHex::m_address
uint32_t m_address
The address offset of the data.
Definition: StIntelHexFile.h:99