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