| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695 |
- /**
- * PhysicsFS; a portable, flexible file i/o abstraction.
- *
- * Documentation is in physfs.h. It's verbose, honest. :)
- *
- * Please see the file LICENSE.txt in the source's root directory.
- *
- * This file written by Ryan C. Gordon.
- */
- /* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "physfs.h"
- #define __PHYSICSFS_INTERNAL__
- #include "physfs_internal.h"
- typedef struct __PHYSFS_DIRHANDLE__
- {
- void *opaque; /* Instance data unique to the archiver. */
- char *dirName; /* Path to archive in platform-dependent notation. */
- char *mountPoint; /* Mountpoint in virtual file tree. */
- const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
- struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
- } DirHandle;
- typedef struct __PHYSFS_FILEHANDLE__
- {
- PHYSFS_Io *io; /* Instance data unique to the archiver for this file. */
- PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
- const DirHandle *dirHandle; /* Archiver instance that created this */
- PHYSFS_uint8 *buffer; /* Buffer, if set (NULL otherwise). Don't touch! */
- PHYSFS_uint32 bufsize; /* Bufsize, if set (0 otherwise). Don't touch! */
- PHYSFS_uint32 buffill; /* Buffer fill size. Don't touch! */
- PHYSFS_uint32 bufpos; /* Buffer position. Don't touch! */
- struct __PHYSFS_FILEHANDLE__ *next; /* linked list stuff. */
- } FileHandle;
- typedef struct __PHYSFS_ERRMSGTYPE__
- {
- void *tid;
- int errorAvailable;
- char errorString[80];
- struct __PHYSFS_ERRMSGTYPE__ *next;
- } ErrMsg;
- /* The various i/o drivers...some of these may not be compiled in. */
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_LZMA;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_LZMA;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ISO9660;
- extern const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660;
- static const PHYSFS_ArchiveInfo *supported_types[] =
- {
- #if (defined PHYSFS_SUPPORTS_ZIP)
- &__PHYSFS_ArchiveInfo_ZIP,
- #endif
- #if (defined PHYSFS_SUPPORTS_7Z)
- &__PHYSFS_ArchiveInfo_LZMA,
- #endif
- #if (defined PHYSFS_SUPPORTS_GRP)
- &__PHYSFS_ArchiveInfo_GRP,
- #endif
- #if (defined PHYSFS_SUPPORTS_QPAK)
- &__PHYSFS_ArchiveInfo_QPAK,
- #endif
- #if (defined PHYSFS_SUPPORTS_HOG)
- &__PHYSFS_ArchiveInfo_HOG,
- #endif
- #if (defined PHYSFS_SUPPORTS_MVL)
- &__PHYSFS_ArchiveInfo_MVL,
- #endif
- #if (defined PHYSFS_SUPPORTS_WAD)
- &__PHYSFS_ArchiveInfo_WAD,
- #endif
- #if (defined PHYSFS_SUPPORTS_ISO9660)
- &__PHYSFS_ArchiveInfo_ISO9660,
- #endif
- NULL
- };
- static const PHYSFS_Archiver *archivers[] =
- {
- #if (defined PHYSFS_SUPPORTS_ZIP)
- &__PHYSFS_Archiver_ZIP,
- #endif
- #if (defined PHYSFS_SUPPORTS_7Z)
- &__PHYSFS_Archiver_LZMA,
- #endif
- #if (defined PHYSFS_SUPPORTS_GRP)
- &__PHYSFS_Archiver_GRP,
- #endif
- #if (defined PHYSFS_SUPPORTS_QPAK)
- &__PHYSFS_Archiver_QPAK,
- #endif
- #if (defined PHYSFS_SUPPORTS_HOG)
- &__PHYSFS_Archiver_HOG,
- #endif
- #if (defined PHYSFS_SUPPORTS_MVL)
- &__PHYSFS_Archiver_MVL,
- #endif
- #if (defined PHYSFS_SUPPORTS_WAD)
- &__PHYSFS_Archiver_WAD,
- #endif
- #if (defined PHYSFS_SUPPORTS_ISO9660)
- &__PHYSFS_Archiver_ISO9660,
- #endif
- NULL
- };
- /* General PhysicsFS state ... */
- static int initialized = 0;
- static ErrMsg *errorMessages = NULL;
- static DirHandle *searchPath = NULL;
- static DirHandle *writeDir = NULL;
- static FileHandle *openWriteList = NULL;
- static FileHandle *openReadList = NULL;
- static char *baseDir = NULL;
- static char *userDir = NULL;
- static int allowSymLinks = 0;
- /* mutexes ... */
- static void *errorLock = NULL; /* protects error message list. */
- static void *stateLock = NULL; /* protects other PhysFS static state. */
- /* allocator ... */
- static int externalAllocator = 0;
- PHYSFS_Allocator allocator;
- /* PHYSFS_Io implementation for i/o to physical filesystem... */
- /* !!! FIXME: maybe refcount the paths in a string pool? */
- typedef struct __PHYSFS_NativeIoInfo
- {
- void *handle;
- const char *path;
- int mode; /* 'r', 'w', or 'a' */
- } NativeIoInfo;
- static PHYSFS_sint64 nativeIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- return __PHYSFS_platformRead(info->handle, buf, len);
- } /* nativeIo_read */
- static PHYSFS_sint64 nativeIo_write(PHYSFS_Io *io, const void *buffer,
- PHYSFS_uint64 len)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- return __PHYSFS_platformWrite(info->handle, buffer, len);
- } /* nativeIo_write */
- static int nativeIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- return __PHYSFS_platformSeek(info->handle, offset);
- } /* nativeIo_seek */
- static PHYSFS_sint64 nativeIo_tell(PHYSFS_Io *io)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- return __PHYSFS_platformTell(info->handle);
- } /* nativeIo_tell */
- static PHYSFS_sint64 nativeIo_length(PHYSFS_Io *io)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- return __PHYSFS_platformFileLength(info->handle);
- } /* nativeIo_length */
- static PHYSFS_Io *nativeIo_duplicate(PHYSFS_Io *io)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- return __PHYSFS_createNativeIo(info->path, info->mode);
- } /* nativeIo_duplicate */
- static int nativeIo_flush(PHYSFS_Io *io)
- {
- return __PHYSFS_platformFlush(io->opaque);
- } /* nativeIo_flush */
- static void nativeIo_destroy(PHYSFS_Io *io)
- {
- NativeIoInfo *info = (NativeIoInfo *) io->opaque;
- __PHYSFS_platformClose(info->handle);
- allocator.Free((void *) info->path);
- allocator.Free(info);
- allocator.Free(io);
- } /* nativeIo_destroy */
- static const PHYSFS_Io __PHYSFS_nativeIoInterface =
- {
- nativeIo_read,
- nativeIo_write,
- nativeIo_seek,
- nativeIo_tell,
- nativeIo_length,
- nativeIo_duplicate,
- nativeIo_flush,
- nativeIo_destroy,
- NULL
- };
- PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode)
- {
- PHYSFS_Io *io = NULL;
- NativeIoInfo *info = NULL;
- void *handle = NULL;
- char *pathdup = NULL;
- assert((mode == 'r') || (mode == 'w') || (mode == 'a'));
- io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
- GOTO_IF_MACRO(io == NULL, ERR_OUT_OF_MEMORY, createNativeIo_failed);
- info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
- GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, createNativeIo_failed);
- pathdup = (char *) allocator.Malloc(strlen(path) + 1);
- GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, createNativeIo_failed);
- if (mode == 'r')
- handle = __PHYSFS_platformOpenRead(path);
- else if (mode == 'w')
- handle = __PHYSFS_platformOpenWrite(path);
- else if (mode == 'a')
- handle = __PHYSFS_platformOpenAppend(path);
- GOTO_IF_MACRO(handle == NULL, NULL, createNativeIo_failed);
- strcpy(pathdup, path);
- info->handle = handle;
- info->path = pathdup;
- info->mode = mode;
- memcpy(io, &__PHYSFS_nativeIoInterface, sizeof (*io));
- io->opaque = info;
- return io;
- createNativeIo_failed:
- if (handle != NULL) __PHYSFS_platformClose(handle);
- if (pathdup != NULL) allocator.Free(pathdup);
- if (info != NULL) allocator.Free(info);
- if (io != NULL) allocator.Free(io);
- return NULL;
- } /* __PHYSFS_createNativeIo */
- /* PHYSFS_Io implementation for i/o to a memory buffer... */
- typedef struct __PHYSFS_MemoryIoInfo
- {
- const PHYSFS_uint8 *buf;
- PHYSFS_uint64 len;
- PHYSFS_uint64 pos;
- PHYSFS_Io *parent;
- volatile PHYSFS_uint32 refcount;
- void (*destruct)(void *);
- } MemoryIoInfo;
- static PHYSFS_sint64 memoryIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
- {
- MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- const PHYSFS_uint64 avail = info->len - info->pos;
- assert(avail <= info->len);
- if (avail == 0)
- return 0; /* we're at EOF; nothing to do. */
- if (len > avail)
- len = avail;
- memcpy(buf, info->buf + info->pos, len);
- info->pos += len;
- return len;
- } /* memoryIo_read */
- static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
- PHYSFS_uint64 len)
- {
- BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
- } /* memoryIo_write */
- static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
- {
- MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- BAIL_IF_MACRO(offset > info->len, ERR_PAST_EOF, 0);
- info->pos = offset;
- return 1;
- } /* memoryIo_seek */
- static PHYSFS_sint64 memoryIo_tell(PHYSFS_Io *io)
- {
- const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- return (PHYSFS_sint64) info->pos;
- } /* memoryIo_tell */
- static PHYSFS_sint64 memoryIo_length(PHYSFS_Io *io)
- {
- const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- return (PHYSFS_sint64) info->len;
- } /* memoryIo_length */
- static PHYSFS_Io *memoryIo_duplicate(PHYSFS_Io *io)
- {
- MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- MemoryIoInfo *newinfo = NULL;
- PHYSFS_Io *parent = info->parent;
- PHYSFS_Io *retval = NULL;
- /* avoid deep copies. */
- assert((!parent) || (!((MemoryIoInfo *) parent->opaque)->parent) );
- /* share the buffer between duplicates. */
- if (parent != NULL) /* dup the parent, increment its refcount. */
- return parent->duplicate(parent);
- /* we're the parent. */
- retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
- BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
- newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
- if (!newinfo)
- {
- allocator.Free(retval);
- BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
- } /* if */
- /* !!! FIXME: want lockless atomic increment. */
- __PHYSFS_platformGrabMutex(stateLock);
- info->refcount++;
- __PHYSFS_platformReleaseMutex(stateLock);
- memset(newinfo, '\0', sizeof (*info));
- newinfo->buf = info->buf;
- newinfo->len = info->len;
- newinfo->pos = 0;
- newinfo->parent = io;
- newinfo->refcount = 0;
- newinfo->destruct = NULL;
- memcpy(retval, io, sizeof (*retval));
- retval->opaque = newinfo;
- return retval;
- } /* memoryIo_duplicate */
- static int memoryIo_flush(PHYSFS_Io *io) { return 1; /* it's read-only. */ }
- static void memoryIo_destroy(PHYSFS_Io *io)
- {
- MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- PHYSFS_Io *parent = info->parent;
- int should_die = 0;
- if (parent != NULL)
- {
- assert(info->buf == ((MemoryIoInfo *) info->parent->opaque)->buf);
- assert(info->len == ((MemoryIoInfo *) info->parent->opaque)->len);
- assert(info->refcount == 0);
- assert(info->destruct == NULL);
- allocator.Free(info);
- allocator.Free(io);
- parent->destroy(parent); /* decrements refcount. */
- return;
- } /* if */
- /* we _are_ the parent. */
- assert(info->refcount > 0); /* even in a race, we hold a reference. */
- /* !!! FIXME: want lockless atomic decrement. */
- __PHYSFS_platformGrabMutex(stateLock);
- info->refcount--;
- should_die = (info->refcount == 0);
- __PHYSFS_platformReleaseMutex(stateLock);
- if (should_die)
- {
- void (*destruct)(void *) = info->destruct;
- void *buf = (void *) info->buf;
- io->opaque = NULL; /* kill this here in case of race. */
- destruct = info->destruct;
- allocator.Free(info);
- allocator.Free(io);
- if (destruct != NULL)
- destruct(buf);
- } /* if */
- } /* memoryIo_destroy */
- static const PHYSFS_Io __PHYSFS_memoryIoInterface =
- {
- memoryIo_read,
- memoryIo_write,
- memoryIo_seek,
- memoryIo_tell,
- memoryIo_length,
- memoryIo_duplicate,
- memoryIo_flush,
- memoryIo_destroy,
- NULL
- };
- PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
- void (*destruct)(void *))
- {
- PHYSFS_Io *io = NULL;
- MemoryIoInfo *info = NULL;
- io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
- GOTO_IF_MACRO(io == NULL, ERR_OUT_OF_MEMORY, createMemoryIo_failed);
- info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
- GOTO_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, createMemoryIo_failed);
- memset(info, '\0', sizeof (*info));
- info->buf = (const PHYSFS_uint8 *) buf;
- info->len = len;
- info->pos = 0;
- info->parent = NULL;
- info->refcount = 1;
- info->destruct = destruct;
- memcpy(io, &__PHYSFS_memoryIoInterface, sizeof (*io));
- io->opaque = info;
- return io;
- createMemoryIo_failed:
- if (info != NULL) allocator.Free(info);
- if (io != NULL) allocator.Free(io);
- return NULL;
- } /* __PHYSFS_createMemoryIo */
- /* functions ... */
- typedef struct
- {
- char **list;
- PHYSFS_uint32 size;
- const char *errorstr;
- } EnumStringListCallbackData;
- static void enumStringListCallback(void *data, const char *str)
- {
- void *ptr;
- char *newstr;
- EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
- if (pecd->errorstr)
- return;
- ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
- newstr = (char *) allocator.Malloc(strlen(str) + 1);
- if (ptr != NULL)
- pecd->list = (char **) ptr;
- if ((ptr == NULL) || (newstr == NULL))
- {
- pecd->errorstr = ERR_OUT_OF_MEMORY;
- pecd->list[pecd->size] = NULL;
- PHYSFS_freeList(pecd->list);
- return;
- } /* if */
- strcpy(newstr, str);
- pecd->list[pecd->size] = newstr;
- pecd->size++;
- } /* enumStringListCallback */
- static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
- {
- EnumStringListCallbackData ecd;
- memset(&ecd, '\0', sizeof (ecd));
- ecd.list = (char **) allocator.Malloc(sizeof (char *));
- BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
- func(enumStringListCallback, &ecd);
- BAIL_IF_MACRO(ecd.errorstr != NULL, ecd.errorstr, NULL);
- ecd.list[ecd.size] = NULL;
- return ecd.list;
- } /* doEnumStringList */
- static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
- int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
- void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
- {
- PHYSFS_uint32 i;
- int sorted;
- do
- {
- sorted = 1;
- for (i = lo; i < hi; i++)
- {
- if (cmpfn(a, i, i + 1) > 0)
- {
- swapfn(a, i, i + 1);
- sorted = 0;
- } /* if */
- } /* for */
- } while (!sorted);
- } /* __PHYSFS_bubble_sort */
- static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
- int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
- void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
- {
- PHYSFS_uint32 i;
- PHYSFS_uint32 j;
- PHYSFS_uint32 v;
- if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
- __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
- else
- {
- i = (hi + lo) / 2;
- if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
- if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
- if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
- j = hi - 1;
- swapfn(a, i, j);
- i = lo;
- v = j;
- while (1)
- {
- while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
- while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
- if (j < i)
- break;
- swapfn(a, i, j);
- } /* while */
- if (i != (hi-1))
- swapfn(a, i, hi-1);
- __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
- __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
- } /* else */
- } /* __PHYSFS_quick_sort */
- void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
- int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
- void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
- {
- /*
- * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
- * http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
- */
- __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
- } /* __PHYSFS_sort */
- static ErrMsg *findErrorForCurrentThread(void)
- {
- ErrMsg *i;
- void *tid;
- if (errorLock != NULL)
- __PHYSFS_platformGrabMutex(errorLock);
- if (errorMessages != NULL)
- {
- tid = __PHYSFS_platformGetThreadID();
- for (i = errorMessages; i != NULL; i = i->next)
- {
- if (i->tid == tid)
- {
- if (errorLock != NULL)
- __PHYSFS_platformReleaseMutex(errorLock);
- return i;
- } /* if */
- } /* for */
- } /* if */
- if (errorLock != NULL)
- __PHYSFS_platformReleaseMutex(errorLock);
- return NULL; /* no error available. */
- } /* findErrorForCurrentThread */
- void __PHYSFS_setError(const char *str)
- {
- ErrMsg *err;
- if (str == NULL)
- return;
- err = findErrorForCurrentThread();
- if (err == NULL)
- {
- err = (ErrMsg *) allocator.Malloc(sizeof (ErrMsg));
- if (err == NULL)
- return; /* uhh...? */
- memset((void *) err, '\0', sizeof (ErrMsg));
- err->tid = __PHYSFS_platformGetThreadID();
- if (errorLock != NULL)
- __PHYSFS_platformGrabMutex(errorLock);
- err->next = errorMessages;
- errorMessages = err;
- if (errorLock != NULL)
- __PHYSFS_platformReleaseMutex(errorLock);
- } /* if */
- err->errorAvailable = 1;
- strncpy(err->errorString, str, sizeof (err->errorString));
- err->errorString[sizeof (err->errorString) - 1] = '\0';
- } /* __PHYSFS_setError */
- const char *PHYSFS_getLastError(void)
- {
- ErrMsg *err = findErrorForCurrentThread();
- if ((err == NULL) || (!err->errorAvailable))
- return NULL;
- err->errorAvailable = 0;
- return err->errorString;
- } /* PHYSFS_getLastError */
- /* MAKE SURE that errorLock is held before calling this! */
- static void freeErrorMessages(void)
- {
- ErrMsg *i;
- ErrMsg *next;
- for (i = errorMessages; i != NULL; i = next)
- {
- next = i->next;
- allocator.Free(i);
- } /* for */
- errorMessages = NULL;
- } /* freeErrorMessages */
- void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
- {
- if (ver != NULL)
- {
- ver->major = PHYSFS_VER_MAJOR;
- ver->minor = PHYSFS_VER_MINOR;
- ver->patch = PHYSFS_VER_PATCH;
- } /* if */
- } /* PHYSFS_getLinkedVersion */
- static const char *find_filename_extension(const char *fname)
- {
- const char *retval = NULL;
- if (fname != NULL)
- {
- retval = strchr(fname, '.');
- const char *p = retval;
- while (p != NULL)
- {
- p = strchr(p + 1, '.');
- if (p != NULL)
- retval = p;
- } /* while */
- if (retval != NULL)
- retval++; /* skip '.' */
- } /* if */
- return retval;
- } /* find_filename_extension */
- static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
- const char *d, int forWriting)
- {
- DirHandle *retval = NULL;
- void *opaque = NULL;
- if (io != NULL)
- BAIL_IF_MACRO(!io->seek(io, 0), NULL, NULL);
- opaque = funcs->openArchive(io, d, forWriting);
- if (opaque != NULL)
- {
- retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
- if (retval == NULL)
- funcs->dirClose(opaque);
- else
- {
- memset(retval, '\0', sizeof (DirHandle));
- retval->mountPoint = NULL;
- retval->funcs = funcs;
- retval->opaque = opaque;
- } /* else */
- } /* if */
- return retval;
- } /* tryOpenDir */
- static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
- {
- DirHandle *retval = NULL;
- const PHYSFS_Archiver **i;
- const char *ext;
- assert((io != NULL) || (d != NULL));
- if (io == NULL)
- {
- BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
- /* DIR gets first shot (unlike the rest, it doesn't deal with files). */
- retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting);
- if (retval != NULL)
- return retval;
- io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
- BAIL_IF_MACRO_MUTEX(io == NULL, NULL, stateLock, 0);
- } /* if */
- ext = find_filename_extension(d);
- if (ext != NULL)
- {
- /* Look for archivers with matching file extensions first... */
- for (i = archivers; (*i != NULL) && (retval == NULL); i++)
- {
- if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) == 0)
- retval = tryOpenDir(io, *i, d, forWriting);
- } /* for */
- /* failing an exact file extension match, try all the others... */
- for (i = archivers; (*i != NULL) && (retval == NULL); i++)
- {
- if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) != 0)
- retval = tryOpenDir(io, *i, d, forWriting);
- } /* for */
- } /* if */
- else /* no extension? Try them all. */
- {
- for (i = archivers; (*i != NULL) && (retval == NULL); i++)
- retval = tryOpenDir(io, *i, d, forWriting);
- } /* else */
- BAIL_IF_MACRO(retval == NULL, ERR_UNSUPPORTED_ARCHIVE, NULL);
- return retval;
- } /* openDirectory */
- /*
- * Make a platform-independent path string sane. Doesn't actually check the
- * file hierarchy, it just cleans up the string.
- * (dst) must be a buffer at least as big as (src), as this is where the
- * cleaned up string is deposited.
- * If there are illegal bits in the path (".." entries, etc) then we
- * return zero and (dst) is undefined. Non-zero if the path was sanitized.
- */
- static int sanitizePlatformIndependentPath(const char *src, char *dst)
- {
- char *prev;
- char ch;
- while (*src == '/') /* skip initial '/' chars... */
- src++;
- prev = dst;
- do
- {
- ch = *(src++);
- if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
- BAIL_MACRO(ERR_INSECURE_FNAME, 0);
- if (ch == '/') /* path separator. */
- {
- *dst = '\0'; /* "." and ".." are illegal pathnames. */
- if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
- BAIL_MACRO(ERR_INSECURE_FNAME, 0);
- while (*src == '/') /* chop out doubles... */
- src++;
- if (*src == '\0') /* ends with a pathsep? */
- break; /* we're done, don't add final pathsep to dst. */
- prev = dst + 1;
- } /* if */
- *(dst++) = ch;
- } while (ch != '\0');
- return 1;
- } /* sanitizePlatformIndependentPath */
- /*
- * Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
- * output from sanitizePlatformIndependentPath(), so that it is in a known
- * state.
- *
- * This only finds legitimate segments of a mountpoint. If the mountpoint is
- * "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
- * all zero. "/a/b" will succeed, though.
- */
- static int partOfMountPoint(DirHandle *h, char *fname)
- {
- /* !!! FIXME: This code feels gross. */
- int rc;
- size_t len, mntpntlen;
- if (h->mountPoint == NULL)
- return 0;
- else if (*fname == '\0')
- return 1;
- len = strlen(fname);
- mntpntlen = strlen(h->mountPoint);
- if (len > mntpntlen) /* can't be a subset of mountpoint. */
- return 0;
- /* if true, must be not a match or a complete match, but not a subset. */
- if ((len + 1) == mntpntlen)
- return 0;
- rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
- if (rc != 0)
- return 0; /* not a match. */
- /* make sure /a/b matches /a/b/ and not /a/bc ... */
- return h->mountPoint[len] == '/';
- } /* partOfMountPoint */
- static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
- const char *mountPoint, int forWriting)
- {
- DirHandle *dirHandle = NULL;
- char *tmpmntpnt = NULL;
- if (mountPoint != NULL)
- {
- const size_t len = strlen(mountPoint) + 1;
- tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
- GOTO_IF_MACRO(!tmpmntpnt, ERR_OUT_OF_MEMORY, badDirHandle);
- if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
- goto badDirHandle;
- mountPoint = tmpmntpnt; /* sanitized version. */
- } /* if */
- dirHandle = openDirectory(io, newDir, forWriting);
- GOTO_IF_MACRO(!dirHandle, NULL, badDirHandle);
- if (newDir == NULL)
- dirHandle->dirName = NULL;
- else
- {
- dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
- GOTO_IF_MACRO(!dirHandle->dirName, ERR_OUT_OF_MEMORY, badDirHandle);
- strcpy(dirHandle->dirName, newDir);
- } /* else */
- if ((mountPoint != NULL) && (*mountPoint != '\0'))
- {
- dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
- GOTO_IF_MACRO(!dirHandle->mountPoint, ERR_OUT_OF_MEMORY, badDirHandle);
- strcpy(dirHandle->mountPoint, mountPoint);
- strcat(dirHandle->mountPoint, "/");
- } /* if */
- __PHYSFS_smallFree(tmpmntpnt);
- return dirHandle;
- badDirHandle:
- if (dirHandle != NULL)
- {
- dirHandle->funcs->dirClose(dirHandle->opaque);
- allocator.Free(dirHandle->dirName);
- allocator.Free(dirHandle->mountPoint);
- allocator.Free(dirHandle);
- } /* if */
- __PHYSFS_smallFree(tmpmntpnt);
- return NULL;
- } /* createDirHandle */
- /* MAKE SURE you've got the stateLock held before calling this! */
- static int freeDirHandle(DirHandle *dh, FileHandle *openList)
- {
- FileHandle *i;
- if (dh == NULL)
- return 1;
- for (i = openList; i != NULL; i = i->next)
- BAIL_IF_MACRO(i->dirHandle == dh, ERR_FILES_STILL_OPEN, 0);
- dh->funcs->dirClose(dh->opaque);
- allocator.Free(dh->dirName);
- allocator.Free(dh->mountPoint);
- allocator.Free(dh);
- return 1;
- } /* freeDirHandle */
- static char *calculateUserDir(void)
- {
- char *retval = __PHYSFS_platformGetUserDir();
- if (retval != NULL)
- {
- /* make sure it really exists and is normalized. */
- char *ptr = __PHYSFS_platformRealPath(retval);
- allocator.Free(retval);
- retval = ptr;
- } /* if */
- if (retval == NULL)
- {
- const char *dirsep = PHYSFS_getDirSeparator();
- const char *uname = __PHYSFS_platformGetUserName();
- const char *str = (uname != NULL) ? uname : "default";
- retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
- strlen(dirsep) + 6);
- if (retval == NULL)
- __PHYSFS_setError(ERR_OUT_OF_MEMORY);
- else
- sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
- allocator.Free((void *) uname);
- } /* else */
- return retval;
- } /* calculateUserDir */
- static int appendDirSep(char **dir)
- {
- const char *dirsep = PHYSFS_getDirSeparator();
- char *ptr;
- if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
- return 1;
- ptr = (char *) allocator.Realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
- if (!ptr)
- {
- allocator.Free(*dir);
- return 0;
- } /* if */
- strcat(ptr, dirsep);
- *dir = ptr;
- return 1;
- } /* appendDirSep */
- static char *calculateBaseDir(const char *argv0)
- {
- char *retval = NULL;
- const char *dirsep = NULL;
- char *ptr = NULL;
- /* Give the platform layer first shot at this. */
- retval = __PHYSFS_platformCalcBaseDir(argv0);
- if (retval != NULL)
- return retval;
- /* We need argv0 to go on. */
- BAIL_IF_MACRO(argv0 == NULL, ERR_ARGV0_IS_NULL, NULL);
- dirsep = PHYSFS_getDirSeparator();
- if (strlen(dirsep) == 1) /* fast path. */
- ptr = strrchr(argv0, *dirsep);
- else
- {
- ptr = strstr(argv0, dirsep);
- if (ptr != NULL)
- {
- char *p = ptr;
- while (p != NULL)
- {
- ptr = p;
- p = strstr(p + 1, dirsep);
- } /* while */
- } /* if */
- } /* else */
- if (ptr != NULL)
- {
- size_t size = (size_t) (ptr - argv0);
- retval = (char *) allocator.Malloc(size + 1);
- BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
- memcpy(retval, argv0, size);
- retval[size] = '\0';
- return retval;
- } /* if */
- /* argv0 wasn't helpful. */
- BAIL_MACRO(ERR_INVALID_ARGUMENT, NULL);
- } /* calculateBaseDir */
- static int initializeMutexes(void)
- {
- errorLock = __PHYSFS_platformCreateMutex();
- if (errorLock == NULL)
- goto initializeMutexes_failed;
- stateLock = __PHYSFS_platformCreateMutex();
- if (stateLock == NULL)
- goto initializeMutexes_failed;
- return 1; /* success. */
- initializeMutexes_failed:
- if (errorLock != NULL)
- __PHYSFS_platformDestroyMutex(errorLock);
- if (stateLock != NULL)
- __PHYSFS_platformDestroyMutex(stateLock);
- errorLock = stateLock = NULL;
- return 0; /* failed. */
- } /* initializeMutexes */
- static void setDefaultAllocator(void);
- int PHYSFS_init(const char *argv0)
- {
- char *ptr;
- BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
- if (!externalAllocator)
- setDefaultAllocator();
- if (allocator.Init != NULL)
- BAIL_IF_MACRO(!allocator.Init(), NULL, 0);
- BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
- BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
- baseDir = calculateBaseDir(argv0);
- BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
- /* !!! FIXME: only call this if we got this from argv0 (unreliable). */
- ptr = __PHYSFS_platformRealPath(baseDir);
- allocator.Free(baseDir);
- BAIL_IF_MACRO(ptr == NULL, NULL, 0);
- baseDir = ptr;
- BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
- userDir = calculateUserDir();
- if ((userDir == NULL) || (!appendDirSep(&userDir)))
- {
- allocator.Free(baseDir);
- baseDir = NULL;
- return 0;
- } /* if */
- initialized = 1;
- /* This makes sure that the error subsystem is initialized. */
- __PHYSFS_setError(PHYSFS_getLastError());
- return 1;
- } /* PHYSFS_init */
- /* MAKE SURE you hold stateLock before calling this! */
- static int closeFileHandleList(FileHandle **list)
- {
- FileHandle *i;
- FileHandle *next = NULL;
- for (i = *list; i != NULL; i = next)
- {
- PHYSFS_Io *io = i->io;
- next = i->next;
- if (!io->flush(io))
- {
- *list = i;
- return 0;
- } /* if */
- io->destroy(io);
- allocator.Free(i);
- } /* for */
- *list = NULL;
- return 1;
- } /* closeFileHandleList */
- /* MAKE SURE you hold the stateLock before calling this! */
- static void freeSearchPath(void)
- {
- DirHandle *i;
- DirHandle *next = NULL;
- closeFileHandleList(&openReadList);
- if (searchPath != NULL)
- {
- for (i = searchPath; i != NULL; i = next)
- {
- next = i->next;
- freeDirHandle(i, openReadList);
- } /* for */
- searchPath = NULL;
- } /* if */
- } /* freeSearchPath */
- int PHYSFS_deinit(void)
- {
- BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
- BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
- closeFileHandleList(&openWriteList);
- BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
- freeSearchPath();
- freeErrorMessages();
- if (baseDir != NULL)
- {
- allocator.Free(baseDir);
- baseDir = NULL;
- } /* if */
- if (userDir != NULL)
- {
- allocator.Free(userDir);
- userDir = NULL;
- } /* if */
- allowSymLinks = 0;
- initialized = 0;
- __PHYSFS_platformDestroyMutex(errorLock);
- __PHYSFS_platformDestroyMutex(stateLock);
- if (allocator.Deinit != NULL)
- allocator.Deinit();
- errorLock = stateLock = NULL;
- return 1;
- } /* PHYSFS_deinit */
- int PHYSFS_isInit(void)
- {
- return initialized;
- } /* PHYSFS_isInit */
- const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
- {
- return supported_types;
- } /* PHYSFS_supportedArchiveTypes */
- void PHYSFS_freeList(void *list)
- {
- void **i;
- if (list != NULL)
- {
- for (i = (void **) list; *i != NULL; i++)
- allocator.Free(*i);
- allocator.Free(list);
- } /* if */
- } /* PHYSFS_freeList */
- const char *PHYSFS_getDirSeparator(void)
- {
- return __PHYSFS_platformDirSeparator;
- } /* PHYSFS_getDirSeparator */
- char **PHYSFS_getCdRomDirs(void)
- {
- return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
- } /* PHYSFS_getCdRomDirs */
- void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
- {
- __PHYSFS_platformDetectAvailableCDs(callback, data);
- } /* PHYSFS_getCdRomDirsCallback */
- const char *PHYSFS_getBaseDir(void)
- {
- return baseDir; /* this is calculated in PHYSFS_init()... */
- } /* PHYSFS_getBaseDir */
- const char *PHYSFS_getUserDir(void)
- {
- return userDir; /* this is calculated in PHYSFS_init()... */
- } /* PHYSFS_getUserDir */
- const char *PHYSFS_getWriteDir(void)
- {
- const char *retval = NULL;
- __PHYSFS_platformGrabMutex(stateLock);
- if (writeDir != NULL)
- retval = writeDir->dirName;
- __PHYSFS_platformReleaseMutex(stateLock);
- return retval;
- } /* PHYSFS_getWriteDir */
- int PHYSFS_setWriteDir(const char *newDir)
- {
- int retval = 1;
- __PHYSFS_platformGrabMutex(stateLock);
- if (writeDir != NULL)
- {
- BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), NULL,
- stateLock, 0);
- writeDir = NULL;
- } /* if */
- if (newDir != NULL)
- {
- /* !!! FIXME: PHYSFS_Io shouldn't be NULL */
- writeDir = createDirHandle(NULL, newDir, NULL, 1);
- retval = (writeDir != NULL);
- } /* if */
- __PHYSFS_platformReleaseMutex(stateLock);
- return retval;
- } /* PHYSFS_setWriteDir */
- static int doMount(PHYSFS_Io *io, const char *fname,
- const char *mountPoint, int appendToPath)
- {
- DirHandle *dh;
- DirHandle *prev = NULL;
- DirHandle *i;
- if (mountPoint == NULL)
- mountPoint = "/";
- __PHYSFS_platformGrabMutex(stateLock);
- if (fname != NULL)
- {
- for (i = searchPath; i != NULL; i = i->next)
- {
- /* already in search path? */
- if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
- BAIL_MACRO_MUTEX(NULL, stateLock, 1);
- prev = i;
- } /* for */
- } /* if */
- dh = createDirHandle(io, fname, mountPoint, 0);
- BAIL_IF_MACRO_MUTEX(dh == NULL, NULL, stateLock, 0);
- if (appendToPath)
- {
- if (prev == NULL)
- searchPath = dh;
- else
- prev->next = dh;
- } /* if */
- else
- {
- dh->next = searchPath;
- searchPath = dh;
- } /* else */
- __PHYSFS_platformReleaseMutex(stateLock);
- return 1;
- } /* doMount */
- int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
- const char *mountPoint, int appendToPath)
- {
- BAIL_IF_MACRO(io == NULL, ERR_INVALID_ARGUMENT, 0);
- return doMount(io, fname, mountPoint, appendToPath);
- } /* PHYSFS_mountIo */
- int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
- const char *fname, const char *mountPoint,
- int appendToPath)
- {
- int retval = 0;
- PHYSFS_Io *io = NULL;
- BAIL_IF_MACRO(buf == NULL, ERR_INVALID_ARGUMENT, 0);
- io = __PHYSFS_createMemoryIo(buf, len, del);
- BAIL_IF_MACRO(io == NULL, NULL, 0);
- retval = doMount(io, fname, mountPoint, appendToPath);
- if (!retval)
- {
- /* docs say not to call (del) in case of failure, so cheat. */
- MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
- info->destruct = NULL;
- io->destroy(io);
- } /* if */
- return retval;
- } /* PHYSFS_mountMemory */
- int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
- {
- BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
- return doMount(NULL, newDir, mountPoint, appendToPath);
- } /* PHYSFS_mount */
- int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
- {
- return PHYSFS_mount(newDir, NULL, appendToPath);
- } /* PHYSFS_addToSearchPath */
- int PHYSFS_removeFromSearchPath(const char *oldDir)
- {
- return PHYSFS_unmount(oldDir);
- } /* PHYSFS_removeFromSearchPath */
- int PHYSFS_unmount(const char *oldDir)
- {
- DirHandle *i;
- DirHandle *prev = NULL;
- DirHandle *next = NULL;
- BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- if (strcmp(i->dirName, oldDir) == 0)
- {
- next = i->next;
- BAIL_IF_MACRO_MUTEX(!freeDirHandle(i, openReadList), NULL,
- stateLock, 0);
- if (prev == NULL)
- searchPath = next;
- else
- prev->next = next;
- BAIL_MACRO_MUTEX(NULL, stateLock, 1);
- } /* if */
- prev = i;
- } /* for */
- BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
- } /* PHYSFS_unmount */
- char **PHYSFS_getSearchPath(void)
- {
- return doEnumStringList(PHYSFS_getSearchPathCallback);
- } /* PHYSFS_getSearchPath */
- const char *PHYSFS_getMountPoint(const char *dir)
- {
- DirHandle *i;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- if (strcmp(i->dirName, dir) == 0)
- {
- const char *retval = ((i->mountPoint) ? i->mountPoint : "/");
- __PHYSFS_platformReleaseMutex(stateLock);
- return retval;
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- BAIL_MACRO(ERR_NOT_IN_SEARCH_PATH, NULL);
- } /* PHYSFS_getMountPoint */
- void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
- {
- DirHandle *i;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- callback(data, i->dirName);
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* PHYSFS_getSearchPathCallback */
- /* Split out to avoid stack allocation in a loop. */
- static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
- int archivesFirst)
- {
- const char *d = PHYSFS_getRealDir(i);
- const size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
- char *str = (char *) __PHYSFS_smallAlloc(allocsize);
- if (str != NULL)
- {
- sprintf(str, "%s%s%s", d, dirsep, i);
- PHYSFS_mount(str, NULL, archivesFirst == 0);
- __PHYSFS_smallFree(str);
- } /* if */
- } /* setSaneCfgAddPath */
- int PHYSFS_setSaneConfig(const char *organization, const char *appName,
- const char *archiveExt, int includeCdRoms,
- int archivesFirst)
- {
- const char *basedir = PHYSFS_getBaseDir();
- const char *userdir = PHYSFS_getUserDir();
- const char *dirsep = PHYSFS_getDirSeparator();
- PHYSFS_uint64 len = 0;
- char *str = NULL;
- BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
- /* set write dir... */
- len = (strlen(userdir) + (strlen(organization) * 2) +
- (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
- str = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
- sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
- if (!PHYSFS_setWriteDir(str))
- {
- int no_write = 0;
- sprintf(str, ".%s/%s", organization, appName);
- if ( (PHYSFS_setWriteDir(userdir)) &&
- (PHYSFS_mkdir(str)) )
- {
- sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
- if (!PHYSFS_setWriteDir(str))
- no_write = 1;
- } /* if */
- else
- {
- no_write = 1;
- } /* else */
- if (no_write)
- {
- PHYSFS_setWriteDir(NULL); /* just in case. */
- __PHYSFS_smallFree(str);
- BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
- } /* if */
- } /* if */
- /* Put write dir first in search path... */
- PHYSFS_mount(str, NULL, 0);
- __PHYSFS_smallFree(str);
- /* Put base path on search path... */
- PHYSFS_mount(basedir, NULL, 1);
- /* handle CD-ROMs... */
- if (includeCdRoms)
- {
- char **cds = PHYSFS_getCdRomDirs();
- char **i;
- for (i = cds; *i != NULL; i++)
- PHYSFS_mount(*i, NULL, 1);
- PHYSFS_freeList(cds);
- } /* if */
- /* Root out archives, and add them to search path... */
- if (archiveExt != NULL)
- {
- char **rc = PHYSFS_enumerateFiles("/");
- char **i;
- size_t extlen = strlen(archiveExt);
- char *ext;
- for (i = rc; *i != NULL; i++)
- {
- size_t l = strlen(*i);
- if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
- {
- ext = (*i) + (l - extlen);
- if (__PHYSFS_stricmpASCII(ext, archiveExt) == 0)
- setSaneCfgAddPath(*i, l, dirsep, archivesFirst);
- } /* if */
- } /* for */
- PHYSFS_freeList(rc);
- } /* if */
- return 1;
- } /* PHYSFS_setSaneConfig */
- void PHYSFS_permitSymbolicLinks(int allow)
- {
- allowSymLinks = allow;
- } /* PHYSFS_permitSymbolicLinks */
- int PHYSFS_symbolicLinksPermitted(void)
- {
- return allowSymLinks;
- } /* PHYSFS_symbolicLinksPermitted */
- /* string manipulation in C makes my ass itch. */
- char *__PHYSFS_convertToDependent(const char *prepend,
- const char *dirName,
- const char *append)
- {
- const char *dirsep = __PHYSFS_platformDirSeparator;
- size_t sepsize = strlen(dirsep);
- char *str;
- char *i1;
- char *i2;
- size_t allocSize;
- while (*dirName == '/') /* !!! FIXME: pass through sanitize function. */
- dirName++;
- allocSize = strlen(dirName) + 1;
- if (prepend != NULL)
- allocSize += strlen(prepend) + sepsize;
- if (append != NULL)
- allocSize += strlen(append) + sepsize;
- /* make sure there's enough space if the dir separator is bigger. */
- if (sepsize > 1)
- {
- str = (char *) dirName;
- do
- {
- str = strchr(str, '/');
- if (str != NULL)
- {
- allocSize += (sepsize - 1);
- str++;
- } /* if */
- } while (str != NULL);
- } /* if */
- str = (char *) allocator.Malloc(allocSize);
- BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
- if (prepend == NULL)
- *str = '\0';
- else
- {
- strcpy(str, prepend);
- strcat(str, dirsep);
- } /* else */
- for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
- {
- if (*i1 == '/')
- {
- strcpy(i2, dirsep);
- i2 += sepsize;
- } /* if */
- else
- {
- *i2 = *i1;
- } /* else */
- } /* for */
- *i2 = '\0';
- if (append)
- {
- strcat(str, dirsep);
- strcat(str, append);
- } /* if */
- return str;
- } /* __PHYSFS_convertToDependent */
- /*
- * Verify that (fname) (in platform-independent notation), in relation
- * to (h) is secure. That means that each element of fname is checked
- * for symlinks (if they aren't permitted). This also allows for quick
- * rejection of files that exist outside an archive's mountpoint.
- *
- * With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
- * at a time), you should always pass zero for "allowMissing" for efficiency.
- *
- * (fname) must point to an output from sanitizePlatformIndependentPath(),
- * since it will make sure that path names are in the right format for
- * passing certain checks. It will also do checks for "insecure" pathnames
- * like ".." which should be done once instead of once per archive. This also
- * gives us license to treat (fname) as scratch space in this function.
- *
- * Returns non-zero if string is safe, zero if there's a security issue.
- * PHYSFS_getLastError() will specify what was wrong. (*fname) will be
- * updated to point past any mount point elements so it is prepared to
- * be used with the archiver directly.
- */
- static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
- {
- char *fname = *_fname;
- int retval = 1;
- char *start;
- char *end;
- if (*fname == '\0') /* quick rejection. */
- return 1;
- /* !!! FIXME: This codeblock sucks. */
- if (h->mountPoint != NULL) /* NULL mountpoint means "/". */
- {
- size_t mntpntlen = strlen(h->mountPoint);
- size_t len = strlen(fname);
- assert(mntpntlen > 1); /* root mount points should be NULL. */
- /* not under the mountpoint, so skip this archive. */
- BAIL_IF_MACRO(len < mntpntlen-1, ERR_NO_SUCH_PATH, 0);
- /* !!! FIXME: Case insensitive? */
- retval = strncmp(h->mountPoint, fname, mntpntlen-1);
- BAIL_IF_MACRO(retval != 0, ERR_NO_SUCH_PATH, 0);
- if (len > mntpntlen-1) /* corner case... */
- BAIL_IF_MACRO(fname[mntpntlen-1] != '/', ERR_NO_SUCH_PATH, 0);
- fname += mntpntlen-1; /* move to start of actual archive path. */
- if (*fname == '/')
- fname++;
- *_fname = fname; /* skip mountpoint for later use. */
- retval = 1; /* may be reset, below. */
- } /* if */
- start = fname;
- if (!allowSymLinks)
- {
- while (1)
- {
- int rc = 0;
- end = strchr(start, '/');
- if (end != NULL) *end = '\0';
- rc = h->funcs->isSymLink(h->opaque, fname, &retval);
- if (end != NULL) *end = '/';
- BAIL_IF_MACRO(rc, ERR_SYMLINK_DISALLOWED, 0); /* insecure. */
- /* break out early if path element is missing. */
- if (!retval)
- {
- /*
- * We need to clear it if it's the last element of the path,
- * since this might be a non-existant file we're opening
- * for writing...
- */
- if ((end == NULL) || (allowMissing))
- retval = 1;
- break;
- } /* if */
- if (end == NULL)
- break;
- start = end + 1;
- } /* while */
- } /* if */
- return retval;
- } /* verifyPath */
- static int doMkdir(const char *_dname, char *dname)
- {
- DirHandle *h;
- char *start;
- char *end;
- int retval = 0;
- int exists = 1; /* force existance check on first path element. */
- BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), NULL, 0);
- __PHYSFS_platformGrabMutex(stateLock);
- BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
- h = writeDir;
- BAIL_IF_MACRO_MUTEX(!verifyPath(h, &dname, 1), NULL, stateLock, 0);
- start = dname;
- while (1)
- {
- end = strchr(start, '/');
- if (end != NULL)
- *end = '\0';
- /* only check for existance if all parent dirs existed, too... */
- if (exists)
- retval = h->funcs->isDirectory(h->opaque, dname, &exists);
- if (!exists)
- retval = h->funcs->mkdir(h->opaque, dname);
- if (!retval)
- break;
- if (end == NULL)
- break;
- *end = '/';
- start = end + 1;
- } /* while */
- __PHYSFS_platformReleaseMutex(stateLock);
- return retval;
- } /* doMkdir */
- int PHYSFS_mkdir(const char *_dname)
- {
- int retval = 0;
- char *dname;
- size_t len;
- BAIL_IF_MACRO(_dname == NULL, ERR_INVALID_ARGUMENT, 0);
- len = strlen(_dname) + 1;
- dname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(dname == NULL, ERR_OUT_OF_MEMORY, 0);
- retval = doMkdir(_dname, dname);
- __PHYSFS_smallFree(dname);
- return retval;
- } /* PHYSFS_mkdir */
- static int doDelete(const char *_fname, char *fname)
- {
- int retval;
- DirHandle *h;
- BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
- __PHYSFS_platformGrabMutex(stateLock);
- BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
- h = writeDir;
- BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), NULL, stateLock, 0);
- retval = h->funcs->remove(h->opaque, fname);
- __PHYSFS_platformReleaseMutex(stateLock);
- return retval;
- } /* doDelete */
- int PHYSFS_delete(const char *_fname)
- {
- int retval;
- char *fname;
- size_t len;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, 0);
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, 0);
- retval = doDelete(_fname, fname);
- __PHYSFS_smallFree(fname);
- return retval;
- } /* PHYSFS_delete */
- const char *PHYSFS_getRealDir(const char *_fname)
- {
- const char *retval = NULL;
- char *fname = NULL;
- size_t len;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, NULL);
- len = strlen(_fname) + 1;
- fname = __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, NULL);
- if (sanitizePlatformIndependentPath(_fname, fname))
- {
- DirHandle *i;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
- {
- char *arcfname = fname;
- if (partOfMountPoint(i, arcfname))
- retval = i->dirName;
- else if (verifyPath(i, &arcfname, 0))
- {
- if (i->funcs->exists(i->opaque, arcfname))
- retval = i->dirName;
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* if */
- __PHYSFS_smallFree(fname);
- return retval;
- } /* PHYSFS_getRealDir */
- static int locateInStringList(const char *str,
- char **list,
- PHYSFS_uint32 *pos)
- {
- PHYSFS_uint32 len = *pos;
- PHYSFS_uint32 half_len;
- PHYSFS_uint32 lo = 0;
- PHYSFS_uint32 middle;
- int cmp;
- while (len > 0)
- {
- half_len = len >> 1;
- middle = lo + half_len;
- cmp = strcmp(list[middle], str);
- if (cmp == 0) /* it's in the list already. */
- return 1;
- else if (cmp > 0)
- len = half_len;
- else
- {
- lo = middle + 1;
- len -= half_len + 1;
- } /* else */
- } /* while */
- *pos = lo;
- return 0;
- } /* locateInStringList */
- static void enumFilesCallback(void *data, const char *origdir, const char *str)
- {
- PHYSFS_uint32 pos;
- void *ptr;
- char *newstr;
- EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
- /*
- * See if file is in the list already, and if not, insert it in there
- * alphabetically...
- */
- pos = pecd->size;
- if (locateInStringList(str, pecd->list, &pos))
- return; /* already in the list. */
- ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
- newstr = (char *) allocator.Malloc(strlen(str) + 1);
- if (ptr != NULL)
- pecd->list = (char **) ptr;
- if ((ptr == NULL) || (newstr == NULL))
- return; /* better luck next time. */
- strcpy(newstr, str);
- if (pos != pecd->size)
- {
- memmove(&pecd->list[pos+1], &pecd->list[pos],
- sizeof (char *) * ((pecd->size) - pos));
- } /* if */
- pecd->list[pos] = newstr;
- pecd->size++;
- } /* enumFilesCallback */
- char **PHYSFS_enumerateFiles(const char *path)
- {
- EnumStringListCallbackData ecd;
- memset(&ecd, '\0', sizeof (ecd));
- ecd.list = (char **) allocator.Malloc(sizeof (char *));
- BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
- PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
- ecd.list[ecd.size] = NULL;
- return ecd.list;
- } /* PHYSFS_enumerateFiles */
- /*
- * Broke out to seperate function so we can use stack allocation gratuitously.
- */
- static void enumerateFromMountPoint(DirHandle *i, const char *arcfname,
- PHYSFS_EnumFilesCallback callback,
- const char *_fname, void *data)
- {
- const size_t len = strlen(arcfname);
- char *ptr = NULL;
- char *end = NULL;
- const size_t slen = strlen(i->mountPoint) + 1;
- char *mountPoint = (char *) __PHYSFS_smallAlloc(slen);
- if (mountPoint == NULL)
- return; /* oh well. */
- strcpy(mountPoint, i->mountPoint);
- ptr = mountPoint + ((len) ? len + 1 : 0);
- end = strchr(ptr, '/');
- assert(end); /* should always find a terminating '/'. */
- *end = '\0';
- callback(data, _fname, ptr);
- __PHYSFS_smallFree(mountPoint);
- } /* enumerateFromMountPoint */
- /* !!! FIXME: this should report error conditions. */
- void PHYSFS_enumerateFilesCallback(const char *_fname,
- PHYSFS_EnumFilesCallback callback,
- void *data)
- {
- size_t len;
- char *fname;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, ) /*0*/;
- BAIL_IF_MACRO(callback == NULL, ERR_INVALID_ARGUMENT, ) /*0*/;
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, ) /*0*/;
- if (sanitizePlatformIndependentPath(_fname, fname))
- {
- DirHandle *i;
- int noSyms;
- __PHYSFS_platformGrabMutex(stateLock);
- noSyms = !allowSymLinks;
- for (i = searchPath; i != NULL; i = i->next)
- {
- char *arcfname = fname;
- if (partOfMountPoint(i, arcfname))
- enumerateFromMountPoint(i, arcfname, callback, _fname, data);
- else if (verifyPath(i, &arcfname, 0))
- {
- i->funcs->enumerateFiles(i->opaque, arcfname, noSyms,
- callback, _fname, data);
- } /* else if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* if */
- __PHYSFS_smallFree(fname);
- } /* PHYSFS_enumerateFilesCallback */
- int PHYSFS_exists(const char *fname)
- {
- return (PHYSFS_getRealDir(fname) != NULL);
- } /* PHYSFS_exists */
- PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
- {
- PHYSFS_Stat statbuf;
- BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), NULL, -1);
- return statbuf.modtime;
- } /* PHYSFS_getLastModTime */
- int PHYSFS_isDirectory(const char *_fname)
- {
- int retval = 0;
- size_t len;
- char *fname;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, 0);
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, 0);
- if (!sanitizePlatformIndependentPath(_fname, fname))
- retval = 0;
- else if (*fname == '\0')
- retval = 1; /* Root is always a dir. :) */
- else
- {
- DirHandle *i;
- int exists = 0;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
- {
- char *arcfname = fname;
- if ((exists = partOfMountPoint(i, arcfname)) != 0)
- retval = 1;
- else if (verifyPath(i, &arcfname, 0))
- retval = i->funcs->isDirectory(i->opaque, arcfname, &exists);
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* else */
- __PHYSFS_smallFree(fname);
- return retval;
- } /* PHYSFS_isDirectory */
- int PHYSFS_isSymbolicLink(const char *_fname)
- {
- int retval = 0;
- size_t len;
- char *fname;
- BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, 0);
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, 0);
- if (!sanitizePlatformIndependentPath(_fname, fname))
- retval = 0;
- else if (*fname == '\0')
- retval = 1; /* Root is never a symlink. */
- else
- {
- DirHandle *i;
- int fileExists = 0;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
- {
- char *arcfname = fname;
- if ((fileExists = partOfMountPoint(i, arcfname)) != 0)
- retval = 0; /* virtual dir...not a symlink. */
- else if (verifyPath(i, &arcfname, 0))
- retval = i->funcs->isSymLink(i->opaque, arcfname, &fileExists);
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* else */
- __PHYSFS_smallFree(fname);
- return retval;
- } /* PHYSFS_isSymbolicLink */
- static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
- {
- FileHandle *fh = NULL;
- size_t len;
- char *fname;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, 0);
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, 0);
- if (sanitizePlatformIndependentPath(_fname, fname))
- {
- PHYSFS_Io *io = NULL;
- DirHandle *h = NULL;
- const PHYSFS_Archiver *f;
- __PHYSFS_platformGrabMutex(stateLock);
- GOTO_IF_MACRO(!writeDir, ERR_NO_WRITE_DIR, doOpenWriteEnd);
- h = writeDir;
- GOTO_IF_MACRO(!verifyPath(h, &fname, 0), NULL, doOpenWriteEnd);
- f = h->funcs;
- if (appending)
- io = f->openAppend(h->opaque, fname);
- else
- io = f->openWrite(h->opaque, fname);
- GOTO_IF_MACRO(io == NULL, NULL, doOpenWriteEnd);
- fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
- if (fh == NULL)
- {
- io->destroy(io);
- GOTO_MACRO(ERR_OUT_OF_MEMORY, doOpenWriteEnd);
- } /* if */
- else
- {
- memset(fh, '\0', sizeof (FileHandle));
- fh->io = io;
- fh->dirHandle = h;
- fh->next = openWriteList;
- openWriteList = fh;
- } /* else */
- doOpenWriteEnd:
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* if */
- __PHYSFS_smallFree(fname);
- return ((PHYSFS_File *) fh);
- } /* doOpenWrite */
- PHYSFS_File *PHYSFS_openWrite(const char *filename)
- {
- return doOpenWrite(filename, 0);
- } /* PHYSFS_openWrite */
- PHYSFS_File *PHYSFS_openAppend(const char *filename)
- {
- return doOpenWrite(filename, 1);
- } /* PHYSFS_openAppend */
- PHYSFS_File *PHYSFS_openRead(const char *_fname)
- {
- FileHandle *fh = NULL;
- char *fname;
- size_t len;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, 0);
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, 0);
- if (sanitizePlatformIndependentPath(_fname, fname))
- {
- int fileExists = 0;
- DirHandle *i = NULL;
- PHYSFS_Io *io = NULL;
- __PHYSFS_platformGrabMutex(stateLock);
- GOTO_IF_MACRO(!searchPath, ERR_NO_SUCH_PATH, openReadEnd);
- /* !!! FIXME: Why aren't we using a for loop here? */
- i = searchPath;
- do
- {
- char *arcfname = fname;
- if (verifyPath(i, &arcfname, 0))
- {
- io = i->funcs->openRead(i->opaque, arcfname, &fileExists);
- if (io)
- break;
- } /* if */
- i = i->next;
- } while ((i != NULL) && (!fileExists));
- /* !!! FIXME: may not set an error if openRead didn't fail. */
- GOTO_IF_MACRO(io == NULL, NULL, openReadEnd);
- fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
- if (fh == NULL)
- {
- io->destroy(io);
- GOTO_MACRO(ERR_OUT_OF_MEMORY, openReadEnd);
- } /* if */
- memset(fh, '\0', sizeof (FileHandle));
- fh->io = io;
- fh->forReading = 1;
- fh->dirHandle = i;
- fh->next = openReadList;
- openReadList = fh;
- openReadEnd:
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* if */
- __PHYSFS_smallFree(fname);
- return ((PHYSFS_File *) fh);
- } /* PHYSFS_openRead */
- static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
- {
- FileHandle *prev = NULL;
- FileHandle *i;
- int rc = 1;
- for (i = *list; i != NULL; i = i->next)
- {
- if (i == handle) /* handle is in this list? */
- {
- PHYSFS_Io *io = handle->io;
- PHYSFS_uint8 *tmp = handle->buffer;
- rc = PHYSFS_flush((PHYSFS_File *) handle);
- if (!rc)
- return -1;
- io->destroy(io);
- if (tmp != NULL) /* free any associated buffer. */
- allocator.Free(tmp);
- if (prev == NULL)
- *list = handle->next;
- else
- prev->next = handle->next;
- allocator.Free(handle);
- return 1;
- } /* if */
- prev = i;
- } /* for */
- return 0;
- } /* closeHandleInOpenList */
- int PHYSFS_close(PHYSFS_File *_handle)
- {
- FileHandle *handle = (FileHandle *) _handle;
- int rc;
- __PHYSFS_platformGrabMutex(stateLock);
- /* -1 == close failure. 0 == not found. 1 == success. */
- rc = closeHandleInOpenList(&openReadList, handle);
- BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
- if (!rc)
- {
- rc = closeHandleInOpenList(&openWriteList, handle);
- BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
- } /* if */
- __PHYSFS_platformReleaseMutex(stateLock);
- BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
- return 1;
- } /* PHYSFS_close */
- static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
- PHYSFS_uint64 len)
- {
- PHYSFS_Io *io = NULL;
- PHYSFS_sint64 retval = 0;
- PHYSFS_uint32 buffered = 0;
- PHYSFS_sint64 rc = 0;
- if (len == 0)
- return 0;
- buffered = fh->buffill - fh->bufpos;
- if (buffered >= len) /* totally in the buffer, just copy and return! */
- {
- memcpy(buffer, fh->buffer + fh->bufpos, (size_t) len);
- fh->bufpos += (PHYSFS_uint32) len;
- return (PHYSFS_sint64) len;
- } /* else if */
- if (buffered > 0) /* partially in the buffer... */
- {
- memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
- buffer = ((PHYSFS_uint8 *) buffer) + buffered;
- len -= buffered;
- retval = buffered;
- buffered = fh->buffill = fh->bufpos = 0;
- } /* if */
- /* if you got here, the buffer is drained and we still need bytes. */
- assert(buffered == 0);
- assert(len > 0);
- io = fh->io;
- if (len >= fh->bufsize) /* need more than the buffer takes. */
- {
- /* leave buffer empty, go right to output instead. */
- rc = io->read(io, buffer, len);
- if (rc < 0)
- return ((retval == 0) ? rc : retval);
- return retval + rc;
- } /* if */
- /* need less than buffer can take. Fill buffer. */
- rc = io->read(io, fh->buffer, fh->bufsize);
- if (rc < 0)
- return ((retval == 0) ? rc : retval);
- assert(fh->bufpos == 0);
- fh->buffill = (PHYSFS_uint32) rc;
- rc = doBufferedRead(fh, buffer, len); /* go from the start, again. */
- if (rc < 0)
- return ((retval == 0) ? rc : retval);
- return retval + rc;
- } /* doBufferedRead */
- PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
- PHYSFS_uint32 size, PHYSFS_uint32 count)
- {
- const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
- const PHYSFS_sint64 retval = PHYSFS_readBytes(handle, buffer, len);
- return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) count)) );
- } /* PHYSFS_read */
- PHYSFS_sint64 PHYSFS_readBytes(PHYSFS_File *handle, void *buffer,
- PHYSFS_uint64 len)
- {
- FileHandle *fh = (FileHandle *) handle;
- #ifdef PHYSFS_NO_64BIT_SUPPORT
- const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
- #else
- const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
- #endif
- BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
- BAIL_IF_MACRO(len > maxlen, ERR_INVALID_ARGUMENT, -1);
- BAIL_IF_MACRO(!fh->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
- BAIL_IF_MACRO(len == 0, NULL, 0);
- if (fh->buffer != NULL)
- return doBufferedRead(fh, buffer, len);
- return fh->io->read(fh->io, buffer, len);
- } /* PHYSFS_readBytes */
- static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
- PHYSFS_uint64 len)
- {
- FileHandle *fh = (FileHandle *) handle;
- /* whole thing fits in the buffer? */
- if ( (((PHYSFS_uint64) fh->buffill) + len) < fh->bufsize )
- {
- memcpy(fh->buffer + fh->buffill, buffer, (size_t) len);
- fh->buffill += (PHYSFS_uint32) len;
- return (PHYSFS_sint64) len;
- } /* if */
- /* would overflow buffer. Flush and then write the new objects, too. */
- BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
- return fh->io->write(fh->io, buffer, len);
- } /* doBufferedWrite */
- PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
- PHYSFS_uint32 size, PHYSFS_uint32 count)
- {
- const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
- const PHYSFS_sint64 retval = PHYSFS_writeBytes(handle, buffer, len);
- return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) count)) );
- } /* PHYSFS_write */
- PHYSFS_sint64 PHYSFS_writeBytes(PHYSFS_File *handle, const void *buffer,
- PHYSFS_uint64 len)
- {
- FileHandle *fh = (FileHandle *) handle;
- #ifdef PHYSFS_NO_64BIT_SUPPORT
- const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
- #else
- const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
- #endif
- BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
- BAIL_IF_MACRO(len > maxlen, ERR_INVALID_ARGUMENT, -1);
- BAIL_IF_MACRO(fh->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
- BAIL_IF_MACRO(len == 0, NULL, 0);
- if (fh->buffer != NULL)
- return doBufferedWrite(handle, buffer, len);
- return fh->io->write(fh->io, buffer, len);
- } /* PHYSFS_write */
- int PHYSFS_eof(PHYSFS_File *handle)
- {
- FileHandle *fh = (FileHandle *) handle;
- if (!fh->forReading) /* never EOF on files opened for write/append. */
- return 0;
- /* can't be eof if buffer isn't empty */
- if (fh->bufpos == fh->buffill)
- {
- /* check the Io. */
- PHYSFS_Io *io = fh->io;
- const PHYSFS_sint64 pos = io->tell(io);
- const PHYSFS_sint64 len = io->length(io);
- if ((pos < 0) || (len < 0))
- return 0; /* beats me. */
- return (pos >= len);
- } /* if */
- return 0;
- } /* PHYSFS_eof */
- PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
- {
- FileHandle *fh = (FileHandle *) handle;
- const PHYSFS_sint64 pos = fh->io->tell(fh->io);
- const PHYSFS_sint64 retval = fh->forReading ?
- (pos - fh->buffill) + fh->bufpos :
- (pos + fh->buffill);
- return retval;
- } /* PHYSFS_tell */
- int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
- {
- FileHandle *fh = (FileHandle *) handle;
- BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
- if (fh->buffer && fh->forReading)
- {
- /* avoid throwing away our precious buffer if seeking within it. */
- PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
- if ( /* seeking within the already-buffered range? */
- ((offset >= 0) && (offset <= fh->buffill - fh->bufpos)) /* fwd */
- || ((offset < 0) && (-offset <= fh->bufpos)) /* backward */ )
- {
- fh->bufpos += (PHYSFS_uint32) offset;
- return 1; /* successful seek */
- } /* if */
- } /* if */
- /* we have to fall back to a 'raw' seek. */
- fh->buffill = fh->bufpos = 0;
- return fh->io->seek(fh->io, pos);
- } /* PHYSFS_seek */
- PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
- {
- PHYSFS_Io *io = ((FileHandle *) handle)->io;
- return io->length(io);
- } /* PHYSFS_filelength */
- int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
- {
- FileHandle *fh = (FileHandle *) handle;
- PHYSFS_uint32 bufsize;
- /* !!! FIXME: Unlocalized string. */
- /* !!! FIXME: actually, why use 32 bits here? */
- BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
- bufsize = (PHYSFS_uint32) _bufsize;
- BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
- /*
- * For reads, we need to move the file pointer to where it would be
- * if we weren't buffering, so that the next read will get the
- * right chunk of stuff from the file. PHYSFS_flush() handles writes.
- */
- if ((fh->forReading) && (fh->buffill != fh->bufpos))
- {
- PHYSFS_uint64 pos;
- const PHYSFS_sint64 curpos = fh->io->tell(fh->io);
- BAIL_IF_MACRO(curpos == -1, NULL, 0);
- pos = ((curpos - fh->buffill) + fh->bufpos);
- BAIL_IF_MACRO(!fh->io->seek(fh->io, pos), NULL, 0);
- } /* if */
- if (bufsize == 0) /* delete existing buffer. */
- {
- if (fh->buffer != NULL)
- {
- allocator.Free(fh->buffer);
- fh->buffer = NULL;
- } /* if */
- } /* if */
- else
- {
- PHYSFS_uint8 *newbuf;
- newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
- BAIL_IF_MACRO(newbuf == NULL, ERR_OUT_OF_MEMORY, 0);
- fh->buffer = newbuf;
- } /* else */
- fh->bufsize = bufsize;
- fh->buffill = fh->bufpos = 0;
- return 1;
- } /* PHYSFS_setBuffer */
- int PHYSFS_flush(PHYSFS_File *handle)
- {
- FileHandle *fh = (FileHandle *) handle;
- PHYSFS_Io *io;
- PHYSFS_sint64 rc;
- if ((fh->forReading) || (fh->bufpos == fh->buffill))
- return 1; /* open for read or buffer empty are successful no-ops. */
- /* dump buffer to disk. */
- io = fh->io;
- rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
- BAIL_IF_MACRO(rc <= 0, NULL, 0);
- fh->bufpos = fh->buffill = 0;
- return io->flush(io);
- } /* PHYSFS_flush */
- int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
- {
- int retval = 0;
- char *fname;
- size_t len;
- BAIL_IF_MACRO(_fname == NULL, ERR_INVALID_ARGUMENT, -1);
- BAIL_IF_MACRO(stat == NULL, ERR_INVALID_ARGUMENT, -1);
- len = strlen(_fname) + 1;
- fname = (char *) __PHYSFS_smallAlloc(len);
- BAIL_IF_MACRO(fname == NULL, ERR_OUT_OF_MEMORY, -1);
- /* set some sane defaults... */
- stat->filesize = -1;
- stat->modtime = -1;
- stat->createtime = -1;
- stat->accesstime = -1;
- stat->filetype = PHYSFS_FILETYPE_OTHER;
- stat->readonly = 1; /* !!! FIXME */
- if (sanitizePlatformIndependentPath(_fname, fname))
- {
- if (*fname == '\0')
- {
- stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
- stat->readonly = !writeDir; /* Writeable if we have a writeDir */
- retval = 1;
- } /* if */
- else
- {
- DirHandle *i;
- int exists = 0;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
- {
- char *arcfname = fname;
- exists = partOfMountPoint(i, arcfname);
- if (exists)
- {
- stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
- stat->readonly = 1; /* !!! FIXME */
- retval = 1;
- } /* if */
- else if (verifyPath(i, &arcfname, 0))
- {
- /* !!! FIXME: this test is wrong and should be elsewhere. */
- stat->readonly = !(writeDir &&
- (strcmp(writeDir->dirName, i->dirName) == 0));
- retval = i->funcs->stat(i->opaque, arcfname, &exists, stat);
- } /* else if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- } /* else */
- } /* if */
- __PHYSFS_smallFree(fname);
- return retval;
- } /* PHYSFS_stat */
- int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
- {
- BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
- externalAllocator = (a != NULL);
- if (externalAllocator)
- memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
- return 1;
- } /* PHYSFS_setAllocator */
- const PHYSFS_Allocator *PHYSFS_getAllocator(void)
- {
- BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, NULL);
- return &allocator;
- } /* PHYSFS_getAllocator */
- static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
- {
- BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
- #undef malloc
- return malloc((size_t) s);
- } /* mallocAllocatorMalloc */
- static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
- {
- BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
- #undef realloc
- return realloc(ptr, (size_t) s);
- } /* mallocAllocatorRealloc */
- static void mallocAllocatorFree(void *ptr)
- {
- #undef free
- free(ptr);
- } /* mallocAllocatorFree */
- static void setDefaultAllocator(void)
- {
- assert(!externalAllocator);
- if (!__PHYSFS_platformSetDefaultAllocator(&allocator))
- {
- allocator.Init = NULL;
- allocator.Deinit = NULL;
- allocator.Malloc = mallocAllocatorMalloc;
- allocator.Realloc = mallocAllocatorRealloc;
- allocator.Free = mallocAllocatorFree;
- } /* if */
- } /* setDefaultAllocator */
- void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len)
- {
- const char useHeap = ((ptr == NULL) ? 1 : 0);
- if (useHeap) /* too large for stack allocation or alloca() failed. */
- ptr = allocator.Malloc(len+1);
- if (ptr != NULL)
- {
- char *retval = (char *) ptr;
- /*printf("%s alloc'd (%d) bytes at (%p).\n",
- useHeap ? "heap" : "stack", (int) len, ptr);*/
- *retval = useHeap;
- return (retval + 1);
- } /* if */
- return NULL; /* allocation failed. */
- } /* __PHYSFS_initSmallAlloc */
- void __PHYSFS_smallFree(void *ptr)
- {
- if (ptr != NULL)
- {
- char *block = ((char *) ptr) - 1;
- const char useHeap = *block;
- if (useHeap)
- allocator.Free(block);
- /*printf("%s free'd (%p).\n", useHeap ? "heap" : "stack", block);*/
- } /* if */
- } /* __PHYSFS_smallFree */
- /* end of physfs.c ... */
|