Debian Codegraph @

emscripten/extract.c

raw
1 /**
2 * Efficient tar+bzip2 extractor for in-game use
3
4 * Copyright (C) 2004 Andrew Reading
5 * Copyright (C) 2007, 2008, 2014, 2018 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #else
27 #define _(s) s
28 #endif
29
30 #include <bzlib.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <libgen.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <errno.h>
38 #include <assert.h>
39
40 struct TarHeader {
41 char Name[100+1]; // 100
42 char Mode[8+1]; // 108
43 char Uid[8+1]; // 116
44 char Gid[8+1]; // 124
45 char Size[12+1]; // 136
46 char Mtime[12+1]; // 148
47 char Chksum[8+1]; // 156
48 char Linkflag[1+1]; // 157
49 char Linkname[100+1]; // 257
50 char Magic[8+1]; // 265
51 char Uname[32+1]; // 297
52 char Gname[32+1]; // 329
53 char Devmajor[8+1]; // 337
54 char Devminor[8+1]; // 345
55 char Padding[167+1]; // 512
56 };
57
58
59 #define TAR_HEADER_SIZE 512
60
61 int mkdir_rec(char* path) {
62 int ret;
63 mode_t mode = 00755;
64 ret = mkdir(path, mode);
65 if (ret == 0 || errno == EEXIST) {
66 return 0;
67 } else {
68 char* dir = strdup(path);
69 dirname(dir);
70 ret = mkdir_rec(dir);
71 free(dir);
72 if (ret < 0) {
73 return ret;
74 }
75 if ((ret = mkdir(path, mode)) < 0) {
76 perror("mkdir");
77 return ret;
78 }
79 return 0;
80 }
81 }
82
83 int DirExists(char* path) {
84 struct stat st;
85 return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
86 }
87
88 void print_bzerror(BZFILE* bzin, int bzerror) {
89 printf("%s\n", BZ2_bzerror(bzin, &bzerror));
90 if (bzerror == BZ_OK || bzerror == BZ_STREAM_END) {
91 // Success
92 } else if (bzerror == BZ_PARAM_ERROR) {
93 printf(_("Invalid .dmod file specified."));
94 } else if (bzerror == BZ_SEQUENCE_ERROR) {
95 printf(_("Critical program function error: opened for write."));
96 } else if (bzerror == BZ_IO_ERROR) {
97 printf(_("Could not read .dmod file.") );
98 } else if (bzerror == BZ_UNEXPECTED_EOF) {
99 printf(_("Incomplete .dmod file. Please download it again.") );
100 } else if (bzerror == BZ_DATA_ERROR) {
101 printf(_("The .dmod file is corrupted. Please download it again."));
102 } else if (bzerror == BZ_DATA_ERROR_MAGIC) {
103 printf(_("The file is not a valid .dmod file."));
104 } else if (bzerror == BZ_MEM_ERROR) {
105 printf(_("Out of memory error."));
106 } else {
107 printf(_("An unhandled error occurred."));
108 }
109 printf("\n");
110 }
111
112
113
114 /**
115 * Read all tar headers from a flux, so it can work if reading file
116 * with BZip2 directly (only the 'read' operation is available).
117 */
118 int TarFillHeaderFromStream(BZFILE* bzin, struct TarHeader* tarHeader)
119 {
120 memset(tarHeader, 0, sizeof(*tarHeader));
121
122 // Read the data. Don't load the whole header to a struct, you
123 // never know what the memory alignment will be, especially in
124 // these 32->64bits days
125 int bzerror;
126 BZ2_bzRead(&bzerror, bzin, tarHeader->Name, 100); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
127 BZ2_bzRead(&bzerror, bzin, tarHeader->Mode, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
128 BZ2_bzRead(&bzerror, bzin, tarHeader->Uid, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
129 BZ2_bzRead(&bzerror, bzin, tarHeader->Gid, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
130 BZ2_bzRead(&bzerror, bzin, tarHeader->Size, 12); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
131 BZ2_bzRead(&bzerror, bzin, tarHeader->Mtime, 12); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
132 BZ2_bzRead(&bzerror, bzin, tarHeader->Chksum, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
133 BZ2_bzRead(&bzerror, bzin, tarHeader->Linkflag, 1); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
134 BZ2_bzRead(&bzerror, bzin, tarHeader->Linkname, 100); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
135 BZ2_bzRead(&bzerror, bzin, tarHeader->Magic, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
136 BZ2_bzRead(&bzerror, bzin, tarHeader->Uname, 32); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
137 BZ2_bzRead(&bzerror, bzin, tarHeader->Gname, 32); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
138 BZ2_bzRead(&bzerror, bzin, tarHeader->Devmajor, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
139 BZ2_bzRead(&bzerror, bzin, tarHeader->Devminor, 8); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
140 BZ2_bzRead(&bzerror, bzin, tarHeader->Padding, 167); if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
141
142 return bzerror;
143 }
144
145 unsigned int TarComputeChecksum(struct TarHeader* tarHeader) {
146 unsigned int sum = 0;
147 char blankChecksum[8+1] = " ";
148 for (int i = 0; i < 100; i++) { sum += tarHeader->Name[i]; }
149 for (int i = 0; i < 8; i++) { sum += tarHeader->Mode[i]; }
150 for (int i = 0; i < 8; i++) { sum += tarHeader->Uid[i]; }
151 for (int i = 0; i < 8; i++) { sum += tarHeader->Gid[i]; }
152 for (int i = 0; i < 12; i++) { sum += tarHeader->Size[i]; }
153 for (int i = 0; i < 12; i++) { sum += tarHeader->Mtime[i]; }
154 //for (int i = 0; i < 8; i++) { sum += tarHeader->Chksum[i]; }
155 for (int i = 0; i < 8; i++) { sum += blankChecksum[i]; }
156 for (int i = 0; i < 1; i++) { sum += tarHeader->Linkflag[i]; }
157 for (int i = 0; i < 100; i++) { sum += tarHeader->Linkname[i]; }
158 for (int i = 0; i < 8; i++) { sum += tarHeader->Magic[i]; }
159 for (int i = 0; i < 32; i++) { sum += tarHeader->Uname[i]; }
160 for (int i = 0; i < 32; i++) { sum += tarHeader->Gname[i]; }
161 for (int i = 0; i < 8; i++) { sum += tarHeader->Devmajor[i]; }
162 for (int i = 0; i < 8; i++) { sum += tarHeader->Devminor[i]; }
163 for (int i = 0; i < 167; i++) { sum += tarHeader->Padding[i]; }
164 return sum;
165 }
166
167
168 void GetFirstDir(const char* path, char* dmodDir) {
169 char* firstDir = "";
170 char* previousDir = "";
171 char* checkPath = malloc(strlen(path)+strlen("dummy")+1);
172 strcpy(checkPath, path);
173 // tokenizer never returns empty strings + distinguish dir// and a/$
174 char last = path[strlen(path)-1];
175 if (last == '\\' || last == '/')
176 strcat(checkPath, "dummy");
177
178 char *str, *curDir, *saveptr;
179 for (str = checkPath; ; str = NULL) {
180 curDir = strtok_r(str, "/\\", &saveptr);
181 if (curDir == NULL)
182 break;
183 if (strcmp(curDir, ".") == 0)
184 continue;
185 if (strcmp(previousDir, "") != 0) {
186 firstDir = previousDir;
187 break;
188 }
189 previousDir = curDir;
190 }
191
192 strcpy(dmodDir, firstDir);
193 free(checkPath);
194 }
195
196 int TarIsPathInsecure(char* path, char* prefix) {
197 if (path[0] == '/' || path[0] == '\\')
198 return 1;
199 {
200 char *str, *token, *saveptr;
201 str = strdup(path);
202 token = strtok_r(str, "/\\", &saveptr);
203 if (strcmp(token, prefix) != 0) {
204 free(str);
205 return 1;
206 }
207 free(str);
208 }
209 {
210 char *str, *token, *saveptr;
211 for (str = path; ; str = NULL) {
212 token = strtok_r(str, "/\\", &saveptr);
213 if (token == NULL)
214 break;
215 if (strcmp(token, "..") == 0)
216 return 1;
217 }
218 }
219 return 0;
220 }
221
222 int DmodExtract(FILE* dmodFile, char* destDir, char** extractedDmodDir) {
223 // Get the file size
224 unsigned long dmodFileCompressedSize;
225 if (fseek(dmodFile, 0, SEEK_END) < 0) perror("fseek");
226 dmodFileCompressedSize = ftell(dmodFile);
227 if (fseek(dmodFile, 0, SEEK_SET) < 0) perror("fseek");
228
229 int bzerror = BZ_OK;
230 BZFILE* bzin = BZ2_bzReadOpen(&bzerror, dmodFile, 0, 0, 0, 0);
231 if (bzerror != BZ_OK) {
232 print_bzerror(bzin, bzerror);
233 printf(_("Error: Invalid .dmod file selected!"));
234 printf("\n");
235 return -1;
236 }
237
238 struct TarHeader tarHeader;
239 unsigned int total_read = 0;
240 int lastPercent = 0;
241 char dmodDir[100+1] = {};
242
243 // Keep getting chunks of the file
244 while (1) {
245 unsigned char* buf;
246 int nbReadUncompressed;
247
248 int ret = TarFillHeaderFromStream(bzin, &tarHeader);
249 total_read += 512;
250 if (ret < 0) {
251 break;
252 }
253
254 // Define dmodDir which needs to be consistent in all the archive
255 if (strcmp(dmodDir, "") == 0) {
256 GetFirstDir(tarHeader.Name, dmodDir);
257 if (strcmp(dmodDir, "") == 0 || strcmp(dmodDir, "..") == 0)
258 {
259 printf(_("Error: empty D-Mod directory. Stopping.\n"));
260 return 1;
261 }
262 char* dmodFullPath = malloc(strlen(destDir)+1+strlen(dmodDir)+1);
263 sprintf(dmodFullPath, "%s/%s", destDir, dmodDir);
264 if (DirExists(dmodFullPath)) {
265 printf(_("Error: directory '%s' already exists. Stopping.\n"), dmodDir);
266 free(dmodFullPath);
267 return 1;
268 }
269 free(dmodFullPath);
270 printf("D-Mod dir: %s\n", dmodDir);
271 }
272
273 // Bug fix for phantom files that aren't really files...
274 // This is due to a bug in DFArc1.5 with empty files
275 if (strcmp(tarHeader.Name, "\xFF") == 0) {
276 continue;
277 }
278
279 // Tar files (except for a design bug in DFArc1.5) have a
280 // multitude of NULL characters at the end to round up to the
281 // 10K mark. If the header is blank, we don't have any more
282 // data.
283 if (strcmp(tarHeader.Name, "") == 0) {
284 break;
285 }
286
287 // TODO: ISO-8869-1 -> UTF-8?
288
289 // Size
290 unsigned long fileSize = 0;
291 sscanf((const char*)&tarHeader.Size, "%lo", &fileSize);
292
293 // Extract
294 char last = tarHeader.Name[strlen(tarHeader.Name)-1];
295 if (last == '\\' || last == '/') {
296 // directory, skip and let mkdir_rec do its job
297 } else {
298 unsigned int checksum = 0;
299 unsigned int expected = TarComputeChecksum(&tarHeader);
300 sscanf((const char*)&tarHeader.Chksum, "%o", &checksum);
301 if (checksum != expected) {
302 printf("Incorrect checksum, but proceeding anyway: found 0%o, expected 0%o\n", checksum, expected);
303 }
304
305 char* checkName = strdup(tarHeader.Name);
306 if (TarIsPathInsecure(checkName, dmodDir)) {
307 printf("Insecure path, stopping: %s\n", tarHeader.Name);
308 free(checkName);
309 return 1;
310 }
311 free(checkName);
312
313 char* destFullPath = malloc(strlen(destDir)+1+strlen(tarHeader.Name)+1);
314 sprintf(destFullPath, "%s/%s", destDir, tarHeader.Name);
315 //printf("* Extracting to: %s\n", destFullPath);
316 char* dir = strdup(destFullPath);
317 dirname(dir);
318 int ret = mkdir_rec(dir);
319 if (ret < 0) printf("Error creating directory %s\n", dir);
320 free(dir);
321 if (ret < 0) {
322 free(destFullPath);
323 break;
324 }
325
326 {
327 // bzip2 -9 -> 900kiB blocks - randomly using 4MiB buf, we
328 // might get better precision with low-level BZ2 API
329 char buf[4*1024*1024];
330 FILE* out = fopen(destFullPath, "wb");
331 if (out == NULL) {
332 perror("fopen");
333 break;
334 }
335 unsigned long nbRemaining = fileSize;
336 while (nbRemaining > 0) {
337 nbReadUncompressed = (nbRemaining > sizeof(buf)) ? sizeof(buf) : nbRemaining;
338 nbReadUncompressed = BZ2_bzRead(&bzerror, bzin, buf, nbReadUncompressed);
339 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) { print_bzerror(bzin, bzerror); return 1; };
340 fwrite(buf, nbReadUncompressed, 1, out);
341 nbRemaining -= nbReadUncompressed;
342
343 {
344 unsigned long pos = ftell(dmodFile);
345 double percent = 100.0 * pos / dmodFileCompressedSize;
346 if ((int)percent != lastPercent)
347 printf("progress: %lu %lu %.02f%%\n", pos, dmodFileCompressedSize, percent);
348 lastPercent = (int)percent;
349 }
350 }
351
352 if (bzerror == BZ_STREAM_END) {
353 // old .mod without end-of-archive record nor padding, no error output
354 break;
355 }
356 fclose(out);
357 }
358
359 free(destFullPath);
360 total_read += fileSize;
361 }
362
363 // Move to the beginning of the next header
364 char padding[512];
365 int padding_size = (512 - (total_read % 512)) % 512;
366 BZ2_bzRead(&bzerror, bzin, padding, padding_size);
367 if (bzerror != BZ_OK) { print_bzerror(bzin, bzerror); return 1; }
368 total_read += padding_size;
369 }
370
371 BZ2_bzReadClose(&bzerror, bzin);
372
373 if (extractedDmodDir != NULL) {
374 *extractedDmodDir = strdup(dmodDir);
375 }
376 return 0;
377 }
378
379 int emDmodExtract(char* filename, char* destdir, char** extractedDmodDir) {
380 // printf("emDmodExtract(%s, %s)\n", filename, destdir);
381 FILE* in = fopen(filename, "rb");
382 if (in == NULL) {
383 perror("fopen");
384 printf("Cannot open %s\n", filename);
385 return 1;
386 }
387 return DmodExtract(in, destdir, extractedDmodDir);
388 }
389
390 #ifdef TEST
391 int main(int argc, char* argv[]) {
392 {
393 char buf[100+1];
394 assert((strcpy(buf, "../"), TarIsPathInsecure(buf, "..") == 1));
395 assert((strcpy(buf, "/../"), TarIsPathInsecure(buf, "") == 1));
396 assert((strcpy(buf, "/../"), TarIsPathInsecure(buf, "..") == 1));
397 assert((strcpy(buf, ".."), TarIsPathInsecure(buf, "..") == 1));
398 assert((strcpy(buf, "/.."), TarIsPathInsecure(buf, "") == 1));
399 assert((strcpy(buf, "/.."), TarIsPathInsecure(buf, "..") == 1));
400 assert((strcpy(buf, "path/.."), TarIsPathInsecure(buf, "path") == 1));
401 assert((strcpy(buf, "path/subpath/.."), TarIsPathInsecure(buf, "path") == 1));
402 assert((strcpy(buf, "path/../subpath"), TarIsPathInsecure(buf, "path") == 1));
403 assert((strcpy(buf, "../path/subpath"), TarIsPathInsecure(buf, "..") == 1));
404 assert((strcpy(buf, "../path/subpath"), TarIsPathInsecure(buf, "path") == 1));
405 assert((strcpy(buf, "/path/subpath"), TarIsPathInsecure(buf, "") == 1));
406 assert((strcpy(buf, "/path/subpath"), TarIsPathInsecure(buf, "path") == 1));
407 assert((strcpy(buf, "\\path/subpath"), TarIsPathInsecure(buf, "") == 1));
408 assert((strcpy(buf, "\\path/subpath"), TarIsPathInsecure(buf, "path") == 1));
409 assert((strcpy(buf, "path\\..\\subpath"), TarIsPathInsecure(buf, "path") == 1));
410 assert((strcpy(buf, "path"), TarIsPathInsecure(buf, "path") == 0));
411 assert((strcpy(buf, "path"), TarIsPathInsecure(buf, "path") == 0));
412 assert((strcpy(buf, "path/"), TarIsPathInsecure(buf, "path") == 0));
413 assert((strcpy(buf, "path\\"), TarIsPathInsecure(buf, "path") == 0));
414 assert((strcpy(buf, "path/subpath"), TarIsPathInsecure(buf, "path") == 0));
415 assert((strcpy(buf, "path\\subpath"), TarIsPathInsecure(buf, "path") == 0));
416 assert((strcpy(buf, "path2\\subpath"), TarIsPathInsecure(buf, "path") == 1));
417 assert((strcpy(buf, "dink.exe"), TarIsPathInsecure(buf, "path") == 1));
418 assert((strcpy(buf, "dink.exe"), TarIsPathInsecure(buf, "path") == 1));
419 }
420 return emDmodExtract(argv[1], "/tmp/testdmod", NULL);
421 }
422 #endif