Improve coding style in AVC config parser

* Avoid magic numbers
* Avoid needlessly verbose syntax
* Add comments
This commit is contained in:
Martchus 2020-12-01 01:12:46 +01:00
parent 40031b8ddf
commit 31e8741db0
3 changed files with 14 additions and 22 deletions

View File

@ -41,18 +41,17 @@ void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagno
std::uint8_t entryCount = reader.readByte() & 0x0f;
spsInfos.reserve(entryCount);
for (; entryCount; --entryCount) {
if (maxSize < 2) {
if (maxSize < SpsInfo::minSize) {
throw TruncatedDataException();
}
spsInfos.emplace_back();
try {
spsInfos.back().parse(
spsInfos.emplace_back().parse(
reader, maxSize > numeric_limits<std::uint32_t>::max() ? numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(maxSize));
} catch (const TruncatedDataException &) {
if (spsInfos.back().size > maxSize - 2) {
throw;
if (spsInfos.back().size > (maxSize - SpsInfo::minSize)) {
throw; // sps info looks bigger than bytes to read
}
spsInfos.pop_back();
spsInfos.pop_back(); // sps info exceeds denoted size
} catch (const Failure &) {
spsInfos.pop_back();
// TODO: log parsing error
@ -64,18 +63,17 @@ void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagno
entryCount = reader.readByte();
ppsInfos.reserve(entryCount);
for (; entryCount; --entryCount) {
if (maxSize < 2) {
if (maxSize < PpsInfo::minSize) {
throw TruncatedDataException();
}
ppsInfos.emplace_back();
try {
ppsInfos.back().parse(
ppsInfos.emplace_back().parse(
reader, maxSize > numeric_limits<std::uint32_t>::max() ? numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(maxSize));
} catch (const TruncatedDataException &) {
if (ppsInfos.back().size > maxSize - 2) {
throw;
if (ppsInfos.back().size > (maxSize - PpsInfo::minSize)) {
throw; // pps info looks bigger than bytes to read
}
ppsInfos.pop_back();
ppsInfos.pop_back(); // pps info exceeds denoted size
} catch (const Failure &) {
ppsInfos.pop_back();
// TODO: log parsing error

View File

@ -24,11 +24,7 @@ namespace TagParser {
void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
{
// read (and check) size
if (maxSize < 2) {
throw TruncatedDataException();
}
maxSize -= 2;
if ((size = reader.readUInt16BE()) > maxSize) {
if ((maxSize < minSize) || (size = reader.readUInt16BE()) > (maxSize - minSize)) {
throw TruncatedDataException();
}
@ -225,11 +221,7 @@ void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
void PpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
{
// read (and check) size
if (maxSize < 2) {
throw TruncatedDataException();
}
maxSize -= 2;
if ((size = reader.readUInt16BE()) > maxSize) {
if ((maxSize < minSize) || (size = reader.readUInt16BE()) > (maxSize - minSize)) {
throw TruncatedDataException();
}

View File

@ -93,6 +93,7 @@ struct TAG_PARSER_EXPORT SpsInfo {
HrdParameters vclHrdParameters;
std::uint8_t pictureStructPresent;
std::uint16_t size;
static constexpr std::uint16_t minSize = 2;
void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize);
};
@ -124,6 +125,7 @@ struct TAG_PARSER_EXPORT PpsInfo {
ugolomb spsId;
std::uint8_t picOrderPresent;
std::uint16_t size;
static constexpr std::uint16_t minSize = 2;
void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize);
};