mvl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * MVL support routines for PhysicsFS.
  3. *
  4. * This driver handles Descent II Movielib archives.
  5. *
  6. * The file format of MVL is quite easy...
  7. *
  8. * //MVL File format - Written by Heiko Herrmann
  9. * char sig[4] = {'D','M', 'V', 'L'}; // "DMVL"=Descent MoVie Library
  10. *
  11. * int num_files; // the number of files in this MVL
  12. *
  13. * struct {
  14. * char file_name[13]; // Filename, padded to 13 bytes with 0s
  15. * int file_size; // filesize in bytes
  16. * }DIR_STRUCT[num_files];
  17. *
  18. * struct {
  19. * char data[file_size]; // The file data
  20. * }FILE_STRUCT[num_files];
  21. *
  22. * (That info is from http://www.descent2.com/ddn/specs/mvl/)
  23. *
  24. * Please see the file LICENSE in the source's root directory.
  25. *
  26. * This file written by Bradley Bell.
  27. * Based on grp.c by Ryan C. Gordon.
  28. */
  29. #if HAVE_CONFIG_H
  30. # include <config.h>
  31. #endif
  32. #if (defined PHYSFS_SUPPORTS_MVL)
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "physfs.h"
  37. #define __PHYSICSFS_INTERNAL__
  38. #include "physfs_internal.h"
  39. typedef struct
  40. {
  41. char name[13];
  42. PHYSFS_uint32 startPos;
  43. PHYSFS_uint32 size;
  44. } MVLentry;
  45. typedef struct
  46. {
  47. char *filename;
  48. PHYSFS_sint64 last_mod_time;
  49. PHYSFS_uint32 entryCount;
  50. MVLentry *entries;
  51. } MVLinfo;
  52. typedef struct
  53. {
  54. void *handle;
  55. MVLentry *entry;
  56. PHYSFS_uint32 curPos;
  57. } MVLfileinfo;
  58. static PHYSFS_sint64 MVL_read(fvoid *opaque, void *buffer,
  59. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  60. static PHYSFS_sint64 MVL_write(fvoid *opaque, const void *buffer,
  61. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  62. static int MVL_eof(fvoid *opaque);
  63. static PHYSFS_sint64 MVL_tell(fvoid *opaque);
  64. static int MVL_seek(fvoid *opaque, PHYSFS_uint64 offset);
  65. static PHYSFS_sint64 MVL_fileLength(fvoid *opaque);
  66. static int MVL_fileClose(fvoid *opaque);
  67. static int MVL_isArchive(const char *filename, int forWriting);
  68. static void *MVL_openArchive(const char *name, int forWriting);
  69. static LinkedStringList *MVL_enumerateFiles(dvoid *opaque,
  70. const char *dirname,
  71. int omitSymLinks);
  72. static int MVL_exists(dvoid *opaque, const char *name);
  73. static int MVL_isDirectory(dvoid *opaque, const char *name, int *fileExists);
  74. static int MVL_isSymLink(dvoid *opaque, const char *name, int *fileExists);
  75. static PHYSFS_sint64 MVL_getLastModTime(dvoid *opaque, const char *n, int *e);
  76. static fvoid *MVL_openRead(dvoid *opaque, const char *name, int *exist);
  77. static fvoid *MVL_openWrite(dvoid *opaque, const char *name);
  78. static fvoid *MVL_openAppend(dvoid *opaque, const char *name);
  79. static int MVL_remove(dvoid *opaque, const char *name);
  80. static int MVL_mkdir(dvoid *opaque, const char *name);
  81. static void MVL_dirClose(dvoid *opaque);
  82. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL =
  83. {
  84. "MVL",
  85. MVL_ARCHIVE_DESCRIPTION,
  86. "Bradley Bell <btb@icculus.org>",
  87. "http://icculus.org/physfs/",
  88. };
  89. const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
  90. {
  91. &__PHYSFS_ArchiveInfo_MVL,
  92. MVL_isArchive, /* isArchive() method */
  93. MVL_openArchive, /* openArchive() method */
  94. MVL_enumerateFiles, /* enumerateFiles() method */
  95. MVL_exists, /* exists() method */
  96. MVL_isDirectory, /* isDirectory() method */
  97. MVL_isSymLink, /* isSymLink() method */
  98. MVL_getLastModTime, /* getLastModTime() method */
  99. MVL_openRead, /* openRead() method */
  100. MVL_openWrite, /* openWrite() method */
  101. MVL_openAppend, /* openAppend() method */
  102. MVL_remove, /* remove() method */
  103. MVL_mkdir, /* mkdir() method */
  104. MVL_dirClose, /* dirClose() method */
  105. MVL_read, /* read() method */
  106. MVL_write, /* write() method */
  107. MVL_eof, /* eof() method */
  108. MVL_tell, /* tell() method */
  109. MVL_seek, /* seek() method */
  110. MVL_fileLength, /* fileLength() method */
  111. MVL_fileClose /* fileClose() method */
  112. };
  113. static void MVL_dirClose(dvoid *opaque)
  114. {
  115. MVLinfo *info = ((MVLinfo *) opaque);
  116. free(info->filename);
  117. free(info->entries);
  118. free(info);
  119. } /* MVL_dirClose */
  120. static PHYSFS_sint64 MVL_read(fvoid *opaque, void *buffer,
  121. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  122. {
  123. MVLfileinfo *finfo = (MVLfileinfo *) opaque;
  124. MVLentry *entry = finfo->entry;
  125. PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
  126. PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
  127. PHYSFS_sint64 rc;
  128. if (objsLeft < objCount)
  129. objCount = objsLeft;
  130. rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  131. if (rc > 0)
  132. finfo->curPos += (PHYSFS_uint32) (rc * objSize);
  133. return(rc);
  134. } /* MVL_read */
  135. static PHYSFS_sint64 MVL_write(fvoid *opaque, const void *buffer,
  136. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  137. {
  138. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  139. } /* MVL_write */
  140. static int MVL_eof(fvoid *opaque)
  141. {
  142. MVLfileinfo *finfo = (MVLfileinfo *) opaque;
  143. MVLentry *entry = finfo->entry;
  144. return(finfo->curPos >= entry->size);
  145. } /* MVL_eof */
  146. static PHYSFS_sint64 MVL_tell(fvoid *opaque)
  147. {
  148. return(((MVLfileinfo *) opaque)->curPos);
  149. } /* MVL_tell */
  150. static int MVL_seek(fvoid *opaque, PHYSFS_uint64 offset)
  151. {
  152. MVLfileinfo *finfo = (MVLfileinfo *) opaque;
  153. MVLentry *entry = finfo->entry;
  154. int rc;
  155. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  156. BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
  157. rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
  158. if (rc)
  159. finfo->curPos = (PHYSFS_uint32) offset;
  160. return(rc);
  161. } /* MVL_seek */
  162. static PHYSFS_sint64 MVL_fileLength(fvoid *opaque)
  163. {
  164. MVLfileinfo *finfo = (MVLfileinfo *) opaque;
  165. return((PHYSFS_sint64) finfo->entry->size);
  166. } /* MVL_fileLength */
  167. static int MVL_fileClose(fvoid *opaque)
  168. {
  169. MVLfileinfo *finfo = (MVLfileinfo *) opaque;
  170. BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
  171. free(finfo);
  172. return(1);
  173. } /* MVL_fileClose */
  174. static int mvl_open(const char *filename, int forWriting,
  175. void **fh, PHYSFS_uint32 *count)
  176. {
  177. PHYSFS_uint8 buf[4];
  178. *fh = NULL;
  179. BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
  180. *fh = __PHYSFS_platformOpenRead(filename);
  181. BAIL_IF_MACRO(*fh == NULL, NULL, 0);
  182. if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1)
  183. goto openMvl_failed;
  184. if (memcmp(buf, "DMVL", 4) != 0)
  185. {
  186. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  187. goto openMvl_failed;
  188. } /* if */
  189. if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
  190. goto openMvl_failed;
  191. *count = PHYSFS_swapULE32(*count);
  192. return(1);
  193. openMvl_failed:
  194. if (*fh != NULL)
  195. __PHYSFS_platformClose(*fh);
  196. *count = -1;
  197. *fh = NULL;
  198. return(0);
  199. } /* mvl_open */
  200. static int MVL_isArchive(const char *filename, int forWriting)
  201. {
  202. void *fh;
  203. PHYSFS_uint32 fileCount;
  204. int retval = mvl_open(filename, forWriting, &fh, &fileCount);
  205. if (fh != NULL)
  206. __PHYSFS_platformClose(fh);
  207. return(retval);
  208. } /* MVL_isArchive */
  209. static int mvl_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  210. {
  211. MVLentry *a = (MVLentry *) _a;
  212. return(strcmp(a[one].name, a[two].name));
  213. } /* mvl_entry_cmp */
  214. static void mvl_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  215. {
  216. MVLentry tmp;
  217. MVLentry *first = &(((MVLentry *) _a)[one]);
  218. MVLentry *second = &(((MVLentry *) _a)[two]);
  219. memcpy(&tmp, first, sizeof (MVLentry));
  220. memcpy(first, second, sizeof (MVLentry));
  221. memcpy(second, &tmp, sizeof (MVLentry));
  222. } /* mvl_entry_swap */
  223. static int mvl_load_entries(const char *name, int forWriting, MVLinfo *info)
  224. {
  225. void *fh = NULL;
  226. PHYSFS_uint32 fileCount;
  227. PHYSFS_uint32 location = 8; /* sizeof sig. */
  228. MVLentry *entry;
  229. BAIL_IF_MACRO(!mvl_open(name, forWriting, &fh, &fileCount), NULL, 0);
  230. info->entryCount = fileCount;
  231. info->entries = (MVLentry *) malloc(sizeof (MVLentry) * fileCount);
  232. if (info->entries == NULL)
  233. {
  234. __PHYSFS_platformClose(fh);
  235. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  236. } /* if */
  237. location += (17 * fileCount);
  238. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  239. {
  240. if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
  241. {
  242. __PHYSFS_platformClose(fh);
  243. return(0);
  244. } /* if */
  245. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  246. {
  247. __PHYSFS_platformClose(fh);
  248. return(0);
  249. } /* if */
  250. entry->size = PHYSFS_swapULE32(entry->size);
  251. entry->startPos = location;
  252. location += entry->size;
  253. } /* for */
  254. __PHYSFS_platformClose(fh);
  255. __PHYSFS_sort(info->entries, info->entryCount,
  256. mvl_entry_cmp, mvl_entry_swap);
  257. return(1);
  258. } /* mvl_load_entries */
  259. static void *MVL_openArchive(const char *name, int forWriting)
  260. {
  261. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  262. MVLinfo *info = malloc(sizeof (MVLinfo));
  263. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  264. memset(info, '\0', sizeof (MVLinfo));
  265. info->filename = (char *) malloc(strlen(name) + 1);
  266. if (info->filename == NULL)
  267. {
  268. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  269. goto MVL_openArchive_failed;
  270. } /* if */
  271. if (!mvl_load_entries(name, forWriting, info))
  272. goto MVL_openArchive_failed;
  273. strcpy(info->filename, name);
  274. info->last_mod_time = modtime;
  275. return(info);
  276. MVL_openArchive_failed:
  277. if (info != NULL)
  278. {
  279. if (info->filename != NULL)
  280. free(info->filename);
  281. if (info->entries != NULL)
  282. free(info->entries);
  283. free(info);
  284. } /* if */
  285. return(NULL);
  286. } /* MVL_openArchive */
  287. static LinkedStringList *MVL_enumerateFiles(dvoid *opaque,
  288. const char *dirname,
  289. int omitSymLinks)
  290. {
  291. MVLinfo *info = ((MVLinfo *) opaque);
  292. MVLentry *entry = info->entries;
  293. LinkedStringList *retval = NULL, *p = NULL;
  294. PHYSFS_uint32 max = info->entryCount;
  295. PHYSFS_uint32 i;
  296. /* no directories in MVL files. */
  297. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  298. for (i = 0; i < max; i++, entry++)
  299. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  300. return(retval);
  301. } /* MVL_enumerateFiles */
  302. static MVLentry *mvl_find_entry(MVLinfo *info, const char *name)
  303. {
  304. char *ptr = strchr(name, '.');
  305. MVLentry *a = info->entries;
  306. PHYSFS_sint32 lo = 0;
  307. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  308. PHYSFS_sint32 middle;
  309. int rc;
  310. /*
  311. * Rule out filenames to avoid unneeded processing...no dirs,
  312. * big filenames, or extensions > 3 chars.
  313. */
  314. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  315. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  316. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  317. while (lo <= hi)
  318. {
  319. middle = lo + ((hi - lo) / 2);
  320. rc = __PHYSFS_platformStricmp(name, a[middle].name);
  321. if (rc == 0) /* found it! */
  322. return(&a[middle]);
  323. else if (rc > 0)
  324. lo = middle + 1;
  325. else
  326. hi = middle - 1;
  327. } /* while */
  328. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  329. } /* mvl_find_entry */
  330. static int MVL_exists(dvoid *opaque, const char *name)
  331. {
  332. return(mvl_find_entry(((MVLinfo *) opaque), name) != NULL);
  333. } /* MVL_exists */
  334. static int MVL_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  335. {
  336. *fileExists = MVL_exists(opaque, name);
  337. return(0); /* never directories in a groupfile. */
  338. } /* MVL_isDirectory */
  339. static int MVL_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  340. {
  341. *fileExists = MVL_exists(opaque, name);
  342. return(0); /* never symlinks in a groupfile. */
  343. } /* MVL_isSymLink */
  344. static PHYSFS_sint64 MVL_getLastModTime(dvoid *opaque,
  345. const char *name,
  346. int *fileExists)
  347. {
  348. MVLinfo *info = ((MVLinfo *) opaque);
  349. PHYSFS_sint64 retval = -1;
  350. *fileExists = (mvl_find_entry(info, name) != NULL);
  351. if (*fileExists) /* use time of MVL itself in the physical filesystem. */
  352. retval = info->last_mod_time;
  353. return(retval);
  354. } /* MVL_getLastModTime */
  355. static fvoid *MVL_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  356. {
  357. MVLinfo *info = ((MVLinfo *) opaque);
  358. MVLfileinfo *finfo;
  359. MVLentry *entry;
  360. entry = mvl_find_entry(info, fnm);
  361. *fileExists = (entry != NULL);
  362. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  363. finfo = (MVLfileinfo *) malloc(sizeof (MVLfileinfo));
  364. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  365. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  366. if ( (finfo->handle == NULL) ||
  367. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  368. {
  369. free(finfo);
  370. return(NULL);
  371. } /* if */
  372. finfo->curPos = 0;
  373. finfo->entry = entry;
  374. return(finfo);
  375. } /* MVL_openRead */
  376. static fvoid *MVL_openWrite(dvoid *opaque, const char *name)
  377. {
  378. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  379. } /* MVL_openWrite */
  380. static fvoid *MVL_openAppend(dvoid *opaque, const char *name)
  381. {
  382. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  383. } /* MVL_openAppend */
  384. static int MVL_remove(dvoid *opaque, const char *name)
  385. {
  386. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  387. } /* MVL_remove */
  388. static int MVL_mkdir(dvoid *opaque, const char *name)
  389. {
  390. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  391. } /* MVL_mkdir */
  392. #endif /* defined PHYSFS_SUPPORTS_MVL */
  393. /* end of mvl.c ... */