physfs.c 53 KB

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