ROSE  0.11.131.0
SRecord.h
1 #ifndef ROSE_BinaryAnalysis_SRecord_H
2 #define ROSE_BinaryAnalysis_SRecord_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 namespace Rose {
7 namespace BinaryAnalysis {
8 
27 class SRecord {
28 public:
30  enum Syntax {
34  };
35 
37  enum Type {
38  // Motorola
50  // Intel
59  };
60 
61 private:
62  Type type_;
63  rose_addr_t addr_;
64  std::vector<uint8_t> data_;
65  std::string error_;
66 
68  // Constructors, etc.
70 public:
72  SRecord(): type_(SREC_UNKNOWN_TYPE), addr_(0) {}
73 
77  SRecord(Type type, rose_addr_t addr, const std::vector<uint8_t> data)
78  : type_(type), addr_(addr), data_(data) {}
79  SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
80  : type_(type), addr_(addr), data_(buffer+0, buffer+bufsz) {}
84  // High-level functions.
87 public:
91  static SRecord parse(const std::string&);
92 
97  static std::vector<SRecord> parse(std::istream&);
98 
103  static AddressIntervalSet dataAddresses(const std::vector<SRecord>&);
104 
116  load(const std::vector<SRecord>&, const MemoryMap::Ptr&, bool createSegments=true,
117  unsigned accessPerms=MemoryMap::READABLE|MemoryMap::WRITABLE|MemoryMap::EXECUTABLE,
118  const std::string &newSegmentNames = "S-Records");
119 
121  static std::vector<SRecord> create(const MemoryMap::Ptr&, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4);
122 
124  std::string toString() const;
125 
129  void print(std::ostream&) const;
130  friend std::ostream& operator<<(std::ostream&, const SRecord&);
133  // S-Record properties.
136 public:
140  Syntax syntax() const;
141 
145  Type type() const { return type_; }
146  SRecord& type(Type type) { type_ = type; return *this; }
157  rose_addr_t address() const { return addr_; }
158  SRecord& address(rose_addr_t addr) { addr_ = addr; return *this; }
164  const std::vector<uint8_t>& data() const { return data_; }
165  std::vector<uint8_t>& data() { return data_; }
166  SRecord& data(const std::vector<uint8_t>& data) { data_ = data; return *this; }
172  const std::string& error() const { return error_; }
173  SRecord& error(const std::string &error) { error_ = error; return *this; }
178  // Low-level functions.
181 public:
184  Sawyer::Optional<rose_addr_t> dataVa_, executionVa_;
185  size_t nDataRecords_;
186  public:
189  : nDataRecords_(0) {}
190 
194  void insert(const SRecord&);
195 
198 
202  rose_addr_t baseDataAddress() const;
203 
206 
208  size_t nDataRecords() const { return nDataRecords_; }
209  };
210 
212  static SRecord parseMotorola(const std::string&);
213 
215  static SRecord parseIntel(const std::string&);
216 
223  static Sawyer::Optional<rose_addr_t> parseBigEndianInteger(const std::string&, size_t start, size_t nChars);
224 
226  static unsigned bigEndian(const std::vector<uint8_t> &data, size_t nBytes);
227 
231  static size_t addressNBytes(Type);
232 
237  static uint8_t checksum(Syntax, const std::string&, size_t start, size_t n);
238 };
239 
240 } // namespace
241 } // namespace
242 
243 #endif
244 #endif
static Sawyer::Optional< rose_addr_t > parseBigEndianInteger(const std::string &, size_t start, size_t nChars)
Parse a hexadecimal string to an unsigned integer.
const std::string & error() const
Property: record error messsage.
Definition: SRecord.h:172
Motorola 16-bit count of data records.
Definition: SRecord.h:44
Motorola 24-bit starting address.
Definition: SRecord.h:47
static unsigned bigEndian(const std::vector< uint8_t > &data, size_t nBytes)
Convert the first few bytes of the buffer to a big-endian integer.
rose_addr_t address() const
Property: record starting address.
Definition: SRecord.h:157
void print(std::ostream &) const
Print an S-Record to a stream.
SRecord & address(rose_addr_t addr)
Property: record starting address.
Definition: SRecord.h:158
static SRecord parse(const std::string &)
Construct an S-Record by parsing a string.
Intel starting linear address.
Definition: SRecord.h:56
Intel extended linear address.
Definition: SRecord.h:55
const std::vector< uint8_t > & data() const
Property: record data.
Definition: SRecord.h:164
Motorola 32-bit starting address.
Definition: SRecord.h:46
Motorola data with 16-bit address.
Definition: SRecord.h:40
Motorola data with 32-bit address.
Definition: SRecord.h:42
static size_t addressNBytes(Type)
Size of address in bytes.
static SRecord parseMotorola(const std::string &)
Construct a Motorola S-Record by parsing a string.
SRecord()
Construct an empty S-Record.
Definition: SRecord.h:72
Main namespace for the ROSE library.
static std::vector< SRecord > create(const MemoryMap::Ptr &, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4)
Create S-Records from a memory map.
Motorola data with 24-bit address.
Definition: SRecord.h:41
friend std::ostream & operator<<(std::ostream &, const SRecord &)
Print an S-Record to a stream.
Motorola 24-bit count of data records.
Definition: SRecord.h:45
Sawyer::Optional< rose_addr_t > executionAddress() const
Returns the execution starting address if one was specified.
Intel extended segment address.
Definition: SRecord.h:53
S-Record hexadecimal data formats.
Definition: SRecord.h:27
std::vector< uint8_t > & data()
Property: record data.
Definition: SRecord.h:165
Syntax syntax() const
Property: syntax family.
Intel starting segment address.
Definition: SRecord.h:54
static Sawyer::Optional< rose_addr_t > load(const std::vector< SRecord > &, const MemoryMap::Ptr &, bool createSegments=true, unsigned accessPerms=MemoryMap::READABLE|MemoryMap::WRITABLE|MemoryMap::EXECUTABLE, const std::string &newSegmentNames="S-Records")
Load S-Records into a memory map.
static uint8_t checksum(Syntax, const std::string &, size_t start, size_t n)
Compute a checksum for part of a record.
Motorola 16-bit starting address.
Definition: SRecord.h:48
Sawyer::Optional< rose_addr_t > dataAddress(const SRecord &) const
Returns the memory address for the start of a data record, or nothing.
Motorola syntax S-Record.
Definition: SRecord.h:31
Type
Type of S-Record.
Definition: SRecord.h:37
SRecord & error(const std::string &error)
Property: record error messsage.
Definition: SRecord.h:173
std::string toString() const
Convert an S-Record to a string.
SRecord(Type type, rose_addr_t addr, const std::vector< uint8_t > data)
Construct an S-Record with data.
Definition: SRecord.h:77
static AddressIntervalSet dataAddresses(const std::vector< SRecord > &)
Addresses occupied by all data.
SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
Construct an S-Record with data.
Definition: SRecord.h:79
SRecord & data(const std::vector< uint8_t > &data)
Property: record data.
Definition: SRecord.h:166
Run-length encoding information.
Definition: SRecord.h:183
void insert(const SRecord &)
Insert an S-Record into the data sumarized by this class.
static SRecord parseIntel(const std::string &)
Construct an Intel S-Record by parsing a string.
Type type() const
Property: record type.
Definition: SRecord.h:145
SRecord & type(Type type)
Property: record type.
Definition: SRecord.h:146
size_t nDataRecords() const
Number of data records seen.
Definition: SRecord.h:208
rose_addr_t baseDataAddress() const
Base data address.
Syntax
Syntax of the S-Record.
Definition: SRecord.h:30
Motorola vendor-specific header, usually NUL-terminated ASCII.
Definition: SRecord.h:39