mix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * MIX support routines for PhysicsFS.
  3. *
  4. * This driver handles old archives used in the famous games
  5. * Command&Conquer Tiberium Dawn and Command&Conquer Red Alert.
  6. *
  7. * Newer MIX files as they are used in C&C Tiberium Sun and C&C Red Alert 2
  8. * aren't supported yet. Keep your eyes open for future updates.
  9. *
  10. * A MIX file has three parts:
  11. * (1) Header
  12. * 16bit integer -> number of files stored in this MIX
  13. * 32bit integer -> filesize
  14. * (2) "Directory"
  15. * 32bit integer -> hash of the filename
  16. * 32bit integer -> starting offset in the MIX
  17. * 32bit integer -> end offset in the MIX
  18. * (3) Data (BODY)
  19. * All data comes here
  20. *
  21. * NOTES:
  22. * The offsets are relative to the body. So offset 0 is directly after
  23. * the directory.
  24. *
  25. * Filenames only exist as hashes. So enumerate_files() will only report all
  26. * hashes. Searching a filename in hashes is extremly quick so I decided not
  27. * to include any sorting routines after then opening of the archive.
  28. *
  29. *
  30. * I found the structure of MIX files here:
  31. * http://www.geocities.com/SiliconValley/8682/cncmap1f.txt
  32. *
  33. *
  34. * Please see the file LICENSE in the source's root directory.
  35. *
  36. * This file written by Sebastian Steinhauer <steini@steini-welt.de>
  37. */
  38. #if HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #if (defined PHYSFS_SUPPORTS_MIX)
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "physfs.h"
  46. #define __PHYSICSFS_INTERNAL__
  47. #include "physfs_internal.h"
  48. typedef struct
  49. {
  50. PHYSFS_uint16 num_files;
  51. PHYSFS_uint32 filesize;
  52. } MIXheader;
  53. typedef struct
  54. {
  55. PHYSFS_uint32 hash;
  56. PHYSFS_uint32 start_offset;
  57. PHYSFS_uint32 end_offset;
  58. } MIXentry;
  59. typedef struct
  60. {
  61. char *filename; /* filename of the archive */
  62. MIXentry *entry; /* list of entries */
  63. MIXheader header; /* the header of the MIX file */
  64. PHYSFS_uint32 delta; /* size of header + entries */
  65. } MIXinfo;
  66. typedef struct
  67. {
  68. PHYSFS_uint64 size; /* filesize */
  69. PHYSFS_uint64 cur_pos; /* position in this file */
  70. MIXentry *entry; /* pointer to the MIX entry */
  71. MIXinfo *info; /* pointer to our MIXinfo */
  72. void *handle; /* filehandle */
  73. } MIXfileinfo;
  74. static void MIX_dirClose(DirHandle *h);
  75. static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
  76. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  77. static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
  78. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  79. static int MIX_eof(FileHandle *handle);
  80. static PHYSFS_sint64 MIX_tell(FileHandle *handle);
  81. static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset);
  82. static PHYSFS_sint64 MIX_fileLength(FileHandle *handle);
  83. static int MIX_fileClose(FileHandle *handle);
  84. static int MIX_isArchive(const char *filename, int forWriting);
  85. static DirHandle *MIX_openArchive(const char *name, int forWriting);
  86. static LinkedStringList *MIX_enumerateFiles(DirHandle *h,
  87. const char *dirname,
  88. int omitSymLinks);
  89. static int MIX_exists(DirHandle *h, const char *name);
  90. static int MIX_isDirectory(DirHandle *h, const char *name, int *fileExists);
  91. static int MIX_isSymLink(DirHandle *h, const char *name, int *fileExists);
  92. static PHYSFS_sint64 MIX_getLastModTime(DirHandle *h, const char *n, int *e);
  93. static FileHandle *MIX_openRead(DirHandle *h, const char *name, int *exist);
  94. static FileHandle *MIX_openWrite(DirHandle *h, const char *name);
  95. static FileHandle *MIX_openAppend(DirHandle *h, const char *name);
  96. static int MIX_remove(DirHandle *h, const char *name);
  97. static int MIX_mkdir(DirHandle *h, const char *name);
  98. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX =
  99. {
  100. "MIX",
  101. "Westwood archive (Tiberian Dawn / Red Alert)",
  102. "Sebastian Steinhauer <steini@steini-welt.de>",
  103. "http://icculus.org/physfs/",
  104. };
  105. static const FileFunctions __PHYSFS_FileFunctions_MIX =
  106. {
  107. MIX_read, /* read() method */
  108. MIX_write, /* write() method */
  109. MIX_eof, /* eof() method */
  110. MIX_tell, /* tell() method */
  111. MIX_seek, /* seek() method */
  112. MIX_fileLength, /* fileLength() method */
  113. MIX_fileClose /* fileClose() method */
  114. };
  115. const DirFunctions __PHYSFS_DirFunctions_MIX =
  116. {
  117. &__PHYSFS_ArchiveInfo_MIX,
  118. MIX_isArchive, /* isArchive() method */
  119. MIX_openArchive, /* openArchive() method */
  120. MIX_enumerateFiles, /* enumerateFiles() method */
  121. MIX_exists, /* exists() method */
  122. MIX_isDirectory, /* isDirectory() method */
  123. MIX_isSymLink, /* isSymLink() method */
  124. MIX_getLastModTime, /* getLastModTime() method */
  125. MIX_openRead, /* openRead() method */
  126. MIX_openWrite, /* openWrite() method */
  127. MIX_openAppend, /* openAppend() method */
  128. MIX_remove, /* remove() method */
  129. MIX_mkdir, /* mkdir() method */
  130. MIX_dirClose /* dirClose() method */
  131. };
  132. static PHYSFS_uint32 MIX_hash(const char *name)
  133. {
  134. PHYSFS_uint32 id = 0;
  135. PHYSFS_uint32 a = 0;
  136. PHYSFS_uint32 i = 0;
  137. PHYSFS_uint32 l;
  138. PHYSFS_uint32 j;
  139. l = strlen(name);
  140. while (i < l)
  141. {
  142. a = 0;
  143. for(j = 0; j < 4; j++)
  144. {
  145. a >>= 8;
  146. if (i < l)
  147. {
  148. a += (unsigned int) (name[i]) << 24;
  149. i++;
  150. } /* if */
  151. } /* for */
  152. id = (id << 1 | id >> 31) + a;
  153. } /* while */
  154. /* a bit debuggin :)
  155. /printf("Filename %s -> %X\n",name,id); */
  156. return(id);
  157. } /* MIX_hash */
  158. static void MIX_dirClose(DirHandle *h)
  159. {
  160. MIXinfo *info = ((MIXinfo *) h->opaque);
  161. free(info->entry);
  162. free(info->filename);
  163. free(h);
  164. } /* MIX_dirClose */
  165. static PHYSFS_sint64 MIX_read(FileHandle *handle, void *buffer,
  166. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  167. {
  168. MIXfileinfo *finfo = (MIXfileinfo*)handle->opaque;
  169. MIXentry *entry = finfo->entry;
  170. PHYSFS_uint32 read;
  171. /* set position in the archive */
  172. __PHYSFS_platformSeek(finfo->handle,
  173. finfo->info->delta +
  174. entry->start_offset +
  175. finfo->cur_pos);
  176. /* read n bytes */
  177. read = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  178. /* keep filepointer up to date */
  179. if (read)
  180. finfo->cur_pos += read * objSize;
  181. return(read);
  182. } /* MIX_read */
  183. static PHYSFS_sint64 MIX_write(FileHandle *handle, const void *buffer,
  184. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  185. {
  186. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  187. } /* MIX_write */
  188. static int MIX_eof(FileHandle *handle)
  189. {
  190. MIXfileinfo *fifo = (MIXfileinfo *) handle->opaque;
  191. return(fifo->cur_pos >= fifo->size);
  192. } /* MIX_eof */
  193. static PHYSFS_sint64 MIX_tell(FileHandle *handle)
  194. {
  195. return(((MIXfileinfo *) (handle->opaque))->cur_pos);
  196. } /* MIX_tell */
  197. static int MIX_seek(FileHandle *handle, PHYSFS_uint64 offset)
  198. {
  199. MIXfileinfo *h = (MIXfileinfo *) (handle->opaque);
  200. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  201. BAIL_IF_MACRO(offset >= h->size, ERR_PAST_EOF, 0);
  202. h->cur_pos = offset;
  203. return(1);
  204. } /* MIX_seek */
  205. static PHYSFS_sint64 MIX_fileLength(FileHandle *handle)
  206. {
  207. return (((MIXfileinfo *) (handle->opaque))->size);
  208. } /* MIX_fileLength */
  209. static int MIX_fileClose(FileHandle *handle)
  210. {
  211. MIXfileinfo *finfo = (MIXfileinfo *) handle->opaque;
  212. __PHYSFS_platformClose(finfo->handle);
  213. free(finfo);
  214. free(handle);
  215. return(1);
  216. } /* MIX_fileClose */
  217. static int MIX_isArchive(const char *filename, int forWriting)
  218. {
  219. /* TODO:
  220. write a simple detection routine for MIX files.
  221. Unfortunaly MIX files have no ID in the header.
  222. */
  223. return(1);
  224. } /* MIX_isArchive */
  225. /*
  226. * Read an unsigned 32-bit int and swap to native byte order.
  227. */
  228. static int readui32(void *in, PHYSFS_uint32 *val)
  229. {
  230. PHYSFS_uint32 v;
  231. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  232. *val = PHYSFS_swapULE32(v);
  233. return(1);
  234. } /* readui32 */
  235. /*
  236. * Read an unsigned 16-bit int and swap to native byte order.
  237. */
  238. static int readui16(void *in, PHYSFS_uint16 *val)
  239. {
  240. PHYSFS_uint16 v;
  241. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  242. *val = PHYSFS_swapULE16(v);
  243. return(1);
  244. } /* readui16 */
  245. static DirHandle *MIX_openArchive(const char *name, int forWriting)
  246. {
  247. PHYSFS_uint32 i;
  248. DirHandle *retval;
  249. MIXheader header;
  250. MIXinfo *info;
  251. MIXentry *entries;
  252. void *handle;
  253. char *fname;
  254. retval = (DirHandle *) malloc(sizeof (DirHandle));
  255. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  256. info = (MIXinfo *) malloc(sizeof (MIXinfo));
  257. if (info == NULL)
  258. {
  259. free(retval);
  260. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  261. } /* if */
  262. fname = (char*) malloc(strlen(name) + 1);
  263. if (fname == NULL)
  264. {
  265. free(retval);
  266. free(info);
  267. BAIL_MACRO(ERR_OUT_OF_MEMORY,NULL);
  268. } /* if */
  269. /* store filename */
  270. strcpy(fname, name);
  271. /* open the file */
  272. handle = __PHYSFS_platformOpenRead(name);
  273. if (!handle)
  274. {
  275. free(retval);
  276. free(info);
  277. return(NULL);
  278. } /* if */
  279. /* read the MIX header */
  280. if ( (!readui16(handle, &header.num_files)) ||
  281. (!readui32(handle, &header.filesize)) )
  282. {
  283. free(retval);
  284. free(info);
  285. __PHYSFS_platformClose(handle);
  286. return(NULL);
  287. } /* if */
  288. /* allocate space for the entries and read the entries */
  289. entries = (MIXentry *) malloc(sizeof (MIXentry) * header.num_files);
  290. if (!entries)
  291. {
  292. free(retval);
  293. free(info);
  294. __PHYSFS_platformClose(handle);
  295. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  296. } /* if */
  297. /* read the directory list */
  298. for (i = 0; i < header.num_files; i++)
  299. {
  300. if ( (!readui32(handle, &entries[i].hash)) ||
  301. (!readui32(handle, &entries[i].start_offset)) ||
  302. (!readui32(handle, &entries[i].end_offset)) )
  303. {
  304. free(entries);
  305. free(retval);
  306. free(info);
  307. __PHYSFS_platformClose(handle);
  308. return(NULL);
  309. } /* if */
  310. } /* for */
  311. __PHYSFS_platformClose(handle);
  312. /* create our driver structure and create the DirHandle */
  313. info->header = header;
  314. info->entry = entries;
  315. info->filename = fname;
  316. info->delta = 6 + (header.num_files*12);
  317. retval->funcs = &__PHYSFS_DirFunctions_MIX;
  318. retval->opaque = info;
  319. return(retval);
  320. } /* MIX_openArchive */
  321. static LinkedStringList *MIX_enumerateFiles(DirHandle *h,
  322. const char *dirname,
  323. int omitSymLinks)
  324. {
  325. LinkedStringList *retval = NULL, *p = NULL;
  326. MIXinfo *info = (MIXinfo*)h->opaque;
  327. MIXentry *entry = info->entry;
  328. int i;
  329. char buffer[32];
  330. for (i = 0; i < info->header.num_files; i++, entry++)
  331. {
  332. sprintf(buffer, "%X", entry->hash);
  333. retval = __PHYSFS_addToLinkedStringList(retval, &p, buffer, -1);
  334. } /* for */
  335. return(retval);
  336. } /* MIX_enumerateFiles */
  337. static MIXentry *MIX_find_entry(MIXinfo *info, const char *name)
  338. {
  339. MIXentry *entry = info->entry;
  340. PHYSFS_uint32 i, id;
  341. /* create hash */
  342. id = MIX_hash(name);
  343. /* look for this hash */
  344. for (i = 0; i < info->header.num_files; i++, entry++)
  345. {
  346. if (entry->hash == id)
  347. return(entry);
  348. } /* for */
  349. /* nothing found... :( */
  350. return(NULL);
  351. } /* MIX_find_entry */
  352. static int MIX_exists(DirHandle *h, const char *name)
  353. {
  354. return(MIX_find_entry(((MIXinfo *) h->opaque), name) != NULL);
  355. } /* MIX_exists */
  356. static int MIX_isDirectory(DirHandle *h, const char *name, int *fileExists)
  357. {
  358. *fileExists = MIX_exists(h, name);
  359. return(0); /* never directories in a MIX */
  360. } /* MIX_isDirectory */
  361. static int MIX_isSymLink(DirHandle *h, const char *name, int *fileExists)
  362. {
  363. *fileExists = MIX_exists(h, name);
  364. return(0); /* never symlinks in a MIX. */
  365. } /* MIX_isSymLink */
  366. static PHYSFS_sint64 MIX_getLastModTime(DirHandle *h,
  367. const char *name,
  368. int *fileExists)
  369. {
  370. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  371. } /* MIX_getLastModTime */
  372. static FileHandle *MIX_openRead(DirHandle *h, const char *fnm, int *fileExists)
  373. {
  374. FileHandle *retval;
  375. MIXinfo *info = ((MIXinfo*)h->opaque);
  376. MIXfileinfo *finfo;
  377. MIXentry *entry;
  378. /* try to find this file */
  379. entry = MIX_find_entry(info,fnm);
  380. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  381. /* allocate a MIX handle */
  382. finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
  383. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  384. /* allocate a filehandle */
  385. retval = (FileHandle*) malloc(sizeof (FileHandle));
  386. if (!retval)
  387. {
  388. free(finfo);
  389. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  390. } /* if */
  391. /* open the archive */
  392. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  393. if(!finfo->handle)
  394. {
  395. free(finfo);
  396. free(retval);
  397. return(NULL);
  398. };
  399. /* setup structures */
  400. finfo->cur_pos = 0;
  401. finfo->info = info;
  402. finfo->entry = entry;
  403. finfo->size = entry->end_offset - entry->start_offset;
  404. retval->opaque = (void *) finfo;
  405. retval->funcs = &__PHYSFS_FileFunctions_MIX;
  406. retval->dirHandle = h;
  407. return(retval);
  408. } /* MIX_openRead */
  409. static FileHandle *MIX_openWrite(DirHandle *h, const char *name)
  410. {
  411. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  412. } /* MIX_openWrite */
  413. static FileHandle *MIX_openAppend(DirHandle *h, const char *name)
  414. {
  415. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  416. } /* MIX_openAppend */
  417. static int MIX_remove(DirHandle *h, const char *name)
  418. {
  419. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  420. } /* MIX_remove */
  421. static int MIX_mkdir(DirHandle *h, const char *name)
  422. {
  423. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  424. } /* MIX_mkdir */
  425. #endif /* defined PHYSFS_SUPPORTS_MIX */
  426. /* end of mix.c ... */