1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 zen::firmware_entry::firmware_entry(bool big_endian
)
25 : _big_endian(big_endian
)
29 zen::firmware_entry::firmware_entry(const firmware_entry
& copy
)
34 zen::firmware_entry
& zen::firmware_entry::operator=(const firmware_entry
& right
)
41 bool zen::firmware_entry::read(std::istream
& is
)
44 is
.read((char*)&_header
, sizeof(firmware_header_t
));
48 // If the firmware is big-endian, swap the header values to little-endian.
51 _header
.tag
= shared::swap(_header
.tag
);
52 if (_header
.tag
!= 'NULL')
54 _header
.size
= shared::swap(_header
.size
);
58 // Resize the bytes buffer to the size specified in the header.
59 _bytes
.resize(_header
.size
);
61 // Read the entry contents.
62 is
.read(reinterpret_cast<char*>(&_bytes
[0]),
68 bool zen::firmware_entry::write(std::ostream
& os
) const
70 // Form a header using the current size of the bytes buffer.
71 firmware_header_t header
= {
73 static_cast<dword
>(_bytes
.size())
76 // If the firmware is big-endian, swap the header values back into big-endian.
79 if (header
.tag
!= 'NULL')
81 header
.size
= shared::swap(header
.size
);
83 header
.tag
= shared::swap(header
.tag
);
87 os
.write((const char*)&header
, sizeof(firmware_header_t
));
91 // Write the entry contents.
92 os
.write(reinterpret_cast<const char*>(&_bytes
[0]),
93 static_cast<std::streamsize
>(_bytes
.size()));
99 bool zen::firmware_entry::is_big_endian() const
104 const zen::firmware_header_t
& zen::firmware_entry::get_header() const
108 zen::firmware_header_t
& zen::firmware_entry::get_header()
113 const shared::bytes
& zen::firmware_entry::get_bytes() const
117 shared::bytes
& zen::firmware_entry::get_bytes()
123 std::string
zen::firmware_entry::get_name() const
126 *(dword
*)name
= shared::swap(_header
.tag
);
129 // Determine if all characters in the tag are printable.
130 bool isprintable
= true;
131 for (int i
= 0; i
< 4; i
++)
133 if (!isprint((byte
)name
[i
]))
140 // If they are, simply return the tag as a string.
143 return std::string(name
);
146 // Otherwise, encode the tag into a hexadecimal string.
148 sprintf(buffer
, "0x%08x", _header
.tag
);
149 return std::string(buffer
);
152 std::string
zen::firmware_entry::get_content_name() const
154 std::string name
= get_name();
158 int nameoff
= is_big_endian() ? 1 : 0;
159 for (int i
= 0; i
< 16; i
++)
161 char c
= get_bytes()[i
* 2 + nameoff
];
167 else if (name
== "EXT0")
170 int nameoff
= is_big_endian() ? 1 : 0;
171 for (int i
= 0; i
< 12; i
++)
173 char c
= get_bytes()[i
* 2 + nameoff
];
182 size_t zen::firmware_entry::get_content_offset() const
184 std::string name
= get_name();
189 else if (name
== "EXT0")
196 size_t zen::firmware_entry::calc_size() const
198 return _bytes
.size() + sizeof(firmware_header_t
);
202 void zen::firmware_entry::assign(const firmware_entry
& copy
)
204 _big_endian
= copy
._big_endian
;
205 _header
.tag
= copy
._header
.tag
;
206 _header
.size
= copy
._header
.size
;
207 _bytes
.assign(copy
._bytes
.begin(), copy
._bytes
.end());
212 zen::firmware_archive::firmware_archive(bool big_endian
)
213 : _big_endian(big_endian
)
217 zen::firmware_archive::firmware_archive(const firmware_archive
& copy
)
222 zen::firmware_archive
& zen::firmware_archive::operator=(const firmware_archive
& right
)
229 bool zen::firmware_archive::read(std::istream
& is
)
231 // Read the root entry's header.
232 firmware_header_t root
;
233 is
.read((char*)&root
, sizeof(firmware_header_t
));
237 if ((root
.tag
!= 'CIFF') && (root
.tag
!= 'FFIC'))
239 throw std::runtime_error("Invalid firmware archive format!");
242 _big_endian
= root
.tag
== 'FFIC' ? true : false;
245 root
.tag
= shared::swap(root
.tag
);
246 root
.size
= shared::swap(root
.size
);
249 // Save the current stream position.
250 std::istream::pos_type endpos
= is
.tellg();
251 std::istream::pos_type curpos
= endpos
;
252 endpos
+= std::istream::pos_type(root
.size
);
254 // Read untill the end of the root entry contents.
255 while (curpos
< endpos
)
257 firmware_entry
entry(_big_endian
);
261 _children
.push_back(entry
);
266 is
.seekg(0, std::ios::end
);
270 // Read untill the end of the file.
271 while (((size_t)curpos
+ sizeof(firmware_header_t
)) < endpos
)
273 firmware_entry
entry(_big_endian
);
277 _neighbours
.push_back(entry
);
284 bool zen::firmware_archive::write(std::ostream
& os
) const
286 // Read the root entry's header.
287 firmware_header_t root
= {'CIFF', 0};
289 // Calculate the total size of all the children entries.
290 for (firmware_entries::const_iterator i
= _children
.begin();
291 i
!= _children
.end(); ++i
)
293 root
.size
+= i
->calc_size();
296 // If the firmware is big-endian, swap the header values back into big-endian.
299 root
.tag
= shared::swap(root
.tag
);
300 root
.size
= shared::swap(root
.size
);
304 os
.write((const char*)&root
, sizeof(firmware_header_t
));
308 // Write all the child entries.
309 for (firmware_entries::const_iterator i
= _children
.begin();
310 i
!= _children
.end(); ++i
)
316 // Write all the neighbour entries.
317 for (firmware_entries::const_iterator i
= _neighbours
.begin();
318 i
!= _neighbours
.end(); ++i
)
328 bool zen::firmware_archive::is_big_endian() const
333 const zen::firmware_entries
& zen::firmware_archive::get_children() const
337 zen::firmware_entries
& zen::firmware_archive::get_children()
342 const zen::firmware_entries
& zen::firmware_archive::get_neighbours() const
346 zen::firmware_entries
& zen::firmware_archive::get_neighbours()
351 bool zen::firmware_archive::is_signed() const
353 for (firmware_entries::const_iterator i
= _neighbours
.begin();
354 i
!= _neighbours
.end(); i
++)
356 if (i
->get_name() == "NULL")
362 size_t zen::firmware_archive::calc_size() const
364 size_t size
= sizeof(firmware_header_t
);
366 for (firmware_entries::const_iterator i
= _children
.begin();
367 i
!= _children
.end(); i
++)
369 size
+= i
->calc_size();
372 for (firmware_entries::const_iterator i
= _neighbours
.begin();
373 i
!= _neighbours
.end(); i
++)
375 size
+= i
->calc_size();
382 void zen::firmware_archive::assign(const firmware_archive
& copy
)
384 _big_endian
= copy
._big_endian
;
385 _children
.assign(copy
._children
.begin(), copy
._children
.end());
386 _neighbours
.assign(copy
._neighbours
.begin(), copy
._neighbours
.end());