1
0

hog.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * HOG support routines for PhysicsFS.
  3. *
  4. * This driver handles Descent I/II HOG archives.
  5. *
  6. * The format is very simple:
  7. *
  8. * The file always starts with the 3-byte signature "DHF" (Descent
  9. * HOG file). After that the files of a HOG are just attached after
  10. * another, divided by a 17 bytes header, which specifies the name
  11. * and length (in bytes) of the forthcoming file! So you just read
  12. * the header with its information of how big the following file is,
  13. * and then skip exact that number of bytes to get to the next file
  14. * in that HOG.
  15. *
  16. * char sig[3] = {'D', 'H', 'F'}; // "DHF"=Descent HOG File
  17. *
  18. * struct {
  19. * char file_name[13]; // Filename, padded to 13 bytes with 0s
  20. * int file_size; // filesize in bytes
  21. * char data[file_size]; // The file data
  22. * } FILE_STRUCT; // Repeated until the end of the file.
  23. *
  24. * (That info is from http://www.descent2.com/ddn/specs/hog/)
  25. *
  26. * Please see the file LICENSE in the source's root directory.
  27. *
  28. * This file written by Bradley Bell.
  29. * Based on grp.c by Ryan C. Gordon.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #if (defined PHYSFS_SUPPORTS_HOG)
  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. /*
  42. * One HOGentry is kept for each file in an open HOG archive.
  43. */
  44. typedef struct
  45. {
  46. char name[13];
  47. PHYSFS_uint32 startPos;
  48. PHYSFS_uint32 size;
  49. } HOGentry;
  50. /*
  51. * One HOGinfo is kept for each open HOG archive.
  52. */
  53. typedef struct
  54. {
  55. char *filename;
  56. PHYSFS_sint64 last_mod_time;
  57. PHYSFS_uint32 entryCount;
  58. HOGentry *entries;
  59. } HOGinfo;
  60. /*
  61. * One HOGfileinfo is kept for each open file in a HOG archive.
  62. */
  63. typedef struct
  64. {
  65. void *handle;
  66. HOGentry *entry;
  67. PHYSFS_uint32 curPos;
  68. } HOGfileinfo;
  69. static void HOG_dirClose(DirHandle *h);
  70. static PHYSFS_sint64 HOG_read(FileHandle *handle, void *buffer,
  71. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  72. static PHYSFS_sint64 HOG_write(FileHandle *handle, const void *buffer,
  73. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  74. static int HOG_eof(FileHandle *handle);
  75. static PHYSFS_sint64 HOG_tell(FileHandle *handle);
  76. static int HOG_seek(FileHandle *handle, PHYSFS_uint64 offset);
  77. static PHYSFS_sint64 HOG_fileLength(FileHandle *handle);
  78. static int HOG_fileClose(FileHandle *handle);
  79. static int HOG_isArchive(const char *filename, int forWriting);
  80. static DirHandle *HOG_openArchive(const char *name, int forWriting);
  81. static LinkedStringList *HOG_enumerateFiles(DirHandle *h,
  82. const char *dirname,
  83. int omitSymLinks);
  84. static int HOG_exists(DirHandle *h, const char *name);
  85. static int HOG_isDirectory(DirHandle *h, const char *name, int *fileExists);
  86. static int HOG_isSymLink(DirHandle *h, const char *name, int *fileExists);
  87. static PHYSFS_sint64 HOG_getLastModTime(DirHandle *h, const char *n, int *e);
  88. static FileHandle *HOG_openRead(DirHandle *h, const char *name, int *exist);
  89. static FileHandle *HOG_openWrite(DirHandle *h, const char *name);
  90. static FileHandle *HOG_openAppend(DirHandle *h, const char *name);
  91. static int HOG_remove(DirHandle *h, const char *name);
  92. static int HOG_mkdir(DirHandle *h, const char *name);
  93. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG =
  94. {
  95. "HOG",
  96. HOG_ARCHIVE_DESCRIPTION,
  97. "Bradley Bell <btb@icculus.org>",
  98. "http://icculus.org/physfs/",
  99. };
  100. static const FileFunctions __PHYSFS_FileFunctions_HOG =
  101. {
  102. HOG_read, /* read() method */
  103. HOG_write, /* write() method */
  104. HOG_eof, /* eof() method */
  105. HOG_tell, /* tell() method */
  106. HOG_seek, /* seek() method */
  107. HOG_fileLength, /* fileLength() method */
  108. HOG_fileClose /* fileClose() method */
  109. };
  110. const DirFunctions __PHYSFS_DirFunctions_HOG =
  111. {
  112. &__PHYSFS_ArchiveInfo_HOG,
  113. HOG_isArchive, /* isArchive() method */
  114. HOG_openArchive, /* openArchive() method */
  115. HOG_enumerateFiles, /* enumerateFiles() method */
  116. HOG_exists, /* exists() method */
  117. HOG_isDirectory, /* isDirectory() method */
  118. HOG_isSymLink, /* isSymLink() method */
  119. HOG_getLastModTime, /* getLastModTime() method */
  120. HOG_openRead, /* openRead() method */
  121. HOG_openWrite, /* openWrite() method */
  122. HOG_openAppend, /* openAppend() method */
  123. HOG_remove, /* remove() method */
  124. HOG_mkdir, /* mkdir() method */
  125. HOG_dirClose /* dirClose() method */
  126. };
  127. static void HOG_dirClose(DirHandle *h)
  128. {
  129. HOGinfo *info = ((HOGinfo *) h->opaque);
  130. free(info->filename);
  131. free(info->entries);
  132. free(info);
  133. free(h);
  134. } /* HOG_dirClose */
  135. static PHYSFS_sint64 HOG_read(FileHandle *handle, void *buffer,
  136. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  137. {
  138. HOGfileinfo *finfo = (HOGfileinfo *) (handle->opaque);
  139. HOGentry *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. } /* HOG_read */
  150. static PHYSFS_sint64 HOG_write(FileHandle *handle, const void *buffer,
  151. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  152. {
  153. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  154. } /* HOG_write */
  155. static int HOG_eof(FileHandle *handle)
  156. {
  157. HOGfileinfo *finfo = (HOGfileinfo *) (handle->opaque);
  158. HOGentry *entry = finfo->entry;
  159. return(finfo->curPos >= entry->size);
  160. } /* HOG_eof */
  161. static PHYSFS_sint64 HOG_tell(FileHandle *handle)
  162. {
  163. return(((HOGfileinfo *) (handle->opaque))->curPos);
  164. } /* HOG_tell */
  165. static int HOG_seek(FileHandle *handle, PHYSFS_uint64 offset)
  166. {
  167. HOGfileinfo *finfo = (HOGfileinfo *) (handle->opaque);
  168. HOGentry *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. } /* HOG_seek */
  177. static PHYSFS_sint64 HOG_fileLength(FileHandle *handle)
  178. {
  179. HOGfileinfo *finfo = ((HOGfileinfo *) handle->opaque);
  180. return((PHYSFS_sint64) finfo->entry->size);
  181. } /* HOG_fileLength */
  182. static int HOG_fileClose(FileHandle *handle)
  183. {
  184. HOGfileinfo *finfo = ((HOGfileinfo *) handle->opaque);
  185. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  186. free(finfo);
  187. free(handle);
  188. return(1);
  189. } /* HOG_fileClose */
  190. static int hog_open(const char *filename, int forWriting,
  191. void **fh, PHYSFS_uint32 *count)
  192. {
  193. PHYSFS_uint8 buf[13];
  194. PHYSFS_uint32 size;
  195. PHYSFS_sint64 pos;
  196. *count = 0;
  197. *fh = NULL;
  198. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  199. *fh = __PHYSFS_platformOpenRead(filename);
  200. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  201. if (__PHYSFS_platformRead(*fh, buf, 3, 1) != 1)
  202. goto openHog_failed;
  203. if (memcmp(buf, "DHF", 3) != 0)
  204. {
  205. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  206. goto openHog_failed;
  207. } /* if */
  208. while (1)
  209. {
  210. if (__PHYSFS_platformRead(*fh, buf, 13, 1) != 1)
  211. break; /* eof here is ok */
  212. if (__PHYSFS_platformRead(*fh, &size, 4, 1) != 1)
  213. goto openHog_failed;
  214. size = PHYSFS_swapULE32(size);
  215. (*count)++;
  216. /* Skip over entry... */
  217. pos = __PHYSFS_platformTell(*fh);
  218. if (pos == -1)
  219. goto openHog_failed;
  220. if (!__PHYSFS_platformSeek(*fh, pos + size))
  221. goto openHog_failed;
  222. } /* while */
  223. /* Rewind to start of entries... */
  224. if (!__PHYSFS_platformSeek(*fh, 3))
  225. goto openHog_failed;
  226. return(1);
  227. openHog_failed:
  228. if (*fh != NULL)
  229. __PHYSFS_platformClose(*fh);
  230. *count = -1;
  231. *fh = NULL;
  232. return(0);
  233. } /* hog_open */
  234. static int HOG_isArchive(const char *filename, int forWriting)
  235. {
  236. void *fh;
  237. PHYSFS_uint32 fileCount;
  238. int retval = hog_open(filename, forWriting, &fh, &fileCount);
  239. if (fh != NULL)
  240. __PHYSFS_platformClose(fh);
  241. return(retval);
  242. } /* HOG_isArchive */
  243. static int hog_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  244. {
  245. if (one != two)
  246. {
  247. const HOGentry *a = (const HOGentry *) _a;
  248. return(__PHYSFS_platformStricmp(a[one].name, a[two].name));
  249. } /* if */
  250. return 0;
  251. } /* hog_entry_cmp */
  252. static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  253. {
  254. if (one != two)
  255. {
  256. HOGentry tmp;
  257. HOGentry *first = &(((HOGentry *) _a)[one]);
  258. HOGentry *second = &(((HOGentry *) _a)[two]);
  259. memcpy(&tmp, first, sizeof (HOGentry));
  260. memcpy(first, second, sizeof (HOGentry));
  261. memcpy(second, &tmp, sizeof (HOGentry));
  262. } /* if */
  263. } /* hog_entry_swap */
  264. static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)
  265. {
  266. void *fh = NULL;
  267. PHYSFS_uint32 fileCount;
  268. HOGentry *entry;
  269. BAIL_IF_MACRO(!hog_open(name, forWriting, &fh, &fileCount), NULL, 0);
  270. info->entryCount = fileCount;
  271. info->entries = (HOGentry *) malloc(sizeof (HOGentry) * fileCount);
  272. if (info->entries == NULL)
  273. {
  274. __PHYSFS_platformClose(fh);
  275. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  276. } /* if */
  277. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  278. {
  279. if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
  280. {
  281. __PHYSFS_platformClose(fh);
  282. return(0);
  283. } /* if */
  284. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  285. {
  286. __PHYSFS_platformClose(fh);
  287. return(0);
  288. } /* if */
  289. entry->size = PHYSFS_swapULE32(entry->size);
  290. entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
  291. if (entry->startPos == -1)
  292. {
  293. __PHYSFS_platformClose(fh);
  294. return(0);
  295. }
  296. /* Skip over entry */
  297. if (!__PHYSFS_platformSeek(fh, entry->startPos + entry->size))
  298. {
  299. __PHYSFS_platformClose(fh);
  300. return(0);
  301. }
  302. } /* for */
  303. __PHYSFS_platformClose(fh);
  304. __PHYSFS_sort(info->entries, info->entryCount,
  305. hog_entry_cmp, hog_entry_swap);
  306. return(1);
  307. } /* hog_load_entries */
  308. static DirHandle *HOG_openArchive(const char *name, int forWriting)
  309. {
  310. HOGinfo *info;
  311. DirHandle *retval = malloc(sizeof (DirHandle));
  312. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  313. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  314. info = retval->opaque = malloc(sizeof (HOGinfo));
  315. if (info == NULL)
  316. {
  317. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  318. goto HOG_openArchive_failed;
  319. } /* if */
  320. memset(info, '\0', sizeof (HOGinfo));
  321. info->filename = (char *) malloc(strlen(name) + 1);
  322. if (info->filename == NULL)
  323. {
  324. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  325. goto HOG_openArchive_failed;
  326. } /* if */
  327. if (!hog_load_entries(name, forWriting, info))
  328. goto HOG_openArchive_failed;
  329. strcpy(info->filename, name);
  330. info->last_mod_time = modtime;
  331. retval->funcs = &__PHYSFS_DirFunctions_HOG;
  332. return(retval);
  333. HOG_openArchive_failed:
  334. if (retval != NULL)
  335. {
  336. if (retval->opaque != NULL)
  337. {
  338. if (info->filename != NULL)
  339. free(info->filename);
  340. if (info->entries != NULL)
  341. free(info->entries);
  342. free(info);
  343. } /* if */
  344. free(retval);
  345. } /* if */
  346. return(NULL);
  347. } /* HOG_openArchive */
  348. static LinkedStringList *HOG_enumerateFiles(DirHandle *h,
  349. const char *dirname,
  350. int omitSymLinks)
  351. {
  352. HOGinfo *info = ((HOGinfo *) h->opaque);
  353. HOGentry *entry = info->entries;
  354. LinkedStringList *retval = NULL, *p = NULL;
  355. PHYSFS_uint32 max = info->entryCount;
  356. PHYSFS_uint32 i;
  357. /* no directories in HOG files. */
  358. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  359. for (i = 0; i < max; i++, entry++)
  360. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  361. return(retval);
  362. } /* HOG_enumerateFiles */
  363. static HOGentry *hog_find_entry(HOGinfo *info, const char *name)
  364. {
  365. char *ptr = strchr(name, '.');
  366. HOGentry *a = info->entries;
  367. PHYSFS_sint32 lo = 0;
  368. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  369. PHYSFS_sint32 middle;
  370. int rc;
  371. /*
  372. * Rule out filenames to avoid unneeded processing...no dirs,
  373. * big filenames, or extensions > 3 chars.
  374. */
  375. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  376. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  377. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  378. while (lo <= hi)
  379. {
  380. middle = lo + ((hi - lo) / 2);
  381. rc = __PHYSFS_platformStricmp(name, a[middle].name);
  382. if (rc == 0) /* found it! */
  383. return(&a[middle]);
  384. else if (rc > 0)
  385. lo = middle + 1;
  386. else
  387. hi = middle - 1;
  388. } /* while */
  389. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  390. } /* hog_find_entry */
  391. static int HOG_exists(DirHandle *h, const char *name)
  392. {
  393. return(hog_find_entry(((HOGinfo *) h->opaque), name) != NULL);
  394. } /* HOG_exists */
  395. static int HOG_isDirectory(DirHandle *h, const char *name, int *fileExists)
  396. {
  397. *fileExists = HOG_exists(h, name);
  398. return(0); /* never directories in a groupfile. */
  399. } /* HOG_isDirectory */
  400. static int HOG_isSymLink(DirHandle *h, const char *name, int *fileExists)
  401. {
  402. *fileExists = HOG_exists(h, name);
  403. return(0); /* never symlinks in a groupfile. */
  404. } /* HOG_isSymLink */
  405. static PHYSFS_sint64 HOG_getLastModTime(DirHandle *h,
  406. const char *name,
  407. int *fileExists)
  408. {
  409. HOGinfo *info = ((HOGinfo *) h->opaque);
  410. PHYSFS_sint64 retval = -1;
  411. *fileExists = (hog_find_entry(info, name) != NULL);
  412. if (*fileExists) /* use time of HOG itself in the physical filesystem. */
  413. retval = ((HOGinfo *) h->opaque)->last_mod_time;
  414. return(retval);
  415. } /* HOG_getLastModTime */
  416. static FileHandle *HOG_openRead(DirHandle *h, const char *fnm, int *fileExists)
  417. {
  418. HOGinfo *info = ((HOGinfo *) h->opaque);
  419. FileHandle *retval;
  420. HOGfileinfo *finfo;
  421. HOGentry *entry;
  422. entry = hog_find_entry(info, fnm);
  423. *fileExists = (entry != NULL);
  424. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  425. retval = (FileHandle *) malloc(sizeof (FileHandle));
  426. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  427. finfo = (HOGfileinfo *) malloc(sizeof (HOGfileinfo));
  428. if (finfo == NULL)
  429. {
  430. free(retval);
  431. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  432. } /* if */
  433. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  434. if ( (finfo->handle == NULL) ||
  435. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  436. {
  437. free(finfo);
  438. free(retval);
  439. return(NULL);
  440. } /* if */
  441. finfo->curPos = 0;
  442. finfo->entry = entry;
  443. retval->opaque = (void *) finfo;
  444. retval->funcs = &__PHYSFS_FileFunctions_HOG;
  445. retval->dirHandle = h;
  446. return(retval);
  447. } /* HOG_openRead */
  448. static FileHandle *HOG_openWrite(DirHandle *h, const char *name)
  449. {
  450. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  451. } /* HOG_openWrite */
  452. static FileHandle *HOG_openAppend(DirHandle *h, const char *name)
  453. {
  454. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  455. } /* HOG_openAppend */
  456. static int HOG_remove(DirHandle *h, const char *name)
  457. {
  458. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  459. } /* HOG_remove */
  460. static int HOG_mkdir(DirHandle *h, const char *name)
  461. {
  462. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  463. } /* HOG_mkdir */
  464. #endif /* defined PHYSFS_SUPPORTS_HOG */
  465. /* end of hog.c ... */