]> icculus.org git repositories - crow/jumpnbump.git/blob - modify/jnbunpack.c
Finished gobpack and included it into the build process.
[crow/jumpnbump.git] / modify / jnbunpack.c
1 /*
2  * unpack.c
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2001 "timecop" <timecop@japan.co.jp>
6  *
7  * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
8  *
9  * Portions of this code are from the MPEG software simulation group
10  * idct implementation. This code will be replaced with a new
11  * implementation soon.
12  *
13  * This file is part of Jump'n'Bump.
14  *
15  * Jump'n'Bump is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * Jump'n'Bump is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #ifndef _MSC_VER
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40
41 typedef struct {
42     char filename[12];
43     unsigned int offset;
44     unsigned int size;
45 } DirEntry;
46
47 #ifndef O_BINARY
48 #define O_BINARY 0
49 #endif
50
51 int main(int argc, char **argv)
52 {
53     int fd;
54     DirEntry *datafile;
55     int num_entries, i;
56
57     if (argc < 2) {
58         printf("dumbass, specify filename to unpack\n");
59         exit(1);
60     }
61
62     fd = open(argv[1], O_RDONLY | O_BINARY);
63     if (fd == -1) {
64         perror("open datafile");
65         exit(1);
66     }
67     /* get number of entries */
68     read(fd, &num_entries, 4);
69
70     printf("%d entries in datafile\n", num_entries);
71
72     datafile = calloc(num_entries, sizeof(DirEntry));
73     read(fd, datafile, num_entries * sizeof(DirEntry));
74     printf("Directory Listing:\n");
75     for (i = 0; i < num_entries; i++) {
76         char filename[14];
77         memset(filename, 0, sizeof(filename));
78         strncpy(filename, datafile[i].filename, 12);
79         printf("%02d:\t%s (%u bytes)\n", i, filename,
80                 datafile[i].size);
81     }
82
83     for (i = 0; i < num_entries; i++) {
84         int outfd;
85         char filename[14];
86         char *buf;
87         memset(filename, 0, sizeof(filename));
88         strncpy(filename, datafile[i].filename, 12);
89         printf("Extracting %s ", filename);
90
91         outfd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
92         if (!outfd) {
93             perror("cant open file");
94             exit(1);
95         }
96         lseek(fd, datafile[i].offset, SEEK_SET);
97         buf = calloc(1, datafile[i].size + 16);
98         read(fd, buf, datafile[i].size);
99         write(outfd, buf, datafile[i].size);
100         close(outfd);
101         free(buf);
102         printf("OK\n");
103     }
104     close(fd);
105     return 0;
106 }