hog.c 13 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 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. free(info->filename);
  73. free(info->entries);
  74. 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. 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 *) 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 = malloc(sizeof (HOGinfo));
  245. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  246. memset(info, '\0', sizeof (HOGinfo));
  247. info->filename = (char *) malloc(strlen(name) + 1);
  248. if (info->filename == NULL)
  249. {
  250. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  251. goto HOG_openArchive_failed;
  252. } /* if */
  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. free(info->filename);
  263. if (info->entries != NULL)
  264. free(info->entries);
  265. free(info);
  266. } /* if */
  267. return(NULL);
  268. } /* HOG_openArchive */
  269. static void HOG_enumerateFiles(dvoid *opaque, const char *dname,
  270. int omitSymLinks, PHYSFS_StringCallback cb,
  271. 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, 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_platformStricmp(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 *) 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. 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 <btb@icculus.org>",
  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 ... */