qpak.c 20 KB

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