/
physfs.c
2742 lines (2191 loc) · 76.1 KB
1
2
3
4
5
/**
* PhysicsFS; a portable, flexible file i/o abstraction.
*
* Documentation is in physfs.h. It's verbose, honest. :)
*
6
* Please see the file LICENSE.txt in the source's root directory.
7
8
9
10
*
* This file written by Ryan C. Gordon.
*/
11
/* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
12
/* !!! FIXME: use snprintf(), not sprintf(). */
13
14
15
16
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
17
18
19
typedef struct __PHYSFS_DIRHANDLE__
{
20
21
void *opaque; /* Instance data unique to the archiver. */
char *dirName; /* Path to archive in platform-dependent notation. */
22
char *mountPoint; /* Mountpoint in virtual file tree. */
23
24
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
25
26
27
} DirHandle;
28
29
typedef struct __PHYSFS_FILEHANDLE__
{
30
PHYSFS_Io *io; /* Instance data unique to the archiver for this file. */
31
32
33
34
35
36
37
38
39
40
PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
const DirHandle *dirHandle; /* Archiver instance that created this */
PHYSFS_uint8 *buffer; /* Buffer, if set (NULL otherwise). Don't touch! */
PHYSFS_uint32 bufsize; /* Bufsize, if set (0 otherwise). Don't touch! */
PHYSFS_uint32 buffill; /* Buffer fill size. Don't touch! */
PHYSFS_uint32 bufpos; /* Buffer position. Don't touch! */
struct __PHYSFS_FILEHANDLE__ *next; /* linked list stuff. */
} FileHandle;
41
typedef struct __PHYSFS_ERRSTATETYPE__
42
{
43
void *tid;
44
45
46
PHYSFS_ErrorCode code;
struct __PHYSFS_ERRSTATETYPE__ *next;
} ErrState;
47
48
49
/* The various i/o drivers...some of these may not be compiled in. */
50
51
52
53
54
55
56
57
58
59
60
extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_LZMA;
extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
extern const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660;
static const PHYSFS_Archiver *staticArchivers[] =
61
{
62
#if PHYSFS_SUPPORTS_ZIP
63
&__PHYSFS_Archiver_ZIP,
64
#endif
65
#if PHYSFS_SUPPORTS_7Z
66
67
&__PHYSFS_Archiver_LZMA,
#endif
68
#if PHYSFS_SUPPORTS_GRP
69
&__PHYSFS_Archiver_GRP,
70
#endif
71
#if PHYSFS_SUPPORTS_QPAK
72
&__PHYSFS_Archiver_QPAK,
73
#endif
74
#if PHYSFS_SUPPORTS_HOG
75
&__PHYSFS_Archiver_HOG,
76
#endif
77
#if PHYSFS_SUPPORTS_MVL
78
&__PHYSFS_Archiver_MVL,
79
#endif
80
#if PHYSFS_SUPPORTS_WAD
81
&__PHYSFS_Archiver_WAD,
82
#endif
83
#if PHYSFS_SUPPORTS_ISO9660
84
&__PHYSFS_Archiver_ISO9660,
85
#endif
86
87
NULL
};
88
89
90
91
92
/* General PhysicsFS state ... */
static int initialized = 0;
93
static ErrState *errorStates = NULL;
94
95
96
97
static DirHandle *searchPath = NULL;
static DirHandle *writeDir = NULL;
static FileHandle *openWriteList = NULL;
static FileHandle *openReadList = NULL;
98
99
static char *baseDir = NULL;
static char *userDir = NULL;
100
static char *prefDir = NULL;
101
static int allowSymLinks = 0;
102
103
static const PHYSFS_Archiver **archivers = NULL;
static const PHYSFS_ArchiveInfo **archiveInfo = NULL;
104
105
106
107
/* mutexes ... */
static void *errorLock = NULL; /* protects error message list. */
static void *stateLock = NULL; /* protects other PhysFS static state. */
108
109
110
/* allocator ... */
static int externalAllocator = 0;
111
PHYSFS_Allocator allocator;
112
113
114
115
116
117
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* PHYSFS_Io implementation for i/o to physical filesystem... */
/* !!! FIXME: maybe refcount the paths in a string pool? */
typedef struct __PHYSFS_NativeIoInfo
{
void *handle;
const char *path;
int mode; /* 'r', 'w', or 'a' */
} NativeIoInfo;
static PHYSFS_sint64 nativeIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
return __PHYSFS_platformRead(info->handle, buf, len);
} /* nativeIo_read */
static PHYSFS_sint64 nativeIo_write(PHYSFS_Io *io, const void *buffer,
PHYSFS_uint64 len)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
return __PHYSFS_platformWrite(info->handle, buffer, len);
} /* nativeIo_write */
static int nativeIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
return __PHYSFS_platformSeek(info->handle, offset);
} /* nativeIo_seek */
static PHYSFS_sint64 nativeIo_tell(PHYSFS_Io *io)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
return __PHYSFS_platformTell(info->handle);
} /* nativeIo_tell */
static PHYSFS_sint64 nativeIo_length(PHYSFS_Io *io)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
return __PHYSFS_platformFileLength(info->handle);
} /* nativeIo_length */
static PHYSFS_Io *nativeIo_duplicate(PHYSFS_Io *io)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
return __PHYSFS_createNativeIo(info->path, info->mode);
} /* nativeIo_duplicate */
static int nativeIo_flush(PHYSFS_Io *io)
{
return __PHYSFS_platformFlush(io->opaque);
} /* nativeIo_flush */
static void nativeIo_destroy(PHYSFS_Io *io)
{
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
__PHYSFS_platformClose(info->handle);
allocator.Free((void *) info->path);
allocator.Free(info);
allocator.Free(io);
} /* nativeIo_destroy */
static const PHYSFS_Io __PHYSFS_nativeIoInterface =
{
nativeIo_read,
nativeIo_write,
nativeIo_seek,
nativeIo_tell,
nativeIo_length,
nativeIo_duplicate,
nativeIo_flush,
nativeIo_destroy,
NULL
};
PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode)
{
PHYSFS_Io *io = NULL;
NativeIoInfo *info = NULL;
void *handle = NULL;
char *pathdup = NULL;
assert((mode == 'r') || (mode == 'w') || (mode == 'a'));
io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
198
GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
199
info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
200
GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
201
pathdup = (char *) allocator.Malloc(strlen(path) + 1);
202
GOTO_IF_MACRO(!pathdup, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
203
204
205
206
207
208
209
210
if (mode == 'r')
handle = __PHYSFS_platformOpenRead(path);
else if (mode == 'w')
handle = __PHYSFS_platformOpenWrite(path);
else if (mode == 'a')
handle = __PHYSFS_platformOpenAppend(path);
211
GOTO_IF_MACRO(!handle, ERRPASS, createNativeIo_failed);
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
strcpy(pathdup, path);
info->handle = handle;
info->path = pathdup;
info->mode = mode;
memcpy(io, &__PHYSFS_nativeIoInterface, sizeof (*io));
io->opaque = info;
return io;
createNativeIo_failed:
if (handle != NULL) __PHYSFS_platformClose(handle);
if (pathdup != NULL) allocator.Free(pathdup);
if (info != NULL) allocator.Free(info);
if (io != NULL) allocator.Free(io);
return NULL;
} /* __PHYSFS_createNativeIo */
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* PHYSFS_Io implementation for i/o to a memory buffer... */
typedef struct __PHYSFS_MemoryIoInfo
{
const PHYSFS_uint8 *buf;
PHYSFS_uint64 len;
PHYSFS_uint64 pos;
PHYSFS_Io *parent;
volatile PHYSFS_uint32 refcount;
void (*destruct)(void *);
} MemoryIoInfo;
static PHYSFS_sint64 memoryIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
{
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
const PHYSFS_uint64 avail = info->len - info->pos;
assert(avail <= info->len);
if (avail == 0)
return 0; /* we're at EOF; nothing to do. */
if (len > avail)
len = avail;
254
memcpy(buf, info->buf + info->pos, (size_t) len);
255
256
257
258
259
260
261
info->pos += len;
return len;
} /* memoryIo_read */
static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
PHYSFS_uint64 len)
{
262
BAIL_MACRO(PHYSFS_ERR_OPEN_FOR_READING, -1);
263
264
265
266
267
} /* memoryIo_write */
static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
{
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
268
BAIL_IF_MACRO(offset > info->len, PHYSFS_ERR_PAST_EOF, 0);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
info->pos = offset;
return 1;
} /* memoryIo_seek */
static PHYSFS_sint64 memoryIo_tell(PHYSFS_Io *io)
{
const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
return (PHYSFS_sint64) info->pos;
} /* memoryIo_tell */
static PHYSFS_sint64 memoryIo_length(PHYSFS_Io *io)
{
const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
return (PHYSFS_sint64) info->len;
} /* memoryIo_length */
static PHYSFS_Io *memoryIo_duplicate(PHYSFS_Io *io)
{
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
MemoryIoInfo *newinfo = NULL;
PHYSFS_Io *parent = info->parent;
PHYSFS_Io *retval = NULL;
/* avoid deep copies. */
assert((!parent) || (!((MemoryIoInfo *) parent->opaque)->parent) );
/* share the buffer between duplicates. */
if (parent != NULL) /* dup the parent, increment its refcount. */
return parent->duplicate(parent);
/* we're the parent. */
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
302
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
303
304
305
306
newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
if (!newinfo)
{
allocator.Free(retval);
307
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
} /* if */
/* !!! FIXME: want lockless atomic increment. */
__PHYSFS_platformGrabMutex(stateLock);
info->refcount++;
__PHYSFS_platformReleaseMutex(stateLock);
memset(newinfo, '\0', sizeof (*info));
newinfo->buf = info->buf;
newinfo->len = info->len;
newinfo->pos = 0;
newinfo->parent = io;
newinfo->refcount = 0;
newinfo->destruct = NULL;
memcpy(retval, io, sizeof (*retval));
retval->opaque = newinfo;
return retval;
} /* memoryIo_duplicate */
static int memoryIo_flush(PHYSFS_Io *io) { return 1; /* it's read-only. */ }
static void memoryIo_destroy(PHYSFS_Io *io)
{
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
PHYSFS_Io *parent = info->parent;
int should_die = 0;
if (parent != NULL)
{
assert(info->buf == ((MemoryIoInfo *) info->parent->opaque)->buf);
assert(info->len == ((MemoryIoInfo *) info->parent->opaque)->len);
assert(info->refcount == 0);
assert(info->destruct == NULL);
allocator.Free(info);
allocator.Free(io);
parent->destroy(parent); /* decrements refcount. */
return;
} /* if */
/* we _are_ the parent. */
assert(info->refcount > 0); /* even in a race, we hold a reference. */
/* !!! FIXME: want lockless atomic decrement. */
__PHYSFS_platformGrabMutex(stateLock);
info->refcount--;
should_die = (info->refcount == 0);
__PHYSFS_platformReleaseMutex(stateLock);
if (should_die)
{
void (*destruct)(void *) = info->destruct;
void *buf = (void *) info->buf;
io->opaque = NULL; /* kill this here in case of race. */
allocator.Free(info);
allocator.Free(io);
if (destruct != NULL)
365
destruct(buf);
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
} /* if */
} /* memoryIo_destroy */
static const PHYSFS_Io __PHYSFS_memoryIoInterface =
{
memoryIo_read,
memoryIo_write,
memoryIo_seek,
memoryIo_tell,
memoryIo_length,
memoryIo_duplicate,
memoryIo_flush,
memoryIo_destroy,
NULL
};
PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
void (*destruct)(void *))
{
PHYSFS_Io *io = NULL;
MemoryIoInfo *info = NULL;
io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
390
GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
391
info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
392
GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
memset(info, '\0', sizeof (*info));
info->buf = (const PHYSFS_uint8 *) buf;
info->len = len;
info->pos = 0;
info->parent = NULL;
info->refcount = 1;
info->destruct = destruct;
memcpy(io, &__PHYSFS_memoryIoInterface, sizeof (*io));
io->opaque = info;
return io;
createMemoryIo_failed:
if (info != NULL) allocator.Free(info);
if (io != NULL) allocator.Free(io);
return NULL;
} /* __PHYSFS_createMemoryIo */
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* PHYSFS_Io implementation for i/o to a PHYSFS_File... */
static PHYSFS_sint64 handleIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
{
return PHYSFS_readBytes((PHYSFS_File *) io->opaque, buf, len);
} /* handleIo_read */
static PHYSFS_sint64 handleIo_write(PHYSFS_Io *io, const void *buffer,
PHYSFS_uint64 len)
{
return PHYSFS_writeBytes((PHYSFS_File *) io->opaque, buffer, len);
} /* handleIo_write */
static int handleIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
{
return PHYSFS_seek((PHYSFS_File *) io->opaque, offset);
} /* handleIo_seek */
static PHYSFS_sint64 handleIo_tell(PHYSFS_Io *io)
{
return PHYSFS_tell((PHYSFS_File *) io->opaque);
} /* handleIo_tell */
static PHYSFS_sint64 handleIo_length(PHYSFS_Io *io)
{
return PHYSFS_fileLength((PHYSFS_File *) io->opaque);
} /* handleIo_length */
static PHYSFS_Io *handleIo_duplicate(PHYSFS_Io *io)
{
/*
* There's no duplicate at the PHYSFS_File level, so we break the
* abstraction. We're allowed to: we're physfs.c!
*/
FileHandle *origfh = (FileHandle *) io->opaque;
FileHandle *newfh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
PHYSFS_Io *retval = NULL;
451
GOTO_IF_MACRO(!newfh, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
452
453
454
memset(newfh, '\0', sizeof (*newfh));
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
455
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
456
457
458
459
460
#if 0 /* we don't buffer the duplicate, at least not at the moment. */
if (origfh->buffer != NULL)
{
newfh->buffer = (PHYSFS_uint8 *) allocator.Malloc(origfh->bufsize);
461
462
if (!newfh->buffer)
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
463
464
465
466
467
newfh->bufsize = origfh->bufsize;
} /* if */
#endif
newfh->io = origfh->io->duplicate(origfh->io);
468
GOTO_IF_MACRO(!newfh->io, ERRPASS, handleIo_dupe_failed);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
newfh->forReading = origfh->forReading;
newfh->dirHandle = origfh->dirHandle;
__PHYSFS_platformGrabMutex(stateLock);
if (newfh->forReading)
{
newfh->next = openReadList;
openReadList = newfh;
} /* if */
else
{
newfh->next = openWriteList;
openWriteList = newfh;
} /* else */
__PHYSFS_platformReleaseMutex(stateLock);
memcpy(retval, io, sizeof (PHYSFS_Io));
retval->opaque = newfh;
return retval;
handleIo_dupe_failed:
if (newfh)
{
if (newfh->io != NULL) newfh->io->destroy(newfh->io);
if (newfh->buffer != NULL) allocator.Free(newfh->buffer);
allocator.Free(newfh);
} /* if */
return NULL;
} /* handleIo_duplicate */
static int handleIo_flush(PHYSFS_Io *io)
{
return PHYSFS_flush((PHYSFS_File *) io->opaque);
} /* handleIo_flush */
static void handleIo_destroy(PHYSFS_Io *io)
{
if (io->opaque != NULL)
PHYSFS_close((PHYSFS_File *) io->opaque);
allocator.Free(io);
} /* handleIo_destroy */
static const PHYSFS_Io __PHYSFS_handleIoInterface =
{
handleIo_read,
handleIo_write,
handleIo_seek,
handleIo_tell,
handleIo_length,
handleIo_duplicate,
handleIo_flush,
handleIo_destroy,
NULL
};
static PHYSFS_Io *__PHYSFS_createHandleIo(PHYSFS_File *f)
{
PHYSFS_Io *io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
529
BAIL_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
530
531
532
533
534
memcpy(io, &__PHYSFS_handleIoInterface, sizeof (*io));
io->opaque = f;
return io;
} /* __PHYSFS_createHandleIo */
535
536
537
/* functions ... */
538
539
540
541
typedef struct
{
char **list;
PHYSFS_uint32 size;
542
PHYSFS_ErrorCode errcode;
543
544
545
546
547
548
549
550
} EnumStringListCallbackData;
static void enumStringListCallback(void *data, const char *str)
{
void *ptr;
char *newstr;
EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
551
if (pecd->errcode)
552
553
return;
554
555
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = (char *) allocator.Malloc(strlen(str) + 1);
556
557
558
559
560
if (ptr != NULL)
pecd->list = (char **) ptr;
if ((ptr == NULL) || (newstr == NULL))
{
561
pecd->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
pecd->list[pecd->size] = NULL;
PHYSFS_freeList(pecd->list);
return;
} /* if */
strcpy(newstr, str);
pecd->list[pecd->size] = newstr;
pecd->size++;
} /* enumStringListCallback */
static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
{
EnumStringListCallbackData ecd;
memset(&ecd, '\0', sizeof (ecd));
577
ecd.list = (char **) allocator.Malloc(sizeof (char *));
578
BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
579
func(enumStringListCallback, &ecd);
580
581
582
583
584
585
586
if (ecd.errcode)
{
__PHYSFS_setError(ecd.errcode);
return NULL;
} /* if */
587
ecd.list[ecd.size] = NULL;
588
return ecd.list;
589
590
591
} /* doEnumStringList */
592
static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
int sorted;
do
{
sorted = 1;
for (i = lo; i < hi; i++)
{
if (cmpfn(a, i, i + 1) > 0)
{
swapfn(a, i, i + 1);
sorted = 0;
} /* if */
} /* for */
} while (!sorted);
} /* __PHYSFS_bubble_sort */
614
static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
615
616
617
618
619
620
621
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
PHYSFS_uint32 j;
PHYSFS_uint32 v;
622
if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
623
624
__PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
else
625
626
{
i = (hi + lo) / 2;
627
628
if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
629
630
631
632
633
634
635
636
637
638
639
640
if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
j = hi - 1;
swapfn(a, i, j);
i = lo;
v = j;
while (1)
{
while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
if (j < i)
641
break;
642
643
swapfn(a, i, j);
} /* while */
644
645
if (i != (hi-1))
swapfn(a, i, hi-1);
646
647
648
__PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
__PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
} /* else */
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
} /* __PHYSFS_quick_sort */
void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
/*
* Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
* http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
*/
__PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
} /* __PHYSFS_sort */
664
static ErrState *findErrorForCurrentThread(void)
665
{
666
ErrState *i;
667
void *tid;
668
669
if (errorLock != NULL)
670
671
__PHYSFS_platformGrabMutex(errorLock);
672
if (errorStates != NULL)
673
{
674
tid = __PHYSFS_platformGetThreadID();
675
676
for (i = errorStates; i != NULL; i = i->next)
677
678
{
if (i->tid == tid)
679
{
680
681
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
682
return i;
683
} /* if */
684
685
} /* for */
} /* if */
686
687
if (errorLock != NULL)
688
__PHYSFS_platformReleaseMutex(errorLock);
689
690
return NULL; /* no error available. */
691
692
693
} /* findErrorForCurrentThread */
694
void __PHYSFS_setError(const PHYSFS_ErrorCode errcode)
695
{
696
ErrState *err;
697
698
if (!errcode)
699
700
701
return;
err = findErrorForCurrentThread();
702
703
if (err == NULL)
{
704
err = (ErrState *) allocator.Malloc(sizeof (ErrState));
705
706
707
if (err == NULL)
return; /* uhh...? */
708
memset(err, '\0', sizeof (ErrState));
709
err->tid = __PHYSFS_platformGetThreadID();
710
711
712
713
if (errorLock != NULL)
__PHYSFS_platformGrabMutex(errorLock);
714
715
err->next = errorStates;
errorStates = err;
716
717
718
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
719
720
} /* if */
721
err->code = errcode;
722
} /* __PHYSFS_setError */
723
724
725
PHYSFS_ErrorCode PHYSFS_getLastErrorCode(void)
726
{
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
ErrState *err = findErrorForCurrentThread();
const PHYSFS_ErrorCode retval = (err) ? err->code : PHYSFS_ERR_OK;
if (err)
err->code = PHYSFS_ERR_OK;
return retval;
} /* PHYSFS_getLastErrorCode */
PHYSFS_DECL const char *PHYSFS_getErrorByCode(PHYSFS_ErrorCode code)
{
switch (code)
{
case PHYSFS_ERR_OK: return "no error";
case PHYSFS_ERR_OTHER_ERROR: return "unknown error";
case PHYSFS_ERR_OUT_OF_MEMORY: return "out of memory";
case PHYSFS_ERR_NOT_INITIALIZED: return "not initialized";
case PHYSFS_ERR_IS_INITIALIZED: return "already initialized";
case PHYSFS_ERR_ARGV0_IS_NULL: return "argv[0] is NULL";
case PHYSFS_ERR_UNSUPPORTED: return "unsupported";
case PHYSFS_ERR_PAST_EOF: return "past end of file";
case PHYSFS_ERR_FILES_STILL_OPEN: return "files still open";
case PHYSFS_ERR_INVALID_ARGUMENT: return "invalid argument";
case PHYSFS_ERR_NOT_MOUNTED: return "not mounted";
case PHYSFS_ERR_NO_SUCH_PATH: return "no such path";
case PHYSFS_ERR_SYMLINK_FORBIDDEN: return "symlinks are forbidden";
case PHYSFS_ERR_NO_WRITE_DIR: return "write directory is not set";
case PHYSFS_ERR_OPEN_FOR_READING: return "file open for reading";
case PHYSFS_ERR_OPEN_FOR_WRITING: return "file open for writing";
case PHYSFS_ERR_NOT_A_FILE: return "not a file";
case PHYSFS_ERR_READ_ONLY: return "read-only filesystem";
case PHYSFS_ERR_CORRUPT: return "corrupted";
case PHYSFS_ERR_SYMLINK_LOOP: return "infinite symbolic link loop";
case PHYSFS_ERR_IO: return "i/o error";
case PHYSFS_ERR_PERMISSION: return "permission denied";
case PHYSFS_ERR_NO_SPACE: return "no space available for writing";
case PHYSFS_ERR_BAD_FILENAME: return "filename is illegal or insecure";
case PHYSFS_ERR_BUSY: return "tried to modify a file the OS needs";
case PHYSFS_ERR_DIR_NOT_EMPTY: return "directory isn't empty";
case PHYSFS_ERR_OS_ERROR: return "OS reported an error";
} /* switch */
return NULL; /* don't know this error code. */
} /* PHYSFS_getErrorByCode */
void PHYSFS_setErrorCode(PHYSFS_ErrorCode code)
{
__PHYSFS_setError(code);
} /* PHYSFS_setErrorCode */
776
777
778
779
780
781
const char *PHYSFS_getLastError(void)
{
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
return (err) ? PHYSFS_getErrorByCode(err) : NULL;
782
783
784
785
} /* PHYSFS_getLastError */
/* MAKE SURE that errorLock is held before calling this! */
786
static void freeErrorStates(void)
787
{
788
789
ErrState *i;
ErrState *next;
790
791
for (i = errorStates; i != NULL; i = next)
792
{
793
next = i->next;
794
allocator.Free(i);
795
} /* for */
796
797
798
errorStates = NULL;
} /* freeErrorStates */
799
800
801
802
803
804
805
806
807
808
809
810
811
void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
{
if (ver != NULL)
{
ver->major = PHYSFS_VER_MAJOR;
ver->minor = PHYSFS_VER_MINOR;
ver->patch = PHYSFS_VER_PATCH;
} /* if */
} /* PHYSFS_getLinkedVersion */
812
813
static const char *find_filename_extension(const char *fname)
{
814
815
const char *retval = NULL;
if (fname != NULL)
816
{
817
818
const char *p = strchr(fname, '.');
retval = p;
819
820
821
822
823
824
825
826
827
828
829
while (p != NULL)
{
p = strchr(p + 1, '.');
if (p != NULL)
retval = p;
} /* while */
if (retval != NULL)
retval++; /* skip '.' */
} /* if */
830
831
return retval;
832
833
834
} /* find_filename_extension */
835
static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
836
const char *d, int forWriting)
837
838
{
DirHandle *retval = NULL;
839
840
841
void *opaque = NULL;
if (io != NULL)
842
BAIL_IF_MACRO(!io->seek(io, 0), ERRPASS, NULL);
843
844
opaque = funcs->openArchive(io, d, forWriting);
845
if (opaque != NULL)
846
{
847
848
retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
if (retval == NULL)
849
funcs->closeArchive(opaque);
850
else
851
{
852
853
854
855
856
memset(retval, '\0', sizeof (DirHandle));
retval->mountPoint = NULL;
retval->funcs = funcs;
retval->opaque = opaque;
} /* else */
857
858
} /* if */
859
return retval;
860
861
862
} /* tryOpenDir */
863
static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
864
{
865
DirHandle *retval = NULL;
866
const PHYSFS_Archiver **i;
867
const char *ext;
868
869
assert((io != NULL) || (d != NULL));
870
871
872
873
874
875
876
877
878
if (io == NULL)
{
/* DIR gets first shot (unlike the rest, it doesn't deal with files). */
retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting);
if (retval != NULL)
return retval;
io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
879
BAIL_IF_MACRO(!io, ERRPASS, 0);
880
} /* if */
881
882
883
ext = find_filename_extension(d);
if (ext != NULL)
884
{
885
/* Look for archivers with matching file extensions first... */
886
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
887
{
888
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) == 0)
889
retval = tryOpenDir(io, *i, d, forWriting);
890
891
892
} /* for */
/* failing an exact file extension match, try all the others... */
893
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
894
{
895
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) != 0)
896
retval = tryOpenDir(io, *i, d, forWriting);
897
898
899
900
901
} /* for */
} /* if */
else /* no extension? Try them all. */
{
902
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
903
retval = tryOpenDir(io, *i, d, forWriting);
904
} /* else */
905
906
BAIL_IF_MACRO(!retval, PHYSFS_ERR_UNSUPPORTED, NULL);
907
return retval;
908
909
910
} /* openDirectory */
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
/*
* Make a platform-independent path string sane. Doesn't actually check the
* file hierarchy, it just cleans up the string.
* (dst) must be a buffer at least as big as (src), as this is where the
* cleaned up string is deposited.
* If there are illegal bits in the path (".." entries, etc) then we
* return zero and (dst) is undefined. Non-zero if the path was sanitized.
*/
static int sanitizePlatformIndependentPath(const char *src, char *dst)
{
char *prev;
char ch;
while (*src == '/') /* skip initial '/' chars... */
src++;
prev = dst;
do
{
ch = *(src++);
if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
933
BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
934
935
936
937
938
if (ch == '/') /* path separator. */
{
*dst = '\0'; /* "." and ".." are illegal pathnames. */
if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
939
BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
940
941
942
943
944
945
946
947
948
949
950
951
952
while (*src == '/') /* chop out doubles... */
src++;
if (*src == '\0') /* ends with a pathsep? */
break; /* we're done, don't add final pathsep to dst. */
prev = dst + 1;
} /* if */
*(dst++) = ch;
} while (ch != '\0');
953
return 1;
954
955
956
} /* sanitizePlatformIndependentPath */
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*
* Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
* output from sanitizePlatformIndependentPath(), so that it is in a known
* state.
*
* This only finds legitimate segments of a mountpoint. If the mountpoint is
* "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
* all zero. "/a/b" will succeed, though.
*/
static int partOfMountPoint(DirHandle *h, char *fname)
{
/* !!! FIXME: This code feels gross. */
int rc;
size_t len, mntpntlen;
if (h->mountPoint == NULL)
973
return 0;
974
else if (*fname == '\0')
975
return 1;
976
977
978
979
len = strlen(fname);
mntpntlen = strlen(h->mountPoint);
if (len > mntpntlen) /* can't be a subset of mountpoint. */
980
return 0;
981
982
983
/* if true, must be not a match or a complete match, but not a subset. */
if ((len + 1) == mntpntlen)
984
return 0;
985
986
987
rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
if (rc != 0)
988
return 0; /* not a match. */
989
990
/* make sure /a/b matches /a/b/ and not /a/bc ... */
991
return h->mountPoint[len] == '/';
992
993
994
} /* partOfMountPoint */
995
996
static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
const char *mountPoint, int forWriting)
997
998
{
DirHandle *dirHandle = NULL;
999
char *tmpmntpnt = NULL;
1000