Skip to content

Commit

Permalink
Detection fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
temisu committed Nov 22, 2024
1 parent f25eb99 commit 61ec98d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions src/Decompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static std::vector<std::pair<bool(*)(uint32_t,uint32_t),std::shared_ptr<Decompre
{TPWMDecompressor::detectHeader,TPWMDecompressor::create},
{VicXDecompressor::detectHeader,VicXDecompressor::create},
{XPKMain::detectHeader,XPKMain::create},
// Putting StoneCracker last since detection can be accidentally be detected instead of correct format
// Formats with missing id / uncertain detection
// old stonecracker is far from certain
{StoneCrackerDecompressor::detectHeader,StoneCrackerDecompressor::create}
};

Expand All @@ -63,7 +64,12 @@ std::shared_ptr<Decompressor> Decompressor::create(const Buffer &packedData,bool
uint32_t footer{(exactSizeKnown&&packedData.size()>=4)?packedData.readBE32(packedData.size()-4):0};
for (auto &it : decompressors)
{
if (it.first(hdr,footer)) return it.second(packedData,exactSizeKnown,verify);
try
{
if (it.first(hdr,footer)) return it.second(packedData,exactSizeKnown,verify);
} catch (const Error&) {
// try next on the list
}
}
throw InvalidFormatError();
} catch (const Buffer::Error&) {
Expand All @@ -74,16 +80,8 @@ std::shared_ptr<Decompressor> Decompressor::create(const Buffer &packedData,bool
bool Decompressor::detect(const Buffer &packedData,bool exactSizeKnown) noexcept
{
if (packedData.size()<2) return false;
try
{
uint32_t hdr{(packedData.size()>=4)?packedData.readBE32(0):(uint32_t(packedData.readBE16(0))<<16)};
uint32_t footer{(exactSizeKnown&&packedData.size()>=4)?packedData.readBE32(packedData.size()-4):0};
for (auto &it : decompressors)
if (it.first(hdr,footer)) return true;
return false;
} catch (const Buffer::Error&) {
return false;
}
// need to create the decompressor in order to work with bad detectors.
return bool(create(packedData,exactSizeKnown,true));
}

void Decompressor::decompress(Buffer &rawData,bool verify)
Expand Down
2 changes: 1 addition & 1 deletion src/IceDecompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void IceDecompressor::decompressInternal(Buffer &rawData,bool useBytes)
}

// picture mode
if (_ver && readBits(1U))
if (_ver && bitReader.available() && readBits(1U))
{
uint32_t pictureSize=32000U;
if (_ver==2)
Expand Down

0 comments on commit 61ec98d

Please sign in to comment.