/
physfs.c
2695 lines (2160 loc) · 73.4 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
12
/* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
18
19
20
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
21
22
23
typedef struct __PHYSFS_DIRHANDLE__
{
24
25
void *opaque; /* Instance data unique to the archiver. */
char *dirName; /* Path to archive in platform-dependent notation. */
26
char *mountPoint; /* Mountpoint in virtual file tree. */
27
28
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
29
30
31
} DirHandle;
32
33
typedef struct __PHYSFS_FILEHANDLE__
{
34
PHYSFS_Io *io; /* Instance data unique to the archiver for this file. */
35
36
37
38
39
40
41
42
43
44
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;
45
46
typedef struct __PHYSFS_ERRMSGTYPE__
{
47
void *tid;
48
49
50
51
52
int errorAvailable;
char errorString[80];
struct __PHYSFS_ERRMSGTYPE__ *next;
} ErrMsg;
53
54
/* The various i/o drivers...some of these may not be compiled in. */
55
56
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
57
58
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_LZMA;
extern const PHYSFS_Archiver __PHYSFS_Archiver_LZMA;
59
60
61
62
63
64
65
66
67
68
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
69
extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
70
71
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ISO9660;
extern const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660;
72
73
74
static const PHYSFS_ArchiveInfo *supported_types[] =
75
76
{
#if (defined PHYSFS_SUPPORTS_ZIP)
77
&__PHYSFS_ArchiveInfo_ZIP,
78
#endif
79
#if (defined PHYSFS_SUPPORTS_7Z)
80
81
&__PHYSFS_ArchiveInfo_LZMA,
#endif
82
83
84
#if (defined PHYSFS_SUPPORTS_GRP)
&__PHYSFS_ArchiveInfo_GRP,
#endif
85
86
87
#if (defined PHYSFS_SUPPORTS_QPAK)
&__PHYSFS_ArchiveInfo_QPAK,
#endif
88
89
90
91
92
93
#if (defined PHYSFS_SUPPORTS_HOG)
&__PHYSFS_ArchiveInfo_HOG,
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
&__PHYSFS_ArchiveInfo_MVL,
#endif
94
95
#if (defined PHYSFS_SUPPORTS_WAD)
&__PHYSFS_ArchiveInfo_WAD,
96
#endif
97
#if (defined PHYSFS_SUPPORTS_ISO9660)
98
&__PHYSFS_ArchiveInfo_ISO9660,
99
#endif
100
101
102
NULL
};
103
static const PHYSFS_Archiver *archivers[] =
104
105
{
#if (defined PHYSFS_SUPPORTS_ZIP)
106
&__PHYSFS_Archiver_ZIP,
107
#endif
108
#if (defined PHYSFS_SUPPORTS_7Z)
109
110
&__PHYSFS_Archiver_LZMA,
#endif
111
#if (defined PHYSFS_SUPPORTS_GRP)
112
&__PHYSFS_Archiver_GRP,
113
#endif
114
#if (defined PHYSFS_SUPPORTS_QPAK)
115
&__PHYSFS_Archiver_QPAK,
116
#endif
117
#if (defined PHYSFS_SUPPORTS_HOG)
118
&__PHYSFS_Archiver_HOG,
119
120
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
121
&__PHYSFS_Archiver_MVL,
122
#endif
123
#if (defined PHYSFS_SUPPORTS_WAD)
124
&__PHYSFS_Archiver_WAD,
125
#endif
126
#if (defined PHYSFS_SUPPORTS_ISO9660)
127
&__PHYSFS_Archiver_ISO9660,
128
#endif
129
130
NULL
};
131
132
133
134
135
136
/* General PhysicsFS state ... */
static int initialized = 0;
static ErrMsg *errorMessages = NULL;
137
138
139
140
static DirHandle *searchPath = NULL;
static DirHandle *writeDir = NULL;
static FileHandle *openWriteList = NULL;
static FileHandle *openReadList = NULL;
141
142
143
144
static char *baseDir = NULL;
static char *userDir = NULL;
static int allowSymLinks = 0;
145
146
147
/* mutexes ... */
static void *errorLock = NULL; /* protects error message list. */
static void *stateLock = NULL; /* protects other PhysFS static state. */
148
149
150
/* allocator ... */
static int externalAllocator = 0;
151
PHYSFS_Allocator allocator;
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* 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));
GOTO_IF_MACRO(io == NULL, ERR_OUT_OF_MEMORY, createNativeIo_failed);
info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, createNativeIo_failed);
pathdup = (char *) allocator.Malloc(strlen(path) + 1);
GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, createNativeIo_failed);
if (mode == 'r')
handle = __PHYSFS_platformOpenRead(path);
else if (mode == 'w')
handle = __PHYSFS_platformOpenWrite(path);
else if (mode == 'a')
handle = __PHYSFS_platformOpenAppend(path);
GOTO_IF_MACRO(handle == NULL, NULL, createNativeIo_failed);
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 */
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
302
303
304
305
306
307
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
451
452
453
454
/* 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;
memcpy(buf, info->buf + info->pos, len);
info->pos += len;
return len;
} /* memoryIo_read */
static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
PHYSFS_uint64 len)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* memoryIo_write */
static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
{
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
BAIL_IF_MACRO(offset > info->len, ERR_PAST_EOF, 0);
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));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
if (!newinfo)
{
allocator.Free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* 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. */
destruct = info->destruct;
allocator.Free(info);
allocator.Free(io);
if (destruct != NULL)
destruct(buf);
} /* 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));
GOTO_IF_MACRO(io == NULL, ERR_OUT_OF_MEMORY, createMemoryIo_failed);
info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, createMemoryIo_failed);
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 */
455
456
/* functions ... */
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
typedef struct
{
char **list;
PHYSFS_uint32 size;
const char *errorstr;
} EnumStringListCallbackData;
static void enumStringListCallback(void *data, const char *str)
{
void *ptr;
char *newstr;
EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
if (pecd->errorstr)
return;
473
474
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = (char *) allocator.Malloc(strlen(str) + 1);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
if (ptr != NULL)
pecd->list = (char **) ptr;
if ((ptr == NULL) || (newstr == NULL))
{
pecd->errorstr = ERR_OUT_OF_MEMORY;
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));
496
ecd.list = (char **) allocator.Malloc(sizeof (char *));
497
498
499
500
BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
func(enumStringListCallback, &ecd);
BAIL_IF_MACRO(ecd.errorstr != NULL, ecd.errorstr, NULL);
ecd.list[ecd.size] = NULL;
501
return ecd.list;
502
503
504
} /* doEnumStringList */
505
static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
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 */
527
static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
528
529
530
531
532
533
534
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
PHYSFS_uint32 j;
PHYSFS_uint32 v;
535
if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
536
537
__PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
else
538
539
{
i = (hi + lo) / 2;
540
541
if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
542
543
544
545
546
547
548
549
550
551
552
553
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)
554
break;
555
556
swapfn(a, i, j);
} /* while */
557
558
if (i != (hi-1))
swapfn(a, i, hi-1);
559
560
561
__PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
__PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
} /* else */
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
} /* __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 */
577
578
579
static ErrMsg *findErrorForCurrentThread(void)
{
ErrMsg *i;
580
void *tid;
581
582
if (errorLock != NULL)
583
584
__PHYSFS_platformGrabMutex(errorLock);
585
586
if (errorMessages != NULL)
{
587
tid = __PHYSFS_platformGetThreadID();
588
589
590
591
for (i = errorMessages; i != NULL; i = i->next)
{
if (i->tid == tid)
592
{
593
594
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
595
return i;
596
} /* if */
597
598
} /* for */
} /* if */
599
600
if (errorLock != NULL)
601
__PHYSFS_platformReleaseMutex(errorLock);
602
603
return NULL; /* no error available. */
604
605
606
} /* findErrorForCurrentThread */
607
void __PHYSFS_setError(const char *str)
608
{
609
610
611
612
613
614
ErrMsg *err;
if (str == NULL)
return;
err = findErrorForCurrentThread();
615
616
617
if (err == NULL)
{
618
err = (ErrMsg *) allocator.Malloc(sizeof (ErrMsg));
619
620
621
if (err == NULL)
return; /* uhh...? */
622
memset((void *) err, '\0', sizeof (ErrMsg));
623
err->tid = __PHYSFS_platformGetThreadID();
624
625
626
627
if (errorLock != NULL)
__PHYSFS_platformGrabMutex(errorLock);
628
629
err->next = errorMessages;
errorMessages = err;
630
631
632
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
633
634
635
636
637
} /* if */
err->errorAvailable = 1;
strncpy(err->errorString, str, sizeof (err->errorString));
err->errorString[sizeof (err->errorString) - 1] = '\0';
638
} /* __PHYSFS_setError */
639
640
641
642
643
644
645
const char *PHYSFS_getLastError(void)
{
ErrMsg *err = findErrorForCurrentThread();
if ((err == NULL) || (!err->errorAvailable))
646
return NULL;
647
648
err->errorAvailable = 0;
649
return err->errorString;
650
651
652
653
} /* PHYSFS_getLastError */
/* MAKE SURE that errorLock is held before calling this! */
654
655
656
657
658
659
660
static void freeErrorMessages(void)
{
ErrMsg *i;
ErrMsg *next;
for (i = errorMessages; i != NULL; i = next)
{
661
next = i->next;
662
allocator.Free(i);
663
} /* for */
664
665
errorMessages = NULL;
666
667
668
} /* freeErrorMessages */
669
670
671
672
673
674
675
676
677
678
679
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 */
680
681
static const char *find_filename_extension(const char *fname)
{
682
683
const char *retval = NULL;
if (fname != NULL)
684
{
685
686
retval = strchr(fname, '.');
const char *p = retval;
687
688
689
690
691
692
693
694
695
696
697
while (p != NULL)
{
p = strchr(p + 1, '.');
if (p != NULL)
retval = p;
} /* while */
if (retval != NULL)
retval++; /* skip '.' */
} /* if */
698
699
return retval;
700
701
702
} /* find_filename_extension */
703
static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
704
const char *d, int forWriting)
705
706
{
DirHandle *retval = NULL;
707
708
709
710
711
712
void *opaque = NULL;
if (io != NULL)
BAIL_IF_MACRO(!io->seek(io, 0), NULL, NULL);
opaque = funcs->openArchive(io, d, forWriting);
713
if (opaque != NULL)
714
{
715
716
717
718
retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
if (retval == NULL)
funcs->dirClose(opaque);
else
719
{
720
721
722
723
724
memset(retval, '\0', sizeof (DirHandle));
retval->mountPoint = NULL;
retval->funcs = funcs;
retval->opaque = opaque;
} /* else */
725
726
} /* if */
727
return retval;
728
729
730
} /* tryOpenDir */
731
static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
732
{
733
DirHandle *retval = NULL;
734
const PHYSFS_Archiver **i;
735
const char *ext;
736
737
assert((io != NULL) || (d != NULL));
738
739
740
741
742
743
744
745
746
747
748
749
750
if (io == NULL)
{
BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, 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');
BAIL_IF_MACRO_MUTEX(io == NULL, NULL, stateLock, 0);
} /* if */
751
752
753
ext = find_filename_extension(d);
if (ext != NULL)
754
{
755
/* Look for archivers with matching file extensions first... */
756
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
757
{
758
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) == 0)
759
retval = tryOpenDir(io, *i, d, forWriting);
760
761
762
} /* for */
/* failing an exact file extension match, try all the others... */
763
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
764
{
765
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) != 0)
766
retval = tryOpenDir(io, *i, d, forWriting);
767
768
769
770
771
} /* for */
} /* if */
else /* no extension? Try them all. */
{
772
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
773
retval = tryOpenDir(io, *i, d, forWriting);
774
} /* else */
775
776
BAIL_IF_MACRO(retval == NULL, ERR_UNSUPPORTED_ARCHIVE, NULL);
777
return retval;
778
779
780
} /* openDirectory */
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
* 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. */
BAIL_MACRO(ERR_INSECURE_FNAME, 0);
if (ch == '/') /* path separator. */
{
*dst = '\0'; /* "." and ".." are illegal pathnames. */
if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
BAIL_MACRO(ERR_INSECURE_FNAME, 0);
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');
823
return 1;
824
825
826
} /* sanitizePlatformIndependentPath */
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
* 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)
843
return 0;
844
else if (*fname == '\0')
845
return 1;
846
847
848
849
len = strlen(fname);
mntpntlen = strlen(h->mountPoint);
if (len > mntpntlen) /* can't be a subset of mountpoint. */
850
return 0;
851
852
853
/* if true, must be not a match or a complete match, but not a subset. */
if ((len + 1) == mntpntlen)
854
return 0;
855
856
857
rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
if (rc != 0)
858
return 0; /* not a match. */
859
860
/* make sure /a/b matches /a/b/ and not /a/bc ... */
861
return h->mountPoint[len] == '/';
862
863
864
} /* partOfMountPoint */
865
866
static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
const char *mountPoint, int forWriting)
867
868
{
DirHandle *dirHandle = NULL;
869
char *tmpmntpnt = NULL;
870
871
872
if (mountPoint != NULL)
{
873
874
875
876
const size_t len = strlen(mountPoint) + 1;
tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
GOTO_IF_MACRO(!tmpmntpnt, ERR_OUT_OF_MEMORY, badDirHandle);
if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
877
goto badDirHandle;
878
mountPoint = tmpmntpnt; /* sanitized version. */
879
} /* if */
880
881
dirHandle = openDirectory(io, newDir, forWriting);
882
GOTO_IF_MACRO(!dirHandle, NULL, badDirHandle);
883
884
885
886
887
888
889
890
891
if (newDir == NULL)
dirHandle->dirName = NULL;
else
{
dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
GOTO_IF_MACRO(!dirHandle->dirName, ERR_OUT_OF_MEMORY, badDirHandle);
strcpy(dirHandle->dirName, newDir);
} /* else */
892
893
894
if ((mountPoint != NULL) && (*mountPoint != '\0'))
{
895
dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
896
897
898
899
900
GOTO_IF_MACRO(!dirHandle->mountPoint, ERR_OUT_OF_MEMORY, badDirHandle);
strcpy(dirHandle->mountPoint, mountPoint);
strcat(dirHandle->mountPoint, "/");
} /* if */
901
__PHYSFS_smallFree(tmpmntpnt);
902
return dirHandle;
903
904
905
badDirHandle:
if (dirHandle != NULL)
906
{
907
dirHandle->funcs->dirClose(dirHandle->opaque);
908
909
910
allocator.Free(dirHandle->dirName);
allocator.Free(dirHandle->mountPoint);
allocator.Free(dirHandle);
911
} /* if */
912
913
__PHYSFS_smallFree(tmpmntpnt);
914
return NULL;
915
} /* createDirHandle */
916
917
918
/* MAKE SURE you've got the stateLock held before calling this! */
919
static int freeDirHandle(DirHandle *dh, FileHandle *openList)
920
{
921
FileHandle *i;
922
923
if (dh == NULL)
924
return 1;
925
926
for (i = openList; i != NULL; i = i->next)
927
BAIL_IF_MACRO(i->dirHandle == dh, ERR_FILES_STILL_OPEN, 0);
928
929
dh->funcs->dirClose(dh->opaque);
930
931
932
allocator.Free(dh->dirName);
allocator.Free(dh->mountPoint);
allocator.Free(dh);
933
return 1;
934
} /* freeDirHandle */
935
936
937
static char *calculateUserDir(void)
938
{
939
940
941
942
943
944
945
946
char *retval = __PHYSFS_platformGetUserDir();
if (retval != NULL)
{
/* make sure it really exists and is normalized. */
char *ptr = __PHYSFS_platformRealPath(retval);
allocator.Free(retval);
retval = ptr;
} /* if */
947
948
if (retval == NULL)
949
950
951
{
const char *dirsep = PHYSFS_getDirSeparator();
const char *uname = __PHYSFS_platformGetUserName();
952
const char *str = (uname != NULL) ? uname : "default";
953
954
955
retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
strlen(dirsep) + 6);
956
957
958
959
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
else
960
sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
961
962
allocator.Free((void *) uname);
963
964
} /* else */
965
return retval;
966
967
968
} /* calculateUserDir */
969
970
971
972
973
974
static int appendDirSep(char **dir)
{
const char *dirsep = PHYSFS_getDirSeparator();
char *ptr;
if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
975
return 1;
976
977
ptr = (char *) allocator.Realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
978
979
if (!ptr)
{
980
allocator.Free(*dir);
981
return 0;
982
983
984
985
} /* if */
strcat(ptr, dirsep);
*dir = ptr;
986
return 1;
987
988
989
} /* appendDirSep */
990
991
static char *calculateBaseDir(const char *argv0)
{
992
993
994
char *retval = NULL;
const char *dirsep = NULL;
char *ptr = NULL;
995
996
/* Give the platform layer first shot at this. */
997
998
retval = __PHYSFS_platformCalcBaseDir(argv0);
if (retval != NULL)
999
return retval;
1000