physfs_internal.h 27 KB

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