qpak.c 20 KB

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