Skip to content

Latest commit

 

History

History
291 lines (220 loc) · 6.94 KB

archiver_unpacked.c

File metadata and controls

291 lines (220 loc) · 6.94 KB
 
1
2
3
4
5
6
7
/*
* High-level PhysicsFS archiver for simple unpacked file formats.
*
* This is a framework that basic archivers build on top of. It's for simple
* formats that can just hand back a list of files and the offsets of their
* uncompressed data. There are an alarming number of formats like this.
*
Jul 16, 2017
Jul 16, 2017
8
9
* RULES: Archive entries must be uncompressed. Dirs and files allowed, but no
* symlinks, etc. We can relax some of these rules as necessary.
10
11
12
13
14
15
16
17
18
19
20
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
typedef struct
{
Jul 16, 2017
Jul 16, 2017
21
__PHYSFS_DirTree tree;
22
23
24
PHYSFS_Io *io;
} UNPKinfo;
Jul 16, 2017
Jul 16, 2017
25
26
27
28
29
30
typedef struct
{
__PHYSFS_DirTreeEntry tree;
PHYSFS_uint64 startPos;
PHYSFS_uint64 size;
} UNPKentry;
31
32
33
34
35
36
37
38
39
typedef struct
{
PHYSFS_Io *io;
UNPKentry *entry;
PHYSFS_uint32 curPos;
} UNPKfileinfo;
Nov 28, 2012
Nov 28, 2012
40
void UNPK_closeArchive(void *opaque)
41
42
{
UNPKinfo *info = ((UNPKinfo *) opaque);
Jul 16, 2017
Jul 16, 2017
43
44
45
46
47
48
49
50
51
if (info)
{
__PHYSFS_DirTreeDeinit(&info->tree);
if (info->io)
info->io->destroy(info->io);
allocator.Free(info);
} /* if */
Mar 24, 2012
Mar 24, 2012
52
} /* UNPK_closeArchive */
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
static PHYSFS_sint64 UNPK_read(PHYSFS_Io *io, void *buffer, PHYSFS_uint64 len)
{
UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
const UNPKentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
PHYSFS_sint64 rc;
if (bytesLeft < len)
len = bytesLeft;
rc = finfo->io->read(finfo->io, buffer, len);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) rc;
return rc;
} /* UNPK_read */
static PHYSFS_sint64 UNPK_write(PHYSFS_Io *io, const void *b, PHYSFS_uint64 len)
{
Jul 6, 2017
Jul 6, 2017
75
BAIL(PHYSFS_ERR_READ_ONLY, -1);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
} /* UNPK_write */
static PHYSFS_sint64 UNPK_tell(PHYSFS_Io *io)
{
return ((UNPKfileinfo *) io->opaque)->curPos;
} /* UNPK_tell */
static int UNPK_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
{
UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
const UNPKentry *entry = finfo->entry;
int rc;
Jul 6, 2017
Jul 6, 2017
91
BAIL_IF(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
rc = finfo->io->seek(finfo->io, entry->startPos + offset);
if (rc)
finfo->curPos = (PHYSFS_uint32) offset;
return rc;
} /* UNPK_seek */
static PHYSFS_sint64 UNPK_length(PHYSFS_Io *io)
{
const UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
return ((PHYSFS_sint64) finfo->entry->size);
} /* UNPK_length */
static PHYSFS_Io *UNPK_duplicate(PHYSFS_Io *_io)
{
UNPKfileinfo *origfinfo = (UNPKfileinfo *) _io->opaque;
PHYSFS_Io *io = NULL;
PHYSFS_Io *retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
UNPKfileinfo *finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
Jul 6, 2017
Jul 6, 2017
113
114
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
115
116
io = origfinfo->io->duplicate(origfinfo->io);
Mar 20, 2012
Mar 20, 2012
117
if (!io) goto UNPK_duplicate_failed;
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
finfo->io = io;
finfo->entry = origfinfo->entry;
finfo->curPos = 0;
memcpy(retval, _io, sizeof (PHYSFS_Io));
retval->opaque = finfo;
return retval;
UNPK_duplicate_failed:
if (finfo != NULL) allocator.Free(finfo);
if (retval != NULL) allocator.Free(retval);
if (io != NULL) io->destroy(io);
return NULL;
} /* UNPK_duplicate */
static int UNPK_flush(PHYSFS_Io *io) { return 1; /* no write support. */ }
static void UNPK_destroy(PHYSFS_Io *io)
{
UNPKfileinfo *finfo = (UNPKfileinfo *) io->opaque;
finfo->io->destroy(finfo->io);
allocator.Free(finfo);
allocator.Free(io);
} /* UNPK_destroy */
static const PHYSFS_Io UNPK_Io =
{
Mar 25, 2012
Mar 25, 2012
145
CURRENT_PHYSFS_IO_API_VERSION, NULL,
146
147
148
149
150
151
152
UNPK_read,
UNPK_write,
UNPK_seek,
UNPK_tell,
UNPK_length,
UNPK_duplicate,
UNPK_flush,
Mar 25, 2012
Mar 25, 2012
153
UNPK_destroy
Jul 16, 2017
Jul 16, 2017
157
static inline UNPKentry *findEntry(UNPKinfo *info, const char *path)
Mar 23, 2012
Mar 23, 2012
158
{
Jul 16, 2017
Jul 16, 2017
159
return (UNPKentry *) __PHYSFS_DirTreeFind(&info->tree, path);
160
161
162
} /* findEntry */
Nov 30, 2012
Nov 30, 2012
163
PHYSFS_Io *UNPK_openRead(void *opaque, const char *name)
164
165
166
{
PHYSFS_Io *retval = NULL;
UNPKinfo *info = (UNPKinfo *) opaque;
Oct 18, 2011
Oct 18, 2011
167
UNPKfileinfo *finfo = NULL;
Jul 16, 2017
Jul 16, 2017
168
UNPKentry *entry = findEntry(info, name);
Jul 16, 2017
Jul 16, 2017
170
171
BAIL_IF_ERRPASS(!entry, NULL);
BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
172
173
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
Jul 6, 2017
Jul 6, 2017
174
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
175
176
finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
Jul 6, 2017
Jul 6, 2017
177
GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
178
179
finfo->io = info->io->duplicate(info->io);
Jul 6, 2017
Jul 6, 2017
180
GOTO_IF_ERRPASS(!finfo->io, UNPK_openRead_failed);
181
182
if (!finfo->io->seek(finfo->io, entry->startPos))
Mar 20, 2012
Mar 20, 2012
183
goto UNPK_openRead_failed;
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
finfo->curPos = 0;
finfo->entry = entry;
memcpy(retval, &UNPK_Io, sizeof (*retval));
retval->opaque = finfo;
return retval;
UNPK_openRead_failed:
if (finfo != NULL)
{
if (finfo->io != NULL)
finfo->io->destroy(finfo->io);
allocator.Free(finfo);
} /* if */
if (retval != NULL)
allocator.Free(retval);
return NULL;
} /* UNPK_openRead */
Nov 28, 2012
Nov 28, 2012
207
PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
209
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
210
211
212
} /* UNPK_openWrite */
Nov 28, 2012
Nov 28, 2012
213
PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
215
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
216
217
218
} /* UNPK_openAppend */
Nov 28, 2012
Nov 28, 2012
219
int UNPK_remove(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
221
BAIL(PHYSFS_ERR_READ_ONLY, 0);
222
223
224
} /* UNPK_remove */
Nov 28, 2012
Nov 28, 2012
225
int UNPK_mkdir(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
227
BAIL(PHYSFS_ERR_READ_ONLY, 0);
228
229
230
} /* UNPK_mkdir */
Jul 16, 2017
Jul 16, 2017
231
int UNPK_stat(void *opaque, const char *path, PHYSFS_Stat *stat)
Jul 16, 2017
Jul 16, 2017
233
234
UNPKinfo *info = (UNPKinfo *) opaque;
const UNPKentry *entry = findEntry(info, path);
Jul 16, 2017
Jul 16, 2017
236
237
238
BAIL_IF_ERRPASS(!entry, 0);
if (entry->tree.isdir)
Mar 23, 2012
Mar 23, 2012
239
240
241
242
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
Jul 16, 2017
Jul 16, 2017
243
else
Mar 23, 2012
Mar 23, 2012
244
245
246
247
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = entry->size;
} /* else */
248
249
250
251
252
253
254
255
256
257
stat->modtime = -1;
stat->createtime = -1;
stat->accesstime = -1;
stat->readonly = 1;
return 1;
} /* UNPK_stat */
Jul 16, 2017
Jul 16, 2017
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
void *UNPK_addEntry(void *opaque, char *name, const int isdir,
const PHYSFS_uint64 pos, const PHYSFS_uint64 len)
{
UNPKinfo *info = (UNPKinfo *) opaque;
UNPKentry *entry;
entry = (UNPKentry *) __PHYSFS_DirTreeAdd(&info->tree, name, isdir);
BAIL_IF_ERRPASS(!entry, NULL);
entry->startPos = pos;
entry->size = len;
return entry;
} /* UNPK_addEntry */
Jul 21, 2017
Jul 21, 2017
274
void *UNPK_openArchive(PHYSFS_Io *io)
275
276
{
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
Jul 16, 2017
Jul 16, 2017
277
278
BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 21, 2017
Jul 21, 2017
279
if (!__PHYSFS_DirTreeInit(&info->tree, sizeof (UNPKentry)))
Jul 16, 2017
Jul 16, 2017
281
282
allocator.Free(info);
return NULL;
283
284
285
286
287
288
289
290
} /* if */
info->io = io;
return info;
} /* UNPK_openArchive */
/* end of archiver_unpacked.c ... */