mix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. allocator.Free(info->entry);
  104. allocator.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. allocator.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 *) allocator.Malloc(sizeof (MIXinfo));
  191. BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
  192. memset(info, '\0', sizeof (MIXinfo));
  193. info->filename = (char *) allocator.Malloc(strlen(name) + 1);
  194. GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, MIX_openArchive_failed);
  195. /* store filename */
  196. strcpy(info->filename, name);
  197. /* open the file */
  198. handle = __PHYSFS_platformOpenRead(name);
  199. if (!handle)
  200. goto MIX_openArchive_failed;
  201. /* read the MIX header */
  202. if ( (!readui16(handle, &info->header.num_files)) ||
  203. (!readui32(handle, &info->header.filesize)) )
  204. goto MIX_openArchive_failed;
  205. info->delta = 6 + (info->header.num_files * 12);
  206. /* allocate space for the entries and read the entries */
  207. info->entry = allocator.Malloc(sizeof (MIXentry) * info->header.num_files);
  208. GOTO_IF_MACRO(!info->entry, ERR_OUT_OF_MEMORY, MIX_openArchive_failed);
  209. /* read the directory list */
  210. for (i = 0; i < header.num_files; i++)
  211. {
  212. if ( (!readui32(handle, &info->entry[i].hash)) ||
  213. (!readui32(handle, &info->entry[i].start_offset)) ||
  214. (!readui32(handle, &info->entry[i].end_offset)) )
  215. goto MIX_openArchive_failed;
  216. } /* for */
  217. __PHYSFS_platformClose(handle);
  218. return(info);
  219. MIX_openArchive_failed:
  220. if (info != NULL)
  221. {
  222. if (info->filename != NULL)
  223. allocator.Free(info->filename);
  224. if (info->entry != NULL)
  225. allocator.Free(info->entry);
  226. allocator.Free(info);
  227. } /* if */
  228. if (handle != NULL)
  229. __PHYSFS_platformClose(handle);
  230. return(NULL);
  231. } /* MIX_openArchive */
  232. static void MIX_enumerateFiles(dvoid *opaque, const char *dname,
  233. int omitSymLinks, PHYSFS_StringCallback cb,
  234. void *callbackdata)
  235. {
  236. /* no directories in MIX files. */
  237. if (*dirname != '\0')
  238. {
  239. MIXinfo *info = (MIXinfo*) opaque;
  240. MIXentry *entry = info->entry;
  241. int i;
  242. char buffer[32];
  243. for (i = 0; i < info->header.num_files; i++, entry++)
  244. {
  245. sprintf(buffer, "%X", entry->hash);
  246. cb(callbackdata, buffer);
  247. } /* for */
  248. } /* if */
  249. } /* MIX_enumerateFiles */
  250. static MIXentry *MIX_find_entry(MIXinfo *info, const char *name)
  251. {
  252. MIXentry *entry = info->entry;
  253. PHYSFS_uint32 i, id;
  254. /* create hash */
  255. id = MIX_hash(name);
  256. /* look for this hash */
  257. for (i = 0; i < info->header.num_files; i++, entry++)
  258. {
  259. if (entry->hash == id)
  260. return(entry);
  261. } /* for */
  262. /* nothing found... :( */
  263. return(NULL);
  264. } /* MIX_find_entry */
  265. static int MIX_exists(dvoid *opaque, const char *name)
  266. {
  267. return(MIX_find_entry(((MIXinfo *) opaque), name) != NULL);
  268. } /* MIX_exists */
  269. static int MIX_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  270. {
  271. *fileExists = MIX_exists(opaque, name);
  272. return(0); /* never directories in a MIX */
  273. } /* MIX_isDirectory */
  274. static int MIX_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  275. {
  276. *fileExists = MIX_exists(opaque, name);
  277. return(0); /* never symlinks in a MIX. */
  278. } /* MIX_isSymLink */
  279. static PHYSFS_sint64 MIX_getLastModTime(dvoid *opaque,
  280. const char *name,
  281. int *fileExists)
  282. {
  283. BAIL_MACRO(ERR_NOT_SUPPORTED, 0); /* !!! FIXME: return .MIX's modtime. */
  284. } /* MIX_getLastModTime */
  285. static fvoid *MIX_openRead(dvoid *opaque, const char *fnm, int *fileExists)
  286. {
  287. MIXinfo *info = ((MIXinfo*) opaque);
  288. MIXfileinfo *finfo;
  289. MIXentry *entry;
  290. /* try to find this file */
  291. entry = MIX_find_entry(info,fnm);
  292. BAIL_IF_MACRO(entry == NULL, ERR_NO_SUCH_FILE, NULL);
  293. /* allocate a MIX handle */
  294. finfo = (MIXfileinfo *) allocator.Malloc(sizeof (MIXfileinfo));
  295. BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
  296. /* open the archive */
  297. finfo->handle = __PHYSFS_platformOpenRead(info->filename);
  298. if(!finfo->handle)
  299. {
  300. allocator.Free(finfo);
  301. return(NULL);
  302. } /* if */
  303. /* setup structures */
  304. finfo->cur_pos = 0;
  305. finfo->info = info;
  306. finfo->entry = entry;
  307. finfo->size = entry->end_offset - entry->start_offset;
  308. return(finfo);
  309. } /* MIX_openRead */
  310. static fvoid *MIX_openWrite(dvoid *opaque, const char *name)
  311. {
  312. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  313. } /* MIX_openWrite */
  314. static fvoid *MIX_openAppend(dvoid *opaque, const char *name)
  315. {
  316. BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
  317. } /* MIX_openAppend */
  318. static int MIX_remove(dvoid *opaque, const char *name)
  319. {
  320. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  321. } /* MIX_remove */
  322. static int MIX_mkdir(dvoid *opaque, const char *name)
  323. {
  324. BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
  325. } /* MIX_mkdir */
  326. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX =
  327. {
  328. "MIX",
  329. "Westwood archive (Tiberian Dawn / Red Alert)",
  330. "Sebastian Steinhauer <steini@steini-welt.de>",
  331. "http://icculus.org/physfs/",
  332. };
  333. const PHYSFS_Archiver __PHYSFS_Archiver_MIX =
  334. {
  335. &__PHYSFS_ArchiveInfo_MIX,
  336. MIX_isArchive, /* isArchive() method */
  337. MIX_openArchive, /* openArchive() method */
  338. MIX_enumerateFiles, /* enumerateFiles() method */
  339. MIX_exists, /* exists() method */
  340. MIX_isDirectory, /* isDirectory() method */
  341. MIX_isSymLink, /* isSymLink() method */
  342. MIX_getLastModTime, /* getLastModTime() method */
  343. MIX_openRead, /* openRead() method */
  344. MIX_openWrite, /* openWrite() method */
  345. MIX_openAppend, /* openAppend() method */
  346. MIX_remove, /* remove() method */
  347. MIX_mkdir, /* mkdir() method */
  348. MIX_dirClose, /* dirClose() method */
  349. MIX_read, /* read() method */
  350. MIX_write, /* write() method */
  351. MIX_eof, /* eof() method */
  352. MIX_tell, /* tell() method */
  353. MIX_seek, /* seek() method */
  354. MIX_fileLength, /* fileLength() method */
  355. MIX_fileClose /* fileClose() method */
  356. };
  357. #endif /* defined PHYSFS_SUPPORTS_MIX */
  358. /* end of mix.c ... */