hog.c 15 KB

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