qpak.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * QPAK support routines for PhysicsFS.
  3. *
  4. * This archiver handles the archive format utilized by Quake 1 and 2.
  5. * Quake3-based games use the PkZip/Info-Zip format (which our zip.c
  6. * archiver handles).
  7. *
  8. * ========================================================================
  9. *
  10. * This format info (in more detail) comes from:
  11. * http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
  12. *
  13. * Quake PAK Format
  14. *
  15. * Header
  16. * (4 bytes) signature = 'PACK'
  17. * (4 bytes) directory offset
  18. * (4 bytes) directory length
  19. *
  20. * Directory
  21. * (56 bytes) file name
  22. * (4 bytes) file position
  23. * (4 bytes) file length
  24. *
  25. * ========================================================================
  26. *
  27. * Please see the file LICENSE in the source's root directory.
  28. *
  29. * This file written by Ryan C. Gordon.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #if (defined PHYSFS_SUPPORTS_QPAK)
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "physfs.h"
  39. #define __PHYSICSFS_INTERNAL__
  40. #include "physfs_internal.h"
  41. #if 1 /* Make this case insensitive? */
  42. #define QPAK_strcmp(x, y) __PHYSFS_platformStricmp(x, y)
  43. #define QPAK_strncmp(x, y, z) __PHYSFS_platformStrnicmp(x, y, z)
  44. #else
  45. #define QPAK_strcmp(x, y) strcmp(x, y)
  46. #define QPAK_strncmp(x, y, z) strncmp(x, y, z)
  47. #endif
  48. typedef struct
  49. {
  50. char name[56];
  51. PHYSFS_uint32 startPos;
  52. PHYSFS_uint32 size;
  53. } QPAKentry;
  54. typedef struct
  55. {
  56. char *filename;
  57. PHYSFS_sint64 last_mod_time;
  58. PHYSFS_uint32 entryCount;
  59. QPAKentry *entries;
  60. } QPAKinfo;
  61. typedef struct
  62. {
  63. void *handle;
  64. QPAKentry *entry;
  65. PHYSFS_uint32 curPos;
  66. } QPAKfileinfo;
  67. /* Magic numbers... */
  68. #define QPAK_SIGNATURE 0x4b434150 /* "PACK" in ASCII. */
  69. static void QPAK_dirClose(DirHandle *h);
  70. static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
  71. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  72. static PHYSFS_sint64 QPAK_write(FileHandle *handle, const void *buffer,
  73. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  74. static int QPAK_eof(FileHandle *handle);
  75. static PHYSFS_sint64 QPAK_tell(FileHandle *handle);
  76. static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset);
  77. static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle);
  78. static int QPAK_fileClose(FileHandle *handle);
  79. static int QPAK_isArchive(const char *filename, int forWriting);
  80. static DirHandle *QPAK_openArchive(const char *name, int forWriting);
  81. static LinkedStringList *QPAK_enumerateFiles(DirHandle *h,
  82. const char *dirname,
  83. int omitSymLinks);
  84. static int QPAK_exists(DirHandle *h, const char *name);
  85. static int QPAK_isDirectory(DirHandle *h, const char *name, int *fileExists);
  86. static int QPAK_isSymLink(DirHandle *h, const char *name, int *fileExists);
  87. static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *n, int *e);
  88. static FileHandle *QPAK_openRead(DirHandle *h, const char *name, int *exist);
  89. static FileHandle *QPAK_openWrite(DirHandle *h, const char *name);
  90. static FileHandle *QPAK_openAppend(DirHandle *h, const char *name);
  91. static int QPAK_remove(DirHandle *h, const char *name);
  92. static int QPAK_mkdir(DirHandle *h, const char *name);
  93. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
  94. {
  95. "PAK",
  96. QPAK_ARCHIVE_DESCRIPTION,
  97. "Ryan C. Gordon <icculus@icculus.org>",
  98. "http://icculus.org/physfs/",
  99. };
  100. static const FileFunctions __PHYSFS_FileFunctions_QPAK =
  101. {
  102. QPAK_read, /* read() method */
  103. QPAK_write, /* write() method */
  104. QPAK_eof, /* eof() method */
  105. QPAK_tell, /* tell() method */
  106. QPAK_seek, /* seek() method */
  107. QPAK_fileLength, /* fileLength() method */
  108. QPAK_fileClose /* fileClose() method */
  109. };
  110. const DirFunctions __PHYSFS_DirFunctions_QPAK =
  111. {
  112. &__PHYSFS_ArchiveInfo_QPAK,
  113. QPAK_isArchive, /* isArchive() method */
  114. QPAK_openArchive, /* openArchive() method */
  115. QPAK_enumerateFiles, /* enumerateFiles() method */
  116. QPAK_exists, /* exists() method */
  117. QPAK_isDirectory, /* isDirectory() method */
  118. QPAK_isSymLink, /* isSymLink() method */
  119. QPAK_getLastModTime, /* getLastModTime() method */
  120. QPAK_openRead, /* openRead() method */
  121. QPAK_openWrite, /* openWrite() method */
  122. QPAK_openAppend, /* openAppend() method */
  123. QPAK_remove, /* remove() method */
  124. QPAK_mkdir, /* mkdir() method */
  125. QPAK_dirClose /* dirClose() method */
  126. };
  127. static void QPAK_dirClose(DirHandle *h)
  128. {
  129. QPAKinfo *info = ((QPAKinfo *) h->opaque);
  130. free(info->filename);
  131. free(info->entries);
  132. free(info);
  133. free(h);
  134. } /* QPAK_dirClose */
  135. static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
  136. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  137. {
  138. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  139. QPAKentry *entry = finfo->entry;
  140. PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
  141. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  142. PHYSFS_sint64 rc;
  143. if (objsLeft < objCount)
  144. objCount = objsLeft;
  145. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  146. if (rc > 0)
  147. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  148. return(rc);
  149. } /* QPAK_read */
  150. static PHYSFS_sint64 QPAK_write(FileHandle *handle, const void *buffer,
  151. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  152. {
  153. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  154. } /* QPAK_write */
  155. static int QPAK_eof(FileHandle *handle)
  156. {
  157. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  158. QPAKentry *entry = finfo->entry;
  159. return(finfo->curPos >= entry->size);
  160. } /* QPAK_eof */
  161. static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
  162. {
  163. return(((QPAKfileinfo *) (handle->opaque))->curPos);
  164. } /* QPAK_tell */
  165. static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
  166. {
  167. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  168. QPAKentry *entry = finfo->entry;
  169. int rc;
  170. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  171. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  172. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  173. if (rc)
  174. finfo->curPos = (PHYSFS_uint32) offset;
  175. return(rc);
  176. } /* QPAK_seek */
  177. static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
  178. {
  179. QPAKfileinfo *finfo = ((QPAKfileinfo *) handle->opaque);
  180. return((PHYSFS_sint64) finfo->entry->size);
  181. } /* QPAK_fileLength */
  182. static int QPAK_fileClose(FileHandle *handle)
  183. {
  184. QPAKfileinfo *finfo = ((QPAKfileinfo *) handle->opaque);
  185. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  186. free(finfo);
  187. free(handle);
  188. return(1);
  189. } /* QPAK_fileClose */
  190. static int qpak_open(const char *filename, int forWriting,
  191. void **fh, PHYSFS_uint32 *count)
  192. {
  193. PHYSFS_uint32 buf;
  194. *fh = NULL;
  195. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  196. *fh = __PHYSFS_platformOpenRead(filename);
  197. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  198. if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
  199. goto openQpak_failed;
  200. buf = PHYSFS_swapULE32(buf);
  201. if (buf != QPAK_SIGNATURE)
  202. {
  203. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  204. goto openQpak_failed;
  205. } /* if */
  206. if (__PHYSFS_platformRead(*fh, &buf, sizeof (PHYSFS_uint32), 1) != 1)
  207. goto openQpak_failed;
  208. buf = PHYSFS_swapULE32(buf); /* directory table offset. */
  209. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  210. goto openQpak_failed;
  211. *count = PHYSFS_swapULE32(*count);
  212. if ((*count % 64) != 0) /* corrupted archive? */
  213. {
  214. __PHYSFS_setError(ERR_CORRUPTED);
  215. goto openQpak_failed;
  216. } /* if */
  217. if (!__PHYSFS_platformSeek(*fh, buf))
  218. goto openQpak_failed;
  219. *count /= 64;
  220. return(1);
  221. openQpak_failed:
  222. if (*fh != NULL)
  223. __PHYSFS_platformClose(*fh);
  224. *count = -1;
  225. *fh = NULL;
  226. return(0);
  227. } /* qpak_open */
  228. static int QPAK_isArchive(const char *filename, int forWriting)
  229. {
  230. void *fh;
  231. PHYSFS_uint32 fileCount;
  232. int retval = qpak_open(filename, forWriting, &fh, &fileCount);
  233. if (fh != NULL)
  234. __PHYSFS_platformClose(fh);
  235. return(retval);
  236. } /* QPAK_isArchive */
  237. static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  238. {
  239. if (one != two)
  240. {
  241. const QPAKentry *a = (const QPAKentry *) _a;
  242. return(QPAK_strcmp(a[one].name, a[two].name));
  243. } /* if */
  244. return 0;
  245. } /* qpak_entry_cmp */
  246. static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  247. {
  248. if (one != two)
  249. {
  250. QPAKentry tmp;
  251. QPAKentry *first = &(((QPAKentry *) _a)[one]);
  252. QPAKentry *second = &(((QPAKentry *) _a)[two]);
  253. memcpy(&tmp, first, sizeof (QPAKentry));
  254. memcpy(first, second, sizeof (QPAKentry));
  255. memcpy(second, &tmp, sizeof (QPAKentry));
  256. } /* if */
  257. } /* qpak_entry_swap */
  258. static int qpak_load_entries(const char *name, int forWriting, QPAKinfo *info)
  259. {
  260. void *fh = NULL;
  261. PHYSFS_uint32 fileCount;
  262. QPAKentry *entry;
  263. BAIL_IF_MACRO(!qpak_open(name, forWriting, &fh, &fileCount), NULL, 0);
  264. info->entryCount = fileCount;
  265. info->entries = (QPAKentry *) malloc(sizeof (QPAKentry) * fileCount);
  266. if (info->entries == NULL)
  267. {
  268. __PHYSFS_platformClose(fh);
  269. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  270. } /* if */
  271. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  272. {
  273. PHYSFS_uint32 loc;
  274. if (__PHYSFS_platformRead(fh,&entry->name,sizeof(entry->name),1) != 1)
  275. {
  276. __PHYSFS_platformClose(fh);
  277. return(0);
  278. } /* if */
  279. if (__PHYSFS_platformRead(fh,&loc,sizeof(loc),1) != 1)
  280. {
  281. __PHYSFS_platformClose(fh);
  282. return(0);
  283. } /* if */
  284. if (__PHYSFS_platformRead(fh,&entry->size,sizeof(entry->size),1) != 1)
  285. {
  286. __PHYSFS_platformClose(fh);
  287. return(0);
  288. } /* if */
  289. entry->size = PHYSFS_swapULE32(entry->size);
  290. entry->startPos = PHYSFS_swapULE32(loc);
  291. } /* for */
  292. __PHYSFS_platformClose(fh);
  293. __PHYSFS_sort(info->entries, info->entryCount,
  294. qpak_entry_cmp, qpak_entry_swap);
  295. return(1);
  296. } /* qpak_load_entries */
  297. static DirHandle *QPAK_openArchive(const char *name, int forWriting)
  298. {
  299. QPAKinfo *info;
  300. DirHandle *retval = malloc(sizeof (DirHandle));
  301. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  302. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  303. info = retval->opaque = malloc(sizeof (QPAKinfo));
  304. if (info == NULL)
  305. {
  306. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  307. goto QPAK_openArchive_failed;
  308. } /* if */
  309. memset(info, '\0', sizeof (QPAKinfo));
  310. info->filename = (char *) malloc(strlen(name) + 1);
  311. if (info->filename == NULL)
  312. {
  313. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  314. goto QPAK_openArchive_failed;
  315. } /* if */
  316. if (!qpak_load_entries(name, forWriting, info))
  317. goto QPAK_openArchive_failed;
  318. strcpy(info->filename, name);
  319. info->last_mod_time = modtime;
  320. retval->funcs = &__PHYSFS_DirFunctions_QPAK;
  321. return(retval);
  322. QPAK_openArchive_failed:
  323. if (retval != NULL)
  324. {
  325. if (retval->opaque != NULL)
  326. {
  327. if (info->filename != NULL)
  328. free(info->filename);
  329. if (info->entries != NULL)
  330. free(info->entries);
  331. free(info);
  332. } /* if */
  333. free(retval);
  334. } /* if */
  335. return(NULL);
  336. } /* QPAK_openArchive */
  337. static PHYSFS_sint32 qpak_find_start_of_dir(QPAKinfo *info, const char *path,
  338. int stop_on_first_find)
  339. {
  340. PHYSFS_sint32 lo = 0;
  341. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  342. PHYSFS_sint32 middle;
  343. PHYSFS_uint32 dlen = strlen(path);
  344. PHYSFS_sint32 retval = -1;
  345. const char *name;
  346. int rc;
  347. if (*path == '\0') /* root dir? */
  348. return(0);
  349. if ((dlen > 0) && (path[dlen - 1] == '/')) /* ignore trailing slash. */
  350. dlen--;
  351. while (lo <= hi)
  352. {
  353. middle = lo + ((hi - lo) / 2);
  354. name = info->entries[middle].name;
  355. rc = QPAK_strncmp(path, name, dlen);
  356. if (rc == 0)
  357. {
  358. char ch = name[dlen];
  359. if (ch < '/') /* make sure this isn't just a substr match. */
  360. rc = -1;
  361. else if (ch > '/')
  362. rc = 1;
  363. else
  364. {
  365. if (stop_on_first_find) /* Just checking dir's existance? */
  366. return(middle);
  367. if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
  368. return(middle + 1);
  369. /* there might be more entries earlier in the list. */
  370. retval = middle;
  371. hi = middle - 1;
  372. } /* else */
  373. } /* if */
  374. if (rc > 0)
  375. lo = middle + 1;
  376. else
  377. hi = middle - 1;
  378. } /* while */
  379. return(retval);
  380. } /* qpak_find_start_of_dir */
  381. static LinkedStringList *QPAK_enumerateFiles(DirHandle *h,
  382. const char *dirname,
  383. int omitSymLinks)
  384. {
  385. QPAKinfo *info = ((QPAKinfo *) h->opaque);
  386. LinkedStringList *retval = NULL, *p = NULL;
  387. PHYSFS_sint32 dlen, dlen_inc, max, i;
  388. i = qpak_find_start_of_dir(info, dirname, 0);
  389. BAIL_IF_MACRO(i == -1, ERR_NO_SUCH_FILE, NULL);
  390. dlen = strlen(dirname);
  391. if ((dlen > 0) && (dirname[dlen - 1] == '/')) /* ignore trailing slash. */
  392. dlen--;
  393. dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
  394. max = (PHYSFS_sint32) info->entryCount;
  395. while (i < max)
  396. {
  397. char *add;
  398. char *ptr;
  399. PHYSFS_sint32 ln;
  400. char *e = info->entries[i].name;
  401. if ((dlen) && ((QPAK_strncmp(e, dirname, dlen)) || (e[dlen] != '/')))
  402. break; /* past end of this dir; we're done. */
  403. add = e + dlen_inc;
  404. ptr = strchr(add, '/');
  405. ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
  406. retval = __PHYSFS_addToLinkedStringList(retval, &p, add, ln);
  407. ln += dlen_inc; /* point past entry to children... */
  408. /* increment counter and skip children of subdirs... */
  409. while ((++i < max) && (ptr != NULL))
  410. {
  411. char *e_new = info->entries[i].name;
  412. if ((QPAK_strncmp(e, e_new, ln) != 0) || (e_new[ln] != '/'))
  413. break;
  414. } /* while */
  415. } /* while */
  416. return(retval);
  417. } /* QPAK_enumerateFiles */
  418. /*
  419. * This will find the QPAKentry associated with a path in platform-independent
  420. * notation. Directories don't have QPAKentries associated with them, but
  421. * (*isDir) will be set to non-zero if a dir was hit.
  422. */
  423. static QPAKentry *qpak_find_entry(QPAKinfo *info, const char *path, int *isDir)
  424. {
  425. QPAKentry *a = info->entries;
  426. PHYSFS_sint32 pathlen = strlen(path);
  427. PHYSFS_sint32 lo = 0;
  428. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  429. PHYSFS_sint32 middle;
  430. const char *thispath = NULL;
  431. int rc;
  432. while (lo <= hi)
  433. {
  434. middle = lo + ((hi - lo) / 2);
  435. thispath = a[middle].name;
  436. rc = QPAK_strncmp(path, thispath, pathlen);
  437. if (rc > 0)
  438. lo = middle + 1;
  439. else if (rc < 0)
  440. hi = middle - 1;
  441. else /* substring match...might be dir or entry or nothing. */
  442. {
  443. if (isDir != NULL)
  444. {
  445. *isDir = (thispath[pathlen] == '/');
  446. if (*isDir)
  447. return(NULL);
  448. } /* if */
  449. if (thispath[pathlen] == '\0') /* found entry? */
  450. return(&a[middle]);
  451. else
  452. hi = middle - 1; /* adjust search params, try again. */
  453. } /* if */
  454. } /* while */
  455. if (isDir != NULL)
  456. *isDir = 0;
  457. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  458. } /* qpak_find_entry */
  459. static int QPAK_exists(DirHandle *h, const char *name)
  460. {
  461. int isDir;
  462. QPAKinfo *info = (QPAKinfo *) h->opaque;
  463. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  464. return((entry != NULL) || (isDir));
  465. } /* QPAK_exists */
  466. static int QPAK_isDirectory(DirHandle *h, const char *name, int *fileExists)
  467. {
  468. QPAKinfo *info = (QPAKinfo *) h->opaque;
  469. int isDir;
  470. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  471. *fileExists = ((isDir) || (entry != NULL));
  472. if (isDir)
  473. return(1); /* definitely a dir. */
  474. BAIL_MACRO(ERR_NO_SUCH_FILE, 0);
  475. } /* QPAK_isDirectory */
  476. static int QPAK_isSymLink(DirHandle *h, const char *name, int *fileExists)
  477. {
  478. *fileExists = QPAK_exists(h, name);
  479. return(0); /* never symlinks in a quake pak. */
  480. } /* QPAK_isSymLink */
  481. static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h,
  482. const char *name,
  483. int *fileExists)
  484. {
  485. int isDir;
  486. QPAKinfo *info = ((QPAKinfo *) h->opaque);
  487. PHYSFS_sint64 retval = -1;
  488. QPAKentry *entry = qpak_find_entry(info, name, &isDir);
  489. *fileExists = ((isDir) || (entry != NULL));
  490. if (*fileExists) /* use time of QPAK itself in the physical filesystem. */
  491. retval = info->last_mod_time;
  492. return(retval);
  493. } /* QPAK_getLastModTime */
  494. static FileHandle *QPAK_openRead(DirHandle *h, const char *fnm, int *fileExists)
  495. {
  496. QPAKinfo *info = ((QPAKinfo *) h->opaque);
  497. FileHandle *retval;
  498. QPAKfileinfo *finfo;
  499. QPAKentry *entry;
  500. int isDir;
  501. entry = qpak_find_entry(info, fnm, &isDir);
  502. *fileExists = ((entry != NULL) || (isDir));
  503. BAIL_IF_MACRO(isDir, ERR_NOT_A_FILE, NULL);
  504. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  505. retval = (FileHandle *) malloc(sizeof (FileHandle));
  506. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  507. finfo = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
  508. if (finfo == NULL)
  509. {
  510. free(retval);
  511. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  512. } /* if */
  513. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  514. if ( (finfo->handle == NULL) ||
  515. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  516. {
  517. free(finfo);
  518. free(retval);
  519. return(NULL);
  520. } /* if */
  521. finfo->curPos = 0;
  522. finfo->entry = entry;
  523. retval->opaque = (void *) finfo;
  524. retval->funcs = &__PHYSFS_FileFunctions_QPAK;
  525. retval->dirHandle = h;
  526. return(retval);
  527. } /* QPAK_openRead */
  528. static FileHandle *QPAK_openWrite(DirHandle *h, const char *name)
  529. {
  530. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  531. } /* QPAK_openWrite */
  532. static FileHandle *QPAK_openAppend(DirHandle *h, const char *name)
  533. {
  534. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  535. } /* QPAK_openAppend */
  536. static int QPAK_remove(DirHandle *h, const char *name)
  537. {
  538. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  539. } /* QPAK_remove */
  540. static int QPAK_mkdir(DirHandle *h, const char *name)
  541. {
  542. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  543. } /* QPAK_mkdir */
  544. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  545. /* end of qpak.c ... */