Debian Codegraph @

src/attic/Lzh/LH1Decompressor.cpp

raw
1 /* Copyright (C) Teemu Suutari */
2
3 #include "LH1Decompressor.hpp"
4
5 #include "../HuffmanDecoder.hpp"
6 #include "../DynamicHuffmanDecoder.hpp"
7 #include "../InputStream.hpp"
8 #include "../OutputStream.hpp"
9
10 namespace ancient::internal
11 {
12
13 LH1Decompressor::LH1Decompressor(const Buffer &packedData) :
14 _packedData(packedData)
15 {
16 // nothing needed
17 }
18
19 LH1Decompressor::~LH1Decompressor()
20 {
21 // nothing needed
22 }
23
24 size_t LH1Decompressor::getRawSize() const noexcept
25 {
26 // N/A
27 return 0;
28 }
29
30 size_t LH1Decompressor::getPackedSize() const noexcept
31 {
32 // N/A
33 return 0;
34 }
35
36 const std::string &LH1Decompressor::getName() const noexcept
37 {
38 static std::string name="LHA: LH1";
39 return name;
40 }
41
42 void LH1Decompressor::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 DynamicHuffmanDecoder<314> decoder;
58
59 static const uint8_t distanceHighBits[64]={
60 3,4,4,4,5,5,5,5, 5,5,5,5,6,6,6,6,
61 6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,
62 7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,
63 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8
64 };
65
66 HuffmanDecoder<uint8_t> distanceDecoder;
67 distanceDecoder.createOrderlyHuffmanTable(distanceHighBits,64);
68
69 while (!outputStream.eof())
70 {
71 uint32_t code=decoder.decode(readBit);
72 if (decoder.getMaxFrequency()==0x8000U) decoder.halve();
73 decoder.update(code);
74
75 if (code<256)
76 {
77 outputStream.writeByte(code);
78 } else {
79 uint32_t distance=distanceDecoder.decode(readBit);
80 distance=(distance<<6)|readBits(6);
81 distance++;
82
83 uint32_t count=code-253;
84
85 outputStream.copy(distance,count,0x20);
86 }
87 }
88 }
89
90 }