hog.c 14 KB

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