mix.c 12 KB

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