hog.c 14 KB

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