hog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. HOGentry *a = (HOGentry *) _a;
  183. return(__PHYSFS_stricmpASCII(a[one].name, a[two].name));
  184. } /* hog_entry_cmp */
  185. static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  186. {
  187. HOGentry tmp;
  188. HOGentry *first = &(((HOGentry *) _a)[one]);
  189. HOGentry *second = &(((HOGentry *) _a)[two]);
  190. memcpy(&tmp, first, sizeof (HOGentry));
  191. memcpy(first, second, sizeof (HOGentry));
  192. memcpy(second, &tmp, sizeof (HOGentry));
  193. } /* hog_entry_swap */
  194. static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)
  195. {
  196. void *fh = NULL;
  197. PHYSFS_uint32 fileCount;
  198. HOGentry *entry;
  199. BAIL_IF_MACRO(!hog_open(name, forWriting, &fh, &fileCount), NULL, 0);
  200. info->entryCount = fileCount;
  201. info->entries = (HOGentry *) allocator.Malloc(sizeof(HOGentry)*fileCount);
  202. if (info->entries == NULL)
  203. {
  204. __PHYSFS_platformClose(fh);
  205. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  206. } /* if */
  207. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  208. {
  209. if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
  210. {
  211. __PHYSFS_platformClose(fh);
  212. return(0);
  213. } /* if */
  214. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  215. {
  216. __PHYSFS_platformClose(fh);
  217. return(0);
  218. } /* if */
  219. entry->size = PHYSFS_swapULE32(entry->size);
  220. entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
  221. if (entry->startPos == -1)
  222. {
  223. __PHYSFS_platformClose(fh);
  224. return(0);
  225. }
  226. /* Skip over entry */
  227. if (!__PHYSFS_platformSeek(fh, entry->startPos + entry->size))
  228. {
  229. __PHYSFS_platformClose(fh);
  230. return(0);
  231. }
  232. } /* for */
  233. __PHYSFS_platformClose(fh);
  234. __PHYSFS_sort(info->entries, info->entryCount,
  235. hog_entry_cmp, hog_entry_swap);
  236. return(1);
  237. } /* hog_load_entries */
  238. static void *HOG_openArchive(const char *name, int forWriting)
  239. {
  240. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  241. HOGinfo *info = (HOGinfo *) allocator.Malloc(sizeof (HOGinfo));
  242. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  243. memset(info, '\0', sizeof (HOGinfo));
  244. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  245. GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, HOG_openArchive_failed);
  246. if (!hog_load_entries(name, forWriting, info))
  247. goto HOG_openArchive_failed;
  248. strcpy(info->filename, name);
  249. info->last_mod_time = modtime;
  250. return(info);
  251. HOG_openArchive_failed:
  252. if (info != NULL)
  253. {
  254. if (info->filename != NULL)
  255. allocator.Free(info->filename);
  256. if (info->entries != NULL)
  257. allocator.Free(info->entries);
  258. allocator.Free(info);
  259. } /* if */
  260. return(NULL);
  261. } /* HOG_openArchive */
  262. static void HOG_enumerateFiles(dvoid *opaque, const char *dname,
  263. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  264. const char *origdir, void *callbackdata)
  265. {
  266. /* no directories in HOG files. */
  267. if (*dname == '\0')
  268. {
  269. HOGinfo *info = (HOGinfo *) opaque;
  270. HOGentry *entry = info->entries;
  271. PHYSFS_uint32 max = info->entryCount;
  272. PHYSFS_uint32 i;
  273. for (i = 0; i < max; i++, entry++)
  274. cb(callbackdata, origdir, entry->name);
  275. } /* if */
  276. } /* HOG_enumerateFiles */
  277. static HOGentry *hog_find_entry(HOGinfo *info, const char *name)
  278. {
  279. char *ptr = strchr(name, '.');
  280. HOGentry *a = info->entries;
  281. PHYSFS_sint32 lo = 0;
  282. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  283. PHYSFS_sint32 middle;
  284. int rc;
  285. /*
  286. * Rule out filenames to avoid unneeded processing...no dirs,
  287. * big filenames, or extensions > 3 chars.
  288. */
  289. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  290. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  291. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  292. while (lo <= hi)
  293. {
  294. middle = lo + ((hi - lo) / 2);
  295. rc = __PHYSFS_stricmpASCII(name, a[middle].name);
  296. if (rc == 0) /* found it! */
  297. return(&a[middle]);
  298. else if (rc > 0)
  299. lo = middle + 1;
  300. else
  301. hi = middle - 1;
  302. } /* while */
  303. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  304. } /* hog_find_entry */
  305. static int HOG_exists(dvoid *opaque, const char *name)
  306. {
  307. return(hog_find_entry(((HOGinfo *) opaque), name) != NULL);
  308. } /* HOG_exists */
  309. static int HOG_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  310. {
  311. *fileExists = HOG_exists(opaque, name);
  312. return(0); /* never directories in a groupfile. */
  313. } /* HOG_isDirectory */
  314. static int HOG_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  315. {
  316. *fileExists = HOG_exists(opaque, name);
  317. return(0); /* never symlinks in a groupfile. */
  318. } /* HOG_isSymLink */
  319. static PHYSFS_sint64 HOG_getLastModTime(dvoid *opaque,
  320. const char *name,
  321. int *fileExists)
  322. {
  323. HOGinfo *info = ((HOGinfo *) opaque);
  324. PHYSFS_sint64 retval = -1;
  325. *fileExists = (hog_find_entry(info, name) != NULL);
  326. if (*fileExists) /* use time of HOG itself in the physical filesystem. */
  327. retval = info->last_mod_time;
  328. return(retval);
  329. } /* HOG_getLastModTime */
  330. static fvoid *HOG_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  331. {
  332. HOGinfo *info = ((HOGinfo *) opaque);
  333. HOGfileinfo *finfo;
  334. HOGentry *entry;
  335. entry = hog_find_entry(info, fnm);
  336. *fileExists = (entry != NULL);
  337. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  338. finfo = (HOGfileinfo *) allocator.Malloc(sizeof (HOGfileinfo));
  339. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  340. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  341. if ( (finfo->handle == NULL) ||
  342. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  343. {
  344. allocator.Free(finfo);
  345. return(NULL);
  346. } /* if */
  347. finfo->curPos = 0;
  348. finfo->entry = entry;
  349. return(finfo);
  350. } /* HOG_openRead */
  351. static fvoid *HOG_openWrite(dvoid *opaque, const char *name)
  352. {
  353. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  354. } /* HOG_openWrite */
  355. static fvoid *HOG_openAppend(dvoid *opaque, const char *name)
  356. {
  357. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  358. } /* HOG_openAppend */
  359. static int HOG_remove(dvoid *opaque, const char *name)
  360. {
  361. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  362. } /* HOG_remove */
  363. static int HOG_mkdir(dvoid *opaque, const char *name)
  364. {
  365. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  366. } /* HOG_mkdir */
  367. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG =
  368. {
  369. "HOG",
  370. HOG_ARCHIVE_DESCRIPTION,
  371. "Bradley Bell <btb@icculus.org>",
  372. "http://icculus.org/physfs/",
  373. };
  374. const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
  375. {
  376. &__PHYSFS_ArchiveInfo_HOG,
  377. HOG_isArchive, /* isArchive() method */
  378. HOG_openArchive, /* openArchive() method */
  379. HOG_enumerateFiles, /* enumerateFiles() method */
  380. HOG_exists, /* exists() method */
  381. HOG_isDirectory, /* isDirectory() method */
  382. HOG_isSymLink, /* isSymLink() method */
  383. HOG_getLastModTime, /* getLastModTime() method */
  384. HOG_openRead, /* openRead() method */
  385. HOG_openWrite, /* openWrite() method */
  386. HOG_openAppend, /* openAppend() method */
  387. HOG_remove, /* remove() method */
  388. HOG_mkdir, /* mkdir() method */
  389. HOG_dirClose, /* dirClose() method */
  390. HOG_read, /* read() method */
  391. HOG_write, /* write() method */
  392. HOG_eof, /* eof() method */
  393. HOG_tell, /* tell() method */
  394. HOG_seek, /* seek() method */
  395. HOG_fileLength, /* fileLength() method */
  396. HOG_fileClose /* fileClose() method */
  397. };
  398. #endif /* defined PHYSFS_SUPPORTS_HOG */
  399. /* end of hog.c ... */