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 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. free(info->filename);
  62. free(info->entries);
  63. 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. 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 *) 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 = malloc(sizeof (MVLinfo));
  208. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
  209. memset(info, '\0', sizeof (MVLinfo));
  210. info->filename = (char *) malloc(strlen(name) + 1);
  211. if (info->filename == NULL)
  212. {
  213. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  214. goto MVL_openArchive_failed;
  215. } /* if */
  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. free(info->filename);
  226. if (info->entries != NULL)
  227. free(info->entries);
  228. free(info);
  229. } /* if */
  230. return(NULL);
  231. } /* MVL_openArchive */
  232. static void MVL_enumerateFiles(dvoid *opaque, const char *dname,
  233. int omitSymLinks, PHYSFS_StringCallback cb,
  234. 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, 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_platformStricmp(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 *) 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. 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 ... */