Skip to content

Latest commit

 

History

History
305 lines (234 loc) · 7.36 KB

physfs_archiver_unpacked.c

File metadata and controls

305 lines (234 loc) · 7.36 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
typedef struct
{
__PHYSFS_DirTreeEntry tree;
PHYSFS_uint64 startPos;
PHYSFS_uint64 size;
Jul 22, 2017
Jul 22, 2017
30
31
PHYSFS_sint64 ctime;
PHYSFS_sint64 mtime;
Jul 16, 2017
Jul 16, 2017
32
} UNPKentry;
33
34
35
36
37
38
39
40
41
typedef struct
{
PHYSFS_Io *io;
UNPKentry *entry;
PHYSFS_uint32 curPos;
} UNPKfileinfo;
Nov 28, 2012
Nov 28, 2012
42
void UNPK_closeArchive(void *opaque)
43
44
{
UNPKinfo *info = ((UNPKinfo *) opaque);
Jul 16, 2017
Jul 16, 2017
45
46
47
48
49
50
51
52
53
if (info)
{
__PHYSFS_DirTreeDeinit(&info->tree);
if (info->io)
info->io->destroy(info->io);
allocator.Free(info);
} /* if */
Mar 24, 2012
Mar 24, 2012
54
} /* UNPK_closeArchive */
Jul 21, 2017
Jul 21, 2017
56
57
58
59
60
61
62
63
64
void UNPK_abandonArchive(void *opaque)
{
UNPKinfo *info = ((UNPKinfo *) opaque);
if (info)
{
info->io = NULL;
UNPK_closeArchive(info);
} /* if */
} /* UNPK_abandonArchive */
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
86
BAIL(PHYSFS_ERR_READ_ONLY, -1);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
} /* 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
102
BAIL_IF(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
124
125
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
126
127
io = origfinfo->io->duplicate(origfinfo->io);
Mar 20, 2012
Mar 20, 2012
128
if (!io) goto UNPK_duplicate_failed;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
156
CURRENT_PHYSFS_IO_API_VERSION, NULL,
157
158
159
160
161
162
163
UNPK_read,
UNPK_write,
UNPK_seek,
UNPK_tell,
UNPK_length,
UNPK_duplicate,
UNPK_flush,
Mar 25, 2012
Mar 25, 2012
164
UNPK_destroy
Jul 16, 2017
Jul 16, 2017
168
static inline UNPKentry *findEntry(UNPKinfo *info, const char *path)
Mar 23, 2012
Mar 23, 2012
169
{
Jul 16, 2017
Jul 16, 2017
170
return (UNPKentry *) __PHYSFS_DirTreeFind(&info->tree, path);
171
172
173
} /* findEntry */
Nov 30, 2012
Nov 30, 2012
174
PHYSFS_Io *UNPK_openRead(void *opaque, const char *name)
175
176
177
{
PHYSFS_Io *retval = NULL;
UNPKinfo *info = (UNPKinfo *) opaque;
Oct 18, 2011
Oct 18, 2011
178
UNPKfileinfo *finfo = NULL;
Jul 16, 2017
Jul 16, 2017
179
UNPKentry *entry = findEntry(info, name);
Jul 16, 2017
Jul 16, 2017
181
182
BAIL_IF_ERRPASS(!entry, NULL);
BAIL_IF(entry->tree.isdir, PHYSFS_ERR_NOT_A_FILE, NULL);
183
184
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
Jul 6, 2017
Jul 6, 2017
185
GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
186
187
finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
Jul 6, 2017
Jul 6, 2017
188
GOTO_IF(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
189
190
finfo->io = info->io->duplicate(info->io);
Jul 6, 2017
Jul 6, 2017
191
GOTO_IF_ERRPASS(!finfo->io, UNPK_openRead_failed);
192
193
if (!finfo->io->seek(finfo->io, entry->startPos))
Mar 20, 2012
Mar 20, 2012
194
goto UNPK_openRead_failed;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
218
PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
220
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
221
222
223
} /* UNPK_openWrite */
Nov 28, 2012
Nov 28, 2012
224
PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
226
BAIL(PHYSFS_ERR_READ_ONLY, NULL);
227
228
229
} /* UNPK_openAppend */
Nov 28, 2012
Nov 28, 2012
230
int UNPK_remove(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
232
BAIL(PHYSFS_ERR_READ_ONLY, 0);
233
234
235
} /* UNPK_remove */
Nov 28, 2012
Nov 28, 2012
236
int UNPK_mkdir(void *opaque, const char *name)
Jul 6, 2017
Jul 6, 2017
238
BAIL(PHYSFS_ERR_READ_ONLY, 0);
239
240
241
} /* UNPK_mkdir */
Jul 16, 2017
Jul 16, 2017
242
int UNPK_stat(void *opaque, const char *path, PHYSFS_Stat *stat)
Jul 16, 2017
Jul 16, 2017
244
245
UNPKinfo *info = (UNPKinfo *) opaque;
const UNPKentry *entry = findEntry(info, path);
Jul 16, 2017
Jul 16, 2017
247
248
249
BAIL_IF_ERRPASS(!entry, 0);
if (entry->tree.isdir)
Mar 23, 2012
Mar 23, 2012
250
251
252
253
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
Jul 16, 2017
Jul 16, 2017
254
else
Mar 23, 2012
Mar 23, 2012
255
256
257
258
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = entry->size;
} /* else */
Jul 22, 2017
Jul 22, 2017
260
261
stat->modtime = entry->mtime;
stat->createtime = entry->ctime;
262
263
264
265
266
267
268
stat->accesstime = -1;
stat->readonly = 1;
return 1;
} /* UNPK_stat */
Jul 16, 2017
Jul 16, 2017
269
void *UNPK_addEntry(void *opaque, char *name, const int isdir,
Jul 22, 2017
Jul 22, 2017
270
const PHYSFS_sint64 ctime, const PHYSFS_sint64 mtime,
Jul 16, 2017
Jul 16, 2017
271
272
273
274
275
276
277
278
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);
Jul 21, 2017
Jul 21, 2017
279
280
entry->startPos = isdir ? 0 : pos;
entry->size = isdir ? 0 : len;
Jul 22, 2017
Jul 22, 2017
281
282
entry->ctime = ctime;
entry->mtime = mtime;
Jul 16, 2017
Jul 16, 2017
283
284
285
286
287
return entry;
} /* UNPK_addEntry */
Jul 21, 2017
Jul 21, 2017
288
void *UNPK_openArchive(PHYSFS_Io *io)
289
290
{
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
Jul 16, 2017
Jul 16, 2017
291
292
BAIL_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 21, 2017
Jul 21, 2017
293
if (!__PHYSFS_DirTreeInit(&info->tree, sizeof (UNPKentry)))
Jul 16, 2017
Jul 16, 2017
295
296
allocator.Free(info);
return NULL;
297
298
299
300
301
302
303
} /* if */
info->io = io;
return info;
} /* UNPK_openArchive */
Jul 22, 2017
Jul 22, 2017
304
/* end of physfs_archiver_unpacked.c ... */