-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathvfs_tar.c
482 lines (402 loc) · 11 KB
/
vfs_tar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: vfs_tar.c,v 1.8 2007-10-13 16:02:51 dkure Exp $
*
*/
#include "quakedef.h"
#include "hash.h"
#include "common.h"
#include "hash.h"
#include "fs.h"
#include "vfs.h"
#include "vfs_tar.h"
//=============================================================================
// T A R V F S
//=============================================================================
// These functions give a VFS layer to operations for a file tar file
typedef struct tarfile_s
{
char filename[MAX_QPATH];
hashtable_t hash;
vfsfile_t *raw;
int numfiles;
packfile_t *files;
unsigned long filepos;
int references;
} tarfile_t;
typedef struct
{
vfsfile_t funcs; // <= must be at top/begining of struct
tarfile_t *parent;
unsigned long startpos;
unsigned long length;
unsigned long currentpos;
} vfstarfile_t;
// convert octal digits to int
// on error return -1
static int getoct (char *p,int width)
{
int result = 0;
char c;
while (width--)
{
c = *p++;
if (c == 0)
break;
if (c == ' ')
continue;
if (c < '0' || c > '7')
return -1;
result = result * 8 + (c - '0');
}
return result;
}
// =====================
// tarOperationGetFiles
// =====================
// Returns the number of files found
// Placing the names in files if a valid pointer
//
// -1 is returned on error
static int tarOperationIndexFiles(vfsfile_t *in, packfile_t *files) {
// Tar vars
union tar_buffer buffer;
int len;
int getheader = HEADER_SHORTNAME;
int remaining = 0;
char fname[BLOCKSIZE];
// Quake vars
int numfiles = 0;
VFS_SEEK(in, 0, SEEK_SET); /* Read the file from the begining */
while (1)
{
len = VFS_READ(in, &buffer, BLOCKSIZE, NULL);
if (len < 0 || len != BLOCKSIZE) {
Com_Printf("Invalid tar file\n");
return -1;
}
// We have reached the end of the tar
if (len == 0 || buffer.header.name[0] == 0)
break;
if (getheader == HEADER_SHORTNAME)
{
strncpy(fname,buffer.header.name,SHORTNAMESIZE);
if (fname[SHORTNAMESIZE-1] != 0)
fname[SHORTNAMESIZE] = 0;
}
else
{
/* The file name is longer than SHORTNAMESIZE */
if (strncmp(fname,buffer.header.name,SHORTNAMESIZE-1) != 0) {
Com_Printf("bad long name\n");
return -1;
}
getheader = HEADER_SHORTNAME;
}
/* Act according to the type flag */
switch (buffer.header.typeflag)
{
case DIRTYPE:
break;
case REGTYPE:
case AREGTYPE:
remaining = getoct(buffer.header.size,12);
if (remaining == -1)
{
Com_Printf("Invalid tar file\n");
return -1;
}
//Com_Printf(" %9d %s\n",remaining,fname);
if (files) {
strlcpy(files[numfiles].name, fname, sizeof(files[numfiles].name));
files[numfiles].filelen = remaining;
files[numfiles].filepos = VFS_TELL(in);
}
numfiles++;
getheader = HEADER_NONE;
break;
case GNUTYPE_LONGLINK:
case GNUTYPE_LONGNAME:
/* FIXME: Handle long names */
remaining = getoct(buffer.header.size,12);
if (remaining < 0 || remaining >= BLOCKSIZE)
{
printf("Invalid tar file\n");
return -1;
}
len = VFS_READ(in, fname, BLOCKSIZE, NULL);
if (len < 0) {
printf("Problem reading blocksize");
return -1;
}
if (fname[BLOCKSIZE-1] != 0 || (int)strlen(fname) > remaining)
{
printf("Invalid tar file\n");
return -1;
}
getheader = HEADER_LONGNAME;
break;
default:
printf(" <---> %s\n",fname);
numfiles++;
break;
}
/* Seek the next valid header which contains file names */
if (getheader == HEADER_NONE) {
unsigned int offset;
/* Round up to the nearest BLOCKSIZE */
if (remaining % BLOCKSIZE) {
offset = remaining + (BLOCKSIZE - (remaining % BLOCKSIZE));
} else {
offset = remaining;
}
VFS_SEEK(in, offset, SEEK_CUR);
getheader = HEADER_SHORTNAME;
}
}
return numfiles;
}
//=============================================================================
static int VFSTAR_ReadBytes(vfsfile_t *file, void *buffer, int bytestoread, vfserrno_t *err)
{
vfstarfile_t *vfst = (vfstarfile_t *)file;
int read;
//unsigned long base, bytes; <-- For reading in multiples of BLOCKSIZE
// VFS-FIXME: Need to read in chunks of BLOCKSIZE
if (vfst->currentpos - vfst->startpos + bytestoread > vfst->length)
bytestoread = vfst->length - (vfst->currentpos - vfst->startpos);
if (bytestoread < 0)
Sys_Error("VFSTAR_ReadBytes: bytestoread < 0");
if (vfst->parent->filepos != vfst->currentpos) {
VFS_SEEK(vfst->parent->raw, vfst->currentpos, SEEK_SET);
}
read = VFS_READ(vfst->parent->raw, buffer, bytestoread, NULL);
vfst->currentpos += read;
vfst->parent->filepos = vfst->currentpos;
// VFS-FIXME: Need to figure out errors
if (err)
*err = bytestoread == 0 ? VFSERR_EOF : VFSERR_NONE;
return read;
}
static int VFSTAR_WriteBytes(vfsfile_t *file, const void *buffer, int bytestowrite)
{
Sys_Error("VFSTAR_WriteBytes: Invalid operatioin\n");
return 0;
}
static int VFSTAR_Seek(vfsfile_t *file, unsigned long offset, int whence)
{
vfstarfile_t *vfst = (vfstarfile_t *)file;
switch(whence) {
case SEEK_SET:
vfst->currentpos = vfst->startpos + offset;
break;
case SEEK_CUR:
vfst->currentpos += offset;
break;
case SEEK_END:
vfst->currentpos = vfst->startpos + vfst->length + offset;
break;
default:
Sys_Error("VFSTAR_Seek: Unknown whence value(%d)\n", whence);
return -1;
}
if (vfst->currentpos > vfst->length) {
Com_Printf("VFSTAR_Seek: Warning seeking past the file's size\n");
}
return 0;
}
static unsigned long VFSTAR_Tell(vfsfile_t *file)
{
vfstarfile_t *vfst = (vfstarfile_t *)file;
return vfst->currentpos - vfst->startpos;;
}
static unsigned long VFSTAR_GetLen(vfsfile_t *file)
{
vfstarfile_t *vfst = (vfstarfile_t *)file;
return vfst->length;
}
static void FSTAR_ClosePath(void *handle);
static void VFSTAR_Close(vfsfile_t *file)
{
vfstarfile_t *vfst = (vfstarfile_t *)file;
FSTAR_ClosePath(vfst->parent);
Q_free(vfst);
}
static void VFSTAR_Flush(vfsfile_t *file)
{
Sys_Error("VFSTAR_Flush: Invalid operation\n");
}
static vfsfile_t *FSTAR_OpenVFS(void *handle, flocation_t *loc, char *mode)
{
tarfile_t *tar = (tarfile_t *) handle;
vfstarfile_t *vfst;
if (strcmp(mode, "rb"))
return NULL;
vfst = Q_calloc(1, sizeof(*vfst));
vfst->parent = tar;
vfst->parent->references++;
vfst->startpos = loc->offset;
vfst->length = loc->len;
vfst->currentpos = vfst->startpos;
vfst->funcs.ReadBytes = VFSTAR_ReadBytes;
vfst->funcs.WriteBytes = VFSTAR_WriteBytes;
vfst->funcs.Seek = VFSTAR_Seek;
vfst->funcs.Tell = VFSTAR_Tell;
vfst->funcs.GetLen = VFSTAR_GetLen;
vfst->funcs.Close = VFSTAR_Close;
vfst->funcs.Flush = VFSTAR_Flush;
if (loc->search)
vfst->funcs.copyprotected = loc->search->copyprotected;
return (vfsfile_t *)vfst;
}
//=============================================
// TAR file (*.tar) - Search Functions
//=============================================
static void FSTAR_PrintPath(void *handle)
{
tarfile_t *tar = (tarfile_t *)handle;
if (tar->references != 1)
Com_Printf("%s (%i)\n", tar->filename, tar->references-1);
else
Com_Printf("%s\n", tar->filename);
}
static void FSTAR_ClosePath(void *handle)
{
tarfile_t *tar = (tarfile_t *)handle;
if (--tar->references > 0)
return; //not yet time
VFS_CLOSE(tar->raw);
if (tar->files)
Q_free(tar->files);
Q_free(tar);
}
static void FSTAR_BuildHash(void *handle)
{
tarfile_t *tar = (tarfile_t *)handle;
int i;
for (i = 0; i < tar->numfiles; i++)
{
if (!Hash_GetInsensitive(filesystemhash, tar->files[i].name))
{
Hash_AddInsensitive(filesystemhash, tar->files[i].name, &tar->files[i]);
fs_hash_files++;
}
else
fs_hash_dups++;
}
}
static qbool FSTAR_FLocate(void *handle, flocation_t *loc, const char *filename, void *hashedresult)
{
packfile_t *pf = (packfile_t *) hashedresult;
tarfile_t *tar = (tarfile_t *) handle;
int i;
// look through all the pak file elements
if (pf)
{ //is this a pointer to a file in this pak?
if (pf < tar->files || pf > tar->files + tar->numfiles)
return false; //was found in a different path
}
else
{
for (i=0 ; i<tar->numfiles ; i++) //look for the file
{
if (!strcmp (tar->files[i].name, filename))
{
pf = &tar->files[i];
break;
}
}
}
if (pf)
{
if (loc)
{
loc->index = pf - tar->files;
snprintf(loc->rawname, sizeof(loc->rawname), "%s/%s", tar->filename, filename);
loc->offset = pf->filepos;
loc->len = pf->filelen;
}
return true;
}
return false;
}
static void FSTAR_ReadFile(void *handle, flocation_t *loc, char *buffer)
{
tarfile_t *tar = handle;
int err;
VFS_SEEK(tar->raw, tar->files[loc->index].filepos, SEEK_SET);
err = VFS_READ(tar->raw, buffer, tar->files[loc->index].filelen, NULL);
if (err!=tar->files[loc->index].filelen)
{
Com_Printf ("Can't extract file \"%s:%s\" (corrupt)\n", tar->filename, tar->files[loc->index].name);
return;
}
return;
}
static int FSTAR_EnumerateFiles (void *handle, char *match, int (*func)(char *, int, void *), void *parm)
{
tarfile_t *tar = handle;
int num;
for (num = 0; num < tar->numfiles; num++)
{
if (wildcmp(match, tar->files[num].name))
{
if (!func(tar->files[num].name, tar->files[num].filelen, parm))
return false;
}
}
return true;
}
// =================
// FSTAR_LoadTarFile
// =================
// Takes an explicit (not game tree related) path to a tar file.
//
// Loads the header and directory, adding the files at the beginning
// of the list so they override previous pack files.
static void *FSTAR_LoadTarFile(vfsfile_t *tarhandle, const char *desc)
{
tarfile_t *tar;
tar = Q_calloc(1, sizeof(*tar));
strlcpy (tar->filename, desc, sizeof (tar->filename));
tar->raw = tarhandle;
if (!tar->raw) goto fail;
// Get the number of files inside the tar
tar->numfiles = tarOperationIndexFiles(tar->raw, NULL);
if (tar->numfiles < 0) goto fail;
// Create a list of the number of files
tar->files = (packfile_t *)Q_malloc(tar->numfiles * sizeof(packfile_t));
tarOperationIndexFiles(tar->raw, tar->files);
tar->references = 1;
return tar;
fail:
// Q_free is safe to call on NULL pointers
Q_free(tar->files);
Q_free(tar);
return NULL;
}
searchpathfuncs_t tarfilefuncs = {
FSTAR_PrintPath,
FSTAR_ClosePath,
FSTAR_BuildHash,
FSTAR_FLocate,
FSTAR_ReadFile,
FSTAR_EnumerateFiles,
FSTAR_LoadTarFile,
NULL, // VFS-FIXME: Might be used for f_modification
FSTAR_OpenVFS
};