check block consistency, reset block.end for length that exceeds buffer size (e.g. final CS)

This commit is contained in:
2021-02-09 16:40:11 +01:00
parent c918746d29
commit 79c1b19e33
4 changed files with 61 additions and 10 deletions

View File

@@ -104,7 +104,20 @@ namespace imc
raw_file_ = raw_file;
buffer_ = buffer;
parse_parameters();
// make sure "end_" does not exceed buffer size due to invalid "length" parameter of block
if ( end_ > buffer_->size() )
{
std::cout<<"WARNING: invalid length parameter in "<<thekey_.name_<<"-block "
<<"(block-end:"<<end_<<",buffer-size:"<<buffer_->size()<<")"
<<" => resetting block-end to buffer-size\n";
end_ = buffer_->size();
}
try {
parse_parameters();
} catch (const std::exception& e) {
throw std::runtime_error("block: failed to parse parameters");
}
}
// identify/parse parameters in block
@@ -158,7 +171,7 @@ namespace imc
// summarize parameters in single string
std::string prsstr("{");
for ( auto par: parameters_ ) prsstr += par.get_info() + std::string(",");
prsstr.pop_back();
if ( prsstr.size() > 1 ) prsstr.pop_back();
prsstr += std::string("}");
// construct block info string

View File

@@ -36,17 +36,17 @@ namespace imc
// constructor
raw() {};
raw(std::string raw_file): raw_file_(raw_file) { this->parse(); };
raw(std::string raw_file): raw_file_(raw_file) { this->parse_blocks(); };
// provide new raw-file
void set_file(std::string raw_file)
{
raw_file_ = raw_file;
this->parse();
this->parse_blocks();
}
// list all blocks in buffer
void parse()
void parse_blocks()
{
// open file and put data in buffer
try {
@@ -64,7 +64,7 @@ namespace imc
// start parsing raw-blocks in buffer
for ( std::vector<unsigned char>::iterator it=buffer_.begin();
it!=buffer_.end(); ++it )
it!=buffer_.end(); ++it )
{
// check for "magic byte"
if ( *it == ch_bgn_ )
@@ -113,8 +113,10 @@ namespace imc
}
else
{
throw std::runtime_error( std::string("invalid block or corrupt buffer at byte: ")
+ std::to_string(it+3-buffer_.begin()) );
throw std::runtime_error(
std::string("invalid block or corrupt buffer at byte: ")
+ std::to_string(it+3-buffer_.begin())
);
}
}
else
@@ -125,6 +127,23 @@ namespace imc
}
}
this->check_consistency();
}
// check consistency of blocks
void check_consistency()
{
for ( unsigned long int b = 0; b < this->rawblocks_.size()-1; b++ )
{
if ( this->rawblocks_[b].get_end() >= this->rawblocks_[b+1].get_begin() )
{
throw std::runtime_error(
std::string("inconsistent subsequent blocks:\n")
+ std::to_string(b) + std::string("-th block:\n") + this->rawblocks_[b].get_info()
+ std::string("\n")
+ std::to_string(b+1) + std::string("-th block:\n") + this->rawblocks_[b+1].get_info() );
}
}
}
// get blocks