]> icculus.org git repositories - crow/jumpnbump.git/blob - modify/unpack.c
Fixed pack and unpack.
[crow/jumpnbump.git] / modify / unpack.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #ifndef _MSC_VER
7 #include <unistd.h>
8 #else
9 #include <io.h>
10 typedef unsigned int u_int32_t;
11 #endif
12
13 typedef struct {
14     char filename[12];
15     u_int32_t offset;
16     u_int32_t size;
17 } DirEntry;
18
19 int main(int argc, char **argv)
20 {
21     int fd;
22     DirEntry *datafile;
23     int num_entries, i;
24
25     if (argc < 2) {
26         printf("dumbass, specify filename to unpack\n");
27         exit(1);
28     }
29
30     fd = open(argv[1], O_RDONLY | O_BINARY);
31     if (fd == -1) {
32         perror("open datafile");
33         exit(1);
34     }
35     /* get number of entries */
36     read(fd, &num_entries, 4);
37
38     printf("%d entries in datafile\n", num_entries);
39
40     datafile = calloc(num_entries, sizeof(DirEntry));
41     read(fd, datafile, num_entries * sizeof(DirEntry));
42     printf("Directory Listing:\n");
43     for (i = 0; i < num_entries; i++) {
44         char filename[14];
45         memset(filename, 0, sizeof(filename));
46         strncpy(filename, datafile[i].filename, 12);
47         printf("%02d:\t%s (%u bytes)\n", i, filename,
48                 datafile[i].size);
49     }
50
51     for (i = 0; i < num_entries; i++) {
52         int outfd;
53         char filename[14];
54         char *buf;
55         memset(filename, 0, sizeof(filename));
56         strncpy(filename, datafile[i].filename, 12);
57         printf("Extracting %s ", filename);
58
59         outfd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
60         if (!outfd) {
61             perror("cant open file");
62             exit(1);
63         }
64         lseek(fd, datafile[i].offset, SEEK_SET);
65         buf = calloc(1, datafile[i].size + 16);
66         read(fd, buf, datafile[i].size);
67         write(outfd, buf, datafile[i].size);
68         close(outfd);
69         free(buf);
70         printf("OK\n");
71     }
72     close(fd);
73     return 0;
74 }