mvl.c 12 KB

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