Skip to content

Latest commit

 

History

History
464 lines (371 loc) · 12.2 KB

archiver_unpacked.c

File metadata and controls

464 lines (371 loc) · 12.2 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.
*
Mar 23, 2012
Mar 23, 2012
8
9
* RULES: Archive entries must be uncompressed, must not have separate subdir
* entries (but can have subdirs), must be case insensitive LOW ASCII
Nov 12, 2012
Nov 12, 2012
10
* filenames <= 64 bytes. No symlinks, etc. We can relax some of these rules
Mar 23, 2012
Mar 23, 2012
11
* as necessary.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
*
* 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
{
PHYSFS_Io *io;
PHYSFS_uint32 entryCount;
UNPKentry *entries;
} UNPKinfo;
typedef struct
{
PHYSFS_Io *io;
UNPKentry *entry;
PHYSFS_uint32 curPos;
} UNPKfileinfo;
Nov 28, 2012
Nov 28, 2012
37
void UNPK_closeArchive(void *opaque)
38
39
40
41
42
{
UNPKinfo *info = ((UNPKinfo *) opaque);
info->io->destroy(info->io);
allocator.Free(info->entries);
allocator.Free(info);
Mar 24, 2012
Mar 24, 2012
43
} /* UNPK_closeArchive */
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)
{
Mar 22, 2012
Mar 22, 2012
66
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, -1);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
} /* 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;
Mar 20, 2012
Mar 20, 2012
82
BAIL_IF_MACRO(offset >= entry->size, PHYSFS_ERR_PAST_EOF, 0);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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));
Mar 20, 2012
Mar 20, 2012
104
105
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_duplicate_failed);
106
107
io = origfinfo->io->duplicate(origfinfo->io);
Mar 20, 2012
Mar 20, 2012
108
if (!io) goto UNPK_duplicate_failed;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
136
CURRENT_PHYSFS_IO_API_VERSION, NULL,
137
138
139
140
141
142
143
UNPK_read,
UNPK_write,
UNPK_seek,
UNPK_tell,
UNPK_length,
UNPK_duplicate,
UNPK_flush,
Mar 25, 2012
Mar 25, 2012
144
UNPK_destroy
Jun 1, 2012
Jun 1, 2012
148
static int entryCmp(void *_a, size_t one, size_t two)
149
150
151
152
{
if (one != two)
{
const UNPKentry *a = (const UNPKentry *) _a;
Mar 23, 2012
Mar 23, 2012
153
return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
154
155
156
157
158
159
} /* if */
return 0;
} /* entryCmp */
Jun 1, 2012
Jun 1, 2012
160
static void entrySwap(void *_a, size_t one, size_t two)
161
162
163
164
165
166
167
168
169
170
171
172
173
{
if (one != two)
{
UNPKentry tmp;
UNPKentry *first = &(((UNPKentry *) _a)[one]);
UNPKentry *second = &(((UNPKentry *) _a)[two]);
memcpy(&tmp, first, sizeof (UNPKentry));
memcpy(first, second, sizeof (UNPKentry));
memcpy(second, &tmp, sizeof (UNPKentry));
} /* if */
} /* entrySwap */
Mar 23, 2012
Mar 23, 2012
174
175
static PHYSFS_sint32 findStartOfDir(UNPKinfo *info, const char *path,
int stop_on_first_find)
Mar 23, 2012
Mar 23, 2012
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
PHYSFS_sint32 lo = 0;
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
PHYSFS_sint32 middle;
PHYSFS_uint32 dlen = (PHYSFS_uint32) strlen(path);
PHYSFS_sint32 retval = -1;
const char *name;
int rc;
if (*path == '\0') /* root dir? */
return 0;
if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
while (lo <= hi)
Mar 23, 2012
Mar 23, 2012
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
middle = lo + ((hi - lo) / 2);
name = info->entries[middle].name;
rc = __PHYSFS_strnicmpASCII(path, name, dlen);
if (rc == 0)
{
char ch = name[dlen];
if (ch < '/') /* make sure this isn't just a substr match. */
rc = -1;
else if (ch > '/')
rc = 1;
else
{
if (stop_on_first_find) /* Just checking dir's existance? */
return middle;
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
return (middle + 1);
/* there might be more entries earlier in the list. */
retval = middle;
hi = middle - 1;
} /* else */
} /* if */
if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
Mar 23, 2012
Mar 23, 2012
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
return retval;
} /* findStartOfDir */
/*
* Moved to seperate function so we can use alloca then immediately throw
* away the allocated stack space...
*/
static void doEnumCallback(PHYSFS_EnumFilesCallback cb, void *callbackdata,
const char *odir, const char *str, PHYSFS_sint32 ln)
{
char *newstr = __PHYSFS_smallAlloc(ln + 1);
if (newstr == NULL)
return;
memcpy(newstr, str, ln);
newstr[ln] = '\0';
cb(callbackdata, odir, newstr);
__PHYSFS_smallFree(newstr);
} /* doEnumCallback */
Nov 28, 2012
Nov 28, 2012
245
void UNPK_enumerateFiles(void *opaque, const char *dname,
Nov 29, 2012
Nov 29, 2012
246
PHYSFS_EnumFilesCallback cb,
Mar 25, 2012
Mar 25, 2012
247
const char *origdir, void *callbackdata)
Mar 23, 2012
Mar 23, 2012
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
{
UNPKinfo *info = ((UNPKinfo *) opaque);
PHYSFS_sint32 dlen, dlen_inc, max, i;
i = findStartOfDir(info, dname, 0);
if (i == -1) /* no such directory. */
return;
dlen = (PHYSFS_sint32) strlen(dname);
if ((dlen > 0) && (dname[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
max = (PHYSFS_sint32) info->entryCount;
while (i < max)
{
char *add;
char *ptr;
PHYSFS_sint32 ln;
char *e = info->entries[i].name;
if ((dlen) &&
((__PHYSFS_strnicmpASCII(e, dname, dlen)) || (e[dlen] != '/')))
{
break; /* past end of this dir; we're done. */
} /* if */
add = e + dlen_inc;
ptr = strchr(add, '/');
ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
doEnumCallback(cb, callbackdata, origdir, add, ln);
ln += dlen_inc; /* point past entry to children... */
/* increment counter and skip children of subdirs... */
while ((++i < max) && (ptr != NULL))
{
char *e_new = info->entries[i].name;
if ((__PHYSFS_strnicmpASCII(e, e_new, ln) != 0) ||
(e_new[ln] != '/'))
{
break;
} /* if */
} /* while */
} /* while */
291
292
293
} /* UNPK_enumerateFiles */
Mar 23, 2012
Mar 23, 2012
294
295
296
297
298
299
/*
* This will find the UNPKentry associated with a path in platform-independent
* notation. Directories don't have UNPKentries associated with them, but
* (*isDir) will be set to non-zero if a dir was hit.
*/
static UNPKentry *findEntry(const UNPKinfo *info, const char *path, int *isDir)
300
301
{
UNPKentry *a = info->entries;
Mar 23, 2012
Mar 23, 2012
302
PHYSFS_sint32 pathlen = (PHYSFS_sint32) strlen(path);
303
304
305
PHYSFS_sint32 lo = 0;
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
PHYSFS_sint32 middle;
Mar 23, 2012
Mar 23, 2012
306
const char *thispath = NULL;
307
308
309
310
311
int rc;
while (lo <= hi)
{
middle = lo + ((hi - lo) / 2);
Mar 23, 2012
Mar 23, 2012
312
313
314
315
thispath = a[middle].name;
rc = __PHYSFS_strnicmpASCII(path, thispath, pathlen);
if (rc > 0)
Mar 23, 2012
Mar 23, 2012
317
318
else if (rc < 0)
Mar 23, 2012
Mar 23, 2012
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
else /* substring match...might be dir or entry or nothing. */
{
if (isDir != NULL)
{
*isDir = (thispath[pathlen] == '/');
if (*isDir)
return NULL;
} /* if */
if (thispath[pathlen] == '\0') /* found entry? */
return &a[middle];
/* adjust search params, try again. */
else if (thispath[pathlen] > '/')
hi = middle - 1;
else
lo = middle + 1;
} /* if */
Mar 23, 2012
Mar 23, 2012
340
341
342
if (isDir != NULL)
*isDir = 0;
Nov 28, 2012
Nov 28, 2012
343
BAIL_MACRO(PHYSFS_ERR_NOT_FOUND, NULL);
344
345
346
} /* findEntry */
Nov 30, 2012
Nov 30, 2012
347
PHYSFS_Io *UNPK_openRead(void *opaque, const char *name)
348
349
350
{
PHYSFS_Io *retval = NULL;
UNPKinfo *info = (UNPKinfo *) opaque;
Oct 18, 2011
Oct 18, 2011
351
UNPKfileinfo *finfo = NULL;
Mar 23, 2012
Mar 23, 2012
352
int isdir = 0;
Nov 30, 2012
Nov 30, 2012
353
UNPKentry *entry = findEntry(info, name, &isdir);
Mar 23, 2012
Mar 23, 2012
355
GOTO_IF_MACRO(isdir, PHYSFS_ERR_NOT_A_FILE, UNPK_openRead_failed);
Mar 20, 2012
Mar 20, 2012
356
GOTO_IF_MACRO(!entry, ERRPASS, UNPK_openRead_failed);
357
358
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
Mar 20, 2012
Mar 20, 2012
359
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
360
361
finfo = (UNPKfileinfo *) allocator.Malloc(sizeof (UNPKfileinfo));
Mar 20, 2012
Mar 20, 2012
362
GOTO_IF_MACRO(!finfo, PHYSFS_ERR_OUT_OF_MEMORY, UNPK_openRead_failed);
363
364
finfo->io = info->io->duplicate(info->io);
Mar 20, 2012
Mar 20, 2012
365
GOTO_IF_MACRO(!finfo->io, ERRPASS, UNPK_openRead_failed);
366
367
if (!finfo->io->seek(finfo->io, entry->startPos))
Mar 20, 2012
Mar 20, 2012
368
goto UNPK_openRead_failed;
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
392
PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name)
Mar 22, 2012
Mar 22, 2012
394
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
395
396
397
} /* UNPK_openWrite */
Nov 28, 2012
Nov 28, 2012
398
PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name)
Mar 22, 2012
Mar 22, 2012
400
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, NULL);
401
402
403
} /* UNPK_openAppend */
Nov 28, 2012
Nov 28, 2012
404
int UNPK_remove(void *opaque, const char *name)
Mar 22, 2012
Mar 22, 2012
406
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
407
408
409
} /* UNPK_remove */
Nov 28, 2012
Nov 28, 2012
410
int UNPK_mkdir(void *opaque, const char *name)
Mar 22, 2012
Mar 22, 2012
412
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
413
414
415
} /* UNPK_mkdir */
Nov 30, 2012
Nov 30, 2012
416
int UNPK_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
Mar 23, 2012
Mar 23, 2012
418
int isDir = 0;
419
const UNPKinfo *info = (const UNPKinfo *) opaque;
Mar 23, 2012
Mar 23, 2012
420
const UNPKentry *entry = findEntry(info, filename, &isDir);
Mar 23, 2012
Mar 23, 2012
422
423
424
425
426
427
428
429
430
431
432
433
if (isDir)
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
else if (entry != NULL)
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = entry->size;
} /* else if */
else
{
Mar 23, 2012
Mar 23, 2012
435
} /* else */
436
437
438
439
440
441
442
443
444
445
stat->modtime = -1;
stat->createtime = -1;
stat->accesstime = -1;
stat->readonly = 1;
return 1;
} /* UNPK_stat */
Nov 28, 2012
Nov 28, 2012
446
void *UNPK_openArchive(PHYSFS_Io *io, UNPKentry *e, const PHYSFS_uint32 num)
447
448
449
450
451
{
UNPKinfo *info = (UNPKinfo *) allocator.Malloc(sizeof (UNPKinfo));
if (info == NULL)
{
allocator.Free(e);
Mar 20, 2012
Mar 20, 2012
452
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jun 1, 2012
Jun 1, 2012
455
__PHYSFS_sort(e, (size_t) num, entryCmp, entrySwap);
456
457
458
459
460
461
462
463
info->io = io;
info->entryCount = num;
info->entries = e;
return info;
} /* UNPK_openArchive */
/* end of archiver_unpacked.c ... */