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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/* 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 MACVENTURE_CONTAINER_H
#define MACVENTURE_CONTAINER_H
#include "macventure/macventure.h"
#include "common/file.h"
#include "common/bitstream.h"
namespace MacVenture {
struct ItemGroup {
uint32 bitOffset; //It's really uint24
uint32 offset; //It's really uint24
uint32 lengths[64];
};
typedef uint32 ContainerHeader;
class Container {
public:
Container(const char *filename) {
_filename = Common::String(filename);
if (!_file.open(filename))
error("Could not open %s", filename);
_res = _file.readStream(_file.size());
_header = _res->readUint32BE();
_simplified = false;
if (!(_header & 0x80000000)) {
// Is simplified container
_simplified = true;
int dataLen = _res->size() - sizeof(_header);
_lenObjs = _header;
_numObjs = dataLen / _lenObjs;
} else {
_header &= 0x7fffffff;
_res->seek(_header, SEEK_SET);
_numObjs = _res->readUint16BE();
for (uint i = 0; i < 15; ++i)
_huff[i] = _res->readUint16BE();
for (uint i = 0; i < 16; ++i)
_lens[i] = _res->readByte();
// Read groups
uint numGroups = _numObjs / 64;
if ((_numObjs % 64) > 0)
numGroups++;
for (uint i = 0; i < numGroups; ++i) {
ItemGroup group;
// Place myself in the correct position to read group
_res->seek(_header + (i * 6) + 0x30, SEEK_SET);
byte b1, b2, b3;
b1 = _res->readByte();
b2 = _res->readByte();
b3 = _res->readByte();
group.bitOffset = (b1 << 16) + (b2 << 8) + (b3 << 0);
b1 = _res->readByte();
b2 = _res->readByte();
b3 = _res->readByte();
group.offset = (b1 << 16) + (b2 << 8) + (b3 << 0);
// Place the bit reader in the correct position
// group.bitOffset indicates the offset from the start of the subHeader
_res->seek(_header + (group.bitOffset >> 3), SEEK_SET);
uint32 bits = group.bitOffset & 7;
for (uint j = 0; j < 64; ++j) {
uint32 length = 0;
//debug("reading mask from address %x", _res->pos());
uint32 mask = _res->readUint32BE();
mask >>= (16 - bits);
mask &= 0xFFFF;
debug(11, "Load mask of object &%d:%d is %x", i, j, mask);
_res->seek(-4, SEEK_CUR);
// Look in the Huffman table
int x = 0;
for (x = 0; x < 16; x++) {
if (_huff[x] > mask) break;
}
// I will opt to copy the code from webventure,
// But according to the docs, this call should suffice:
// length = bitStream.getBits(_lens[x]);
// The problem is that _lens[] usually contains values larger
// Than 32, so we have to read them with the method below
//This code below, taken from the implementation, seems to give the same results.
uint32 bitSize = _lens[x];
bits += bitSize & 0xF;
if (bits & 0x10) {
bits &= 0xF;
_res->seek(2, SEEK_CUR);
}
bitSize >>= 4;
if (bitSize) {
length = _res->readUint32BE();
_res->seek(-4, SEEK_CUR);
bitSize--;
if (bitSize == 0) length = 0;
else length >>= (32 - bitSize) - bits;
length &= (1 << bitSize) - 1;
length |= 1 << bitSize;
bits += bitSize;
if (bits & 0x10) {
bits &= 0xF;
_res->seek(2, SEEK_CUR);
}
}
group.lengths[j] = length;
debug(11, "Load legth of object %d:%d is %d", i, j, length);
}
_groups.push_back(group);
}
}
}
~Container() {
if (_file.isOpen())
_file.close();
if (_res)
delete _res;
}
public:
/**
* Must be called before retrieving an object.
*/
uint32 getItemByteSize(uint32 id) {
if (_simplified) {
return _lenObjs;
} else {
uint32 groupID = (id >> 6);
uint32 objectIndex = id & 0x3f; // Index within the group
return _groups[groupID].lengths[objectIndex];
}
}
/**
* getItemByteSize should be called before this one
*/
Common::SeekableReadStream *getItem(uint32 id) {
if (_simplified) {
_res->seek((id * _lenObjs) + sizeof(_header), SEEK_SET);
} else {
uint32 groupID = (id >> 6);
uint32 objectIndex = id & 0x3f; // Index within the group
uint32 offset = 0;
for (uint i = 0; i < objectIndex; i++) {
offset += _groups[groupID].lengths[i];
}
_res->seek(_groups[groupID].offset + offset + sizeof(_header), SEEK_SET);
}
Common::SeekableReadStream *res = _res->readStream(getItemByteSize(id) * 2);
return res;
}
protected:
bool _simplified;
uint _lenObjs; // In the case of simple container, lenght of an object
uint _numObjs;
ContainerHeader _header;
uint16 _huff[15]; // huffman masks
uint8 _lens[16]; // huffman lengths
Common::Array<ItemGroup> _groups;
Common::String _filename;
Common::File _file;
Common::SeekableReadStream *_res;
};
} // End of namespace MacVenture
#endif
|