mvl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. if (one != two)
  217. {
  218. const MVLentry *a = (const MVLentry *) _a;
  219. return(strcmp(a[one].name, a[two].name));
  220. } /* if */
  221. return 0;
  222. } /* mvl_entry_cmp */
  223. static void mvl_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
  224. {
  225. if (one != two)
  226. {
  227. MVLentry tmp;
  228. MVLentry *first = &(((MVLentry *) _a)[one]);
  229. MVLentry *second = &(((MVLentry *) _a)[two]);
  230. memcpy(&tmp, first, sizeof (MVLentry));
  231. memcpy(first, second, sizeof (MVLentry));
  232. memcpy(second, &tmp, sizeof (MVLentry));
  233. } /* if */
  234. } /* mvl_entry_swap */
  235. static int mvl_load_entries(const char *name, int forWriting, MVLinfo *info)
  236. {
  237. void *fh = NULL;
  238. PHYSFS_uint32 fileCount;
  239. PHYSFS_uint32 location = 8; /* sizeof sig. */
  240. MVLentry *entry;
  241. BAIL_IF_MACRO(!mvl_open(name, forWriting, &fh, &fileCount), NULL, 0);
  242. info->entryCount = fileCount;
  243. info->entries = (MVLentry *) malloc(sizeof (MVLentry) * fileCount);
  244. if (info->entries == NULL)
  245. {
  246. __PHYSFS_platformClose(fh);
  247. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  248. } /* if */
  249. location += (17 * fileCount);
  250. for (entry = info->entries; fileCount > 0; fileCount--, entry++)
  251. {
  252. if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
  253. {
  254. __PHYSFS_platformClose(fh);
  255. return(0);
  256. } /* if */
  257. if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
  258. {
  259. __PHYSFS_platformClose(fh);
  260. return(0);
  261. } /* if */
  262. entry->size = PHYSFS_swapULE32(entry->size);
  263. entry->startPos = location;
  264. location += entry->size;
  265. } /* for */
  266. __PHYSFS_platformClose(fh);
  267. __PHYSFS_sort(info->entries, info->entryCount,
  268. mvl_entry_cmp, mvl_entry_swap);
  269. return(1);
  270. } /* mvl_load_entries */
  271. static DirHandle *MVL_openArchive(const char *name, int forWriting)
  272. {
  273. MVLinfo *info;
  274. DirHandle *retval = malloc(sizeof (DirHandle));
  275. PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
  276. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  277. info = retval->opaque = malloc(sizeof (MVLinfo));
  278. if (info == NULL)
  279. {
  280. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  281. goto MVL_openArchive_failed;
  282. } /* if */
  283. memset(info, '\0', sizeof (MVLinfo));
  284. info->filename = (char *) malloc(strlen(name) + 1);
  285. if (info->filename == NULL)
  286. {
  287. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  288. goto MVL_openArchive_failed;
  289. } /* if */
  290. if (!mvl_load_entries(name, forWriting, info))
  291. goto MVL_openArchive_failed;
  292. strcpy(info->filename, name);
  293. info->last_mod_time = modtime;
  294. retval->funcs = &__PHYSFS_DirFunctions_MVL;
  295. return(retval);
  296. MVL_openArchive_failed:
  297. if (retval != NULL)
  298. {
  299. if (retval->opaque != NULL)
  300. {
  301. if (info->filename != NULL)
  302. free(info->filename);
  303. if (info->entries != NULL)
  304. free(info->entries);
  305. free(info);
  306. } /* if */
  307. free(retval);
  308. } /* if */
  309. return(NULL);
  310. } /* MVL_openArchive */
  311. static LinkedStringList *MVL_enumerateFiles(DirHandle *h,
  312. const char *dirname,
  313. int omitSymLinks)
  314. {
  315. MVLinfo *info = ((MVLinfo *) h->opaque);
  316. MVLentry *entry = info->entries;
  317. LinkedStringList *retval = NULL, *p = NULL;
  318. PHYSFS_uint32 max = info->entryCount;
  319. PHYSFS_uint32 i;
  320. /* no directories in MVL files. */
  321. BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
  322. for (i = 0; i < max; i++, entry++)
  323. retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
  324. return(retval);
  325. } /* MVL_enumerateFiles */
  326. static MVLentry *mvl_find_entry(MVLinfo *info, const char *name)
  327. {
  328. char *ptr = strchr(name, '.');
  329. MVLentry *a = info->entries;
  330. PHYSFS_sint32 lo = 0;
  331. PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
  332. PHYSFS_sint32 middle;
  333. int rc;
  334. /*
  335. * Rule out filenames to avoid unneeded processing...no dirs,
  336. * big filenames, or extensions > 3 chars.
  337. */
  338. BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
  339. BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
  340. BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
  341. while (lo <= hi)
  342. {
  343. middle = lo + ((hi - lo) / 2);
  344. rc = __PHYSFS_platformStricmp(name, a[middle].name);
  345. if (rc == 0) /* found it! */
  346. return(&a[middle]);
  347. else if (rc > 0)
  348. lo = middle + 1;
  349. else
  350. hi = middle - 1;
  351. } /* while */
  352. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  353. } /* mvl_find_entry */
  354. static int MVL_exists(DirHandle *h, const char *name)
  355. {
  356. return(mvl_find_entry(((MVLinfo *) h->opaque), name) != NULL);
  357. } /* MVL_exists */
  358. static int MVL_isDirectory(DirHandle *h, const char *name, int *fileExists)
  359. {
  360. *fileExists = MVL_exists(h, name);
  361. return(0); /* never directories in a groupfile. */
  362. } /* MVL_isDirectory */
  363. static int MVL_isSymLink(DirHandle *h, const char *name, int *fileExists)
  364. {
  365. *fileExists = MVL_exists(h, name);
  366. return(0); /* never symlinks in a groupfile. */
  367. } /* MVL_isSymLink */
  368. static PHYSFS_sint64 MVL_getLastModTime(DirHandle *h,
  369. const char *name,
  370. int *fileExists)
  371. {
  372. MVLinfo *info = ((MVLinfo *) h->opaque);
  373. PHYSFS_sint64 retval = -1;
  374. *fileExists = (mvl_find_entry(info, name) != NULL);
  375. if (*fileExists) /* use time of MVL itself in the physical filesystem. */
  376. retval = ((MVLinfo *) h->opaque)->last_mod_time;
  377. return(retval);
  378. } /* MVL_getLastModTime */
  379. static FileHandle *MVL_openRead(DirHandle *h, const char *fnm, int *fileExists)
  380. {
  381. MVLinfo *info = ((MVLinfo *) h->opaque);
  382. FileHandle *retval;
  383. MVLfileinfo *finfo;
  384. MVLentry *entry;
  385. entry = mvl_find_entry(info, fnm);
  386. *fileExists = (entry != NULL);
  387. BAIL_IF_MACRO(entry == NULL, NULL, NULL);
  388. retval = (FileHandle *) malloc(sizeof (FileHandle));
  389. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  390. finfo = (MVLfileinfo *) malloc(sizeof (MVLfileinfo));
  391. if (finfo == NULL)
  392. {
  393. free(retval);
  394. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  395. } /* if */
  396. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  397. if ( (finfo->handle == NULL) ||
  398. (!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
  399. {
  400. free(finfo);
  401. free(retval);
  402. return(NULL);
  403. } /* if */
  404. finfo->curPos = 0;
  405. finfo->entry = entry;
  406. retval->opaque = (void *) finfo;
  407. retval->funcs = &__PHYSFS_FileFunctions_MVL;
  408. retval->dirHandle = h;
  409. return(retval);
  410. } /* MVL_openRead */
  411. static FileHandle *MVL_openWrite(DirHandle *h, const char *name)
  412. {
  413. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  414. } /* MVL_openWrite */
  415. static FileHandle *MVL_openAppend(DirHandle *h, const char *name)
  416. {
  417. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  418. } /* MVL_openAppend */
  419. static int MVL_remove(DirHandle *h, const char *name)
  420. {
  421. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  422. } /* MVL_remove */
  423. static int MVL_mkdir(DirHandle *h, const char *name)
  424. {
  425. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  426. } /* MVL_mkdir */
  427. #endif /* defined PHYSFS_SUPPORTS_MVL */
  428. /* end of mvl.c ... */