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