1 /* gladpack.c
2 * for the packing and unpacking of .001 files
3 * 8/18/02, Zardus
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #define FILENAME_SIZE 13
9
main(int argc,char ** argv)10 int main(int argc, char **argv)
11 {
12 if (argc > 1 && !strcmp(argv[1], "p")) pack(argc, argv);
13 else if (argc > 1 && !strcmp(argv[1], "u")) unpack(argc, argv);
14 else
15 {
16 printf("Usage: gladpack (p|u) ...\n");
17 return 1;
18 }
19 }
20
pack(int argc,char ** argv)21 int pack(int argc, char **argv)
22 {
23 short numfiles = (short) argc - 3;
24 long *filelocation;
25 long *filesize;
26 long sizeoffile;
27 int i;
28 FILE *infile;
29 FILE *outfile;
30 char buffer[5000000]; //buffers: malloc is for wimps
31
32 if (numfiles < 1)
33 {
34 printf("Usage: gladpack p outfile.001 file1 [file2] [file3] ...\n");
35 return 1;
36 }
37
38 printf ("Creating packfile %s...\n", argv[2]);
39
40 filelocation = (long *) malloc(sizeof(long) * (numfiles + 1));
41 filesize = (long *) malloc(sizeof(long) * (numfiles + 1));
42
43 filelocation[0] = 8 + sizeof(short) + numfiles * (13 + sizeof(long)) + sizeof(long);
44 if (!(outfile = fopen(argv[2], "w"))) return 1;
45 fwrite("GladPack", 8, 1, outfile);
46 fwrite(&numfiles, sizeof(short), 1, outfile);
47
48 for (i = 0; i < numfiles; i++)
49 {
50 if (!(infile = fopen(argv[i + 3], "rb"))) exit(0);
51 fseek(infile, 0, SEEK_END);
52 filesize[i] = ftell(infile);
53 filelocation[i + 1] = filelocation[i] + filesize[i];
54
55 fclose(infile);
56 }
57
58 for (i = 0; i < numfiles; i++)
59 {
60 fwrite(&filelocation[i], sizeof(long), 1, outfile);
61 fwrite(argv[i+3], sizeof(char) * 13, 1, outfile);
62 }
63
64 sizeoffile = filelocation[numfiles];
65 fwrite(&sizeoffile, sizeof(long), 1, outfile);
66
67 for (i = 0; i < numfiles; i++)
68 {
69 if (!(infile = fopen(argv[i + 3], "rb"))) exit(0);
70 //printf("Packing %s ",argv[i+3]);
71 //printf("-- location %i ", filelocation[i]);
72 //printf("-- size %d\n",filesize[i]);
73
74 fread(buffer,filesize[i],1,infile);
75 fwrite(buffer, filesize[i], 1, outfile);
76
77 fclose(infile);
78 }
79
80 fclose(outfile);
81
82 return 0;
83 }
84
unpack(int argc,char ** argv)85 int unpack(int argc, char **argv)
86 {
87 long *filelocation;
88 char filename[300][13];
89 int headersize;
90 int bodysize;
91 short numfiles;
92 long sizeoffile;
93 int i;
94 char buffer[5000000];
95 FILE *infile;
96 FILE *outfile;
97
98 if (!(infile = fopen(argv[2], "r")))
99 {
100 printf("Error: can't open input file.\n");
101 return 1;
102 }
103
104 fread(buffer, 8, 1, infile);
105 if (strcmp(buffer, "GladPack")) printf("Warning: this file does not appear to be a pack file.\n");
106
107 fread(&numfiles, sizeof(short), 1, infile);
108 printf("%i files in pack.\n", numfiles);
109
110 filelocation = (long *) malloc((numfiles + 1) * sizeof(long));
111
112 for (i = 0; i < numfiles; i++)
113 {
114 fread(&filelocation[i], sizeof(long), 1, infile);
115 fread(filename[i], sizeof(char) * 13, 1, infile);
116 }
117
118 fread(&sizeoffile, sizeof(long), 1, infile);
119 printf("Pack file is %i bytes long.\n", sizeoffile);
120
121 filelocation[numfiles] = sizeoffile;
122
123 for (i = 0; i < numfiles; i++)
124 {
125 //printf("Extracting %s at location %i, size %i\n", filename[i], filelocation[i],
126 // filelocation[i + 1] - filelocation[i]);
127 fread(buffer, filelocation[i + 1] - filelocation[i], 1, infile);
128
129 outfile = fopen(filename[i], "w");
130 fwrite(buffer, filelocation[i + 1] - filelocation[i], 1, outfile);
131 fclose(outfile);
132 }
133
134 fclose(infile);
135 return 0;
136 }
137