physfs.c 84 KB

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