]> icculus.org git repositories - btb/d2x.git/blob - utilities/mvlextract.c
eliminate warning
[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) | (((uint)(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                 bigendian = 1;
51         if (bigendian)
52                 nfiles = SWAPINT(nfiles);
53         printf("Extracting from: %s\n", argv[1]);
54         free(buf);
55         for (i = 0; i < nfiles; i++) {
56                 fread(filename[i], 13, 1, mvlfile);
57                 fread(&len[i], 4, 1, mvlfile);
58                 if (bigendian)
59                         len[i] = SWAPINT(len[i]);
60                 if (argc == 2 || !strcmp(argv[2], filename[i]))
61                         printf("Filename: %s \tLength: %i\n", filename[i], len[i]);
62         }
63
64         if (!v) {
65                 for (i = 0; i < nfiles; i++) {
66                         if (argc > 2 && strcmp(argv[2], filename[i]))
67                                 fseek(mvlfile, len[i], SEEK_CUR);
68                         else {
69                                 if (ftell(mvlfile) > statbuf.st_size) {
70                                         printf("Error, end of file\n");
71                                         exit(1);
72                                 }
73                                 buf = (char *)malloc(len[i]);
74                                 if (buf == NULL) {
75                                         printf("Unable to allocate memory\n");
76                                 } else {
77                                         fread(buf, len[i], 1, mvlfile);
78                                         writefile = fopen(filename[i], "w");
79                                         fwrite(buf, len[i], 1, writefile);
80                                         fclose(writefile);
81                                         free(buf);
82                                 }
83                         }
84                 }
85         }
86         fclose(mvlfile);
87
88         return 0;
89 }