Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
StIntelHexFile.h
1 /*
2  * Copyright (c) 2013-15, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_StIntelHexFile_h_)
31 #define _StIntelHexFile_h_
32 
33 #include "stdafx.h"
34 #include <istream>
35 #include <string>
36 #include <vector>
37 #include <stdexcept>
38 
39 enum
40 {
42  INTELHEX_START_CHAR = ':',
43 
46  INTELHEX_MIN_LENGTH = 11,
47 
49  INTELHEX_ADDRESS_START_CHAR_INDEX = 3,
50 
52  INTELHEX_TYPE_START_CHAR_INDEX = 7,
53 
55  INTELHEX_DATA_START_CHAR_INDEX = 9
56 };
57 
59 enum
60 {
62  INTELHEX_RECORD_DATA = 0x00,
63 
66  INTELHEX_RECORD_END_OF_FILE = 0x01,
67 
69  INTELHEX_RECORD_EXTENDED_SEGMENT_ADDRESS = 0x02,
70 
72  INTELHEX_RECORD_START_SEGMENT_ADDRESS = 0x03,
73 
75  INTELHEX_RECORD_EXTENDED_LINEAR_ADDRESS = 0x04,
76 
78  INTELHEX_RECORD_START_LINEAR_ADDRESS = 0x05
79 };
80 
91 {
92 public:
96  struct IntelHex
97  {
98  unsigned m_dataCount;
99  uint32_t m_address;
100  unsigned m_type;
101  uint8_t *m_data;
107  uint8_t m_checksum;
108  };
109 
111  typedef std::vector<IntelHex>::const_iterator const_iterator;
112 
113 public:
115  StIntelHexFile(std::istream &inStream);
116 
118  virtual ~StIntelHexFile();
119 
121 
122  virtual void setName(const std::string &inName) { m_name = inName; }
123  virtual std::string getName() const { return m_name; }
125 
127 
128  virtual bool isIntelHexFile();
130 
132  virtual void parse();
134 
136 
137  inline unsigned getRecordCount() const { return static_cast<unsigned>(m_records.size()); }
140  inline const_iterator getBegin() const { return m_records.begin(); }
141  inline const_iterator getEnd() const { return m_records.end(); }
143 
145 
146  inline const IntelHex &operator[](unsigned inIndex) { return m_records[inIndex]; }
148 
149 protected:
150  std::istream &m_stream;
151  std::vector<IntelHex> m_records;
152 
153  std::string m_name;
154 
156 
157  virtual void parseLine(std::string &inLine);
158 
159  bool isHexDigit(char c);
160  int hexDigitToInt(char digit);
161  int readHexByte(std::string &inString, int inIndex);
163 };
164 
168 class StIntelHexParseException : public std::runtime_error
169 {
170 public:
172  StIntelHexParseException(const std::string &inMessage)
173  : std::runtime_error(inMessage)
174  {
175  }
176 };
177 
178 #endif // _StIntelHexFile_h_
unsigned getRecordCount() const
Definition: StIntelHexFile.h:138
StIntelHexFile(std::istream &inStream)
Constructor.
Definition: StIntelHexFile.cpp:37
virtual void parseLine(std::string &inLine)
Parses individual Intel Hex.
Definition: StIntelHexFile.cpp:153
STL namespace.
uint8_t m_checksum
The checksum byte used to verify the record.
Definition: StIntelHexFile.h:107
std::vector< IntelHex >::const_iterator const_iterator
Iterator type.
Definition: StIntelHexFile.h:111
virtual void parse()
Parse the entire IntelHex input stream.
Definition: StIntelHexFile.cpp:79
std::istream & m_stream
The input stream for the Intel Hex data.
Definition: StIntelHexFile.h:150
virtual ~StIntelHexFile()
Destructor.
Definition: StIntelHexFile.cpp:43
unsigned m_type
Definition: StIntelHexFile.h:100
StIntelHexParseException(const std::string &inMessage)
Default constructor.
Definition: StIntelHexFile.h:172
std::string m_name
File name. (optional)
Definition: StIntelHexFile.h:153
Definition: StIntelHexFile.h:96
uint8_t * m_data
Pointer to data, or NULL if no data for this record.
Definition: StIntelHexFile.h:106
int readHexByte(std::string &inString, int inIndex)
Definition: StIntelHexFile.cpp:133
const_iterator getBegin() const
Definition: StIntelHexFile.h:140
virtual bool isIntelHexFile()
Determine if the file is a Intel Hex file.
Definition: StIntelHexFile.cpp:58
Simple exception thrown to indicate an error in the input Intel Hex data format.
Definition: StIntelHexFile.h:168
unsigned m_dataCount
The number of bytes in the data field.
Definition: StIntelHexFile.h:98
Intel Hex parser.
Definition: StIntelHexFile.h:90
std::vector< IntelHex > m_records
Vector of Intel Hex in the input data.
Definition: StIntelHexFile.h:151
uint32_t m_address
The address offset of the data.
Definition: StIntelHexFile.h:99