ROSE  0.11.2.0
SRecord.h
1 #ifndef ROSE_BinaryFormats_SRecord_H
2 #define ROSE_BinaryFormats_SRecord_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 namespace Rose {
8 namespace BinaryAnalysis {
9 
28 class SRecord {
29 public:
31  enum Syntax {
35  };
36 
38  enum Type {
39  // Motorola
51  // Intel
60  };
61 
62 private:
63  Type type_;
64  rose_addr_t addr_;
65  std::vector<uint8_t> data_;
66  std::string error_;
67 
69  // Constructors, etc.
71 public:
73  SRecord(): type_(SREC_UNKNOWN_TYPE), addr_(0) {}
74 
78  SRecord(Type type, rose_addr_t addr, const std::vector<uint8_t> data)
79  : type_(type), addr_(addr), data_(data) {}
80  SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
81  : type_(type), addr_(addr), data_(buffer+0, buffer+bufsz) {}
85  // High-level functions.
88 public:
92  static SRecord parse(const std::string&);
93 
98  static std::vector<SRecord> parse(std::istream&);
99 
104  static AddressIntervalSet dataAddresses(const std::vector<SRecord>&);
105 
117  load(const std::vector<SRecord>&, const MemoryMap::Ptr&, bool createSegments=true,
118  unsigned accessPerms=MemoryMap::READABLE|MemoryMap::WRITABLE|MemoryMap::EXECUTABLE,
119  const std::string &newSegmentNames = "S-Records");
120 
122  static std::vector<SRecord> create(const MemoryMap::Ptr&, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4);
123 
125  std::string toString() const;
126 
130  void print(std::ostream&) const;
131  friend std::ostream& operator<<(std::ostream&, const SRecord&);
134  // S-Record properties.
137 public:
141  Syntax syntax() const;
142 
146  Type type() const { return type_; }
147  SRecord& type(Type type) { type_ = type; return *this; }
158  rose_addr_t address() const { return addr_; }
159  SRecord& address(rose_addr_t addr) { addr_ = addr; return *this; }
165  const std::vector<uint8_t>& data() const { return data_; }
166  std::vector<uint8_t>& data() { return data_; }
167  SRecord& data(const std::vector<uint8_t>& data) { data_ = data; return *this; }
173  const std::string& error() const { return error_; }
174  SRecord& error(const std::string &error) { error_ = error; return *this; }
179  // Low-level functions.
182 public:
185  Sawyer::Optional<rose_addr_t> dataVa_, executionVa_;
186  size_t nDataRecords_;
187  public:
190  : nDataRecords_(0) {}
191 
195  void insert(const SRecord&);
196 
199 
203  rose_addr_t baseDataAddress() const;
204 
207 
209  size_t nDataRecords() const { return nDataRecords_; }
210  };
211 
213  static SRecord parseMotorola(const std::string&);
214 
216  static SRecord parseIntel(const std::string&);
217 
224  static Sawyer::Optional<rose_addr_t> parseBigEndianInteger(const std::string&, size_t start, size_t nChars);
225 
227  static unsigned bigEndian(const std::vector<uint8_t> &data, size_t nBytes);
228 
232  static size_t addressNBytes(Type);
233 
238  static uint8_t checksum(Syntax, const std::string&, size_t start, size_t n);
239 };
240 
241 } // namespace
242 } // namespace
243 
244 #endif
245 #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:173
Motorola 16-bit count of data records.
Definition: SRecord.h:45
Motorola 24-bit starting address.
Definition: SRecord.h:48
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:158
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:159
static SRecord parse(const std::string &)
Construct an S-Record by parsing a string.
Intel starting linear address.
Definition: SRecord.h:57
Intel extended linear address.
Definition: SRecord.h:56
const std::vector< uint8_t > & data() const
Property: record data.
Definition: SRecord.h:165
Motorola 32-bit starting address.
Definition: SRecord.h:47
Motorola data with 16-bit address.
Definition: SRecord.h:41
Motorola data with 32-bit address.
Definition: SRecord.h:43
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:73
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:42
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:46
Sawyer::Optional< rose_addr_t > executionAddress() const
Returns the execution starting address if one was specified.
Intel extended segment address.
Definition: SRecord.h:54
S-Record hexadecimal data formats.
Definition: SRecord.h:28
std::vector< uint8_t > & data()
Property: record data.
Definition: SRecord.h:166
Syntax syntax() const
Property: syntax family.
Intel starting segment address.
Definition: SRecord.h:55
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:49
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:32
Type
Type of S-Record.
Definition: SRecord.h:38
SRecord & error(const std::string &error)
Property: record error messsage.
Definition: SRecord.h:174
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:78
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:80
SRecord & data(const std::vector< uint8_t > &data)
Property: record data.
Definition: SRecord.h:167
Run-length encoding information.
Definition: SRecord.h:184
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:146
SRecord & type(Type type)
Property: record type.
Definition: SRecord.h:147
size_t nDataRecords() const
Number of data records seen.
Definition: SRecord.h:209
rose_addr_t baseDataAddress() const
Base data address.
Syntax
Syntax of the S-Record.
Definition: SRecord.h:31
Motorola vendor-specific header, usually NUL-terminated ASCII.
Definition: SRecord.h:40