physfs_internal.h 30 KB

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