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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2005 The ScummVM project
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*
*/
#include "scumm/util.h"
namespace Scumm {
#pragma mark -
#pragma mark --- ScummFile ---
#pragma mark -
ScummFile::ScummFile() : _encbyte(0), _subFileStart(0), _subFileLen(0) {
}
void ScummFile::setEnc(byte value) {
_encbyte = value;
}
void ScummFile::setSubfileRange(uint32 start, uint32 len) {
// TODO: Add sanity checks
const uint32 fileSize = File::size();
assert(start <= fileSize);
assert(start + len <= fileSize);
_subFileStart = start;
_subFileLen = len;
seek(0, SEEK_SET);
}
void ScummFile::resetSubfile() {
_subFileStart = 0;
_subFileLen = 0;
seek(0, SEEK_SET);
}
bool ScummFile::open(const char *filename, AccessMode mode) {
if (File::open(filename, mode)) {
resetSubfile();
return true;
} else {
return false;
}
}
bool ScummFile::openSubFile(const char *filename) {
assert(isOpen());
// Disable the XOR encryption and reset any current subfile range
setEnc(0);
resetSubfile();
// Read in the filename table and look for the specified file
unsigned long file_off, file_len;
char file_name[0x20+1];
unsigned long i;
// Get the length of the data file to use for consistency checks
const uint32 data_file_len = size();
// Read offset and length to the file records */
const uint32 file_record_off = readUint32BE();
const uint32 file_record_len = readUint32BE();
// Do a quick check to make sure the offset and length are good
if (file_record_off + file_record_len > data_file_len) {
return false;
}
// Do a little consistancy check on file_record_length
if (file_record_len % 0x28) {
return false;
}
// Scan through the files
for (i = 0; i < file_record_len; i += 0x28) {
// read a file record
seek(file_record_off + i, SEEK_SET);
file_off = readUint32BE();
file_len = readUint32BE();
read(file_name, 0x20);
file_name[0x20] = 0;
assert(file_name[0]);
//debug(7, " extracting \'%s\'", file_name);
// Consistency check. make sure the file data is in the file
if (file_off + file_len > data_file_len) {
return false;
}
if (scumm_stricmp(file_name, filename) == 0) {
// We got a match!
setSubfileRange(file_off, file_len);
return true;
}
}
return false;
}
bool ScummFile::eof() {
return _subFileLen ? (pos() >= _subFileLen) : File::eof();
}
uint32 ScummFile::pos() {
return File::pos() - _subFileStart;
}
uint32 ScummFile::size() {
return _subFileLen ? _subFileLen : File::size();
}
void ScummFile::seek(int32 offs, int whence) {
if (_subFileLen) {
// Constrain the seek to the subfile
switch (whence) {
case SEEK_END:
offs = _subFileStart + _subFileLen - offs;
break;
case SEEK_SET:
offs += _subFileStart;
break;
case SEEK_CUR:
offs += File::pos();
break;
}
assert((int32)_subFileStart <= offs && offs <= (int32)(_subFileStart + _subFileLen));
whence = SEEK_SET;
}
File::seek(offs, whence);
}
uint32 ScummFile::read(void *ptr, uint32 len) {
uint32 realLen;
if (_subFileLen) {
// Limit the amount we read by the subfile boundaries.
const uint32 curPos = pos();
assert(_subFileLen >= curPos);
uint32 newPos = curPos + len;
if (newPos > _subFileLen) {
len = _subFileLen - curPos;
_ioFailed = true;
}
}
realLen = File::read(ptr, len);
// If an encryption byte was specified, XOR the data we just read by it.
// This simple kind of "encryption" was used by some of the older SCUMM
// games.
if (_encbyte) {
byte *p = (byte *)ptr;
byte *end = p + realLen;
while (p < end)
*p++ ^= _encbyte;
}
return realLen;
}
uint32 ScummFile::write(const void *, uint32) {
error("ScummFile does not support writing!");
}
#pragma mark -
#pragma mark --- Utilities ---
#pragma mark -
void checkRange(int max, int min, int no, const char *str) {
if (no < min || no > max) {
char buf[256];
snprintf(buf, sizeof(buf), str, no);
error("Value %d is out of bounds (%d,%d) (%s)", no, min, max, buf);
}
}
/**
* Convert an old style direction to a new style one (angle),
*/
int newDirToOldDir(int dir) {
if (dir >= 71 && dir <= 109)
return 1;
if (dir >= 109 && dir <= 251)
return 2;
if (dir >= 251 && dir <= 289)
return 0;
return 3;
}
/**
* Convert an new style (angle) direction to an old style one.
*/
int oldDirToNewDir(int dir) {
assert(0 <= dir && dir <= 3);
const int new_dir_table[4] = { 270, 90, 180, 0 };
return new_dir_table[dir];
}
/**
* Convert an angle to a simple direction.
*/
int toSimpleDir(int dirType, int dir) {
if (dirType) {
const int16 directions[] = { 22, 72, 107, 157, 202, 252, 287, 337 };
for (int i = 0; i < 7; i++)
if (dir >= directions[i] && dir <= directions[i+1])
return i+1;
} else {
const int16 directions[] = { 71, 109, 251, 289 };
for (int i = 0; i < 3; i++)
if (dir >= directions[i] && dir <= directions[i+1])
return i+1;
}
return 0;
}
/**
* Convert a simple direction to an angle.
*/
int fromSimpleDir(int dirType, int dir) {
if (dirType)
return dir * 45;
else
return dir * 90;
}
/**
* Normalize the given angle - that means, ensure it is positive, and
* change it to the closest multiple of 45 degree by abusing toSimpleDir.
*/
int normalizeAngle(int angle) {
int temp;
temp = (angle + 360) % 360;
return toSimpleDir(1, temp) * 45;
}
const char *tag2str(uint32 tag) {
static char str[5];
str[0] = (char)(tag >> 24);
str[1] = (char)(tag >> 16);
str[2] = (char)(tag >> 8);
str[3] = (char)tag;
str[4] = '\0';
return str;
}
} // End of namespace Scumm
|