| 1 |
/* Copyright (C) Teemu Suutari */ |
| 2 |
|
| 3 |
#include "LZSDecompressor.hpp" |
| 4 |
|
| 5 |
#include "../InputStream.hpp" |
| 6 |
#include "../OutputStream.hpp" |
| 7 |
|
| 8 |
|
| 9 |
namespace ancient::internal |
| 10 |
{ |
| 11 |
|
| 12 |
LZSDecompressor::LZSDecompressor(const Buffer &packedData) : |
| 13 |
_packedData(packedData) |
| 14 |
{ |
| 15 |
// nothing needed |
| 16 |
} |
| 17 |
|
| 18 |
LZSDecompressor::~LZSDecompressor() |
| 19 |
{ |
| 20 |
// nothing needed |
| 21 |
} |
| 22 |
|
| 23 |
size_t LZSDecompressor::getRawSize() const noexcept |
| 24 |
{ |
| 25 |
// N/A |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
size_t LZSDecompressor::getPackedSize() const noexcept |
| 30 |
{ |
| 31 |
// N/A |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
const std::string &LZSDecompressor::getName() const noexcept |
| 36 |
{ |
| 37 |
static std::string name="LHA: LZS"; |
| 38 |
return name; |
| 39 |
} |
| 40 |
|
| 41 |
void LZSDecompressor::decompressImpl(Buffer &rawData,bool verify) |
| 42 |
{ |
| 43 |
ForwardInputStream inputStream(_packedData,0,_packedData.size()); |
| 44 |
MSBBitReader<ForwardInputStream> bitReader(inputStream); |
| 45 |
auto readBits=[&](uint32_t count)->uint32_t |
| 46 |
{ |
| 47 |
return bitReader.readBits8(count); |
| 48 |
}; |
| 49 |
auto readBit=[&]()->uint32_t |
| 50 |
{ |
| 51 |
return bitReader.readBits8(1); |
| 52 |
}; |
| 53 |
|
| 54 |
ForwardOutputStream outputStream(rawData,0,rawData.size()); |
| 55 |
|
| 56 |
while (!outputStream.eof()) |
| 57 |
{ |
| 58 |
if (readBit()) |
| 59 |
{ |
| 60 |
outputStream.writeByte(readBits(8)); |
| 61 |
} else { |
| 62 |
uint32_t distance=((outputStream.getOffset()-readBits(11)-18)&0x7ffU)+1; |
| 63 |
uint32_t count=readBits(4)+2; |
| 64 |
|
| 65 |
outputStream.copy(distance,count,0x20); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
} |