| 1 |
/* Copyright (C) Teemu Suutari */ |
| 2 |
|
| 3 |
#include "LH2Decompressor.hpp" |
| 4 |
|
| 5 |
#include "../HuffmanDecoder.hpp" |
| 6 |
#include "../DynamicHuffmanDecoder.hpp" |
| 7 |
#include "../InputStream.hpp" |
| 8 |
#include "../OutputStream.hpp" |
| 9 |
|
| 10 |
|
| 11 |
namespace ancient::internal |
| 12 |
{ |
| 13 |
|
| 14 |
LH2Decompressor::LH2Decompressor(const Buffer &packedData) : |
| 15 |
_packedData(packedData) |
| 16 |
{ |
| 17 |
// nothing needed |
| 18 |
} |
| 19 |
|
| 20 |
LH2Decompressor::~LH2Decompressor() |
| 21 |
{ |
| 22 |
// nothing needed |
| 23 |
} |
| 24 |
|
| 25 |
size_t LH2Decompressor::getRawSize() const noexcept |
| 26 |
{ |
| 27 |
// N/A |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
size_t LH2Decompressor::getPackedSize() const noexcept |
| 32 |
{ |
| 33 |
// N/A |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
const std::string &LH2Decompressor::getName() const noexcept |
| 38 |
{ |
| 39 |
static std::string name="LHA: LH2"; |
| 40 |
return name; |
| 41 |
} |
| 42 |
|
| 43 |
// This is probably fishiest of the fishy formats I've worked with |
| 44 |
// Basically it uses Dynamic Huffman decoder but instead of static |
| 45 |
// size, it is dynamically growing. However, that part is not well |
| 46 |
// defined. Thus |
| 47 |
// a. It is very hard to use LH2 using generic implementation |
| 48 |
// instead of the specific one used by LHA |
| 49 |
// b. There are bugs in encoder which need to be baked in the decoder |
| 50 |
// as well (Probably there is lots of unnecessary stuff in addCode, |
| 51 |
// but better safe than sorry) |
| 52 |
// c. LH 1.9x and UNLHA32 refuse to use LH2 beyond 8k files. Thus |
| 53 |
// we can only guess if the wraparound is correct, since nothing |
| 54 |
// should use it |
| 55 |
// jLHA in theory supports LH2 without limitations, but it produces |
| 56 |
// broken bitstream. |
| 57 |
// |
| 58 |
// So far this works with the files I've tried from "official" LHA |
| 59 |
void LH2Decompressor::decompressImpl(Buffer &rawData,bool verify) |
| 60 |
{ |
| 61 |
ForwardInputStream inputStream(_packedData,0,_packedData.size()); |
| 62 |
MSBBitReader<ForwardInputStream> bitReader(inputStream); |
| 63 |
auto readBits=[&](uint32_t count)->uint32_t |
| 64 |
{ |
| 65 |
return bitReader.readBits8(count); |
| 66 |
}; |
| 67 |
auto readBit=[&]()->uint32_t |
| 68 |
{ |
| 69 |
return bitReader.readBits8(1); |
| 70 |
}; |
| 71 |
|
| 72 |
ForwardOutputStream outputStream(rawData,0,rawData.size()); |
| 73 |
|
| 74 |
DynamicHuffmanDecoder<286> valueDecoder; |
| 75 |
DynamicHuffmanDecoder<128> distanceDecoder(0); |
| 76 |
uint32_t distancePosition=0; |
| 77 |
|
| 78 |
uint32_t distCount=0; |
| 79 |
while (!outputStream.eof()) |
| 80 |
{ |
| 81 |
uint32_t code=valueDecoder.decode(readBit); |
| 82 |
if (valueDecoder.getMaxFrequency()==0x8000U) valueDecoder.halve(); |
| 83 |
valueDecoder.update(code); |
| 84 |
|
| 85 |
if (code<256) |
| 86 |
{ |
| 87 |
outputStream.writeByte(code); |
| 88 |
} else { |
| 89 |
if (code==285) code+=readBits(8); |
| 90 |
uint32_t count=code-253; |
| 91 |
|
| 92 |
// Bug in LH2 where count does not match frequency. |
| 93 |
// Makes things more complicated |
| 94 |
auto updateDist=[&](uint32_t code) |
| 95 |
{ |
| 96 |
if (distCount==0x8000U) |
| 97 |
{ |
| 98 |
distanceDecoder.halve(); |
| 99 |
distCount=distanceDecoder.getMaxFrequency(); |
| 100 |
} |
| 101 |
distanceDecoder.update(code); |
| 102 |
distCount++; |
| 103 |
}; |
| 104 |
|
| 105 |
uint32_t maxDist=std::min(uint32_t((outputStream.getOffset()+63)>>6),128U); |
| 106 |
if (distancePosition!=maxDist) |
| 107 |
{ |
| 108 |
for (uint32_t i=distancePosition;i<maxDist;i++) |
| 109 |
{ |
| 110 |
distanceDecoder.addCode(); |
| 111 |
updateDist(i); |
| 112 |
} |
| 113 |
distancePosition=maxDist; |
| 114 |
} |
| 115 |
|
| 116 |
uint32_t distance=distanceDecoder.decode(readBit); |
| 117 |
updateDist(distance); |
| 118 |
distance=(distance<<6)|readBits(6); |
| 119 |
distance++; |
| 120 |
|
| 121 |
outputStream.copy(distance,count,0x20); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
} |