| 1 | /* |
| 2 | * Internal function/structure declaration. Do NOT include in your |
| 3 | * application. |
| 4 | * |
| 5 | * Please see the file LICENSE.txt in the source's root directory. |
| 6 | * |
| 7 | * This file written by Ryan C. Gordon. |
| 8 | */ |
| 9 | |
| 10 | #ifndef _INCLUDE_PHYSFS_INTERNAL_H_ |
| 11 | #define _INCLUDE_PHYSFS_INTERNAL_H_ |
| 12 | |
| 13 | #ifndef __PHYSICSFS_INTERNAL__ |
| 14 | #error Do not include this header from your applications. |
| 15 | #endif |
| 16 | |
| 17 | /* Turn off MSVC warnings that are aggressively anti-portability. */ |
| 18 | #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| 19 | #define _CRT_SECURE_NO_WARNINGS 1 |
| 20 | #endif |
| 21 | |
| 22 | #include "physfs.h" |
| 23 | |
| 24 | /* The holy trinity. */ |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "physfs_platforms.h" |
| 30 | |
| 31 | #include <assert.h> |
| 32 | |
| 33 | #define __PHYSFS_COMPILE_TIME_ASSERT(name, x) \ |
| 34 | typedef int __PHYSFS_compile_time_assert_##name[(x) * 2 - 1] |
| 35 | |
| 36 | /* !!! FIXME: remove this when revamping stack allocation code... */ |
| 37 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__WATCOMC__) |
| 38 | #include <malloc.h> |
| 39 | #endif |
| 40 | |
| 41 | #if defined(PHYSFS_PLATFORM_SOLARIS) || defined(PHYSFS_PLATFORM_LINUX) |
| 42 | #include <alloca.h> |
| 43 | #endif |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | #ifdef __GNUC__ |
| 50 | #define PHYSFS_MINIMUM_GCC_VERSION(major, minor) \ |
| 51 | ( ((__GNUC__ << 16) + __GNUC_MINOR__) >= (((major) << 16) + (minor)) ) |
| 52 | #else |
| 53 | #define PHYSFS_MINIMUM_GCC_VERSION(major, minor) (0) |
| 54 | #endif |
| 55 | |
| 56 | #ifdef __cplusplus |
| 57 | /* C++ always has a real inline keyword. */ |
| 58 | #elif (defined macintosh) && !(defined __MWERKS__) |
| 59 | # define inline |
| 60 | #elif (defined _MSC_VER) |
| 61 | # define inline __inline |
| 62 | #endif |
| 63 | |
| 64 | #if defined(PHYSFS_PLATFORM_LINUX) && !defined(_FILE_OFFSET_BITS) |
| 65 | #define _FILE_OFFSET_BITS 64 |
| 66 | #endif |
| 67 | |
| 68 | /* All public APIs need to be in physfs.h with a PHYSFS_DECL. |
| 69 | All file-private symbols need to be marked "static". |
| 70 | Everything shared between PhysicsFS sources needs to be in this |
| 71 | file between the visibility pragma blocks. */ |
| 72 | #if !defined(_WIN32) && (PHYSFS_MINIMUM_GCC_VERSION(4,0) || defined(__clang__)) |
| 73 | #define PHYSFS_HAVE_PRAGMA_VISIBILITY 1 |
| 74 | #endif |
| 75 | |
| 76 | #if PHYSFS_HAVE_PRAGMA_VISIBILITY |
| 77 | #pragma GCC visibility push(hidden) |
| 78 | #endif |
| 79 | |
| 80 | /* These are the build-in archivers. We list them all as "extern" here without |
| 81 | #ifdefs to keep it tidy, but obviously you need to make sure these are |
| 82 | wrapped in PHYSFS_SUPPORTS_* checks before actually referencing them. */ |
| 83 | extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR; |
| 84 | extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP; |
| 85 | extern const PHYSFS_Archiver __PHYSFS_Archiver_7Z; |
| 86 | extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP; |
| 87 | extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK; |
| 88 | extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG; |
| 89 | extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL; |
| 90 | extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD; |
| 91 | extern const PHYSFS_Archiver __PHYSFS_Archiver_SLB; |
| 92 | extern const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660; |
| 93 | extern const PHYSFS_Archiver __PHYSFS_Archiver_VDF; |
| 94 | |
| 95 | /* a real C99-compliant snprintf() is in Visual Studio 2015, |
| 96 | but just use this everywhere for binary compatibility. */ |
| 97 | #if defined(_MSC_VER) |
| 98 | #include <stdarg.h> |
| 99 | int __PHYSFS_msvc_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap); |
| 100 | int __PHYSFS_msvc_snprintf(char *outBuf, size_t size, const char *format, ...); |
| 101 | #define vsnprintf __PHYSFS_msvc_vsnprintf |
| 102 | #define snprintf __PHYSFS_msvc_snprintf |
| 103 | #endif |
| 104 | |
| 105 | /* Some simple wrappers around WinRT C++ interfaces we can call from C. */ |
| 106 | #ifdef PHYSFS_PLATFORM_WINRT |
| 107 | const void *__PHYSFS_winrtCalcBaseDir(void); |
| 108 | const void *__PHYSFS_winrtCalcPrefDir(void); |
| 109 | #endif |
| 110 | |
| 111 | /* atomic operations. */ |
| 112 | /* increment/decrement operations return the final incremented/decremented value. */ |
| 113 | #if defined(_MSC_VER) && (_MSC_VER >= 1500) |
| 114 | #include <intrin.h> |
| 115 | __PHYSFS_COMPILE_TIME_ASSERT(LongEqualsInt, sizeof (int) == sizeof (long)); |
| 116 | #define __PHYSFS_ATOMIC_INCR(ptrval) _InterlockedIncrement((long*)(ptrval)) |
| 117 | #define __PHYSFS_ATOMIC_DECR(ptrval) _InterlockedDecrement((long*)(ptrval)) |
| 118 | #elif defined(__clang__) || (defined(__GNUC__) && (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100)) >= 40100)) |
| 119 | #define __PHYSFS_ATOMIC_INCR(ptrval) __sync_add_and_fetch(ptrval, 1) |
| 120 | #define __PHYSFS_ATOMIC_DECR(ptrval) __sync_add_and_fetch(ptrval, -1) |
| 121 | #elif defined(__WATCOMC__) && defined(__386__) |
| 122 | extern __inline int _xadd_watcom(volatile int *a, int v); |
| 123 | #pragma aux _xadd_watcom = \ |
| 124 | "lock xadd [ecx], eax" \ |
| 125 | parm [ecx] [eax] \ |
| 126 | value [eax] \ |
| 127 | modify exact [eax]; |
| 128 | #define __PHYSFS_ATOMIC_INCR(ptrval) (_xadd_watcom(ptrval, 1)+1) |
| 129 | #define __PHYSFS_ATOMIC_DECR(ptrval) (_xadd_watcom(ptrval, -1)-1) |
| 130 | #else |
| 131 | #define PHYSFS_NEED_ATOMIC_OP_FALLBACK 1 |
| 132 | int __PHYSFS_ATOMIC_INCR(int *ptrval); |
| 133 | int __PHYSFS_ATOMIC_DECR(int *ptrval); |
| 134 | #endif |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * Interface for small allocations. If you need a little scratch space for |
| 139 | * a throwaway buffer or string, use this. It will make small allocations |
| 140 | * on the stack if possible, and use allocator.Malloc() if they are too |
| 141 | * large. This helps reduce malloc pressure. |
| 142 | * There are some rules, though: |
| 143 | * NEVER return a pointer from this, as stack-allocated buffers go away |
| 144 | * when your function returns. |
| 145 | * NEVER allocate in a loop, as stack-allocated pointers will pile up. Call |
| 146 | * a function that uses smallAlloc from your loop, so the allocation can |
| 147 | * free each time. |
| 148 | * NEVER call smallAlloc with any complex expression (it's a macro that WILL |
| 149 | * have side effects...it references the argument multiple times). Use a |
| 150 | * variable or a literal. |
| 151 | * NEVER free a pointer from this with anything but smallFree. It will not |
| 152 | * be a valid pointer to the allocator, regardless of where the memory came |
| 153 | * from. |
| 154 | * NEVER realloc a pointer from this. |
| 155 | * NEVER forget to use smallFree: it may not be a pointer from the stack. |
| 156 | * NEVER forget to check for NULL...allocation can fail here, of course! |
| 157 | */ |
| 158 | #define __PHYSFS_SMALLALLOCTHRESHOLD 256 |
| 159 | void *__PHYSFS_initSmallAlloc(void *ptr, const size_t len); |
| 160 | |
| 161 | #define __PHYSFS_smallAlloc(bytes) ( \ |
| 162 | __PHYSFS_initSmallAlloc( \ |
| 163 | (((bytes) < __PHYSFS_SMALLALLOCTHRESHOLD) ? \ |
| 164 | alloca((size_t)((bytes)+sizeof(void*))) : NULL), (bytes)) \ |
| 165 | ) |
| 166 | |
| 167 | void __PHYSFS_smallFree(void *ptr); |
| 168 | |
| 169 | |
| 170 | /* Use the allocation hooks. */ |
| 171 | #define malloc(x) Do not use malloc() directly. |
| 172 | #define realloc(x, y) Do not use realloc() directly. |
| 173 | #define free(x) Do not use free() directly. |
| 174 | /* !!! FIXME: add alloca check here. */ |
| 175 | |
| 176 | |
| 177 | /* by default, enable things, so builds can opt out of a few things they |
| 178 | want to avoid. But you can build with this #defined to 0 if you would |
| 179 | like to turn off everything except a handful of things you opt into. */ |
| 180 | #ifndef PHYSFS_SUPPORTS_DEFAULT |
| 181 | #define PHYSFS_SUPPORTS_DEFAULT 1 |
| 182 | #endif |
| 183 | |
| 184 | |
| 185 | #ifndef PHYSFS_SUPPORTS_ZIP |
| 186 | #define PHYSFS_SUPPORTS_ZIP PHYSFS_SUPPORTS_DEFAULT |
| 187 | #endif |
| 188 | #ifndef PHYSFS_SUPPORTS_7Z |
| 189 | #define PHYSFS_SUPPORTS_7Z PHYSFS_SUPPORTS_DEFAULT |
| 190 | #endif |
| 191 | #ifndef PHYSFS_SUPPORTS_GRP |
| 192 | #define PHYSFS_SUPPORTS_GRP PHYSFS_SUPPORTS_DEFAULT |
| 193 | #endif |
| 194 | #ifndef PHYSFS_SUPPORTS_HOG |
| 195 | #define PHYSFS_SUPPORTS_HOG PHYSFS_SUPPORTS_DEFAULT |
| 196 | #endif |
| 197 | #ifndef PHYSFS_SUPPORTS_MVL |
| 198 | #define PHYSFS_SUPPORTS_MVL PHYSFS_SUPPORTS_DEFAULT |
| 199 | #endif |
| 200 | #ifndef PHYSFS_SUPPORTS_WAD |
| 201 | #define PHYSFS_SUPPORTS_WAD PHYSFS_SUPPORTS_DEFAULT |
| 202 | #endif |
| 203 | #ifndef PHYSFS_SUPPORTS_QPAK |
| 204 | #define PHYSFS_SUPPORTS_QPAK PHYSFS_SUPPORTS_DEFAULT |
| 205 | #endif |
| 206 | #ifndef PHYSFS_SUPPORTS_SLB |
| 207 | #define PHYSFS_SUPPORTS_SLB PHYSFS_SUPPORTS_DEFAULT |
| 208 | #endif |
| 209 | #ifndef PHYSFS_SUPPORTS_ISO9660 |
| 210 | #define PHYSFS_SUPPORTS_ISO9660 PHYSFS_SUPPORTS_DEFAULT |
| 211 | #endif |
| 212 | #ifndef PHYSFS_SUPPORTS_VDF |
| 213 | #define PHYSFS_SUPPORTS_VDF PHYSFS_SUPPORTS_DEFAULT |
| 214 | #endif |
| 215 | |
| 216 | #if PHYSFS_SUPPORTS_7Z |
| 217 | /* 7zip support needs a global init function called at startup (no deinit). */ |
| 218 | extern void SZIP_global_init(void); |
| 219 | #endif |
| 220 | |
| 221 | /* The latest supported PHYSFS_Io::version value. */ |
| 222 | #define CURRENT_PHYSFS_IO_API_VERSION 0 |
| 223 | |
| 224 | /* The latest supported PHYSFS_Archiver::version value. */ |
| 225 | #define CURRENT_PHYSFS_ARCHIVER_API_VERSION 0 |
| 226 | |
| 227 | |
| 228 | /* This byteorder stuff was lifted from SDL. https://www.libsdl.org/ */ |
| 229 | #define PHYSFS_LIL_ENDIAN 1234 |
| 230 | #define PHYSFS_BIG_ENDIAN 4321 |
| 231 | |
| 232 | #ifdef __linux__ |
| 233 | #include <endian.h> |
| 234 | #define PHYSFS_BYTEORDER __BYTE_ORDER |
| 235 | #elif defined(__OpenBSD__) || defined(__DragonFly__) |
| 236 | #include <endian.h> |
| 237 | #define PHYSFS_BYTEORDER BYTE_ORDER |
| 238 | #elif defined(__FreeBSD__) || defined(__NetBSD__) |
| 239 | #include <sys/endian.h> |
| 240 | #define PHYSFS_BYTEORDER BYTE_ORDER |
| 241 | /* predefs from newer gcc and clang versions: */ |
| 242 | #elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__) |
| 243 | #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 244 | #define PHYSFS_BYTEORDER PHYSFS_LIL_ENDIAN |
| 245 | #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 246 | #define PHYSFS_BYTEORDER PHYSFS_BIG_ENDIAN |
| 247 | #else |
| 248 | #error Unsupported endianness |
| 249 | #endif /**/ |
| 250 | #else |
| 251 | #if defined(__hppa__) || \ |
| 252 | defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \ |
| 253 | (defined(__MIPS__) && defined(__MIPSEB__)) || \ |
| 254 | defined(__ppc__) || defined(__POWERPC__) || defined(__powerpc__) || defined(__PPC__) || \ |
| 255 | defined(__sparc__) |
| 256 | #define PHYSFS_BYTEORDER PHYSFS_BIG_ENDIAN |
| 257 | #else |
| 258 | #define PHYSFS_BYTEORDER PHYSFS_LIL_ENDIAN |
| 259 | #endif |
| 260 | #endif /* __linux__ */ |
| 261 | |
| 262 | |
| 263 | /* |
| 264 | * When sorting the entries in an archive, we use a modified QuickSort. |
| 265 | * When there are less then PHYSFS_QUICKSORT_THRESHOLD entries left to sort, |
| 266 | * we switch over to a BubbleSort for the remainder. Tweak to taste. |
| 267 | * |
| 268 | * You can override this setting by defining PHYSFS_QUICKSORT_THRESHOLD |
| 269 | * before #including "physfs_internal.h". |
| 270 | */ |
| 271 | #ifndef PHYSFS_QUICKSORT_THRESHOLD |
| 272 | #define PHYSFS_QUICKSORT_THRESHOLD 4 |
| 273 | #endif |
| 274 | |
| 275 | /* |
| 276 | * Sort an array (or whatever) of (max) elements. This uses a mixture of |
| 277 | * a QuickSort and BubbleSort internally. |
| 278 | * (cmpfn) is used to determine ordering, and (swapfn) does the actual |
| 279 | * swapping of elements in the list. |
| 280 | */ |
| 281 | void __PHYSFS_sort(void *entries, size_t max, |
| 282 | int (*cmpfn)(void *, size_t, size_t), |
| 283 | void (*swapfn)(void *, size_t, size_t)); |
| 284 | |
| 285 | /* These get used all over for lessening code clutter. */ |
| 286 | /* "ERRPASS" means "something else just set the error state for us" and is |
| 287 | just to make it clear where the responsibility for the error state lays. */ |
| 288 | #define BAIL(e, r) do { if (e) PHYSFS_setErrorCode(e); return r; } while (0) |
| 289 | #define BAIL_ERRPASS(r) do { return r; } while (0) |
| 290 | #define BAIL_IF(c, e, r) do { if (c) { if (e) PHYSFS_setErrorCode(e); return r; } } while (0) |
| 291 | #define BAIL_IF_ERRPASS(c, r) do { if (c) { return r; } } while (0) |
| 292 | #define BAIL_MUTEX(e, m, r) do { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); return r; } while (0) |
| 293 | #define BAIL_MUTEX_ERRPASS(m, r) do { __PHYSFS_platformReleaseMutex(m); return r; } while (0) |
| 294 | #define BAIL_IF_MUTEX(c, e, m, r) do { if (c) { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); return r; } } while (0) |
| 295 | #define BAIL_IF_MUTEX_ERRPASS(c, m, r) do { if (c) { __PHYSFS_platformReleaseMutex(m); return r; } } while (0) |
| 296 | #define GOTO(e, g) do { if (e) PHYSFS_setErrorCode(e); goto g; } while (0) |
| 297 | #define GOTO_ERRPASS(g) do { goto g; } while (0) |
| 298 | #define GOTO_IF(c, e, g) do { if (c) { if (e) PHYSFS_setErrorCode(e); goto g; } } while (0) |
| 299 | #define GOTO_IF_ERRPASS(c, g) do { if (c) { goto g; } } while (0) |
| 300 | #define GOTO_MUTEX(e, m, g) do { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); goto g; } while (0) |
| 301 | #define GOTO_MUTEX_ERRPASS(m, g) do { __PHYSFS_platformReleaseMutex(m); goto g; } while (0) |
| 302 | #define GOTO_IF_MUTEX(c, e, m, g) do { if (c) { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); goto g; } } while (0) |
| 303 | #define GOTO_IF_MUTEX_ERRPASS(c, m, g) do { if (c) { __PHYSFS_platformReleaseMutex(m); goto g; } } while (0) |
| 304 | |
| 305 | #define __PHYSFS_ARRAYLEN(x) ( (sizeof (x)) / (sizeof (x[0])) ) |
| 306 | |
| 307 | #ifdef PHYSFS_NO_64BIT_SUPPORT |
| 308 | #define __PHYSFS_SI64(x) ((PHYSFS_sint64) (x)) |
| 309 | #define __PHYSFS_UI64(x) ((PHYSFS_uint64) (x)) |
| 310 | #elif (defined __GNUC__) |
| 311 | #define __PHYSFS_SI64(x) x##LL |
| 312 | #define __PHYSFS_UI64(x) x##ULL |
| 313 | #elif (defined _MSC_VER) |
| 314 | #define __PHYSFS_SI64(x) x##i64 |
| 315 | #define __PHYSFS_UI64(x) x##ui64 |
| 316 | #else |
| 317 | #define __PHYSFS_SI64(x) ((PHYSFS_sint64) (x)) |
| 318 | #define __PHYSFS_UI64(x) ((PHYSFS_uint64) (x)) |
| 319 | #endif |
| 320 | |
| 321 | |
| 322 | /* |
| 323 | * Check if a ui64 will fit in the platform's address space. |
| 324 | * The initial sizeof check will optimize this macro out entirely on |
| 325 | * 64-bit (and larger?!) platforms, and the other condition will |
| 326 | * return zero or non-zero if the variable will fit in the platform's |
| 327 | * size_t, suitable to pass to malloc. This is kinda messy, but effective. |
| 328 | */ |
| 329 | #define __PHYSFS_ui64FitsAddressSpace(s) ( \ |
| 330 | (sizeof (PHYSFS_uint64) <= sizeof (size_t)) || \ |
| 331 | ((s) < (__PHYSFS_UI64(0xFFFFFFFFFFFFFFFF) >> (64-(sizeof(size_t)*8)))) \ |
| 332 | ) |
| 333 | |
| 334 | /* |
| 335 | * Like strdup(), but uses the current PhysicsFS allocator. |
| 336 | */ |
| 337 | char *__PHYSFS_strdup(const char *str); |
| 338 | |
| 339 | /* |
| 340 | * Give a hash value for a C string (uses djb's xor hashing algorithm). |
| 341 | */ |
| 342 | PHYSFS_uint32 __PHYSFS_hashString(const char *str); |
| 343 | |
| 344 | /* |
| 345 | * Give a hash value for a C string (uses djb's xor hashing algorithm), case folding as it goes. |
| 346 | */ |
| 347 | PHYSFS_uint32 __PHYSFS_hashStringCaseFold(const char *str); |
| 348 | |
| 349 | /* |
| 350 | * Give a hash value for a C string (uses djb's xor hashing algorithm), case folding as it goes, |
| 351 | * assuming that this is only US-ASCII chars (one byte per char, only 'A' through 'Z' need folding). |
| 352 | */ |
| 353 | PHYSFS_uint32 __PHYSFS_hashStringCaseFoldUSAscii(const char *str); |
| 354 | |
| 355 | |
| 356 | /* |
| 357 | * The current allocator. Not valid before PHYSFS_init is called! |
| 358 | */ |
| 359 | extern PHYSFS_Allocator __PHYSFS_AllocatorHooks; |
| 360 | |
| 361 | /* convenience macro to make this less cumbersome internally... */ |
| 362 | #define allocator __PHYSFS_AllocatorHooks |
| 363 | |
| 364 | /* |
| 365 | * Create a PHYSFS_Io for a file in the physical filesystem. |
| 366 | * This path is in platform-dependent notation. (mode) must be 'r', 'w', or |
| 367 | * 'a' for Read, Write, or Append. |
| 368 | */ |
| 369 | PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode); |
| 370 | |
| 371 | /* |
| 372 | * Create a PHYSFS_Io for a buffer of memory (READ-ONLY). If you already |
| 373 | * have one of these, just use its duplicate() method, and it'll increment |
| 374 | * its refcount without allocating a copy of the buffer. |
| 375 | */ |
| 376 | PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len, |
| 377 | void (*destruct)(void *)); |
| 378 | |
| 379 | |
| 380 | /* |
| 381 | * Read (len) bytes from (io) into (buf). Returns non-zero on success, |
| 382 | * zero on i/o error. Literally: "return (io->read(io, buf, len) == len);" |
| 383 | */ |
| 384 | int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const size_t len); |
| 385 | |
| 386 | |
| 387 | /* These are shared between some archivers. */ |
| 388 | |
| 389 | /* LOTS of legacy formats that only use US ASCII, not actually UTF-8, so let them optimize here. */ |
| 390 | void *UNPK_openArchive(PHYSFS_Io *io, const int case_sensitive, const int only_usascii); |
| 391 | void UNPK_abandonArchive(void *opaque); |
| 392 | void UNPK_closeArchive(void *opaque); |
| 393 | void *UNPK_addEntry(void *opaque, char *name, const int isdir, |
| 394 | const PHYSFS_sint64 ctime, const PHYSFS_sint64 mtime, |
| 395 | const PHYSFS_uint64 pos, const PHYSFS_uint64 len); |
| 396 | PHYSFS_Io *UNPK_openRead(void *opaque, const char *name); |
| 397 | PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name); |
| 398 | PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name); |
| 399 | int UNPK_remove(void *opaque, const char *name); |
| 400 | int UNPK_mkdir(void *opaque, const char *name); |
| 401 | int UNPK_stat(void *opaque, const char *fn, PHYSFS_Stat *st); |
| 402 | #define UNPK_enumerate __PHYSFS_DirTreeEnumerate |
| 403 | |
| 404 | |
| 405 | |
| 406 | /* Optional API many archivers use this to manage their directory tree. */ |
| 407 | /* !!! FIXME: document this better. */ |
| 408 | |
| 409 | typedef struct __PHYSFS_DirTreeEntry |
| 410 | { |
| 411 | char *name; /* Full path in archive. */ |
| 412 | struct __PHYSFS_DirTreeEntry *hashnext; /* next item in hash bucket. */ |
| 413 | struct __PHYSFS_DirTreeEntry *children; /* linked list of kids, if dir. */ |
| 414 | struct __PHYSFS_DirTreeEntry *sibling; /* next item in same dir. */ |
| 415 | int isdir; |
| 416 | } __PHYSFS_DirTreeEntry; |
| 417 | |
| 418 | typedef struct __PHYSFS_DirTree |
| 419 | { |
| 420 | __PHYSFS_DirTreeEntry *root; /* root of directory tree. */ |
| 421 | __PHYSFS_DirTreeEntry **hash; /* all entries hashed for fast lookup. */ |
| 422 | size_t hashBuckets; /* number of buckets in hash. */ |
| 423 | size_t entrylen; /* size in bytes of entries (including subclass). */ |
| 424 | int case_sensitive; /* non-zero to treat entries as case-sensitive in DirTreeFind */ |
| 425 | int only_usascii; /* non-zero to treat paths as US ASCII only (one byte per char, only 'A' through 'Z' are considered for case folding). */ |
| 426 | } __PHYSFS_DirTree; |
| 427 | |
| 428 | |
| 429 | /* LOTS of legacy formats that only use US ASCII, not actually UTF-8, so let them optimize here. */ |
| 430 | int __PHYSFS_DirTreeInit(__PHYSFS_DirTree *dt, const size_t entrylen, const int case_sensitive, const int only_usascii); |
| 431 | void *__PHYSFS_DirTreeAdd(__PHYSFS_DirTree *dt, char *name, const int isdir); |
| 432 | void *__PHYSFS_DirTreeFind(__PHYSFS_DirTree *dt, const char *path); |
| 433 | PHYSFS_EnumerateCallbackResult __PHYSFS_DirTreeEnumerate(void *opaque, |
| 434 | const char *dname, PHYSFS_EnumerateCallback cb, |
| 435 | const char *origdir, void *callbackdata); |
| 436 | void __PHYSFS_DirTreeDeinit(__PHYSFS_DirTree *dt); |
| 437 | |
| 438 | |
| 439 | |
| 440 | /*--------------------------------------------------------------------------*/ |
| 441 | /*--------------------------------------------------------------------------*/ |
| 442 | /*------------ ----------------*/ |
| 443 | /*------------ You MUST implement the following functions ----------------*/ |
| 444 | /*------------ if porting to a new platform. ----------------*/ |
| 445 | /*------------ (see platform/unix.c for an example) ----------------*/ |
| 446 | /*------------ ----------------*/ |
| 447 | /*--------------------------------------------------------------------------*/ |
| 448 | /*--------------------------------------------------------------------------*/ |
| 449 | |
| 450 | |
| 451 | /* |
| 452 | * The dir separator; '/' on unix, '\\' on win32, ":" on MacOS, etc... |
| 453 | * Obviously, this isn't a function. If you need more than one char for this, |
| 454 | * you'll need to pull some old pieces of PhysicsFS out of revision control. |
| 455 | */ |
| 456 | #if defined(PHYSFS_PLATFORM_WINDOWS) || defined(PHYSFS_PLATFORM_OS2) |
| 457 | #define __PHYSFS_platformDirSeparator '\\' |
| 458 | #else |
| 459 | #define __PHYSFS_STANDARD_DIRSEP 1 |
| 460 | #define __PHYSFS_platformDirSeparator '/' |
| 461 | #endif |
| 462 | |
| 463 | /* |
| 464 | * Initialize the platform. This is called when PHYSFS_init() is called from |
| 465 | * the application. |
| 466 | * |
| 467 | * Return zero if there was a catastrophic failure (which prevents you from |
| 468 | * functioning at all), and non-zero otherwise. |
| 469 | */ |
| 470 | int __PHYSFS_platformInit(void); |
| 471 | |
| 472 | |
| 473 | /* |
| 474 | * Deinitialize the platform. This is called when PHYSFS_deinit() is called |
| 475 | * from the application. You can use this to clean up anything you've |
| 476 | * allocated in your platform driver. |
| 477 | */ |
| 478 | void __PHYSFS_platformDeinit(void); |
| 479 | |
| 480 | |
| 481 | /* |
| 482 | * Open a file for reading. (filename) is in platform-dependent notation. The |
| 483 | * file pointer should be positioned on the first byte of the file. |
| 484 | * |
| 485 | * The return value will be some platform-specific datatype that is opaque to |
| 486 | * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32. |
| 487 | * |
| 488 | * The same file can be opened for read multiple times, and each should have |
| 489 | * a unique file handle; this is frequently employed to prevent race |
| 490 | * conditions in the archivers. |
| 491 | * |
| 492 | * Call PHYSFS_setErrorCode() and return (NULL) if the file can't be opened. |
| 493 | */ |
| 494 | void *__PHYSFS_platformOpenRead(const char *filename); |
| 495 | |
| 496 | |
| 497 | /* |
| 498 | * Open a file for writing. (filename) is in platform-dependent notation. If |
| 499 | * the file exists, it should be truncated to zero bytes, and if it doesn't |
| 500 | * exist, it should be created as a zero-byte file. The file pointer should |
| 501 | * be positioned on the first byte of the file. |
| 502 | * |
| 503 | * The return value will be some platform-specific datatype that is opaque to |
| 504 | * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32, |
| 505 | * etc. |
| 506 | * |
| 507 | * Opening a file for write multiple times has undefined results. |
| 508 | * |
| 509 | * Call PHYSFS_setErrorCode() and return (NULL) if the file can't be opened. |
| 510 | */ |
| 511 | void *__PHYSFS_platformOpenWrite(const char *filename); |
| 512 | |
| 513 | |
| 514 | /* |
| 515 | * Open a file for appending. (filename) is in platform-dependent notation. If |
| 516 | * the file exists, the file pointer should be place just past the end of the |
| 517 | * file, so that the first write will be one byte after the current end of |
| 518 | * the file. If the file doesn't exist, it should be created as a zero-byte |
| 519 | * file. The file pointer should be positioned on the first byte of the file. |
| 520 | * |
| 521 | * The return value will be some platform-specific datatype that is opaque to |
| 522 | * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32, |
| 523 | * etc. |
| 524 | * |
| 525 | * Opening a file for append multiple times has undefined results. |
| 526 | * |
| 527 | * Call PHYSFS_setErrorCode() and return (NULL) if the file can't be opened. |
| 528 | */ |
| 529 | void *__PHYSFS_platformOpenAppend(const char *filename); |
| 530 | |
| 531 | /* |
| 532 | * Read more data from a platform-specific file handle. (opaque) should be |
| 533 | * cast to whatever data type your platform uses. Read a maximum of (len) |
| 534 | * 8-bit bytes to the area pointed to by (buf). If there isn't enough data |
| 535 | * available, return the number of bytes read, and position the file pointer |
| 536 | * immediately after those bytes. |
| 537 | * On success, return (len) and position the file pointer immediately past |
| 538 | * the end of the last read byte. Return (-1) if there is a catastrophic |
| 539 | * error, and call PHYSFS_setErrorCode() to describe the problem; the file |
| 540 | * pointer should not move in such a case. A partial read is success; only |
| 541 | * return (-1) on total failure; presumably, the next read call after a |
| 542 | * partial read will fail as such. |
| 543 | */ |
| 544 | PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len); |
| 545 | |
| 546 | /* |
| 547 | * Write more data to a platform-specific file handle. (opaque) should be |
| 548 | * cast to whatever data type your platform uses. Write a maximum of (len) |
| 549 | * 8-bit bytes from the area pointed to by (buffer). If there is a problem, |
| 550 | * return the number of bytes written, and position the file pointer |
| 551 | * immediately after those bytes. Return (-1) if there is a catastrophic |
| 552 | * error, and call PHYSFS_setErrorCode() to describe the problem; the file |
| 553 | * pointer should not move in such a case. A partial write is success; only |
| 554 | * return (-1) on total failure; presumably, the next write call after a |
| 555 | * partial write will fail as such. |
| 556 | */ |
| 557 | PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer, |
| 558 | PHYSFS_uint64 len); |
| 559 | |
| 560 | /* |
| 561 | * Set the file pointer to a new position. (opaque) should be cast to |
| 562 | * whatever data type your platform uses. (pos) specifies the number |
| 563 | * of 8-bit bytes to seek to from the start of the file. Seeking past the |
| 564 | * end of the file is an error condition, and you should check for it. |
| 565 | * |
| 566 | * Not all file types can seek; this is to be expected by the caller. |
| 567 | * |
| 568 | * On error, call PHYSFS_setErrorCode() and return zero. On success, return |
| 569 | * a non-zero value. |
| 570 | */ |
| 571 | int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos); |
| 572 | |
| 573 | |
| 574 | /* |
| 575 | * Get the file pointer's position, in an 8-bit byte offset from the start of |
| 576 | * the file. (opaque) should be cast to whatever data type your platform |
| 577 | * uses. |
| 578 | * |
| 579 | * Not all file types can "tell"; this is to be expected by the caller. |
| 580 | * |
| 581 | * On error, call PHYSFS_setErrorCode() and return -1. On success, return >= 0. |
| 582 | */ |
| 583 | PHYSFS_sint64 __PHYSFS_platformTell(void *opaque); |
| 584 | |
| 585 | |
| 586 | /* |
| 587 | * Determine the current size of a file, in 8-bit bytes, from an open file. |
| 588 | * |
| 589 | * The caller expects that this information may not be available for all |
| 590 | * file types on all platforms. |
| 591 | * |
| 592 | * Return -1 if you can't do it, and call PHYSFS_setErrorCode(). Otherwise, |
| 593 | * return the file length in 8-bit bytes. |
| 594 | */ |
| 595 | PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle); |
| 596 | |
| 597 | |
| 598 | /* |
| 599 | * Read filesystem metadata for a specific path. |
| 600 | * |
| 601 | * This needs to fill in all the fields of (stat). For fields that might not |
| 602 | * mean anything on a platform (access time, perhaps), choose a reasonable |
| 603 | * default. if (follow), we want to follow symlinks and stat what they |
| 604 | * link to and not the link itself. |
| 605 | * |
| 606 | * Return zero on failure, non-zero on success. |
| 607 | */ |
| 608 | int __PHYSFS_platformStat(const char *fn, PHYSFS_Stat *stat, const int follow); |
| 609 | |
| 610 | /* |
| 611 | * Flush any pending writes to disk. (opaque) should be cast to whatever data |
| 612 | * type your platform uses. Be sure to check for errors; the caller expects |
| 613 | * that this function can fail if there was a flushing error, etc. |
| 614 | * |
| 615 | * Return zero on failure, non-zero on success. |
| 616 | */ |
| 617 | int __PHYSFS_platformFlush(void *opaque); |
| 618 | |
| 619 | /* |
| 620 | * Close file and deallocate resources. (opaque) should be cast to whatever |
| 621 | * data type your platform uses. This should close the file in any scenario: |
| 622 | * flushing is a separate function call, and this function should never fail. |
| 623 | * |
| 624 | * You should clean up all resources associated with (opaque); the pointer |
| 625 | * will be considered invalid after this call. |
| 626 | */ |
| 627 | void __PHYSFS_platformClose(void *opaque); |
| 628 | |
| 629 | /* |
| 630 | * Platform implementation of PHYSFS_getCdRomDirsCallback()... |
| 631 | * CD directories are discovered and reported to the callback one at a time. |
| 632 | * Pointers passed to the callback are assumed to be invalid to the |
| 633 | * application after the callback returns, so you can free them or whatever. |
| 634 | * Callback does not assume results will be sorted in any meaningful way. |
| 635 | */ |
| 636 | void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data); |
| 637 | |
| 638 | /* |
| 639 | * Calculate the base dir, if your platform needs special consideration. |
| 640 | * Just return NULL if the standard routines will suffice. (see |
| 641 | * calculateBaseDir() in physfs.c ...) |
| 642 | * Your string must end with a dir separator if you don't return NULL. |
| 643 | * Caller will allocator.Free() the retval if it's not NULL. |
| 644 | */ |
| 645 | char *__PHYSFS_platformCalcBaseDir(const char *argv0); |
| 646 | |
| 647 | /* |
| 648 | * Get the platform-specific user dir. |
| 649 | * As of PhysicsFS 2.1, returning NULL means fatal error. |
| 650 | * Your string must end with a dir separator if you don't return NULL. |
| 651 | * Caller will allocator.Free() the retval if it's not NULL. |
| 652 | */ |
| 653 | char *__PHYSFS_platformCalcUserDir(void); |
| 654 | |
| 655 | |
| 656 | /* This is the cached version from PHYSFS_init(). This is a fast call. */ |
| 657 | const char *__PHYSFS_getUserDir(void); /* not deprecated internal version. */ |
| 658 | |
| 659 | |
| 660 | /* |
| 661 | * Get the platform-specific pref dir. |
| 662 | * Returning NULL means fatal error. |
| 663 | * Your string must end with a dir separator if you don't return NULL. |
| 664 | * Caller will allocator.Free() the retval if it's not NULL. |
| 665 | * Caller will make missing directories if necessary; this just reports |
| 666 | * the final path. |
| 667 | */ |
| 668 | char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app); |
| 669 | |
| 670 | |
| 671 | /* |
| 672 | * Return a pointer that uniquely identifies the current thread. |
| 673 | * On a platform without threading, (0x1) will suffice. These numbers are |
| 674 | * arbitrary; the only requirement is that no two threads have the same |
| 675 | * pointer. |
| 676 | */ |
| 677 | void *__PHYSFS_platformGetThreadID(void); |
| 678 | |
| 679 | |
| 680 | /* |
| 681 | * Enumerate a directory of files. This follows the rules for the |
| 682 | * PHYSFS_Archiver::enumerate() method, except that the (dirName) that is |
| 683 | * passed to this function is converted to platform-DEPENDENT notation by |
| 684 | * the caller. The PHYSFS_Archiver version uses platform-independent |
| 685 | * notation. Note that ".", "..", and other meta-entries should always |
| 686 | * be ignored. |
| 687 | */ |
| 688 | PHYSFS_EnumerateCallbackResult (const char *dirname, |
| 689 | PHYSFS_EnumerateCallback callback, |
| 690 | const char *origdir, void *callbackdata); |
| 691 | |
| 692 | /* |
| 693 | * Make a directory in the actual filesystem. (path) is specified in |
| 694 | * platform-dependent notation. On error, return zero and set the error |
| 695 | * message. Return non-zero on success. |
| 696 | */ |
| 697 | int __PHYSFS_platformMkDir(const char *path); |
| 698 | |
| 699 | |
| 700 | /* |
| 701 | * Remove a file or directory entry in the actual filesystem. (path) is |
| 702 | * specified in platform-dependent notation. Note that this deletes files |
| 703 | * _and_ directories, so you might need to do some determination. |
| 704 | * Non-empty directories should report an error and not delete themselves |
| 705 | * or their contents. |
| 706 | * |
| 707 | * Deleting a symlink should remove the link, not what it points to. |
| 708 | * |
| 709 | * On error, return zero and set the error message. Return non-zero on success. |
| 710 | */ |
| 711 | int __PHYSFS_platformDelete(const char *path); |
| 712 | |
| 713 | |
| 714 | /* |
| 715 | * Create a platform-specific mutex. This can be whatever datatype your |
| 716 | * platform uses for mutexes, but it is cast to a (void *) for abstractness. |
| 717 | * |
| 718 | * Return (NULL) if you couldn't create one. Systems without threads can |
| 719 | * return any arbitrary non-NULL value. |
| 720 | */ |
| 721 | void *__PHYSFS_platformCreateMutex(void); |
| 722 | |
| 723 | /* |
| 724 | * Destroy a platform-specific mutex, and clean up any resources associated |
| 725 | * with it. (mutex) is a value previously returned by |
| 726 | * __PHYSFS_platformCreateMutex(). This can be a no-op on single-threaded |
| 727 | * platforms. |
| 728 | */ |
| 729 | void __PHYSFS_platformDestroyMutex(void *mutex); |
| 730 | |
| 731 | /* |
| 732 | * Grab possession of a platform-specific mutex. Mutexes should be recursive; |
| 733 | * that is, the same thread should be able to call this function multiple |
| 734 | * times in a row without causing a deadlock. This function should block |
| 735 | * until a thread can gain possession of the mutex. |
| 736 | * |
| 737 | * Return non-zero if the mutex was grabbed, zero if there was an |
| 738 | * unrecoverable problem grabbing it (this should not be a matter of |
| 739 | * timing out! We're talking major system errors; block until the mutex |
| 740 | * is available otherwise.) |
| 741 | * |
| 742 | * _DO NOT_ call PHYSFS_setErrorCode() in here! Since setErrorCode calls this |
| 743 | * function, you'll cause an infinite recursion. This means you can't |
| 744 | * use the BAIL_*MACRO* macros, either. |
| 745 | */ |
| 746 | int __PHYSFS_platformGrabMutex(void *mutex); |
| 747 | |
| 748 | /* |
| 749 | * Relinquish possession of the mutex when this method has been called |
| 750 | * once for each time that platformGrabMutex was called. Once possession has |
| 751 | * been released, the next thread in line to grab the mutex (if any) may |
| 752 | * proceed. |
| 753 | * |
| 754 | * _DO NOT_ call PHYSFS_setErrorCode() in here! Since setErrorCode calls this |
| 755 | * function, you'll cause an infinite recursion. This means you can't |
| 756 | * use the BAIL_*MACRO* macros, either. |
| 757 | */ |
| 758 | void __PHYSFS_platformReleaseMutex(void *mutex); |
| 759 | |
| 760 | |
| 761 | /* !!! FIXME: move to public API? */ |
| 762 | PHYSFS_uint32 __PHYSFS_utf8codepoint(const char **_str); |
| 763 | |
| 764 | |
| 765 | #if PHYSFS_HAVE_PRAGMA_VISIBILITY |
| 766 | #pragma GCC visibility pop |
| 767 | #endif |
| 768 | |
| 769 | #ifdef __cplusplus |
| 770 | } |
| 771 | #endif |
| 772 | |
| 773 | #endif |
| 774 | |
| 775 | /* end of physfs_internal.h ... */ |
| 776 | |
| 777 | |