aboutsummaryrefslogtreecommitdiff
path: root/tools/create_kyradat/pak.cpp
blob: f3446df291949cdc69e9d66380c8f4982979d938 (plain)
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
/* Scumm Tools
 * Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "pak.h"

bool PAKFile::loadFile(const char *file, const bool isAmiga) {
	_isAmiga = isAmiga;
	if (!file)
		return true;

	delete _fileList;
	_fileList = 0;

	FILE *pakfile = fopen(file, "rb");
	if (!pakfile)
		return false;

	uint32 filesize = fileSize(pakfile);

	// TODO: get rid of temp. buffer
	uint8 *buffer = new uint8[filesize];
	assert(buffer);

	fread(buffer, filesize, 1, pakfile);

	fclose(pakfile);

	const char *currentName = 0;

	uint32 startoffset = _isAmiga ? READ_BE_UINT32(buffer) : READ_LE_UINT32(buffer);
	uint32 endoffset = 0;
	uint8* position = buffer + 4;

	while (true) {
		uint32 strlgt = strlen((const char*)position);
		currentName = (const char*)position;

		if (!(*currentName))
			break;

		position += strlgt + 1;
		endoffset = _isAmiga ? READ_BE_UINT32(position) : READ_LE_UINT32(position);
		if (endoffset > filesize) {
			endoffset = filesize;
		} else if (endoffset == 0) {
			endoffset = filesize;
		}
		position += 4;

		uint8 *data = new uint8[endoffset - startoffset];
		assert(data);
		memcpy(data, buffer + startoffset, endoffset - startoffset);
		addFile(currentName, data, endoffset - startoffset);
		data = 0;

		if (endoffset == filesize)
			break;

		startoffset = endoffset;
	}

	delete [] buffer;
	return true;
}

bool PAKFile::saveFile(const char *file) {
	if (!_fileList)
		return true;

	FILE *f = fopen(file, "wb");
	if (!f) {
		error("couldn't open file '%s' for writing", file);
		return false;
	}

	// TODO: implement error handling
	uint32 startAddr = _fileList->getTableSize()+5+4;
	static const char *zeroName = "\0\0\0\0\0";

	uint32 curAddr = startAddr;
	for (FileList *cur = _fileList; cur; cur = cur->next) {
		if (_isAmiga)
			writeUint32BE(f, curAddr);
		else
			writeUint32LE(f, curAddr);
		fwrite(cur->filename, 1, strlen(cur->filename) + 1, f);
		curAddr += cur->size;
	}
	if (_isAmiga)
		writeUint32BE(f, curAddr);
	else
		writeUint32LE(f, curAddr);
	fwrite(zeroName, 1, 5, f);

	for (FileList *cur = _fileList; cur; cur = cur->next)
		fwrite(cur->data, 1, cur->size, f);

	fclose(f);
	return true;
}

void PAKFile::drawFileList() {
	FileList *cur = _fileList;
	while (cur) {
		printf("Filename: '%s' size: %d\n", cur->filename, cur->size);
		cur = cur->next;
	}
}

bool PAKFile::outputAllFiles() {
	FileList *cur = _fileList;
	while (cur) {
		FILE *file = fopen(cur->filename, "wb");
		if (!file) {
			error("couldn't open file '%s' for writing", cur->filename);
			return false;
		}
		printf("Exracting file '%s'...", cur->filename);
		if (fwrite(cur->data, 1, cur->size, file) == cur->size) {
			printf("OK\n");
		} else {
			printf("FAILED\n");
			return false;
		}
		fclose(file);
		cur = cur->next;
	}
	return true;
}

bool PAKFile::outputFileAs(const char *f, const char *fn) {
	FileList *cur = (_fileList != 0) ? _fileList->findEntry(f) : 0;

	if (!cur) {
		error("file '%s' not found");
		return false;
	}

	FILE *file = fopen(fn, "wb");
	if (!file) {
		error("couldn't open file '%s' in write mode", fn);
		return false;
	}
	printf("Exracting file '%s' to file '%s'...", cur->filename, fn);
	if (fwrite(cur->data, 1, cur->size, file) == cur->size) {
		printf("OK\n");
	} else {
		printf("FAILED\n");
		return false;
	}
	fclose(file);
	return true;
}

const uint8 *PAKFile::getFileData(const char *file, uint32 *size) {
	FileList *cur = (_fileList != 0) ? _fileList->findEntry(file) : 0;

	if (!cur)
		return 0;

	if (size)
		*size = cur->size;
	return cur->data;
}

bool PAKFile::addFile(const char *name, const char *file) {
	if (_fileList && _fileList->findEntry(name)) {
		error("entry '%s' already exists");
		return false;
	}

	FILE *f = fopen(file, "rb");
	if (!f) {
		error("couldn't open file '%s'", file);
		return false;
	}

	uint32 filesize = fileSize(f);
	uint8 *data = new uint8[filesize];
	assert(data);
	if (fread(data, 1, filesize, f) != filesize) {
		error("couldn't read from file '%s'", file);
		return false;
	}
	fclose(f);
	return addFile(name, data, filesize);
}

bool PAKFile::addFile(const char *name, uint8 *data, uint32 size) {
	if (_fileList && _fileList->findEntry(name)) {
		error("entry '%s' already exists");
		return false;
	}

	FileList *newEntry = new FileList;
	assert(newEntry);
	newEntry->filename = new char[strlen(name)+1];
	assert(newEntry->filename);
	strncpy(newEntry->filename, name, strlen(name)+1);
	newEntry->size = size;
	newEntry->data = data;

	if (_fileList) {
		_fileList->addEntry(newEntry);
	} else {
		_fileList = newEntry;
	}
	return true;
}

bool PAKFile::removeFile(const char *name) {
	for (FileList *cur = _fileList, *last = 0; cur; last = cur, cur = cur->next) {
		if (scumm_stricmp(cur->filename, name) == 0) {
			FileList *next = cur->next;
			cur->next = 0;
			delete cur;
			if (last)
				last->next = next;
			else
				_fileList = next;
			return true;
		}
	}
	return false;
}