[go: up one dir, main page]

File: LZW2Decompressor.cpp

package info (click to toggle)
ancient 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,168 kB
  • sloc: cpp: 15,188; makefile: 217; sh: 31
file content (71 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Copyright (C) Teemu Suutari */

#include "LZW2Decompressor.hpp"
#include "InputStream.hpp"
#include "OutputStream.hpp"
#include "common/Common.hpp"


namespace ancient::internal
{

bool LZW2Decompressor::detectHeaderXPK(uint32_t hdr) noexcept
{
	return hdr==FourCC("LZW2") || hdr==FourCC("LZW3");
}

std::shared_ptr<XPKDecompressor> LZW2Decompressor::create(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify)
{
	return std::make_shared<LZW2Decompressor>(hdr,recursionLevel,packedData,state,verify);
}

LZW2Decompressor::LZW2Decompressor(uint32_t hdr,uint32_t recursionLevel,const Buffer &packedData,std::shared_ptr<XPKDecompressor::State> &state,bool verify) :
	XPKDecompressor{recursionLevel},
	_packedData{packedData}
{
	if (!detectHeaderXPK(hdr))
		throw Decompressor::InvalidFormatError();
	_ver=(hdr==FourCC("LZW2"))?2:3;
}

const std::string &LZW2Decompressor::getSubName() const noexcept
{
	static std::string name2{"XPK-LZW2: LZW2 CyberYAFA compressor"};
	static std::string name3{"XPK-LZW3: LZW3 CyberYAFA compressor"};
	return (_ver==2)?name2:name3;
}

void LZW2Decompressor::decompressImpl(Buffer &rawData,const Buffer &previousData,bool verify)
{
	ForwardInputStream inputStream{_packedData,0,_packedData.size()};
	LSBBitReader<ForwardInputStream> bitReader{inputStream};
	auto readBit=[&]()->uint32_t
	{
		return bitReader.readBitsBE32(1);
	};
	auto readByte=[&]()->uint8_t
	{
		return inputStream.readByte();
	};

	ForwardOutputStream outputStream{rawData,0,rawData.size()};

	while (!outputStream.eof())
	{
		if (!readBit())
		{
			outputStream.writeByte(readByte());
		} else {
			uint32_t distance{uint32_t(readByte())<<8};
			distance|=uint32_t(readByte());
			if (!distance)
				throw Decompressor::DecompressionError();
			distance=65536-distance;
			uint32_t count{uint32_t(readByte())+4};

			outputStream.copy(distance,count);
		}
	}
}

}