aboutsummaryrefslogtreecommitdiff
path: root/engines/dm/lzw.cpp
blob: dc5055f6874c63687888c29b81d1c0695aed054a (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

/*
* Based on the Reverse Engineering work of Christophe Fontanel,
* maintainer of the Dungeon Master Encyclopaedia (http://dmweb.free.fr/)
*/

#include "common/memstream.h"

#include "dm/lzw.h"

namespace DM {

LZWdecompressor::LZWdecompressor() {
	_repetitionEnabled = false;
	_codeBitCount = 0;
	_currentMaximumCode = 0;
	_absoluteMaximumCode = 4096;
	for (int i = 0; i < 12; ++i)
		_inputBuffer[i] = 0;
	_dictNextAvailableCode = 0;
	_dictFlushed = false;

	byte leastSignificantBitmasks[9] = {0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
	for (uint16 i = 0; i < 9; ++i)
		_leastSignificantBitmasks[i] = leastSignificantBitmasks[i];
	_inputBufferBitIndex = 0;
	_inputBufferBitCount = 0;
	_charToRepeat = 0;

	_tempBuffer = new byte[5004];
	_prefixCode = new int16[5003];
	_appendCharacter = new byte[5226];
}

LZWdecompressor::~LZWdecompressor() {
	delete[] _appendCharacter;
	delete[] _prefixCode;
	delete[] _tempBuffer;
}

int16 LZWdecompressor::getNextInputCode(Common::MemoryReadStream &inputStream, int32 *inputByteCount) {
	byte *inputBuffer = _inputBuffer;
	if (_dictFlushed || (_inputBufferBitIndex >= _inputBufferBitCount) || (_dictNextAvailableCode > _currentMaximumCode)) {
		if (_dictNextAvailableCode > _currentMaximumCode) {
			_codeBitCount++;
			if (_codeBitCount == 12) {
				_currentMaximumCode = _absoluteMaximumCode;
			} else {
				_currentMaximumCode = (1 << _codeBitCount) - 1;
			}
		}
		if (_dictFlushed) {
			_currentMaximumCode = (1 << (_codeBitCount = 9)) - 1;
			_dictFlushed = false;
		}
		if (*inputByteCount > _codeBitCount) {
			_inputBufferBitCount = _codeBitCount;
		} else {
			_inputBufferBitCount = *inputByteCount;
		}
		if (_inputBufferBitCount > 0) {
			inputStream.read(_inputBuffer, _inputBufferBitCount);
			*inputByteCount -= _inputBufferBitCount;
		} else {
			return -1;
		}
		_inputBufferBitIndex = 0;
		_inputBufferBitCount = (_inputBufferBitCount << 3) - (_codeBitCount - 1);
	}
	int16 bitIndex = _inputBufferBitIndex;
	int16 requiredInputBitCount = _codeBitCount;
	inputBuffer += bitIndex >> 3; /* Address of byte in input buffer containing current bit */
	bitIndex &= 0x0007; /* Bit index of the current bit in the byte */
	int16 nextInputCode = *inputBuffer++ >> bitIndex; /* Get the first bits of the next input code from the input buffer byte */
	requiredInputBitCount -= 8 - bitIndex; /* Remaining number of bits to get for a complete input code */
	bitIndex = 8 - bitIndex;
	if (requiredInputBitCount >= 8) {
		nextInputCode |= *inputBuffer++ << bitIndex;
		bitIndex += 8;
		requiredInputBitCount -= 8;
	}
	nextInputCode |= (*inputBuffer & _leastSignificantBitmasks[requiredInputBitCount]) << bitIndex;
	_inputBufferBitIndex += _codeBitCount;
	return nextInputCode;
}

void LZWdecompressor::outputCharacter(byte character, byte **out) {
	byte *output = *out;

	if (false == _repetitionEnabled) {
		if (character == 0x90)
			_repetitionEnabled = true;
		else
			*output++ = _charToRepeat = character;
	} else {
		if (character) { /* If character following 0x90 is not 0x00 then it is the repeat count */
			while (--character)
				*output++ = _charToRepeat;
		} else /* else output a 0x90 character */
			*output++ = 0x90;

		_repetitionEnabled = false;
	}

	*out = output;
	return;
}

int32 LZWdecompressor::decompress(Common::MemoryReadStream &inStream, int32 inputByteCount, byte *out) {
	byte *reversedDecodedStringStart;
	byte *reversedDecodedStringEnd = reversedDecodedStringStart = _tempBuffer;
	byte *originalOut = out;
	_repetitionEnabled = false;
	_codeBitCount = 9;
	_dictFlushed = false;
	_currentMaximumCode = (1 << (_codeBitCount = 9)) - 1;
	for (int16 code = 255; code >= 0; code--) {
		_prefixCode[code] = 0;
		_appendCharacter[code] = code;
	}
	_dictNextAvailableCode = 257;
	int16 oldCode;
	int16 character = oldCode = getNextInputCode(inStream, &inputByteCount);
	if (oldCode == -1) {
		return -1L;
	}
	outputCharacter(character, &out);
	int16 code;
	while ((code = getNextInputCode(inStream, &inputByteCount)) > -1) {
		if (code == 256) { /* This code is used to flush the dictionary */
			for (int i = 0; i < 256; ++i)
				_prefixCode[i] = 0;
			_dictFlushed = true;
			_dictNextAvailableCode = 256;
			if ((code = getNextInputCode(inStream, &inputByteCount)) == -1) {
				break;
			}
		}
		/* This code checks for the special STRING+CHARACTER+STRING+CHARACTER+STRING case which generates an undefined code.
		It handles it by decoding the last code, adding a single character to the end of the decoded string */
		int16 newCode = code;
		if (code >= _dictNextAvailableCode) { /* If code is not defined yet */
			*reversedDecodedStringEnd++ = character;
			code = oldCode;
		}
		/* Use the string table to decode the string corresponding to the code and store the string in the temporary buffer */
		while (code >= 256) {
			*reversedDecodedStringEnd++ = _appendCharacter[code];
			code = _prefixCode[code];
		}
		*reversedDecodedStringEnd++ = (character = _appendCharacter[code]);
		/* Output the decoded string in reverse order */
		do {
			outputCharacter(*(--reversedDecodedStringEnd), &out);
		} while (reversedDecodedStringEnd > reversedDecodedStringStart);
		/* If possible, add a new code to the string table */
		if ((code = _dictNextAvailableCode) < _absoluteMaximumCode) {
			_prefixCode[code] = oldCode;
			_appendCharacter[code] = character;
			_dictNextAvailableCode = code + 1;
		}
		oldCode = newCode;
	}
	return out - originalOut; /* Byte count of decompressed data */
}
}