mvl.c 15 KB

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