dfuFileParser: nitpick

This commit is contained in:
Gwenhael Goavec-Merou 2026-04-03 10:33:12 +02:00
parent 771b75aade
commit 816d6558fe
2 changed files with 24 additions and 21 deletions

View File

@ -10,7 +10,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@ -80,7 +79,6 @@ DFUFileParser::DFUFileParser(const std::string &filename, bool verbose):
/* p.40-47 */ /* p.40-47 */
int DFUFileParser::parseHeader() int DFUFileParser::parseHeader()
{ {
std::string ucDfuSignature;
/* check size: If file size <= 16 /* check size: If file size <= 16
* no space for suffix and/or bitstream * no space for suffix and/or bitstream
* */ * */
@ -88,11 +86,9 @@ int DFUFileParser::parseHeader()
return -1; return -1;
/* first check DFU signature : /* first check DFU signature :
* must be equal to 'DFU' * stored as 'UFD' (reversed 'DFU') in the file
*/ */
ucDfuSignature = _raw_data.substr(_file_size-8, 3); if (_raw_data.compare(_file_size - 8, 3, "UFD") != 0) {
reverse(ucDfuSignature.begin(), ucDfuSignature.end());
if (ucDfuSignature != "DFU") {
if (_verbose) if (_verbose)
printWarn("Not a DFU file"); printWarn("Not a DFU file");
return 0; return 0;
@ -102,15 +98,23 @@ int DFUFileParser::parseHeader()
(((uint32_t)_raw_data[_file_size - 3] & 0xff) << 8) | (((uint32_t)_raw_data[_file_size - 3] & 0xff) << 8) |
(((uint32_t)_raw_data[_file_size - 2] & 0xff) << 16) | (((uint32_t)_raw_data[_file_size - 2] & 0xff) << 16) |
(((uint32_t)_raw_data[_file_size - 1] & 0xff) << 24); (((uint32_t)_raw_data[_file_size - 1] & 0xff) << 24);
_bLength = _raw_data[_file_size - 5]; _bLength = static_cast<uint8_t>(_raw_data[_file_size - 5]);
_bcdDFU = (_raw_data[_file_size - 10] << 0) | if (_bLength > _file_size) {
(_raw_data[_file_size - 9] << 8); printError("Error: invalid DFU suffix length");
_idVendor = (_raw_data[_file_size - 12] << 0) | return -1;
(_raw_data[_file_size - 11] << 8); }
_idProduct = (((uint16_t)_raw_data[_file_size - 14] & 0xff) << 0) | _bcdDFU =
(((uint16_t)_raw_data[_file_size - 13] & 0xff) << 8); (static_cast<uint16_t>(_raw_data[_file_size - 10] & 0xff) << 0) |
_bcdDevice = (_raw_data[_file_size - 16] << 0) | (static_cast<uint16_t>(_raw_data[_file_size - 9] & 0xff) << 8);
(_raw_data[_file_size - 15] << 8); _idVendor =
(static_cast<uint16_t>(_raw_data[_file_size - 12] & 0xff) << 0) |
(static_cast<uint16_t>(_raw_data[_file_size - 11] & 0xff) << 8);
_idProduct =
(static_cast<uint16_t>(_raw_data[_file_size - 14] & 0xff) << 0) |
(static_cast<uint16_t>(_raw_data[_file_size - 13] & 0xff) << 8);
_bcdDevice =
(static_cast<uint16_t>(_raw_data[_file_size - 16] & 0xff) << 0) |
(static_cast<uint16_t>(_raw_data[_file_size - 15] & 0xff) << 8);
/* yes it's silly but it's simpliest way */ /* yes it's silly but it's simpliest way */
_hdr = {{"dwCRC", std::string(11, ' ')}, {"bLength", ""}, _hdr = {{"dwCRC", std::string(11, ' ')}, {"bLength", ""},
@ -125,7 +129,7 @@ int DFUFileParser::parseHeader()
_hdr["dwCRC"] = std::string(__buf, __buf_valid_bytes); _hdr["dwCRC"] = std::string(__buf, __buf_valid_bytes);
_hdr["dwCRC"].resize(11, ' '); _hdr["dwCRC"].resize(11, ' ');
_hdr["bLength"] = std::to_string(_bLength); _hdr["bLength"] = std::to_string(_bLength);
_hdr["ucDfuSignature"] = ucDfuSignature; _hdr["ucDfuSignature"] = "DFU";
__buf_valid_bytes = snprintf(__buf, 7, "0x%04x", _bcdDFU); __buf_valid_bytes = snprintf(__buf, 7, "0x%04x", _bcdDFU);
_hdr["bcdDFU"] = std::string(__buf, __buf_valid_bytes); _hdr["bcdDFU"] = std::string(__buf, __buf_valid_bytes);
_hdr["bcdDFU"].resize(7, ' '); _hdr["bcdDFU"].resize(7, ' ');
@ -148,14 +152,13 @@ int DFUFileParser::parse()
if (ret < 0) if (ret < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
_bit_data.resize(_file_size - _bLength); _bit_data.assign(_raw_data.begin(), _raw_data.begin() + (_file_size - _bLength));
std::move(_raw_data.begin(), _raw_data.end(), _bit_data.begin());
/* If file contains suffix check CRC */ /* If file contains suffix check CRC */
if (ret != 0) { if (ret != 0) {
/* check if CRC match content */ /* check if CRC match content */
uint32_t crc = 0xffffffff; uint32_t crc = 0xffffffff;
for (int i = 0; i < _file_size-4; i++) for (int i = 0; i < _file_size - 4; i++)
crc = crc32tbl[(crc ^ (uint8_t)_raw_data[i]) & 0xff] ^ (crc >> 8); crc = crc32tbl[(crc ^ (uint8_t)_raw_data[i]) & 0xff] ^ (crc >> 8);
if (crc != _dwCRC) { if (crc != _dwCRC) {

View File

@ -37,12 +37,12 @@ class DFUFileParser: public ConfigBitstreamParser {
* \brief return vendor id associated * \brief return vendor id associated
* \return _idVendor * \return _idVendor
*/ */
uint16_t vendorID() {return _idVendor;} uint16_t vendorID() const noexcept {return _idVendor;}
/*! /*!
* \brief return product id associated * \brief return product id associated
* \return _idProduct * \return _idProduct
*/ */
uint16_t productID() {return _idProduct;} uint16_t productID() const noexcept {return _idProduct;}
private: private:
/*! /*!