anlogicBitParser: use _raw_data and work with this one instead of file descriptor

This commit is contained in:
Gwenhael Goavec-Merou 2021-02-06 11:29:32 +01:00
parent 5c49b1465a
commit f6c036f1c0
1 changed files with 34 additions and 37 deletions

View File

@ -23,6 +23,7 @@
#include <functional> #include <functional>
#include <cctype> #include <cctype>
#include <iostream> #include <iostream>
#include <sstream>
#include <locale> #include <locale>
#include <vector> #include <vector>
@ -54,17 +55,14 @@ void AnlogicBitParser::displayHeader()
*/ */
int AnlogicBitParser::parseHeader() int AnlogicBitParser::parseHeader()
{ {
int ret = EXIT_SUCCESS; int ret = 0;
printInfo("parseHeader"); printInfo("parseHeader");
while(1) { string buffer;
string buffer; istringstream lineStream(_raw_data);
std::getline(_fd, buffer, '\n');
if (_fd.eof()) { while (std::getline(lineStream, buffer, '\n')) {
printError("End of file before start of data"); ret += buffer.size() + 1;
return EXIT_FAILURE;
}
if (buffer.empty()) { if (buffer.empty()) {
printInfo("header end"); printInfo("header end");
@ -76,7 +74,7 @@ int AnlogicBitParser::parseHeader()
return EXIT_FAILURE; return EXIT_FAILURE;
} }
string content = buffer.substr(2); string content = buffer.substr(2); // drop '# '
size_t pos = content.find(':'); size_t pos = content.find(':');
if (pos == string::npos) { if (pos == string::npos) {
_hdr["tool"] = content; _hdr["tool"] = content;
@ -87,52 +85,51 @@ int AnlogicBitParser::parseHeader()
} }
} }
if (_raw_data[ret] != 0x00) {
printError("Header must end with 0x00 (binary) bit");
ret = -1;
}
return ret; return ret;
} }
int AnlogicBitParser::parse() int AnlogicBitParser::parse()
{ {
if (parseHeader() == EXIT_FAILURE) int end_header = 0;
return EXIT_FAILURE; /* fill raw buffer with file content */
_fd.read((char *)&_raw_data[0], sizeof(char) * _file_size);
uint8_t dummy = _fd.get(); if (_fd.gcount() != _file_size) {
if (dummy != 0x00) { printError("Error: fails to read full file content");
printError("Header must end with 0x00 (binary) bit");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
size_t start_data = _fd.tellg(); /* parse header */
start_data--; if ((end_header = parseHeader()) == -1)
return EXIT_FAILURE;
_fd.seekg(0, _fd.end); unsigned int pos = end_header;
size_t size_data = (size_t)_fd.tellg() - start_data;
_fd.seekg(start_data, _fd.beg);
vector<uint8_t> data;
data.resize(size_data);
_fd.read(reinterpret_cast<char *>(&(data[0])), size_data);
unsigned int pos = 0;
std::vector<std::vector<uint8_t>> blocks; std::vector<std::vector<uint8_t>> blocks;
do { do {
uint16_t len = (data[pos++] << 8); uint16_t len = (((uint16_t)_raw_data[pos]) << 8) |
len += data[pos++]; (0xff & (uint16_t)_raw_data[pos + 1]);
if ((len & 7) != 0) {
pos += 2;
if ((len & 7) != 0) {
printf("error\n"); printf("error\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
len >>= 3; len >>= 3;
if ((pos + len) > data.size()) { if ((pos + len) > _raw_data.size()) {
printf("error\n"); printf("error\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
std::vector<uint8_t> block = std::vector<uint8_t>(data.begin() + pos, std::vector<uint8_t> block = std::vector<uint8_t>(_raw_data.begin() + pos,
data.begin() + pos + len); _raw_data.begin() + pos + len);
blocks.push_back(block); blocks.push_back(block);
pos += len; pos += len;
} while (pos < data.size()); } while (pos < _raw_data.size());
_bit_data.clear(); _bit_data.clear();