Skip to content

Latest commit

 

History

History
484 lines (390 loc) · 13.3 KB

mix.c

File metadata and controls

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