physfs.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966
  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. * http://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. assert((io != NULL) || (d != NULL));
  683. if (io == NULL)
  684. {
  685. /* DIR gets first shot (unlike the rest, it doesn't deal with files). */
  686. extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
  687. retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting);
  688. if (retval != NULL)
  689. return retval;
  690. io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
  691. BAIL_IF_MACRO(!io, ERRPASS, 0);
  692. } /* if */
  693. ext = find_filename_extension(d);
  694. if (ext != NULL)
  695. {
  696. /* Look for archivers with matching file extensions first... */
  697. for (i = archivers; (*i != NULL) && (retval == NULL); i++)
  698. {
  699. if (__PHYSFS_utf8stricmp(ext, (*i)->info.extension) == 0)
  700. retval = tryOpenDir(io, *i, d, forWriting);
  701. } /* for */
  702. /* failing an exact file extension match, try all the others... */
  703. for (i = archivers; (*i != NULL) && (retval == NULL); i++)
  704. {
  705. if (__PHYSFS_utf8stricmp(ext, (*i)->info.extension) != 0)
  706. retval = tryOpenDir(io, *i, d, forWriting);
  707. } /* for */
  708. } /* if */
  709. else /* no extension? Try them all. */
  710. {
  711. for (i = archivers; (*i != NULL) && (retval == NULL); i++)
  712. retval = tryOpenDir(io, *i, d, forWriting);
  713. } /* else */
  714. BAIL_IF_MACRO(!retval, PHYSFS_ERR_UNSUPPORTED, NULL);
  715. return retval;
  716. } /* openDirectory */
  717. /*
  718. * Make a platform-independent path string sane. Doesn't actually check the
  719. * file hierarchy, it just cleans up the string.
  720. * (dst) must be a buffer at least as big as (src), as this is where the
  721. * cleaned up string is deposited.
  722. * If there are illegal bits in the path (".." entries, etc) then we
  723. * return zero and (dst) is undefined. Non-zero if the path was sanitized.
  724. */
  725. static int sanitizePlatformIndependentPath(const char *src, char *dst)
  726. {
  727. char *prev;
  728. char ch;
  729. while (*src == '/') /* skip initial '/' chars... */
  730. src++;
  731. prev = dst;
  732. do
  733. {
  734. ch = *(src++);
  735. if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
  736. BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
  737. if (ch == '/') /* path separator. */
  738. {
  739. *dst = '\0'; /* "." and ".." are illegal pathnames. */
  740. if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
  741. BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
  742. while (*src == '/') /* chop out doubles... */
  743. src++;
  744. if (*src == '\0') /* ends with a pathsep? */
  745. break; /* we're done, don't add final pathsep to dst. */
  746. prev = dst + 1;
  747. } /* if */
  748. *(dst++) = ch;
  749. } while (ch != '\0');
  750. return 1;
  751. } /* sanitizePlatformIndependentPath */
  752. /*
  753. * Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
  754. * output from sanitizePlatformIndependentPath(), so that it is in a known
  755. * state.
  756. *
  757. * This only finds legitimate segments of a mountpoint. If the mountpoint is
  758. * "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
  759. * all zero. "/a/b" will succeed, though.
  760. */
  761. static int partOfMountPoint(DirHandle *h, char *fname)
  762. {
  763. /* !!! FIXME: This code feels gross. */
  764. int rc;
  765. size_t len, mntpntlen;
  766. if (h->mountPoint == NULL)
  767. return 0;
  768. else if (*fname == '\0')
  769. return 1;
  770. len = strlen(fname);
  771. mntpntlen = strlen(h->mountPoint);
  772. if (len > mntpntlen) /* can't be a subset of mountpoint. */
  773. return 0;
  774. /* if true, must be not a match or a complete match, but not a subset. */
  775. if ((len + 1) == mntpntlen)
  776. return 0;
  777. rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
  778. if (rc != 0)
  779. return 0; /* not a match. */
  780. /* make sure /a/b matches /a/b/ and not /a/bc ... */
  781. return h->mountPoint[len] == '/';
  782. } /* partOfMountPoint */
  783. static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
  784. const char *mountPoint, int forWriting)
  785. {
  786. DirHandle *dirHandle = NULL;
  787. char *tmpmntpnt = NULL;
  788. if (mountPoint != NULL)
  789. {
  790. const size_t len = strlen(mountPoint) + 1;
  791. tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
  792. GOTO_IF_MACRO(!tmpmntpnt, PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  793. if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
  794. goto badDirHandle;
  795. mountPoint = tmpmntpnt; /* sanitized version. */
  796. } /* if */
  797. dirHandle = openDirectory(io, newDir, forWriting);
  798. GOTO_IF_MACRO(!dirHandle, ERRPASS, badDirHandle);
  799. if (newDir == NULL)
  800. dirHandle->dirName = NULL;
  801. else
  802. {
  803. dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
  804. if (!dirHandle->dirName)
  805. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  806. strcpy(dirHandle->dirName, newDir);
  807. } /* else */
  808. if ((mountPoint != NULL) && (*mountPoint != '\0'))
  809. {
  810. dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
  811. if (!dirHandle->mountPoint)
  812. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  813. strcpy(dirHandle->mountPoint, mountPoint);
  814. strcat(dirHandle->mountPoint, "/");
  815. } /* if */
  816. __PHYSFS_smallFree(tmpmntpnt);
  817. return dirHandle;
  818. badDirHandle:
  819. if (dirHandle != NULL)
  820. {
  821. dirHandle->funcs->closeArchive(dirHandle->opaque);
  822. allocator.Free(dirHandle->dirName);
  823. allocator.Free(dirHandle->mountPoint);
  824. allocator.Free(dirHandle);
  825. } /* if */
  826. __PHYSFS_smallFree(tmpmntpnt);
  827. return NULL;
  828. } /* createDirHandle */
  829. /* MAKE SURE you've got the stateLock held before calling this! */
  830. static int freeDirHandle(DirHandle *dh, FileHandle *openList)
  831. {
  832. FileHandle *i;
  833. if (dh == NULL)
  834. return 1;
  835. for (i = openList; i != NULL; i = i->next)
  836. BAIL_IF_MACRO(i->dirHandle == dh, PHYSFS_ERR_FILES_STILL_OPEN, 0);
  837. dh->funcs->closeArchive(dh->opaque);
  838. allocator.Free(dh->dirName);
  839. allocator.Free(dh->mountPoint);
  840. allocator.Free(dh);
  841. return 1;
  842. } /* freeDirHandle */
  843. static char *calculateBaseDir(const char *argv0)
  844. {
  845. const char dirsep = __PHYSFS_platformDirSeparator;
  846. char *retval = NULL;
  847. char *ptr = NULL;
  848. /* Give the platform layer first shot at this. */
  849. retval = __PHYSFS_platformCalcBaseDir(argv0);
  850. if (retval != NULL)
  851. return retval;
  852. /* We need argv0 to go on. */
  853. BAIL_IF_MACRO(argv0 == NULL, PHYSFS_ERR_ARGV0_IS_NULL, NULL);
  854. ptr = strrchr(argv0, dirsep);
  855. if (ptr != NULL)
  856. {
  857. const size_t size = ((size_t) (ptr - argv0)) + 1;
  858. retval = (char *) allocator.Malloc(size + 1);
  859. BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  860. memcpy(retval, argv0, size);
  861. retval[size] = '\0';
  862. return retval;
  863. } /* if */
  864. /* argv0 wasn't helpful. */
  865. BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  866. } /* calculateBaseDir */
  867. static int initializeMutexes(void)
  868. {
  869. errorLock = __PHYSFS_platformCreateMutex();
  870. if (errorLock == NULL)
  871. goto initializeMutexes_failed;
  872. stateLock = __PHYSFS_platformCreateMutex();
  873. if (stateLock == NULL)
  874. goto initializeMutexes_failed;
  875. return 1; /* success. */
  876. initializeMutexes_failed:
  877. if (errorLock != NULL)
  878. __PHYSFS_platformDestroyMutex(errorLock);
  879. if (stateLock != NULL)
  880. __PHYSFS_platformDestroyMutex(stateLock);
  881. errorLock = stateLock = NULL;
  882. return 0; /* failed. */
  883. } /* initializeMutexes */
  884. static int doRegisterArchiver(const PHYSFS_Archiver *_archiver);
  885. static int initStaticArchivers(void)
  886. {
  887. #define REGISTER_STATIC_ARCHIVER(arc) { \
  888. extern const PHYSFS_Archiver __PHYSFS_Archiver_##arc; \
  889. if (!doRegisterArchiver(&__PHYSFS_Archiver_##arc)) { \
  890. return 0; \
  891. } \
  892. }
  893. #if PHYSFS_SUPPORTS_ZIP
  894. REGISTER_STATIC_ARCHIVER(ZIP);
  895. #endif
  896. #if PHYSFS_SUPPORTS_7Z
  897. REGISTER_STATIC_ARCHIVER(LZMA);
  898. #endif
  899. #if PHYSFS_SUPPORTS_GRP
  900. REGISTER_STATIC_ARCHIVER(GRP);
  901. #endif
  902. #if PHYSFS_SUPPORTS_QPAK
  903. REGISTER_STATIC_ARCHIVER(QPAK);
  904. #endif
  905. #if PHYSFS_SUPPORTS_HOG
  906. REGISTER_STATIC_ARCHIVER(HOG);
  907. #endif
  908. #if PHYSFS_SUPPORTS_MVL
  909. REGISTER_STATIC_ARCHIVER(MVL);
  910. #endif
  911. #if PHYSFS_SUPPORTS_WAD
  912. REGISTER_STATIC_ARCHIVER(WAD);
  913. #endif
  914. #if PHYSFS_SUPPORTS_SLB
  915. REGISTER_STATIC_ARCHIVER(SLB);
  916. #endif
  917. #if PHYSFS_SUPPORTS_ISO9660
  918. REGISTER_STATIC_ARCHIVER(ISO9660);
  919. #endif
  920. #undef REGISTER_STATIC_ARCHIVER
  921. return 1;
  922. } /* initStaticArchivers */
  923. static void setDefaultAllocator(void);
  924. static int doDeinit(void);
  925. int PHYSFS_init(const char *argv0)
  926. {
  927. BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
  928. if (!externalAllocator)
  929. setDefaultAllocator();
  930. if ((allocator.Init != NULL) && (!allocator.Init())) return 0;
  931. if (!__PHYSFS_platformInit())
  932. {
  933. if (allocator.Deinit != NULL) allocator.Deinit();
  934. return 0;
  935. } /* if */
  936. /* everything below here can be cleaned up safely by doDeinit(). */
  937. if (!initializeMutexes()) goto initFailed;
  938. baseDir = calculateBaseDir(argv0);
  939. if (!baseDir) goto initFailed;
  940. userDir = __PHYSFS_platformCalcUserDir();
  941. if (!userDir) goto initFailed;
  942. /* Platform layer is required to append a dirsep. */
  943. assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator);
  944. assert(userDir[strlen(userDir) - 1] == __PHYSFS_platformDirSeparator);
  945. if (!initStaticArchivers()) goto initFailed;
  946. initialized = 1;
  947. /* This makes sure that the error subsystem is initialized. */
  948. PHYSFS_setErrorCode(PHYSFS_getLastErrorCode());
  949. return 1;
  950. initFailed:
  951. doDeinit();
  952. return 0;
  953. } /* PHYSFS_init */
  954. /* MAKE SURE you hold stateLock before calling this! */
  955. static int closeFileHandleList(FileHandle **list)
  956. {
  957. FileHandle *i;
  958. FileHandle *next = NULL;
  959. for (i = *list; i != NULL; i = next)
  960. {
  961. PHYSFS_Io *io = i->io;
  962. next = i->next;
  963. if (!io->flush(io))
  964. {
  965. *list = i;
  966. return 0;
  967. } /* if */
  968. io->destroy(io);
  969. allocator.Free(i);
  970. } /* for */
  971. *list = NULL;
  972. return 1;
  973. } /* closeFileHandleList */
  974. /* MAKE SURE you hold the stateLock before calling this! */
  975. static void freeSearchPath(void)
  976. {
  977. DirHandle *i;
  978. DirHandle *next = NULL;
  979. closeFileHandleList(&openReadList);
  980. if (searchPath != NULL)
  981. {
  982. for (i = searchPath; i != NULL; i = next)
  983. {
  984. next = i->next;
  985. freeDirHandle(i, openReadList);
  986. } /* for */
  987. searchPath = NULL;
  988. } /* if */
  989. } /* freeSearchPath */
  990. /* MAKE SURE you hold stateLock before calling this! */
  991. static int archiverInUse(const PHYSFS_Archiver *arc, const DirHandle *list)
  992. {
  993. const DirHandle *i;
  994. for (i = list; i != NULL; i = i->next)
  995. {
  996. if (i->funcs == arc)
  997. return 1;
  998. } /* for */
  999. return 0; /* not in use */
  1000. } /* archiverInUse */
  1001. /* MAKE SURE you hold stateLock before calling this! */
  1002. static int doDeregisterArchiver(const size_t idx)
  1003. {
  1004. const size_t len = (numArchivers - idx) * sizeof (void *);
  1005. const PHYSFS_ArchiveInfo *info = archiveInfo[idx];
  1006. const PHYSFS_Archiver *arc = archivers[idx];
  1007. /* make sure nothing is still using this archiver */
  1008. if (archiverInUse(arc, searchPath) || archiverInUse(arc, writeDir))
  1009. BAIL_MACRO(PHYSFS_ERR_FILES_STILL_OPEN, 0);
  1010. allocator.Free((void *) info->extension);
  1011. allocator.Free((void *) info->description);
  1012. allocator.Free((void *) info->author);
  1013. allocator.Free((void *) info->url);
  1014. allocator.Free((void *) arc);
  1015. memmove(&archiveInfo[idx], &archiveInfo[idx+1], len);
  1016. memmove(&archivers[idx], &archivers[idx+1], len);
  1017. assert(numArchivers > 0);
  1018. numArchivers--;
  1019. return 1;
  1020. } /* doDeregisterArchiver */
  1021. /* Does NOT hold the state lock; we're shutting down. */
  1022. static void freeArchivers(void)
  1023. {
  1024. while (numArchivers > 0)
  1025. {
  1026. const int rc = doDeregisterArchiver(numArchivers - 1);
  1027. assert(rc); /* nothing should be mounted during shutdown. */
  1028. } /* while */
  1029. allocator.Free(archivers);
  1030. allocator.Free(archiveInfo);
  1031. archivers = NULL;
  1032. archiveInfo = NULL;
  1033. } /* freeArchivers */
  1034. static int doDeinit(void)
  1035. {
  1036. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), ERRPASS, 0);
  1037. closeFileHandleList(&openWriteList);
  1038. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), PHYSFS_ERR_FILES_STILL_OPEN, 0);
  1039. freeSearchPath();
  1040. freeArchivers();
  1041. freeErrorStates();
  1042. if (baseDir != NULL)
  1043. {
  1044. allocator.Free(baseDir);
  1045. baseDir = NULL;
  1046. } /* if */
  1047. if (userDir != NULL)
  1048. {
  1049. allocator.Free(userDir);
  1050. userDir = NULL;
  1051. } /* if */
  1052. if (prefDir != NULL)
  1053. {
  1054. allocator.Free(prefDir);
  1055. prefDir = NULL;
  1056. } /* if */
  1057. if (archiveInfo != NULL)
  1058. {
  1059. allocator.Free(archiveInfo);
  1060. archiveInfo = NULL;
  1061. } /* if */
  1062. if (archivers != NULL)
  1063. {
  1064. allocator.Free(archivers);
  1065. archivers = NULL;
  1066. } /* if */
  1067. allowSymLinks = 0;
  1068. initialized = 0;
  1069. if (errorLock) __PHYSFS_platformDestroyMutex(errorLock);
  1070. if (stateLock) __PHYSFS_platformDestroyMutex(stateLock);
  1071. if (allocator.Deinit != NULL)
  1072. allocator.Deinit();
  1073. errorLock = stateLock = NULL;
  1074. return 1;
  1075. } /* doDeinit */
  1076. int PHYSFS_deinit(void)
  1077. {
  1078. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1079. return doDeinit();
  1080. } /* PHYSFS_deinit */
  1081. int PHYSFS_isInit(void)
  1082. {
  1083. return initialized;
  1084. } /* PHYSFS_isInit */
  1085. static char *PHYSFS_strdup(const char *str)
  1086. {
  1087. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  1088. if (retval)
  1089. strcpy(retval, str);
  1090. return retval;
  1091. } /* PHYSFS_strdup */
  1092. /* MAKE SURE you hold stateLock before calling this! */
  1093. static int doRegisterArchiver(const PHYSFS_Archiver *_archiver)
  1094. {
  1095. const PHYSFS_uint32 maxver = CURRENT_PHYSFS_ARCHIVER_API_VERSION;
  1096. const size_t len = (numArchivers + 2) * sizeof (void *);
  1097. PHYSFS_Archiver *archiver = NULL;
  1098. PHYSFS_ArchiveInfo *info = NULL;
  1099. const char *ext = NULL;
  1100. void *ptr = NULL;
  1101. size_t i;
  1102. BAIL_IF_MACRO(!_archiver, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1103. BAIL_IF_MACRO(_archiver->version > maxver, PHYSFS_ERR_UNSUPPORTED, 0);
  1104. BAIL_IF_MACRO(!_archiver->info.extension, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1105. BAIL_IF_MACRO(!_archiver->info.description, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1106. BAIL_IF_MACRO(!_archiver->info.author, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1107. BAIL_IF_MACRO(!_archiver->info.url, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1108. BAIL_IF_MACRO(!_archiver->openArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1109. BAIL_IF_MACRO(!_archiver->enumerateFiles, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1110. BAIL_IF_MACRO(!_archiver->openRead, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1111. BAIL_IF_MACRO(!_archiver->openWrite, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1112. BAIL_IF_MACRO(!_archiver->openAppend, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1113. BAIL_IF_MACRO(!_archiver->remove, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1114. BAIL_IF_MACRO(!_archiver->mkdir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1115. BAIL_IF_MACRO(!_archiver->closeArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1116. BAIL_IF_MACRO(!_archiver->stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1117. ext = _archiver->info.extension;
  1118. for (i = 0; i < numArchivers; i++)
  1119. {
  1120. if (__PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
  1121. BAIL_MACRO(PHYSFS_ERR_DUPLICATE, 0); // !!! FIXME: better error? ERR_IN_USE?
  1122. } /* for */
  1123. /* make a copy of the data. */
  1124. archiver = (PHYSFS_Archiver *) allocator.Malloc(sizeof (*archiver));
  1125. GOTO_IF_MACRO(!archiver, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1126. /* Must copy sizeof (OLD_VERSION_OF_STRUCT) when version changes! */
  1127. memcpy(archiver, _archiver, sizeof (*archiver));
  1128. info = (PHYSFS_ArchiveInfo *) &archiver->info;
  1129. memset(info, '\0', sizeof (*info)); /* NULL in case an alloc fails. */
  1130. #define CPYSTR(item) \
  1131. info->item = PHYSFS_strdup(_archiver->info.item); \
  1132. GOTO_IF_MACRO(!info->item, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1133. CPYSTR(extension);
  1134. CPYSTR(description);
  1135. CPYSTR(author);
  1136. CPYSTR(url);
  1137. info->supportsSymlinks = _archiver->info.supportsSymlinks;
  1138. #undef CPYSTR
  1139. ptr = allocator.Realloc(archiveInfo, len);
  1140. GOTO_IF_MACRO(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1141. archiveInfo = (const PHYSFS_ArchiveInfo **) ptr;
  1142. ptr = allocator.Realloc(archivers, len);
  1143. GOTO_IF_MACRO(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1144. archivers = (const PHYSFS_Archiver **) ptr;
  1145. archiveInfo[numArchivers] = info;
  1146. archiveInfo[numArchivers + 1] = NULL;
  1147. archivers[numArchivers] = archiver;
  1148. archivers[numArchivers + 1] = NULL;
  1149. numArchivers++;
  1150. return 1;
  1151. regfailed:
  1152. if (info != NULL)
  1153. {
  1154. allocator.Free((void *) info->extension);
  1155. allocator.Free((void *) info->description);
  1156. allocator.Free((void *) info->author);
  1157. allocator.Free((void *) info->url);
  1158. } /* if */
  1159. allocator.Free(archiver);
  1160. return 0;
  1161. } /* doRegisterArchiver */
  1162. int PHYSFS_registerArchiver(const PHYSFS_Archiver *archiver)
  1163. {
  1164. int retval;
  1165. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1166. __PHYSFS_platformGrabMutex(stateLock);
  1167. retval = doRegisterArchiver(archiver);
  1168. __PHYSFS_platformReleaseMutex(stateLock);
  1169. return retval;
  1170. } /* PHYSFS_registerArchiver */
  1171. int PHYSFS_deregisterArchiver(const char *ext)
  1172. {
  1173. size_t i;
  1174. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1175. BAIL_IF_MACRO(!ext, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1176. __PHYSFS_platformGrabMutex(stateLock);
  1177. for (i = 0; i < numArchivers; i++)
  1178. {
  1179. if (__PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
  1180. {
  1181. const int retval = doDeregisterArchiver(i);
  1182. __PHYSFS_platformReleaseMutex(stateLock);
  1183. return retval;
  1184. } /* if */
  1185. } /* for */
  1186. __PHYSFS_platformReleaseMutex(stateLock);
  1187. BAIL_MACRO(PHYSFS_ERR_NOT_FOUND, 0);
  1188. } /* PHYSFS_deregisterArchiver */
  1189. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  1190. {
  1191. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
  1192. return archiveInfo;
  1193. } /* PHYSFS_supportedArchiveTypes */
  1194. void PHYSFS_freeList(void *list)
  1195. {
  1196. void **i;
  1197. if (list != NULL)
  1198. {
  1199. for (i = (void **) list; *i != NULL; i++)
  1200. allocator.Free(*i);
  1201. allocator.Free(list);
  1202. } /* if */
  1203. } /* PHYSFS_freeList */
  1204. const char *PHYSFS_getDirSeparator(void)
  1205. {
  1206. static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
  1207. return retval;
  1208. } /* PHYSFS_getDirSeparator */
  1209. char **PHYSFS_getCdRomDirs(void)
  1210. {
  1211. return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
  1212. } /* PHYSFS_getCdRomDirs */
  1213. void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
  1214. {
  1215. __PHYSFS_platformDetectAvailableCDs(callback, data);
  1216. } /* PHYSFS_getCdRomDirsCallback */
  1217. const char *PHYSFS_getPrefDir(const char *org, const char *app)
  1218. {
  1219. const char dirsep = __PHYSFS_platformDirSeparator;
  1220. PHYSFS_Stat statbuf;
  1221. char *ptr = NULL;
  1222. char *endstr = NULL;
  1223. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1224. BAIL_IF_MACRO(!org, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1225. BAIL_IF_MACRO(*org == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1226. BAIL_IF_MACRO(!app, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1227. BAIL_IF_MACRO(*app == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1228. allocator.Free(prefDir);
  1229. prefDir = __PHYSFS_platformCalcPrefDir(org, app);
  1230. BAIL_IF_MACRO(!prefDir, ERRPASS, NULL);
  1231. assert(strlen(prefDir) > 0);
  1232. endstr = prefDir + (strlen(prefDir) - 1);
  1233. assert(*endstr == dirsep);
  1234. *endstr = '\0'; /* mask out the final dirsep for now. */
  1235. if (!__PHYSFS_platformStat(prefDir, &statbuf))
  1236. {
  1237. for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
  1238. {
  1239. *ptr = '\0';
  1240. __PHYSFS_platformMkDir(prefDir);
  1241. *ptr = dirsep;
  1242. } /* for */
  1243. if (!__PHYSFS_platformMkDir(prefDir))
  1244. {
  1245. allocator.Free(prefDir);
  1246. prefDir = NULL;
  1247. } /* if */
  1248. } /* if */
  1249. *endstr = dirsep; /* readd the final dirsep. */
  1250. return prefDir;
  1251. } /* PHYSFS_getPrefDir */
  1252. const char *PHYSFS_getBaseDir(void)
  1253. {
  1254. return baseDir; /* this is calculated in PHYSFS_init()... */
  1255. } /* PHYSFS_getBaseDir */
  1256. const char *__PHYSFS_getUserDir(void) /* not deprecated internal version. */
  1257. {
  1258. return userDir; /* this is calculated in PHYSFS_init()... */
  1259. } /* __PHYSFS_getUserDir */
  1260. const char *PHYSFS_getUserDir(void)
  1261. {
  1262. return __PHYSFS_getUserDir();
  1263. } /* PHYSFS_getUserDir */
  1264. const char *PHYSFS_getWriteDir(void)
  1265. {
  1266. const char *retval = NULL;
  1267. __PHYSFS_platformGrabMutex(stateLock);
  1268. if (writeDir != NULL)
  1269. retval = writeDir->dirName;
  1270. __PHYSFS_platformReleaseMutex(stateLock);
  1271. return retval;
  1272. } /* PHYSFS_getWriteDir */
  1273. int PHYSFS_setWriteDir(const char *newDir)
  1274. {
  1275. int retval = 1;
  1276. __PHYSFS_platformGrabMutex(stateLock);
  1277. if (writeDir != NULL)
  1278. {
  1279. BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), ERRPASS,
  1280. stateLock, 0);
  1281. writeDir = NULL;
  1282. } /* if */
  1283. if (newDir != NULL)
  1284. {
  1285. /* !!! FIXME: PHYSFS_Io shouldn't be NULL */
  1286. writeDir = createDirHandle(NULL, newDir, NULL, 1);
  1287. retval = (writeDir != NULL);
  1288. } /* if */
  1289. __PHYSFS_platformReleaseMutex(stateLock);
  1290. return retval;
  1291. } /* PHYSFS_setWriteDir */
  1292. static int doMount(PHYSFS_Io *io, const char *fname,
  1293. const char *mountPoint, int appendToPath)
  1294. {
  1295. DirHandle *dh;
  1296. DirHandle *prev = NULL;
  1297. DirHandle *i;
  1298. if (mountPoint == NULL)
  1299. mountPoint = "/";
  1300. __PHYSFS_platformGrabMutex(stateLock);
  1301. if (fname != NULL)
  1302. {
  1303. for (i = searchPath; i != NULL; i = i->next)
  1304. {
  1305. /* already in search path? */
  1306. if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
  1307. BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
  1308. prev = i;
  1309. } /* for */
  1310. } /* if */
  1311. dh = createDirHandle(io, fname, mountPoint, 0);
  1312. BAIL_IF_MACRO_MUTEX(!dh, ERRPASS, stateLock, 0);
  1313. if (appendToPath)
  1314. {
  1315. if (prev == NULL)
  1316. searchPath = dh;
  1317. else
  1318. prev->next = dh;
  1319. } /* if */
  1320. else
  1321. {
  1322. dh->next = searchPath;
  1323. searchPath = dh;
  1324. } /* else */
  1325. __PHYSFS_platformReleaseMutex(stateLock);
  1326. return 1;
  1327. } /* doMount */
  1328. int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
  1329. const char *mountPoint, int appendToPath)
  1330. {
  1331. BAIL_IF_MACRO(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1332. BAIL_IF_MACRO(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
  1333. return doMount(io, fname, mountPoint, appendToPath);
  1334. } /* PHYSFS_mountIo */
  1335. int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
  1336. const char *fname, const char *mountPoint,
  1337. int appendToPath)
  1338. {
  1339. int retval = 0;
  1340. PHYSFS_Io *io = NULL;
  1341. BAIL_IF_MACRO(!buf, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1342. io = __PHYSFS_createMemoryIo(buf, len, del);
  1343. BAIL_IF_MACRO(!io, ERRPASS, 0);
  1344. retval = doMount(io, fname, mountPoint, appendToPath);
  1345. if (!retval)
  1346. {
  1347. /* docs say not to call (del) in case of failure, so cheat. */
  1348. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  1349. info->destruct = NULL;
  1350. io->destroy(io);
  1351. } /* if */
  1352. return retval;
  1353. } /* PHYSFS_mountMemory */
  1354. int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
  1355. const char *mountPoint, int appendToPath)
  1356. {
  1357. int retval = 0;
  1358. PHYSFS_Io *io = NULL;
  1359. BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1360. io = __PHYSFS_createHandleIo(file);
  1361. BAIL_IF_MACRO(!io, ERRPASS, 0);
  1362. retval = doMount(io, fname, mountPoint, appendToPath);
  1363. if (!retval)
  1364. {
  1365. /* docs say not to destruct in case of failure, so cheat. */
  1366. io->opaque = NULL;
  1367. io->destroy(io);
  1368. } /* if */
  1369. return retval;
  1370. } /* PHYSFS_mountHandle */
  1371. int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
  1372. {
  1373. BAIL_IF_MACRO(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1374. return doMount(NULL, newDir, mountPoint, appendToPath);
  1375. } /* PHYSFS_mount */
  1376. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  1377. {
  1378. return doMount(NULL, newDir, NULL, appendToPath);
  1379. } /* PHYSFS_addToSearchPath */
  1380. int PHYSFS_removeFromSearchPath(const char *oldDir)
  1381. {
  1382. return PHYSFS_unmount(oldDir);
  1383. } /* PHYSFS_removeFromSearchPath */
  1384. int PHYSFS_unmount(const char *oldDir)
  1385. {
  1386. DirHandle *i;
  1387. DirHandle *prev = NULL;
  1388. DirHandle *next = NULL;
  1389. BAIL_IF_MACRO(oldDir == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1390. __PHYSFS_platformGrabMutex(stateLock);
  1391. for (i = searchPath; i != NULL; i = i->next)
  1392. {
  1393. if (strcmp(i->dirName, oldDir) == 0)
  1394. {
  1395. next = i->next;
  1396. BAIL_IF_MACRO_MUTEX(!freeDirHandle(i, openReadList), ERRPASS,
  1397. stateLock, 0);
  1398. if (prev == NULL)
  1399. searchPath = next;
  1400. else
  1401. prev->next = next;
  1402. BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
  1403. } /* if */
  1404. prev = i;
  1405. } /* for */
  1406. BAIL_MACRO_MUTEX(PHYSFS_ERR_NOT_MOUNTED, stateLock, 0);
  1407. } /* PHYSFS_unmount */
  1408. char **PHYSFS_getSearchPath(void)
  1409. {
  1410. return doEnumStringList(PHYSFS_getSearchPathCallback);
  1411. } /* PHYSFS_getSearchPath */
  1412. const char *PHYSFS_getMountPoint(const char *dir)
  1413. {
  1414. DirHandle *i;
  1415. __PHYSFS_platformGrabMutex(stateLock);
  1416. for (i = searchPath; i != NULL; i = i->next)
  1417. {
  1418. if (strcmp(i->dirName, dir) == 0)
  1419. {
  1420. const char *retval = ((i->mountPoint) ? i->mountPoint : "/");
  1421. __PHYSFS_platformReleaseMutex(stateLock);
  1422. return retval;
  1423. } /* if */
  1424. } /* for */
  1425. __PHYSFS_platformReleaseMutex(stateLock);
  1426. BAIL_MACRO(PHYSFS_ERR_NOT_MOUNTED, NULL);
  1427. } /* PHYSFS_getMountPoint */
  1428. void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
  1429. {
  1430. DirHandle *i;
  1431. __PHYSFS_platformGrabMutex(stateLock);
  1432. for (i = searchPath; i != NULL; i = i->next)
  1433. callback(data, i->dirName);
  1434. __PHYSFS_platformReleaseMutex(stateLock);
  1435. } /* PHYSFS_getSearchPathCallback */
  1436. /* Split out to avoid stack allocation in a loop. */
  1437. static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
  1438. int archivesFirst)
  1439. {
  1440. const char *d = PHYSFS_getRealDir(i);
  1441. const size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
  1442. char *str = (char *) __PHYSFS_smallAlloc(allocsize);
  1443. if (str != NULL)
  1444. {
  1445. sprintf(str, "%s%s%s", d, dirsep, i);
  1446. PHYSFS_mount(str, NULL, archivesFirst == 0);
  1447. __PHYSFS_smallFree(str);
  1448. } /* if */
  1449. } /* setSaneCfgAddPath */
  1450. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  1451. const char *archiveExt, int includeCdRoms,
  1452. int archivesFirst)
  1453. {
  1454. const char *dirsep = PHYSFS_getDirSeparator();
  1455. const char *basedir;
  1456. const char *prefdir;
  1457. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1458. prefdir = PHYSFS_getPrefDir(organization, appName);
  1459. BAIL_IF_MACRO(!prefdir, ERRPASS, 0);
  1460. basedir = PHYSFS_getBaseDir();
  1461. BAIL_IF_MACRO(!basedir, ERRPASS, 0);
  1462. BAIL_IF_MACRO(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0);
  1463. /* Put write dir first in search path... */
  1464. PHYSFS_mount(prefdir, NULL, 0);
  1465. /* Put base path on search path... */
  1466. PHYSFS_mount(basedir, NULL, 1);
  1467. /* handle CD-ROMs... */
  1468. if (includeCdRoms)
  1469. {
  1470. char **cds = PHYSFS_getCdRomDirs();
  1471. char **i;
  1472. for (i = cds; *i != NULL; i++)
  1473. PHYSFS_mount(*i, NULL, 1);
  1474. PHYSFS_freeList(cds);
  1475. } /* if */
  1476. /* Root out archives, and add them to search path... */
  1477. if (archiveExt != NULL)
  1478. {
  1479. char **rc = PHYSFS_enumerateFiles("/");
  1480. char **i;
  1481. size_t extlen = strlen(archiveExt);
  1482. char *ext;
  1483. for (i = rc; *i != NULL; i++)
  1484. {
  1485. size_t l = strlen(*i);
  1486. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  1487. {
  1488. ext = (*i) + (l - extlen);
  1489. if (__PHYSFS_utf8stricmp(ext, archiveExt) == 0)
  1490. setSaneCfgAddPath(*i, l, dirsep, archivesFirst);
  1491. } /* if */
  1492. } /* for */
  1493. PHYSFS_freeList(rc);
  1494. } /* if */
  1495. return 1;
  1496. } /* PHYSFS_setSaneConfig */
  1497. void PHYSFS_permitSymbolicLinks(int allow)
  1498. {
  1499. allowSymLinks = allow;
  1500. } /* PHYSFS_permitSymbolicLinks */
  1501. int PHYSFS_symbolicLinksPermitted(void)
  1502. {
  1503. return allowSymLinks;
  1504. } /* PHYSFS_symbolicLinksPermitted */
  1505. /*
  1506. * Verify that (fname) (in platform-independent notation), in relation
  1507. * to (h) is secure. That means that each element of fname is checked
  1508. * for symlinks (if they aren't permitted). This also allows for quick
  1509. * rejection of files that exist outside an archive's mountpoint.
  1510. *
  1511. * With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
  1512. * at a time), you should always pass zero for "allowMissing" for efficiency.
  1513. *
  1514. * (fname) must point to an output from sanitizePlatformIndependentPath(),
  1515. * since it will make sure that path names are in the right format for
  1516. * passing certain checks. It will also do checks for "insecure" pathnames
  1517. * like ".." which should be done once instead of once per archive. This also
  1518. * gives us license to treat (fname) as scratch space in this function.
  1519. *
  1520. * Returns non-zero if string is safe, zero if there's a security issue.
  1521. * PHYSFS_getLastError() will specify what was wrong. (*fname) will be
  1522. * updated to point past any mount point elements so it is prepared to
  1523. * be used with the archiver directly.
  1524. */
  1525. static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
  1526. {
  1527. char *fname = *_fname;
  1528. int retval = 1;
  1529. char *start;
  1530. char *end;
  1531. if (*fname == '\0') /* quick rejection. */
  1532. return 1;
  1533. /* !!! FIXME: This codeblock sucks. */
  1534. if (h->mountPoint != NULL) /* NULL mountpoint means "/". */
  1535. {
  1536. size_t mntpntlen = strlen(h->mountPoint);
  1537. size_t len = strlen(fname);
  1538. assert(mntpntlen > 1); /* root mount points should be NULL. */
  1539. /* not under the mountpoint, so skip this archive. */
  1540. BAIL_IF_MACRO(len < mntpntlen-1, PHYSFS_ERR_NOT_FOUND, 0);
  1541. /* !!! FIXME: Case insensitive? */
  1542. retval = strncmp(h->mountPoint, fname, mntpntlen-1);
  1543. BAIL_IF_MACRO(retval != 0, PHYSFS_ERR_NOT_FOUND, 0);
  1544. if (len > mntpntlen-1) /* corner case... */
  1545. BAIL_IF_MACRO(fname[mntpntlen-1]!='/', PHYSFS_ERR_NOT_FOUND, 0);
  1546. fname += mntpntlen-1; /* move to start of actual archive path. */
  1547. if (*fname == '/')
  1548. fname++;
  1549. *_fname = fname; /* skip mountpoint for later use. */
  1550. retval = 1; /* may be reset, below. */
  1551. } /* if */
  1552. start = fname;
  1553. if (!allowSymLinks)
  1554. {
  1555. while (1)
  1556. {
  1557. PHYSFS_Stat statbuf;
  1558. int rc = 0;
  1559. end = strchr(start, '/');
  1560. if (end != NULL) *end = '\0';
  1561. rc = h->funcs->stat(h->opaque, fname, &statbuf);
  1562. if (rc)
  1563. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  1564. else if (currentErrorCode() == PHYSFS_ERR_NOT_FOUND)
  1565. retval = 0;
  1566. if (end != NULL) *end = '/';
  1567. /* insecure path (has a disallowed symlink in it)? */
  1568. BAIL_IF_MACRO(rc, PHYSFS_ERR_SYMLINK_FORBIDDEN, 0);
  1569. /* break out early if path element is missing. */
  1570. if (!retval)
  1571. {
  1572. /*
  1573. * We need to clear it if it's the last element of the path,
  1574. * since this might be a non-existant file we're opening
  1575. * for writing...
  1576. */
  1577. if ((end == NULL) || (allowMissing))
  1578. retval = 1;
  1579. break;
  1580. } /* if */
  1581. if (end == NULL)
  1582. break;
  1583. start = end + 1;
  1584. } /* while */
  1585. } /* if */
  1586. return retval;
  1587. } /* verifyPath */
  1588. static int doMkdir(const char *_dname, char *dname)
  1589. {
  1590. DirHandle *h;
  1591. char *start;
  1592. char *end;
  1593. int retval = 0;
  1594. int exists = 1; /* force existance check on first path element. */
  1595. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), ERRPASS, 0);
  1596. __PHYSFS_platformGrabMutex(stateLock);
  1597. BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  1598. h = writeDir;
  1599. BAIL_IF_MACRO_MUTEX(!verifyPath(h, &dname, 1), ERRPASS, stateLock, 0);
  1600. start = dname;
  1601. while (1)
  1602. {
  1603. end = strchr(start, '/');
  1604. if (end != NULL)
  1605. *end = '\0';
  1606. /* only check for existance if all parent dirs existed, too... */
  1607. if (exists)
  1608. {
  1609. PHYSFS_Stat statbuf;
  1610. const int rc = h->funcs->stat(h->opaque, dname, &statbuf);
  1611. if ((!rc) && (currentErrorCode() == PHYSFS_ERR_NOT_FOUND))
  1612. exists = 0;
  1613. retval = ((rc) && (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY));
  1614. } /* if */
  1615. if (!exists)
  1616. retval = h->funcs->mkdir(h->opaque, dname);
  1617. if (!retval)
  1618. break;
  1619. if (end == NULL)
  1620. break;
  1621. *end = '/';
  1622. start = end + 1;
  1623. } /* while */
  1624. __PHYSFS_platformReleaseMutex(stateLock);
  1625. return retval;
  1626. } /* doMkdir */
  1627. int PHYSFS_mkdir(const char *_dname)
  1628. {
  1629. int retval = 0;
  1630. char *dname;
  1631. size_t len;
  1632. BAIL_IF_MACRO(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1633. len = strlen(_dname) + 1;
  1634. dname = (char *) __PHYSFS_smallAlloc(len);
  1635. BAIL_IF_MACRO(!dname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1636. retval = doMkdir(_dname, dname);
  1637. __PHYSFS_smallFree(dname);
  1638. return retval;
  1639. } /* PHYSFS_mkdir */
  1640. static int doDelete(const char *_fname, char *fname)
  1641. {
  1642. int retval;
  1643. DirHandle *h;
  1644. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), ERRPASS, 0);
  1645. __PHYSFS_platformGrabMutex(stateLock);
  1646. BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  1647. h = writeDir;
  1648. BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), ERRPASS, stateLock, 0);
  1649. retval = h->funcs->remove(h->opaque, fname);
  1650. __PHYSFS_platformReleaseMutex(stateLock);
  1651. return retval;
  1652. } /* doDelete */
  1653. int PHYSFS_delete(const char *_fname)
  1654. {
  1655. int retval;
  1656. char *fname;
  1657. size_t len;
  1658. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1659. len = strlen(_fname) + 1;
  1660. fname = (char *) __PHYSFS_smallAlloc(len);
  1661. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1662. retval = doDelete(_fname, fname);
  1663. __PHYSFS_smallFree(fname);
  1664. return retval;
  1665. } /* PHYSFS_delete */
  1666. const char *PHYSFS_getRealDir(const char *_fname)
  1667. {
  1668. const char *retval = NULL;
  1669. char *fname = NULL;
  1670. size_t len;
  1671. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1672. len = strlen(_fname) + 1;
  1673. fname = __PHYSFS_smallAlloc(len);
  1674. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1675. if (sanitizePlatformIndependentPath(_fname, fname))
  1676. {
  1677. DirHandle *i;
  1678. __PHYSFS_platformGrabMutex(stateLock);
  1679. for (i = searchPath; i != NULL; i = i->next)
  1680. {
  1681. char *arcfname = fname;
  1682. if (partOfMountPoint(i, arcfname))
  1683. {
  1684. retval = i->dirName;
  1685. break;
  1686. } /* if */
  1687. else if (verifyPath(i, &arcfname, 0))
  1688. {
  1689. PHYSFS_Stat statbuf;
  1690. if (i->funcs->stat(i->opaque, arcfname, &statbuf))
  1691. {
  1692. retval = i->dirName;
  1693. break;
  1694. } /* if */
  1695. } /* if */
  1696. } /* for */
  1697. __PHYSFS_platformReleaseMutex(stateLock);
  1698. } /* if */
  1699. __PHYSFS_smallFree(fname);
  1700. return retval;
  1701. } /* PHYSFS_getRealDir */
  1702. static int locateInStringList(const char *str,
  1703. char **list,
  1704. PHYSFS_uint32 *pos)
  1705. {
  1706. PHYSFS_uint32 len = *pos;
  1707. PHYSFS_uint32 half_len;
  1708. PHYSFS_uint32 lo = 0;
  1709. PHYSFS_uint32 middle;
  1710. int cmp;
  1711. while (len > 0)
  1712. {
  1713. half_len = len >> 1;
  1714. middle = lo + half_len;
  1715. cmp = strcmp(list[middle], str);
  1716. if (cmp == 0) /* it's in the list already. */
  1717. return 1;
  1718. else if (cmp > 0)
  1719. len = half_len;
  1720. else
  1721. {
  1722. lo = middle + 1;
  1723. len -= half_len + 1;
  1724. } /* else */
  1725. } /* while */
  1726. *pos = lo;
  1727. return 0;
  1728. } /* locateInStringList */
  1729. static void enumFilesCallback(void *data, const char *origdir, const char *str)
  1730. {
  1731. PHYSFS_uint32 pos;
  1732. void *ptr;
  1733. char *newstr;
  1734. EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
  1735. /*
  1736. * See if file is in the list already, and if not, insert it in there
  1737. * alphabetically...
  1738. */
  1739. pos = pecd->size;
  1740. if (locateInStringList(str, pecd->list, &pos))
  1741. return; /* already in the list. */
  1742. ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
  1743. newstr = (char *) allocator.Malloc(strlen(str) + 1);
  1744. if (ptr != NULL)
  1745. pecd->list = (char **) ptr;
  1746. if ((ptr == NULL) || (newstr == NULL))
  1747. return; /* better luck next time. */
  1748. strcpy(newstr, str);
  1749. if (pos != pecd->size)
  1750. {
  1751. memmove(&pecd->list[pos+1], &pecd->list[pos],
  1752. sizeof (char *) * ((pecd->size) - pos));
  1753. } /* if */
  1754. pecd->list[pos] = newstr;
  1755. pecd->size++;
  1756. } /* enumFilesCallback */
  1757. char **PHYSFS_enumerateFiles(const char *path)
  1758. {
  1759. EnumStringListCallbackData ecd;
  1760. memset(&ecd, '\0', sizeof (ecd));
  1761. ecd.list = (char **) allocator.Malloc(sizeof (char *));
  1762. BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1763. PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
  1764. ecd.list[ecd.size] = NULL;
  1765. return ecd.list;
  1766. } /* PHYSFS_enumerateFiles */
  1767. /*
  1768. * Broke out to seperate function so we can use stack allocation gratuitously.
  1769. */
  1770. static void enumerateFromMountPoint(DirHandle *i, const char *arcfname,
  1771. PHYSFS_EnumFilesCallback callback,
  1772. const char *_fname, void *data)
  1773. {
  1774. const size_t len = strlen(arcfname);
  1775. char *ptr = NULL;
  1776. char *end = NULL;
  1777. const size_t slen = strlen(i->mountPoint) + 1;
  1778. char *mountPoint = (char *) __PHYSFS_smallAlloc(slen);
  1779. if (mountPoint == NULL)
  1780. return; /* oh well. */
  1781. strcpy(mountPoint, i->mountPoint);
  1782. ptr = mountPoint + ((len) ? len + 1 : 0);
  1783. end = strchr(ptr, '/');
  1784. assert(end); /* should always find a terminating '/'. */
  1785. *end = '\0';
  1786. callback(data, _fname, ptr);
  1787. __PHYSFS_smallFree(mountPoint);
  1788. } /* enumerateFromMountPoint */
  1789. typedef struct SymlinkFilterData
  1790. {
  1791. PHYSFS_EnumFilesCallback callback;
  1792. void *callbackData;
  1793. DirHandle *dirhandle;
  1794. } SymlinkFilterData;
  1795. static void enumCallbackFilterSymLinks(void *_data, const char *origdir,
  1796. const char *fname)
  1797. {
  1798. const char *trimmedDir = (*origdir == '/') ? (origdir+1) : origdir;
  1799. const size_t slen = strlen(trimmedDir) + strlen(fname) + 2;
  1800. char *path = (char *) __PHYSFS_smallAlloc(slen);
  1801. if (path != NULL)
  1802. {
  1803. SymlinkFilterData *data = (SymlinkFilterData *) _data;
  1804. const DirHandle *dh = data->dirhandle;
  1805. PHYSFS_Stat statbuf;
  1806. sprintf(path, "%s%s%s", trimmedDir, *trimmedDir ? "/" : "", fname);
  1807. if (dh->funcs->stat(dh->opaque, path, &statbuf))
  1808. {
  1809. /* Pass it on to the application if it's not a symlink. */
  1810. if (statbuf.filetype != PHYSFS_FILETYPE_SYMLINK)
  1811. data->callback(data->callbackData, origdir, fname);
  1812. } /* if */
  1813. __PHYSFS_smallFree(path);
  1814. } /* if */
  1815. } /* enumCallbackFilterSymLinks */
  1816. /* !!! FIXME: this should report error conditions. */
  1817. void PHYSFS_enumerateFilesCallback(const char *_fname,
  1818. PHYSFS_EnumFilesCallback callback,
  1819. void *data)
  1820. {
  1821. size_t len;
  1822. char *fname;
  1823. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
  1824. BAIL_IF_MACRO(!callback, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
  1825. len = strlen(_fname) + 1;
  1826. fname = (char *) __PHYSFS_smallAlloc(len);
  1827. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, ) /*0*/;
  1828. if (sanitizePlatformIndependentPath(_fname, fname))
  1829. {
  1830. DirHandle *i;
  1831. SymlinkFilterData filterdata;
  1832. __PHYSFS_platformGrabMutex(stateLock);
  1833. if (!allowSymLinks)
  1834. {
  1835. memset(&filterdata, '\0', sizeof (filterdata));
  1836. filterdata.callback = callback;
  1837. filterdata.callbackData = data;
  1838. } /* if */
  1839. for (i = searchPath; i != NULL; i = i->next)
  1840. {
  1841. char *arcfname = fname;
  1842. if (partOfMountPoint(i, arcfname))
  1843. enumerateFromMountPoint(i, arcfname, callback, _fname, data);
  1844. else if (verifyPath(i, &arcfname, 0))
  1845. {
  1846. if ((!allowSymLinks) && (i->funcs->info.supportsSymlinks))
  1847. {
  1848. filterdata.dirhandle = i;
  1849. i->funcs->enumerateFiles(i->opaque, arcfname,
  1850. enumCallbackFilterSymLinks,
  1851. _fname, &filterdata);
  1852. } /* if */
  1853. else
  1854. {
  1855. i->funcs->enumerateFiles(i->opaque, arcfname,
  1856. callback, _fname, data);
  1857. } /* else */
  1858. } /* else if */
  1859. } /* for */
  1860. __PHYSFS_platformReleaseMutex(stateLock);
  1861. } /* if */
  1862. __PHYSFS_smallFree(fname);
  1863. } /* PHYSFS_enumerateFilesCallback */
  1864. int PHYSFS_exists(const char *fname)
  1865. {
  1866. return (PHYSFS_getRealDir(fname) != NULL);
  1867. } /* PHYSFS_exists */
  1868. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1869. {
  1870. PHYSFS_Stat statbuf;
  1871. BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, -1);
  1872. return statbuf.modtime;
  1873. } /* PHYSFS_getLastModTime */
  1874. int PHYSFS_isDirectory(const char *fname)
  1875. {
  1876. PHYSFS_Stat statbuf;
  1877. BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
  1878. return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  1879. } /* PHYSFS_isDirectory */
  1880. int PHYSFS_isSymbolicLink(const char *fname)
  1881. {
  1882. PHYSFS_Stat statbuf;
  1883. BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
  1884. return (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  1885. } /* PHYSFS_isSymbolicLink */
  1886. static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
  1887. {
  1888. FileHandle *fh = NULL;
  1889. size_t len;
  1890. char *fname;
  1891. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1892. len = strlen(_fname) + 1;
  1893. fname = (char *) __PHYSFS_smallAlloc(len);
  1894. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1895. if (sanitizePlatformIndependentPath(_fname, fname))
  1896. {
  1897. PHYSFS_Io *io = NULL;
  1898. DirHandle *h = NULL;
  1899. const PHYSFS_Archiver *f;
  1900. __PHYSFS_platformGrabMutex(stateLock);
  1901. GOTO_IF_MACRO(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, doOpenWriteEnd);
  1902. h = writeDir;
  1903. GOTO_IF_MACRO(!verifyPath(h, &fname, 0), ERRPASS, doOpenWriteEnd);
  1904. f = h->funcs;
  1905. if (appending)
  1906. io = f->openAppend(h->opaque, fname);
  1907. else
  1908. io = f->openWrite(h->opaque, fname);
  1909. GOTO_IF_MACRO(!io, ERRPASS, doOpenWriteEnd);
  1910. fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  1911. if (fh == NULL)
  1912. {
  1913. io->destroy(io);
  1914. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, doOpenWriteEnd);
  1915. } /* if */
  1916. else
  1917. {
  1918. memset(fh, '\0', sizeof (FileHandle));
  1919. fh->io = io;
  1920. fh->dirHandle = h;
  1921. fh->next = openWriteList;
  1922. openWriteList = fh;
  1923. } /* else */
  1924. doOpenWriteEnd:
  1925. __PHYSFS_platformReleaseMutex(stateLock);
  1926. } /* if */
  1927. __PHYSFS_smallFree(fname);
  1928. return ((PHYSFS_File *) fh);
  1929. } /* doOpenWrite */
  1930. PHYSFS_File *PHYSFS_openWrite(const char *filename)
  1931. {
  1932. return doOpenWrite(filename, 0);
  1933. } /* PHYSFS_openWrite */
  1934. PHYSFS_File *PHYSFS_openAppend(const char *filename)
  1935. {
  1936. return doOpenWrite(filename, 1);
  1937. } /* PHYSFS_openAppend */
  1938. PHYSFS_File *PHYSFS_openRead(const char *_fname)
  1939. {
  1940. FileHandle *fh = NULL;
  1941. char *fname;
  1942. size_t len;
  1943. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1944. len = strlen(_fname) + 1;
  1945. fname = (char *) __PHYSFS_smallAlloc(len);
  1946. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  1947. if (sanitizePlatformIndependentPath(_fname, fname))
  1948. {
  1949. DirHandle *i = NULL;
  1950. PHYSFS_Io *io = NULL;
  1951. __PHYSFS_platformGrabMutex(stateLock);
  1952. GOTO_IF_MACRO(!searchPath, PHYSFS_ERR_NOT_FOUND, openReadEnd);
  1953. for (i = searchPath; i != NULL; i = i->next)
  1954. {
  1955. char *arcfname = fname;
  1956. if (verifyPath(i, &arcfname, 0))
  1957. {
  1958. io = i->funcs->openRead(i->opaque, arcfname);
  1959. if (io)
  1960. break;
  1961. } /* if */
  1962. } /* for */
  1963. GOTO_IF_MACRO(!io, ERRPASS, openReadEnd);
  1964. fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  1965. if (fh == NULL)
  1966. {
  1967. io->destroy(io);
  1968. GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, openReadEnd);
  1969. } /* if */
  1970. memset(fh, '\0', sizeof (FileHandle));
  1971. fh->io = io;
  1972. fh->forReading = 1;
  1973. fh->dirHandle = i;
  1974. fh->next = openReadList;
  1975. openReadList = fh;
  1976. openReadEnd:
  1977. __PHYSFS_platformReleaseMutex(stateLock);
  1978. } /* if */
  1979. __PHYSFS_smallFree(fname);
  1980. return ((PHYSFS_File *) fh);
  1981. } /* PHYSFS_openRead */
  1982. static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
  1983. {
  1984. FileHandle *prev = NULL;
  1985. FileHandle *i;
  1986. int rc = 1;
  1987. for (i = *list; i != NULL; i = i->next)
  1988. {
  1989. if (i == handle) /* handle is in this list? */
  1990. {
  1991. PHYSFS_Io *io = handle->io;
  1992. PHYSFS_uint8 *tmp = handle->buffer;
  1993. rc = PHYSFS_flush((PHYSFS_File *) handle);
  1994. if (!rc)
  1995. return -1;
  1996. io->destroy(io);
  1997. if (tmp != NULL) /* free any associated buffer. */
  1998. allocator.Free(tmp);
  1999. if (prev == NULL)
  2000. *list = handle->next;
  2001. else
  2002. prev->next = handle->next;
  2003. allocator.Free(handle);
  2004. return 1;
  2005. } /* if */
  2006. prev = i;
  2007. } /* for */
  2008. return 0;
  2009. } /* closeHandleInOpenList */
  2010. int PHYSFS_close(PHYSFS_File *_handle)
  2011. {
  2012. FileHandle *handle = (FileHandle *) _handle;
  2013. int rc;
  2014. __PHYSFS_platformGrabMutex(stateLock);
  2015. /* -1 == close failure. 0 == not found. 1 == success. */
  2016. rc = closeHandleInOpenList(&openReadList, handle);
  2017. BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
  2018. if (!rc)
  2019. {
  2020. rc = closeHandleInOpenList(&openWriteList, handle);
  2021. BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
  2022. } /* if */
  2023. __PHYSFS_platformReleaseMutex(stateLock);
  2024. BAIL_IF_MACRO(!rc, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2025. return 1;
  2026. } /* PHYSFS_close */
  2027. static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
  2028. PHYSFS_uint64 len)
  2029. {
  2030. PHYSFS_Io *io = NULL;
  2031. PHYSFS_sint64 retval = 0;
  2032. PHYSFS_uint32 buffered = 0;
  2033. PHYSFS_sint64 rc = 0;
  2034. if (len == 0)
  2035. return 0;
  2036. buffered = fh->buffill - fh->bufpos;
  2037. if (buffered >= len) /* totally in the buffer, just copy and return! */
  2038. {
  2039. memcpy(buffer, fh->buffer + fh->bufpos, (size_t) len);
  2040. fh->bufpos += (PHYSFS_uint32) len;
  2041. return (PHYSFS_sint64) len;
  2042. } /* else if */
  2043. if (buffered > 0) /* partially in the buffer... */
  2044. {
  2045. memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
  2046. buffer = ((PHYSFS_uint8 *) buffer) + buffered;
  2047. len -= buffered;
  2048. retval = buffered;
  2049. fh->buffill = fh->bufpos = 0;
  2050. } /* if */
  2051. /* if you got here, the buffer is drained and we still need bytes. */
  2052. assert(len > 0);
  2053. io = fh->io;
  2054. if (len >= fh->bufsize) /* need more than the buffer takes. */
  2055. {
  2056. /* leave buffer empty, go right to output instead. */
  2057. rc = io->read(io, buffer, len);
  2058. if (rc < 0)
  2059. return ((retval == 0) ? rc : retval);
  2060. return retval + rc;
  2061. } /* if */
  2062. /* need less than buffer can take. Fill buffer. */
  2063. rc = io->read(io, fh->buffer, fh->bufsize);
  2064. if (rc < 0)
  2065. return ((retval == 0) ? rc : retval);
  2066. assert(fh->bufpos == 0);
  2067. fh->buffill = (PHYSFS_uint32) rc;
  2068. rc = doBufferedRead(fh, buffer, len); /* go from the start, again. */
  2069. if (rc < 0)
  2070. return ((retval == 0) ? rc : retval);
  2071. return retval + rc;
  2072. } /* doBufferedRead */
  2073. PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
  2074. PHYSFS_uint32 size, PHYSFS_uint32 count)
  2075. {
  2076. const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
  2077. const PHYSFS_sint64 retval = PHYSFS_readBytes(handle, buffer, len);
  2078. return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
  2079. } /* PHYSFS_read */
  2080. PHYSFS_sint64 PHYSFS_readBytes(PHYSFS_File *handle, void *buffer,
  2081. PHYSFS_uint64 len)
  2082. {
  2083. FileHandle *fh = (FileHandle *) handle;
  2084. #ifdef PHYSFS_NO_64BIT_SUPPORT
  2085. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
  2086. #else
  2087. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
  2088. #endif
  2089. if (!__PHYSFS_ui64FitsAddressSpace(len))
  2090. BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2091. BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2092. BAIL_IF_MACRO(!fh->forReading, PHYSFS_ERR_OPEN_FOR_WRITING, -1);
  2093. BAIL_IF_MACRO(len == 0, ERRPASS, 0);
  2094. if (fh->buffer)
  2095. return doBufferedRead(fh, buffer, len);
  2096. return fh->io->read(fh->io, buffer, len);
  2097. } /* PHYSFS_readBytes */
  2098. static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
  2099. PHYSFS_uint64 len)
  2100. {
  2101. FileHandle *fh = (FileHandle *) handle;
  2102. /* whole thing fits in the buffer? */
  2103. if ( (((PHYSFS_uint64) fh->buffill) + len) < fh->bufsize )
  2104. {
  2105. memcpy(fh->buffer + fh->buffill, buffer, (size_t) len);
  2106. fh->buffill += (PHYSFS_uint32) len;
  2107. return (PHYSFS_sint64) len;
  2108. } /* if */
  2109. /* would overflow buffer. Flush and then write the new objects, too. */
  2110. BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, -1);
  2111. return fh->io->write(fh->io, buffer, len);
  2112. } /* doBufferedWrite */
  2113. PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
  2114. PHYSFS_uint32 size, PHYSFS_uint32 count)
  2115. {
  2116. const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
  2117. const PHYSFS_sint64 retval = PHYSFS_writeBytes(handle, buffer, len);
  2118. return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
  2119. } /* PHYSFS_write */
  2120. PHYSFS_sint64 PHYSFS_writeBytes(PHYSFS_File *handle, const void *buffer,
  2121. PHYSFS_uint64 len)
  2122. {
  2123. FileHandle *fh = (FileHandle *) handle;
  2124. #ifdef PHYSFS_NO_64BIT_SUPPORT
  2125. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
  2126. #else
  2127. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
  2128. #endif
  2129. if (!__PHYSFS_ui64FitsAddressSpace(len))
  2130. BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2131. BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2132. BAIL_IF_MACRO(fh->forReading, PHYSFS_ERR_OPEN_FOR_READING, -1);
  2133. BAIL_IF_MACRO(len == 0, ERRPASS, 0);
  2134. if (fh->buffer)
  2135. return doBufferedWrite(handle, buffer, len);
  2136. return fh->io->write(fh->io, buffer, len);
  2137. } /* PHYSFS_write */
  2138. int PHYSFS_eof(PHYSFS_File *handle)
  2139. {
  2140. FileHandle *fh = (FileHandle *) handle;
  2141. if (!fh->forReading) /* never EOF on files opened for write/append. */
  2142. return 0;
  2143. /* can't be eof if buffer isn't empty */
  2144. if (fh->bufpos == fh->buffill)
  2145. {
  2146. /* check the Io. */
  2147. PHYSFS_Io *io = fh->io;
  2148. const PHYSFS_sint64 pos = io->tell(io);
  2149. const PHYSFS_sint64 len = io->length(io);
  2150. if ((pos < 0) || (len < 0))
  2151. return 0; /* beats me. */
  2152. return (pos >= len);
  2153. } /* if */
  2154. return 0;
  2155. } /* PHYSFS_eof */
  2156. PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
  2157. {
  2158. FileHandle *fh = (FileHandle *) handle;
  2159. const PHYSFS_sint64 pos = fh->io->tell(fh->io);
  2160. const PHYSFS_sint64 retval = fh->forReading ?
  2161. (pos - fh->buffill) + fh->bufpos :
  2162. (pos + fh->buffill);
  2163. return retval;
  2164. } /* PHYSFS_tell */
  2165. int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
  2166. {
  2167. FileHandle *fh = (FileHandle *) handle;
  2168. BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
  2169. if (fh->buffer && fh->forReading)
  2170. {
  2171. /* avoid throwing away our precious buffer if seeking within it. */
  2172. PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
  2173. if ( /* seeking within the already-buffered range? */
  2174. ((offset >= 0) && (offset <= fh->buffill - fh->bufpos)) /* fwd */
  2175. || ((offset < 0) && (-offset <= fh->bufpos)) /* backward */ )
  2176. {
  2177. fh->bufpos += (PHYSFS_uint32) offset;
  2178. return 1; /* successful seek */
  2179. } /* if */
  2180. } /* if */
  2181. /* we have to fall back to a 'raw' seek. */
  2182. fh->buffill = fh->bufpos = 0;
  2183. return fh->io->seek(fh->io, pos);
  2184. } /* PHYSFS_seek */
  2185. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
  2186. {
  2187. PHYSFS_Io *io = ((FileHandle *) handle)->io;
  2188. return io->length(io);
  2189. } /* PHYSFS_filelength */
  2190. int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
  2191. {
  2192. FileHandle *fh = (FileHandle *) handle;
  2193. PHYSFS_uint32 bufsize;
  2194. /* !!! FIXME: actually, why use 32 bits here? */
  2195. /*BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);*/
  2196. BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2197. bufsize = (PHYSFS_uint32) _bufsize;
  2198. BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
  2199. /*
  2200. * For reads, we need to move the file pointer to where it would be
  2201. * if we weren't buffering, so that the next read will get the
  2202. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  2203. */
  2204. if ((fh->forReading) && (fh->buffill != fh->bufpos))
  2205. {
  2206. PHYSFS_uint64 pos;
  2207. const PHYSFS_sint64 curpos = fh->io->tell(fh->io);
  2208. BAIL_IF_MACRO(curpos == -1, ERRPASS, 0);
  2209. pos = ((curpos - fh->buffill) + fh->bufpos);
  2210. BAIL_IF_MACRO(!fh->io->seek(fh->io, pos), ERRPASS, 0);
  2211. } /* if */
  2212. if (bufsize == 0) /* delete existing buffer. */
  2213. {
  2214. if (fh->buffer)
  2215. {
  2216. allocator.Free(fh->buffer);
  2217. fh->buffer = NULL;
  2218. } /* if */
  2219. } /* if */
  2220. else
  2221. {
  2222. PHYSFS_uint8 *newbuf;
  2223. newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
  2224. BAIL_IF_MACRO(!newbuf, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  2225. fh->buffer = newbuf;
  2226. } /* else */
  2227. fh->bufsize = bufsize;
  2228. fh->buffill = fh->bufpos = 0;
  2229. return 1;
  2230. } /* PHYSFS_setBuffer */
  2231. int PHYSFS_flush(PHYSFS_File *handle)
  2232. {
  2233. FileHandle *fh = (FileHandle *) handle;
  2234. PHYSFS_Io *io;
  2235. PHYSFS_sint64 rc;
  2236. if ((fh->forReading) || (fh->bufpos == fh->buffill))
  2237. return 1; /* open for read or buffer empty are successful no-ops. */
  2238. /* dump buffer to disk. */
  2239. io = fh->io;
  2240. rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
  2241. BAIL_IF_MACRO(rc <= 0, ERRPASS, 0);
  2242. fh->bufpos = fh->buffill = 0;
  2243. return io->flush(io);
  2244. } /* PHYSFS_flush */
  2245. int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
  2246. {
  2247. int retval = 0;
  2248. char *fname;
  2249. size_t len;
  2250. BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2251. BAIL_IF_MACRO(!stat, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2252. len = strlen(_fname) + 1;
  2253. fname = (char *) __PHYSFS_smallAlloc(len);
  2254. BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, -1);
  2255. /* set some sane defaults... */
  2256. stat->filesize = -1;
  2257. stat->modtime = -1;
  2258. stat->createtime = -1;
  2259. stat->accesstime = -1;
  2260. stat->filetype = PHYSFS_FILETYPE_OTHER;
  2261. stat->readonly = 1; /* !!! FIXME */
  2262. if (sanitizePlatformIndependentPath(_fname, fname))
  2263. {
  2264. if (*fname == '\0')
  2265. {
  2266. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  2267. stat->readonly = !writeDir; /* Writeable if we have a writeDir */
  2268. retval = 1;
  2269. } /* if */
  2270. else
  2271. {
  2272. DirHandle *i;
  2273. int exists = 0;
  2274. __PHYSFS_platformGrabMutex(stateLock);
  2275. for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
  2276. {
  2277. char *arcfname = fname;
  2278. exists = partOfMountPoint(i, arcfname);
  2279. if (exists)
  2280. {
  2281. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  2282. stat->readonly = 1; /* !!! FIXME */
  2283. retval = 1;
  2284. } /* if */
  2285. else if (verifyPath(i, &arcfname, 0))
  2286. {
  2287. /* !!! FIXME: this test is wrong and should be elsewhere. */
  2288. stat->readonly = !(writeDir &&
  2289. (strcmp(writeDir->dirName, i->dirName) == 0));
  2290. retval = i->funcs->stat(i->opaque, arcfname, stat);
  2291. if ((retval) || (currentErrorCode() != PHYSFS_ERR_NOT_FOUND))
  2292. exists = 1;
  2293. } /* else if */
  2294. } /* for */
  2295. __PHYSFS_platformReleaseMutex(stateLock);
  2296. } /* else */
  2297. } /* if */
  2298. __PHYSFS_smallFree(fname);
  2299. return retval;
  2300. } /* PHYSFS_stat */
  2301. int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
  2302. {
  2303. return (io->read(io, buf, len) == len);
  2304. } /* __PHYSFS_readAll */
  2305. void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len)
  2306. {
  2307. void *useHeap = ((ptr == NULL) ? ((void *) 1) : ((void *) 0));
  2308. if (useHeap) /* too large for stack allocation or alloca() failed. */
  2309. ptr = allocator.Malloc(len+sizeof (void *));
  2310. if (ptr != NULL)
  2311. {
  2312. void **retval = (void **) ptr;
  2313. /*printf("%s alloc'd (%d) bytes at (%p).\n",
  2314. useHeap ? "heap" : "stack", (int) len, ptr);*/
  2315. *retval = useHeap;
  2316. return retval + 1;
  2317. } /* if */
  2318. return NULL; /* allocation failed. */
  2319. } /* __PHYSFS_initSmallAlloc */
  2320. void __PHYSFS_smallFree(void *ptr)
  2321. {
  2322. if (ptr != NULL)
  2323. {
  2324. void **block = ((void **) ptr) - 1;
  2325. const int useHeap = (*block != 0);
  2326. if (useHeap)
  2327. allocator.Free(block);
  2328. /*printf("%s free'd (%p).\n", useHeap ? "heap" : "stack", block);*/
  2329. } /* if */
  2330. } /* __PHYSFS_smallFree */
  2331. int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
  2332. {
  2333. BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
  2334. externalAllocator = (a != NULL);
  2335. if (externalAllocator)
  2336. memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
  2337. return 1;
  2338. } /* PHYSFS_setAllocator */
  2339. const PHYSFS_Allocator *PHYSFS_getAllocator(void)
  2340. {
  2341. BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
  2342. return &allocator;
  2343. } /* PHYSFS_getAllocator */
  2344. static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
  2345. {
  2346. if (!__PHYSFS_ui64FitsAddressSpace(s))
  2347. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2348. #undef malloc
  2349. return malloc((size_t) s);
  2350. } /* mallocAllocatorMalloc */
  2351. static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  2352. {
  2353. if (!__PHYSFS_ui64FitsAddressSpace(s))
  2354. BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2355. #undef realloc
  2356. return realloc(ptr, (size_t) s);
  2357. } /* mallocAllocatorRealloc */
  2358. static void mallocAllocatorFree(void *ptr)
  2359. {
  2360. #undef free
  2361. free(ptr);
  2362. } /* mallocAllocatorFree */
  2363. static void setDefaultAllocator(void)
  2364. {
  2365. assert(!externalAllocator);
  2366. if (!__PHYSFS_platformSetDefaultAllocator(&allocator))
  2367. {
  2368. allocator.Init = NULL;
  2369. allocator.Deinit = NULL;
  2370. allocator.Malloc = mallocAllocatorMalloc;
  2371. allocator.Realloc = mallocAllocatorRealloc;
  2372. allocator.Free = mallocAllocatorFree;
  2373. } /* if */
  2374. } /* setDefaultAllocator */
  2375. /* end of physfs.c ... */