| 1 |
/* Copyright (C) Teemu Suutari */ |
| 2 |
|
| 3 |
#include "LHXDecompressor.hpp" |
| 4 |
|
| 5 |
#include "../HuffmanDecoder.hpp" |
| 6 |
#include "../InputStream.hpp" |
| 7 |
#include "../OutputStream.hpp" |
| 8 |
|
| 9 |
|
| 10 |
namespace ancient::internal |
| 11 |
{ |
| 12 |
|
| 13 |
LHXDecompressor::LHXDecompressor(const Buffer &packedData) : |
| 14 |
_packedData(packedData), |
| 15 |
_method(0) |
| 16 |
{ |
| 17 |
// nothing needed |
| 18 |
} |
| 19 |
|
| 20 |
LHXDecompressor::LHXDecompressor(const Buffer &packedData,uint32_t method) : |
| 21 |
_packedData(packedData), |
| 22 |
_method(method-3) |
| 23 |
{ |
| 24 |
if (method<4 || method>8) throw InvalidFormatError(); |
| 25 |
} |
| 26 |
|
| 27 |
LHXDecompressor::~LHXDecompressor() |
| 28 |
{ |
| 29 |
// nothing needed |
| 30 |
} |
| 31 |
|
| 32 |
size_t LHXDecompressor::getRawSize() const noexcept |
| 33 |
{ |
| 34 |
// N/A |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
size_t LHXDecompressor::getPackedSize() const noexcept |
| 39 |
{ |
| 40 |
// N/A |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
const std::string &LHXDecompressor::getName() const noexcept |
| 45 |
{ |
| 46 |
static std::string name="LHA: LH4, LH5, LH6, LH7, LH8, LHX"; |
| 47 |
return name; |
| 48 |
} |
| 49 |
|
| 50 |
void LHXDecompressor::decompressImpl(Buffer &rawData,bool verify) |
| 51 |
{ |
| 52 |
ForwardInputStream inputStream(_packedData,0,_packedData.size()); |
| 53 |
MSBBitReader<ForwardInputStream> bitReader(inputStream); |
| 54 |
auto readBits=[&](uint32_t count)->uint32_t |
| 55 |
{ |
| 56 |
return bitReader.readBits8(count); |
| 57 |
}; |
| 58 |
auto readBit=[&]()->uint32_t |
| 59 |
{ |
| 60 |
return bitReader.readBits8(1); |
| 61 |
}; |
| 62 |
|
| 63 |
ForwardOutputStream outputStream(rawData,0,rawData.size()); |
| 64 |
|
| 65 |
OptionalHuffmanDecoder<uint32_t> decoder; |
| 66 |
OptionalHuffmanDecoder<uint32_t> distanceDecoder; |
| 67 |
|
| 68 |
static const struct { |
| 69 |
uint32_t mask; |
| 70 |
uint32_t distanceTableSize; |
| 71 |
uint32_t distanceBits; |
| 72 |
} methodTable[6] = { |
| 73 |
{0x7ffffU,20,5}, // LHX |
| 74 |
{0xfffU,14,4}, // LH4 |
| 75 |
{0x1fffU,14,4}, // LH5 |
| 76 |
{0x7fffU,16,5}, // LH6 |
| 77 |
{0xffffU,17,5}, // LH7 |
| 78 |
{0xffffU,17,5} // LH8 |
| 79 |
// LH8 is the only Joe Jared method seen. |
| 80 |
// It exists in early versions of LH7-tool |
| 81 |
// In those versions LH8 is just synonym for LH7 |
| 82 |
// However, LH9+ is something I have not seen at |
| 83 |
// all (i.e. not in DLH7021Q/WLH7021Q.) |
| 84 |
// There would be one more version (0.21R) but |
| 85 |
// that seems lost. Most likely minor bump from Q->R |
| 86 |
// do not enable those formats either and the LH9+ |
| 87 |
// formats are nothing but hopes for "future work" |
| 88 |
// that never materialized (until I'm proven wrong ofc) |
| 89 |
}; |
| 90 |
|
| 91 |
uint32_t blockRemaining=0; |
| 92 |
while (!outputStream.eof()) |
| 93 |
{ |
| 94 |
if (!blockRemaining) |
| 95 |
{ |
| 96 |
blockRemaining=readBits(16); |
| 97 |
if (!blockRemaining) blockRemaining=0x10000; |
| 98 |
|
| 99 |
auto createTable=[&](OptionalHuffmanDecoder<uint32_t> &dest,uint32_t count,uint32_t bits,bool enableHole) |
| 100 |
{ |
| 101 |
uint8_t symbolBits[20]; |
| 102 |
uint32_t length=readBits(bits); |
| 103 |
if (!length) |
| 104 |
{ |
| 105 |
dest.setEmpty(readBits(bits)); |
| 106 |
} else if (length<=count) { |
| 107 |
for (uint32_t i=0;i<length;) |
| 108 |
{ |
| 109 |
uint32_t value=readBits(3); |
| 110 |
if (value==7) |
| 111 |
while (readBit()) value++; |
| 112 |
if (value>32) throw DecompressionError(); |
| 113 |
symbolBits[i++]=value; |
| 114 |
if (i==3 && enableHole) |
| 115 |
{ |
| 116 |
uint32_t zeros=readBits(2); |
| 117 |
if (i+zeros>length) throw DecompressionError(); |
| 118 |
for (uint32_t j=0;j<zeros;j++) symbolBits[i++]=0; |
| 119 |
} |
| 120 |
} |
| 121 |
dest.createOrderlyHuffmanTable(symbolBits,length); |
| 122 |
} else throw DecompressionError(); |
| 123 |
}; |
| 124 |
|
| 125 |
OptionalHuffmanDecoder<uint32_t> tmpDecoder; |
| 126 |
createTable(tmpDecoder,19,5,true); |
| 127 |
|
| 128 |
decoder.reset(); |
| 129 |
|
| 130 |
uint8_t symbolBits[511]; |
| 131 |
uint32_t length=readBits(9); |
| 132 |
if (!length) |
| 133 |
{ |
| 134 |
decoder.setEmpty(readBits(9)); |
| 135 |
} else { |
| 136 |
for (uint32_t i=0;i<length;) |
| 137 |
{ |
| 138 |
uint32_t value=tmpDecoder.decode(readBit); |
| 139 |
uint32_t rep; |
| 140 |
switch (value) |
| 141 |
{ |
| 142 |
case 0: |
| 143 |
value=0; |
| 144 |
rep=1; |
| 145 |
break; |
| 146 |
|
| 147 |
case 1: |
| 148 |
value=0; |
| 149 |
rep=readBits(4)+3; |
| 150 |
break; |
| 151 |
|
| 152 |
case 2: |
| 153 |
value=0; |
| 154 |
rep=readBits(9)+20; |
| 155 |
break; |
| 156 |
|
| 157 |
default: |
| 158 |
value-=2; |
| 159 |
rep=1; |
| 160 |
break; |
| 161 |
} |
| 162 |
if (i+rep>length) throw DecompressionError(); |
| 163 |
for (uint32_t j=0;j<rep;j++) symbolBits[i++]=value; |
| 164 |
} |
| 165 |
decoder.createOrderlyHuffmanTable(symbolBits,length); |
| 166 |
} |
| 167 |
|
| 168 |
distanceDecoder.reset(); |
| 169 |
createTable(distanceDecoder,methodTable[_method].distanceTableSize,methodTable[_method].distanceBits,false); |
| 170 |
} |
| 171 |
blockRemaining--; |
| 172 |
|
| 173 |
uint32_t code=decoder.decode(readBit); |
| 174 |
|
| 175 |
if (code<256) |
| 176 |
{ |
| 177 |
outputStream.writeByte(code); |
| 178 |
} else { |
| 179 |
uint32_t distanceBits=distanceDecoder.decode(readBit); |
| 180 |
uint32_t distance=distanceBits?((1<<(distanceBits-1))|readBits(distanceBits-1)):0; |
| 181 |
distance&=methodTable[_method].mask; // this is theoretical, but original LH has it |
| 182 |
distance++; |
| 183 |
|
| 184 |
uint32_t count=code-253; |
| 185 |
|
| 186 |
outputStream.copy(distance,count,0x20); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
} |