1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * $URL: https://scummvm-startrek.googlecode.com/svn/trunk/lzss.cpp $
22 * $Id: lzss.cpp 18 2010-12-17 01:22:54Z clone2727 $
23 *
24 */
25
26 #include "common/textconsole.h"
27 #include "common/memstream.h"
28 #include "common/util.h"
29
30 #include "startrek/lzss.h"
31
32 namespace StarTrek {
33
decodeLZSS(Common::SeekableReadStream * indata,uint32 uncompressedSize)34 Common::SeekableReadStream *decodeLZSS(Common::SeekableReadStream *indata, uint32 uncompressedSize) {
35 uint32 N = 0x1000; /* History buffer size */
36 byte *histbuff = new byte[N]; /* History buffer */
37 memset(histbuff, 0, N);
38 uint32 outstreampos = 0;
39 uint32 bufpos = 0;
40 byte *outLzssBufData = (byte *)malloc(uncompressedSize);
41
42 for (;;) {
43 byte flagbyte = indata->readByte();
44
45 if (indata->eos())
46 break;
47
48 for (byte i = 0; i < 8; i++) {
49 if ((flagbyte & (1 << i)) == 0) {
50 uint32 offsetlen = indata->readUint16LE();
51
52 if (indata->eos())
53 break;
54
55 uint32 length = (offsetlen & 0xF) + 3;
56 uint32 offset = (bufpos - (offsetlen >> 4)) & (N - 1);
57 for (uint32 j = 0; j < length; j++) {
58 byte tempa = histbuff[(offset + j) & (N - 1)];
59 outLzssBufData[outstreampos++] = tempa;
60 histbuff[bufpos] = tempa;
61 bufpos = (bufpos + 1) & (N - 1);
62 }
63 } else {
64 byte tempa = indata->readByte();
65
66 if (indata->eos())
67 break;
68
69 outLzssBufData[outstreampos++] = tempa;
70 histbuff[bufpos] = tempa;
71 bufpos = (bufpos + 1) & (N - 1);
72 }
73 }
74 }
75
76 delete[] histbuff;
77
78 if (outstreampos != uncompressedSize)
79 error("Size mismatch in LZSS decompression; expected %d bytes, got %d bytes", uncompressedSize, outstreampos);
80
81 return new Common::MemoryReadStream(outLzssBufData, uncompressedSize, DisposeAfterUse::YES);
82 }
83
84 } // End of namespace StarTrek
85
86