/
physfs_archiver_slb.c
135 lines (108 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
* SLB support routines for PhysicsFS.
*
* This driver handles SLB archives ("slab files"). This uncompressed format
* is used in I-War / Independence War and Independence War: Defiance.
*
* The format begins with four zero bytes (version?), the file count and the
* location of the table of contents. Each ToC entry contains a 64-byte buffer
* containing a zero-terminated filename, the offset of the data, and its size.
* All the filenames begin with the separator character '\'.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Aleksi Nurmi, based on the GRP archiver by
* Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
#if PHYSFS_SUPPORTS_SLB
23
static int slbLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
24
{
25
26
PHYSFS_uint32 i;
for (i = 0; i < count; i++)
27
{
28
PHYSFS_uint32 pos;
29
30
31
PHYSFS_uint32 size;
char name[64];
char backslash;
32
33
34
char *ptr;
/* don't include the '\' in the beginning */
35
36
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &backslash, 1), 0);
BAIL_IF(backslash != '\\', PHYSFS_ERR_CORRUPT, 0);
37
38
/* read the rest of the buffer, 63 bytes */
39
40
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &name, 63), 0);
name[63] = '\0'; /* in case the name lacks the null terminator */
41
42
/* convert backslashes */
43
for (ptr = name; *ptr; ptr++)
44
45
46
47
48
{
if (*ptr == '\\')
*ptr = '/';
} /* for */
49
50
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);
pos = PHYSFS_swapULE32(pos);
52
53
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
size = PHYSFS_swapULE32(size);
55
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
56
} /* for */
59
60
61
} /* slbLoadEntries */
62
63
static void *SLB_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
64
65
{
PHYSFS_uint32 version;
66
67
68
PHYSFS_uint32 count;
PHYSFS_uint32 tocPos;
void *unpkarc;
70
71
72
73
74
75
76
/* There's no identifier on an SLB file, so we assume it's _not_ if the
file count or tocPos is zero. Beyond that, we'll assume it's
bogus/corrupt if the entries' filenames don't start with '\' or the
tocPos is past the end of the file (seek will fail). This probably
covers all meaningful cases where we would accidentally accept a non-SLB
file with this archiver. */
77
78
assert(io != NULL); /* shouldn't ever happen. */
79
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
81
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &version, sizeof (version)), NULL);
82
version = PHYSFS_swapULE32(version);
83
BAIL_IF(version != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
85
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
86
count = PHYSFS_swapULE32(count);
87
BAIL_IF(!count, PHYSFS_ERR_UNSUPPORTED, NULL);
88
89
/* offset of the table of contents */
90
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &tocPos, sizeof (tocPos)), NULL);
91
tocPos = PHYSFS_swapULE32(tocPos);
92
BAIL_IF(!tocPos, PHYSFS_ERR_UNSUPPORTED, NULL);
93
94
/* seek to the table of contents */
95
BAIL_IF_ERRPASS(!io->seek(io, tocPos), NULL);
97
unpkarc = UNPK_openArchive(io);
98
BAIL_IF_ERRPASS(!unpkarc, NULL);
100
101
if (!slbLoadEntries(io, count, unpkarc))
{
102
UNPK_abandonArchive(unpkarc);
103
104
105
return NULL;
} /* if */
106
107
*claimed = 1; /* oh well. */
108
return unpkarc;
109
110
111
112
113
} /* SLB_openArchive */
const PHYSFS_Archiver __PHYSFS_Archiver_SLB =
{
114
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
115
116
117
118
{
"SLB",
"I-War / Independence War Slab file",
"Aleksi Nurmi <aleksi.nurmi@gmail.com>",
119
"https://bitbucket.org/ahnurmi/",
120
0, /* supportsSymlinks */
121
},
122
SLB_openArchive,
123
UNPK_enumerate,
124
125
126
127
128
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
129
130
UNPK_stat,
UNPK_closeArchive
131
132
133
};
#endif /* defined PHYSFS_SUPPORTS_SLB */
135
/* end of physfs_archiver_slb.c ... */