]> icculus.org git repositories - btb/d2x.git/blob - utilities/mvlextract.c
check return values of fread
[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], "rb");
43         stat(argv[1], &statbuf);
44         printf("%i\n", (int)statbuf.st_size);
45         buf = (char *)malloc(4);
46         if ( fread(buf, 4, 1, mvlfile) != 1 ||
47                  fread(&nfiles, 4, 1, mvlfile) != 1 ) {
48                 printf("Error reading %s\n", argv[1]);
49                 fclose(mvlfile);
50                 exit(EXIT_FAILURE);
51         }
52         printf("%d files\n", nfiles);
53         if (nfiles > MAX_FILES) { // must be a bigendian mvl
54                 fprintf(stderr, "warning: nfiles>%d, trying reverse byte order...",
55                                 MAX_FILES);
56                 bigendian = 1;
57         }
58         if (bigendian)
59                 nfiles = SWAPINT(nfiles);
60         printf("Extracting from: %s\n", argv[1]);
61         free(buf);
62         for (i = 0; i < nfiles; i++) {
63                 if ( fread(filename[i], 13, 1, mvlfile) != 1 ||
64                          fread(&len[i], 4, 1, mvlfile) != 1 ) {
65                         printf("Error reading %s\n", argv[1]);
66                         fclose(mvlfile);
67                         exit(EXIT_FAILURE);
68                 }
69                 if (bigendian)
70                         len[i] = SWAPINT(len[i]);
71                 if (argc == 2 || !strcmp(argv[2], filename[i]))
72                         printf("Filename: %s \tLength: %i\n", filename[i], len[i]);
73         }
74
75         if (!v) {
76                 for (i = 0; i < nfiles; i++) {
77                         if (argc > 2 && strcmp(argv[2], filename[i]))
78                                 fseek(mvlfile, len[i], SEEK_CUR);
79                         else {
80                                 if (ftell(mvlfile) > statbuf.st_size) {
81                                         printf("Error, end of file\n");
82                                         exit(1);
83                                 }
84                                 buf = (char *)malloc(len[i]);
85                                 if (buf == NULL) {
86                                         printf("Unable to allocate memory\n");
87                                 } else {
88                                         if ( fread(buf, len[i], 1, mvlfile) != 1 ) {
89                                                 printf("Error reading %s\n", argv[1]);
90                                                 fclose(mvlfile);
91                                                 exit(EXIT_FAILURE);
92                                         }
93                                         writefile = fopen(filename[i], "wb");
94                                         fwrite(buf, len[i], 1, writefile);
95                                         fclose(writefile);
96                                         free(buf);
97                                 }
98                         }
99                 }
100         }
101         fclose(mvlfile);
102
103         return 0;
104 }