mvl.c 12 KB

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