physfs_internal.h 31 KB

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