physfs.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982
  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.txt in the source's root directory.
  7. *
  8. * This file written by Ryan C. Gordon.
  9. */
  10. /* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
  11. /* !!! FIXME: use snprintf(), not sprintf(). */
  12. #define __PHYSICSFS_INTERNAL__
  13. #include "physfs_internal.h"
  14. typedef struct __PHYSFS_DIRHANDLE__
  15. {
  16. void *opaque; /* Instance data unique to the archiver. */
  17. char *dirName; /* Path to archive in platform-dependent notation. */
  18. char *mountPoint; /* Mountpoint in virtual file tree. */
  19. const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
  20. struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
  21. } DirHandle;
  22. typedef struct __PHYSFS_FILEHANDLE__
  23. {
  24. PHYSFS_Io *io; /* Instance data unique to the archiver for this file. */
  25. PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
  26. const DirHandle *dirHandle; /* Archiver instance that created this */
  27. PHYSFS_uint8 *buffer; /* Buffer, if set (NULL otherwise). Don't touch! */
  28. PHYSFS_uint32 bufsize; /* Bufsize, if set (0 otherwise). Don't touch! */
  29. PHYSFS_uint32 buffill; /* Buffer fill size. Don't touch! */
  30. PHYSFS_uint32 bufpos; /* Buffer position. Don't touch! */
  31. struct __PHYSFS_FILEHANDLE__ *next; /* linked list stuff. */
  32. } FileHandle;
  33. typedef struct __PHYSFS_ERRSTATETYPE__
  34. {
  35. void *tid;
  36. PHYSFS_ErrorCode code;
  37. struct __PHYSFS_ERRSTATETYPE__ *next;
  38. } ErrState;
  39. /* General PhysicsFS state ... */
  40. static int initialized = 0;
  41. static ErrState *errorStates = NULL;
  42. static DirHandle *searchPath = NULL;
  43. static DirHandle *writeDir = NULL;
  44. static FileHandle *openWriteList = NULL;
  45. static FileHandle *openReadList = NULL;
  46. static char *baseDir = NULL;
  47. static char *userDir = NULL;
  48. static char *prefDir = NULL;
  49. static int allowSymLinks = 0;
  50. static const PHYSFS_Archiver **archivers = NULL;
  51. static const PHYSFS_ArchiveInfo **archiveInfo = NULL;
  52. static volatile size_t numArchivers = 0;
  53. /* mutexes ... */
  54. static void *errorLock = NULL; /* protects error message list. */
  55. static void *stateLock = NULL; /* protects other PhysFS static state. */
  56. /* allocator ... */
  57. static int externalAllocator = 0;
  58. PHYSFS_Allocator allocator;
  59. /* PHYSFS_Io implementation for i/o to physical filesystem... */
  60. /* !!! FIXME: maybe refcount the paths in a string pool? */
  61. typedef struct __PHYSFS_NativeIoInfo
  62. {
  63. void *handle;
  64. const char *path;
  65. int mode; /* 'r', 'w', or 'a' */
  66. } NativeIoInfo;
  67. static PHYSFS_sint64 nativeIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  68. {
  69. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  70. return __PHYSFS_platformRead(info->handle, buf, len);
  71. } /* nativeIo_read */
  72. static PHYSFS_sint64 nativeIo_write(PHYSFS_Io *io, const void *buffer,
  73. PHYSFS_uint64 len)
  74. {
  75. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  76. return __PHYSFS_platformWrite(info->handle, buffer, len);
  77. } /* nativeIo_write */
  78. static int nativeIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  79. {
  80. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  81. return __PHYSFS_platformSeek(info->handle, offset);
  82. } /* nativeIo_seek */
  83. static PHYSFS_sint64 nativeIo_tell(PHYSFS_Io *io)
  84. {
  85. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  86. return __PHYSFS_platformTell(info->handle);
  87. } /* nativeIo_tell */
  88. static PHYSFS_sint64 nativeIo_length(PHYSFS_Io *io)
  89. {
  90. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  91. return __PHYSFS_platformFileLength(info->handle);
  92. } /* nativeIo_length */
  93. static PHYSFS_Io *nativeIo_duplicate(PHYSFS_Io *io)
  94. {
  95. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  96. return __PHYSFS_createNativeIo(info->path, info->mode);
  97. } /* nativeIo_duplicate */
  98. static int nativeIo_flush(PHYSFS_Io *io)
  99. {
  100. return __PHYSFS_platformFlush(io->opaque);
  101. } /* nativeIo_flush */
  102. static void nativeIo_destroy(PHYSFS_Io *io)
  103. {
  104. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  105. __PHYSFS_platformClose(info->handle);
  106. allocator.Free((void *) info->path);
  107. allocator.Free(info);
  108. allocator.Free(io);
  109. } /* nativeIo_destroy */
  110. static const PHYSFS_Io __PHYSFS_nativeIoInterface =
  111. {
  112. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  113. nativeIo_read,
  114. nativeIo_write,
  115. nativeIo_seek,
  116. nativeIo_tell,
  117. nativeIo_length,
  118. nativeIo_duplicate,
  119. nativeIo_flush,
  120. nativeIo_destroy
  121. };
  122. PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode)
  123. {
  124. PHYSFS_Io *io = NULL;
  125. NativeIoInfo *info = NULL;
  126. void *handle = NULL;
  127. char *pathdup = NULL;
  128. assert((mode == 'r') || (mode == 'w') || (mode == 'a'));
  129. io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  130. GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
  131. info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
  132. GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
  133. pathdup = (char *) allocator.Malloc(strlen(path) + 1);
  134. GOTO_IF_MACRO(!pathdup, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
  135. if (mode == 'r')
  136. handle = __PHYSFS_platformOpenRead(path);
  137. else if (mode == 'w')
  138. handle = __PHYSFS_platformOpenWrite(path);
  139. else if (mode == 'a')
  140. handle = __PHYSFS_platformOpenAppend(path);
  141. GOTO_IF_MACRO(!handle, ERRPASS, createNativeIo_failed);
  142. strcpy(pathdup, path);
  143. info->handle = handle;
  144. info->path = pathdup;
  145. info->mode = mode;
  146. memcpy(io, &__PHYSFS_nativeIoInterface, sizeof (*io));
  147. io->opaque = info;
  148. return io;
  149. createNativeIo_failed:
  150. if (handle != NULL) __PHYSFS_platformClose(handle);
  151. if (pathdup != NULL) allocator.Free(pathdup);
  152. if (info != NULL) allocator.Free(info);
  153. if (io != NULL) allocator.Free(io);
  154. return NULL;
  155. } /* __PHYSFS_createNativeIo */
  156. /* PHYSFS_Io implementation for i/o to a memory buffer... */
  157. typedef struct __PHYSFS_MemoryIoInfo
  158. {
  159. const PHYSFS_uint8 *buf;
  160. PHYSFS_uint64 len;
  161. PHYSFS_uint64 pos;
  162. PHYSFS_Io *parent;
  163. volatile PHYSFS_uint32 refcount;
  164. void (*destruct)(void *);
  165. } MemoryIoInfo;
  166. static PHYSFS_sint64 memoryIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  167. {
  168. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  169. const PHYSFS_uint64 avail = info->len - info->pos;
  170. assert(avail <= info->len);
  171. if (avail == 0)
  172. return 0; /* we're at EOF; nothing to do. */
  173. if (len > avail)
  174. len = avail;
  175. memcpy(buf, info->buf + info->pos, (size_t) len);
  176. info->pos += len;
  177. return len;
  178. } /* memoryIo_read */
  179. static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
  180. PHYSFS_uint64 len)
  181. {
  182. BAIL_MACRO(PHYSFS_ERR_OPEN_FOR_READING, -1);
  183. } /* memoryIo_write */
  184. static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  185. {
  186. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  187. BAIL_IF_MACRO(offset > info->len, PHYSFS_ERR_PAST_EOF, 0);
  188. info->pos = offset;
  189. return 1;
  190. } /* memoryIo_seek */
  191. static PHYSFS_sint64 memoryIo_tell(PHYSFS_Io *io)
  192. {
  193. const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  194. return (PHYSFS_sint64) info->pos;
  195. } /* memoryIo_tell */
  196. static PHYSFS_sint64 memoryIo_length(PHYSFS_Io *io)
  197. {
  198. const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  199. return (PHYSFS_sint64) info->len;
  200. } /* memoryIo_length */
  201. static PHYSFS_Io *memoryIo_duplicate(PHYSFS_Io *io)
  202. {
  203. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  204. MemoryIoInfo *newinfo = NULL;
  205. PHYSFS_Io *parent = info->parent;
  206. PHYSFS_Io *retval = NULL;
  207. /* avoid deep copies. */
  208. assert((!parent) || (!((MemoryIoInfo *) parent->opaque)->parent) );
  209. /* share the buffer between duplicates. */
  210. if (parent != NULL) /* dup the parent, increment its refcount. */
  211. return parent->duplicate(parent);
  212. /* we're the parent. */
  213. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  214. BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  215. newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
  216. if (!newinfo)
  217. {
  218. allocator.Free(retval);
  219. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  220. } /* if */
  221. /* !!! FIXME: want lockless atomic increment. */
  222. __PHYSFS_platformGrabMutex(stateLock);
  223. info->refcount++;
  224. __PHYSFS_platformReleaseMutex(stateLock);
  225. memset(newinfo, '\0', sizeof (*info));
  226. newinfo->buf = info->buf;
  227. newinfo->len = info->len;
  228. newinfo->pos = 0;
  229. newinfo->parent = io;
  230. newinfo->refcount = 0;
  231. newinfo->destruct = NULL;
  232. memcpy(retval, io, sizeof (*retval));
  233. retval->opaque = newinfo;
  234. return retval;
  235. } /* memoryIo_duplicate */
  236. static int memoryIo_flush(PHYSFS_Io *io) { return 1; /* it's read-only. */ }
  237. static void memoryIo_destroy(PHYSFS_Io *io)
  238. {
  239. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  240. PHYSFS_Io *parent = info->parent;
  241. int should_die = 0;
  242. if (parent != NULL)
  243. {
  244. assert(info->buf == ((MemoryIoInfo *) info->parent->opaque)->buf);
  245. assert(info->len == ((MemoryIoInfo *) info->parent->opaque)->len);
  246. assert(info->refcount == 0);
  247. assert(info->destruct == NULL);
  248. allocator.Free(info);
  249. allocator.Free(io);
  250. parent->destroy(parent); /* decrements refcount. */
  251. return;
  252. } /* if */
  253. /* we _are_ the parent. */
  254. assert(info->refcount > 0); /* even in a race, we hold a reference. */
  255. /* !!! FIXME: want lockless atomic decrement. */
  256. __PHYSFS_platformGrabMutex(stateLock);
  257. info->refcount--;
  258. should_die = (info->refcount == 0);
  259. __PHYSFS_platformReleaseMutex(stateLock);
  260. if (should_die)
  261. {
  262. void (*destruct)(void *) = info->destruct;
  263. void *buf = (void *) info->buf;
  264. io->opaque = NULL; /* kill this here in case of race. */
  265. allocator.Free(info);
  266. allocator.Free(io);
  267. if (destruct != NULL)
  268. destruct(buf);
  269. } /* if */
  270. } /* memoryIo_destroy */
  271. static const PHYSFS_Io __PHYSFS_memoryIoInterface =
  272. {
  273. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  274. memoryIo_read,
  275. memoryIo_write,
  276. memoryIo_seek,
  277. memoryIo_tell,
  278. memoryIo_length,
  279. memoryIo_duplicate,
  280. memoryIo_flush,
  281. memoryIo_destroy
  282. };
  283. PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
  284. void (*destruct)(void *))
  285. {
  286. PHYSFS_Io *io = NULL;
  287. MemoryIoInfo *info = NULL;
  288. io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  289. GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
  290. info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
  291. GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
  292. memset(info, '\0', sizeof (*info));
  293. info->buf = (const PHYSFS_uint8 *) buf;
  294. info->len = len;
  295. info->pos = 0;
  296. info->parent = NULL;
  297. info->refcount = 1;
  298. info->destruct = destruct;
  299. memcpy(io, &__PHYSFS_memoryIoInterface, sizeof (*io));
  300. io->opaque = info;
  301. return io;
  302. createMemoryIo_failed:
  303. if (info != NULL) allocator.Free(info);
  304. if (io != NULL) allocator.Free(io);
  305. return NULL;
  306. } /* __PHYSFS_createMemoryIo */
  307. /* PHYSFS_Io implementation for i/o to a PHYSFS_File... */
  308. static PHYSFS_sint64 handleIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  309. {
  310. return PHYSFS_readBytes((PHYSFS_File *) io->opaque, buf, len);
  311. } /* handleIo_read */
  312. static PHYSFS_sint64 handleIo_write(PHYSFS_Io *io, const void *buffer,
  313. PHYSFS_uint64 len)
  314. {
  315. return PHYSFS_writeBytes((PHYSFS_File *) io->opaque, buffer, len);
  316. } /* handleIo_write */
  317. static int handleIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  318. {
  319. return PHYSFS_seek((PHYSFS_File *) io->opaque, offset);
  320. } /* handleIo_seek */
  321. static PHYSFS_sint64 handleIo_tell(PHYSFS_Io *io)
  322. {
  323. return PHYSFS_tell((PHYSFS_File *) io->opaque);
  324. } /* handleIo_tell */
  325. static PHYSFS_sint64 handleIo_length(PHYSFS_Io *io)
  326. {
  327. return PHYSFS_fileLength((PHYSFS_File *) io->opaque);
  328. } /* handleIo_length */
  329. static PHYSFS_Io *handleIo_duplicate(PHYSFS_Io *io)
  330. {
  331. /*
  332. * There's no duplicate at the PHYSFS_File level, so we break the
  333. * abstraction. We're allowed to: we're physfs.c!
  334. */
  335. FileHandle *origfh = (FileHandle *) io->opaque;
  336. FileHandle *newfh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  337. PHYSFS_Io *retval = NULL;
  338. GOTO_IF_MACRO(!newfh, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
  339. memset(newfh, '\0', sizeof (*newfh));
  340. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  341. GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
  342. #if 0 /* we don't buffer the duplicate, at least not at the moment. */
  343. if (origfh->buffer != NULL)
  344. {
  345. newfh->buffer = (PHYSFS_uint8 *) allocator.Malloc(origfh->bufsize);
  346. if (!newfh->buffer)
  347. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
  348. newfh->bufsize = origfh->bufsize;
  349. } /* if */
  350. #endif
  351. newfh->io = origfh->io->duplicate(origfh->io);
  352. GOTO_IF_MACRO(!newfh->io, ERRPASS, handleIo_dupe_failed);
  353. newfh->forReading = origfh->forReading;
  354. newfh->dirHandle = origfh->dirHandle;
  355. __PHYSFS_platformGrabMutex(stateLock);
  356. if (newfh->forReading)
  357. {
  358. newfh->next = openReadList;
  359. openReadList = newfh;
  360. } /* if */
  361. else
  362. {
  363. newfh->next = openWriteList;
  364. openWriteList = newfh;
  365. } /* else */
  366. __PHYSFS_platformReleaseMutex(stateLock);
  367. memcpy(retval, io, sizeof (PHYSFS_Io));
  368. retval->opaque = newfh;
  369. return retval;
  370. handleIo_dupe_failed:
  371. if (newfh)
  372. {
  373. if (newfh->io != NULL) newfh->io->destroy(newfh->io);
  374. if (newfh->buffer != NULL) allocator.Free(newfh->buffer);
  375. allocator.Free(newfh);
  376. } /* if */
  377. return NULL;
  378. } /* handleIo_duplicate */
  379. static int handleIo_flush(PHYSFS_Io *io)
  380. {
  381. return PHYSFS_flush((PHYSFS_File *) io->opaque);
  382. } /* handleIo_flush */
  383. static void handleIo_destroy(PHYSFS_Io *io)
  384. {
  385. if (io->opaque != NULL)
  386. PHYSFS_close((PHYSFS_File *) io->opaque);
  387. allocator.Free(io);
  388. } /* handleIo_destroy */
  389. static const PHYSFS_Io __PHYSFS_handleIoInterface =
  390. {
  391. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  392. handleIo_read,
  393. handleIo_write,
  394. handleIo_seek,
  395. handleIo_tell,
  396. handleIo_length,
  397. handleIo_duplicate,
  398. handleIo_flush,
  399. handleIo_destroy
  400. };
  401. static PHYSFS_Io *__PHYSFS_createHandleIo(PHYSFS_File *f)
  402. {
  403. PHYSFS_Io *io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  404. BAIL_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  405. memcpy(io, &__PHYSFS_handleIoInterface, sizeof (*io));
  406. io->opaque = f;
  407. return io;
  408. } /* __PHYSFS_createHandleIo */
  409. /* functions ... */
  410. typedef struct
  411. {
  412. char **list;
  413. PHYSFS_uint32 size;
  414. PHYSFS_ErrorCode errcode;
  415. } EnumStringListCallbackData;
  416. static void enumStringListCallback(void *data, const char *str)
  417. {
  418. void *ptr;
  419. char *newstr;
  420. EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
  421. if (pecd->errcode)
  422. return;
  423. ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
  424. newstr = (char *) allocator.Malloc(strlen(str) + 1);
  425. if (ptr != NULL)
  426. pecd->list = (char **) ptr;
  427. if ((ptr == NULL) || (newstr == NULL))
  428. {
  429. pecd->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
  430. pecd->list[pecd->size] = NULL;
  431. PHYSFS_freeList(pecd->list);
  432. return;
  433. } /* if */
  434. strcpy(newstr, str);
  435. pecd->list[pecd->size] = newstr;
  436. pecd->size++;
  437. } /* enumStringListCallback */
  438. static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
  439. {
  440. EnumStringListCallbackData ecd;
  441. memset(&ecd, '\0', sizeof (ecd));
  442. ecd.list = (char **) allocator.Malloc(sizeof (char *));
  443. BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  444. func(enumStringListCallback, &ecd);
  445. if (ecd.errcode)
  446. {
  447. PHYSFS_setErrorCode(ecd.errcode);
  448. return NULL;
  449. } /* if */
  450. ecd.list[ecd.size] = NULL;
  451. return ecd.list;
  452. } /* doEnumStringList */
  453. static void __PHYSFS_bubble_sort(void *a, size_t lo, size_t hi,
  454. int (*cmpfn)(void *, size_t, size_t),
  455. void (*swapfn)(void *, size_t, size_t))
  456. {
  457. size_t i;
  458. int sorted;
  459. do
  460. {
  461. sorted = 1;
  462. for (i = lo; i < hi; i++)
  463. {
  464. if (cmpfn(a, i, i + 1) > 0)
  465. {
  466. swapfn(a, i, i + 1);
  467. sorted = 0;
  468. } /* if */
  469. } /* for */
  470. } while (!sorted);
  471. } /* __PHYSFS_bubble_sort */
  472. static void __PHYSFS_quick_sort(void *a, size_t lo, size_t hi,
  473. int (*cmpfn)(void *, size_t, size_t),
  474. void (*swapfn)(void *, size_t, size_t))
  475. {
  476. size_t i;
  477. size_t j;
  478. size_t v;
  479. if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
  480. __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
  481. else
  482. {
  483. i = (hi + lo) / 2;
  484. if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
  485. if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
  486. if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
  487. j = hi - 1;
  488. swapfn(a, i, j);
  489. i = lo;
  490. v = j;
  491. while (1)
  492. {
  493. while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
  494. while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
  495. if (j < i)
  496. break;
  497. swapfn(a, i, j);
  498. } /* while */
  499. if (i != (hi-1))
  500. swapfn(a, i, hi-1);
  501. __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
  502. __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
  503. } /* else */
  504. } /* __PHYSFS_quick_sort */
  505. void __PHYSFS_sort(void *entries, size_t max,
  506. int (*cmpfn)(void *, size_t, size_t),
  507. void (*swapfn)(void *, size_t, size_t))
  508. {
  509. /*
  510. * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
  511. * https://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  512. */
  513. if (max > 0)
  514. __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
  515. } /* __PHYSFS_sort */
  516. static ErrState *findErrorForCurrentThread(void)
  517. {
  518. ErrState *i;
  519. void *tid;
  520. if (errorLock != NULL)
  521. __PHYSFS_platformGrabMutex(errorLock);
  522. if (errorStates != NULL)
  523. {
  524. tid = __PHYSFS_platformGetThreadID();
  525. for (i = errorStates; i != NULL; i = i->next)
  526. {
  527. if (i->tid == tid)
  528. {
  529. if (errorLock != NULL)
  530. __PHYSFS_platformReleaseMutex(errorLock);
  531. return i;
  532. } /* if */
  533. } /* for */
  534. } /* if */
  535. if (errorLock != NULL)
  536. __PHYSFS_platformReleaseMutex(errorLock);
  537. return NULL; /* no error available. */
  538. } /* findErrorForCurrentThread */
  539. /* this doesn't reset the error state. */
  540. static inline PHYSFS_ErrorCode currentErrorCode(void)
  541. {
  542. const ErrState *err = findErrorForCurrentThread();
  543. return err ? err->code : PHYSFS_ERR_OK;
  544. } /* currentErrorCode */
  545. PHYSFS_ErrorCode PHYSFS_getLastErrorCode(void)
  546. {
  547. ErrState *err = findErrorForCurrentThread();
  548. const PHYSFS_ErrorCode retval = (err) ? err->code : PHYSFS_ERR_OK;
  549. if (err)
  550. err->code = PHYSFS_ERR_OK;
  551. return retval;
  552. } /* PHYSFS_getLastErrorCode */
  553. PHYSFS_DECL const char *PHYSFS_getErrorByCode(PHYSFS_ErrorCode code)
  554. {
  555. switch (code)
  556. {
  557. case PHYSFS_ERR_OK: return "no error";
  558. case PHYSFS_ERR_OTHER_ERROR: return "unknown error";
  559. case PHYSFS_ERR_OUT_OF_MEMORY: return "out of memory";
  560. case PHYSFS_ERR_NOT_INITIALIZED: return "not initialized";
  561. case PHYSFS_ERR_IS_INITIALIZED: return "already initialized";
  562. case PHYSFS_ERR_ARGV0_IS_NULL: return "argv[0] is NULL";
  563. case PHYSFS_ERR_UNSUPPORTED: return "unsupported";
  564. case PHYSFS_ERR_PAST_EOF: return "past end of file";
  565. case PHYSFS_ERR_FILES_STILL_OPEN: return "files still open";
  566. case PHYSFS_ERR_INVALID_ARGUMENT: return "invalid argument";
  567. case PHYSFS_ERR_NOT_MOUNTED: return "not mounted";
  568. case PHYSFS_ERR_NOT_FOUND: return "not found";
  569. case PHYSFS_ERR_SYMLINK_FORBIDDEN: return "symlinks are forbidden";
  570. case PHYSFS_ERR_NO_WRITE_DIR: return "write directory is not set";
  571. case PHYSFS_ERR_OPEN_FOR_READING: return "file open for reading";
  572. case PHYSFS_ERR_OPEN_FOR_WRITING: return "file open for writing";
  573. case PHYSFS_ERR_NOT_A_FILE: return "not a file";
  574. case PHYSFS_ERR_READ_ONLY: return "read-only filesystem";
  575. case PHYSFS_ERR_CORRUPT: return "corrupted";
  576. case PHYSFS_ERR_SYMLINK_LOOP: return "infinite symbolic link loop";
  577. case PHYSFS_ERR_IO: return "i/o error";
  578. case PHYSFS_ERR_PERMISSION: return "permission denied";
  579. case PHYSFS_ERR_NO_SPACE: return "no space available for writing";
  580. case PHYSFS_ERR_BAD_FILENAME: return "filename is illegal or insecure";
  581. case PHYSFS_ERR_BUSY: return "tried to modify a file the OS needs";
  582. case PHYSFS_ERR_DIR_NOT_EMPTY: return "directory isn't empty";
  583. case PHYSFS_ERR_OS_ERROR: return "OS reported an error";
  584. case PHYSFS_ERR_DUPLICATE: return "duplicate resource";
  585. } /* switch */
  586. return NULL; /* don't know this error code. */
  587. } /* PHYSFS_getErrorByCode */
  588. void PHYSFS_setErrorCode(PHYSFS_ErrorCode errcode)
  589. {
  590. ErrState *err;
  591. if (!errcode)
  592. return;
  593. err = findErrorForCurrentThread();
  594. if (err == NULL)
  595. {
  596. err = (ErrState *) allocator.Malloc(sizeof (ErrState));
  597. if (err == NULL)
  598. return; /* uhh...? */
  599. memset(err, '\0', sizeof (ErrState));
  600. err->tid = __PHYSFS_platformGetThreadID();
  601. if (errorLock != NULL)
  602. __PHYSFS_platformGrabMutex(errorLock);
  603. err->next = errorStates;
  604. errorStates = err;
  605. if (errorLock != NULL)
  606. __PHYSFS_platformReleaseMutex(errorLock);
  607. } /* if */
  608. err->code = errcode;
  609. } /* PHYSFS_setErrorCode */
  610. const char *PHYSFS_getLastError(void)
  611. {
  612. const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
  613. return (err) ? PHYSFS_getErrorByCode(err) : NULL;
  614. } /* PHYSFS_getLastError */
  615. /* MAKE SURE that errorLock is held before calling this! */
  616. static void freeErrorStates(void)
  617. {
  618. ErrState *i;
  619. ErrState *next;
  620. for (i = errorStates; i != NULL; i = next)
  621. {
  622. next = i->next;
  623. allocator.Free(i);
  624. } /* for */
  625. errorStates = NULL;
  626. } /* freeErrorStates */
  627. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  628. {
  629. if (ver != NULL)
  630. {
  631. ver->major = PHYSFS_VER_MAJOR;
  632. ver->minor = PHYSFS_VER_MINOR;
  633. ver->patch = PHYSFS_VER_PATCH;
  634. } /* if */
  635. } /* PHYSFS_getLinkedVersion */
  636. static const char *find_filename_extension(const char *fname)
  637. {
  638. const char *retval = NULL;
  639. if (fname != NULL)
  640. {
  641. const char *p = strchr(fname, '.');
  642. retval = p;
  643. while (p != NULL)
  644. {
  645. p = strchr(p + 1, '.');
  646. if (p != NULL)
  647. retval = p;
  648. } /* while */
  649. if (retval != NULL)
  650. retval++; /* skip '.' */
  651. } /* if */
  652. return retval;
  653. } /* find_filename_extension */
  654. static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
  655. const char *d, int forWriting)
  656. {
  657. DirHandle *retval = NULL;
  658. void *opaque = NULL;
  659. if (io != NULL)
  660. BAIL_IF_MACRO(!io->seek(io, 0), ERRPASS, NULL);
  661. opaque = funcs->openArchive(io, d, forWriting);
  662. if (opaque != NULL)
  663. {
  664. retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
  665. if (retval == NULL)
  666. funcs->closeArchive(opaque);
  667. else
  668. {
  669. memset(retval, '\0', sizeof (DirHandle));
  670. retval->mountPoint = NULL;
  671. retval->funcs = funcs;
  672. retval->opaque = opaque;
  673. } /* else */
  674. } /* if */
  675. return retval;
  676. } /* tryOpenDir */
  677. static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
  678. {
  679. DirHandle *retval = NULL;
  680. const PHYSFS_Archiver **i;
  681. const char *ext;
  682. int created_io = 0;
  683. assert((io != NULL) || (d != NULL));
  684. if (io == NULL)
  685. {
  686. /* DIR gets first shot (unlike the rest, it doesn't deal with files). */
  687. extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
  688. retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting);
  689. if (retval != NULL)
  690. return retval;
  691. io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
  692. BAIL_IF_MACRO(!io, ERRPASS, 0);
  693. created_io = 1;
  694. } /* if */
  695. ext = find_filename_extension(d);
  696. if (ext != NULL)
  697. {
  698. /* Look for archivers with matching file extensions first... */
  699. for (i = archivers; (*i != NULL) && (retval == NULL); i++)
  700. {
  701. if (__PHYSFS_utf8stricmp(ext, (*i)->info.extension) == 0)
  702. retval = tryOpenDir(io, *i, d, forWriting);
  703. } /* for */
  704. /* failing an exact file extension match, try all the others... */
  705. for (i = archivers; (*i != NULL) && (retval == NULL); i++)
  706. {
  707. if (__PHYSFS_utf8stricmp(ext, (*i)->info.extension) != 0)
  708. retval = tryOpenDir(io, *i, d, forWriting);
  709. } /* for */
  710. } /* if */
  711. else /* no extension? Try them all. */
  712. {
  713. for (i = archivers; (*i != NULL) && (retval == NULL); i++)
  714. retval = tryOpenDir(io, *i, d, forWriting);
  715. } /* else */
  716. if ((!retval) && (created_io))
  717. io->destroy(io);
  718. BAIL_IF_MACRO(!retval, PHYSFS_ERR_UNSUPPORTED, NULL);
  719. return retval;
  720. } /* openDirectory */
  721. /*
  722. * Make a platform-independent path string sane. Doesn't actually check the
  723. * file hierarchy, it just cleans up the string.
  724. * (dst) must be a buffer at least as big as (src), as this is where the
  725. * cleaned up string is deposited.
  726. * If there are illegal bits in the path (".." entries, etc) then we
  727. * return zero and (dst) is undefined. Non-zero if the path was sanitized.
  728. */
  729. static int sanitizePlatformIndependentPath(const char *src, char *dst)
  730. {
  731. char *prev;
  732. char ch;
  733. while (*src == '/') /* skip initial '/' chars... */
  734. src++;
  735. prev = dst;
  736. do
  737. {
  738. ch = *(src++);
  739. if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
  740. BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
  741. if (ch == '/') /* path separator. */
  742. {
  743. *dst = '\0'; /* "." and ".." are illegal pathnames. */
  744. if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
  745. BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
  746. while (*src == '/') /* chop out doubles... */
  747. src++;
  748. if (*src == '\0') /* ends with a pathsep? */
  749. break; /* we're done, don't add final pathsep to dst. */
  750. prev = dst + 1;
  751. } /* if */
  752. *(dst++) = ch;
  753. } while (ch != '\0');
  754. return 1;
  755. } /* sanitizePlatformIndependentPath */
  756. /*
  757. * Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
  758. * output from sanitizePlatformIndependentPath(), so that it is in a known
  759. * state.
  760. *
  761. * This only finds legitimate segments of a mountpoint. If the mountpoint is
  762. * "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
  763. * all zero. "/a/b" will succeed, though.
  764. */
  765. static int partOfMountPoint(DirHandle *h, char *fname)
  766. {
  767. /* !!! FIXME: This code feels gross. */
  768. int rc;
  769. size_t len, mntpntlen;
  770. if (h->mountPoint == NULL)
  771. return 0;
  772. else if (*fname == '\0')
  773. return 1;
  774. len = strlen(fname);
  775. mntpntlen = strlen(h->mountPoint);
  776. if (len > mntpntlen) /* can't be a subset of mountpoint. */
  777. return 0;
  778. /* if true, must be not a match or a complete match, but not a subset. */
  779. if ((len + 1) == mntpntlen)
  780. return 0;
  781. rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
  782. if (rc != 0)
  783. return 0; /* not a match. */
  784. /* make sure /a/b matches /a/b/ and not /a/bc ... */
  785. return h->mountPoint[len] == '/';
  786. } /* partOfMountPoint */
  787. static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
  788. const char *mountPoint, int forWriting)
  789. {
  790. DirHandle *dirHandle = NULL;
  791. char *tmpmntpnt = NULL;
  792. if (mountPoint != NULL)
  793. {
  794. const size_t len = strlen(mountPoint) + 1;
  795. tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
  796. GOTO_IF_MACRO(!tmpmntpnt, PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  797. if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
  798. goto badDirHandle;
  799. mountPoint = tmpmntpnt; /* sanitized version. */
  800. } /* if */
  801. dirHandle = openDirectory(io, newDir, forWriting);
  802. GOTO_IF_MACRO(!dirHandle, ERRPASS, badDirHandle);
  803. if (newDir == NULL)
  804. dirHandle->dirName = NULL;
  805. else
  806. {
  807. dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
  808. if (!dirHandle->dirName)
  809. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  810. strcpy(dirHandle->dirName, newDir);
  811. } /* else */
  812. if ((mountPoint != NULL) && (*mountPoint != '\0'))
  813. {
  814. dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
  815. if (!dirHandle->mountPoint)
  816. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  817. strcpy(dirHandle->mountPoint, mountPoint);
  818. strcat(dirHandle->mountPoint, "/");
  819. } /* if */
  820. __PHYSFS_smallFree(tmpmntpnt);
  821. return dirHandle;
  822. badDirHandle:
  823. if (dirHandle != NULL)
  824. {
  825. dirHandle->funcs->closeArchive(dirHandle->opaque);
  826. allocator.Free(dirHandle->dirName);
  827. allocator.Free(dirHandle->mountPoint);
  828. allocator.Free(dirHandle);
  829. } /* if */
  830. __PHYSFS_smallFree(tmpmntpnt);
  831. return NULL;
  832. } /* createDirHandle */
  833. /* MAKE SURE you've got the stateLock held before calling this! */
  834. static int freeDirHandle(DirHandle *dh, FileHandle *openList)
  835. {
  836. FileHandle *i;
  837. if (dh == NULL)
  838. return 1;
  839. for (i = openList; i != NULL; i = i->next)
  840. BAIL_IF_MACRO(i->dirHandle == dh, PHYSFS_ERR_FILES_STILL_OPEN, 0);
  841. dh->funcs->closeArchive(dh->opaque);
  842. allocator.Free(dh->dirName);
  843. allocator.Free(dh->mountPoint);
  844. allocator.Free(dh);
  845. return 1;
  846. } /* freeDirHandle */
  847. static char *calculateBaseDir(const char *argv0)
  848. {
  849. const char dirsep = __PHYSFS_platformDirSeparator;
  850. char *retval = NULL;
  851. char *ptr = NULL;
  852. /* Give the platform layer first shot at this. */
  853. retval = __PHYSFS_platformCalcBaseDir(argv0);
  854. if (retval != NULL)
  855. return retval;
  856. /* We need argv0 to go on. */
  857. BAIL_IF_MACRO(argv0 == NULL, PHYSFS_ERR_ARGV0_IS_NULL, NULL);
  858. ptr = strrchr(argv0, dirsep);
  859. if (ptr != NULL)
  860. {
  861. const size_t size = ((size_t) (ptr - argv0)) + 1;
  862. retval = (char *) allocator.Malloc(size + 1);
  863. BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  864. memcpy(retval, argv0, size);
  865. retval[size] = '\0';
  866. return retval;
  867. } /* if */
  868. /* argv0 wasn't helpful. */
  869. BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  870. } /* calculateBaseDir */
  871. static int initializeMutexes(void)
  872. {
  873. errorLock = __PHYSFS_platformCreateMutex();
  874. if (errorLock == NULL)
  875. goto initializeMutexes_failed;
  876. stateLock = __PHYSFS_platformCreateMutex();
  877. if (stateLock == NULL)
  878. goto initializeMutexes_failed;
  879. return 1; /* success. */
  880. initializeMutexes_failed:
  881. if (errorLock != NULL)
  882. __PHYSFS_platformDestroyMutex(errorLock);
  883. if (stateLock != NULL)
  884. __PHYSFS_platformDestroyMutex(stateLock);
  885. errorLock = stateLock = NULL;
  886. return 0; /* failed. */
  887. } /* initializeMutexes */
  888. static int doRegisterArchiver(const PHYSFS_Archiver *_archiver);
  889. static int initStaticArchivers(void)
  890. {
  891. #define REGISTER_STATIC_ARCHIVER(arc) { \
  892. extern const PHYSFS_Archiver __PHYSFS_Archiver_##arc; \
  893. if (!doRegisterArchiver(&__PHYSFS_Archiver_##arc)) { \
  894. return 0; \
  895. } \
  896. }
  897. #if PHYSFS_SUPPORTS_ZIP
  898. REGISTER_STATIC_ARCHIVER(ZIP);
  899. #endif
  900. #if PHYSFS_SUPPORTS_7Z
  901. REGISTER_STATIC_ARCHIVER(LZMA);
  902. #endif
  903. #if PHYSFS_SUPPORTS_GRP
  904. REGISTER_STATIC_ARCHIVER(GRP);
  905. #endif
  906. #if PHYSFS_SUPPORTS_QPAK
  907. REGISTER_STATIC_ARCHIVER(QPAK);
  908. #endif
  909. #if PHYSFS_SUPPORTS_HOG
  910. REGISTER_STATIC_ARCHIVER(HOG);
  911. #endif
  912. #if PHYSFS_SUPPORTS_MVL
  913. REGISTER_STATIC_ARCHIVER(MVL);
  914. #endif
  915. #if PHYSFS_SUPPORTS_WAD
  916. REGISTER_STATIC_ARCHIVER(WAD);
  917. #endif
  918. #if PHYSFS_SUPPORTS_SLB
  919. REGISTER_STATIC_ARCHIVER(SLB);
  920. #endif
  921. #if PHYSFS_SUPPORTS_ISO9660
  922. REGISTER_STATIC_ARCHIVER(ISO9660);
  923. #endif
  924. #undef REGISTER_STATIC_ARCHIVER
  925. return 1;
  926. } /* initStaticArchivers */
  927. static void setDefaultAllocator(void);
  928. static int doDeinit(void);
  929. int PHYSFS_init(const char *argv0)
  930. {
  931. BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
  932. if (!externalAllocator)
  933. setDefaultAllocator();
  934. if ((allocator.Init != NULL) && (!allocator.Init())) return 0;
  935. if (!__PHYSFS_platformInit())
  936. {
  937. if (allocator.Deinit != NULL) allocator.Deinit();
  938. return 0;
  939. } /* if */
  940. /* everything below here can be cleaned up safely by doDeinit(). */
  941. if (!initializeMutexes()) goto initFailed;
  942. baseDir = calculateBaseDir(argv0);
  943. if (!baseDir) goto initFailed;
  944. userDir = __PHYSFS_platformCalcUserDir();
  945. if (!userDir) goto initFailed;
  946. /* Platform layer is required to append a dirsep. */
  947. assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator);
  948. assert(userDir[strlen(userDir) - 1] == __PHYSFS_platformDirSeparator);
  949. if (!initStaticArchivers()) goto initFailed;
  950. initialized = 1;
  951. /* This makes sure that the error subsystem is initialized. */
  952. PHYSFS_setErrorCode(PHYSFS_getLastErrorCode());
  953. return 1;
  954. initFailed:
  955. doDeinit();
  956. return 0;
  957. } /* PHYSFS_init */
  958. /* MAKE SURE you hold stateLock before calling this! */
  959. static int closeFileHandleList(FileHandle **list)
  960. {
  961. FileHandle *i;
  962. FileHandle *next = NULL;
  963. for (i = *list; i != NULL; i = next)
  964. {
  965. PHYSFS_Io *io = i->io;
  966. next = i->next;
  967. if (io->flush && !io->flush(io))
  968. {
  969. *list = i;
  970. return 0;
  971. } /* if */
  972. io->destroy(io);
  973. allocator.Free(i);
  974. } /* for */
  975. *list = NULL;
  976. return 1;
  977. } /* closeFileHandleList */
  978. /* MAKE SURE you hold the stateLock before calling this! */
  979. static void freeSearchPath(void)
  980. {
  981. DirHandle *i;
  982. DirHandle *next = NULL;
  983. closeFileHandleList(&openReadList);
  984. if (searchPath != NULL)
  985. {
  986. for (i = searchPath; i != NULL; i = next)
  987. {
  988. next = i->next;
  989. freeDirHandle(i, openReadList);
  990. } /* for */
  991. searchPath = NULL;
  992. } /* if */
  993. } /* freeSearchPath */
  994. /* MAKE SURE you hold stateLock before calling this! */
  995. static int archiverInUse(const PHYSFS_Archiver *arc, const DirHandle *list)
  996. {
  997. const DirHandle *i;
  998. for (i = list; i != NULL; i = i->next)
  999. {
  1000. if (i->funcs == arc)
  1001. return 1;
  1002. } /* for */
  1003. return 0; /* not in use */
  1004. } /* archiverInUse */
  1005. /* MAKE SURE you hold stateLock before calling this! */
  1006. static int doDeregisterArchiver(const size_t idx)
  1007. {
  1008. const size_t len = (numArchivers - idx) * sizeof (void *);
  1009. const PHYSFS_ArchiveInfo *info = archiveInfo[idx];
  1010. const PHYSFS_Archiver *arc = archivers[idx];
  1011. /* make sure nothing is still using this archiver */
  1012. if (archiverInUse(arc, searchPath) || archiverInUse(arc, writeDir))
  1013. BAIL_MACRO(PHYSFS_ERR_FILES_STILL_OPEN, 0);
  1014. allocator.Free((void *) info->extension);
  1015. allocator.Free((void *) info->description);
  1016. allocator.Free((void *) info->author);
  1017. allocator.Free((void *) info->url);
  1018. allocator.Free((void *) arc);
  1019. memmove(&archiveInfo[idx], &archiveInfo[idx+1], len);
  1020. memmove(&archivers[idx], &archivers[idx+1], len);
  1021. assert(numArchivers > 0);
  1022. numArchivers--;
  1023. return 1;
  1024. } /* doDeregisterArchiver */
  1025. /* Does NOT hold the state lock; we're shutting down. */
  1026. static void freeArchivers(void)
  1027. {
  1028. while (numArchivers > 0)
  1029. {
  1030. if (!doDeregisterArchiver(numArchivers - 1))
  1031. assert(!"nothing should be mounted during shutdown.");
  1032. } /* while */
  1033. allocator.Free(archivers);
  1034. allocator.Free(archiveInfo);
  1035. archivers = NULL;
  1036. archiveInfo = NULL;
  1037. } /* freeArchivers */
  1038. static int doDeinit(void)
  1039. {
  1040. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), ERRPASS, 0);
  1041. closeFileHandleList(&openWriteList);
  1042. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), PHYSFS_ERR_FILES_STILL_OPEN, 0);
  1043. freeSearchPath();
  1044. freeArchivers();
  1045. freeErrorStates();
  1046. if (baseDir != NULL)
  1047. {
  1048. allocator.Free(baseDir);
  1049. baseDir = NULL;
  1050. } /* if */
  1051. if (userDir != NULL)
  1052. {
  1053. allocator.Free(userDir);
  1054. userDir = NULL;
  1055. } /* if */
  1056. if (prefDir != NULL)
  1057. {
  1058. allocator.Free(prefDir);
  1059. prefDir = NULL;
  1060. } /* if */
  1061. if (archiveInfo != NULL)
  1062. {
  1063. allocator.Free(archiveInfo);
  1064. archiveInfo = NULL;
  1065. } /* if */
  1066. if (archivers != NULL)
  1067. {
  1068. allocator.Free(archivers);
  1069. archivers = NULL;
  1070. } /* if */
  1071. allowSymLinks = 0;
  1072. initialized = 0;
  1073. if (errorLock) __PHYSFS_platformDestroyMutex(errorLock);
  1074. if (stateLock) __PHYSFS_platformDestroyMutex(stateLock);
  1075. if (allocator.Deinit != NULL)
  1076. allocator.Deinit();
  1077. errorLock = stateLock = NULL;
  1078. return 1;
  1079. } /* doDeinit */
  1080. int PHYSFS_deinit(void)
  1081. {
  1082. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1083. return doDeinit();
  1084. } /* PHYSFS_deinit */
  1085. int PHYSFS_isInit(void)
  1086. {
  1087. return initialized;
  1088. } /* PHYSFS_isInit */
  1089. char *__PHYSFS_strdup(const char *str)
  1090. {
  1091. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  1092. if (retval)
  1093. strcpy(retval, str);
  1094. return retval;
  1095. } /* __PHYSFS_strdup */
  1096. PHYSFS_uint32 __PHYSFS_hashString(const char *str, size_t len)
  1097. {
  1098. PHYSFS_uint32 hash = 5381;
  1099. while (len--)
  1100. hash = ((hash << 5) + hash) ^ *(str++);
  1101. return hash;
  1102. } /* __PHYSFS_hashString */
  1103. /* MAKE SURE you hold stateLock before calling this! */
  1104. static int doRegisterArchiver(const PHYSFS_Archiver *_archiver)
  1105. {
  1106. const PHYSFS_uint32 maxver = CURRENT_PHYSFS_ARCHIVER_API_VERSION;
  1107. const size_t len = (numArchivers + 2) * sizeof (void *);
  1108. PHYSFS_Archiver *archiver = NULL;
  1109. PHYSFS_ArchiveInfo *info = NULL;
  1110. const char *ext = NULL;
  1111. void *ptr = NULL;
  1112. size_t i;
  1113. BAIL_IF_MACRO(!_archiver, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1114. BAIL_IF_MACRO(_archiver->version > maxver, PHYSFS_ERR_UNSUPPORTED, 0);
  1115. BAIL_IF_MACRO(!_archiver->info.extension, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1116. BAIL_IF_MACRO(!_archiver->info.description, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1117. BAIL_IF_MACRO(!_archiver->info.author, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1118. BAIL_IF_MACRO(!_archiver->info.url, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1119. BAIL_IF_MACRO(!_archiver->openArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1120. BAIL_IF_MACRO(!_archiver->enumerateFiles, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1121. BAIL_IF_MACRO(!_archiver->openRead, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1122. BAIL_IF_MACRO(!_archiver->openWrite, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1123. BAIL_IF_MACRO(!_archiver->openAppend, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1124. BAIL_IF_MACRO(!_archiver->remove, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1125. BAIL_IF_MACRO(!_archiver->mkdir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1126. BAIL_IF_MACRO(!_archiver->closeArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1127. BAIL_IF_MACRO(!_archiver->stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1128. ext = _archiver->info.extension;
  1129. for (i = 0; i < numArchivers; i++)
  1130. {
  1131. if (__PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
  1132. BAIL_MACRO(PHYSFS_ERR_DUPLICATE, 0); /* !!! FIXME: better error? ERR_IN_USE? */
  1133. } /* for */
  1134. /* make a copy of the data. */
  1135. archiver = (PHYSFS_Archiver *) allocator.Malloc(sizeof (*archiver));
  1136. GOTO_IF_MACRO(!archiver, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1137. /* Must copy sizeof (OLD_VERSION_OF_STRUCT) when version changes! */
  1138. memcpy(archiver, _archiver, sizeof (*archiver));
  1139. info = (PHYSFS_ArchiveInfo *) &archiver->info;
  1140. memset(info, '\0', sizeof (*info)); /* NULL in case an alloc fails. */
  1141. #define CPYSTR(item) \
  1142. info->item = __PHYSFS_strdup(_archiver->info.item); \
  1143. GOTO_IF_MACRO(!info->item, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1144. CPYSTR(extension);
  1145. CPYSTR(description);
  1146. CPYSTR(author);
  1147. CPYSTR(url);
  1148. info->supportsSymlinks = _archiver->info.supportsSymlinks;
  1149. #undef CPYSTR
  1150. ptr = allocator.Realloc(archiveInfo, len);
  1151. GOTO_IF_MACRO(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1152. archiveInfo = (const PHYSFS_ArchiveInfo **) ptr;
  1153. ptr = allocator.Realloc(archivers, len);
  1154. GOTO_IF_MACRO(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1155. archivers = (const PHYSFS_Archiver **) ptr;
  1156. archiveInfo[numArchivers] = info;
  1157. archiveInfo[numArchivers + 1] = NULL;
  1158. archivers[numArchivers] = archiver;
  1159. archivers[numArchivers + 1] = NULL;
  1160. numArchivers++;
  1161. return 1;
  1162. regfailed:
  1163. if (info != NULL)
  1164. {
  1165. allocator.Free((void *) info->extension);
  1166. allocator.Free((void *) info->description);
  1167. allocator.Free((void *) info->author);
  1168. allocator.Free((void *) info->url);
  1169. } /* if */
  1170. allocator.Free(archiver);
  1171. return 0;
  1172. } /* doRegisterArchiver */
  1173. int PHYSFS_registerArchiver(const PHYSFS_Archiver *archiver)
  1174. {
  1175. int retval;
  1176. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1177. __PHYSFS_platformGrabMutex(stateLock);
  1178. retval = doRegisterArchiver(archiver);
  1179. __PHYSFS_platformReleaseMutex(stateLock);
  1180. return retval;
  1181. } /* PHYSFS_registerArchiver */
  1182. int PHYSFS_deregisterArchiver(const char *ext)
  1183. {
  1184. size_t i;
  1185. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1186. BAIL_IF_MACRO(!ext, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1187. __PHYSFS_platformGrabMutex(stateLock);
  1188. for (i = 0; i < numArchivers; i++)
  1189. {
  1190. if (__PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
  1191. {
  1192. const int retval = doDeregisterArchiver(i);
  1193. __PHYSFS_platformReleaseMutex(stateLock);
  1194. return retval;
  1195. } /* if */
  1196. } /* for */
  1197. __PHYSFS_platformReleaseMutex(stateLock);
  1198. BAIL_MACRO(PHYSFS_ERR_NOT_FOUND, 0);
  1199. } /* PHYSFS_deregisterArchiver */
  1200. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  1201. {
  1202. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
  1203. return archiveInfo;
  1204. } /* PHYSFS_supportedArchiveTypes */
  1205. void PHYSFS_freeList(void *list)
  1206. {
  1207. void **i;
  1208. if (list != NULL)
  1209. {
  1210. for (i = (void **) list; *i != NULL; i++)
  1211. allocator.Free(*i);
  1212. allocator.Free(list);
  1213. } /* if */
  1214. } /* PHYSFS_freeList */
  1215. const char *PHYSFS_getDirSeparator(void)
  1216. {
  1217. static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
  1218. return retval;
  1219. } /* PHYSFS_getDirSeparator */
  1220. char **PHYSFS_getCdRomDirs(void)
  1221. {
  1222. return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
  1223. } /* PHYSFS_getCdRomDirs */
  1224. void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
  1225. {
  1226. __PHYSFS_platformDetectAvailableCDs(callback, data);
  1227. } /* PHYSFS_getCdRomDirsCallback */
  1228. const char *PHYSFS_getPrefDir(const char *org, const char *app)
  1229. {
  1230. const char dirsep = __PHYSFS_platformDirSeparator;
  1231. PHYSFS_Stat statbuf;
  1232. char *ptr = NULL;
  1233. char *endstr = NULL;
  1234. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1235. BAIL_IF_MACRO(!org, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1236. BAIL_IF_MACRO(*org == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1237. BAIL_IF_MACRO(!app, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1238. BAIL_IF_MACRO(*app == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1239. allocator.Free(prefDir);
  1240. prefDir = __PHYSFS_platformCalcPrefDir(org, app);
  1241. BAIL_IF_MACRO(!prefDir, ERRPASS, NULL);
  1242. assert(strlen(prefDir) > 0);
  1243. endstr = prefDir + (strlen(prefDir) - 1);
  1244. assert(*endstr == dirsep);
  1245. *endstr = '\0'; /* mask out the final dirsep for now. */
  1246. if (!__PHYSFS_platformStat(prefDir, &statbuf))
  1247. {
  1248. for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
  1249. {
  1250. *ptr = '\0';
  1251. __PHYSFS_platformMkDir(prefDir);
  1252. *ptr = dirsep;
  1253. } /* for */
  1254. if (!__PHYSFS_platformMkDir(prefDir))
  1255. {
  1256. allocator.Free(prefDir);
  1257. prefDir = NULL;
  1258. } /* if */
  1259. } /* if */
  1260. *endstr = dirsep; /* readd the final dirsep. */
  1261. return prefDir;
  1262. } /* PHYSFS_getPrefDir */
  1263. const char *PHYSFS_getBaseDir(void)
  1264. {
  1265. return baseDir; /* this is calculated in PHYSFS_init()... */
  1266. } /* PHYSFS_getBaseDir */
  1267. const char *__PHYSFS_getUserDir(void) /* not deprecated internal version. */
  1268. {
  1269. return userDir; /* this is calculated in PHYSFS_init()... */
  1270. } /* __PHYSFS_getUserDir */
  1271. const char *PHYSFS_getUserDir(void)
  1272. {
  1273. return __PHYSFS_getUserDir();
  1274. } /* PHYSFS_getUserDir */
  1275. const char *PHYSFS_getWriteDir(void)
  1276. {
  1277. const char *retval = NULL;
  1278. __PHYSFS_platformGrabMutex(stateLock);
  1279. if (writeDir != NULL)
  1280. retval = writeDir->dirName;
  1281. __PHYSFS_platformReleaseMutex(stateLock);
  1282. return retval;
  1283. } /* PHYSFS_getWriteDir */
  1284. int PHYSFS_setWriteDir(const char *newDir)
  1285. {
  1286. int retval = 1;
  1287. __PHYSFS_platformGrabMutex(stateLock);
  1288. if (writeDir != NULL)
  1289. {
  1290. BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), ERRPASS,
  1291. stateLock, 0);
  1292. writeDir = NULL;
  1293. } /* if */
  1294. if (newDir != NULL)
  1295. {
  1296. /* !!! FIXME: PHYSFS_Io shouldn't be NULL */
  1297. writeDir = createDirHandle(NULL, newDir, NULL, 1);
  1298. retval = (writeDir != NULL);
  1299. } /* if */
  1300. __PHYSFS_platformReleaseMutex(stateLock);
  1301. return retval;
  1302. } /* PHYSFS_setWriteDir */
  1303. static int doMount(PHYSFS_Io *io, const char *fname,
  1304. const char *mountPoint, int appendToPath)
  1305. {
  1306. DirHandle *dh;
  1307. DirHandle *prev = NULL;
  1308. DirHandle *i;
  1309. if (mountPoint == NULL)
  1310. mountPoint = "/";
  1311. __PHYSFS_platformGrabMutex(stateLock);
  1312. if (fname != NULL)
  1313. {
  1314. for (i = searchPath; i != NULL; i = i->next)
  1315. {
  1316. /* already in search path? */
  1317. if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
  1318. BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
  1319. prev = i;
  1320. } /* for */
  1321. } /* if */
  1322. dh = createDirHandle(io, fname, mountPoint, 0);
  1323. BAIL_IF_MACRO_MUTEX(!dh, ERRPASS, stateLock, 0);
  1324. if (appendToPath)
  1325. {
  1326. if (prev == NULL)
  1327. searchPath = dh;
  1328. else
  1329. prev->next = dh;
  1330. } /* if */
  1331. else
  1332. {
  1333. dh->next = searchPath;
  1334. searchPath = dh;
  1335. } /* else */
  1336. __PHYSFS_platformReleaseMutex(stateLock);
  1337. return 1;
  1338. } /* doMount */
  1339. int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
  1340. const char *mountPoint, int appendToPath)
  1341. {
  1342. BAIL_IF_MACRO(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1343. BAIL_IF_MACRO(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
  1344. return doMount(io, fname, mountPoint, appendToPath);
  1345. } /* PHYSFS_mountIo */
  1346. int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
  1347. const char *fname, const char *mountPoint,
  1348. int appendToPath)
  1349. {
  1350. int retval = 0;
  1351. PHYSFS_Io *io = NULL;
  1352. BAIL_IF_MACRO(!buf, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1353. io = __PHYSFS_createMemoryIo(buf, len, del);
  1354. BAIL_IF_MACRO(!io, ERRPASS, 0);
  1355. retval = doMount(io, fname, mountPoint, appendToPath);
  1356. if (!retval)
  1357. {
  1358. /* docs say not to call (del) in case of failure, so cheat. */
  1359. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  1360. info->destruct = NULL;
  1361. io->destroy(io);
  1362. } /* if */
  1363. return retval;
  1364. } /* PHYSFS_mountMemory */
  1365. int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
  1366. const char *mountPoint, int appendToPath)
  1367. {
  1368. int retval = 0;
  1369. PHYSFS_Io *io = NULL;
  1370. BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1371. io = __PHYSFS_createHandleIo(file);
  1372. BAIL_IF_MACRO(!io, ERRPASS, 0);
  1373. retval = doMount(io, fname, mountPoint, appendToPath);
  1374. if (!retval)
  1375. {
  1376. /* docs say not to destruct in case of failure, so cheat. */
  1377. io->opaque = NULL;
  1378. io->destroy(io);
  1379. } /* if */
  1380. return retval;
  1381. } /* PHYSFS_mountHandle */
  1382. int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
  1383. {
  1384. BAIL_IF_MACRO(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1385. return doMount(NULL, newDir, mountPoint, appendToPath);
  1386. } /* PHYSFS_mount */
  1387. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  1388. {
  1389. return doMount(NULL, newDir, NULL, appendToPath);
  1390. } /* PHYSFS_addToSearchPath */
  1391. int PHYSFS_removeFromSearchPath(const char *oldDir)
  1392. {
  1393. return PHYSFS_unmount(oldDir);
  1394. } /* PHYSFS_removeFromSearchPath */
  1395. int PHYSFS_unmount(const char *oldDir)
  1396. {
  1397. DirHandle *i;
  1398. DirHandle *prev = NULL;
  1399. DirHandle *next = NULL;
  1400. BAIL_IF_MACRO(oldDir == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1401. __PHYSFS_platformGrabMutex(stateLock);
  1402. for (i = searchPath; i != NULL; i = i->next)
  1403. {
  1404. if (strcmp(i->dirName, oldDir) == 0)
  1405. {
  1406. next = i->next;
  1407. BAIL_IF_MACRO_MUTEX(!freeDirHandle(i, openReadList), ERRPASS,
  1408. stateLock, 0);
  1409. if (prev == NULL)
  1410. searchPath = next;
  1411. else
  1412. prev->next = next;
  1413. BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
  1414. } /* if */
  1415. prev = i;
  1416. } /* for */
  1417. BAIL_MACRO_MUTEX(PHYSFS_ERR_NOT_MOUNTED, stateLock, 0);
  1418. } /* PHYSFS_unmount */
  1419. char **PHYSFS_getSearchPath(void)
  1420. {
  1421. return doEnumStringList(PHYSFS_getSearchPathCallback);
  1422. } /* PHYSFS_getSearchPath */
  1423. const char *PHYSFS_getMountPoint(const char *dir)
  1424. {
  1425. DirHandle *i;
  1426. __PHYSFS_platformGrabMutex(stateLock);
  1427. for (i = searchPath; i != NULL; i = i->next)
  1428. {
  1429. if (strcmp(i->dirName, dir) == 0)
  1430. {
  1431. const char *retval = ((i->mountPoint) ? i->mountPoint : "/");
  1432. __PHYSFS_platformReleaseMutex(stateLock);
  1433. return retval;
  1434. } /* if */
  1435. } /* for */
  1436. __PHYSFS_platformReleaseMutex(stateLock);
  1437. BAIL_MACRO(PHYSFS_ERR_NOT_MOUNTED, NULL);
  1438. } /* PHYSFS_getMountPoint */
  1439. void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
  1440. {
  1441. DirHandle *i;
  1442. __PHYSFS_platformGrabMutex(stateLock);
  1443. for (i = searchPath; i != NULL; i = i->next)
  1444. callback(data, i->dirName);
  1445. __PHYSFS_platformReleaseMutex(stateLock);
  1446. } /* PHYSFS_getSearchPathCallback */
  1447. /* Split out to avoid stack allocation in a loop. */
  1448. static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
  1449. int archivesFirst)
  1450. {
  1451. const char *d = PHYSFS_getRealDir(i);
  1452. const size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
  1453. char *str = (char *) __PHYSFS_smallAlloc(allocsize);
  1454. if (str != NULL)
  1455. {
  1456. sprintf(str, "%s%s%s", d, dirsep, i);
  1457. PHYSFS_mount(str, NULL, archivesFirst == 0);
  1458. __PHYSFS_smallFree(str);
  1459. } /* if */
  1460. } /* setSaneCfgAddPath */
  1461. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  1462. const char *archiveExt, int includeCdRoms,
  1463. int archivesFirst)
  1464. {
  1465. const char *dirsep = PHYSFS_getDirSeparator();
  1466. const char *basedir;
  1467. const char *prefdir;
  1468. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1469. prefdir = PHYSFS_getPrefDir(organization, appName);
  1470. BAIL_IF_MACRO(!prefdir, ERRPASS, 0);
  1471. basedir = PHYSFS_getBaseDir();
  1472. BAIL_IF_MACRO(!basedir, ERRPASS, 0);
  1473. BAIL_IF_MACRO(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0);
  1474. /* Put write dir first in search path... */
  1475. PHYSFS_mount(prefdir, NULL, 0);
  1476. /* Put base path on search path... */
  1477. PHYSFS_mount(basedir, NULL, 1);
  1478. /* handle CD-ROMs... */
  1479. if (includeCdRoms)
  1480. {
  1481. char **cds = PHYSFS_getCdRomDirs();
  1482. char **i;
  1483. for (i = cds; *i != NULL; i++)
  1484. PHYSFS_mount(*i, NULL, 1);
  1485. PHYSFS_freeList(cds);
  1486. } /* if */
  1487. /* Root out archives, and add them to search path... */
  1488. if (archiveExt != NULL)
  1489. {
  1490. char **rc = PHYSFS_enumerateFiles("/");
  1491. char **i;
  1492. size_t extlen = strlen(archiveExt);
  1493. char *ext;
  1494. for (i = rc; *i != NULL; i++)
  1495. {
  1496. size_t l = strlen(*i);
  1497. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  1498. {
  1499. ext = (*i) + (l - extlen);
  1500. if (__PHYSFS_utf8stricmp(ext, archiveExt) == 0)
  1501. setSaneCfgAddPath(*i, l, dirsep, archivesFirst);
  1502. } /* if */
  1503. } /* for */
  1504. PHYSFS_freeList(rc);
  1505. } /* if */
  1506. return 1;
  1507. } /* PHYSFS_setSaneConfig */
  1508. void PHYSFS_permitSymbolicLinks(int allow)
  1509. {
  1510. allowSymLinks = allow;
  1511. } /* PHYSFS_permitSymbolicLinks */
  1512. int PHYSFS_symbolicLinksPermitted(void)
  1513. {
  1514. return allowSymLinks;
  1515. } /* PHYSFS_symbolicLinksPermitted */
  1516. /*
  1517. * Verify that (fname) (in platform-independent notation), in relation
  1518. * to (h) is secure. That means that each element of fname is checked
  1519. * for symlinks (if they aren't permitted). This also allows for quick
  1520. * rejection of files that exist outside an archive's mountpoint.
  1521. *
  1522. * With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
  1523. * at a time), you should always pass zero for "allowMissing" for efficiency.
  1524. *
  1525. * (fname) must point to an output from sanitizePlatformIndependentPath(),
  1526. * since it will make sure that path names are in the right format for
  1527. * passing certain checks. It will also do checks for "insecure" pathnames
  1528. * like ".." which should be done once instead of once per archive. This also
  1529. * gives us license to treat (fname) as scratch space in this function.
  1530. *
  1531. * Returns non-zero if string is safe, zero if there's a security issue.
  1532. * PHYSFS_getLastError() will specify what was wrong. (*fname) will be
  1533. * updated to point past any mount point elements so it is prepared to
  1534. * be used with the archiver directly.
  1535. */
  1536. static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
  1537. {
  1538. char *fname = *_fname;
  1539. int retval = 1;
  1540. char *start;
  1541. char *end;
  1542. if (*fname == '\0') /* quick rejection. */
  1543. return 1;
  1544. /* !!! FIXME: This codeblock sucks. */
  1545. if (h->mountPoint != NULL) /* NULL mountpoint means "/". */
  1546. {
  1547. size_t mntpntlen = strlen(h->mountPoint);
  1548. size_t len = strlen(fname);
  1549. assert(mntpntlen > 1); /* root mount points should be NULL. */
  1550. /* not under the mountpoint, so skip this archive. */
  1551. BAIL_IF_MACRO(len < mntpntlen-1, PHYSFS_ERR_NOT_FOUND, 0);
  1552. /* !!! FIXME: Case insensitive? */
  1553. retval = strncmp(h->mountPoint, fname, mntpntlen-1);
  1554. BAIL_IF_MACRO(retval != 0, PHYSFS_ERR_NOT_FOUND, 0);
  1555. if (len > mntpntlen-1) /* corner case... */
  1556. BAIL_IF_MACRO(fname[mntpntlen-1]!='/', PHYSFS_ERR_NOT_FOUND, 0);
  1557. fname += mntpntlen-1; /* move to start of actual archive path. */
  1558. if (*fname == '/')
  1559. fname++;
  1560. *_fname = fname; /* skip mountpoint for later use. */
  1561. retval = 1; /* may be reset, below. */
  1562. } /* if */
  1563. start = fname;
  1564. if (!allowSymLinks)
  1565. {
  1566. while (1)
  1567. {
  1568. PHYSFS_Stat statbuf;
  1569. int rc = 0;
  1570. end = strchr(start, '/');
  1571. if (end != NULL) *end = '\0';
  1572. rc = h->funcs->stat(h->opaque, fname, &statbuf);
  1573. if (rc)
  1574. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  1575. else if (currentErrorCode() == PHYSFS_ERR_NOT_FOUND)
  1576. retval = 0;
  1577. if (end != NULL) *end = '/';
  1578. /* insecure path (has a disallowed symlink in it)? */
  1579. BAIL_IF_MACRO(rc, PHYSFS_ERR_SYMLINK_FORBIDDEN, 0);
  1580. /* break out early if path element is missing. */
  1581. if (!retval)
  1582. {
  1583. /*
  1584. * We need to clear it if it's the last element of the path,
  1585. * since this might be a non-existant file we're opening
  1586. * for writing...
  1587. */
  1588. if ((end == NULL) || (allowMissing))
  1589. retval = 1;
  1590. break;
  1591. } /* if */
  1592. if (end == NULL)
  1593. break;
  1594. start = end + 1;
  1595. } /* while */
  1596. } /* if */
  1597. return retval;
  1598. } /* verifyPath */
  1599. static int doMkdir(const char *_dname, char *dname)
  1600. {
  1601. DirHandle *h;
  1602. char *start;
  1603. char *end;
  1604. int retval = 0;
  1605. int exists = 1; /* force existance check on first path element. */
  1606. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), ERRPASS, 0);
  1607. __PHYSFS_platformGrabMutex(stateLock);
  1608. BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  1609. h = writeDir;
  1610. BAIL_IF_MACRO_MUTEX(!verifyPath(h, &dname, 1), ERRPASS, stateLock, 0);
  1611. start = dname;
  1612. while (1)
  1613. {
  1614. end = strchr(start, '/');
  1615. if (end != NULL)
  1616. *end = '\0';
  1617. /* only check for existance if all parent dirs existed, too... */
  1618. if (exists)
  1619. {
  1620. PHYSFS_Stat statbuf;
  1621. const int rc = h->funcs->stat(h->opaque, dname, &statbuf);
  1622. if ((!rc) && (currentErrorCode() == PHYSFS_ERR_NOT_FOUND))
  1623. exists = 0;
  1624. retval = ((rc) && (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY));
  1625. } /* if */
  1626. if (!exists)
  1627. retval = h->funcs->mkdir(h->opaque, dname);
  1628. if (!retval)
  1629. break;
  1630. if (end == NULL)
  1631. break;
  1632. *end = '/';
  1633. start = end + 1;
  1634. } /* while */
  1635. __PHYSFS_platformReleaseMutex(stateLock);
  1636. return retval;
  1637. } /* doMkdir */
  1638. int PHYSFS_mkdir(const char *_dname)
  1639. {
  1640. int retval = 0;
  1641. char *dname;
  1642. size_t len;
  1643. BAIL_IF_MACRO(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1644. len = strlen(_dname) + 1;
  1645. dname = (char *) __PHYSFS_smallAlloc(len);
  1646. BAIL_IF_MACRO(!dname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1647. retval = doMkdir(_dname, dname);
  1648. __PHYSFS_smallFree(dname);
  1649. return retval;
  1650. } /* PHYSFS_mkdir */
  1651. static int doDelete(const char *_fname, char *fname)
  1652. {
  1653. int retval;
  1654. DirHandle *h;
  1655. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), ERRPASS, 0);
  1656. __PHYSFS_platformGrabMutex(stateLock);
  1657. BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  1658. h = writeDir;
  1659. BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), ERRPASS, stateLock, 0);
  1660. retval = h->funcs->remove(h->opaque, fname);
  1661. __PHYSFS_platformReleaseMutex(stateLock);
  1662. return retval;
  1663. } /* doDelete */
  1664. int PHYSFS_delete(const char *_fname)
  1665. {
  1666. int retval;
  1667. char *fname;
  1668. size_t len;
  1669. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1670. len = strlen(_fname) + 1;
  1671. fname = (char *) __PHYSFS_smallAlloc(len);
  1672. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1673. retval = doDelete(_fname, fname);
  1674. __PHYSFS_smallFree(fname);
  1675. return retval;
  1676. } /* PHYSFS_delete */
  1677. const char *PHYSFS_getRealDir(const char *_fname)
  1678. {
  1679. const char *retval = NULL;
  1680. char *fname = NULL;
  1681. size_t len;
  1682. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1683. len = strlen(_fname) + 1;
  1684. fname = __PHYSFS_smallAlloc(len);
  1685. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1686. if (sanitizePlatformIndependentPath(_fname, fname))
  1687. {
  1688. DirHandle *i;
  1689. __PHYSFS_platformGrabMutex(stateLock);
  1690. for (i = searchPath; i != NULL; i = i->next)
  1691. {
  1692. char *arcfname = fname;
  1693. if (partOfMountPoint(i, arcfname))
  1694. {
  1695. retval = i->dirName;
  1696. break;
  1697. } /* if */
  1698. else if (verifyPath(i, &arcfname, 0))
  1699. {
  1700. PHYSFS_Stat statbuf;
  1701. if (i->funcs->stat(i->opaque, arcfname, &statbuf))
  1702. {
  1703. retval = i->dirName;
  1704. break;
  1705. } /* if */
  1706. } /* if */
  1707. } /* for */
  1708. __PHYSFS_platformReleaseMutex(stateLock);
  1709. } /* if */
  1710. __PHYSFS_smallFree(fname);
  1711. return retval;
  1712. } /* PHYSFS_getRealDir */
  1713. static int locateInStringList(const char *str,
  1714. char **list,
  1715. PHYSFS_uint32 *pos)
  1716. {
  1717. PHYSFS_uint32 len = *pos;
  1718. PHYSFS_uint32 half_len;
  1719. PHYSFS_uint32 lo = 0;
  1720. PHYSFS_uint32 middle;
  1721. int cmp;
  1722. while (len > 0)
  1723. {
  1724. half_len = len >> 1;
  1725. middle = lo + half_len;
  1726. cmp = strcmp(list[middle], str);
  1727. if (cmp == 0) /* it's in the list already. */
  1728. return 1;
  1729. else if (cmp > 0)
  1730. len = half_len;
  1731. else
  1732. {
  1733. lo = middle + 1;
  1734. len -= half_len + 1;
  1735. } /* else */
  1736. } /* while */
  1737. *pos = lo;
  1738. return 0;
  1739. } /* locateInStringList */
  1740. static void enumFilesCallback(void *data, const char *origdir, const char *str)
  1741. {
  1742. PHYSFS_uint32 pos;
  1743. void *ptr;
  1744. char *newstr;
  1745. EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
  1746. /*
  1747. * See if file is in the list already, and if not, insert it in there
  1748. * alphabetically...
  1749. */
  1750. pos = pecd->size;
  1751. if (locateInStringList(str, pecd->list, &pos))
  1752. return; /* already in the list. */
  1753. ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
  1754. newstr = (char *) allocator.Malloc(strlen(str) + 1);
  1755. if (ptr != NULL)
  1756. pecd->list = (char **) ptr;
  1757. if ((ptr == NULL) || (newstr == NULL))
  1758. return; /* better luck next time. */
  1759. strcpy(newstr, str);
  1760. if (pos != pecd->size)
  1761. {
  1762. memmove(&pecd->list[pos+1], &pecd->list[pos],
  1763. sizeof (char *) * ((pecd->size) - pos));
  1764. } /* if */
  1765. pecd->list[pos] = newstr;
  1766. pecd->size++;
  1767. } /* enumFilesCallback */
  1768. char **PHYSFS_enumerateFiles(const char *path)
  1769. {
  1770. EnumStringListCallbackData ecd;
  1771. memset(&ecd, '\0', sizeof (ecd));
  1772. ecd.list = (char **) allocator.Malloc(sizeof (char *));
  1773. BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1774. PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
  1775. ecd.list[ecd.size] = NULL;
  1776. return ecd.list;
  1777. } /* PHYSFS_enumerateFiles */
  1778. /*
  1779. * Broke out to seperate function so we can use stack allocation gratuitously.
  1780. */
  1781. static void enumerateFromMountPoint(DirHandle *i, const char *arcfname,
  1782. PHYSFS_EnumFilesCallback callback,
  1783. const char *_fname, void *data)
  1784. {
  1785. const size_t len = strlen(arcfname);
  1786. char *ptr = NULL;
  1787. char *end = NULL;
  1788. const size_t slen = strlen(i->mountPoint) + 1;
  1789. char *mountPoint = (char *) __PHYSFS_smallAlloc(slen);
  1790. if (mountPoint == NULL)
  1791. return; /* oh well. */
  1792. strcpy(mountPoint, i->mountPoint);
  1793. ptr = mountPoint + ((len) ? len + 1 : 0);
  1794. end = strchr(ptr, '/');
  1795. assert(end); /* should always find a terminating '/'. */
  1796. *end = '\0';
  1797. callback(data, _fname, ptr);
  1798. __PHYSFS_smallFree(mountPoint);
  1799. } /* enumerateFromMountPoint */
  1800. typedef struct SymlinkFilterData
  1801. {
  1802. PHYSFS_EnumFilesCallback callback;
  1803. void *callbackData;
  1804. DirHandle *dirhandle;
  1805. } SymlinkFilterData;
  1806. /* !!! FIXME: broken if in a virtual mountpoint (stat call fails). */
  1807. static void enumCallbackFilterSymLinks(void *_data, const char *origdir,
  1808. const char *fname)
  1809. {
  1810. const char *trimmedDir = (*origdir == '/') ? (origdir+1) : origdir;
  1811. const size_t slen = strlen(trimmedDir) + strlen(fname) + 2;
  1812. char *path = (char *) __PHYSFS_smallAlloc(slen);
  1813. if (path != NULL)
  1814. {
  1815. SymlinkFilterData *data = (SymlinkFilterData *) _data;
  1816. const DirHandle *dh = data->dirhandle;
  1817. PHYSFS_Stat statbuf;
  1818. sprintf(path, "%s%s%s", trimmedDir, *trimmedDir ? "/" : "", fname);
  1819. if (dh->funcs->stat(dh->opaque, path, &statbuf))
  1820. {
  1821. /* Pass it on to the application if it's not a symlink. */
  1822. if (statbuf.filetype != PHYSFS_FILETYPE_SYMLINK)
  1823. data->callback(data->callbackData, origdir, fname);
  1824. } /* if */
  1825. __PHYSFS_smallFree(path);
  1826. } /* if */
  1827. } /* enumCallbackFilterSymLinks */
  1828. /* !!! FIXME: this should report error conditions. */
  1829. void PHYSFS_enumerateFilesCallback(const char *_fname,
  1830. PHYSFS_EnumFilesCallback callback,
  1831. void *data)
  1832. {
  1833. size_t len;
  1834. char *fname;
  1835. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
  1836. BAIL_IF_MACRO(!callback, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
  1837. len = strlen(_fname) + 1;
  1838. fname = (char *) __PHYSFS_smallAlloc(len);
  1839. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, ) /*0*/;
  1840. if (sanitizePlatformIndependentPath(_fname, fname))
  1841. {
  1842. DirHandle *i;
  1843. SymlinkFilterData filterdata;
  1844. __PHYSFS_platformGrabMutex(stateLock);
  1845. if (!allowSymLinks)
  1846. {
  1847. memset(&filterdata, '\0', sizeof (filterdata));
  1848. filterdata.callback = callback;
  1849. filterdata.callbackData = data;
  1850. } /* if */
  1851. for (i = searchPath; i != NULL; i = i->next)
  1852. {
  1853. char *arcfname = fname;
  1854. if (partOfMountPoint(i, arcfname))
  1855. enumerateFromMountPoint(i, arcfname, callback, _fname, data);
  1856. else if (verifyPath(i, &arcfname, 0))
  1857. {
  1858. if ((!allowSymLinks) && (i->funcs->info.supportsSymlinks))
  1859. {
  1860. filterdata.dirhandle = i;
  1861. i->funcs->enumerateFiles(i->opaque, arcfname,
  1862. enumCallbackFilterSymLinks,
  1863. _fname, &filterdata);
  1864. } /* if */
  1865. else
  1866. {
  1867. i->funcs->enumerateFiles(i->opaque, arcfname,
  1868. callback, _fname, data);
  1869. } /* else */
  1870. } /* else if */
  1871. } /* for */
  1872. __PHYSFS_platformReleaseMutex(stateLock);
  1873. } /* if */
  1874. __PHYSFS_smallFree(fname);
  1875. } /* PHYSFS_enumerateFilesCallback */
  1876. int PHYSFS_exists(const char *fname)
  1877. {
  1878. return (PHYSFS_getRealDir(fname) != NULL);
  1879. } /* PHYSFS_exists */
  1880. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1881. {
  1882. PHYSFS_Stat statbuf;
  1883. BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, -1);
  1884. return statbuf.modtime;
  1885. } /* PHYSFS_getLastModTime */
  1886. int PHYSFS_isDirectory(const char *fname)
  1887. {
  1888. PHYSFS_Stat statbuf;
  1889. BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
  1890. return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  1891. } /* PHYSFS_isDirectory */
  1892. int PHYSFS_isSymbolicLink(const char *fname)
  1893. {
  1894. PHYSFS_Stat statbuf;
  1895. BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
  1896. return (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  1897. } /* PHYSFS_isSymbolicLink */
  1898. static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
  1899. {
  1900. FileHandle *fh = NULL;
  1901. size_t len;
  1902. char *fname;
  1903. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1904. len = strlen(_fname) + 1;
  1905. fname = (char *) __PHYSFS_smallAlloc(len);
  1906. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1907. if (sanitizePlatformIndependentPath(_fname, fname))
  1908. {
  1909. PHYSFS_Io *io = NULL;
  1910. DirHandle *h = NULL;
  1911. const PHYSFS_Archiver *f;
  1912. __PHYSFS_platformGrabMutex(stateLock);
  1913. GOTO_IF_MACRO(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, doOpenWriteEnd);
  1914. h = writeDir;
  1915. GOTO_IF_MACRO(!verifyPath(h, &fname, 0), ERRPASS, doOpenWriteEnd);
  1916. f = h->funcs;
  1917. if (appending)
  1918. io = f->openAppend(h->opaque, fname);
  1919. else
  1920. io = f->openWrite(h->opaque, fname);
  1921. GOTO_IF_MACRO(!io, ERRPASS, doOpenWriteEnd);
  1922. fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  1923. if (fh == NULL)
  1924. {
  1925. io->destroy(io);
  1926. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, doOpenWriteEnd);
  1927. } /* if */
  1928. else
  1929. {
  1930. memset(fh, '\0', sizeof (FileHandle));
  1931. fh->io = io;
  1932. fh->dirHandle = h;
  1933. fh->next = openWriteList;
  1934. openWriteList = fh;
  1935. } /* else */
  1936. doOpenWriteEnd:
  1937. __PHYSFS_platformReleaseMutex(stateLock);
  1938. } /* if */
  1939. __PHYSFS_smallFree(fname);
  1940. return ((PHYSFS_File *) fh);
  1941. } /* doOpenWrite */
  1942. PHYSFS_File *PHYSFS_openWrite(const char *filename)
  1943. {
  1944. return doOpenWrite(filename, 0);
  1945. } /* PHYSFS_openWrite */
  1946. PHYSFS_File *PHYSFS_openAppend(const char *filename)
  1947. {
  1948. return doOpenWrite(filename, 1);
  1949. } /* PHYSFS_openAppend */
  1950. PHYSFS_File *PHYSFS_openRead(const char *_fname)
  1951. {
  1952. FileHandle *fh = NULL;
  1953. char *fname;
  1954. size_t len;
  1955. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1956. len = strlen(_fname) + 1;
  1957. fname = (char *) __PHYSFS_smallAlloc(len);
  1958. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1959. if (sanitizePlatformIndependentPath(_fname, fname))
  1960. {
  1961. DirHandle *i = NULL;
  1962. PHYSFS_Io *io = NULL;
  1963. __PHYSFS_platformGrabMutex(stateLock);
  1964. GOTO_IF_MACRO(!searchPath, PHYSFS_ERR_NOT_FOUND, openReadEnd);
  1965. for (i = searchPath; i != NULL; i = i->next)
  1966. {
  1967. char *arcfname = fname;
  1968. if (verifyPath(i, &arcfname, 0))
  1969. {
  1970. io = i->funcs->openRead(i->opaque, arcfname);
  1971. if (io)
  1972. break;
  1973. } /* if */
  1974. } /* for */
  1975. GOTO_IF_MACRO(!io, ERRPASS, openReadEnd);
  1976. fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  1977. if (fh == NULL)
  1978. {
  1979. io->destroy(io);
  1980. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, openReadEnd);
  1981. } /* if */
  1982. memset(fh, '\0', sizeof (FileHandle));
  1983. fh->io = io;
  1984. fh->forReading = 1;
  1985. fh->dirHandle = i;
  1986. fh->next = openReadList;
  1987. openReadList = fh;
  1988. openReadEnd:
  1989. __PHYSFS_platformReleaseMutex(stateLock);
  1990. } /* if */
  1991. __PHYSFS_smallFree(fname);
  1992. return ((PHYSFS_File *) fh);
  1993. } /* PHYSFS_openRead */
  1994. static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
  1995. {
  1996. FileHandle *prev = NULL;
  1997. FileHandle *i;
  1998. int rc = 1;
  1999. for (i = *list; i != NULL; i = i->next)
  2000. {
  2001. if (i == handle) /* handle is in this list? */
  2002. {
  2003. PHYSFS_Io *io = handle->io;
  2004. PHYSFS_uint8 *tmp = handle->buffer;
  2005. rc = PHYSFS_flush((PHYSFS_File *) handle);
  2006. if (!rc)
  2007. return -1;
  2008. io->destroy(io);
  2009. if (tmp != NULL) /* free any associated buffer. */
  2010. allocator.Free(tmp);
  2011. if (prev == NULL)
  2012. *list = handle->next;
  2013. else
  2014. prev->next = handle->next;
  2015. allocator.Free(handle);
  2016. return 1;
  2017. } /* if */
  2018. prev = i;
  2019. } /* for */
  2020. return 0;
  2021. } /* closeHandleInOpenList */
  2022. int PHYSFS_close(PHYSFS_File *_handle)
  2023. {
  2024. FileHandle *handle = (FileHandle *) _handle;
  2025. int rc;
  2026. __PHYSFS_platformGrabMutex(stateLock);
  2027. /* -1 == close failure. 0 == not found. 1 == success. */
  2028. rc = closeHandleInOpenList(&openReadList, handle);
  2029. BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
  2030. if (!rc)
  2031. {
  2032. rc = closeHandleInOpenList(&openWriteList, handle);
  2033. BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
  2034. } /* if */
  2035. __PHYSFS_platformReleaseMutex(stateLock);
  2036. BAIL_IF_MACRO(!rc, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2037. return 1;
  2038. } /* PHYSFS_close */
  2039. static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
  2040. PHYSFS_uint64 len)
  2041. {
  2042. PHYSFS_Io *io = NULL;
  2043. PHYSFS_sint64 retval = 0;
  2044. PHYSFS_uint32 buffered = 0;
  2045. PHYSFS_sint64 rc = 0;
  2046. if (len == 0)
  2047. return 0;
  2048. buffered = fh->buffill - fh->bufpos;
  2049. if (buffered >= len) /* totally in the buffer, just copy and return! */
  2050. {
  2051. memcpy(buffer, fh->buffer + fh->bufpos, (size_t) len);
  2052. fh->bufpos += (PHYSFS_uint32) len;
  2053. return (PHYSFS_sint64) len;
  2054. } /* if */
  2055. else if (buffered > 0) /* partially in the buffer... */
  2056. {
  2057. memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
  2058. buffer = ((PHYSFS_uint8 *) buffer) + buffered;
  2059. len -= buffered;
  2060. retval = buffered;
  2061. } /* if */
  2062. /* if you got here, the buffer is drained and we still need bytes. */
  2063. assert(len > 0);
  2064. fh->buffill = fh->bufpos = 0;
  2065. io = fh->io;
  2066. if (len >= fh->bufsize) /* need more than the buffer takes. */
  2067. {
  2068. /* leave buffer empty, go right to output instead. */
  2069. rc = io->read(io, buffer, len);
  2070. if (rc < 0)
  2071. return ((retval == 0) ? rc : retval);
  2072. return retval + rc;
  2073. } /* if */
  2074. /* need less than buffer can take. Fill buffer. */
  2075. rc = io->read(io, fh->buffer, fh->bufsize);
  2076. if (rc < 0)
  2077. return ((retval == 0) ? rc : retval);
  2078. assert(fh->bufpos == 0);
  2079. fh->buffill = (PHYSFS_uint32) rc;
  2080. rc = doBufferedRead(fh, buffer, len); /* go from the start, again. */
  2081. if (rc < 0)
  2082. return ((retval == 0) ? rc : retval);
  2083. return retval + rc;
  2084. } /* doBufferedRead */
  2085. PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
  2086. PHYSFS_uint32 size, PHYSFS_uint32 count)
  2087. {
  2088. const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
  2089. const PHYSFS_sint64 retval = PHYSFS_readBytes(handle, buffer, len);
  2090. return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
  2091. } /* PHYSFS_read */
  2092. PHYSFS_sint64 PHYSFS_readBytes(PHYSFS_File *handle, void *buffer,
  2093. PHYSFS_uint64 len)
  2094. {
  2095. FileHandle *fh = (FileHandle *) handle;
  2096. #ifdef PHYSFS_NO_64BIT_SUPPORT
  2097. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
  2098. #else
  2099. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
  2100. #endif
  2101. if (!__PHYSFS_ui64FitsAddressSpace(len))
  2102. BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2103. BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2104. BAIL_IF_MACRO(!fh->forReading, PHYSFS_ERR_OPEN_FOR_WRITING, -1);
  2105. BAIL_IF_MACRO(len == 0, ERRPASS, 0);
  2106. if (fh->buffer)
  2107. return doBufferedRead(fh, buffer, len);
  2108. return fh->io->read(fh->io, buffer, len);
  2109. } /* PHYSFS_readBytes */
  2110. static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
  2111. PHYSFS_uint64 len)
  2112. {
  2113. FileHandle *fh = (FileHandle *) handle;
  2114. /* whole thing fits in the buffer? */
  2115. if ( (((PHYSFS_uint64) fh->buffill) + len) < fh->bufsize )
  2116. {
  2117. memcpy(fh->buffer + fh->buffill, buffer, (size_t) len);
  2118. fh->buffill += (PHYSFS_uint32) len;
  2119. return (PHYSFS_sint64) len;
  2120. } /* if */
  2121. /* would overflow buffer. Flush and then write the new objects, too. */
  2122. BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, -1);
  2123. return fh->io->write(fh->io, buffer, len);
  2124. } /* doBufferedWrite */
  2125. PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
  2126. PHYSFS_uint32 size, PHYSFS_uint32 count)
  2127. {
  2128. const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
  2129. const PHYSFS_sint64 retval = PHYSFS_writeBytes(handle, buffer, len);
  2130. return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
  2131. } /* PHYSFS_write */
  2132. PHYSFS_sint64 PHYSFS_writeBytes(PHYSFS_File *handle, const void *buffer,
  2133. PHYSFS_uint64 len)
  2134. {
  2135. FileHandle *fh = (FileHandle *) handle;
  2136. #ifdef PHYSFS_NO_64BIT_SUPPORT
  2137. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
  2138. #else
  2139. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
  2140. #endif
  2141. if (!__PHYSFS_ui64FitsAddressSpace(len))
  2142. BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2143. BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2144. BAIL_IF_MACRO(fh->forReading, PHYSFS_ERR_OPEN_FOR_READING, -1);
  2145. BAIL_IF_MACRO(len == 0, ERRPASS, 0);
  2146. if (fh->buffer)
  2147. return doBufferedWrite(handle, buffer, len);
  2148. return fh->io->write(fh->io, buffer, len);
  2149. } /* PHYSFS_write */
  2150. int PHYSFS_eof(PHYSFS_File *handle)
  2151. {
  2152. FileHandle *fh = (FileHandle *) handle;
  2153. if (!fh->forReading) /* never EOF on files opened for write/append. */
  2154. return 0;
  2155. /* can't be eof if buffer isn't empty */
  2156. if (fh->bufpos == fh->buffill)
  2157. {
  2158. /* check the Io. */
  2159. PHYSFS_Io *io = fh->io;
  2160. const PHYSFS_sint64 pos = io->tell(io);
  2161. const PHYSFS_sint64 len = io->length(io);
  2162. if ((pos < 0) || (len < 0))
  2163. return 0; /* beats me. */
  2164. return (pos >= len);
  2165. } /* if */
  2166. return 0;
  2167. } /* PHYSFS_eof */
  2168. PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
  2169. {
  2170. FileHandle *fh = (FileHandle *) handle;
  2171. const PHYSFS_sint64 pos = fh->io->tell(fh->io);
  2172. const PHYSFS_sint64 retval = fh->forReading ?
  2173. (pos - fh->buffill) + fh->bufpos :
  2174. (pos + fh->buffill);
  2175. return retval;
  2176. } /* PHYSFS_tell */
  2177. int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
  2178. {
  2179. FileHandle *fh = (FileHandle *) handle;
  2180. BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
  2181. if (fh->buffer && fh->forReading)
  2182. {
  2183. /* avoid throwing away our precious buffer if seeking within it. */
  2184. PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
  2185. if ( /* seeking within the already-buffered range? */
  2186. ((offset >= 0) && (offset <= fh->buffill - fh->bufpos)) /* fwd */
  2187. || ((offset < 0) && (-offset <= fh->bufpos)) /* backward */ )
  2188. {
  2189. fh->bufpos += (PHYSFS_uint32) offset;
  2190. return 1; /* successful seek */
  2191. } /* if */
  2192. } /* if */
  2193. /* we have to fall back to a 'raw' seek. */
  2194. fh->buffill = fh->bufpos = 0;
  2195. return fh->io->seek(fh->io, pos);
  2196. } /* PHYSFS_seek */
  2197. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
  2198. {
  2199. PHYSFS_Io *io = ((FileHandle *) handle)->io;
  2200. return io->length(io);
  2201. } /* PHYSFS_filelength */
  2202. int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
  2203. {
  2204. FileHandle *fh = (FileHandle *) handle;
  2205. PHYSFS_uint32 bufsize;
  2206. /* !!! FIXME: actually, why use 32 bits here? */
  2207. /*BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);*/
  2208. BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2209. bufsize = (PHYSFS_uint32) _bufsize;
  2210. BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
  2211. /*
  2212. * For reads, we need to move the file pointer to where it would be
  2213. * if we weren't buffering, so that the next read will get the
  2214. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  2215. */
  2216. if ((fh->forReading) && (fh->buffill != fh->bufpos))
  2217. {
  2218. PHYSFS_uint64 pos;
  2219. const PHYSFS_sint64 curpos = fh->io->tell(fh->io);
  2220. BAIL_IF_MACRO(curpos == -1, ERRPASS, 0);
  2221. pos = ((curpos - fh->buffill) + fh->bufpos);
  2222. BAIL_IF_MACRO(!fh->io->seek(fh->io, pos), ERRPASS, 0);
  2223. } /* if */
  2224. if (bufsize == 0) /* delete existing buffer. */
  2225. {
  2226. if (fh->buffer)
  2227. {
  2228. allocator.Free(fh->buffer);
  2229. fh->buffer = NULL;
  2230. } /* if */
  2231. } /* if */
  2232. else
  2233. {
  2234. PHYSFS_uint8 *newbuf;
  2235. newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
  2236. BAIL_IF_MACRO(!newbuf, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  2237. fh->buffer = newbuf;
  2238. } /* else */
  2239. fh->bufsize = bufsize;
  2240. fh->buffill = fh->bufpos = 0;
  2241. return 1;
  2242. } /* PHYSFS_setBuffer */
  2243. int PHYSFS_flush(PHYSFS_File *handle)
  2244. {
  2245. FileHandle *fh = (FileHandle *) handle;
  2246. PHYSFS_Io *io;
  2247. PHYSFS_sint64 rc;
  2248. if ((fh->forReading) || (fh->bufpos == fh->buffill))
  2249. return 1; /* open for read or buffer empty are successful no-ops. */
  2250. /* dump buffer to disk. */
  2251. io = fh->io;
  2252. rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
  2253. BAIL_IF_MACRO(rc <= 0, ERRPASS, 0);
  2254. fh->bufpos = fh->buffill = 0;
  2255. return io->flush ? io->flush(io) : 1;
  2256. } /* PHYSFS_flush */
  2257. int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
  2258. {
  2259. int retval = 0;
  2260. char *fname;
  2261. size_t len;
  2262. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2263. BAIL_IF_MACRO(!stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2264. len = strlen(_fname) + 1;
  2265. fname = (char *) __PHYSFS_smallAlloc(len);
  2266. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  2267. /* set some sane defaults... */
  2268. stat->filesize = -1;
  2269. stat->modtime = -1;
  2270. stat->createtime = -1;
  2271. stat->accesstime = -1;
  2272. stat->filetype = PHYSFS_FILETYPE_OTHER;
  2273. stat->readonly = 1; /* !!! FIXME */
  2274. if (sanitizePlatformIndependentPath(_fname, fname))
  2275. {
  2276. if (*fname == '\0')
  2277. {
  2278. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  2279. stat->readonly = !writeDir; /* Writeable if we have a writeDir */
  2280. retval = 1;
  2281. } /* if */
  2282. else
  2283. {
  2284. DirHandle *i;
  2285. int exists = 0;
  2286. __PHYSFS_platformGrabMutex(stateLock);
  2287. for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
  2288. {
  2289. char *arcfname = fname;
  2290. exists = partOfMountPoint(i, arcfname);
  2291. if (exists)
  2292. {
  2293. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  2294. stat->readonly = 1; /* !!! FIXME */
  2295. retval = 1;
  2296. } /* if */
  2297. else if (verifyPath(i, &arcfname, 0))
  2298. {
  2299. /* !!! FIXME: this test is wrong and should be elsewhere. */
  2300. stat->readonly = !(writeDir &&
  2301. (strcmp(writeDir->dirName, i->dirName) == 0));
  2302. retval = i->funcs->stat(i->opaque, arcfname, stat);
  2303. if ((retval) || (currentErrorCode() != PHYSFS_ERR_NOT_FOUND))
  2304. exists = 1;
  2305. } /* else if */
  2306. } /* for */
  2307. __PHYSFS_platformReleaseMutex(stateLock);
  2308. } /* else */
  2309. } /* if */
  2310. __PHYSFS_smallFree(fname);
  2311. return retval;
  2312. } /* PHYSFS_stat */
  2313. int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
  2314. {
  2315. return (io->read(io, buf, len) == len);
  2316. } /* __PHYSFS_readAll */
  2317. void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len)
  2318. {
  2319. void *useHeap = ((ptr == NULL) ? ((void *) 1) : ((void *) 0));
  2320. if (useHeap) /* too large for stack allocation or alloca() failed. */
  2321. ptr = allocator.Malloc(len+sizeof (void *));
  2322. if (ptr != NULL)
  2323. {
  2324. void **retval = (void **) ptr;
  2325. /*printf("%s alloc'd (%d) bytes at (%p).\n",
  2326. useHeap ? "heap" : "stack", (int) len, ptr);*/
  2327. *retval = useHeap;
  2328. return retval + 1;
  2329. } /* if */
  2330. return NULL; /* allocation failed. */
  2331. } /* __PHYSFS_initSmallAlloc */
  2332. void __PHYSFS_smallFree(void *ptr)
  2333. {
  2334. if (ptr != NULL)
  2335. {
  2336. void **block = ((void **) ptr) - 1;
  2337. const int useHeap = (*block != 0);
  2338. if (useHeap)
  2339. allocator.Free(block);
  2340. /*printf("%s free'd (%p).\n", useHeap ? "heap" : "stack", block);*/
  2341. } /* if */
  2342. } /* __PHYSFS_smallFree */
  2343. int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
  2344. {
  2345. BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
  2346. externalAllocator = (a != NULL);
  2347. if (externalAllocator)
  2348. memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
  2349. return 1;
  2350. } /* PHYSFS_setAllocator */
  2351. const PHYSFS_Allocator *PHYSFS_getAllocator(void)
  2352. {
  2353. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
  2354. return &allocator;
  2355. } /* PHYSFS_getAllocator */
  2356. static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
  2357. {
  2358. if (!__PHYSFS_ui64FitsAddressSpace(s))
  2359. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2360. #undef malloc
  2361. return malloc((size_t) s);
  2362. } /* mallocAllocatorMalloc */
  2363. static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  2364. {
  2365. if (!__PHYSFS_ui64FitsAddressSpace(s))
  2366. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2367. #undef realloc
  2368. return realloc(ptr, (size_t) s);
  2369. } /* mallocAllocatorRealloc */
  2370. static void mallocAllocatorFree(void *ptr)
  2371. {
  2372. #undef free
  2373. free(ptr);
  2374. } /* mallocAllocatorFree */
  2375. static void setDefaultAllocator(void)
  2376. {
  2377. assert(!externalAllocator);
  2378. if (!__PHYSFS_platformSetDefaultAllocator(&allocator))
  2379. {
  2380. allocator.Init = NULL;
  2381. allocator.Deinit = NULL;
  2382. allocator.Malloc = mallocAllocatorMalloc;
  2383. allocator.Realloc = mallocAllocatorRealloc;
  2384. allocator.Free = mallocAllocatorFree;
  2385. } /* if */
  2386. } /* setDefaultAllocator */
  2387. /* end of physfs.c ... */