| 1 |
/* Copyright (C) Teemu Suutari */ |
| 2 |
|
| 3 |
#include "LZ5Decompressor.hpp" |
| 4 |
|
| 5 |
#include "common/StaticBuffer.hpp" |
| 6 |
|
| 7 |
#include "../InputStream.hpp" |
| 8 |
#include "../OutputStream.hpp" |
| 9 |
|
| 10 |
|
| 11 |
namespace ancient::internal |
| 12 |
{ |
| 13 |
|
| 14 |
LZ5Decompressor::LZ5Decompressor(const Buffer &packedData) : |
| 15 |
_packedData(packedData) |
| 16 |
{ |
| 17 |
// nothing needed |
| 18 |
} |
| 19 |
|
| 20 |
LZ5Decompressor::~LZ5Decompressor() |
| 21 |
{ |
| 22 |
// nothing needed |
| 23 |
} |
| 24 |
|
| 25 |
size_t LZ5Decompressor::getRawSize() const noexcept |
| 26 |
{ |
| 27 |
// N/A |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
size_t LZ5Decompressor::getPackedSize() const noexcept |
| 32 |
{ |
| 33 |
// N/A |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
const std::string &LZ5Decompressor::getName() const noexcept |
| 38 |
{ |
| 39 |
static std::string name="LHA: LZ5"; |
| 40 |
return name; |
| 41 |
} |
| 42 |
|
| 43 |
void LZ5Decompressor::decompressImpl(Buffer &rawData,bool verify) |
| 44 |
{ |
| 45 |
ForwardInputStream inputStream(_packedData,0,_packedData.size()); |
| 46 |
LSBBitReader<ForwardInputStream> bitReader(inputStream); |
| 47 |
auto readBit=[&]()->uint32_t |
| 48 |
{ |
| 49 |
return bitReader.readBits8(1); |
| 50 |
}; |
| 51 |
auto readByte=[&]()->uint32_t |
| 52 |
{ |
| 53 |
return inputStream.readByte(); |
| 54 |
}; |
| 55 |
|
| 56 |
ForwardOutputStream outputStream(rawData,0,rawData.size()); |
| 57 |
StaticBuffer<4096> prevBuffer; |
| 58 |
{ |
| 59 |
uint8_t *bufPtr=prevBuffer.data(); |
| 60 |
|
| 61 |
for (uint32_t i=0;i<18;i++) *(bufPtr++)=0; |
| 62 |
for (uint32_t i=0;i<256;i++) |
| 63 |
for (uint32_t j=0;j<13;j++) *(bufPtr++)=i; |
| 64 |
for (uint32_t i=0;i<256;i++) *(bufPtr++)=i; |
| 65 |
for (uint32_t i=0;i<256;i++) *(bufPtr++)=255-i; |
| 66 |
for (uint32_t i=0;i<128;i++) *(bufPtr++)=0; |
| 67 |
for (uint32_t i=0;i<110;i++) *(bufPtr++)=' '; |
| 68 |
} |
| 69 |
|
| 70 |
while (!outputStream.eof()) |
| 71 |
{ |
| 72 |
if (readBit()) |
| 73 |
{ |
| 74 |
outputStream.writeByte(readByte()); |
| 75 |
} else { |
| 76 |
uint32_t byte1=readByte(); |
| 77 |
uint32_t byte2=readByte(); |
| 78 |
uint32_t distance=((outputStream.getOffset()-byte1-((byte2&0xf0U)<<4)-19)&0xfffU)+1; |
| 79 |
uint32_t count=(byte2&0xfU)+3; |
| 80 |
|
| 81 |
outputStream.copy(distance,count,prevBuffer); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
} |