Skip to content

Latest commit

 

History

History
500 lines (393 loc) · 13.7 KB

mvl.c

File metadata and controls

500 lines (393 loc) · 13.7 KB
 
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
/*
* MVL support routines for PhysicsFS.
*
* This driver handles Descent II Movielib archives.
*
* The file format of MVL is quite easy...
*
* //MVL File format - Written by Heiko Herrmann
* char sig[4] = {'D','M', 'V', 'L'}; // "DMVL"=Descent MoVie Library
*
* int num_files; // the number of files in this MVL
*
* struct {
* char file_name[13]; // Filename, padded to 13 bytes with 0s
* int file_size; // filesize in bytes
* }DIR_STRUCT[num_files];
*
* struct {
* char data[file_size]; // The file data
* }FILE_STRUCT[num_files];
*
* (That info is from http://www.descent2.com/ddn/specs/mvl/)
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Bradley Bell.
* Based on grp.c by Ryan C. Gordon.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
typedef struct
{
char name[13];
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
} MVLentry;
typedef struct
{
char *filename;
PHYSFS_sint64 last_mod_time;
PHYSFS_uint32 entryCount;
MVLentry *entries;
} MVLinfo;
typedef struct
{
void *handle;
MVLentry *entry;
PHYSFS_uint32 curPos;
} MVLfileinfo;
Sep 26, 2004
Sep 26, 2004
67
static PHYSFS_sint64 MVL_read(fvoid *opaque, void *buffer,
68
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
Sep 26, 2004
Sep 26, 2004
69
static PHYSFS_sint64 MVL_write(fvoid *opaque, const void *buffer,
70
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
Sep 26, 2004
Sep 26, 2004
71
72
73
74
75
static int MVL_eof(fvoid *opaque);
static PHYSFS_sint64 MVL_tell(fvoid *opaque);
static int MVL_seek(fvoid *opaque, PHYSFS_uint64 offset);
static PHYSFS_sint64 MVL_fileLength(fvoid *opaque);
static int MVL_fileClose(fvoid *opaque);
76
static int MVL_isArchive(const char *filename, int forWriting);
Sep 26, 2004
Sep 26, 2004
77
static void *MVL_openArchive(const char *name, int forWriting);
Sep 26, 2004
Sep 26, 2004
78
static LinkedStringList *MVL_enumerateFiles(dvoid *opaque,
79
80
const char *dirname,
int omitSymLinks);
Sep 26, 2004
Sep 26, 2004
81
82
83
84
85
86
87
88
89
90
static int MVL_exists(dvoid *opaque, const char *name);
static int MVL_isDirectory(dvoid *opaque, const char *name, int *fileExists);
static int MVL_isSymLink(dvoid *opaque, const char *name, int *fileExists);
static PHYSFS_sint64 MVL_getLastModTime(dvoid *opaque, const char *n, int *e);
static fvoid *MVL_openRead(dvoid *opaque, const char *name, int *exist);
static fvoid *MVL_openWrite(dvoid *opaque, const char *name);
static fvoid *MVL_openAppend(dvoid *opaque, const char *name);
static int MVL_remove(dvoid *opaque, const char *name);
static int MVL_mkdir(dvoid *opaque, const char *name);
static void MVL_dirClose(dvoid *opaque);
91
92
93
94
95
96
97
98
99
100
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL =
{
"MVL",
MVL_ARCHIVE_DESCRIPTION,
"Bradley Bell <btb@icculus.org>",
"http://icculus.org/physfs/",
};
Sep 26, 2004
Sep 26, 2004
101
const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
&__PHYSFS_ArchiveInfo_MVL,
MVL_isArchive, /* isArchive() method */
MVL_openArchive, /* openArchive() method */
MVL_enumerateFiles, /* enumerateFiles() method */
MVL_exists, /* exists() method */
MVL_isDirectory, /* isDirectory() method */
MVL_isSymLink, /* isSymLink() method */
MVL_getLastModTime, /* getLastModTime() method */
MVL_openRead, /* openRead() method */
MVL_openWrite, /* openWrite() method */
MVL_openAppend, /* openAppend() method */
MVL_remove, /* remove() method */
MVL_mkdir, /* mkdir() method */
Sep 26, 2004
Sep 26, 2004
116
117
118
119
120
121
122
123
MVL_dirClose, /* dirClose() method */
MVL_read, /* read() method */
MVL_write, /* write() method */
MVL_eof, /* eof() method */
MVL_tell, /* tell() method */
MVL_seek, /* seek() method */
MVL_fileLength, /* fileLength() method */
MVL_fileClose /* fileClose() method */
Sep 26, 2004
Sep 26, 2004
128
static void MVL_dirClose(dvoid *opaque)
Sep 26, 2004
Sep 26, 2004
130
MVLinfo *info = ((MVLinfo *) opaque);
131
132
133
134
135
136
free(info->filename);
free(info->entries);
free(info);
} /* MVL_dirClose */
Sep 26, 2004
Sep 26, 2004
137
static PHYSFS_sint64 MVL_read(fvoid *opaque, void *buffer,
138
139
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
Sep 26, 2004
Sep 26, 2004
140
MVLfileinfo *finfo = (MVLfileinfo *) opaque;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
MVLentry *entry = finfo->entry;
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
PHYSFS_sint64 rc;
if (objsLeft < objCount)
objCount = objsLeft;
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
return(rc);
} /* MVL_read */
Sep 26, 2004
Sep 26, 2004
157
static PHYSFS_sint64 MVL_write(fvoid *opaque, const void *buffer,
158
159
160
161
162
163
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* MVL_write */
Sep 26, 2004
Sep 26, 2004
164
static int MVL_eof(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
166
MVLfileinfo *finfo = (MVLfileinfo *) opaque;
167
168
169
170
171
MVLentry *entry = finfo->entry;
return(finfo->curPos >= entry->size);
} /* MVL_eof */
Sep 26, 2004
Sep 26, 2004
172
static PHYSFS_sint64 MVL_tell(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
174
return(((MVLfileinfo *) opaque)->curPos);
175
176
177
} /* MVL_tell */
Sep 26, 2004
Sep 26, 2004
178
static int MVL_seek(fvoid *opaque, PHYSFS_uint64 offset)
Sep 26, 2004
Sep 26, 2004
180
MVLfileinfo *finfo = (MVLfileinfo *) opaque;
181
182
183
184
185
186
187
188
189
190
191
192
193
MVLentry *entry = finfo->entry;
int rc;
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
if (rc)
finfo->curPos = (PHYSFS_uint32) offset;
return(rc);
} /* MVL_seek */
Sep 26, 2004
Sep 26, 2004
194
static PHYSFS_sint64 MVL_fileLength(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
196
MVLfileinfo *finfo = (MVLfileinfo *) opaque;
197
198
199
200
return((PHYSFS_sint64) finfo->entry->size);
} /* MVL_fileLength */
Sep 26, 2004
Sep 26, 2004
201
static int MVL_fileClose(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
203
MVLfileinfo *finfo = (MVLfileinfo *) opaque;
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
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
free(finfo);
return(1);
} /* MVL_fileClose */
static int mvl_open(const char *filename, int forWriting,
void **fh, PHYSFS_uint32 *count)
{
PHYSFS_uint8 buf[4];
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1)
goto openMvl_failed;
if (memcmp(buf, "DMVL", 4) != 0)
{
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
goto openMvl_failed;
} /* if */
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
goto openMvl_failed;
*count = PHYSFS_swapULE32(*count);
return(1);
openMvl_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
return(0);
} /* mvl_open */
static int MVL_isArchive(const char *filename, int forWriting)
{
void *fh;
PHYSFS_uint32 fileCount;
int retval = mvl_open(filename, forWriting, &fh, &fileCount);
if (fh != NULL)
__PHYSFS_platformClose(fh);
return(retval);
} /* MVL_isArchive */
static int mvl_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
MVLentry *a = (MVLentry *) _a;
return(strcmp(a[one].name, a[two].name));
} /* mvl_entry_cmp */
static void mvl_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
MVLentry tmp;
MVLentry *first = &(((MVLentry *) _a)[one]);
MVLentry *second = &(((MVLentry *) _a)[two]);
memcpy(&tmp, first, sizeof (MVLentry));
memcpy(first, second, sizeof (MVLentry));
memcpy(second, &tmp, sizeof (MVLentry));
} /* mvl_entry_swap */
static int mvl_load_entries(const char *name, int forWriting, MVLinfo *info)
{
void *fh = NULL;
PHYSFS_uint32 fileCount;
PHYSFS_uint32 location = 8; /* sizeof sig. */
MVLentry *entry;
BAIL_IF_MACRO(!mvl_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
info->entries = (MVLentry *) malloc(sizeof (MVLentry) * fileCount);
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
location += (17 * fileCount);
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
{
__PHYSFS_platformClose(fh);
return(0);
} /* if */
if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
{
__PHYSFS_platformClose(fh);
return(0);
} /* if */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
location += entry->size;
} /* for */
__PHYSFS_platformClose(fh);
__PHYSFS_sort(info->entries, info->entryCount,
mvl_entry_cmp, mvl_entry_swap);
return(1);
} /* mvl_load_entries */
Sep 26, 2004
Sep 26, 2004
323
static void *MVL_openArchive(const char *name, int forWriting)
324
325
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
Sep 26, 2004
Sep 26, 2004
326
MVLinfo *info = malloc(sizeof (MVLinfo));
Sep 26, 2004
Sep 26, 2004
328
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
memset(info, '\0', sizeof (MVLinfo));
info->filename = (char *) malloc(strlen(name) + 1);
if (info->filename == NULL)
{
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
goto MVL_openArchive_failed;
} /* if */
if (!mvl_load_entries(name, forWriting, info))
goto MVL_openArchive_failed;
strcpy(info->filename, name);
info->last_mod_time = modtime;
Sep 26, 2004
Sep 26, 2004
343
return(info);
344
345
MVL_openArchive_failed:
Sep 26, 2004
Sep 26, 2004
346
if (info != NULL)
Sep 26, 2004
Sep 26, 2004
348
349
350
351
352
if (info->filename != NULL)
free(info->filename);
if (info->entries != NULL)
free(info->entries);
free(info);
353
354
355
356
357
358
} /* if */
return(NULL);
} /* MVL_openArchive */
Sep 26, 2004
Sep 26, 2004
359
static LinkedStringList *MVL_enumerateFiles(dvoid *opaque,
360
361
362
const char *dirname,
int omitSymLinks)
{
Sep 26, 2004
Sep 26, 2004
363
MVLinfo *info = ((MVLinfo *) opaque);
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
MVLentry *entry = info->entries;
LinkedStringList *retval = NULL, *p = NULL;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
/* no directories in MVL files. */
BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
for (i = 0; i < max; i++, entry++)
retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
return(retval);
} /* MVL_enumerateFiles */
static MVLentry *mvl_find_entry(MVLinfo *info, const char *name)
{
char *ptr = strchr(name, '.');
MVLentry *a = info->entries;
PHYSFS_sint32 lo = 0;
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
PHYSFS_sint32 middle;
int rc;
/*
* Rule out filenames to avoid unneeded processing...no dirs,
* big filenames, or extensions > 3 chars.
*/
BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
while (lo <= hi)
{
middle = lo + ((hi - lo) / 2);
rc = __PHYSFS_platformStricmp(name, a[middle].name);
if (rc == 0) /* found it! */
return(&a[middle]);
else if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* mvl_find_entry */
Sep 26, 2004
Sep 26, 2004
412
static int MVL_exists(dvoid *opaque, const char *name)
Sep 26, 2004
Sep 26, 2004
414
return(mvl_find_entry(((MVLinfo *) opaque), name) != NULL);
415
416
417
} /* MVL_exists */
Sep 26, 2004
Sep 26, 2004
418
static int MVL_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
420
*fileExists = MVL_exists(opaque, name);
421
422
423
424
return(0); /* never directories in a groupfile. */
} /* MVL_isDirectory */
Sep 26, 2004
Sep 26, 2004
425
static int MVL_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
427
*fileExists = MVL_exists(opaque, name);
428
429
430
431
return(0); /* never symlinks in a groupfile. */
} /* MVL_isSymLink */
Sep 26, 2004
Sep 26, 2004
432
static PHYSFS_sint64 MVL_getLastModTime(dvoid *opaque,
433
434
435
const char *name,
int *fileExists)
{
Sep 26, 2004
Sep 26, 2004
436
MVLinfo *info = ((MVLinfo *) opaque);
437
438
439
440
PHYSFS_sint64 retval = -1;
*fileExists = (mvl_find_entry(info, name) != NULL);
if (*fileExists) /* use time of MVL itself in the physical filesystem. */
Sep 26, 2004
Sep 26, 2004
441
retval = info->last_mod_time;
442
443
444
445
446
return(retval);
} /* MVL_getLastModTime */
Sep 26, 2004
Sep 26, 2004
447
static fvoid *MVL_openRead(dvoid *opaque, const char *fnm, int *fileExists)
Sep 26, 2004
Sep 26, 2004
449
MVLinfo *info = ((MVLinfo *) opaque);
450
451
452
453
454
455
456
457
MVLfileinfo *finfo;
MVLentry *entry;
entry = mvl_find_entry(info, fnm);
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
finfo = (MVLfileinfo *) malloc(sizeof (MVLfileinfo));
Sep 26, 2004
Sep 26, 2004
458
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
459
460
461
462
463
464
465
466
467
468
469
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
free(finfo);
return(NULL);
} /* if */
finfo->curPos = 0;
finfo->entry = entry;
Sep 26, 2004
Sep 26, 2004
470
return(finfo);
471
472
473
} /* MVL_openRead */
Sep 26, 2004
Sep 26, 2004
474
static fvoid *MVL_openWrite(dvoid *opaque, const char *name)
475
476
477
478
479
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* MVL_openWrite */
Sep 26, 2004
Sep 26, 2004
480
static fvoid *MVL_openAppend(dvoid *opaque, const char *name)
481
482
483
484
485
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* MVL_openAppend */
Sep 26, 2004
Sep 26, 2004
486
static int MVL_remove(dvoid *opaque, const char *name)
487
488
489
490
491
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* MVL_remove */
Sep 26, 2004
Sep 26, 2004
492
static int MVL_mkdir(dvoid *opaque, const char *name)
493
494
495
496
497
498
499
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* MVL_mkdir */
#endif /* defined PHYSFS_SUPPORTS_MVL */
/* end of mvl.c ... */