| 1 |
/* Copyright (C) Teemu Suutari */ |
| 2 |
|
| 3 |
#include "LH3Decompressor.hpp" |
| 4 |
|
| 5 |
#include "../HuffmanDecoder.hpp" |
| 6 |
#include "../InputStream.hpp" |
| 7 |
#include "../OutputStream.hpp" |
| 8 |
|
| 9 |
|
| 10 |
namespace ancient::internal |
| 11 |
{ |
| 12 |
|
| 13 |
LH3Decompressor::LH3Decompressor(const Buffer &packedData) : |
| 14 |
_packedData(packedData) |
| 15 |
{ |
| 16 |
// nothing needed |
| 17 |
} |
| 18 |
|
| 19 |
LH3Decompressor::~LH3Decompressor() |
| 20 |
{ |
| 21 |
// nothing needed |
| 22 |
} |
| 23 |
|
| 24 |
size_t LH3Decompressor::getRawSize() const noexcept |
| 25 |
{ |
| 26 |
// N/A |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
size_t LH3Decompressor::getPackedSize() const noexcept |
| 31 |
{ |
| 32 |
// N/A |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
const std::string &LH3Decompressor::getName() const noexcept |
| 37 |
{ |
| 38 |
static std::string name="LHA: LH3"; |
| 39 |
return name; |
| 40 |
} |
| 41 |
|
| 42 |
void LH3Decompressor::decompressImpl(Buffer &rawData,bool verify) |
| 43 |
{ |
| 44 |
ForwardInputStream inputStream(_packedData,0,_packedData.size()); |
| 45 |
MSBBitReader<ForwardInputStream> bitReader(inputStream); |
| 46 |
auto readBits=[&](uint32_t count)->uint32_t |
| 47 |
{ |
| 48 |
return bitReader.readBits8(count); |
| 49 |
}; |
| 50 |
auto readBit=[&]()->uint32_t |
| 51 |
{ |
| 52 |
return bitReader.readBits8(1); |
| 53 |
}; |
| 54 |
|
| 55 |
ForwardOutputStream outputStream(rawData,0,rawData.size()); |
| 56 |
|
| 57 |
OptionalHuffmanDecoder<uint32_t> decoder; |
| 58 |
|
| 59 |
static const uint8_t distanceHighBits[128]={ |
| 60 |
2,4,4,5,5,5,6,6, 6,6,6,6,6,7,7,7, |
| 61 |
7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,8, |
| 62 |
8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, |
| 63 |
8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, |
| 64 |
|
| 65 |
8,8,8,8,8,8,8,8, 8,8,8,8,8,8,9,9, |
| 66 |
9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9, |
| 67 |
9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9, |
| 68 |
9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9 |
| 69 |
}; |
| 70 |
|
| 71 |
OptionalHuffmanDecoder<uint8_t> distanceDecoder; |
| 72 |
|
| 73 |
uint32_t blockRemaining=0; |
| 74 |
while (!outputStream.eof()) |
| 75 |
{ |
| 76 |
if (!blockRemaining) |
| 77 |
{ |
| 78 |
blockRemaining=readBits(16); |
| 79 |
if (!blockRemaining) blockRemaining=0x10000; |
| 80 |
|
| 81 |
// not strictly needed as a lambda, but cleaner this way |
| 82 |
auto createDecoderTable=[&]() |
| 83 |
{ |
| 84 |
decoder.reset(); |
| 85 |
|
| 86 |
uint8_t symbolBits[286]; |
| 87 |
uint32_t oneCount=0; |
| 88 |
for (uint32_t i=0;i<286;i++) |
| 89 |
{ |
| 90 |
if (readBit()) symbolBits[i]=readBits(4)+1; |
| 91 |
else symbolBits[i]=0; |
| 92 |
if (symbolBits[i]==1) oneCount++; |
| 93 |
if (i==2 && oneCount==3) |
| 94 |
{ |
| 95 |
decoder.setEmpty(readBits(9)); |
| 96 |
return; |
| 97 |
} |
| 98 |
} |
| 99 |
decoder.createOrderlyHuffmanTable(symbolBits,286); |
| 100 |
}; |
| 101 |
|
| 102 |
// ditto |
| 103 |
auto createDistanceDecoderTable=[&]() |
| 104 |
{ |
| 105 |
distanceDecoder.reset(); |
| 106 |
|
| 107 |
if (readBit()) |
| 108 |
{ |
| 109 |
uint8_t symbolBits[128]; |
| 110 |
uint32_t oneCount=0; |
| 111 |
for (uint32_t i=0;i<128;i++) |
| 112 |
{ |
| 113 |
symbolBits[i]=readBits(4); |
| 114 |
if (symbolBits[i]==1) oneCount++; |
| 115 |
if (i==2 && oneCount==3) |
| 116 |
{ |
| 117 |
// 7 bits would be fine, but whatever |
| 118 |
distanceDecoder.setEmpty(readBits(9)); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
distanceDecoder.createOrderlyHuffmanTable(symbolBits,128); |
| 123 |
} else distanceDecoder.createOrderlyHuffmanTable(distanceHighBits,128); |
| 124 |
}; |
| 125 |
|
| 126 |
createDecoderTable(); |
| 127 |
createDistanceDecoderTable(); |
| 128 |
} |
| 129 |
blockRemaining--; |
| 130 |
|
| 131 |
uint32_t code=decoder.decode(readBit); |
| 132 |
|
| 133 |
if (code<256) |
| 134 |
{ |
| 135 |
outputStream.writeByte(code); |
| 136 |
} else { |
| 137 |
if (code==285) code+=readBits(8); |
| 138 |
uint32_t distance=distanceDecoder.decode(readBit); |
| 139 |
distance=(distance<<6)|readBits(6); |
| 140 |
distance++; |
| 141 |
|
| 142 |
uint32_t count=code-253; |
| 143 |
|
| 144 |
outputStream.copy(distance,count,0x20); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
} |