1 /* ScummVM Tools
2  *
3  * ScummVM Tools is the legal property of its developers, whose
4  * names are too numerous to list here. Please refer to the
5  * COPYRIGHT 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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "extract_hdb.h"
26 #include <zlib.h>
27 
28 
ExtractHDB(const std::string & name)29 ExtractHDB::ExtractHDB(const std::string &name) : Tool(name, TOOLTYPE_EXTRACTION) {
30 	ToolInput input;
31 	input.format = "*.*";
32 	_inputPaths.push_back(input);
33 
34 	_outputToDirectory = true;
35 
36 	_shorthelp = "Used to extract Hyperspace Delivery Boy! archive files.";
37 	_helptext = "\nUsage: " + getName() + " [-o /path/to/output/dir/] <inputfile>\n";
38 
39 	_compressed = false;
40 }
41 
execute()42 void ExtractHDB::execute() {
43 	Common::Filename filename = _inputPaths[0].path;
44 
45 	if (!openMPC(filename))
46 		error("Unable to open %s", filename.getFullName().c_str());
47 
48 	Common::File fOut;
49 	for (Common::Array<MPCEntry *>::iterator it = _dir.begin(); it != _dir.end(); ++it) {
50 		byte *buffer = (byte *)malloc((*it)->length);
51 
52 		_mpcFile.seek((*it)->offset, SEEK_SET);
53 
54 		_mpcFile.read_noThrow(buffer, (*it)->length);
55 
56 		_outputPath.setFullName((*it)->filename);
57 
58 		fOut.open(_outputPath, "wb");
59 
60 		if (_compressed) {
61 			byte *buffer2 = (byte *)calloc((*it)->ulength, 1);
62 			unsigned long len = (*it)->ulength;
63 
64 			print("... decompressing %s", (*it)->filename);
65 
66 			if (uncompress(buffer2, &len, buffer, (*it)->length) != Z_OK) {
67 				print("Error uncompressing file %s", (*it)->filename);
68 
69 				fOut.write(buffer, (*it)->length);
70 			} else if (len != (*it)->ulength) {
71 				print("Size mismatch for %s: %d <> %d", (*it)->filename, len, (*it)->ulength);
72 
73 				fOut.write(buffer, (*it)->length);
74 			} else {
75 				fOut.write(buffer2, (*it)->ulength);
76 			}
77 
78 			free(buffer2);
79 		} else {
80 			print("... %s", (*it)->filename);
81 			fOut.write(buffer, (*it)->length);
82 		}
83 
84 		fOut.close();
85 
86 		free(buffer);
87 	}
88 
89 	_mpcFile.close();
90 }
91 
inspectInput(const Common::Filename & filename)92 InspectionMatch ExtractHDB::inspectInput(const Common::Filename &filename) {
93 	// Accept either 'vol.cat' or 'vol.dat'
94 	std::string file = filename.getFullName();
95 	if (
96 		scumm_stricmp(file.c_str(), "hyperspace.mpc") == 0 ||
97 		scumm_stricmp(file.c_str(), "hyperspace.msd") == 0 ||
98 		scumm_stricmp(file.c_str(), "hyperdemo.mpc") == 0 ||
99 		scumm_stricmp(file.c_str(), "hyperdemo.msd") == 0
100 	)
101 		return IMATCH_PERFECT;
102 	return IMATCH_AWFUL;
103 }
104 
openMPC(Common::Filename & filename)105 bool ExtractHDB::openMPC(Common::Filename &filename) {
106 	uint32 offset;
107 
108 	_mpcFile.open(filename, "rb");
109 	if (!_mpcFile.isOpen()) {
110 		error("FileMan::openMPC(): Error reading the MSD/MPC file %s", filename.getFullName().c_str());
111 		return false;
112 	}
113 
114 	_dataHeader.id = _mpcFile.readUint32BE();
115 
116 	if (_dataHeader.id == 'MPCC') {
117 		print("COMPRESSED MPC FILE");
118 		return false;
119 	} else if (_dataHeader.id == 'MPCU') {
120 		print("Unpacking uncompressed MPC file...");
121 		// we're fine
122 	} else if (_dataHeader.id == 'MSDC') {
123 		print("Unpacking compressed MSD file...");
124 		_compressed = true;
125 		// we're fine
126 	} else if (_dataHeader.id == 'MSDU') {
127 		print("UNCOMPRESSED MSD FILE");
128 		return false;
129 	} else {
130 		error("Invalid MPC/MSD File.");
131 		return false;
132 	}
133 
134 	// read the directory
135 	offset = _mpcFile.readUint32LE();
136 	_mpcFile.seek((int32)offset, SEEK_SET);
137 
138 	// Note: The MPC archive format assumes the offset to be uint32,
139 	// but Common::File::seek() takes the offset as int32.
140 
141 	_dataHeader.dirSize = _mpcFile.readUint32LE();
142 
143 	print("MPCU: Read %d entries", _dataHeader.dirSize);
144 
145 	for (uint32 fileIndex = 0; fileIndex < _dataHeader.dirSize; fileIndex++) {
146 		MPCEntry *dirEntry = new MPCEntry();
147 
148 		for (int i = 0; i < 64; i++) {
149 			dirEntry->filename[i] = _mpcFile.readByte();
150 		}
151 
152 		dirEntry->offset = _mpcFile.readUint32LE();
153 		dirEntry->length = _mpcFile.readUint32LE();
154 		dirEntry->ulength = _mpcFile.readUint32LE();
155 		dirEntry->type = (DataType)_mpcFile.readUint32LE();
156 
157 		_dir.push_back(dirEntry);
158 	}
159 
160 	return true;
161 }
162 
163 #ifdef STANDALONE_MAIN
main(int argc,char * argv[])164 int main(int argc, char *argv[]) {
165 	ExtractHDB hdb(argv[0]);
166 	return hdb.run(argc, argv);
167 }
168 #endif
169