mix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 PHYSFS_sint64 MIX_read(fvoid *opaque, void *buffer,
  75. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  76. static PHYSFS_sint64 MIX_write(fvoid *opaque, const void *buffer,
  77. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  78. static int MIX_eof(fvoid *opaque);
  79. static PHYSFS_sint64 MIX_tell(fvoid *opaque);
  80. static int MIX_seek(fvoid *opaque, PHYSFS_uint64 offset);
  81. static PHYSFS_sint64 MIX_fileLength(fvoid *opaque);
  82. static int MIX_fileClose(fvoid *opaque);
  83. static int MIX_isArchive(const char *filename, int forWriting);
  84. static void *MIX_openArchive(const char *name, int forWriting);
  85. static LinkedStringList *MIX_enumerateFiles(dvoid *opaque,
  86. const char *dirname,
  87. int omitSymLinks);
  88. static int MIX_exists(dvoid *opaque, const char *name);
  89. static int MIX_isDirectory(dvoid *opaque, const char *name, int *fileExists);
  90. static int MIX_isSymLink(dvoid *opaque, const char *name, int *fileExists);
  91. static PHYSFS_sint64 MIX_getLastModTime(dvoid *opaque, const char *n, int *e);
  92. static fvoid *MIX_openRead(dvoid *opaque, const char *name, int *exist);
  93. static fvoid *MIX_openWrite(dvoid *opaque, const char *name);
  94. static fvoid *MIX_openAppend(dvoid *opaque, const char *name);
  95. static int MIX_remove(dvoid *opaque, const char *name);
  96. static int MIX_mkdir(dvoid *opaque, const char *name);
  97. static void MIX_dirClose(dvoid *opaque);
  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. const PHYSFS_Archiver __PHYSFS_Archiver_MIX =
  106. {
  107. &__PHYSFS_ArchiveInfo_MIX,
  108. MIX_isArchive, /* isArchive() method */
  109. MIX_openArchive, /* openArchive() method */
  110. MIX_enumerateFiles, /* enumerateFiles() method */
  111. MIX_exists, /* exists() method */
  112. MIX_isDirectory, /* isDirectory() method */
  113. MIX_isSymLink, /* isSymLink() method */
  114. MIX_getLastModTime, /* getLastModTime() method */
  115. MIX_openRead, /* openRead() method */
  116. MIX_openWrite, /* openWrite() method */
  117. MIX_openAppend, /* openAppend() method */
  118. MIX_remove, /* remove() method */
  119. MIX_mkdir, /* mkdir() method */
  120. MIX_dirClose, /* dirClose() method */
  121. MIX_read, /* read() method */
  122. MIX_write, /* write() method */
  123. MIX_eof, /* eof() method */
  124. MIX_tell, /* tell() method */
  125. MIX_seek, /* seek() method */
  126. MIX_fileLength, /* fileLength() method */
  127. MIX_fileClose /* fileClose() method */
  128. };
  129. static PHYSFS_uint32 MIX_hash(const char *name)
  130. {
  131. PHYSFS_uint32 id = 0;
  132. PHYSFS_uint32 a = 0;
  133. PHYSFS_uint32 i = 0;
  134. PHYSFS_uint32 l;
  135. PHYSFS_uint32 j;
  136. l = strlen(name);
  137. while (i < l)
  138. {
  139. a = 0;
  140. for(j = 0; j < 4; j++)
  141. {
  142. a >>= 8;
  143. if (i < l)
  144. {
  145. a += (unsigned int) (name[i]) << 24;
  146. i++;
  147. } /* if */
  148. } /* for */
  149. id = (id << 1 | id >> 31) + a;
  150. } /* while */
  151. /* a bit debuggin :)
  152. /printf("Filename %s -> %X\n",name,id); */
  153. return(id);
  154. } /* MIX_hash */
  155. static void MIX_dirClose(dvoid *opaque)
  156. {
  157. MIXinfo *info = ((MIXinfo *) opaque);
  158. free(info->entry);
  159. free(info->filename);
  160. } /* MIX_dirClose */
  161. static PHYSFS_sint64 MIX_read(fvoid *opaque, void *buffer,
  162. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  163. {
  164. MIXfileinfo *finfo = (MIXfileinfo *) opaque;
  165. MIXentry *entry = finfo->entry;
  166. PHYSFS_uint32 read;
  167. /* set position in the archive */
  168. __PHYSFS_platformSeek(finfo->handle,
  169. finfo->info->delta +
  170. entry->start_offset +
  171. finfo->cur_pos);
  172. /* read n bytes */
  173. read = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
  174. /* keep filepointer up to date */
  175. if (read)
  176. finfo->cur_pos += read * objSize;
  177. return(read);
  178. } /* MIX_read */
  179. static PHYSFS_sint64 MIX_write(fvoid *opaque, const void *buffer,
  180. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  181. {
  182. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  183. } /* MIX_write */
  184. static int MIX_eof(fvoid *opaque)
  185. {
  186. MIXfileinfo *fifo = (MIXfileinfo *) opaque;
  187. return(fifo->cur_pos >= fifo->size);
  188. } /* MIX_eof */
  189. static PHYSFS_sint64 MIX_tell(fvoid *opaque)
  190. {
  191. return(((MIXfileinfo *) opaque)->cur_pos);
  192. } /* MIX_tell */
  193. static int MIX_seek(fvoid *opaque, PHYSFS_uint64 offset)
  194. {
  195. MIXfileinfo *h = (MIXfileinfo *) opaque;
  196. BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
  197. BAIL_IF_MACRO(offset >= h->size, ERR_PAST_EOF, 0);
  198. h->cur_pos = offset;
  199. return(1);
  200. } /* MIX_seek */
  201. static PHYSFS_sint64 MIX_fileLength(fvoid *opaque)
  202. {
  203. return (((MIXfileinfo *) opaque)->size);
  204. } /* MIX_fileLength */
  205. static int MIX_fileClose(fvoid *opaque)
  206. {
  207. MIXfileinfo *finfo = (MIXfileinfo *) opaque;
  208. __PHYSFS_platformClose(finfo->handle);
  209. free(finfo);
  210. return(1);
  211. } /* MIX_fileClose */
  212. static int MIX_isArchive(const char *filename, int forWriting)
  213. {
  214. /* !!! FIXME:
  215. write a simple detection routine for MIX files.
  216. Unfortunaly MIX files have no ID in the header.
  217. */
  218. return(1);
  219. } /* MIX_isArchive */
  220. /*
  221. * Read an unsigned 32-bit int and swap to native byte order.
  222. */
  223. static int readui32(void *in, PHYSFS_uint32 *val)
  224. {
  225. PHYSFS_uint32 v;
  226. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  227. *val = PHYSFS_swapULE32(v);
  228. return(1);
  229. } /* readui32 */
  230. /*
  231. * Read an unsigned 16-bit int and swap to native byte order.
  232. */
  233. static int readui16(void *in, PHYSFS_uint16 *val)
  234. {
  235. PHYSFS_uint16 v;
  236. BAIL_IF_MACRO(__PHYSFS_platformRead(in, &v, sizeof (v), 1) != 1, NULL, 0);
  237. *val = PHYSFS_swapULE16(v);
  238. return(1);
  239. } /* readui16 */
  240. static void *MIX_openArchive(const char *name, int forWriting)
  241. {
  242. PHYSFS_uint32 i = 0;
  243. MIXinfo *info = NULL;
  244. void *handle = NULL;
  245. info = (MIXinfo *) malloc(sizeof (MIXinfo));
  246. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  247. memset(info, '\0', sizeof (MIXinfo));
  248. info->filename = (char *) malloc(strlen(name) + 1);
  249. if (info->filename == NULL)
  250. {
  251. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  252. goto MIX_openArchive_failed;
  253. } /* if */
  254. /* store filename */
  255. strcpy(info->filename, name);
  256. /* open the file */
  257. handle = __PHYSFS_platformOpenRead(name);
  258. if (!handle)
  259. goto MIX_openArchive_failed;
  260. /* read the MIX header */
  261. if ( (!readui16(handle, &info->header.num_files)) ||
  262. (!readui32(handle, &info->header.filesize)) )
  263. goto MIX_openArchive_failed;
  264. info->delta = 6 + (info->header.num_files * 12);
  265. /* allocate space for the entries and read the entries */
  266. info->entry = malloc(sizeof (MIXentry) * info->header.num_files);
  267. if (info->entry == NULL)
  268. {
  269. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  270. goto MIX_openArchive_failed;
  271. } /* if */
  272. /* read the directory list */
  273. for (i = 0; i < header.num_files; i++)
  274. {
  275. if ( (!readui32(handle, &info->entry[i].hash)) ||
  276. (!readui32(handle, &info->entry[i].start_offset)) ||
  277. (!readui32(handle, &info->entry[i].end_offset)) )
  278. goto MIX_openArchive_failed;
  279. } /* for */
  280. __PHYSFS_platformClose(handle);
  281. return(info);
  282. MIX_openArchive_failed:
  283. if (info != NULL)
  284. {
  285. if (info->filename != NULL)
  286. free(info->filename);
  287. if (info->entry != NULL)
  288. free(info->entry);
  289. free(info);
  290. } /* if */
  291. if (handle != NULL)
  292. __PHYSFS_platformClose(handle);
  293. return(NULL);
  294. } /* MIX_openArchive */
  295. static LinkedStringList *MIX_enumerateFiles(dvoid *opaque,
  296. const char *dirname,
  297. int omitSymLinks)
  298. {
  299. LinkedStringList *retval = NULL, *p = NULL;
  300. MIXinfo *info = (MIXinfo*) opaque;
  301. MIXentry *entry = info->entry;
  302. int i;
  303. char buffer[32];
  304. for (i = 0; i < info->header.num_files; i++, entry++)
  305. {
  306. sprintf(buffer, "%X", entry->hash);
  307. retval = __PHYSFS_addToLinkedStringList(retval, &p, buffer, -1);
  308. } /* for */
  309. return(retval);
  310. } /* MIX_enumerateFiles */
  311. static MIXentry *MIX_find_entry(MIXinfo *info, const char *name)
  312. {
  313. MIXentry *entry = info->entry;
  314. PHYSFS_uint32 i, id;
  315. /* create hash */
  316. id = MIX_hash(name);
  317. /* look for this hash */
  318. for (i = 0; i < info->header.num_files; i++, entry++)
  319. {
  320. if (entry->hash == id)
  321. return(entry);
  322. } /* for */
  323. /* nothing found... :( */
  324. return(NULL);
  325. } /* MIX_find_entry */
  326. static int MIX_exists(dvoid *opaque, const char *name)
  327. {
  328. return(MIX_find_entry(((MIXinfo *) opaque), name) != NULL);
  329. } /* MIX_exists */
  330. static int MIX_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  331. {
  332. *fileExists = MIX_exists(opaque, name);
  333. return(0); /* never directories in a MIX */
  334. } /* MIX_isDirectory */
  335. static int MIX_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  336. {
  337. *fileExists = MIX_exists(opaque, name);
  338. return(0); /* never symlinks in a MIX. */
  339. } /* MIX_isSymLink */
  340. static PHYSFS_sint64 MIX_getLastModTime(dvoid *opaque,
  341. const char *name,
  342. int *fileExists)
  343. {
  344. BAIL_MACRO(ERR_NOT_SUPPORTED, 0); /* !!! FIXME: return .MIX's modtime. */
  345. } /* MIX_getLastModTime */
  346. static fvoid *MIX_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  347. {
  348. MIXinfo *info = ((MIXinfo*) opaque);
  349. MIXfileinfo *finfo;
  350. MIXentry *entry;
  351. /* try to find this file */
  352. entry = MIX_find_entry(info,fnm);
  353. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  354. /* allocate a MIX handle */
  355. finfo = (MIXfileinfo *) malloc(sizeof (MIXfileinfo));
  356. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  357. /* open the archive */
  358. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  359. if(!finfo->handle)
  360. {
  361. free(finfo);
  362. return(NULL);
  363. } /* if */
  364. /* setup structures */
  365. finfo->cur_pos = 0;
  366. finfo->info = info;
  367. finfo->entry = entry;
  368. finfo->size = entry->end_offset - entry->start_offset;
  369. return(finfo);
  370. } /* MIX_openRead */
  371. static fvoid *MIX_openWrite(dvoid *opaque, const char *name)
  372. {
  373. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  374. } /* MIX_openWrite */
  375. static fvoid *MIX_openAppend(dvoid *opaque, const char *name)
  376. {
  377. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  378. } /* MIX_openAppend */
  379. static int MIX_remove(dvoid *opaque, const char *name)
  380. {
  381. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  382. } /* MIX_remove */
  383. static int MIX_mkdir(dvoid *opaque, const char *name)
  384. {
  385. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  386. } /* MIX_mkdir */
  387. #endif /* defined PHYSFS_SUPPORTS_MIX */
  388. /* end of mix.c ... */