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->filename != NULL)
  346. free(pakInfo->filename);
  347. if (pakInfo->entries != NULL)
  348. free(pakInfo->entries);
  349. qpak_deleteDirectory(pakInfo->root);
  350. free(pakInfo);
  351. } /* qpak_deletePakInfo */
  352. static DirHandle *QPAK_openArchive(const char *name, int forWriting)
  353. {
  354. void *fh = NULL;
  355. PHYSFS_uint32 dirOffset, dirLength;
  356. QPAKinfo *pi;
  357. DirHandle *retval;
  358. retval = (DirHandle *) malloc(sizeof (DirHandle));
  359. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  360. pi = (QPAKinfo *) malloc(sizeof (QPAKinfo));
  361. if (pi == NULL)
  362. {
  363. free(retval);
  364. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  365. } /* if */
  366. retval->opaque = pi;
  367. pi->filename = (char *) malloc(strlen(name) + 1);
  368. if (pi->filename == NULL)
  369. {
  370. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  371. goto QPAK_openArchive_failed;
  372. } /* if */
  373. if (!openQPak(name, forWriting, &fh))
  374. goto QPAK_openArchive_failed;
  375. if (!readui32(fh, &dirOffset))
  376. goto QPAK_openArchive_failed;
  377. if (!readui32(fh, &dirLength))
  378. goto QPAK_openArchive_failed;
  379. if (__PHYSFS_platformFileLength(fh) < dirOffset + dirLength)
  380. goto QPAK_openArchive_failed;
  381. strcpy(pi->filename, name);
  382. pi->handle = fh;
  383. pi->dirOffset = dirOffset;
  384. pi->totalEntries = dirLength / 64;
  385. pi->entries = (QPAKentry *) malloc(pi->totalEntries * sizeof (QPAKentry));
  386. if (pi->entries == NULL)
  387. {
  388. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  389. goto QPAK_openArchive_failed;
  390. } /* if */
  391. if (qpak_loadEntries(fh, dirOffset, pi->totalEntries, pi->entries) == 0)
  392. goto QPAK_openArchive_failed;
  393. qpak_sort_entries(pi->entries, pi->totalEntries);
  394. pi->root = qpak_newDirectory("");
  395. if (pi->root == NULL)
  396. goto QPAK_openArchive_failed;
  397. if (qpak_populateDirectories(pi->entries, pi->totalEntries, pi->root) == 0)
  398. goto QPAK_openArchive_failed;
  399. retval->funcs = &__PHYSFS_DirFunctions_QPAK;
  400. return(retval);
  401. QPAK_openArchive_failed:
  402. if (retval != NULL)
  403. {
  404. if (retval->opaque != NULL)
  405. qpak_deletePakInfo((QPAKinfo *) retval->opaque);
  406. free(retval);
  407. } /* if */
  408. if (fh != NULL)
  409. __PHYSFS_platformClose(fh);
  410. return(0);
  411. } /* QPAK_openArchive */
  412. static void QPAK_dirClose(DirHandle *dirHandle)
  413. {
  414. QPAKinfo *info = (QPAKinfo *) dirHandle->opaque;
  415. __PHYSFS_platformClose(info->handle);
  416. free(info->filename);
  417. free(info);
  418. free(dirHandle);
  419. } /* QPAK_dirClose */
  420. static LinkedStringList *QPAK_enumerateFiles(DirHandle *h, const char *dirname,
  421. int omitSymLinks)
  422. {
  423. LinkedStringList *retval = NULL, *p = NULL;
  424. QPAKdirectory *dir;
  425. QPAKinfo *info = (QPAKinfo *) h->opaque;
  426. if ((dirname == NULL) || (*dirname == '\0'))
  427. dir = info->root;
  428. else
  429. dir = qpak_findDirectory(info->root, dirname);
  430. if (dir != NULL)
  431. {
  432. QPAKdirectory *child = dir->dirs;
  433. QPAKdirentry *file = dir->files;
  434. while (child != NULL)
  435. {
  436. retval = __PHYSFS_addToLinkedStringList(retval, &p, child->name, -1);
  437. child = child->next;
  438. } /* while */
  439. while (file != NULL)
  440. {
  441. retval = __PHYSFS_addToLinkedStringList(retval, &p, file->name, -1);
  442. file = file->next;
  443. } /* while */
  444. } /* if */
  445. return(retval);
  446. } /* QPAK_enumerateFiles */
  447. static int QPAK_exists(DirHandle *h, const char *name)
  448. {
  449. QPAKinfo *driver = (QPAKinfo *) h->opaque;
  450. if ((name == NULL) || (*name == '\0'))
  451. return(0);
  452. if (qpak_findDirectory(driver->root, name) != 0)
  453. return(1);
  454. if (qpak_findEntry(driver->root, name) != 0)
  455. return(1);
  456. return(0);
  457. } /* QPAK_exists */
  458. static int QPAK_isDirectory(DirHandle *h, const char *name)
  459. {
  460. QPAKinfo *info = (QPAKinfo *) h->opaque;
  461. return(qpak_findDirectory(info->root, name) != 0);
  462. } /* QPAK_isDirectory */
  463. static int QPAK_isSymLink(DirHandle *h, const char *name)
  464. {
  465. return(0); /* we don't support symlinks for now */
  466. } /* QPAK_isSymlink */
  467. static PHYSFS_sint64 QPAK_getLastModTime(DirHandle *h, const char *name)
  468. {
  469. return(__PHYSFS_platformGetLastModTime(((QPAKinfo *) h->opaque)->filename));
  470. } /* QPAK_getLastModTime */
  471. static void *qpak_getFileHandle(const char *name, QPAKentry *entry)
  472. {
  473. void *retval = __PHYSFS_platformOpenRead(name);
  474. if (retval == NULL)
  475. return(NULL);
  476. if (!__PHYSFS_platformSeek(retval, entry->offset))
  477. {
  478. __PHYSFS_platformClose(retval);
  479. return(NULL);
  480. } /* if */
  481. return(retval);
  482. } /* qpak_getFileHandle */
  483. static FileHandle *QPAK_openRead(DirHandle *h, const char *name)
  484. {
  485. QPAKinfo *driver = (QPAKinfo *) h->opaque;
  486. QPAKentry *entry = qpak_findEntry(driver->root, name);
  487. QPAKfileinfo *fileDriver = 0;
  488. FileHandle *result = 0;
  489. if (entry == NULL)
  490. return(NULL);
  491. fileDriver = (QPAKfileinfo *) malloc(sizeof (QPAKfileinfo));
  492. BAIL_IF_MACRO(fileDriver == NULL, ERR_OUT_OF_MEMORY, NULL);
  493. fileDriver->handle = qpak_getFileHandle(driver->filename, entry);
  494. if (fileDriver->handle == NULL)
  495. {
  496. free(fileDriver);
  497. return(NULL);
  498. } /* if */
  499. fileDriver->entry = entry;
  500. fileDriver->curPos = 0;
  501. result = (FileHandle *)malloc(sizeof (FileHandle));
  502. if (result == NULL)
  503. {
  504. __PHYSFS_platformClose(fileDriver->handle);
  505. free(fileDriver);
  506. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  507. } /* if */
  508. result->opaque = fileDriver;
  509. result->dirHandle = h;
  510. result->funcs = &__PHYSFS_FileFunctions_QPAK;
  511. return(result);
  512. } /* QPAK_openRead */
  513. static PHYSFS_sint64 QPAK_read(FileHandle *handle, void *buffer,
  514. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  515. {
  516. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  517. QPAKentry *entry = finfo->entry;
  518. PHYSFS_uint64 bytesLeft = entry->size - finfo->curPos;
  519. PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
  520. PHYSFS_sint64 rc;
  521. if (objsLeft < objCount)
  522. objCount = (PHYSFS_uint32) objsLeft;
  523. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  524. if (rc > 0)
  525. finfo->curPos += (rc * objSize);
  526. return(rc);
  527. } /* QPAK_read */
  528. static int QPAK_eof(FileHandle *handle)
  529. {
  530. QPAKfileinfo *finfo = (QPAKfileinfo *) (handle->opaque);
  531. QPAKentry *entry = finfo->entry;
  532. return(finfo->curPos >= (PHYSFS_sint64) entry->size);
  533. } /* QPAK_eof */
  534. static PHYSFS_sint64 QPAK_tell(FileHandle *handle)
  535. {
  536. return(((QPAKfileinfo *) handle->opaque)->curPos);
  537. } /* QPAK_tell */
  538. static int QPAK_seek(FileHandle *handle, PHYSFS_uint64 offset)
  539. {
  540. QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
  541. QPAKentry *entry = finfo->entry;
  542. PHYSFS_uint64 newPos = entry->offset + offset;
  543. int rc;
  544. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  545. BAIL_IF_MACRO(newPos > entry->offset + entry->size, ERR_PAST_EOF, 0);
  546. rc = __PHYSFS_platformSeek(finfo->handle, newPos);
  547. if (rc)
  548. finfo->curPos = offset;
  549. return(rc);
  550. } /* QPAK_seek */
  551. static PHYSFS_sint64 QPAK_fileLength(FileHandle *handle)
  552. {
  553. return ((QPAKfileinfo *) handle->opaque)->entry->size;
  554. } /* QPAK_fileLength */
  555. static int QPAK_fileClose(FileHandle *handle)
  556. {
  557. QPAKfileinfo *finfo = (QPAKfileinfo *) handle->opaque;
  558. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  559. free(finfo);
  560. free(handle);
  561. return(1);
  562. } /* QPAK_fileClose */
  563. #endif /* defined PHYSFS_SUPPORTS_QPAK */
  564. /* end of qpak.c ... */