bitparser.cpp: parserHeader. Added bound check and simplied type A

parsing
This commit is contained in:
Gwenhael Goavec-Merou 2026-04-01 10:09:43 +02:00
parent 3429d34a30
commit 63c97410b6
1 changed files with 61 additions and 20 deletions

View File

@ -36,49 +36,86 @@ int BitParser::parseHeader()
{ {
int pos_data = 0; int pos_data = 0;
int ret = 1; int ret = 1;
short length; uint16_t length;
std::string tmp; std::string tmp;
int pos, prev_pos;
/* Field 1 : misc header */ /* Field 1 : misc header */
if (_raw_data.size() < 2) {
printError("BitParser: Bound check failure. Can't read Field 1 length");
return -1;
}
length = *(uint16_t *)&_raw_data[0]; length = *(uint16_t *)&_raw_data[0];
length = ntohs(length); length = ntohs(length);
pos_data += length + 2; pos_data += length + 2;
length = *(uint16_t *)&_raw_data[pos_data]; if (pos_data + 2 >= static_cast<int>(_raw_data.size())) {
length = ntohs(length); printError("BitParser: Bound check failure. Can't read Field 2 length");
return -1;
}
length = ((static_cast<uint16_t>(_raw_data[pos_data]) & 0xff) << 8) |
((static_cast<uint16_t>(_raw_data[pos_data + 1]) & 0xff) << 0);
pos_data += 2; pos_data += 2;
while (1) { while (1) {
/* type */ /* type */
uint8_t type; if (pos_data >= static_cast<int>(_raw_data.size())) {
type = (uint8_t)_raw_data[pos_data++]; printError("BitParser: Bound check failure. Field type");
return -1;
}
const uint8_t type = (uint8_t)_raw_data[pos_data++];
if (type != 'e') { if (type != 'e') {
length = *(uint16_t *)&_raw_data[pos_data]; if (pos_data + 2 >= static_cast<int>(_raw_data.size())) {
length = ntohs(length); printError("BitParser: Bound check failure, Field length");
return -1;
}
length = ((static_cast<uint16_t>(_raw_data[pos_data]) & 0xff) << 8) |
((static_cast<uint16_t>(_raw_data[pos_data + 1]) & 0xff) << 0);
pos_data += 2; pos_data += 2;
} else { } else {
length = 4; length = 4;
} }
if (static_cast<int>(_raw_data.size()) < pos_data + length) {
printError("BitParser: Bound check failure. Field Data");
return -1;
}
tmp = _raw_data.substr(pos_data, length); tmp = _raw_data.substr(pos_data, length);
pos_data += length; pos_data += length;
switch (type) { switch (type) {
case 'a': /* design name:userid:synthesize tool version */ case 'a': { /* design name:userid:synthesize tool version */
prev_pos = 0; std::stringstream ss(tmp);
pos = tmp.find(";"); std::string token;
_hdr["design_name"] = tmp.substr(prev_pos, pos); bool first = true;
prev_pos = pos+1;
pos = tmp.find(";", prev_pos); while (std::getline(ss, token, ';')) {
prev_pos = tmp.find("=", prev_pos) + 1; if (first) {
_hdr["userID"] = tmp.substr(prev_pos, pos-prev_pos); _hdr["design_name"] = token;
prev_pos = pos+1; first = false;
continue;
}
prev_pos = tmp.find("=", prev_pos) + 1; auto pos = token.find('=');
_hdr["toolVersion"] = tmp.substr(prev_pos, length-prev_pos); if (pos != std::string::npos) {
break; auto key = token.substr(0, pos);
if (length < pos + 1) {
printError("Failed to find for key");
return -1;
}
auto value = token.substr(pos + 1);
if (key == "UserID") {
_hdr["userId"] = value;
} else if (key == "Version") {
_hdr["version"] = value;
} else {
printError("Unknown key " + key);
return -1;
}
}
}
} break;
case 'b': /* FPGA model */ case 'b': /* FPGA model */
_hdr["part_name"] = tmp.substr(0, length); _hdr["part_name"] = tmp.substr(0, length);
break; break;
@ -107,6 +144,10 @@ int BitParser::parse()
{ {
/* process all field */ /* process all field */
int pos = parseHeader(); int pos = parseHeader();
if (pos == -1) {
printError("BitParser: parseHeader failed");
return 1;
}
/* _bit_length is length of data to send */ /* _bit_length is length of data to send */
int rest_of_file_length = _file_size - pos; int rest_of_file_length = _file_size - pos;