1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
/* Copyright (C) Teemu Suutari */
#include "TPWMDecompressor.hpp"
#include "InputStream.hpp"
#include "OutputStream.hpp"
#include "common/Common.hpp"
namespace ancient::internal
{
bool TPWMDecompressor::detectHeader(uint32_t hdr) noexcept
{
return hdr==FourCC("TPWM");
}
std::shared_ptr<Decompressor> TPWMDecompressor::create(const Buffer &packedData,bool exactSizeKnown,bool verify)
{
return std::make_shared<TPWMDecompressor>(packedData,verify);
}
TPWMDecompressor::TPWMDecompressor(const Buffer &packedData,bool verify) :
_packedData{packedData}
{
uint32_t hdr{packedData.readBE32(0)};
if (!detectHeader(hdr) || packedData.size()<12)
throw InvalidFormatError();
_rawSize=packedData.readBE32(4);
if (!_rawSize || _rawSize>getMaxRawSize())
throw InvalidFormatError();
}
const std::string &TPWMDecompressor::getName() const noexcept
{
static std::string name="TPWM: Turbo Packer";
return name;
}
size_t TPWMDecompressor::getPackedSize() const noexcept
{
// No packed size in the stream :(
// After decompression, we can tell how many bytes were actually used
return _decompressedPackedSize;
}
size_t TPWMDecompressor::getRawSize() const noexcept
{
return _rawSize;
}
void TPWMDecompressor::decompressImpl(Buffer &rawData,bool verify)
{
if (rawData.size()<_rawSize)
throw DecompressionError();
ForwardInputStream inputStream{_packedData,8U,_packedData.size()};
MSBBitReader<ForwardInputStream> bitReader{inputStream};
auto readBit=[&]()->uint32_t
{
return bitReader.readBits8(1U);
};
auto readByte=[&]()->uint8_t
{
return inputStream.readByte();
};
ForwardOutputStream outputStream{rawData,0,_rawSize};
while (!outputStream.eof())
{
if (readBit())
{
uint8_t byte1{readByte()};
uint8_t byte2{readByte()};
uint32_t distance{(uint32_t(byte1&0xf0U)<<4U)|byte2};
uint32_t count{uint32_t(byte1&0xfU)+3U};
count=std::min(count,uint32_t(_rawSize-outputStream.getOffset()));
outputStream.copy(distance,count);
} else {
outputStream.writeByte(readByte());
}
}
_decompressedPackedSize=inputStream.getOffset();
}
}
|