qpak.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Quake PAK support routines for PhysicsFS.
  3. *
  4. * This driver handles id Software Quake PAK files.
  5. *
  6. * Please see the file LICENSE in the source's root directory.
  7. *
  8. * This file written by Ed Sinjiashvili.
  9. */
  10. #if HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. #if (defined PHYSFS_SUPPORTS_QPAK)
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <assert.h>
  20. #include "physfs.h"
  21. #define __PHYSICSFS_INTERNAL__
  22. #include "physfs_internal.h"
  23. #define QPAK_MAXDIRLEN 60
  24. typedef struct
  25. {
  26. char name[56];
  27. PHYSFS_uint32 offset;
  28. PHYSFS_uint32 size;
  29. } QPAKentry;
  30. typedef struct tagQPAKdirentry
  31. {
  32. char *name;
  33. QPAKentry *entry;
  34. struct tagQPAKdirentry *next;
  35. } QPAKdirentry;
  36. typedef struct QPAKDirectory
  37. {
  38. char name[QPAK_MAXDIRLEN];
  39. struct QPAKDirectory *dirs, *next;
  40. QPAKdirentry *files;
  41. } QPAKdirectory;
  42. typedef struct
  43. {
  44. void *handle;
  45. char *filename;
  46. PHYSFS_uint32 dirOffset;
  47. PHYSFS_uint32 totalEntries;
  48. QPAKentry *entries;
  49. QPAKdirectory *root;
  50. } QPAKinfo;
  51. typedef struct
  52. {
  53. void *handle;
  54. QPAKentry *entry;
  55. PHYSFS_sint64 curPos;
  56. } QPAKfileinfo;
  57. static int QPAK_isArchive(const char *filename, int forWriting);
  58. static DirHandle *QPAK_openArchive(const char *name, int forWriting);
  59. static void QPAK_dirClose(DirHandle *h);
  60. static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
  61. int omitSymLinks);
  62. static int QPAK_exists(DirHandle *h, const char *name);
  63. static int QPAK_isDirectory(DirHandle *h, const char *name);
  64. static int QPAK_isSymLink(DirHandle *h, const char *name);
  65. static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *name);
  66. static FileHandle *QPAK_openRead(DirHandle *h, const char *name);
  67. static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
  68. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  69. static int QPAK_eof(FileHandle *handle);
  70. static PHYSFS_sint64 QPAK_tell(FileHandle *handle);
  71. static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset);
  72. static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle);
  73. static int QPAK_fileClose(FileHandle *handle);
  74. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK =
  75. {
  76. "PAK",
  77. "Quake PAK file format",
  78. "Ed Sinjiashvili <slimb@swes.saren.ru>",
  79. "http://icculus.org/physfs/",
  80. };
  81. static const FileFunctions __PHYSFS_FileFunctions_QPAK =
  82. {
  83. QPAK_read, /* read() method */
  84. NULL, /* write() method */
  85. QPAK_eof, /* eof() method */
  86. QPAK_tell, /* tell() method */
  87. QPAK_seek, /* seek() method */
  88. QPAK_fileLength, /* fileLength() method */
  89. QPAK_fileClose /* fileClose() method */
  90. };
  91. const DirFunctions __PHYSFS_DirFunctions_QPAK =
  92. {
  93. &__PHYSFS_ArchiveInfo_QPAK,
  94. QPAK_isArchive, /* isArchive() method */
  95. QPAK_openArchive, /* openArchive() method */
  96. QPAK_enumerateFiles, /* enumerateFiles() method */
  97. QPAK_exists, /* exists() method */
  98. QPAK_isDirectory, /* isDirectory() method */
  99. QPAK_isSymLink, /* isSymLink() method */
  100. QPAK_getLastModTime, /* getLastModTime() method */
  101. QPAK_openRead, /* openRead() method */
  102. NULL, /* openWrite() method */
  103. NULL, /* openAppend() method */
  104. NULL, /* remove() method */
  105. NULL, /* mkdir() method */
  106. QPAK_dirClose /* dirClose() method */
  107. };
  108. #define QPAK_MAGIC 0x4B434150 /* look like "PACK" in ascii. */
  109. /*
  110. * Read an unsigned 32-bit int and swap to native byte order.
  111. */
  112. static int readui32(void *in, PHYSFS_uint32 *val)
  113. {
  114. PHYSFS_uint32 v;
  115. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  116. *val = PHYSFS_swapULE32(v);
  117. return(1);
  118. } /* readui32 */
  119. static int openQPak(const char *filename, int forWriting, void **fileHandle)
  120. {
  121. PHYSFS_uint32 sig;
  122. *fileHandle = NULL;
  123. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  124. *fileHandle = __PHYSFS_platformOpenRead(filename);
  125. BAIL_IF_MACRO(*fileHandle == NULL, NULL, 0);
  126. if (!readui32(*fileHandle, &sig))
  127. goto openPak_failed;
  128. if (sig != QPAK_MAGIC)
  129. {
  130. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  131. goto openPak_failed;
  132. } /* if */
  133. return(1);
  134. openPak_failed:
  135. if (*fileHandle != NULL)
  136. __PHYSFS_platformClose(*fileHandle);
  137. *fileHandle = NULL;
  138. return(0);
  139. } /* openQPak */
  140. static int QPAK_isArchive(const char *filename, int forWriting)
  141. {
  142. void *fileHandle;
  143. int retval = openQPak(filename, forWriting, &fileHandle);
  144. if (fileHandle != NULL)
  145. __PHYSFS_platformClose(fileHandle);
  146. return(retval);
  147. } /* QPAK_isArchive */
  148. static void qpak_insertion_sort(QPAKentry *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi)
  149. {
  150. PHYSFS_uint32 i;
  151. PHYSFS_uint32 j;
  152. QPAKentry tmp;
  153. for (i = lo + 1; i <= hi; i++)
  154. {
  155. memcpy(&tmp, &a[i], sizeof (QPAKentry));
  156. j = i;
  157. while ((j > lo) && (strcmp(a[j - 1].name, tmp.name) > 0))
  158. {
  159. memcpy(&a[j], &a[j - 1], sizeof (QPAKentry));
  160. j--;
  161. } /* while */
  162. memcpy(&a[j], &tmp, sizeof (QPAKentry));
  163. } /* for */
  164. } /* qpak_insertion_sort */
  165. static void qpak_sort_entries(QPAKentry *entries, PHYSFS_uint32 max)
  166. {
  167. qpak_insertion_sort(entries, 0, max - 1);
  168. } /* qpak_sort_entries */
  169. static int qpak_loadEntries(void *fh, int dirOffset, int numEntries,
  170. QPAKentry *entries)
  171. {
  172. PHYSFS_uint32 i;
  173. BAIL_IF_MACRO(__PHYSFS_platformSeek(fh, dirOffset) == 0, NULL, 0);
  174. for (i = 0; i < numEntries; i++, entries++)
  175. {
  176. PHYSFS_sint64 r = __PHYSFS_platformRead(fh, entries->name, 56, 1);
  177. BAIL_IF_MACRO(r == 0, NULL, 0);
  178. BAIL_IF_MACRO(!readui32(fh, &entries->offset), NULL, 0);
  179. BAIL_IF_MACRO(!readui32(fh, &entries->size), NULL, 0);
  180. } /* for */
  181. return(1);
  182. } /* qpak_loadEntries */
  183. static QPAKdirentry *qpak_newDirentry(char *name, QPAKentry *entry)
  184. {
  185. QPAKdirentry *retval = (QPAKdirentry *) malloc(sizeof (QPAKdirentry));
  186. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, 0);
  187. retval->name = name;
  188. retval->entry = entry;
  189. retval->next = NULL;
  190. return(retval);
  191. } /* qpak_newDirentry */
  192. static void qpak_deleteDirentry(QPAKdirentry *e)
  193. {
  194. while (e != NULL)
  195. {
  196. QPAKdirentry *next = e->next;
  197. free(e);
  198. e = next;
  199. } /* while */
  200. } /* qpak_deleteDirentry */
  201. static QPAKdirectory *qpak_newDirectory(char *name)
  202. {
  203. QPAKdirectory *dir = (QPAKdirectory *) malloc(sizeof (QPAKdirectory));
  204. BAIL_IF_MACRO(dir == NULL, ERR_OUT_OF_MEMORY, 0);
  205. strcpy(dir->name, name);
  206. dir->dirs = NULL;
  207. dir->next = NULL;
  208. dir->files = NULL;
  209. return dir;
  210. } /* qpak_newDirectory */
  211. static void qpak_deleteDirectory(QPAKdirectory *d)
  212. {
  213. while (d != NULL)
  214. {
  215. QPAKdirectory *next = d->next;
  216. qpak_deleteDirentry(d->files);
  217. qpak_deleteDirectory(d->dirs);
  218. free(d);
  219. d = next;
  220. } /* while */
  221. } /* qpak_deleteDirectory */
  222. static int qpak_addFile(QPAKdirectory *dir, char *name, QPAKentry *entry)
  223. {
  224. QPAKdirentry *file = qpak_newDirentry(name, entry);
  225. if (file == NULL)
  226. return(0);
  227. /* !!! FIXME: Traversing a linkedlist gets slower with each added file. */
  228. if (dir->files == NULL)
  229. {
  230. dir->files = file;
  231. } /* if */
  232. else
  233. {
  234. QPAKdirentry *tail = dir->files;
  235. while (tail->next != NULL)
  236. tail = tail->next;
  237. tail->next = file;
  238. } /* else */
  239. return(1);
  240. } /* qpak_addFile */
  241. static QPAKdirectory *qpak_findDirectory(QPAKdirectory *root, const char *name)
  242. {
  243. char *p = strchr(name, '/');
  244. if (p == NULL)
  245. {
  246. QPAKdirectory *thisDir = root->dirs;
  247. while (thisDir != NULL)
  248. {
  249. if (strcmp(thisDir->name, name) == 0)
  250. return(thisDir);
  251. thisDir = thisDir->next;
  252. } /* while */
  253. } /* if */
  254. else
  255. {
  256. char temp[QPAK_MAXDIRLEN];
  257. QPAKdirectory *thisDir = root->dirs;
  258. strncpy (temp, name, p - name);
  259. temp[p - name] = '\0';
  260. while (thisDir != NULL)
  261. {
  262. if (strcmp(thisDir->name, temp) == 0)
  263. return(qpak_findDirectory(thisDir, p + 1));
  264. thisDir = thisDir->next;
  265. } /* while */
  266. } /* else */
  267. return(0);
  268. } /* qpak_findDirectory */
  269. static QPAKdirectory *qpak_addDir(QPAKdirectory *dir, char *name)
  270. {
  271. QPAKdirectory *newDir = qpak_findDirectory(dir, name);
  272. if (newDir != 0)
  273. return(newDir);
  274. newDir = qpak_newDirectory(name);
  275. if (newDir == 0)
  276. return 0;
  277. if (dir->dirs == NULL)
  278. {
  279. dir->dirs = newDir;
  280. } /* if */
  281. else
  282. {
  283. QPAKdirectory *tail = dir->dirs;
  284. while (tail->next != NULL)
  285. tail = tail->next;
  286. tail->next = newDir;
  287. } /* else */
  288. return(newDir);
  289. } /* qpak_addDir */
  290. static int qpak_addEntry(QPAKdirectory *dir, char *name, QPAKentry *entry)
  291. {
  292. char tempName[QPAK_MAXDIRLEN];
  293. QPAKdirectory *child;
  294. char *p = strchr(name, '/');
  295. if (p == NULL)
  296. return(qpak_addFile(dir, name, entry));
  297. strncpy(tempName, name, p - name);
  298. tempName[p - name] = '\0';
  299. child = qpak_addDir(dir, tempName);
  300. return(qpak_addEntry(child, p + 1, entry));
  301. } /* qpak_addEntry */
  302. static QPAKentry *qpak_findEntry(QPAKdirectory *root, const char *name)
  303. {
  304. QPAKdirectory *dir = NULL;
  305. QPAKdirentry *thisFile = NULL;
  306. const char *t = strrchr (name, '/');
  307. if (t == NULL)
  308. {
  309. dir = root;
  310. t = name;
  311. } /* if */
  312. else
  313. {
  314. char temp[QPAK_MAXDIRLEN];
  315. strncpy(temp, name, t - name);
  316. temp[t - name] = '\0';
  317. dir = qpak_findDirectory(root, temp);
  318. t++;
  319. } /* else */
  320. if (dir == NULL)
  321. return(0);
  322. thisFile = dir->files;
  323. while (thisFile != NULL)
  324. {
  325. if (strcmp(thisFile->name, t) == 0)
  326. return(thisFile->entry);
  327. thisFile = thisFile->next;
  328. } /* while */
  329. return(0);
  330. } /* qpak_findEntry */
  331. static int qpak_populateDirectories(QPAKentry *entries, int numEntries,
  332. QPAKdirectory *root)
  333. {
  334. PHYSFS_uint32 i;
  335. QPAKentry *entry = entries;
  336. for (i = 0; i < numEntries; i++, entry++)
  337. {
  338. if (qpak_addEntry(root, entry->name, entry) == 0)
  339. return(0);
  340. } /* for */
  341. return(1);
  342. } /* qpak_populateDirectories */
  343. static void qpak_deletePakInfo (QPAKinfo *pakInfo)
  344. {
  345. if (pakInfo->handle != NULL)
  346. __PHYSFS_platformClose(pakInfo->handle);
  347. if (pakInfo->filename != NULL)
  348. free(pakInfo->filename);
  349. if (pakInfo->entries != NULL)
  350. free(pakInfo->entries);
  351. qpak_deleteDirectory(pakInfo->root);
  352. free(pakInfo);
  353. } /* qpak_deletePakInfo */
  354. static DirHandle *QPAK_openArchive(const char *name, int forWriting)
  355. {
  356. void *fh = NULL;
  357. PHYSFS_uint32 dirOffset, dirLength;
  358. QPAKinfo *pi;
  359. DirHandle *retval;
  360. retval = (DirHandle *) malloc(sizeof (DirHandle));
  361. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  362. pi = (QPAKinfo *) malloc(sizeof (QPAKinfo));
  363. if (pi == NULL)
  364. {
  365. free(retval);
  366. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  367. } /* if */
  368. retval->opaque = pi;
  369. pi->filename = (char *) malloc(strlen(name) + 1);
  370. if (pi->filename == NULL)
  371. {
  372. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  373. goto QPAK_openArchive_failed;
  374. } /* if */
  375. if (!openQPak(name, forWriting, &fh))
  376. goto QPAK_openArchive_failed;
  377. if (!readui32(fh, &dirOffset))
  378. goto QPAK_openArchive_failed;
  379. if (!readui32(fh, &dirLength))
  380. goto QPAK_openArchive_failed;
  381. if (__PHYSFS_platformFileLength(fh) < dirOffset + dirLength)
  382. goto QPAK_openArchive_failed;
  383. strcpy(pi->filename, name);
  384. pi->handle = fh;
  385. pi->dirOffset = dirOffset;
  386. pi->totalEntries = dirLength / 64;
  387. pi->entries = (QPAKentry *) malloc(pi->totalEntries * sizeof (QPAKentry));
  388. if (pi->entries == NULL)
  389. {
  390. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  391. goto QPAK_openArchive_failed;
  392. } /* if */
  393. if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
  394. goto QPAK_openArchive_failed;
  395. qpak_sort_entries(pi->entries, pi->totalEntries);
  396. pi->root = qpak_newDirectory("");
  397. if (pi->root == NULL)
  398. goto QPAK_openArchive_failed;
  399. if (qpak_populateDirectories(pi->entries, pi->totalEntries, pi->root) == 0)
  400. goto QPAK_openArchive_failed;
  401. retval->funcs = &__PHYSFS_DirFunctions_QPAK;
  402. return(retval);
  403. QPAK_openArchive_failed:
  404. if (retval != NULL)
  405. {
  406. if (retval->opaque != NULL)
  407. qpak_deletePakInfo((QPAKinfo *) retval->opaque);
  408. free(retval);
  409. } /* if */
  410. if (fh != NULL)
  411. __PHYSFS_platformClose(fh);
  412. return(0);
  413. } /* QPAK_openArchive */
  414. static void QPAK_dirClose(DirHandle *dirHandle)
  415. {
  416. qpak_deletePakInfo((QPAKinfo *) dirHandle->opaque);
  417. free(dirHandle);
  418. } /* QPAK_dirClose */
  419. static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
  420. int omitSymLinks)
  421. {
  422. LinkedStringList *retval = NULL, *p = NULL;
  423. QPAKdirectory *dir;
  424. QPAKinfo *info = (QPAKinfo *) h->opaque;
  425. if ((dirname == NULL) || (*dirname == '\0'))
  426. dir = info->root;
  427. else
  428. dir = qpak_findDirectory(info->root, dirname);
  429. if (dir != NULL)
  430. {
  431. QPAKdirectory *child = dir->dirs;
  432. QPAKdirentry *file = dir->files;
  433. while (child != NULL)
  434. {
  435. retval = __PHYSFS_addToLinkedStringList(retval, &p, child->name, -1);
  436. child = child->next;
  437. } /* while */
  438. while (file != NULL)
  439. {
  440. retval = __PHYSFS_addToLinkedStringList(retval, &p, file->name, -1);
  441. file = file->next;
  442. } /* while */
  443. } /* if */
  444. return(retval);
  445. } /* QPAK_enumerateFiles */
  446. static int QPAK_exists(DirHandle *h, const char *name)
  447. {
  448. QPAKinfo *driver = (QPAKinfo *) h->opaque;
  449. if ((name == NULL) || (*name == '\0'))
  450. return(0);
  451. if (qpak_findDirectory(driver->root, name) != 0)
  452. return(1);
  453. if (qpak_findEntry(driver->root, name) != 0)
  454. return(1);
  455. return(0);
  456. } /* QPAK_exists */
  457. static int QPAK_isDirectory(DirHandle *h, const char *name)
  458. {
  459. QPAKinfo *info = (QPAKinfo *) h->opaque;
  460. return(qpak_findDirectory(info->root, name) != 0);
  461. } /* QPAK_isDirectory */
  462. static int QPAK_isSymLink(DirHandle *h, const char *name)
  463. {
  464. return(0); /* we don't support symlinks for now */
  465. } /* QPAK_isSymlink */
  466. static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *name)
  467. {
  468. return(__PHYSFS_platformGetLastModTime(((QPAKinfo *) h->opaque)->filename));
  469. } /* QPAK_getLastModTime */
  470. static void *qpak_getFileHandle(const char *name, QPAKentry *entry)
  471. {
  472. void *retval = __PHYSFS_platformOpenRead(name);
  473. if (retval == NULL)
  474. return(NULL);
  475. if (!__PHYSFS_platformSeek(retval, entry->offset))
  476. {
  477. __PHYSFS_platformClose(retval);
  478. return(NULL);
  479. } /* if */
  480. return(retval);
  481. } /* qpak_getFileHandle */
  482. static FileHandle *QPAK_openRead(DirHandle *h, const char *name)
  483. {
  484. QPAKinfo *driver = (QPAKinfo *) h->opaque;
  485. QPAKentry *entry = qpak_findEntry(driver->root, name);
  486. QPAKfileinfo *fileDriver = 0;
  487. FileHandle *result = 0;
  488. if (entry == NULL)
  489. return(NULL);
  490. fileDriver = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
  491. BAIL_IF_MACRO(fileDriver == NULL, ERR_OUT_OF_MEMORY, NULL);
  492. fileDriver->handle = qpak_getFileHandle(driver->filename, entry);
  493. if (fileDriver->handle == NULL)
  494. {
  495. free(fileDriver);
  496. return(NULL);
  497. } /* if */
  498. fileDriver->entry = entry;
  499. fileDriver->curPos = 0;
  500. result = (FileHandle *)malloc(sizeof (FileHandle));
  501. if (result == NULL)
  502. {
  503. __PHYSFS_platformClose(fileDriver->handle);
  504. free(fileDriver);
  505. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  506. } /* if */
  507. result->opaque = fileDriver;
  508. result->dirHandle = h;
  509. result->funcs = &__PHYSFS_FileFunctions_QPAK;
  510. return(result);
  511. } /* QPAK_openRead */
  512. static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
  513. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  514. {
  515. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  516. QPAKentry *entry = finfo->entry;
  517. PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
  518. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  519. PHYSFS_sint64 rc;
  520. if (objsLeft < objCount)
  521. objCount = (PHYSFS_uint32) objsLeft;
  522. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  523. if (rc > 0)
  524. finfo->curPos += (rc * objSize);
  525. return(rc);
  526. } /* QPAK_read */
  527. static int QPAK_eof(FileHandle *handle)
  528. {
  529. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  530. QPAKentry *entry = finfo->entry;
  531. return(finfo->curPos >= (PHYSFS_sint64) entry->size);
  532. } /* QPAK_eof */
  533. static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
  534. {
  535. return(((QPAKfileinfo *) handle->opaque)->curPos);
  536. } /* QPAK_tell */
  537. static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
  538. {
  539. QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
  540. QPAKentry *entry = finfo->entry;
  541. PHYSFS_uint64 newPos = entry->offset + offset;
  542. int rc;
  543. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  544. BAIL_IF_MACRO(newPos > entry->offset + entry->size, ERR_PAST_EOF, 0);
  545. rc = __PHYSFS_platformSeek(finfo->handle, newPos);
  546. if (rc)
  547. finfo->curPos = offset;
  548. return(rc);
  549. } /* QPAK_seek */
  550. static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
  551. {
  552. return ((QPAKfileinfo *) handle->opaque)->entry->size;
  553. } /* QPAK_fileLength */
  554. static int QPAK_fileClose(FileHandle *handle)
  555. {
  556. QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
  557. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  558. free(finfo);
  559. free(handle);
  560. return(1);
  561. } /* QPAK_fileClose */
  562. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  563. /* end of qpak.c ... */