/
physfs_archiver_slb.c
132 lines (106 loc) · 3.91 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);
51
52
53
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
size = PHYSFS_swapULE32(size);
54
55
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
56
} /* for */
57
58
return 1;
59
60
61
62
63
64
} /* slbLoadEntries */
static void *SLB_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
{
PHYSFS_uint32 version;
65
66
67
PHYSFS_uint32 count;
PHYSFS_uint32 tocPos;
void *unpkarc;
68
69
70
71
72
73
74
75
/* 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. */
76
77
assert(io != NULL); /* shouldn't ever happen. */
78
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
79
80
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &version, sizeof (version)), NULL);
81
version = PHYSFS_swapULE32(version);
82
BAIL_IF(version != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
83
84
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL);
85
count = PHYSFS_swapULE32(count);
86
BAIL_IF(!count, PHYSFS_ERR_UNSUPPORTED, NULL);
87
88
/* offset of the table of contents */
89
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &tocPos, sizeof (tocPos)), NULL);
90
tocPos = PHYSFS_swapULE32(tocPos);
91
BAIL_IF(!tocPos, PHYSFS_ERR_UNSUPPORTED, NULL);
92
93
/* seek to the table of contents */
94
BAIL_IF_ERRPASS(!io->seek(io, tocPos), NULL);
95
96
unpkarc = UNPK_openArchive(io);
97
BAIL_IF_ERRPASS(!unpkarc, NULL);
98
99
100
if (!slbLoadEntries(io, count, unpkarc))
{
101
UNPK_abandonArchive(unpkarc);
102
103
104
105
return NULL;
} /* if */
return unpkarc;
106
107
108
109
110
} /* SLB_openArchive */
const PHYSFS_Archiver __PHYSFS_Archiver_SLB =
{
111
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
112
113
114
115
{
"SLB",
"I-War / Independence War Slab file",
"Aleksi Nurmi <aleksi.nurmi@gmail.com>",
116
"https://bitbucket.org/ahnurmi/",
117
0, /* supportsSymlinks */
118
},
119
SLB_openArchive,
120
UNPK_enumerate,
121
122
123
124
125
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
126
127
UNPK_stat,
UNPK_closeArchive
128
129
130
};
#endif /* defined PHYSFS_SUPPORTS_SLB */
131
132
/* end of physfs_archiver_slb.c ... */