srecord  1.65.0
msbin.h
Go to the documentation of this file.
1 //
2 // srecord - manipulate eprom load files
3 // Copyright (C) 2009-2011 Peter Miller
4 //
5 // Code contribution by David Kozub <zub@linux.fjfi.cvut.cz>
6 // Copyright assigned to Peter Miller 27-Jan-2010.
7 //
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or (at
11 // your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 
22 #ifndef SRECORD_OUTPUT_FILE_MSBIN_H
23 #define SRECORD_OUTPUT_FILE_MSBIN_H
24 
25 #include <stdint.h>
26 #include <srecord/output/file.h>
27 #include <srecord/record.h>
28 #include <vector>
29 
30 namespace srecord
31 {
32 
33 /**
34  * The srecord::output_file_msbin class is used to represent an output
35  * file in Windows CE Binary Image Data Format.
36  *
37  * See http://msdn.microsoft.com/en-us/library/ms924510.aspx
38  * for a description of the format.
39  */
41  public output_file
42 {
43 public:
44  /**
45  * The destructor.
46  */
47  virtual ~output_file_msbin();
48 
49 private:
50  /**
51  * The constructor.
52  *
53  * @param file_name
54  * The file name to open and write output to.
55  */
56  output_file_msbin(const std::string &file_name);
57 
58 public:
59  /**
60  * The create class method is used to create new dynamically
61  * allocated instances of this class.
62  *
63  * @param file_name
64  * The file name to open to write data to. The name "-" is
65  * understood to mean the standard output.
66  */
67  static pointer create(const std::string &file_name);
68 
69 protected:
70  // See base class for documentation.
71  void write(const record &);
72 
73  // See base class for documentation.
74  void line_length_set(int);
75 
76  // See base class for documentation.
77  void address_length_set(int);
78 
79  // See base class for documentation.
81 
82  // See base class for documentation.
83  bool preferred_block_size_set(int nbytes);
84 
85  // See base class for documentation.
86  const char *format_name() const;
87 
88  // See base class for documentation.
89  void notify_upper_bound(unsigned long addr);
90 
91  // See base class for documentation.
92  bool is_binary(void) const;
93 
94 private:
95  /**
96  * MAX_PENDING_DATA_SIZE is the upper limit of the size of data
97  * we are willing to keep in the pending_records vector.
98  * (50MiB)
99  */
100  enum { MAX_PENDING_DATA_SIZE = 50*1024*1024 };
101 
102  /**
103  * The write_dword_le method is used to write a little endian
104  * double word into the output.
105  *
106  * @param d
107  * The data to be written.
108  */
109  void write_dword_le(uint32_t d);
110 
111  /**
112  * The checksum method is used to calculate the checksum of a given
113  * data block.
114  *
115  * The checksum is additive, so:
116  * checkum([Block1,Block2]) = checksum(Block1) + checksum(Block2)
117  *
118  * @param data
119  * The data to be check-summed.
120  * @param len
121  * The length of the data to be check-summed.
122  */
123  static uint32_t checksum(const unsigned char *data, size_t len);
124 
125  /**
126  * The write_file_header method is used to write the file header
127  * (including the magic bytes) into the output.
128  *
129  * @param start
130  * The least address in the file.
131  * @param length
132  * The span of addresses in the file.
133  */
134  void write_file_header(uint32_t start, uint32_t length);
135 
136  /**
137  * The write_record_header method is used to write a record header
138  * into the output.
139  *
140  * @param addr
141  * The address of the first by of the record.
142  * @param length
143  * The number of bytes of data in the record.
144  * @param checksum
145  * The checksum of the data bytes in the record.
146  */
147  void write_record_header(uint32_t addr, uint32_t length, uint32_t checksum);
148 
149  /**
150  * The write_data method is used to write the data contrained in
151  * a record to the output.
152  *
153  * @param r
154  * The record containing the data to be written to output.
155  */
156  void write_data(const record &r);
157 
158  /**
159  * The flush_pending_records method is used to write out all the
160  * data contained in the vector pending_records plus optionally
161  * a record r to the output, forming a single record in the output.
162  * The vector pending_records is cleared.
163  *
164  * @param r
165  * Optional pointer to a record that should be treated as if
166  * it's the last element of pending_records vector.
167  */
168  void flush_pending_records(const record *r = NULL);
169 
170  /**
171  * The append_pending_record handles addind a new record. If the
172  * new record could form a single continuous record in the output
173  * file and this would fit in the MAX_PENDING_DATA_SIZE limit,
174  * it's appended to the pending_records vector. Otherwise,
175  * pending records are written to the output and depending on the
176  * size of the record to be added, it's either copied to the
177  * pending_records vector, or written directly to the output.
178  *
179  * @param r
180  * The record to be added.
181  */
182  void append_pending_record(const record &r);
183 
184  /**
185  * The start_address_set instance variable is used to remember
186  * whether the #start_address instance variable is valid.
187  *
188  * Becomes true once we encounter a record of type
189  * record::type_execution_start_address.
190  */
191  bool start_address_set;
192 
193  /**
194  * The start_address instance variable is used to remember
195  * the execution start address, taken from a record of type
196  * record::type_execution_start_address.
197  */
198  record::address_t start_address;
199 
200  /**
201  * The upper_bound instance variable is used to remember the upper
202  * bound address as received via the #notify_upper_bound method.
203  */
204  record::address_t upper_bound;
205 
206  /**
207  * The beginning_of_file instance variable is used to remember
208  * that we haven't written the file header yet.
209  */
210  bool beginning_of_file;
211 
212  /**
213  * Type holding copies of records.
214  */
215  typedef std::vector<std::shared_ptr<record> > record_vector;
216 
217  /**
218  * The pending_records instance variable is used to keep pending
219  * records before they can be written to the output file.
220  * This is needed to implement the concatenation of adjacent
221  * records.
222  */
223  record_vector pending_records;
224 
225  /**
226  * The default constructor. Do not use.
227  */
229 
230  /**
231  * The copy constructor. Do not use.
232  */
234 
235  /**
236  * The copy constructor. Do not use.
237  */
238  output_file_msbin &operator=(const output_file_msbin &);
239 };
240 
241 };
242 
243 #endif // SRECORD_OUTPUT_FILE_MSBIN_H
The srecord::output_file_msbin class is used to represent an output file in Windows CE Binary Image D...
Definition: msbin.h:42
void write(const record &)
The write method is used to write a recordonto an output.
bool preferred_block_size_set(int nbytes)
The preferred_block_size_set method is is to set a precific number of bytes for the preferred block s...
void address_length_set(int)
The address_length_set method is used to set the minimum number of bytes to be written for addresses ...
int preferred_block_size_get() const
The preferred_block_size_get method is used to get the proferred block size of the output fformat.
virtual ~output_file_msbin()
The destructor.
void line_length_set(int)
The set_line_length method is used to set the maximum length of an output line, for those formats for...
void notify_upper_bound(unsigned long addr)
The notify_upper_bound method is used to notify the output class of the upper bound (highest address ...
static pointer create(const std::string &file_name)
The create class method is used to create new dynamically allocated instances of this class.
bool is_binary(void) const
The is_binary method is used to to determine whether or not a file format is binary (true) of text (f...
const char * format_name() const
The format_name method is used to obtain the name of this output format.
The srecord::output_file class is used to represent a generic output file.
Definition: file.h:35
int checksum
The checksum instance variable is used record the running checksum.
Definition: file.h:369
std::shared_ptr< output > pointer
Definition: output.h:41
The srecord::record class is used to represent a data record read from a file.
Definition: record.h:35
uint32_t address_t
The type of record addresses.
Definition: record.h:58