physfs_internal.h 28 KB

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