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  */
22 
23 #include "common/debug.h"
24 #include "common/endian.h"
25 #include "common/textconsole.h"
26 
27 #include "cine/cine.h"
28 #include "cine/unpack.h"
29 #include "cine/various.h"
30 
31 namespace Cine {
32 
loadPart(const char * partName)33 void loadPart(const char *partName) {
34 	g_cine->_partBuffer.clear();
35 
36 	g_cine->_partFileHandle.close();
37 
38 	checkDataDisk(-1);
39 
40 	if (!g_cine->_partFileHandle.open(partName))
41 		error("loadPart(): Cannot open file %s", partName);
42 
43 	setMouseCursor(MOUSE_CURSOR_DISK);
44 
45 	uint16 numElementInPart = g_cine->_partFileHandle.readUint16BE();
46 	g_cine->_partBuffer.resize(numElementInPart);
47 	g_cine->_partFileHandle.readUint16BE(); // entry size
48 
49 	if (currentPartName != partName)
50 		Common::strlcpy(currentPartName, partName, sizeof(currentPartName));
51 
52 	for (uint16 i = 0; i < g_cine->_partBuffer.size(); i++) {
53 		g_cine->_partFileHandle.read(g_cine->_partBuffer[i].partName, 14);
54 		g_cine->_partBuffer[i].offset = g_cine->_partFileHandle.readUint32BE();
55 		g_cine->_partBuffer[i].packedSize = g_cine->_partFileHandle.readUint32BE();
56 		g_cine->_partBuffer[i].unpackedSize = g_cine->_partFileHandle.readUint32BE();
57 		g_cine->_partFileHandle.readUint32BE(); // unused
58 	}
59 
60 	if (g_cine->getGameType() == Cine::GType_FW && g_cine->getPlatform() == Common::kPlatformDOS && strcmp(partName, "BASESON.SND") != 0)
61 		loadPal(partName);
62 }
63 
closePart()64 void closePart() {
65 	// TODO
66 }
67 
fixVolCnfFileName(const uint8 * src,uint len)68 static Common::String fixVolCnfFileName(const uint8 *src, uint len) {
69 	assert(len == 11 || len == 13);
70 	// Copy source to a temporary buffer and force a trailing zero for string manipulation
71 	char tmp[14];
72 	memcpy(tmp, src, len);
73 	tmp[len] = 0;
74 
75 	if (len == 11) {
76 		// Filenames of length 11 have no separation of the extension and the basename
77 		// so that's why we have to convert them first. There's no trailing zero in them
78 		// either and they're always of the full length 11 with padding spaces. Extension
79 		// can be always found at offset 8 onwards.
80 		//
81 		// Examples of filename mappings:
82 		// "AEROPORTMSG" -> "AEROPORT.MSG"
83 		// "MITRAILLHP " -> "MITRAILL.HP" (Notice the trailing space after the extension)
84 		// "BOND10     " -> "BOND10"
85 		// "GIRL    SET" -> "GIRL.SET"
86 
87 		// Replace all space characters with zeroes
88 		for (uint i = 0; i < len; i++)
89 			if (tmp[i] == ' ')
90 				tmp[i] = 0;
91 		// Extract the filename's extension
92 		Common::String extension(tmp + 8);
93 		tmp[8] = 0; // Force separation of extension and basename
94 		Common::String basename(tmp);
95 		if (extension.empty()) {
96 			return basename;
97 		} else {
98 			return basename + "." + extension;
99 		}
100 	} else {
101 		// Filenames of length 13 are okay as they are, no need for conversion
102 		return Common::String(tmp);
103 	}
104 }
105 
readVolCnf()106 void CineEngine::readVolCnf() {
107 	Common::File f;
108 	if (!f.open("vol.cnf")) {
109 		error("Unable to open 'vol.cnf'");
110 	}
111 	uint32 unpackedSize, packedSize;
112 	char hdr[8];
113 	f.read(hdr, 8);
114 	bool compressed = (memcmp(hdr, "ABASECP", 7) == 0);
115 	if (compressed) {
116 		unpackedSize = f.readUint32BE();
117 		packedSize = f.readUint32BE();
118 	} else {
119 		f.seek(0);
120 		unpackedSize = packedSize = f.size();
121 	}
122 	uint8 *buf = new uint8[unpackedSize];
123 	uint8 *packedBuf = new uint8[packedSize];
124 	f.read(packedBuf, packedSize);
125 	CineUnpacker cineUnpacker;
126 	if (!cineUnpacker.unpack(packedBuf, packedSize, buf, unpackedSize)) {
127 		error("Error while unpacking 'vol.cnf' data");
128 	}
129 	delete[] packedBuf;
130 	uint8 *p = buf;
131 	int resourceFilesCount = READ_BE_UINT16(p); p += 2;
132 	int entrySize = READ_BE_UINT16(p); p += 2;
133 	for (int i = 0; i < resourceFilesCount; ++i) {
134 		char volumeResourceFile[9];
135 		memcpy(volumeResourceFile, p, 8);
136 		volumeResourceFile[8] = 0;
137 		_volumeResourceFiles.push_back(volumeResourceFile);
138 		p += entrySize;
139 	}
140 
141 	// Check file name blocks' sizes
142 	bool fileNameLenMod11, fileNameLenMod13;
143 	fileNameLenMod11 = fileNameLenMod13 = true;
144 	for (int i = 0; i < resourceFilesCount; ++i) {
145 		int size = READ_BE_UINT32(p); p += 4;
146 		fileNameLenMod11 &= ((size % 11) == 0);
147 		fileNameLenMod13 &= ((size % 13) == 0);
148 		p += size;
149 	}
150 	// Make sure at least one of the candidates for file name length fits the data
151 	assert(fileNameLenMod11 || fileNameLenMod13);
152 
153 	// File name length used to be deduced from the fact whether the file
154 	// was compressed or not. Compressed files used file name length 11,
155 	// uncompressed files used file name length 13. It worked almost always,
156 	// but not with the game entry that's detected as the Operation Stealth's
157 	// US Amiga release. It uses a compressed 'vol.cnf' file but still uses
158 	// file names of length 13. So we try to deduce the file name length from
159 	// the data in the 'vol.cnf' file.
160 	int fileNameLength;
161 	if (fileNameLenMod11 != fileNameLenMod13) {
162 		// All file name blocks' sizes were divisible by either 11 or 13, but not with both.
163 		fileNameLength = (fileNameLenMod11 ? 11 : 13);
164 	} else {
165 		warning("Couldn't deduce file name length from data in 'vol.cnf', using a backup deduction scheme");
166 		// Here we use the former file name length detection method
167 		// if we couldn't deduce the file name length from the data.
168 		fileNameLength = (compressed ? 11 : 13);
169 	}
170 
171 	p = buf + 4 + resourceFilesCount * entrySize;
172 	for (int i = 0; i < resourceFilesCount; ++i) {
173 		int count = READ_BE_UINT32(p) / fileNameLength; p += 4;
174 		while (count--) {
175 			Common::String volumeEntryName = fixVolCnfFileName(p, fileNameLength);
176 			_volumeEntriesMap.setVal(volumeEntryName, _volumeResourceFiles[i].c_str());
177 			debugC(5, kCineDebugPart, "Added volume entry name '%s' resource file '%s'", volumeEntryName.c_str(), _volumeResourceFiles[i].c_str());
178 			p += fileNameLength;
179 		}
180 	}
181 
182 	delete[] buf;
183 }
184 
findFileInBundle(const char * fileName)185 int16 findFileInBundle(const char *fileName) {
186 	if (g_cine->getGameType() == Cine::GType_OS) {
187 		// look first in currently loaded resource file
188 		for (uint i = 0; i < g_cine->_partBuffer.size(); i++) {
189 			if (!scumm_stricmp(fileName, g_cine->_partBuffer[i].partName)) {
190 				return i;
191 			}
192 		}
193 		// not found, open the required resource file
194 		StringPtrHashMap::const_iterator it = g_cine->_volumeEntriesMap.find(fileName);
195 		if (it == g_cine->_volumeEntriesMap.end()) {
196 			warning("Unable to find part file for filename '%s'", fileName);
197 			return -1;
198 		}
199 		const char *part = (*it)._value;
200 		loadPart(part);
201 	}
202 	for (uint i = 0; i < g_cine->_partBuffer.size(); i++) {
203 		if (!scumm_stricmp(fileName, g_cine->_partBuffer[i].partName)) {
204 			return i;
205 		}
206 	}
207 	return -1;
208 }
209 
readFromPart(int16 idx,byte * dataPtr,uint32 maxSize)210 void readFromPart(int16 idx, byte *dataPtr, uint32 maxSize) {
211 	assert(maxSize >= g_cine->_partBuffer[idx].packedSize);
212 	setMouseCursor(MOUSE_CURSOR_DISK);
213 
214 	g_cine->_partFileHandle.seek(g_cine->_partBuffer[idx].offset, SEEK_SET);
215 	g_cine->_partFileHandle.read(dataPtr, g_cine->_partBuffer[idx].packedSize);
216 }
217 
readBundleFile(int16 foundFileIdx,uint32 * size)218 byte *readBundleFile(int16 foundFileIdx, uint32 *size) {
219 	assert(foundFileIdx >= 0 && foundFileIdx < (int32)g_cine->_partBuffer.size());
220 	bool error = false;
221 	byte *dataPtr = (byte *)calloc(g_cine->_partBuffer[foundFileIdx].unpackedSize, 1);
222 	byte *packedData = (byte *)calloc(g_cine->_partBuffer[foundFileIdx].packedSize, 1);
223 	assert(dataPtr && packedData);
224 	readFromPart(foundFileIdx, packedData, g_cine->_partBuffer[foundFileIdx].packedSize);
225 	CineUnpacker cineUnpacker;
226 	error = !cineUnpacker.unpack(packedData, g_cine->_partBuffer[foundFileIdx].packedSize, dataPtr, g_cine->_partBuffer[foundFileIdx].unpackedSize);
227 	free(packedData);
228 
229 	if (error) {
230 		warning("Error unpacking '%s' from bundle file '%s'", g_cine->_partBuffer[foundFileIdx].partName, currentPartName);
231 	}
232 
233 	// Set the size variable if a pointer to it has been given
234 	if (size != NULL) {
235 		*size = g_cine->_partBuffer[foundFileIdx].unpackedSize;
236 	}
237 
238 	return dataPtr;
239 }
240 
readBundleSoundFile(const char * entryName,uint32 * size)241 byte *readBundleSoundFile(const char *entryName, uint32 *size) {
242 	int16 index;
243 	byte *data = 0;
244 	char previousPartName[15] = "";
245 
246 	if (g_cine->getGameType() == Cine::GType_FW) {
247 		strcpy(previousPartName, currentPartName);
248 		loadPart("BASESON.SND");
249 	}
250 	index = findFileInBundle((const char *)entryName);
251 	if (index != -1) {
252 		data = readBundleFile(index);
253 		if (size) {
254 			*size = g_cine->_partBuffer[index].unpackedSize;
255 		}
256 	}
257 	if (g_cine->getGameType() == Cine::GType_FW) {
258 		loadPart(previousPartName);
259 	}
260 	return data;
261 }
262 
263 /** Rotate byte value to the left by n bits */
rolByte(byte value,uint n)264 byte rolByte(byte value, uint n) {
265 	n %= 8;
266 	return (byte)((value << n) | (value >> (8 - n)));
267 }
268 
readFile(const char * filename,bool crypted)269 byte *readFile(const char *filename, bool crypted) {
270 	Common::File in;
271 
272 	in.open(filename);
273 
274 	if (!in.isOpen())
275 		error("readFile(): Cannot open file %s", filename);
276 
277 	uint32 size = in.size();
278 
279 	byte *dataPtr = (byte *)malloc(size);
280 	in.read(dataPtr, size);
281 
282 	// The Sony published CD version of Future Wars has its
283 	// AUTO00.PRC file's bytes rotated to the right by one.
284 	// So we decode the so called crypting by rotating all
285 	// the bytes to the left by one.
286 	if (crypted) {
287 		for (uint index = 0; index < size; index++) {
288 			dataPtr[index] = rolByte(dataPtr[index], 1);
289 		}
290 	}
291 
292 	return dataPtr;
293 }
294 
checkDataDisk(int16 param)295 void checkDataDisk(int16 param) {
296 }
297 
dumpBundle(const char * fileName)298 void dumpBundle(const char *fileName) {
299 	char tmpPart[15];
300 
301 	strcpy(tmpPart, currentPartName);
302 
303 	loadPart(fileName);
304 	for (uint i = 0; i < g_cine->_partBuffer.size(); i++) {
305 		byte *data = readBundleFile(i);
306 
307 		debug(0, "%s", g_cine->_partBuffer[i].partName);
308 
309 		Common::DumpFile out;
310 		if (out.open(Common::String("dumps/") + g_cine->_partBuffer[i].partName)) {
311 			out.write(data, g_cine->_partBuffer[i].unpackedSize);
312 			out.close();
313 		}
314 
315 		free(data);
316 	}
317 
318 	loadPart(tmpPart);
319 }
320 
321 } // End of namespace Cine
322