/
archiver_mvl.c
115 lines (93 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* 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/)
*
24
* Please see the file LICENSE.txt in the source's root directory.
25
26
27
28
29
30
31
32
33
34
35
*
* This file written by Bradley Bell.
* Based on grp.c by Ryan C. Gordon.
*/
#if (defined PHYSFS_SUPPORTS_MVL)
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
36
37
38
39
40
41
static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
{
return (io->read(io, buf, len) == len);
} /* readAll */
42
static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
44
PHYSFS_uint32 location = 8; /* sizeof sig. */
45
46
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
48
49
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF_MACRO(entries == NULL, ERR_OUT_OF_MEMORY, NULL);
50
51
52
location += (17 * fileCount);
53
for (entry = entries; fileCount > 0; fileCount--, entry++)
55
56
GOTO_IF_MACRO(!readAll(io, &entry->name, 13), NULL, mvlLoad_failed);
GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, mvlLoad_failed);
57
58
59
60
61
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
location += entry->size;
} /* for */
62
63
64
65
66
67
return entries;
mvlLoad_failed:
allocator.Free(entries);
return NULL;
} /* mvlLoadEntries */
70
static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
72
PHYSFS_uint8 buf[4];
73
74
PHYSFS_uint32 entryCount = 0;
UNPKentry *entries = NULL;
76
77
78
assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
BAIL_IF_MACRO(!readAll(io, buf, 4), NULL, NULL);
79
80
BAIL_IF_MACRO(memcmp(buf, "DMVL", 4) != 0, ERR_NOT_AN_ARCHIVE, NULL);
BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
82
83
84
85
entryCount = PHYSFS_swapULE32(entryCount);
entries = mvlLoadEntries(io, entryCount);
BAIL_IF_MACRO(entries == NULL, NULL, NULL);
return UNPK_openArchive(io, entries, entryCount);
86
87
88
} /* MVL_openArchive */
89
90
91
92
93
94
95
96
97
98
99
100
101
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL =
{
"MVL",
MVL_ARCHIVE_DESCRIPTION,
"Bradley Bell <btb@icculus.org>",
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
{
&__PHYSFS_ArchiveInfo_MVL,
MVL_openArchive, /* openArchive() method */
102
103
104
105
106
107
108
109
UNPK_enumerateFiles, /* enumerateFiles() method */
UNPK_openRead, /* openRead() method */
UNPK_openWrite, /* openWrite() method */
UNPK_openAppend, /* openAppend() method */
UNPK_remove, /* remove() method */
UNPK_mkdir, /* mkdir() method */
UNPK_dirClose, /* dirClose() method */
UNPK_stat /* stat() method */
112
113
114
#endif /* defined PHYSFS_SUPPORTS_MVL */
/* end of mvl.c ... */