Improve coding style in AVC config parser
* Avoid magic numbers * Avoid needlessly verbose syntax * Add comments
This commit is contained in:
parent
40031b8ddf
commit
31e8741db0
|
@ -41,18 +41,17 @@ void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagno
|
||||||
std::uint8_t entryCount = reader.readByte() & 0x0f;
|
std::uint8_t entryCount = reader.readByte() & 0x0f;
|
||||||
spsInfos.reserve(entryCount);
|
spsInfos.reserve(entryCount);
|
||||||
for (; entryCount; --entryCount) {
|
for (; entryCount; --entryCount) {
|
||||||
if (maxSize < 2) {
|
if (maxSize < SpsInfo::minSize) {
|
||||||
throw TruncatedDataException();
|
throw TruncatedDataException();
|
||||||
}
|
}
|
||||||
spsInfos.emplace_back();
|
|
||||||
try {
|
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));
|
reader, maxSize > numeric_limits<std::uint32_t>::max() ? numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(maxSize));
|
||||||
} catch (const TruncatedDataException &) {
|
} catch (const TruncatedDataException &) {
|
||||||
if (spsInfos.back().size > maxSize - 2) {
|
if (spsInfos.back().size > (maxSize - SpsInfo::minSize)) {
|
||||||
throw;
|
throw; // sps info looks bigger than bytes to read
|
||||||
}
|
}
|
||||||
spsInfos.pop_back();
|
spsInfos.pop_back(); // sps info exceeds denoted size
|
||||||
} catch (const Failure &) {
|
} catch (const Failure &) {
|
||||||
spsInfos.pop_back();
|
spsInfos.pop_back();
|
||||||
// TODO: log parsing error
|
// TODO: log parsing error
|
||||||
|
@ -64,18 +63,17 @@ void AvcConfiguration::parse(BinaryReader &reader, std::uint64_t maxSize, Diagno
|
||||||
entryCount = reader.readByte();
|
entryCount = reader.readByte();
|
||||||
ppsInfos.reserve(entryCount);
|
ppsInfos.reserve(entryCount);
|
||||||
for (; entryCount; --entryCount) {
|
for (; entryCount; --entryCount) {
|
||||||
if (maxSize < 2) {
|
if (maxSize < PpsInfo::minSize) {
|
||||||
throw TruncatedDataException();
|
throw TruncatedDataException();
|
||||||
}
|
}
|
||||||
ppsInfos.emplace_back();
|
|
||||||
try {
|
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));
|
reader, maxSize > numeric_limits<std::uint32_t>::max() ? numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(maxSize));
|
||||||
} catch (const TruncatedDataException &) {
|
} catch (const TruncatedDataException &) {
|
||||||
if (ppsInfos.back().size > maxSize - 2) {
|
if (ppsInfos.back().size > (maxSize - PpsInfo::minSize)) {
|
||||||
throw;
|
throw; // pps info looks bigger than bytes to read
|
||||||
}
|
}
|
||||||
ppsInfos.pop_back();
|
ppsInfos.pop_back(); // pps info exceeds denoted size
|
||||||
} catch (const Failure &) {
|
} catch (const Failure &) {
|
||||||
ppsInfos.pop_back();
|
ppsInfos.pop_back();
|
||||||
// TODO: log parsing error
|
// TODO: log parsing error
|
||||||
|
|
|
@ -24,11 +24,7 @@ namespace TagParser {
|
||||||
void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
|
void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
|
||||||
{
|
{
|
||||||
// read (and check) size
|
// read (and check) size
|
||||||
if (maxSize < 2) {
|
if ((maxSize < minSize) || (size = reader.readUInt16BE()) > (maxSize - minSize)) {
|
||||||
throw TruncatedDataException();
|
|
||||||
}
|
|
||||||
maxSize -= 2;
|
|
||||||
if ((size = reader.readUInt16BE()) > maxSize) {
|
|
||||||
throw TruncatedDataException();
|
throw TruncatedDataException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,11 +221,7 @@ void SpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
|
||||||
void PpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
|
void PpsInfo::parse(BinaryReader &reader, std::uint32_t maxSize)
|
||||||
{
|
{
|
||||||
// read (and check) size
|
// read (and check) size
|
||||||
if (maxSize < 2) {
|
if ((maxSize < minSize) || (size = reader.readUInt16BE()) > (maxSize - minSize)) {
|
||||||
throw TruncatedDataException();
|
|
||||||
}
|
|
||||||
maxSize -= 2;
|
|
||||||
if ((size = reader.readUInt16BE()) > maxSize) {
|
|
||||||
throw TruncatedDataException();
|
throw TruncatedDataException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,7 @@ struct TAG_PARSER_EXPORT SpsInfo {
|
||||||
HrdParameters vclHrdParameters;
|
HrdParameters vclHrdParameters;
|
||||||
std::uint8_t pictureStructPresent;
|
std::uint8_t pictureStructPresent;
|
||||||
std::uint16_t size;
|
std::uint16_t size;
|
||||||
|
static constexpr std::uint16_t minSize = 2;
|
||||||
|
|
||||||
void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize);
|
void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize);
|
||||||
};
|
};
|
||||||
|
@ -124,6 +125,7 @@ struct TAG_PARSER_EXPORT PpsInfo {
|
||||||
ugolomb spsId;
|
ugolomb spsId;
|
||||||
std::uint8_t picOrderPresent;
|
std::uint8_t picOrderPresent;
|
||||||
std::uint16_t size;
|
std::uint16_t size;
|
||||||
|
static constexpr std::uint16_t minSize = 2;
|
||||||
|
|
||||||
void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize);
|
void parse(CppUtilities::BinaryReader &reader, std::uint32_t maxSize);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue