This commit was manufactured by cvs2svn to create branch
[btb/d2x.git] / utilities / mvlextract.c
1 /*
2  * Written 1999 Jan 29 by Josh Cogliati
3  * Modified for mvl by Bradley Bell, 2002, 2003
4  * This program is licensed under the terms of the GPL, version 2 or later
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13
14 #define SWAPINT(x)   (((x)<<24) | (((unsigned int)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
15
16 #define MAX_FILES 256
17
18 int
19 main(int argc, char *argv[])
20 {
21         FILE *mvlfile, *writefile;
22         int i, nfiles, len[MAX_FILES];
23         char filename[MAX_FILES][13];
24         char *buf;
25         struct stat statbuf;
26         int v = 0;
27         int bigendian = 0;
28
29         if (argc > 1 && !strcmp(argv[1], "v")) {
30                 v = 1;
31                 argc--;
32                 argv++;
33         }
34
35         if (argc < 2) {
36                 printf("Usage: mvlextract [v] mvlfile [filename]\n"
37                        "extracts all the files in mvlfile into the current directory\n"
38                            "Options:\n"
39                            "  v    View files, don't extract\n");
40                 exit(0);
41         }
42         mvlfile = fopen(argv[1], "r");
43         stat(argv[1], &statbuf);
44         printf("%i\n", (int)statbuf.st_size);
45         buf = (char *)malloc(4);
46         fread(buf, 4, 1, mvlfile);
47         fread(&nfiles, 4, 1, mvlfile);
48         printf("%d files\n", nfiles);
49         if (nfiles > MAX_FILES) { // must be a bigendian mvl
50                 fprintf(stderr, "warning: nfiles>%d, trying reverse byte order...",
51                                 MAX_FILES);
52                 bigendian = 1;
53         }
54         if (bigendian)
55                 nfiles = SWAPINT(nfiles);
56         printf("Extracting from: %s\n", argv[1]);
57         free(buf);
58         for (i = 0; i < nfiles; i++) {
59                 fread(filename[i], 13, 1, mvlfile);
60                 fread(&len[i], 4, 1, mvlfile);
61                 if (bigendian)
62                         len[i] = SWAPINT(len[i]);
63                 if (argc == 2 || !strcmp(argv[2], filename[i]))
64                         printf("Filename: %s \tLength: %i\n", filename[i], len[i]);
65         }
66
67         if (!v) {
68                 for (i = 0; i < nfiles; i++) {
69                         if (argc > 2 && strcmp(argv[2], filename[i]))
70                                 fseek(mvlfile, len[i], SEEK_CUR);
71                         else {
72                                 if (ftell(mvlfile) > statbuf.st_size) {
73                                         printf("Error, end of file\n");
74                                         exit(1);
75                                 }
76                                 buf = (char *)malloc(len[i]);
77                                 if (buf == NULL) {
78                                         printf("Unable to allocate memory\n");
79                                 } else {
80                                         fread(buf, len[i], 1, mvlfile);
81                                         writefile = fopen(filename[i], "w");
82                                         fwrite(buf, len[i], 1, writefile);
83                                         fclose(writefile);
84                                         free(buf);
85                                 }
86                         }
87                 }
88         }
89         fclose(mvlfile);
90
91         return 0;
92 }