physfs_internal.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. #include "physfs.h"
  15. /* The holy trinity. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "physfs_platforms.h"
  20. #include <assert.h>
  21. /* !!! FIXME: remove this when revamping stack allocation code... */
  22. #if defined(_MSC_VER) || defined(__MINGW32__)
  23. #include <malloc.h>
  24. #endif
  25. #if PHYSFS_PLATFORM_SOLARIS
  26. #include <alloca.h>
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #ifdef __GNUC__
  32. #define PHYSFS_MINIMUM_GCC_VERSION(major, minor) \
  33. ( ((__GNUC__ << 16) + __GNUC_MINOR__) >= (((major) << 16) + (minor)) )
  34. #else
  35. #define PHYSFS_MINIMUM_GCC_VERSION(major, minor) (0)
  36. #endif
  37. #ifdef __cplusplus
  38. /* C++ always has a real inline keyword. */
  39. #elif (defined macintosh) && !(defined __MWERKS__)
  40. # define inline
  41. #elif (defined _MSC_VER)
  42. # define inline __inline
  43. #endif
  44. #if PHYSFS_PLATFORM_LINUX && !defined(_FILE_OFFSET_BITS)
  45. #define _FILE_OFFSET_BITS 64
  46. #endif
  47. /*
  48. * Interface for small allocations. If you need a little scratch space for
  49. * a throwaway buffer or string, use this. It will make small allocations
  50. * on the stack if possible, and use allocator.Malloc() if they are too
  51. * large. This helps reduce malloc pressure.
  52. * There are some rules, though:
  53. * NEVER return a pointer from this, as stack-allocated buffers go away
  54. * when your function returns.
  55. * NEVER allocate in a loop, as stack-allocated pointers will pile up. Call
  56. * a function that uses smallAlloc from your loop, so the allocation can
  57. * free each time.
  58. * NEVER call smallAlloc with any complex expression (it's a macro that WILL
  59. * have side effects...it references the argument multiple times). Use a
  60. * variable or a literal.
  61. * NEVER free a pointer from this with anything but smallFree. It will not
  62. * be a valid pointer to the allocator, regardless of where the memory came
  63. * from.
  64. * NEVER realloc a pointer from this.
  65. * NEVER forget to use smallFree: it may not be a pointer from the stack.
  66. * NEVER forget to check for NULL...allocation can fail here, of course!
  67. */
  68. #define __PHYSFS_SMALLALLOCTHRESHOLD 256
  69. void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len);
  70. #define __PHYSFS_smallAlloc(bytes) ( \
  71. __PHYSFS_initSmallAlloc( \
  72. (((bytes) < __PHYSFS_SMALLALLOCTHRESHOLD) ? \
  73. alloca((size_t)((bytes)+sizeof(void*))) : NULL), (bytes)) \
  74. )
  75. void __PHYSFS_smallFree(void *ptr);
  76. /* Use the allocation hooks. */
  77. #define malloc(x) Do not use malloc() directly.
  78. #define realloc(x, y) Do not use realloc() directly.
  79. #define free(x) Do not use free() directly.
  80. /* !!! FIXME: add alloca check here. */
  81. #ifndef PHYSFS_SUPPORTS_ZIP
  82. #define PHYSFS_SUPPORTS_ZIP 1
  83. #endif
  84. #ifndef PHYSFS_SUPPORTS_7Z
  85. #define PHYSFS_SUPPORTS_7Z 0
  86. #endif
  87. #ifndef PHYSFS_SUPPORTS_GRP
  88. #define PHYSFS_SUPPORTS_GRP 0
  89. #endif
  90. #ifndef PHYSFS_SUPPORTS_HOG
  91. #define PHYSFS_SUPPORTS_HOG 0
  92. #endif
  93. #ifndef PHYSFS_SUPPORTS_MVL
  94. #define PHYSFS_SUPPORTS_MVL 0
  95. #endif
  96. #ifndef PHYSFS_SUPPORTS_WAD
  97. #define PHYSFS_SUPPORTS_WAD 0
  98. #endif
  99. #ifndef PHYSFS_SUPPORTS_SLB
  100. #define PHYSFS_SUPPORTS_SLB 0
  101. #endif
  102. #ifndef PHYSFS_SUPPORTS_ISO9660
  103. #define PHYSFS_SUPPORTS_ISO9660 0
  104. #endif
  105. /* The latest supported PHYSFS_Io::version value. */
  106. #define CURRENT_PHYSFS_IO_API_VERSION 0
  107. /* Opaque data for file and dir handlers... */
  108. typedef void PHYSFS_Dir;
  109. typedef struct
  110. {
  111. /*
  112. * Basic info about this archiver...
  113. */
  114. const PHYSFS_ArchiveInfo info;
  115. /*
  116. * DIRECTORY ROUTINES:
  117. * These functions are for dir handles. Generate a handle with the
  118. * openArchive() method, then pass it as the "opaque" PHYSFS_Dir to the
  119. * others.
  120. *
  121. * Symlinks should always be followed (except in stat()); PhysicsFS will
  122. * use the stat() method to check for symlinks and make a judgement on
  123. * whether to continue to call other methods based on that.
  124. */
  125. /*
  126. * Open a dirhandle for dir/archive data provided by (io).
  127. * (name) is a filename associated with (io), but doesn't necessarily
  128. * map to anything, let alone a real filename. This possibly-
  129. * meaningless name is in platform-dependent notation.
  130. * (forWrite) is non-zero if this is to be used for
  131. * the write directory, and zero if this is to be used for an
  132. * element of the search path.
  133. * Returns NULL on failure. We ignore any error code you set here.
  134. * Returns non-NULL on success. The pointer returned will be
  135. * passed as the "opaque" parameter for later calls.
  136. */
  137. PHYSFS_Dir *(*openArchive)(PHYSFS_Io *io, const char *name, int forWrite);
  138. /*
  139. * List all files in (dirname). Each file is passed to (cb),
  140. * where a copy is made if appropriate, so you should dispose of
  141. * it properly upon return from the callback.
  142. * You should omit symlinks if (omitSymLinks) is non-zero.
  143. * If you have a failure, report as much as you can.
  144. * (dirname) is in platform-independent notation.
  145. */
  146. void (*enumerateFiles)(PHYSFS_Dir *opaque, const char *dirname,
  147. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  148. const char *origdir, void *callbackdata);
  149. /*
  150. * Open file for reading.
  151. * This filename, (fnm), is in platform-independent notation.
  152. * If you can't handle multiple opens of the same file,
  153. * you can opt to fail for the second call.
  154. * Fail if the file does not exist.
  155. * Returns NULL on failure, and calls __PHYSFS_setError().
  156. * Returns non-NULL on success. The pointer returned will be
  157. * passed as the "opaque" parameter for later file calls.
  158. *
  159. * Regardless of success or failure, please set *exists to
  160. * non-zero if the file existed (even if it's a broken symlink!),
  161. * zero if it did not.
  162. */
  163. PHYSFS_Io *(*openRead)(PHYSFS_Dir *opaque, const char *fnm, int *exists);
  164. /*
  165. * Open file for writing.
  166. * If the file does not exist, it should be created. If it exists,
  167. * it should be truncated to zero bytes. The writing
  168. * offset should be the start of the file.
  169. * This filename is in platform-independent notation.
  170. * If you can't handle multiple opens of the same file,
  171. * you can opt to fail for the second call.
  172. * Returns NULL on failure, and calls __PHYSFS_setError().
  173. * Returns non-NULL on success. The pointer returned will be
  174. * passed as the "opaque" parameter for later file calls.
  175. */
  176. PHYSFS_Io *(*openWrite)(PHYSFS_Dir *opaque, const char *filename);
  177. /*
  178. * Open file for appending.
  179. * If the file does not exist, it should be created. The writing
  180. * offset should be the end of the file.
  181. * This filename is in platform-independent notation.
  182. * If you can't handle multiple opens of the same file,
  183. * you can opt to fail for the second call.
  184. * Returns NULL on failure, and calls __PHYSFS_setError().
  185. * Returns non-NULL on success. The pointer returned will be
  186. * passed as the "opaque" parameter for later file calls.
  187. */
  188. PHYSFS_Io *(*openAppend)(PHYSFS_Dir *opaque, const char *filename);
  189. /*
  190. * Delete a file in the archive/directory.
  191. * Return non-zero on success, zero on failure.
  192. * This filename is in platform-independent notation.
  193. * This method may be NULL.
  194. * On failure, call __PHYSFS_setError().
  195. */
  196. int (*remove)(PHYSFS_Dir *opaque, const char *filename);
  197. /*
  198. * Create a directory in the archive/directory.
  199. * If the application is trying to make multiple dirs, PhysicsFS
  200. * will split them up into multiple calls before passing them to
  201. * your driver.
  202. * Return non-zero on success, zero on failure.
  203. * This filename is in platform-independent notation.
  204. * This method may be NULL.
  205. * On failure, call __PHYSFS_setError().
  206. */
  207. int (*mkdir)(PHYSFS_Dir *opaque, const char *filename);
  208. /*
  209. * Close directories/archives, and free any associated memory,
  210. * including the original PHYSFS_Io and (opaque) itself, if
  211. * applicable. Implementation can assume that it won't be called if
  212. * there are still files open from this archive.
  213. */
  214. void (*closeArchive)(PHYSFS_Dir *opaque);
  215. /*
  216. * Obtain basic file metadata.
  217. * Returns non-zero on success, zero on failure.
  218. * On failure, call __PHYSFS_setError().
  219. */
  220. int (*stat)(PHYSFS_Dir *opaque, const char *fn,
  221. int *exists, PHYSFS_Stat *stat);
  222. } PHYSFS_Archiver;
  223. /*
  224. * Call this to set the message returned by PHYSFS_getLastError().
  225. * Please only use the ERR_* constants above, or add new constants to the
  226. * above group, but I want these all in one place.
  227. *
  228. * Calling this with a NULL argument is a safe no-op.
  229. */
  230. void __PHYSFS_setError(const PHYSFS_ErrorCode err);
  231. /* This byteorder stuff was lifted from SDL. http://www.libsdl.org/ */
  232. #define PHYSFS_LIL_ENDIAN 1234
  233. #define PHYSFS_BIG_ENDIAN 4321
  234. #if defined(__i386__) || defined(__ia64__) || \
  235. defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64) || \
  236. (defined(__alpha__) || defined(__alpha)) || \
  237. defined(__arm__) || defined(ARM) || \
  238. (defined(__mips__) && defined(__MIPSEL__)) || \
  239. defined(__SYMBIAN32__) || \
  240. defined(__x86_64__) || \
  241. defined(__LITTLE_ENDIAN__)
  242. #define PHYSFS_BYTEORDER PHYSFS_LIL_ENDIAN
  243. #else
  244. #define PHYSFS_BYTEORDER PHYSFS_BIG_ENDIAN
  245. #endif
  246. /*
  247. * When sorting the entries in an archive, we use a modified QuickSort.
  248. * When there are less then PHYSFS_QUICKSORT_THRESHOLD entries left to sort,
  249. * we switch over to a BubbleSort for the remainder. Tweak to taste.
  250. *
  251. * You can override this setting by defining PHYSFS_QUICKSORT_THRESHOLD
  252. * before #including "physfs_internal.h".
  253. */
  254. #ifndef PHYSFS_QUICKSORT_THRESHOLD
  255. #define PHYSFS_QUICKSORT_THRESHOLD 4
  256. #endif
  257. /*
  258. * Sort an array (or whatever) of (max) elements. This uses a mixture of
  259. * a QuickSort and BubbleSort internally.
  260. * (cmpfn) is used to determine ordering, and (swapfn) does the actual
  261. * swapping of elements in the list.
  262. *
  263. * See zip.c for an example.
  264. */
  265. void __PHYSFS_sort(void *entries, size_t max,
  266. int (*cmpfn)(void *, size_t, size_t),
  267. void (*swapfn)(void *, size_t, size_t));
  268. /*
  269. * This isn't a formal error code, it's just for BAIL_MACRO.
  270. * It means: there was an error, but someone else already set it for us.
  271. */
  272. #define ERRPASS PHYSFS_ERR_OK
  273. /* These get used all over for lessening code clutter. */
  274. #define BAIL_MACRO(e, r) do { if (e) __PHYSFS_setError(e); return r; } while (0)
  275. #define BAIL_IF_MACRO(c, e, r) do { if (c) { if (e) __PHYSFS_setError(e); return r; } } while (0)
  276. #define BAIL_MACRO_MUTEX(e, m, r) do { if (e) __PHYSFS_setError(e); __PHYSFS_platformReleaseMutex(m); return r; } while (0)
  277. #define BAIL_IF_MACRO_MUTEX(c, e, m, r) do { if (c) { if (e) __PHYSFS_setError(e); __PHYSFS_platformReleaseMutex(m); return r; } } while (0)
  278. #define GOTO_MACRO(e, g) do { if (e) __PHYSFS_setError(e); goto g; } while (0)
  279. #define GOTO_IF_MACRO(c, e, g) do { if (c) { if (e) __PHYSFS_setError(e); goto g; } } while (0)
  280. #define GOTO_MACRO_MUTEX(e, m, g) do { if (e) __PHYSFS_setError(e); __PHYSFS_platformReleaseMutex(m); goto g; } while (0)
  281. #define GOTO_IF_MACRO_MUTEX(c, e, m, g) do { if (c) { if (e) __PHYSFS_setError(e); __PHYSFS_platformReleaseMutex(m); goto g; } } while (0)
  282. #define __PHYSFS_ARRAYLEN(x) ( (sizeof (x)) / (sizeof (x[0])) )
  283. #ifdef PHYSFS_NO_64BIT_SUPPORT
  284. #define __PHYSFS_SI64(x) ((PHYSFS_sint64) (x))
  285. #define __PHYSFS_UI64(x) ((PHYSFS_uint64) (x))
  286. #elif (defined __GNUC__)
  287. #define __PHYSFS_SI64(x) x##LL
  288. #define __PHYSFS_UI64(x) x##ULL
  289. #elif (defined _MSC_VER)
  290. #define __PHYSFS_SI64(x) x##i64
  291. #define __PHYSFS_UI64(x) x##ui64
  292. #else
  293. #define __PHYSFS_SI64(x) ((PHYSFS_sint64) (x))
  294. #define __PHYSFS_UI64(x) ((PHYSFS_uint64) (x))
  295. #endif
  296. /*
  297. * Check if a ui64 will fit in the platform's address space.
  298. * The initial sizeof check will optimize this macro out entirely on
  299. * 64-bit (and larger?!) platforms, and the other condition will
  300. * return zero or non-zero if the variable will fit in the platform's
  301. * size_t, suitable to pass to malloc. This is kinda messy, but effective.
  302. */
  303. #define __PHYSFS_ui64FitsAddressSpace(s) ( \
  304. (sizeof (PHYSFS_uint64) <= sizeof (size_t)) || \
  305. ((s) < (__PHYSFS_UI64(0xFFFFFFFFFFFFFFFF) >> (64-(sizeof(size_t)*8)))) \
  306. )
  307. /*
  308. * This is a strcasecmp() or stricmp() replacement that expects both strings
  309. * to be in UTF-8 encoding. It will do "case folding" to decide if the
  310. * Unicode codepoints in the strings match.
  311. *
  312. * It will report which string is "greater than" the other, but be aware that
  313. * this doesn't necessarily mean anything: 'a' may be "less than" 'b', but
  314. * a random Kanji codepoint has no meaningful alphabetically relationship to
  315. * a Greek Lambda, but being able to assign a reliable "value" makes sorting
  316. * algorithms possible, if not entirely sane. Most cases should treat the
  317. * return value as "equal" or "not equal".
  318. */
  319. int __PHYSFS_utf8stricmp(const char *s1, const char *s2);
  320. /*
  321. * This works like __PHYSFS_utf8stricmp(), but takes a character (NOT BYTE
  322. * COUNT) argument, like strcasencmp().
  323. */
  324. int __PHYSFS_utf8strnicmp(const char *s1, const char *s2, PHYSFS_uint32 l);
  325. /*
  326. * stricmp() that guarantees to only work with low ASCII. The C runtime
  327. * stricmp() might try to apply a locale/codepage/etc, which we don't want.
  328. */
  329. int __PHYSFS_stricmpASCII(const char *s1, const char *s2);
  330. /*
  331. * strnicmp() that guarantees to only work with low ASCII. The C runtime
  332. * strnicmp() might try to apply a locale/codepage/etc, which we don't want.
  333. */
  334. int __PHYSFS_strnicmpASCII(const char *s1, const char *s2, PHYSFS_uint32 l);
  335. /*
  336. * The current allocator. Not valid before PHYSFS_init is called!
  337. */
  338. extern PHYSFS_Allocator __PHYSFS_AllocatorHooks;
  339. /* convenience macro to make this less cumbersome internally... */
  340. #define allocator __PHYSFS_AllocatorHooks
  341. /*
  342. * Create a PHYSFS_Io for a file in the physical filesystem.
  343. * This path is in platform-dependent notation. (mode) must be 'r', 'w', or
  344. * 'a' for Read, Write, or Append.
  345. */
  346. PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode);
  347. /*
  348. * Create a PHYSFS_Io for a buffer of memory (READ-ONLY). If you already
  349. * have one of these, just use its duplicate() method, and it'll increment
  350. * its refcount without allocating a copy of the buffer.
  351. */
  352. PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
  353. void (*destruct)(void *));
  354. /*
  355. * Read (len) bytes from (io) into (buf). Returns non-zero on success,
  356. * zero on i/o error. Literally: "return (io->read(io, buf, len) == len);"
  357. */
  358. int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len);
  359. /* These are shared between some archivers. */
  360. typedef struct
  361. {
  362. char name[64];
  363. PHYSFS_uint32 startPos;
  364. PHYSFS_uint32 size;
  365. } UNPKentry;
  366. void UNPK_closeArchive(PHYSFS_Dir *opaque);
  367. PHYSFS_Dir *UNPK_openArchive(PHYSFS_Io *io,UNPKentry *e,const PHYSFS_uint32 n);
  368. void UNPK_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
  369. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  370. const char *origdir, void *callbackdata);
  371. PHYSFS_Io *UNPK_openRead(PHYSFS_Dir *opaque, const char *fnm, int *fileExists);
  372. PHYSFS_Io *UNPK_openWrite(PHYSFS_Dir *opaque, const char *name);
  373. PHYSFS_Io *UNPK_openAppend(PHYSFS_Dir *opaque, const char *name);
  374. int UNPK_remove(PHYSFS_Dir *opaque, const char *name);
  375. int UNPK_mkdir(PHYSFS_Dir *opaque, const char *name);
  376. int UNPK_stat(PHYSFS_Dir *opaque, const char *fn, int *exist, PHYSFS_Stat *st);
  377. /*--------------------------------------------------------------------------*/
  378. /*--------------------------------------------------------------------------*/
  379. /*------------ ----------------*/
  380. /*------------ You MUST implement the following functions ----------------*/
  381. /*------------ if porting to a new platform. ----------------*/
  382. /*------------ (see platform/unix.c for an example) ----------------*/
  383. /*------------ ----------------*/
  384. /*--------------------------------------------------------------------------*/
  385. /*--------------------------------------------------------------------------*/
  386. /*
  387. * The dir separator; '/' on unix, '\\' on win32, ":" on MacOS, etc...
  388. * Obviously, this isn't a function. If you need more than one char for this,
  389. * you'll need to pull some old pieces of PhysicsFS out of revision control.
  390. */
  391. #if PHYSFS_PLATFORM_WINDOWS
  392. #define __PHYSFS_platformDirSeparator '\\'
  393. #else
  394. #define __PHYSFS_platformDirSeparator '/'
  395. #endif
  396. /*
  397. * Initialize the platform. This is called when PHYSFS_init() is called from
  398. * the application.
  399. *
  400. * Return zero if there was a catastrophic failure (which prevents you from
  401. * functioning at all), and non-zero otherwise.
  402. */
  403. int __PHYSFS_platformInit(void);
  404. /*
  405. * Deinitialize the platform. This is called when PHYSFS_deinit() is called
  406. * from the application. You can use this to clean up anything you've
  407. * allocated in your platform driver.
  408. *
  409. * Return zero if there was a catastrophic failure (which prevents you from
  410. * functioning at all), and non-zero otherwise.
  411. */
  412. int __PHYSFS_platformDeinit(void);
  413. /*
  414. * Open a file for reading. (filename) is in platform-dependent notation. The
  415. * file pointer should be positioned on the first byte of the file.
  416. *
  417. * The return value will be some platform-specific datatype that is opaque to
  418. * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32.
  419. *
  420. * The same file can be opened for read multiple times, and each should have
  421. * a unique file handle; this is frequently employed to prevent race
  422. * conditions in the archivers.
  423. *
  424. * Call __PHYSFS_setError() and return (NULL) if the file can't be opened.
  425. */
  426. void *__PHYSFS_platformOpenRead(const char *filename);
  427. /*
  428. * Open a file for writing. (filename) is in platform-dependent notation. If
  429. * the file exists, it should be truncated to zero bytes, and if it doesn't
  430. * exist, it should be created as a zero-byte file. The file pointer should
  431. * be positioned on the first byte of the file.
  432. *
  433. * The return value will be some platform-specific datatype that is opaque to
  434. * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32,
  435. * etc.
  436. *
  437. * Opening a file for write multiple times has undefined results.
  438. *
  439. * Call __PHYSFS_setError() and return (NULL) if the file can't be opened.
  440. */
  441. void *__PHYSFS_platformOpenWrite(const char *filename);
  442. /*
  443. * Open a file for appending. (filename) is in platform-dependent notation. If
  444. * the file exists, the file pointer should be place just past the end of the
  445. * file, so that the first write will be one byte after the current end of
  446. * the file. If the file doesn't exist, it should be created as a zero-byte
  447. * file. The file pointer should be positioned on the first byte of the file.
  448. *
  449. * The return value will be some platform-specific datatype that is opaque to
  450. * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32,
  451. * etc.
  452. *
  453. * Opening a file for append multiple times has undefined results.
  454. *
  455. * Call __PHYSFS_setError() and return (NULL) if the file can't be opened.
  456. */
  457. void *__PHYSFS_platformOpenAppend(const char *filename);
  458. /*
  459. * Read more data from a platform-specific file handle. (opaque) should be
  460. * cast to whatever data type your platform uses. Read a maximum of (len)
  461. * 8-bit bytes to the area pointed to by (buf). If there isn't enough data
  462. * available, return the number of bytes read, and position the file pointer
  463. * immediately after those bytes.
  464. * On success, return (len) and position the file pointer immediately past
  465. * the end of the last read byte. Return (-1) if there is a catastrophic
  466. * error, and call __PHYSFS_setError() to describe the problem; the file
  467. * pointer should not move in such a case. A partial read is success; only
  468. * return (-1) on total failure; presumably, the next read call after a
  469. * partial read will fail as such.
  470. */
  471. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len);
  472. /*
  473. * Write more data to a platform-specific file handle. (opaque) should be
  474. * cast to whatever data type your platform uses. Write a maximum of (len)
  475. * 8-bit bytes from the area pointed to by (buffer). If there is a problem,
  476. * return the number of bytes written, and position the file pointer
  477. * immediately after those bytes. Return (-1) if there is a catastrophic
  478. * error, and call __PHYSFS_setError() to describe the problem; the file
  479. * pointer should not move in such a case. A partial write is success; only
  480. * return (-1) on total failure; presumably, the next write call after a
  481. * partial write will fail as such.
  482. */
  483. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  484. PHYSFS_uint64 len);
  485. /*
  486. * Set the file pointer to a new position. (opaque) should be cast to
  487. * whatever data type your platform uses. (pos) specifies the number
  488. * of 8-bit bytes to seek to from the start of the file. Seeking past the
  489. * end of the file is an error condition, and you should check for it.
  490. *
  491. * Not all file types can seek; this is to be expected by the caller.
  492. *
  493. * On error, call __PHYSFS_setError() and return zero. On success, return
  494. * a non-zero value.
  495. */
  496. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos);
  497. /*
  498. * Get the file pointer's position, in an 8-bit byte offset from the start of
  499. * the file. (opaque) should be cast to whatever data type your platform
  500. * uses.
  501. *
  502. * Not all file types can "tell"; this is to be expected by the caller.
  503. *
  504. * On error, call __PHYSFS_setError() and return -1. On success, return >= 0.
  505. */
  506. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque);
  507. /*
  508. * Determine the current size of a file, in 8-bit bytes, from an open file.
  509. *
  510. * The caller expects that this information may not be available for all
  511. * file types on all platforms.
  512. *
  513. * Return -1 if you can't do it, and call __PHYSFS_setError(). Otherwise,
  514. * return the file length in 8-bit bytes.
  515. */
  516. PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle);
  517. /*
  518. * !!! FIXME: comment me.
  519. */
  520. int __PHYSFS_platformStat(const char *fn, int *exists, PHYSFS_Stat *stat);
  521. /*
  522. * Flush any pending writes to disk. (opaque) should be cast to whatever data
  523. * type your platform uses. Be sure to check for errors; the caller expects
  524. * that this function can fail if there was a flushing error, etc.
  525. *
  526. * Return zero on failure, non-zero on success.
  527. */
  528. int __PHYSFS_platformFlush(void *opaque);
  529. /*
  530. * Close file and deallocate resources. (opaque) should be cast to whatever
  531. * data type your platform uses. This should close the file in any scenario:
  532. * flushing is a separate function call, and this function should never fail.
  533. *
  534. * You should clean up all resources associated with (opaque); the pointer
  535. * will be considered invalid after this call.
  536. */
  537. void __PHYSFS_platformClose(void *opaque);
  538. /*
  539. * Platform implementation of PHYSFS_getCdRomDirsCallback()...
  540. * CD directories are discovered and reported to the callback one at a time.
  541. * Pointers passed to the callback are assumed to be invalid to the
  542. * application after the callback returns, so you can free them or whatever.
  543. * Callback does not assume results will be sorted in any meaningful way.
  544. */
  545. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data);
  546. /*
  547. * Calculate the base dir, if your platform needs special consideration.
  548. * Just return NULL if the standard routines will suffice. (see
  549. * calculateBaseDir() in physfs.c ...)
  550. * Your string must end with a dir separator if you don't return NULL.
  551. * Caller will allocator.Free() the retval if it's not NULL.
  552. */
  553. char *__PHYSFS_platformCalcBaseDir(const char *argv0);
  554. /*
  555. * Get the platform-specific user dir.
  556. * As of PhysicsFS 2.1, returning NULL means fatal error.
  557. * Your string must end with a dir separator if you don't return NULL.
  558. * Caller will allocator.Free() the retval if it's not NULL.
  559. */
  560. char *__PHYSFS_platformCalcUserDir(void);
  561. /* This is the cached version from PHYSFS_init(). This is a fast call. */
  562. const char *__PHYSFS_getUserDir(void); /* not deprecated internal version. */
  563. /*
  564. * Get the platform-specific pref dir.
  565. * Returning NULL means fatal error.
  566. * Your string must end with a dir separator if you don't return NULL.
  567. * Caller will allocator.Free() the retval if it's not NULL.
  568. * Caller will make missing directories if necessary; this just reports
  569. * the final path.
  570. */
  571. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app);
  572. /*
  573. * Return a pointer that uniquely identifies the current thread.
  574. * On a platform without threading, (0x1) will suffice. These numbers are
  575. * arbitrary; the only requirement is that no two threads have the same
  576. * pointer.
  577. */
  578. void *__PHYSFS_platformGetThreadID(void);
  579. /*
  580. * Enumerate a directory of files. This follows the rules for the
  581. * PHYSFS_Archiver->enumerateFiles() method (see above), except that the
  582. * (dirName) that is passed to this function is converted to
  583. * platform-DEPENDENT notation by the caller. The PHYSFS_Archiver version
  584. * uses platform-independent notation. Note that ".", "..", and other
  585. * metaentries should always be ignored.
  586. */
  587. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  588. int omitSymLinks,
  589. PHYSFS_EnumFilesCallback callback,
  590. const char *origdir,
  591. void *callbackdata);
  592. /*
  593. * Make a directory in the actual filesystem. (path) is specified in
  594. * platform-dependent notation. On error, return zero and set the error
  595. * message. Return non-zero on success.
  596. */
  597. int __PHYSFS_platformMkDir(const char *path);
  598. /*
  599. * Remove a file or directory entry in the actual filesystem. (path) is
  600. * specified in platform-dependent notation. Note that this deletes files
  601. * _and_ directories, so you might need to do some determination.
  602. * Non-empty directories should report an error and not delete themselves
  603. * or their contents.
  604. *
  605. * Deleting a symlink should remove the link, not what it points to.
  606. *
  607. * On error, return zero and set the error message. Return non-zero on success.
  608. */
  609. int __PHYSFS_platformDelete(const char *path);
  610. /*
  611. * Create a platform-specific mutex. This can be whatever datatype your
  612. * platform uses for mutexes, but it is cast to a (void *) for abstractness.
  613. *
  614. * Return (NULL) if you couldn't create one. Systems without threads can
  615. * return any arbitrary non-NULL value.
  616. */
  617. void *__PHYSFS_platformCreateMutex(void);
  618. /*
  619. * Destroy a platform-specific mutex, and clean up any resources associated
  620. * with it. (mutex) is a value previously returned by
  621. * __PHYSFS_platformCreateMutex(). This can be a no-op on single-threaded
  622. * platforms.
  623. */
  624. void __PHYSFS_platformDestroyMutex(void *mutex);
  625. /*
  626. * Grab possession of a platform-specific mutex. Mutexes should be recursive;
  627. * that is, the same thread should be able to call this function multiple
  628. * times in a row without causing a deadlock. This function should block
  629. * until a thread can gain possession of the mutex.
  630. *
  631. * Return non-zero if the mutex was grabbed, zero if there was an
  632. * unrecoverable problem grabbing it (this should not be a matter of
  633. * timing out! We're talking major system errors; block until the mutex
  634. * is available otherwise.)
  635. *
  636. * _DO NOT_ call __PHYSFS_setError() in here! Since setError calls this
  637. * function, you'll cause an infinite recursion. This means you can't
  638. * use the BAIL_*MACRO* macros, either.
  639. */
  640. int __PHYSFS_platformGrabMutex(void *mutex);
  641. /*
  642. * Relinquish possession of the mutex when this method has been called
  643. * once for each time that platformGrabMutex was called. Once possession has
  644. * been released, the next thread in line to grab the mutex (if any) may
  645. * proceed.
  646. *
  647. * _DO NOT_ call __PHYSFS_setError() in here! Since setError calls this
  648. * function, you'll cause an infinite recursion. This means you can't
  649. * use the BAIL_*MACRO* macros, either.
  650. */
  651. void __PHYSFS_platformReleaseMutex(void *mutex);
  652. /*
  653. * Called at the start of PHYSFS_init() to prepare the allocator, if the user
  654. * hasn't selected their own allocator via PHYSFS_setAllocator().
  655. * If the platform has a custom allocator, it should fill in the fields of
  656. * (a) with the proper function pointers and return non-zero.
  657. * If the platform just wants to use malloc()/free()/etc, return zero
  658. * immediately and the higher level will handle it. The Init and Deinit
  659. * fields of (a) are optional...set them to NULL if you don't need them.
  660. * Everything else must be implemented. All rules follow those for
  661. * PHYSFS_setAllocator(). If Init isn't NULL, it will be called shortly
  662. * after this function returns non-zero.
  663. */
  664. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a);
  665. #ifdef __cplusplus
  666. }
  667. #endif
  668. #endif
  669. /* end of physfs_internal.h ... */