physfs.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. /**
  2. * PhysicsFS; a portable, flexible file i/o abstraction.
  3. *
  4. * Documentation is in physfs.h. It's verbose, honest. :)
  5. *
  6. * Please see the file LICENSE in the source's root directory.
  7. *
  8. * This file written by Ryan C. Gordon.
  9. */
  10. #if HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. #if (defined PHYSFS_PROFILING)
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "physfs.h"
  21. #define __PHYSICSFS_INTERNAL__
  22. #include "physfs_internal.h"
  23. /* !!! FIXME: Get rid of this. Merge it with PhysDirInfo. */
  24. typedef struct __PHYSFS_DIRHANDLE__
  25. {
  26. /*
  27. * This is reserved for the driver to store information.
  28. */
  29. void *opaque;
  30. /*
  31. * Pointer to the directory i/o functions for this handle.
  32. */
  33. const struct __PHYSFS_DIRFUNCTIONS__ *funcs;
  34. } DirHandle;
  35. typedef struct __PHYSFS_ERRMSGTYPE__
  36. {
  37. PHYSFS_uint64 tid;
  38. int errorAvailable;
  39. char errorString[80];
  40. struct __PHYSFS_ERRMSGTYPE__ *next;
  41. } ErrMsg;
  42. typedef struct __PHYSFS_DIRINFO__
  43. {
  44. char *dirName;
  45. DirHandle *dirHandle;
  46. struct __PHYSFS_DIRINFO__ *next;
  47. } PhysDirInfo;
  48. typedef struct __PHYSFS_FILEHANDLELIST__
  49. {
  50. PHYSFS_file handle;
  51. struct __PHYSFS_FILEHANDLELIST__ *next;
  52. } FileHandleList;
  53. /* The various i/o drivers... */
  54. #if (defined PHYSFS_SUPPORTS_ZIP)
  55. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  56. extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
  57. #endif
  58. #if (defined PHYSFS_SUPPORTS_GRP)
  59. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
  60. extern const DirFunctions __PHYSFS_DirFunctions_GRP;
  61. #endif
  62. #if (defined PHYSFS_SUPPORTS_QPAK)
  63. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
  64. extern const DirFunctions __PHYSFS_DirFunctions_QPAK;
  65. #endif
  66. #if (defined PHYSFS_SUPPORTS_HOG)
  67. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
  68. extern const DirFunctions __PHYSFS_DirFunctions_HOG;
  69. #endif
  70. #if (defined PHYSFS_SUPPORTS_MVL)
  71. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
  72. extern const DirFunctions __PHYSFS_DirFunctions_MVL;
  73. #endif
  74. #if (defined PHYSFS_SUPPORTS_WAD)
  75. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
  76. extern const DirFunctions __PHYSFS_DirFunctions_WAD;
  77. #endif
  78. #if (defined PHYSFS_SUPPORTS_MIX)
  79. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX;
  80. extern const DirFunctions __PHYSFS_DirFunctions_MIX;
  81. #endif
  82. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  83. static const PHYSFS_ArchiveInfo *supported_types[] =
  84. {
  85. #if (defined PHYSFS_SUPPORTS_ZIP)
  86. &__PHYSFS_ArchiveInfo_ZIP,
  87. #endif
  88. #if (defined PHYSFS_SUPPORTS_GRP)
  89. &__PHYSFS_ArchiveInfo_GRP,
  90. #endif
  91. #if (defined PHYSFS_SUPPORTS_QPAK)
  92. &__PHYSFS_ArchiveInfo_QPAK,
  93. #endif
  94. #if (defined PHYSFS_SUPPORTS_HOG)
  95. &__PHYSFS_ArchiveInfo_HOG,
  96. #endif
  97. #if (defined PHYSFS_SUPPORTS_MVL)
  98. &__PHYSFS_ArchiveInfo_MVL,
  99. #endif
  100. #if (defined PHYSFS_SUPPORTS_WAD)
  101. &__PHYSFS_ArchiveInfo_WAD,
  102. #endif
  103. #if (defined PHYSFS_SUPPORTS_MIX)
  104. &__PHYSFS_ArchiveInfo_MIX,
  105. #endif
  106. NULL
  107. };
  108. static const DirFunctions *dirFunctions[] =
  109. {
  110. #if (defined PHYSFS_SUPPORTS_ZIP)
  111. &__PHYSFS_DirFunctions_ZIP,
  112. #endif
  113. #if (defined PHYSFS_SUPPORTS_GRP)
  114. &__PHYSFS_DirFunctions_GRP,
  115. #endif
  116. #if (defined PHYSFS_SUPPORTS_QPAK)
  117. &__PHYSFS_DirFunctions_QPAK,
  118. #endif
  119. #if (defined PHYSFS_SUPPORTS_HOG)
  120. &__PHYSFS_DirFunctions_HOG,
  121. #endif
  122. #if (defined PHYSFS_SUPPORTS_MVL)
  123. &__PHYSFS_DirFunctions_MVL,
  124. #endif
  125. #if (defined PHYSFS_SUPPORTS_WAD)
  126. &__PHYSFS_DirFunctions_WAD,
  127. #endif
  128. #if (defined PHYSFS_SUPPORTS_MIX)
  129. &__PHYSFS_DirFunctions_MIX,
  130. #endif
  131. &__PHYSFS_DirFunctions_DIR,
  132. NULL
  133. };
  134. /* General PhysicsFS state ... */
  135. static int initialized = 0;
  136. static ErrMsg *errorMessages = NULL;
  137. static PhysDirInfo *searchPath = NULL;
  138. static PhysDirInfo *writeDir = NULL;
  139. static FileHandleList *openWriteList = NULL;
  140. static FileHandleList *openReadList = NULL;
  141. static char *baseDir = NULL;
  142. static char *userDir = NULL;
  143. static int allowSymLinks = 0;
  144. /* mutexes ... */
  145. static void *errorLock = NULL; /* protects error message list. */
  146. static void *stateLock = NULL; /* protects other PhysFS static state. */
  147. /* allocator ... */
  148. static int externalAllocator = 0;
  149. static PHYSFS_allocator allocator;
  150. /* functions ... */
  151. static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
  152. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  153. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  154. {
  155. PHYSFS_uint32 i;
  156. int sorted;
  157. do
  158. {
  159. sorted = 1;
  160. for (i = lo; i < hi; i++)
  161. {
  162. if (cmpfn(a, i, i + 1) > 0)
  163. {
  164. swapfn(a, i, i + 1);
  165. sorted = 0;
  166. } /* if */
  167. } /* for */
  168. } while (!sorted);
  169. } /* __PHYSFS_bubble_sort */
  170. static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
  171. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  172. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  173. {
  174. PHYSFS_uint32 i;
  175. PHYSFS_uint32 j;
  176. PHYSFS_uint32 v;
  177. if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
  178. __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
  179. else
  180. {
  181. i = (hi + lo) / 2;
  182. if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
  183. if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
  184. if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
  185. j = hi - 1;
  186. swapfn(a, i, j);
  187. i = lo;
  188. v = j;
  189. while (1)
  190. {
  191. while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
  192. while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
  193. if (j < i)
  194. break;
  195. swapfn(a, i, j);
  196. } /* while */
  197. swapfn(a, i, hi-1);
  198. __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
  199. __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
  200. } /* else */
  201. } /* __PHYSFS_quick_sort */
  202. void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
  203. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  204. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  205. {
  206. /*
  207. * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
  208. * http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  209. */
  210. __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
  211. } /* __PHYSFS_sort */
  212. #if (defined PHYSFS_PROFILING)
  213. #define PHYSFS_TEST_SORT_ITERATIONS 150
  214. #define PHYSFS_TEST_SORT_ELEMENTS (64 * 1024)
  215. static int __PHYSFS_test_sort_cmp(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
  216. {
  217. PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
  218. PHYSFS_sint32 one = a[x];
  219. PHYSFS_sint32 two = a[y];
  220. if (one < two)
  221. return(-1);
  222. else if (one > two)
  223. return(1);
  224. return(0);
  225. } /* __PHYSFS_test_sort_cmp */
  226. static void __PHYSFS_test_sort_swap(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
  227. {
  228. PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
  229. PHYSFS_sint32 tmp;
  230. tmp = a[x];
  231. a[x] = a[y];
  232. a[y] = tmp;
  233. } /* __PHYSFS_test_sort_swap */
  234. static int __PHYSFS_test_sort_do(PHYSFS_uint32 *timer,
  235. PHYSFS_sint32 *a, PHYSFS_uint32 max,
  236. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  237. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  238. {
  239. PHYSFS_uint32 i;
  240. struct timeval starttime, endtime;
  241. gettimeofday(&starttime, NULL);
  242. __PHYSFS_sort(a, max, cmpfn, swapfn);
  243. gettimeofday(&endtime, NULL);
  244. for (i = 1; i < max; i++)
  245. {
  246. if (a[i] < a[i - 1])
  247. return(0);
  248. } /* for */
  249. if (timer != NULL)
  250. {
  251. *timer = ( ((endtime.tv_sec - starttime.tv_sec) * 1000) +
  252. ((endtime.tv_usec - starttime.tv_usec) / 1000) );
  253. } /* if */
  254. return(1);
  255. } /* __PHYSFS_test_sort_time */
  256. static void __PHYSFS_test_sort(void)
  257. {
  258. PHYSFS_uint32 elasped[PHYSFS_TEST_SORT_ITERATIONS];
  259. PHYSFS_sint32 iter;
  260. PHYSFS_sint32 a[PHYSFS_TEST_SORT_ELEMENTS];
  261. PHYSFS_sint32 i, x;
  262. int success;
  263. printf("Testing __PHYSFS_sort (linear presorted) ... ");
  264. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  265. {
  266. /* set up array to sort. */
  267. for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
  268. a[i] = i;
  269. /* sort it. */
  270. success = __PHYSFS_test_sort_do(&elasped[iter],
  271. a, PHYSFS_TEST_SORT_ELEMENTS,
  272. __PHYSFS_test_sort_cmp,
  273. __PHYSFS_test_sort_swap);
  274. if (!success)
  275. break;
  276. } /* for */
  277. if (!success)
  278. printf("Failed!\n");
  279. else
  280. {
  281. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  282. x += elasped[iter];
  283. x /= PHYSFS_TEST_SORT_ITERATIONS;
  284. printf("Average run (%lu) ms.\n", (unsigned long) x);
  285. } /* else */
  286. printf("Testing __PHYSFS_sort (linear presorted reverse) ... ");
  287. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  288. {
  289. /* set up array to sort. */
  290. for (i = 0, x = PHYSFS_TEST_SORT_ELEMENTS;
  291. i < PHYSFS_TEST_SORT_ELEMENTS;
  292. i++, x--)
  293. {
  294. a[i] = x;
  295. } /* for */
  296. /* sort it. */
  297. success = __PHYSFS_test_sort_do(&elasped[iter],
  298. a, PHYSFS_TEST_SORT_ELEMENTS,
  299. __PHYSFS_test_sort_cmp,
  300. __PHYSFS_test_sort_swap);
  301. if (!success)
  302. break;
  303. } /* for */
  304. if (!success)
  305. printf("Failed!\n");
  306. else
  307. {
  308. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  309. x += elasped[iter];
  310. x /= PHYSFS_TEST_SORT_ITERATIONS;
  311. printf("Average run (%lu) ms.\n", (unsigned long) x);
  312. } /* else */
  313. printf("Testing __PHYSFS_sort (randomized) ... ");
  314. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  315. {
  316. /* set up array to sort. */
  317. for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
  318. a[i] = (PHYSFS_uint32) rand();
  319. /* sort it. */
  320. success = __PHYSFS_test_sort_do(&elasped[iter],
  321. a, PHYSFS_TEST_SORT_ELEMENTS,
  322. __PHYSFS_test_sort_cmp,
  323. __PHYSFS_test_sort_swap);
  324. if (!success)
  325. break;
  326. } /* for */
  327. if (!success)
  328. printf("Failed!\n");
  329. else
  330. {
  331. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  332. x += elasped[iter];
  333. x /= PHYSFS_TEST_SORT_ITERATIONS;
  334. printf("Average run (%lu) ms.\n", (unsigned long) x);
  335. } /* else */
  336. printf("__PHYSFS_test_sort() complete.\n\n");
  337. } /* __PHYSFS_test_sort */
  338. #endif
  339. static ErrMsg *findErrorForCurrentThread(void)
  340. {
  341. ErrMsg *i;
  342. PHYSFS_uint64 tid;
  343. if (errorLock != NULL)
  344. __PHYSFS_platformGrabMutex(errorLock);
  345. if (errorMessages != NULL)
  346. {
  347. tid = __PHYSFS_platformGetThreadID();
  348. for (i = errorMessages; i != NULL; i = i->next)
  349. {
  350. if (i->tid == tid)
  351. {
  352. if (errorLock != NULL)
  353. __PHYSFS_platformReleaseMutex(errorLock);
  354. return(i);
  355. } /* if */
  356. } /* for */
  357. } /* if */
  358. if (errorLock != NULL)
  359. __PHYSFS_platformReleaseMutex(errorLock);
  360. return(NULL); /* no error available. */
  361. } /* findErrorForCurrentThread */
  362. void __PHYSFS_setError(const char *str)
  363. {
  364. ErrMsg *err;
  365. if (str == NULL)
  366. return;
  367. err = findErrorForCurrentThread();
  368. if (err == NULL)
  369. {
  370. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  371. if (err == NULL)
  372. return; /* uhh...? */
  373. memset((void *) err, '\0', sizeof (ErrMsg));
  374. err->tid = __PHYSFS_platformGetThreadID();
  375. if (errorLock != NULL)
  376. __PHYSFS_platformGrabMutex(errorLock);
  377. err->next = errorMessages;
  378. errorMessages = err;
  379. if (errorLock != NULL)
  380. __PHYSFS_platformReleaseMutex(errorLock);
  381. } /* if */
  382. err->errorAvailable = 1;
  383. strncpy(err->errorString, str, sizeof (err->errorString));
  384. err->errorString[sizeof (err->errorString) - 1] = '\0';
  385. } /* __PHYSFS_setError */
  386. const char *PHYSFS_getLastError(void)
  387. {
  388. ErrMsg *err = findErrorForCurrentThread();
  389. if ((err == NULL) || (!err->errorAvailable))
  390. return(NULL);
  391. err->errorAvailable = 0;
  392. return(err->errorString);
  393. } /* PHYSFS_getLastError */
  394. /* MAKE SURE that errorLock is held before calling this! */
  395. static void freeErrorMessages(void)
  396. {
  397. ErrMsg *i;
  398. ErrMsg *next;
  399. for (i = errorMessages; i != NULL; i = next)
  400. {
  401. next = i->next;
  402. free(i);
  403. } /* for */
  404. errorMessages = NULL;
  405. } /* freeErrorMessages */
  406. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  407. {
  408. if (ver != NULL)
  409. {
  410. ver->major = PHYSFS_VER_MAJOR;
  411. ver->minor = PHYSFS_VER_MINOR;
  412. ver->patch = PHYSFS_VER_PATCH;
  413. } /* if */
  414. } /* PHYSFS_getLinkedVersion */
  415. static const char *find_filename_extension(const char *fname)
  416. {
  417. const char *retval = strchr(fname, '.');
  418. const char *p = retval;
  419. while (p != NULL)
  420. {
  421. p = strchr(p + 1, '.');
  422. if (p != NULL)
  423. retval = p;
  424. } /* while */
  425. if (retval != NULL)
  426. retval++; /* skip '.' */
  427. return(retval);
  428. } /* find_filename_extension */
  429. static DirHandle *tryOpenDir(const DirFunctions *f, const char *d, int fw)
  430. {
  431. DirHandle *retval = NULL;
  432. if (f->isArchive(d, fw)) /* fw == "for writing" */
  433. {
  434. void *opaque = f->openArchive(d, fw);
  435. if (opaque != NULL)
  436. {
  437. retval = (DirHandle *) allocator.malloc(sizeof (DirHandle));
  438. if (retval == NULL)
  439. f->dirClose(opaque);
  440. else
  441. {
  442. retval->funcs = f;
  443. retval->opaque = opaque;
  444. } /* else */
  445. } /* if */
  446. } /* if */
  447. return(retval);
  448. } /* tryOpenDir */
  449. static DirHandle *openDirectory(const char *d, int forWriting)
  450. {
  451. DirHandle *retval = NULL;
  452. const DirFunctions **i;
  453. const char *ext;
  454. BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
  455. ext = find_filename_extension(d);
  456. if (ext != NULL)
  457. {
  458. /* Look for archivers with matching file extensions first... */
  459. for (i = dirFunctions; (*i != NULL) && (retval == NULL); i++)
  460. {
  461. if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) == 0)
  462. retval = tryOpenDir(*i, d, forWriting);
  463. } /* for */
  464. /* failing an exact file extension match, try all the others... */
  465. for (i = dirFunctions; (*i != NULL) && (retval == NULL); i++)
  466. {
  467. if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) != 0)
  468. retval = tryOpenDir(*i, d, forWriting);
  469. } /* for */
  470. } /* if */
  471. else /* no extension? Try them all. */
  472. {
  473. for (i = dirFunctions; (*i != NULL) && (retval == NULL); i++)
  474. retval = tryOpenDir(*i, d, forWriting);
  475. } /* else */
  476. BAIL_IF_MACRO(retval == NULL, ERR_UNSUPPORTED_ARCHIVE, NULL);
  477. return(retval);
  478. } /* openDirectory */
  479. static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
  480. {
  481. DirHandle *dirHandle = NULL;
  482. PhysDirInfo *di = NULL;
  483. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  484. dirHandle = openDirectory(newDir, forWriting);
  485. BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
  486. /* !!! FIXME: get rid of this allocation */
  487. di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
  488. if (di == NULL)
  489. {
  490. dirHandle->funcs->dirClose(dirHandle->opaque);
  491. free(dirHandle);
  492. BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
  493. } /* if */
  494. di->dirName = (char *) malloc(strlen(newDir) + 1);
  495. if (di->dirName == NULL)
  496. {
  497. free(di);
  498. dirHandle->funcs->dirClose(dirHandle->opaque);
  499. free(dirHandle);
  500. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  501. } /* if */
  502. di->next = NULL;
  503. di->dirHandle = dirHandle;
  504. strcpy(di->dirName, newDir);
  505. return(di);
  506. } /* buildDirInfo */
  507. /* MAKE SURE you've got the stateLock held before calling this! */
  508. static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
  509. {
  510. FileHandleList *i;
  511. if (di == NULL)
  512. return(1);
  513. for (i = openList; i != NULL; i = i->next)
  514. {
  515. const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
  516. BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
  517. } /* for */
  518. di->dirHandle->funcs->dirClose(di->dirHandle->opaque);
  519. free(di->dirHandle);
  520. free(di->dirName);
  521. free(di);
  522. return(1);
  523. } /* freeDirInfo */
  524. static char *calculateUserDir(void)
  525. {
  526. char *retval = NULL;
  527. const char *str = NULL;
  528. str = __PHYSFS_platformGetUserDir();
  529. if (str != NULL)
  530. retval = (char *) str;
  531. else
  532. {
  533. const char *dirsep = PHYSFS_getDirSeparator();
  534. const char *uname = __PHYSFS_platformGetUserName();
  535. str = (uname != NULL) ? uname : "default";
  536. retval = (char *) malloc(strlen(baseDir) + strlen(str) +
  537. strlen(dirsep) + 6);
  538. if (retval == NULL)
  539. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  540. else
  541. sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
  542. if (uname != NULL)
  543. free((void *) uname);
  544. } /* else */
  545. return(retval);
  546. } /* calculateUserDir */
  547. static int appendDirSep(char **dir)
  548. {
  549. const char *dirsep = PHYSFS_getDirSeparator();
  550. char *ptr;
  551. if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
  552. return(1);
  553. ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
  554. if (!ptr)
  555. {
  556. free(*dir);
  557. return(0);
  558. } /* if */
  559. strcat(ptr, dirsep);
  560. *dir = ptr;
  561. return(1);
  562. } /* appendDirSep */
  563. static char *calculateBaseDir(const char *argv0)
  564. {
  565. const char *dirsep = PHYSFS_getDirSeparator();
  566. char *retval;
  567. char *ptr;
  568. /*
  569. * See if the platform driver wants to handle this for us...
  570. */
  571. retval = __PHYSFS_platformCalcBaseDir(argv0);
  572. if (retval != NULL)
  573. return(retval);
  574. /*
  575. * Determine if there's a path on argv0. If there is, that's the base dir.
  576. */
  577. ptr = strstr(argv0, dirsep);
  578. if (ptr != NULL)
  579. {
  580. char *p = ptr;
  581. size_t size;
  582. while (p != NULL)
  583. {
  584. ptr = p;
  585. p = strstr(p + 1, dirsep);
  586. } /* while */
  587. size = (size_t) (ptr - argv0);
  588. retval = (char *) malloc(size + 1);
  589. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  590. memcpy(retval, argv0, size);
  591. retval[size] = '\0';
  592. return(retval);
  593. } /* if */
  594. /*
  595. * Last ditch effort: it's the current working directory. (*shrug*)
  596. */
  597. retval = __PHYSFS_platformCurrentDir();
  598. if(retval != NULL) {
  599. return(retval);
  600. }
  601. /*
  602. * Ok, current directory doesn't exist, use the root directory.
  603. * Not a good alternative, but it only happens if the current
  604. * directory was deleted from under the program.
  605. */
  606. retval = (char *) malloc(strlen(dirsep) + 1);
  607. strcpy(retval, dirsep);
  608. return(retval);
  609. } /* calculateBaseDir */
  610. static int initializeMutexes(void)
  611. {
  612. errorLock = __PHYSFS_platformCreateMutex();
  613. if (errorLock == NULL)
  614. goto initializeMutexes_failed;
  615. stateLock = __PHYSFS_platformCreateMutex();
  616. if (stateLock == NULL)
  617. goto initializeMutexes_failed;
  618. return(1); /* success. */
  619. initializeMutexes_failed:
  620. if (errorLock != NULL)
  621. __PHYSFS_platformDestroyMutex(errorLock);
  622. if (stateLock != NULL)
  623. __PHYSFS_platformDestroyMutex(stateLock);
  624. errorLock = stateLock = NULL;
  625. return(0); /* failed. */
  626. } /* initializeMutexes */
  627. static void setDefaultAllocator(void);
  628. int PHYSFS_init(const char *argv0)
  629. {
  630. char *ptr;
  631. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  632. if (!externalAllocator)
  633. setDefaultAllocator();
  634. BAIL_IF_MACRO(!allocator.init(), NULL, 0);
  635. BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
  636. BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
  637. baseDir = calculateBaseDir(argv0);
  638. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  639. ptr = __PHYSFS_platformRealPath(baseDir);
  640. free(baseDir);
  641. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  642. baseDir = ptr;
  643. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  644. userDir = calculateUserDir();
  645. if (userDir != NULL)
  646. {
  647. ptr = __PHYSFS_platformRealPath(userDir);
  648. free(userDir);
  649. userDir = ptr;
  650. } /* if */
  651. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  652. {
  653. free(baseDir);
  654. baseDir = NULL;
  655. return(0);
  656. } /* if */
  657. initialized = 1;
  658. /* This makes sure that the error subsystem is initialized. */
  659. __PHYSFS_setError(PHYSFS_getLastError());
  660. #if (defined PHYSFS_PROFILING)
  661. srand(time(NULL));
  662. setbuf(stdout, NULL);
  663. printf("\n");
  664. printf("********************************************************\n");
  665. printf("Warning! Profiling is built into this copy of PhysicsFS!\n");
  666. printf("********************************************************\n");
  667. printf("\n");
  668. printf("\n");
  669. __PHYSFS_test_sort();
  670. #endif
  671. return(1);
  672. } /* PHYSFS_init */
  673. /* MAKE SURE you hold stateLock before calling this! */
  674. static int closeFileHandleList(FileHandleList **list)
  675. {
  676. FileHandleList *i;
  677. FileHandleList *next = NULL;
  678. FileHandle *h;
  679. for (i = *list; i != NULL; i = next)
  680. {
  681. next = i->next;
  682. h = (FileHandle *) (i->handle.opaque);
  683. if (!h->funcs->fileClose(h))
  684. {
  685. *list = i;
  686. return(0);
  687. } /* if */
  688. free(i);
  689. } /* for */
  690. *list = NULL;
  691. return(1);
  692. } /* closeFileHandleList */
  693. /* MAKE SURE you hold the stateLock before calling this! */
  694. static void freeSearchPath(void)
  695. {
  696. PhysDirInfo *i;
  697. PhysDirInfo *next = NULL;
  698. closeFileHandleList(&openReadList);
  699. if (searchPath != NULL)
  700. {
  701. for (i = searchPath; i != NULL; i = next)
  702. {
  703. next = i->next;
  704. freeDirInfo(i, openReadList);
  705. } /* for */
  706. searchPath = NULL;
  707. } /* if */
  708. } /* freeSearchPath */
  709. int PHYSFS_deinit(void)
  710. {
  711. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  712. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
  713. closeFileHandleList(&openWriteList);
  714. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  715. freeSearchPath();
  716. freeErrorMessages();
  717. if (baseDir != NULL)
  718. {
  719. free(baseDir);
  720. baseDir = NULL;
  721. } /* if */
  722. if (userDir != NULL)
  723. {
  724. free(userDir);
  725. userDir = NULL;
  726. } /* if */
  727. allowSymLinks = 0;
  728. initialized = 0;
  729. __PHYSFS_platformDestroyMutex(errorLock);
  730. __PHYSFS_platformDestroyMutex(stateLock);
  731. allocator.deinit();
  732. errorLock = stateLock = NULL;
  733. return(1);
  734. } /* PHYSFS_deinit */
  735. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  736. {
  737. return(supported_types);
  738. } /* PHYSFS_supportedArchiveTypes */
  739. void PHYSFS_freeList(void *list)
  740. {
  741. void **i;
  742. for (i = (void **) list; *i != NULL; i++)
  743. free(*i);
  744. free(list);
  745. } /* PHYSFS_freeList */
  746. const char *PHYSFS_getDirSeparator(void)
  747. {
  748. return(__PHYSFS_platformDirSeparator);
  749. } /* PHYSFS_getDirSeparator */
  750. char **PHYSFS_getCdRomDirs(void)
  751. {
  752. return(__PHYSFS_platformDetectAvailableCDs());
  753. } /* PHYSFS_getCdRomDirs */
  754. const char *PHYSFS_getBaseDir(void)
  755. {
  756. return(baseDir); /* this is calculated in PHYSFS_init()... */
  757. } /* PHYSFS_getBaseDir */
  758. const char *PHYSFS_getUserDir(void)
  759. {
  760. return(userDir); /* this is calculated in PHYSFS_init()... */
  761. } /* PHYSFS_getUserDir */
  762. const char *PHYSFS_getWriteDir(void)
  763. {
  764. const char *retval = NULL;
  765. __PHYSFS_platformGrabMutex(stateLock);
  766. if (writeDir != NULL)
  767. retval = writeDir->dirName;
  768. __PHYSFS_platformReleaseMutex(stateLock);
  769. return(retval);
  770. } /* PHYSFS_getWriteDir */
  771. int PHYSFS_setWriteDir(const char *newDir)
  772. {
  773. int retval = 1;
  774. __PHYSFS_platformGrabMutex(stateLock);
  775. if (writeDir != NULL)
  776. {
  777. BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
  778. stateLock, 0);
  779. writeDir = NULL;
  780. } /* if */
  781. if (newDir != NULL)
  782. {
  783. writeDir = buildDirInfo(newDir, 1);
  784. retval = (writeDir != NULL);
  785. } /* if */
  786. __PHYSFS_platformReleaseMutex(stateLock);
  787. return(retval);
  788. } /* PHYSFS_setWriteDir */
  789. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  790. {
  791. PhysDirInfo *di;
  792. PhysDirInfo *prev = NULL;
  793. PhysDirInfo *i;
  794. __PHYSFS_platformGrabMutex(stateLock);
  795. for (i = searchPath; i != NULL; i = i->next)
  796. {
  797. /* already in search path? */
  798. BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
  799. prev = i;
  800. } /* for */
  801. di = buildDirInfo(newDir, 0);
  802. BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
  803. if (appendToPath)
  804. {
  805. di->next = NULL;
  806. if (prev == NULL)
  807. searchPath = di;
  808. else
  809. prev->next = di;
  810. } /* if */
  811. else
  812. {
  813. di->next = searchPath;
  814. searchPath = di;
  815. } /* else */
  816. __PHYSFS_platformReleaseMutex(stateLock);
  817. return(1);
  818. } /* PHYSFS_addToSearchPath */
  819. int PHYSFS_removeFromSearchPath(const char *oldDir)
  820. {
  821. PhysDirInfo *i;
  822. PhysDirInfo *prev = NULL;
  823. PhysDirInfo *next = NULL;
  824. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  825. __PHYSFS_platformGrabMutex(stateLock);
  826. for (i = searchPath; i != NULL; i = i->next)
  827. {
  828. if (strcmp(i->dirName, oldDir) == 0)
  829. {
  830. next = i->next;
  831. BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
  832. stateLock, 0);
  833. if (prev == NULL)
  834. searchPath = next;
  835. else
  836. prev->next = next;
  837. BAIL_MACRO_MUTEX(NULL, stateLock, 1);
  838. } /* if */
  839. prev = i;
  840. } /* for */
  841. BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
  842. } /* PHYSFS_removeFromSearchPath */
  843. char **PHYSFS_getSearchPath(void)
  844. {
  845. int count = 1;
  846. int x;
  847. PhysDirInfo *i;
  848. char **retval;
  849. __PHYSFS_platformGrabMutex(stateLock);
  850. for (i = searchPath; i != NULL; i = i->next)
  851. count++;
  852. retval = (char **) malloc(sizeof (char *) * count);
  853. BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
  854. count--;
  855. retval[count] = NULL;
  856. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  857. {
  858. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  859. if (retval[x] == NULL) /* this is friggin' ugly. */
  860. {
  861. while (x > 0)
  862. {
  863. x--;
  864. free(retval[x]);
  865. } /* while */
  866. free(retval);
  867. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  868. } /* if */
  869. strcpy(retval[x], i->dirName);
  870. } /* for */
  871. __PHYSFS_platformReleaseMutex(stateLock);
  872. return(retval);
  873. } /* PHYSFS_getSearchPath */
  874. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  875. const char *archiveExt, int includeCdRoms,
  876. int archivesFirst)
  877. {
  878. const char *basedir = PHYSFS_getBaseDir();
  879. const char *userdir = PHYSFS_getUserDir();
  880. const char *dirsep = PHYSFS_getDirSeparator();
  881. char *str;
  882. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  883. /* set write dir... */
  884. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  885. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  886. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  887. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  888. if (!PHYSFS_setWriteDir(str))
  889. {
  890. int no_write = 0;
  891. sprintf(str, ".%s/%s", organization, appName);
  892. if ( (PHYSFS_setWriteDir(userdir)) &&
  893. (PHYSFS_mkdir(str)) )
  894. {
  895. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  896. if (!PHYSFS_setWriteDir(str))
  897. no_write = 1;
  898. } /* if */
  899. else
  900. {
  901. no_write = 1;
  902. } /* else */
  903. if (no_write)
  904. {
  905. PHYSFS_setWriteDir(NULL); /* just in case. */
  906. free(str);
  907. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  908. } /* if */
  909. } /* if */
  910. /* Put write dir first in search path... */
  911. PHYSFS_addToSearchPath(str, 0);
  912. free(str);
  913. /* Put base path on search path... */
  914. PHYSFS_addToSearchPath(basedir, 1);
  915. /* handle CD-ROMs... */
  916. if (includeCdRoms)
  917. {
  918. char **cds = PHYSFS_getCdRomDirs();
  919. char **i;
  920. for (i = cds; *i != NULL; i++)
  921. PHYSFS_addToSearchPath(*i, 1);
  922. PHYSFS_freeList(cds);
  923. } /* if */
  924. /* Root out archives, and add them to search path... */
  925. if (archiveExt != NULL)
  926. {
  927. char **rc = PHYSFS_enumerateFiles("/");
  928. char **i;
  929. size_t extlen = strlen(archiveExt);
  930. char *ext;
  931. for (i = rc; *i != NULL; i++)
  932. {
  933. size_t l = strlen(*i);
  934. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  935. {
  936. ext = (*i) + (l - extlen);
  937. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  938. {
  939. const char *d = PHYSFS_getRealDir(*i);
  940. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  941. if (str != NULL)
  942. {
  943. sprintf(str, "%s%s%s", d, dirsep, *i);
  944. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  945. free(str);
  946. } /* if */
  947. } /* if */
  948. } /* if */
  949. } /* for */
  950. PHYSFS_freeList(rc);
  951. } /* if */
  952. return(1);
  953. } /* PHYSFS_setSaneConfig */
  954. void PHYSFS_permitSymbolicLinks(int allow)
  955. {
  956. allowSymLinks = allow;
  957. } /* PHYSFS_permitSymbolicLinks */
  958. /* string manipulation in C makes my ass itch. */
  959. char * __PHYSFS_convertToDependent(const char *prepend,
  960. const char *dirName,
  961. const char *append)
  962. {
  963. const char *dirsep = __PHYSFS_platformDirSeparator;
  964. size_t sepsize = strlen(dirsep);
  965. char *str;
  966. char *i1;
  967. char *i2;
  968. size_t allocSize;
  969. while (*dirName == '/')
  970. dirName++;
  971. allocSize = strlen(dirName) + 1;
  972. if (prepend != NULL)
  973. allocSize += strlen(prepend) + sepsize;
  974. if (append != NULL)
  975. allocSize += strlen(append) + sepsize;
  976. /* make sure there's enough space if the dir separator is bigger. */
  977. if (sepsize > 1)
  978. {
  979. str = (char *) dirName;
  980. do
  981. {
  982. str = strchr(str, '/');
  983. if (str != NULL)
  984. {
  985. allocSize += (sepsize - 1);
  986. str++;
  987. } /* if */
  988. } while (str != NULL);
  989. } /* if */
  990. str = (char *) malloc(allocSize);
  991. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  992. if (prepend == NULL)
  993. *str = '\0';
  994. else
  995. {
  996. strcpy(str, prepend);
  997. strcat(str, dirsep);
  998. } /* else */
  999. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  1000. {
  1001. if (*i1 == '/')
  1002. {
  1003. strcpy(i2, dirsep);
  1004. i2 += sepsize;
  1005. } /* if */
  1006. else
  1007. {
  1008. *i2 = *i1;
  1009. } /* else */
  1010. } /* for */
  1011. *i2 = '\0';
  1012. if (append)
  1013. {
  1014. strcat(str, dirsep);
  1015. strcpy(str, append);
  1016. } /* if */
  1017. return(str);
  1018. } /* __PHYSFS_convertToDependent */
  1019. /*
  1020. * Verify that (fname) (in platform-independent notation), in relation
  1021. * to (h) is secure. That means that each element of fname is checked
  1022. * for symlinks (if they aren't permitted). Also, elements such as
  1023. * ".", "..", or ":" are flagged.
  1024. *
  1025. * With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
  1026. * at a time), you should always pass zero for "allowMissing" for efficiency.
  1027. *
  1028. * Returns non-zero if string is safe, zero if there's a security issue.
  1029. * PHYSFS_getLastError() will specify what was wrong.
  1030. */
  1031. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname, int allowMissing)
  1032. {
  1033. int retval = 1;
  1034. char *start;
  1035. char *end;
  1036. char *str;
  1037. if (*fname == '\0') /* quick rejection. */
  1038. return(1);
  1039. /* !!! FIXME: Can we ditch this malloc()? */
  1040. start = str = malloc(strlen(fname) + 1);
  1041. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  1042. strcpy(str, fname);
  1043. while (1)
  1044. {
  1045. end = strchr(start, '/');
  1046. if (end != NULL)
  1047. *end = '\0';
  1048. if ( (strcmp(start, ".") == 0) ||
  1049. (strcmp(start, "..") == 0) ||
  1050. (strchr(start, '\\') != NULL) ||
  1051. (strchr(start, ':') != NULL) )
  1052. {
  1053. __PHYSFS_setError(ERR_INSECURE_FNAME);
  1054. retval = 0;
  1055. break;
  1056. } /* if */
  1057. if (!allowSymLinks)
  1058. {
  1059. if (h->funcs->isSymLink(h->opaque, str, &retval))
  1060. {
  1061. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  1062. free(str);
  1063. return(0); /* insecure. */
  1064. } /* if */
  1065. /* break out early if path element is missing. */
  1066. if (!retval)
  1067. {
  1068. /*
  1069. * We need to clear it if it's the last element of the path,
  1070. * since this might be a non-existant file we're opening
  1071. * for writing...
  1072. */
  1073. if ((end == NULL) || (allowMissing))
  1074. retval = 1;
  1075. break;
  1076. } /* if */
  1077. } /* if */
  1078. if (end == NULL)
  1079. break;
  1080. *end = '/';
  1081. start = end + 1;
  1082. } /* while */
  1083. free(str);
  1084. return(retval);
  1085. } /* __PHYSFS_verifySecurity */
  1086. int PHYSFS_mkdir(const char *dname)
  1087. {
  1088. DirHandle *h;
  1089. char *str;
  1090. char *start;
  1091. char *end;
  1092. int retval = 0;
  1093. int exists = 1; /* force existance check on first path element. */
  1094. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  1095. while (*dname == '/')
  1096. dname++;
  1097. __PHYSFS_platformGrabMutex(stateLock);
  1098. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1099. h = writeDir->dirHandle;
  1100. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,dname,1),NULL,stateLock,0);
  1101. start = str = malloc(strlen(dname) + 1);
  1102. BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
  1103. strcpy(str, dname);
  1104. while (1)
  1105. {
  1106. end = strchr(start, '/');
  1107. if (end != NULL)
  1108. *end = '\0';
  1109. /* only check for existance if all parent dirs existed, too... */
  1110. if (exists)
  1111. retval = h->funcs->isDirectory(h->opaque, str, &exists);
  1112. if (!exists)
  1113. retval = h->funcs->mkdir(h->opaque, str);
  1114. if (!retval)
  1115. break;
  1116. if (end == NULL)
  1117. break;
  1118. *end = '/';
  1119. start = end + 1;
  1120. } /* while */
  1121. __PHYSFS_platformReleaseMutex(stateLock);
  1122. free(str);
  1123. return(retval);
  1124. } /* PHYSFS_mkdir */
  1125. int PHYSFS_delete(const char *fname)
  1126. {
  1127. int retval;
  1128. DirHandle *h;
  1129. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1130. while (*fname == '/')
  1131. fname++;
  1132. __PHYSFS_platformGrabMutex(stateLock);
  1133. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1134. h = writeDir->dirHandle;
  1135. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,fname,0),NULL,stateLock,0);
  1136. retval = h->funcs->remove(h->opaque, fname);
  1137. __PHYSFS_platformReleaseMutex(stateLock);
  1138. return(retval);
  1139. } /* PHYSFS_delete */
  1140. const char *PHYSFS_getRealDir(const char *filename)
  1141. {
  1142. PhysDirInfo *i;
  1143. const char *retval = NULL;
  1144. while (*filename == '/')
  1145. filename++;
  1146. __PHYSFS_platformGrabMutex(stateLock);
  1147. for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
  1148. {
  1149. DirHandle *h = i->dirHandle;
  1150. if (__PHYSFS_verifySecurity(h, filename, 0))
  1151. {
  1152. if (h->funcs->exists(h->opaque, filename))
  1153. retval = i->dirName;
  1154. } /* if */
  1155. } /* for */
  1156. __PHYSFS_platformReleaseMutex(stateLock);
  1157. return(retval);
  1158. } /* PHYSFS_getRealDir */
  1159. static int countList(LinkedStringList *list)
  1160. {
  1161. int retval = 0;
  1162. LinkedStringList *i;
  1163. for (i = list; i != NULL; i = i->next)
  1164. retval++;
  1165. return(retval);
  1166. } /* countList */
  1167. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  1168. {
  1169. int i;
  1170. LinkedStringList *next = NULL;
  1171. int len = countList(finalList);
  1172. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  1173. if (retval == NULL)
  1174. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  1175. for (i = 0; i < len; i++)
  1176. {
  1177. next = finalList->next;
  1178. if (retval == NULL)
  1179. free(finalList->str);
  1180. else
  1181. retval[i] = finalList->str;
  1182. free(finalList);
  1183. finalList = next;
  1184. } /* for */
  1185. if (retval != NULL)
  1186. retval[i] = NULL;
  1187. return(retval);
  1188. } /* convertStringListToPhysFSList */
  1189. static void insertStringListItem(LinkedStringList **final,
  1190. LinkedStringList *item)
  1191. {
  1192. LinkedStringList *i;
  1193. LinkedStringList *prev = NULL;
  1194. int rc;
  1195. for (i = *final; i != NULL; i = i->next)
  1196. {
  1197. rc = strcmp(i->str, item->str);
  1198. if (rc > 0) /* insertion point. */
  1199. break;
  1200. else if (rc == 0) /* already in list. */
  1201. {
  1202. free(item->str);
  1203. free(item);
  1204. return;
  1205. } /* else if */
  1206. prev = i;
  1207. } /* for */
  1208. /*
  1209. * If we are here, we are either at the insertion point.
  1210. * This may be the end of the list, or the list may be empty, too.
  1211. */
  1212. if (prev == NULL)
  1213. *final = item;
  1214. else
  1215. prev->next = item;
  1216. item->next = i;
  1217. } /* insertStringListItem */
  1218. /* if we run out of memory anywhere in here, we give back what we can. */
  1219. static void interpolateStringLists(LinkedStringList **final,
  1220. LinkedStringList *newList)
  1221. {
  1222. LinkedStringList *next = NULL;
  1223. while (newList != NULL)
  1224. {
  1225. next = newList->next;
  1226. insertStringListItem(final, newList);
  1227. newList = next;
  1228. } /* while */
  1229. } /* interpolateStringLists */
  1230. char **PHYSFS_enumerateFiles(const char *path)
  1231. {
  1232. PhysDirInfo *i;
  1233. char **retval = NULL;
  1234. LinkedStringList *rc;
  1235. LinkedStringList *finalList = NULL;
  1236. int omitSymLinks = !allowSymLinks;
  1237. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  1238. while (*path == '/')
  1239. path++;
  1240. __PHYSFS_platformGrabMutex(stateLock);
  1241. for (i = searchPath; i != NULL; i = i->next)
  1242. {
  1243. DirHandle *h = i->dirHandle;
  1244. if (__PHYSFS_verifySecurity(h, path, 0))
  1245. {
  1246. rc = h->funcs->enumerateFiles(h->opaque, path, omitSymLinks);
  1247. interpolateStringLists(&finalList, rc);
  1248. } /* if */
  1249. } /* for */
  1250. __PHYSFS_platformReleaseMutex(stateLock);
  1251. retval = convertStringListToPhysFSList(finalList);
  1252. return(retval);
  1253. } /* PHYSFS_enumerateFiles */
  1254. int PHYSFS_exists(const char *fname)
  1255. {
  1256. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1257. while (*fname == '/')
  1258. fname++;
  1259. return(PHYSFS_getRealDir(fname) != NULL);
  1260. } /* PHYSFS_exists */
  1261. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1262. {
  1263. PhysDirInfo *i;
  1264. PHYSFS_sint64 retval = -1;
  1265. int fileExists = 0;
  1266. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1267. while (*fname == '/')
  1268. fname++;
  1269. if (*fname == '\0') /* eh...punt if it's the root dir. */
  1270. return(1);
  1271. __PHYSFS_platformGrabMutex(stateLock);
  1272. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1273. {
  1274. DirHandle *h = i->dirHandle;
  1275. if (__PHYSFS_verifySecurity(h, fname, 0))
  1276. retval = h->funcs->getLastModTime(h->opaque, fname, &fileExists);
  1277. } /* for */
  1278. __PHYSFS_platformReleaseMutex(stateLock);
  1279. return(retval);
  1280. } /* PHYSFS_getLastModTime */
  1281. int PHYSFS_isDirectory(const char *fname)
  1282. {
  1283. PhysDirInfo *i;
  1284. int retval = 0;
  1285. int fileExists = 0;
  1286. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1287. while (*fname == '/')
  1288. fname++;
  1289. BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
  1290. __PHYSFS_platformGrabMutex(stateLock);
  1291. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1292. {
  1293. DirHandle *h = i->dirHandle;
  1294. if (__PHYSFS_verifySecurity(h, fname, 0))
  1295. retval = h->funcs->isDirectory(h->opaque, fname, &fileExists);
  1296. } /* for */
  1297. __PHYSFS_platformReleaseMutex(stateLock);
  1298. return(retval);
  1299. } /* PHYSFS_isDirectory */
  1300. int PHYSFS_isSymbolicLink(const char *fname)
  1301. {
  1302. PhysDirInfo *i;
  1303. int retval = 0;
  1304. int fileExists = 0;
  1305. BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
  1306. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1307. while (*fname == '/')
  1308. fname++;
  1309. BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
  1310. __PHYSFS_platformGrabMutex(stateLock);
  1311. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1312. {
  1313. DirHandle *h = i->dirHandle;
  1314. if (__PHYSFS_verifySecurity(h, fname, 0))
  1315. retval = h->funcs->isSymLink(h->opaque, fname, &fileExists);
  1316. } /* for */
  1317. __PHYSFS_platformReleaseMutex(stateLock);
  1318. return(retval);
  1319. } /* PHYSFS_isSymbolicLink */
  1320. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  1321. {
  1322. PHYSFS_file *retval = NULL;
  1323. FileHandle *rc = NULL;
  1324. DirHandle *h;
  1325. const DirFunctions *f;
  1326. FileHandleList *list;
  1327. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1328. while (*fname == '/')
  1329. fname++;
  1330. __PHYSFS_platformGrabMutex(stateLock);
  1331. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  1332. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  1333. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname, 0), NULL,
  1334. stateLock, NULL);
  1335. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1336. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1337. f = h->funcs;
  1338. if (appending)
  1339. rc = f->openAppend(h->opaque, fname);
  1340. else
  1341. rc = f->openWrite(h->opaque, fname);
  1342. if (rc == NULL)
  1343. free(list);
  1344. else
  1345. {
  1346. rc->dirHandle = h;
  1347. rc->buffer = NULL; /* just in case. */
  1348. rc->buffill = rc->bufpos = rc->bufsize = 0; /* just in case. */
  1349. rc->forReading = 0;
  1350. list->handle.opaque = (void *) rc;
  1351. list->next = openWriteList;
  1352. openWriteList = list;
  1353. retval = &(list->handle);
  1354. } /* else */
  1355. __PHYSFS_platformReleaseMutex(stateLock);
  1356. return(retval);
  1357. } /* doOpenWrite */
  1358. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1359. {
  1360. return(doOpenWrite(filename, 0));
  1361. } /* PHYSFS_openWrite */
  1362. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1363. {
  1364. return(doOpenWrite(filename, 1));
  1365. } /* PHYSFS_openAppend */
  1366. PHYSFS_file *PHYSFS_openRead(const char *fname)
  1367. {
  1368. PHYSFS_file *retval = NULL;
  1369. FileHandle *rc = NULL;
  1370. FileHandleList *list;
  1371. DirHandle *h = NULL;
  1372. int fileExists = 0;
  1373. PhysDirInfo *i = NULL;
  1374. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1375. while (*fname == '/')
  1376. fname++;
  1377. __PHYSFS_platformGrabMutex(stateLock);
  1378. BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
  1379. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1380. {
  1381. h = i->dirHandle;
  1382. if (__PHYSFS_verifySecurity(h, fname, 0))
  1383. rc = h->funcs->openRead(h->opaque, fname, &fileExists);
  1384. } /* for */
  1385. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1386. rc->dirHandle = h;
  1387. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1388. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1389. list->handle.opaque = (void *) rc;
  1390. list->next = openReadList;
  1391. openReadList = list;
  1392. retval = &(list->handle);
  1393. __PHYSFS_platformReleaseMutex(stateLock);
  1394. rc->buffer = NULL; /* just in case. */
  1395. rc->buffill = rc->bufpos = rc->bufsize = 0; /* just in case. */
  1396. rc->forReading = 1;
  1397. return(retval);
  1398. } /* PHYSFS_openRead */
  1399. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1400. {
  1401. FileHandle *h = (FileHandle *) handle->opaque;
  1402. FileHandleList *prev = NULL;
  1403. FileHandleList *i;
  1404. int rc = 1;
  1405. for (i = *list; i != NULL; i = i->next)
  1406. {
  1407. if (&i->handle == handle) /* handle is in this list? */
  1408. {
  1409. PHYSFS_uint8 *tmp = h->buffer;
  1410. rc = PHYSFS_flush(handle);
  1411. if (rc)
  1412. rc = h->funcs->fileClose(h);
  1413. if (!rc)
  1414. return(-1);
  1415. if (tmp != NULL) /* free any associated buffer. */
  1416. free(tmp);
  1417. if (prev == NULL)
  1418. *list = i->next;
  1419. else
  1420. prev->next = i->next;
  1421. free(i);
  1422. return(1);
  1423. } /* if */
  1424. prev = i;
  1425. } /* for */
  1426. return(0);
  1427. } /* closeHandleInOpenList */
  1428. int PHYSFS_close(PHYSFS_file *handle)
  1429. {
  1430. int rc;
  1431. __PHYSFS_platformGrabMutex(stateLock);
  1432. /* -1 == close failure. 0 == not found. 1 == success. */
  1433. rc = closeHandleInOpenList(&openReadList, handle);
  1434. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1435. if (!rc)
  1436. {
  1437. rc = closeHandleInOpenList(&openWriteList, handle);
  1438. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1439. } /* if */
  1440. __PHYSFS_platformReleaseMutex(stateLock);
  1441. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1442. return(1);
  1443. } /* PHYSFS_close */
  1444. static PHYSFS_sint64 doBufferedRead(PHYSFS_file *handle, void *buffer,
  1445. PHYSFS_uint32 objSize,
  1446. PHYSFS_uint32 objCount)
  1447. {
  1448. FileHandle *h = (FileHandle *) handle->opaque;
  1449. PHYSFS_sint64 retval = 0;
  1450. PHYSFS_uint32 remainder = 0;
  1451. while (objCount > 0)
  1452. {
  1453. PHYSFS_uint32 buffered = h->buffill - h->bufpos;
  1454. PHYSFS_uint64 mustread = (objSize * objCount) - remainder;
  1455. PHYSFS_uint32 copied;
  1456. if (buffered == 0) /* need to refill buffer? */
  1457. {
  1458. PHYSFS_sint64 rc = h->funcs->read(h, h->buffer, 1, h->bufsize);
  1459. if (rc <= 0)
  1460. {
  1461. h->bufpos -= remainder;
  1462. return(((rc == -1) && (retval == 0)) ? -1 : retval);
  1463. } /* if */
  1464. buffered = h->buffill = (PHYSFS_uint32) rc;
  1465. h->bufpos = 0;
  1466. } /* if */
  1467. if (buffered > mustread)
  1468. buffered = (PHYSFS_uint32) mustread;
  1469. memcpy(buffer, h->buffer + h->bufpos, (size_t) buffered);
  1470. buffer = ((PHYSFS_uint8 *) buffer) + buffered;
  1471. h->bufpos += buffered;
  1472. buffered += remainder; /* take remainder into account. */
  1473. copied = (buffered / objSize);
  1474. remainder = (buffered % objSize);
  1475. retval += copied;
  1476. objCount -= copied;
  1477. } /* while */
  1478. return(retval);
  1479. } /* doBufferedRead */
  1480. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1481. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1482. {
  1483. FileHandle *h = (FileHandle *) handle->opaque;
  1484. BAIL_IF_MACRO(!h->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
  1485. if (h->buffer != NULL)
  1486. return(doBufferedRead(handle, buffer, objSize, objCount));
  1487. return(h->funcs->read(h, buffer, objSize, objCount));
  1488. } /* PHYSFS_read */
  1489. static PHYSFS_sint64 doBufferedWrite(PHYSFS_file *handle, const void *buffer,
  1490. PHYSFS_uint32 objSize,
  1491. PHYSFS_uint32 objCount)
  1492. {
  1493. FileHandle *h = (FileHandle *) handle->opaque;
  1494. /* whole thing fits in the buffer? */
  1495. if (h->buffill + (objSize * objCount) < h->bufsize)
  1496. {
  1497. memcpy(h->buffer + h->buffill, buffer, objSize * objCount);
  1498. h->buffill += (objSize * objCount);
  1499. return(objCount);
  1500. } /* if */
  1501. /* would overflow buffer. Flush and then write the new objects, too. */
  1502. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
  1503. return(h->funcs->write(h, buffer, objSize, objCount));
  1504. } /* doBufferedWrite */
  1505. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1506. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1507. {
  1508. FileHandle *h = (FileHandle *) handle->opaque;
  1509. BAIL_IF_MACRO(h->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
  1510. if (h->buffer != NULL)
  1511. return(doBufferedWrite(handle, buffer, objSize, objCount));
  1512. return(h->funcs->write(h, buffer, objSize, objCount));
  1513. } /* PHYSFS_write */
  1514. int PHYSFS_eof(PHYSFS_file *handle)
  1515. {
  1516. FileHandle *h = (FileHandle *) handle->opaque;
  1517. if (!h->forReading) /* never EOF on files opened for write/append. */
  1518. return(0);
  1519. /* eof if buffer is empty and archiver says so. */
  1520. return((h->bufpos == h->buffill) && (h->funcs->eof(h)));
  1521. } /* PHYSFS_eof */
  1522. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1523. {
  1524. FileHandle *h = (FileHandle *) handle->opaque;
  1525. PHYSFS_sint64 retval = h->forReading ?
  1526. (h->funcs->tell(h) - h->buffill) + h->bufpos :
  1527. (h->funcs->tell(h) + h->buffill);
  1528. return(retval);
  1529. } /* PHYSFS_tell */
  1530. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1531. {
  1532. FileHandle *h = (FileHandle *) handle->opaque;
  1533. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1534. if (h->buffer && h->forReading)
  1535. {
  1536. /* avoid throwing away our precious buffer if seeking within it. */
  1537. PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
  1538. if ( /* seeking within the already-buffered range? */
  1539. ((offset >= 0) && (offset <= h->buffill - h->bufpos)) /* forwards */
  1540. || ((offset < 0) && (-offset <= h->bufpos)) /* backwards */ )
  1541. {
  1542. h->bufpos += offset;
  1543. return(1); /* successful seek */
  1544. } /* if */
  1545. } /* if */
  1546. /* we have to fall back to a 'raw' seek. */
  1547. h->buffill = h->bufpos = 0;
  1548. return(h->funcs->seek(h, pos));
  1549. } /* PHYSFS_seek */
  1550. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1551. {
  1552. FileHandle *h = (FileHandle *) handle->opaque;
  1553. return(h->funcs->fileLength(h));
  1554. } /* PHYSFS_filelength */
  1555. int PHYSFS_setBuffer(PHYSFS_file *handle, PHYSFS_uint64 _bufsize)
  1556. {
  1557. FileHandle *h = (FileHandle *) handle->opaque;
  1558. PHYSFS_uint32 bufsize = (PHYSFS_uint32) _bufsize;
  1559. BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
  1560. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1561. /*
  1562. * For reads, we need to move the file pointer to where it would be
  1563. * if we weren't buffering, so that the next read will get the
  1564. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  1565. */
  1566. if ((h->forReading) && (h->buffill != h->bufpos))
  1567. {
  1568. PHYSFS_uint64 pos;
  1569. PHYSFS_sint64 curpos = h->funcs->tell(h);
  1570. BAIL_IF_MACRO(curpos == -1, NULL, 0);
  1571. pos = ((curpos - h->buffill) + h->bufpos);
  1572. BAIL_IF_MACRO(!h->funcs->seek(h, pos), NULL, 0);
  1573. } /* if */
  1574. if (bufsize == 0) /* delete existing buffer. */
  1575. {
  1576. if (h->buffer != NULL)
  1577. {
  1578. free(h->buffer);
  1579. h->buffer = NULL;
  1580. } /* if */
  1581. } /* if */
  1582. else
  1583. {
  1584. PHYSFS_uint8 *newbuf = realloc(h->buffer, bufsize);
  1585. BAIL_IF_MACRO(newbuf == NULL, ERR_OUT_OF_MEMORY, 0);
  1586. h->buffer = newbuf;
  1587. } /* else */
  1588. h->bufsize = bufsize;
  1589. h->buffill = h->bufpos = 0;
  1590. return(1);
  1591. } /* PHYSFS_setBuffer */
  1592. int PHYSFS_flush(PHYSFS_file *handle)
  1593. {
  1594. FileHandle *h = (FileHandle *) handle->opaque;
  1595. PHYSFS_sint64 rc;
  1596. if ((h->forReading) || (h->bufpos == h->buffill))
  1597. return(1); /* open for read or buffer empty are successful no-ops. */
  1598. /* dump buffer to disk. */
  1599. rc = h->funcs->write(h, h->buffer + h->bufpos, h->buffill - h->bufpos, 1);
  1600. BAIL_IF_MACRO(rc <= 0, NULL, 0);
  1601. h->bufpos = h->buffill = 0;
  1602. return(1);
  1603. } /* PHYSFS_flush */
  1604. LinkedStringList *__PHYSFS_addToLinkedStringList(LinkedStringList *retval,
  1605. LinkedStringList **prev,
  1606. const char *str,
  1607. PHYSFS_sint32 len)
  1608. {
  1609. LinkedStringList *l;
  1610. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  1611. BAIL_IF_MACRO(l == NULL, ERR_OUT_OF_MEMORY, retval);
  1612. if (len < 0)
  1613. len = strlen(str);
  1614. l->str = (char *) malloc(len + 1);
  1615. if (l->str == NULL)
  1616. {
  1617. free(l);
  1618. BAIL_MACRO(ERR_OUT_OF_MEMORY, retval);
  1619. } /* if */
  1620. strncpy(l->str, str, len);
  1621. l->str[len] = '\0';
  1622. if (retval == NULL)
  1623. retval = l;
  1624. else
  1625. (*prev)->next = l;
  1626. *prev = l;
  1627. l->next = NULL;
  1628. return(retval);
  1629. } /* __PHYSFS_addToLinkedStringList */
  1630. int PHYSFS_setAllocator(PHYSFS_allocator *a)
  1631. {
  1632. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  1633. externalAllocator = (a != NULL);
  1634. if (externalAllocator)
  1635. memcpy(&allocator, a, sizeof (PHYSFS_allocator));
  1636. return(1);
  1637. } /* PHYSFS_setAllocator */
  1638. static void setDefaultAllocator(void)
  1639. {
  1640. assert(!externalAllocator);
  1641. allocator.init = __PHYSFS_platformAllocatorInit;
  1642. allocator.deinit = __PHYSFS_platformAllocatorDeinit;
  1643. allocator.malloc = __PHYSFS_platformAllocatorMalloc;
  1644. allocator.realloc = __PHYSFS_platformAllocatorRealloc;
  1645. allocator.free = __PHYSFS_platformAllocatorFree;
  1646. } /* setDefaultAllocator */
  1647. PHYSFS_allocator *__PHYSFS_getAllocator(void)
  1648. {
  1649. return(&allocator);
  1650. } /* __PHYFS_getAllocator */
  1651. /* end of physfs.c ... */