hog.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer,
  70. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  71. static PHYSFS_sint64 HOG_write(fvoid *opaque, const void *buffer,
  72. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  73. static int HOG_eof(fvoid *opaque);
  74. static PHYSFS_sint64 HOG_tell(fvoid *opaque);
  75. static int HOG_seek(fvoid *opaque, PHYSFS_uint64 offset);
  76. static PHYSFS_sint64 HOG_fileLength(fvoid *opaque);
  77. static int HOG_fileClose(fvoid *opaque);
  78. static int HOG_isArchive(const char *filename, int forWriting);
  79. static void *HOG_openArchive(const char *name, int forWriting);
  80. static LinkedStringList *HOG_enumerateFiles(dvoid *opaque,
  81. const char *dirname,
  82. int omitSymLinks);
  83. static int HOG_exists(dvoid *opaque, const char *name);
  84. static int HOG_isDirectory(dvoid *opaque, const char *name, int *fileExists);
  85. static int HOG_isSymLink(dvoid *opaque, const char *name, int *fileExists);
  86. static PHYSFS_sint64 HOG_getLastModTime(dvoid *opaque, const char *n, int *e);
  87. static fvoid *HOG_openRead(dvoid *opaque, const char *name, int *exist);
  88. static fvoid *HOG_openWrite(dvoid *opaque, const char *name);
  89. static fvoid *HOG_openAppend(dvoid *opaque, const char *name);
  90. static int HOG_remove(dvoid *opaque, const char *name);
  91. static int HOG_mkdir(dvoid *opaque, const char *name);
  92. static void HOG_dirClose(dvoid *opaque);
  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. const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
  101. {
  102. &__PHYSFS_ArchiveInfo_HOG,
  103. HOG_isArchive, /* isArchive() method */
  104. HOG_openArchive, /* openArchive() method */
  105. HOG_enumerateFiles, /* enumerateFiles() method */
  106. HOG_exists, /* exists() method */
  107. HOG_isDirectory, /* isDirectory() method */
  108. HOG_isSymLink, /* isSymLink() method */
  109. HOG_getLastModTime, /* getLastModTime() method */
  110. HOG_openRead, /* openRead() method */
  111. HOG_openWrite, /* openWrite() method */
  112. HOG_openAppend, /* openAppend() method */
  113. HOG_remove, /* remove() method */
  114. HOG_mkdir, /* mkdir() method */
  115. HOG_dirClose, /* dirClose() method */
  116. HOG_read, /* read() method */
  117. HOG_write, /* write() method */
  118. HOG_eof, /* eof() method */
  119. HOG_tell, /* tell() method */
  120. HOG_seek, /* seek() method */
  121. HOG_fileLength, /* fileLength() method */
  122. HOG_fileClose /* fileClose() method */
  123. };
  124. static void HOG_dirClose(dvoid *opaque)
  125. {
  126. HOGinfo *info = ((HOGinfo *) opaque);
  127. free(info->filename);
  128. free(info->entries);
  129. free(info);
  130. } /* HOG_dirClose */
  131. static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer,
  132. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  133. {
  134. HOGfileinfo *finfo = (HOGfileinfo *) opaque;
  135. HOGentry *entry = finfo->entry;
  136. PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
  137. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  138. PHYSFS_sint64 rc;
  139. if (objsLeft < objCount)
  140. objCount = objsLeft;
  141. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  142. if (rc > 0)
  143. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  144. return(rc);
  145. } /* HOG_read */
  146. static PHYSFS_sint64 HOG_write(fvoid *opaque, const void *buffer,
  147. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  148. {
  149. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  150. } /* HOG_write */
  151. static int HOG_eof(fvoid *opaque)
  152. {
  153. HOGfileinfo *finfo = (HOGfileinfo *) opaque;
  154. HOGentry *entry = finfo->entry;
  155. return(finfo->curPos >= entry->size);
  156. } /* HOG_eof */
  157. static PHYSFS_sint64 HOG_tell(fvoid *opaque)
  158. {
  159. return(((HOGfileinfo *) opaque)->curPos);
  160. } /* HOG_tell */
  161. static int HOG_seek(fvoid *opaque, PHYSFS_uint64 offset)
  162. {
  163. HOGfileinfo *finfo = (HOGfileinfo *) opaque;
  164. HOGentry *entry = finfo->entry;
  165. int rc;
  166. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  167. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  168. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  169. if (rc)
  170. finfo->curPos = (PHYSFS_uint32) offset;
  171. return(rc);
  172. } /* HOG_seek */
  173. static PHYSFS_sint64 HOG_fileLength(fvoid *opaque)
  174. {
  175. HOGfileinfo *finfo = (HOGfileinfo *) opaque;
  176. return((PHYSFS_sint64) finfo->entry->size);
  177. } /* HOG_fileLength */
  178. static int HOG_fileClose(fvoid *opaque)
  179. {
  180. HOGfileinfo *finfo = (HOGfileinfo *) opaque;
  181. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  182. free(finfo);
  183. return(1);
  184. } /* HOG_fileClose */
  185. static int hog_open(const char *filename, int forWriting,
  186. void **fh, PHYSFS_uint32 *count)
  187. {
  188. PHYSFS_uint8 buf[13];
  189. PHYSFS_uint32 size;
  190. PHYSFS_sint64 pos;
  191. *count = 0;
  192. *fh = NULL;
  193. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  194. *fh = __PHYSFS_platformOpenRead(filename);
  195. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  196. if (__PHYSFS_platformRead(*fh, buf, 3, 1) != 1)
  197. goto openHog_failed;
  198. if (memcmp(buf, "DHF", 3) != 0)
  199. {
  200. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  201. goto openHog_failed;
  202. } /* if */
  203. while (1)
  204. {
  205. if (__PHYSFS_platformRead(*fh, buf, 13, 1) != 1)
  206. break; /* eof here is ok */
  207. if (__PHYSFS_platformRead(*fh, &size, 4, 1) != 1)
  208. goto openHog_failed;
  209. size = PHYSFS_swapULE32(size);
  210. (*count)++;
  211. /* Skip over entry... */
  212. pos = __PHYSFS_platformTell(*fh);
  213. if (pos == -1)
  214. goto openHog_failed;
  215. if (!__PHYSFS_platformSeek(*fh, pos + size))
  216. goto openHog_failed;
  217. } /* while */
  218. /* Rewind to start of entries... */
  219. if (!__PHYSFS_platformSeek(*fh, 3))
  220. goto openHog_failed;
  221. return(1);
  222. openHog_failed:
  223. if (*fh != NULL)
  224. __PHYSFS_platformClose(*fh);
  225. *count = -1;
  226. *fh = NULL;
  227. return(0);
  228. } /* hog_open */
  229. static int HOG_isArchive(const char *filename, int forWriting)
  230. {
  231. void *fh;
  232. PHYSFS_uint32 fileCount;
  233. int retval = hog_open(filename, forWriting, &fh, &fileCount);
  234. if (fh != NULL)
  235. __PHYSFS_platformClose(fh);
  236. return(retval);
  237. } /* HOG_isArchive */
  238. static int hog_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  239. {
  240. HOGentry *a = (HOGentry *) _a;
  241. return(strcmp(a[one].name, a[two].name));
  242. } /* hog_entry_cmp */
  243. static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  244. {
  245. HOGentry tmp;
  246. HOGentry *first = &(((HOGentry *) _a)[one]);
  247. HOGentry *second = &(((HOGentry *) _a)[two]);
  248. memcpy(&tmp, first, sizeof (HOGentry));
  249. memcpy(first, second, sizeof (HOGentry));
  250. memcpy(second, &tmp, sizeof (HOGentry));
  251. } /* hog_entry_swap */
  252. static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)
  253. {
  254. void *fh = NULL;
  255. PHYSFS_uint32 fileCount;
  256. HOGentry *entry;
  257. BAIL_IF_MACRO(!hog_open(name, forWriting, &fh, &fileCount), NULL, 0);
  258. info->entryCount = fileCount;
  259. info->entries = (HOGentry *) malloc(sizeof (HOGentry) * fileCount);
  260. if (info->entries == NULL)
  261. {
  262. __PHYSFS_platformClose(fh);
  263. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  264. } /* if */
  265. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  266. {
  267. if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
  268. {
  269. __PHYSFS_platformClose(fh);
  270. return(0);
  271. } /* if */
  272. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  273. {
  274. __PHYSFS_platformClose(fh);
  275. return(0);
  276. } /* if */
  277. entry->size = PHYSFS_swapULE32(entry->size);
  278. entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
  279. if (entry->startPos == -1)
  280. {
  281. __PHYSFS_platformClose(fh);
  282. return(0);
  283. }
  284. /* Skip over entry */
  285. if (!__PHYSFS_platformSeek(fh, entry->startPos + entry->size))
  286. {
  287. __PHYSFS_platformClose(fh);
  288. return(0);
  289. }
  290. } /* for */
  291. __PHYSFS_platformClose(fh);
  292. __PHYSFS_sort(info->entries, info->entryCount,
  293. hog_entry_cmp, hog_entry_swap);
  294. return(1);
  295. } /* hog_load_entries */
  296. static void *HOG_openArchive(const char *name, int forWriting)
  297. {
  298. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  299. HOGinfo *info = malloc(sizeof (HOGinfo));
  300. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  301. memset(info, '\0', sizeof (HOGinfo));
  302. info->filename = (char *) malloc(strlen(name) + 1);
  303. if (info->filename == NULL)
  304. {
  305. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  306. goto HOG_openArchive_failed;
  307. } /* if */
  308. if (!hog_load_entries(name, forWriting, info))
  309. goto HOG_openArchive_failed;
  310. strcpy(info->filename, name);
  311. info->last_mod_time = modtime;
  312. return(info);
  313. HOG_openArchive_failed:
  314. if (info != NULL)
  315. {
  316. if (info->filename != NULL)
  317. free(info->filename);
  318. if (info->entries != NULL)
  319. free(info->entries);
  320. free(info);
  321. } /* if */
  322. return(NULL);
  323. } /* HOG_openArchive */
  324. static LinkedStringList *HOG_enumerateFiles(dvoid *opaque,
  325. const char *dirname,
  326. int omitSymLinks)
  327. {
  328. HOGinfo *info = ((HOGinfo *) opaque);
  329. HOGentry *entry = info->entries;
  330. LinkedStringList *retval = NULL, *p = NULL;
  331. PHYSFS_uint32 max = info->entryCount;
  332. PHYSFS_uint32 i;
  333. /* no directories in HOG files. */
  334. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  335. for (i = 0; i < max; i++, entry++)
  336. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  337. return(retval);
  338. } /* HOG_enumerateFiles */
  339. static HOGentry *hog_find_entry(HOGinfo *info, const char *name)
  340. {
  341. char *ptr = strchr(name, '.');
  342. HOGentry *a = info->entries;
  343. PHYSFS_sint32 lo = 0;
  344. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  345. PHYSFS_sint32 middle;
  346. int rc;
  347. /*
  348. * Rule out filenames to avoid unneeded processing...no dirs,
  349. * big filenames, or extensions > 3 chars.
  350. */
  351. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  352. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  353. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  354. while (lo <= hi)
  355. {
  356. middle = lo + ((hi - lo) / 2);
  357. rc = __PHYSFS_platformStricmp(name, a[middle].name);
  358. if (rc == 0) /* found it! */
  359. return(&a[middle]);
  360. else if (rc > 0)
  361. lo = middle + 1;
  362. else
  363. hi = middle - 1;
  364. } /* while */
  365. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  366. } /* hog_find_entry */
  367. static int HOG_exists(dvoid *opaque, const char *name)
  368. {
  369. return(hog_find_entry(((HOGinfo *) opaque), name) != NULL);
  370. } /* HOG_exists */
  371. static int HOG_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  372. {
  373. *fileExists = HOG_exists(opaque, name);
  374. return(0); /* never directories in a groupfile. */
  375. } /* HOG_isDirectory */
  376. static int HOG_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  377. {
  378. *fileExists = HOG_exists(opaque, name);
  379. return(0); /* never symlinks in a groupfile. */
  380. } /* HOG_isSymLink */
  381. static PHYSFS_sint64 HOG_getLastModTime(dvoid *opaque,
  382. const char *name,
  383. int *fileExists)
  384. {
  385. HOGinfo *info = ((HOGinfo *) opaque);
  386. PHYSFS_sint64 retval = -1;
  387. *fileExists = (hog_find_entry(info, name) != NULL);
  388. if (*fileExists) /* use time of HOG itself in the physical filesystem. */
  389. retval = info->last_mod_time;
  390. return(retval);
  391. } /* HOG_getLastModTime */
  392. static fvoid *HOG_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  393. {
  394. HOGinfo *info = ((HOGinfo *) opaque);
  395. HOGfileinfo *finfo;
  396. HOGentry *entry;
  397. entry = hog_find_entry(info, fnm);
  398. *fileExists = (entry != NULL);
  399. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  400. finfo = (HOGfileinfo *) malloc(sizeof (HOGfileinfo));
  401. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  402. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  403. if ( (finfo->handle == NULL) ||
  404. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  405. {
  406. free(finfo);
  407. return(NULL);
  408. } /* if */
  409. finfo->curPos = 0;
  410. finfo->entry = entry;
  411. return(finfo);
  412. } /* HOG_openRead */
  413. static fvoid *HOG_openWrite(dvoid *opaque, const char *name)
  414. {
  415. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  416. } /* HOG_openWrite */
  417. static fvoid *HOG_openAppend(dvoid *opaque, const char *name)
  418. {
  419. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  420. } /* HOG_openAppend */
  421. static int HOG_remove(dvoid *opaque, const char *name)
  422. {
  423. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  424. } /* HOG_remove */
  425. static int HOG_mkdir(dvoid *opaque, const char *name)
  426. {
  427. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  428. } /* HOG_mkdir */
  429. #endif /* defined PHYSFS_SUPPORTS_HOG */
  430. /* end of hog.c ... */