aboutsummaryrefslogtreecommitdiff
path: root/engines/groovie/lzss.cpp
blob: 889a12bd1234aba67bbc5097189895df6ae505c0 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "groovie/lzss.h"

namespace Groovie {

#define OUT_BUFF_SIZE 131072
#define COMP_THRESH 3 // Compression not attempted if string to be compressed is less than 3 long

LzssReadStream::LzssReadStream(Common::ReadStream *indata, uint8 lengthmask, uint8 lengthbits) {
	/*
	TODO: Nasty hack. Make a buffer bigger than I'll ever need... probably.
	What should *really* happen is I should define a whole new type of stream
	that gets lzss decompressed on the fly
	*/
	_outLzssBufData = (uint8 *)malloc(OUT_BUFF_SIZE);
	_size = decodeLZSS(indata, lengthmask, lengthbits);
	_pos = 0;
}

LzssReadStream::~LzssReadStream() {
	free(_outLzssBufData);
}

uint32 LzssReadStream::decodeLZSS(Common::ReadStream *in, uint8 lengthmask, uint8 lengthbits) {
	uint32 N = 1 << (16 - lengthbits); /* History buffer size */
	byte *histbuff = new byte[N]; /* History buffer */
	memset(histbuff, 0, N);
	uint32 outstreampos = 0;
	uint32 bufpos = 0;

	while (!in->eos()) {
		byte flagbyte = in->readByte();
		for (uint32 i = 1; i <= 8; i++) {
			if (!in->eos()) {
				if ((flagbyte & 1) == 0) {
					uint32 offsetlen = in->readUint16LE();
					if (offsetlen == 0) {
						break;
					}
					uint32 length = (offsetlen & lengthmask) + COMP_THRESH;
					uint32 offset = (bufpos - (offsetlen >> lengthbits)) & (N - 1);
					for (uint32 j = 0; j < length; j++) {
						byte tempa = histbuff[(offset + j) & (N - 1)];
						_outLzssBufData[outstreampos++] = tempa;
						histbuff[bufpos] = tempa;
						bufpos = (bufpos + 1) & (N - 1);
					}
				} else {
					byte tempa = in->readByte();
					if (in->eos()) {
						break;
					}
					_outLzssBufData[outstreampos++] = tempa;
					histbuff[bufpos] = tempa;
					bufpos = (bufpos + 1) & (N - 1);
				}
				flagbyte = flagbyte >> 1;
			}
		}
	}
	delete[] histbuff;
	return outstreampos;
}

bool LzssReadStream::eos() const {
	return _pos >= _size;
}

uint32 LzssReadStream::read(void *buf, uint32 size) {
	if (size > _size - _pos)
		size = _size - _pos;

	memcpy(buf, &_outLzssBufData[_pos], size);
	_pos += size;

	return size;
}

} // End of Groovie namespace