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 /* Split one-big-file Macintosh game data into seperate .00x files for ScummVM */
23 
24 #include "extract_scumm_mac.h"
25 
26 #include <algorithm>
27 
ExtractScummMac(const std::string & name)28 ExtractScummMac::ExtractScummMac(const std::string &name) : Tool(name, TOOLTYPE_EXTRACTION) {
29 	ToolInput input;
30 	input.format = "*.*";
31 	_inputPaths.push_back(input);
32 
33 	_shorthelp = "Extract data files from the single data file of later LucasArts Macintosh SCUMM games.";
34 	_helptext =
35 		"\nUsage: " + getName() + " [-o <output dir> = out/] <file>\n" +
36 		_shorthelp + "\n";
37 }
38 
inspectInput(const Common::Filename & filename)39 InspectionMatch ExtractScummMac::inspectInput(const Common::Filename &filename) {
40 	std::string name = filename.getFullName();
41 	std::transform(name.begin(), name.end(), name.begin(), tolower);
42 	std::string::size_type pos = name.find(" data");
43 	if (pos == name.length() - 5) // True if the file name ends with " Data"
44 		return IMATCH_PERFECT;
45 	return IMATCH_AWFUL;
46 }
47 
execute()48 void ExtractScummMac::execute() {
49 	Common::Filename inPath(_inputPaths[0].path);
50 	Common::Filename outPath(_outputPath);
51 
52 	if (outPath.empty())
53 		outPath.setFullPath("./");
54 
55 	Common::File ifp(inPath, "rb");
56 
57 	// Get the length of the data file to use for consistency checks
58 	uint32 dataFileLength = ifp.size();
59 
60 	// Read offset and length to the file records
61 	uint32 fileRecordOffset = ifp.readUint32BE();
62 	uint32 fileRecordLength = ifp.readUint32BE();
63 
64 	// Do a quick check to make sure the offset and length are good
65 	if (fileRecordOffset + fileRecordLength > dataFileLength)
66 		error("File records out of bounds");
67 
68 	// Do a little consistancy check on fileRecordLength
69 	if (fileRecordLength % 0x28)
70 		error("File record length not multiple of 40");
71 
72 	// Extract the files
73 	for (uint32 i = 0; i < fileRecordLength; i += 0x28) {
74 		// read a file record
75 		ifp.seek(fileRecordOffset + i, SEEK_SET);
76 
77 		uint32 fileOffset = ifp.readUint32BE();
78 		uint32 fileLength = ifp.readUint32BE();
79 
80 		char fileName[0x21];
81 		ifp.read_throwsOnError(fileName, 0x20);
82 		fileName[0x20] = 0;
83 
84 		if (!fileName[0])
85 			error("File has no name");
86 
87 		print("Extracting %s...", fileName);
88 
89 		// Consistency check. make sure the file data is in the file
90 		if (fileOffset + fileLength > dataFileLength)
91 			error("File out of bounds");
92 
93 		// Write a file
94 		ifp.seek(fileOffset, SEEK_SET);
95 
96 		outPath.setFullName(fileName);
97 		Common::File ofp(outPath, "wb");
98 
99 		byte *buf = new byte[fileLength];
100 		ifp.read_throwsOnError(buf, fileLength);
101 		ofp.write(buf, fileLength);
102 		delete[] buf;
103 	}
104 }
105 
106 #ifdef STANDALONE_MAIN
main(int argc,char * argv[])107 int main(int argc, char *argv[]) {
108 	return export_main(extract_scumm_mac)(argc, argv);
109 }
110 #endif
111