aboutsummaryrefslogtreecommitdiff
path: root/engines/made/redreader.h
blob: 84181fb72298e6160c116e19107883f0bf6f96f1 (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
/* 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.
 *
 */

#ifndef MADE_REDREADER_H
#define MADE_REDREADER_H

#include "common/util.h"
#include "common/file.h"
#include "common/stream.h"

namespace Made {

class RedReader {
public:
	Common::SeekableReadStream *load(const char *redFilename, const char *filename);
	static Common::SeekableReadStream *loadFromRed(const char *redFilename, const char *filename);
private:
	struct FileEntry {
		uint32 compSize, origSize;
	};
	bool seekFile(Common::File &fd, FileEntry &fileEntry, const char *filename);
};

const uint BITBUFSIZ = 16;
const uint DICBIT = 13;
const uint DICSIZ = 1 << DICBIT;
const uint MATCHBIT = 8;
const uint MAXMATCH = 256;
const uint THRESHOLD = 3;
const uint NC = 255 + MAXMATCH + 2 - THRESHOLD;
const uint CBIT = 9;
const uint CODE_BIT = 16;
const uint NP = DICBIT + 1;
const int NT = CODE_BIT + 3;
const uint PBIT = 4;
const uint TBIT = 5;
const uint NPT = NT;

class LzhDecompressor {
public:
	LzhDecompressor();
	~LzhDecompressor();
	int decompress(Common::SeekableReadStream &source, byte *dest, uint32 compSize, uint32 origSize);
private:
	Common::SeekableReadStream *_source;
	uint32 _compSize, _blockPos;

	uint16 _bitbuf;
	uint _subbitbuf;
	int _bitcount;
	uint16 _left[2 * NC - 1], _right[2 * NC - 1];
	byte _c_len[NC], _pt_len[NPT];
	uint _blocksize;
	uint16 _c_table[4096], _pt_table[256];
	int tree_n, heapsize;
	short heap[NC + 1];
	uint16 *freq, *sortptr, len_cnt[17];
	byte *len_table;

	int decode_i, decode_j;
	int count_len_depth;

	byte readByte();

	void fillbuf(int count);
	uint getbits(int count);
	void init_getbits();
	void decode_start();
	void decode(uint count, byte text[]);
	void huf_decode_start();
	unsigned int decode_c();
	unsigned int decode_p();
	void read_pt_len(int nn, int nbit, int i_special);
	void read_c_len();
	void count_len(int i);
	void make_len(int root);
	void downheap(int i);
	void make_code(int n, byte len[], uint16 code[]);
	void make_table(uint nchar, byte bitlen[], uint tablebits, uint16 table[]);
	int make_tree(int nparm, uint16 freqparm[], byte lenparm[], uint16 codeparm[]);

};

} // End of namespace Made

#endif /* MADE_H */