mvl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. MVLentry *a = (MVLentry *) _a;
  154. return(strcmp(a[one].name, a[two].name));
  155. } /* mvl_entry_cmp */
  156. static void mvl_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  157. {
  158. MVLentry tmp;
  159. MVLentry *first = &(((MVLentry *) _a)[one]);
  160. MVLentry *second = &(((MVLentry *) _a)[two]);
  161. memcpy(&tmp, first, sizeof (MVLentry));
  162. memcpy(first, second, sizeof (MVLentry));
  163. memcpy(second, &tmp, sizeof (MVLentry));
  164. } /* mvl_entry_swap */
  165. static int mvl_load_entries(const char *name, int forWriting, MVLinfo *info)
  166. {
  167. void *fh = NULL;
  168. PHYSFS_uint32 fileCount;
  169. PHYSFS_uint32 location = 8; /* sizeof sig. */
  170. MVLentry *entry;
  171. BAIL_IF_MACRO(!mvl_open(name, forWriting, &fh, &fileCount), NULL, 0);
  172. info->entryCount = fileCount;
  173. info->entries = (MVLentry *) allocator.Malloc(sizeof(MVLentry)*fileCount);
  174. if (info->entries == NULL)
  175. {
  176. __PHYSFS_platformClose(fh);
  177. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  178. } /* if */
  179. location += (17 * fileCount);
  180. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  181. {
  182. if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
  183. {
  184. __PHYSFS_platformClose(fh);
  185. return(0);
  186. } /* if */
  187. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  188. {
  189. __PHYSFS_platformClose(fh);
  190. return(0);
  191. } /* if */
  192. entry->size = PHYSFS_swapULE32(entry->size);
  193. entry->startPos = location;
  194. location += entry->size;
  195. } /* for */
  196. __PHYSFS_platformClose(fh);
  197. __PHYSFS_sort(info->entries, info->entryCount,
  198. mvl_entry_cmp, mvl_entry_swap);
  199. return(1);
  200. } /* mvl_load_entries */
  201. static void *MVL_openArchive(const char *name, int forWriting)
  202. {
  203. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  204. MVLinfo *info = (MVLinfo *) allocator.Malloc(sizeof (MVLinfo));
  205. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  206. memset(info, '\0', sizeof (MVLinfo));
  207. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  208. GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, MVL_openArchive_failed);
  209. if (!mvl_load_entries(name, forWriting, info))
  210. goto MVL_openArchive_failed;
  211. strcpy(info->filename, name);
  212. info->last_mod_time = modtime;
  213. return(info);
  214. MVL_openArchive_failed:
  215. if (info != NULL)
  216. {
  217. if (info->filename != NULL)
  218. allocator.Free(info->filename);
  219. if (info->entries != NULL)
  220. allocator.Free(info->entries);
  221. allocator.Free(info);
  222. } /* if */
  223. return(NULL);
  224. } /* MVL_openArchive */
  225. static void MVL_enumerateFiles(dvoid *opaque, const char *dname,
  226. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  227. const char *origdir, void *callbackdata)
  228. {
  229. /* no directories in MVL files. */
  230. if (*dname == '\0')
  231. {
  232. MVLinfo *info = ((MVLinfo *) opaque);
  233. MVLentry *entry = info->entries;
  234. PHYSFS_uint32 max = info->entryCount;
  235. PHYSFS_uint32 i;
  236. for (i = 0; i < max; i++, entry++)
  237. cb(callbackdata, origdir, entry->name);
  238. } /* if */
  239. } /* MVL_enumerateFiles */
  240. static MVLentry *mvl_find_entry(MVLinfo *info, const char *name)
  241. {
  242. char *ptr = strchr(name, '.');
  243. MVLentry *a = info->entries;
  244. PHYSFS_sint32 lo = 0;
  245. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  246. PHYSFS_sint32 middle;
  247. int rc;
  248. /*
  249. * Rule out filenames to avoid unneeded processing...no dirs,
  250. * big filenames, or extensions > 3 chars.
  251. */
  252. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  253. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  254. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  255. while (lo <= hi)
  256. {
  257. middle = lo + ((hi - lo) / 2);
  258. rc = __PHYSFS_stricmpASCII(name, a[middle].name);
  259. if (rc == 0) /* found it! */
  260. return(&a[middle]);
  261. else if (rc > 0)
  262. lo = middle + 1;
  263. else
  264. hi = middle - 1;
  265. } /* while */
  266. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  267. } /* mvl_find_entry */
  268. static int MVL_exists(dvoid *opaque, const char *name)
  269. {
  270. return(mvl_find_entry(((MVLinfo *) opaque), name) != NULL);
  271. } /* MVL_exists */
  272. static int MVL_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  273. {
  274. *fileExists = MVL_exists(opaque, name);
  275. return(0); /* never directories in a groupfile. */
  276. } /* MVL_isDirectory */
  277. static int MVL_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  278. {
  279. *fileExists = MVL_exists(opaque, name);
  280. return(0); /* never symlinks in a groupfile. */
  281. } /* MVL_isSymLink */
  282. static PHYSFS_sint64 MVL_getLastModTime(dvoid *opaque,
  283. const char *name,
  284. int *fileExists)
  285. {
  286. MVLinfo *info = ((MVLinfo *) opaque);
  287. PHYSFS_sint64 retval = -1;
  288. *fileExists = (mvl_find_entry(info, name) != NULL);
  289. if (*fileExists) /* use time of MVL itself in the physical filesystem. */
  290. retval = info->last_mod_time;
  291. return(retval);
  292. } /* MVL_getLastModTime */
  293. static fvoid *MVL_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  294. {
  295. MVLinfo *info = ((MVLinfo *) opaque);
  296. MVLfileinfo *finfo;
  297. MVLentry *entry;
  298. entry = mvl_find_entry(info, fnm);
  299. *fileExists = (entry != NULL);
  300. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  301. finfo = (MVLfileinfo *) allocator.Malloc(sizeof (MVLfileinfo));
  302. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  303. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  304. if ( (finfo->handle == NULL) ||
  305. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  306. {
  307. allocator.Free(finfo);
  308. return(NULL);
  309. } /* if */
  310. finfo->curPos = 0;
  311. finfo->entry = entry;
  312. return(finfo);
  313. } /* MVL_openRead */
  314. static fvoid *MVL_openWrite(dvoid *opaque, const char *name)
  315. {
  316. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  317. } /* MVL_openWrite */
  318. static fvoid *MVL_openAppend(dvoid *opaque, const char *name)
  319. {
  320. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  321. } /* MVL_openAppend */
  322. static int MVL_remove(dvoid *opaque, const char *name)
  323. {
  324. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  325. } /* MVL_remove */
  326. static int MVL_mkdir(dvoid *opaque, const char *name)
  327. {
  328. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  329. } /* MVL_mkdir */
  330. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL =
  331. {
  332. "MVL",
  333. MVL_ARCHIVE_DESCRIPTION,
  334. "Bradley Bell <btb@icculus.org>",
  335. "http://icculus.org/physfs/",
  336. };
  337. const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
  338. {
  339. &__PHYSFS_ArchiveInfo_MVL,
  340. MVL_isArchive, /* isArchive() method */
  341. MVL_openArchive, /* openArchive() method */
  342. MVL_enumerateFiles, /* enumerateFiles() method */
  343. MVL_exists, /* exists() method */
  344. MVL_isDirectory, /* isDirectory() method */
  345. MVL_isSymLink, /* isSymLink() method */
  346. MVL_getLastModTime, /* getLastModTime() method */
  347. MVL_openRead, /* openRead() method */
  348. MVL_openWrite, /* openWrite() method */
  349. MVL_openAppend, /* openAppend() method */
  350. MVL_remove, /* remove() method */
  351. MVL_mkdir, /* mkdir() method */
  352. MVL_dirClose, /* dirClose() method */
  353. MVL_read, /* read() method */
  354. MVL_write, /* write() method */
  355. MVL_eof, /* eof() method */
  356. MVL_tell, /* tell() method */
  357. MVL_seek, /* seek() method */
  358. MVL_fileLength, /* fileLength() method */
  359. MVL_fileClose /* fileClose() method */
  360. };
  361. #endif /* defined PHYSFS_SUPPORTS_MVL */
  362. /* end of mvl.c ... */