summaryrefslogtreecommitdiff
path: root/mpq-bios.c
blob: a093b84d59c05773368c68e9ac3ef6a792d56ab6 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#define _LARGEFILE64_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#include "MPQCryptography.h"
#include "mpq-bios.h"
#include "mpq-errors.h"
#include "mpq-misc.h"
#include "int-bios.h"
#include "errors.h"
#include "inttypes.h"

typedef struct {
    /* basic version of the header. */
    char magic[4];
    uint32_t header_size;
    uint32_t archive_size;
    uint16_t format_version;
    uint16_t sector_size;
    
    uint32_t hash_table_offset;
    uint32_t block_table_offset;
    uint32_t hash_table_entries;
    uint32_t block_table_entries;
    
    /* extended version of header - Burning Crusade. */    
    uint64_t extended_block_table_offset;
    uint16_t hash_table_offset_high;
    uint16_t block_table_offset_high;
} __attribute__ ((packed)) mpq_header_t;

typedef struct {
    uint32_t file_path_hasha;
    uint32_t file_path_hashb;
    uint16_t language;
    uint16_t platform;
    uint32_t file_block_index;
} __attribute__ ((packed)) mpq_hash_t;

typedef struct {
    uint32_t block_offset;
    uint32_t block_size;
    uint32_t file_size;
    uint32_t flags;
} __attribute__ ((packed)) mpq_block_t;

typedef struct {
    uint32_t file_path_hasha;
    uint32_t file_path_hashb;
    uint16_t language;
    uint16_t platform;
    uint32_t file_block_index;
} hash_t;

/*  Defined in int-bios.h

typedef struct {
    uint64_t block_offset;
    uint32_t block_size;
    uint32_t file_size;
    uint32_t flags;
} block_t;

*/

struct mpq_internals_t {
    int fd;
    int closeit;
    
    uint32_t header_size;
    uint32_t archive_size;
    uint16_t format_version;
    uint32_t sector_size;

    uint64_t hash_table_offset;
    uint64_t block_table_offset;
    uint32_t hash_table_entries;
    uint32_t block_table_entries;
    
    uint64_t extended_block_table_offset;
    
    hash_t * hashs;
    block_t * blocks;
};

#ifndef O_BINARY
#define O_BINARY 0
#endif

mpq_archive_t * mpqlib_open_archive(const char * fname) {
    int fd;
    mpq_archive_t * r;
    
    if ((fd = open(fname, O_RDONLY | O_LARGEFILE | O_BINARY)) == -1) {
        __mpqlib_errno = MPQLIB_ERROR_OPEN;
        return NULL;
    }
    
    r = mpqlib_reopen_archive(fd);
    if (r) {
        r->mpq_i->closeit = 1;
    } else {
        close(fd);
    }
    
    return r;
}

#define STD_HEADER_SIZE 0x20
#define EXT_HEADER_SIZE 0x0C

#define no_mpq() { \
    __mpqlib_errno = MPQLIB_ERROR_NOT_MPQ_ARCHIVE; \
    free(mpq_i); \
    free(mpq_a); \
    return NULL; \
}

#define read_error() { \
    __mpqlib_errno = MPQLIB_ERROR_READ; \
    free_archive(mpq_a); \
    free(mpq_hashs); \
    free(mpq_blocks); \
    return NULL; \
}

static void free_archive(mpq_archive_t * mpq_a) {
    free(mpq_a->mpq_i->hashs);
    free(mpq_a->mpq_i->blocks);
    free(mpq_a->mpq_i);
    free(mpq_a);
}

static int read_data(mpq_archive_t * mpq_a, void * buf, size_t l) {
    if (read(mpq_a->mpq_i->fd, buf, l) != l)
        return 0;
    return 1;
}

void mpqlib_init() {
    __mpqlib_init_cryptography();
}

void mpqlib_close_archive(mpq_archive_t * mpq_a) {
    if (mpq_a) {
        if (mpq_a->mpq_i->closeit) {
            close(mpq_a->mpq_i->fd);
        }
        free_archive(mpq_a);
    }
}

mpq_archive_t * mpqlib_reopen_archive(int fd) {
    struct mpq_internals_t * mpq_i;
    mpq_archive_t * mpq_a;
    mpq_header_t mpq_h;
    mpq_hash_t * mpq_hashs;
    mpq_block_t * mpq_blocks;
    uint16_t * mpq_extblocks = NULL;
    int i;
    
    /****TODO****/
    /* Implement endianess a bit everywhere */
    
    if (!(mpq_i = (struct mpq_internals_t *) malloc(sizeof(struct mpq_internals_t)))) {
        __mpqlib_errno = MPQLIB_ERROR_MEMORY;
        return NULL;
    }
    
    if (!(mpq_a = (mpq_archive_t *) malloc(sizeof(mpq_archive_t)))) {
        __mpqlib_errno = MPQLIB_ERROR_MEMORY;
        free(mpq_i);
        return NULL;
    }
    
    mpq_i->closeit = 0;
    mpq_i->fd = fd;
    mpq_a->mpq_i = mpq_i;
    
    if (read(fd, &mpq_h, STD_HEADER_SIZE) != STD_HEADER_SIZE)
        no_mpq();
    
    if (strncmp(mpq_h.magic, "MPQ\x1a", 4))
        no_mpq();

    if ((mpq_h.format_version | 1) != 1)
        no_mpq();
    
    if (mpq_h.format_version == 1) {
        if (!read_data(mpq_a, ((char *)(&mpq_h)) + STD_HEADER_SIZE, EXT_HEADER_SIZE))
            no_mpq();
    } else {
        mpq_h.extended_block_table_offset = 0;
        mpq_h.hash_table_offset_high = 0;
        mpq_h.block_table_offset_high = 0;
    }

    mpq_i->header_size = mpq_h.header_size;
    mpq_i->archive_size = mpq_h.archive_size;
    mpq_i->format_version = mpq_h.format_version;
    mpq_i->sector_size = 0x200 << mpq_h.sector_size;
    
    mpq_i->hash_table_offset = (uint64_t) mpq_h.hash_table_offset + (((uint64_t) mpq_h.hash_table_offset_high) << 32);
    mpq_i->block_table_offset = (uint64_t) mpq_h.block_table_offset + (((uint64_t) mpq_h.block_table_offset_high) << 32);
    mpq_i->hash_table_entries = mpq_h.hash_table_entries;
    mpq_i->block_table_entries = mpq_h.block_table_entries;
    mpq_i->extended_block_table_offset = mpq_h.extended_block_table_offset;
    
    if (!(mpq_i->hashs = (hash_t *) malloc(mpq_i->hash_table_entries * sizeof(hash_t)))) {
        __mpqlib_errno = MPQLIB_ERROR_MEMORY;
        free(mpq_i);
        free(mpq_a);
        return NULL;
    }
    
    if (!(mpq_i->blocks = (block_t *) malloc(mpq_i->block_table_entries * sizeof(block_t)))) {
        __mpqlib_errno = MPQLIB_ERROR_MEMORY;
        free(mpq_i->hashs);
        free(mpq_i);
        free(mpq_a);
        return NULL;
    }
    
    if (!(mpq_hashs = (mpq_hash_t *) malloc(mpq_i->hash_table_entries * sizeof(mpq_hash_t)))) {
        __mpqlib_errno = MPQLIB_ERROR_MEMORY;
        free_archive(mpq_a);
        return NULL;
    }
    
    if (!(mpq_blocks = (mpq_block_t *) malloc(mpq_i->block_table_entries * sizeof(mpq_block_t)))) {
        __mpqlib_errno = MPQLIB_ERROR_MEMORY;
        free(mpq_hashs);
        free_archive(mpq_a);
        return NULL;
    }
    
    if (lseek64(fd, mpq_i->hash_table_offset, SEEK_SET) != mpq_i->hash_table_offset)
        read_error();
    if (!read_data(mpq_a, mpq_hashs, mpq_i->hash_table_entries * sizeof(mpq_hash_t)))
        read_error();

    if (lseek64(fd, mpq_i->block_table_offset, SEEK_SET) != mpq_i->block_table_offset)
        read_error();
    if (!read_data(mpq_a, mpq_blocks, mpq_i->block_table_entries * sizeof(mpq_block_t)))
        read_error();
    
    if (mpq_i->extended_block_table_offset) {
        if (lseek64(fd, mpq_i->block_table_offset, SEEK_SET))
            read_error();
        
        if (!(mpq_extblocks = (uint16_t *) malloc(mpq_i->block_table_entries * sizeof(uint16_t)))) {
            __mpqlib_errno = MPQLIB_ERROR_MEMORY;
            free_archive(mpq_a);
            free(mpq_hashs);
            free(mpq_blocks);
            return NULL;
        }
        
        if (!read_data(mpq_a, mpq_blocks, mpq_i->block_table_entries * sizeof(mpq_block_t))) {
            free(mpq_extblocks);
            read_error();
        }
    }
    
    __mpqlib_decrypt(mpq_hashs, mpq_i->hash_table_entries * sizeof(mpq_block_t), __mpqlib_hash_cstring("(hash table)", 3), 1);
    __mpqlib_decrypt(mpq_blocks, mpq_i->block_table_entries * sizeof(mpq_block_t), __mpqlib_hash_cstring("(block table)", 3), 1);
    
    for (i = 0; i < mpq_i->hash_table_entries; i++) {
        /****TODO****/
        /* Implement various checks of the hash table. */
        mpq_i->hashs[i].file_path_hasha = mpq_hashs[i].file_path_hasha;
        mpq_i->hashs[i].file_path_hashb = mpq_hashs[i].file_path_hashb;
        mpq_i->hashs[i].language = mpq_hashs[i].language;
        mpq_i->hashs[i].platform = mpq_hashs[i].platform;
        mpq_i->hashs[i].file_block_index = mpq_hashs[i].file_block_index;
    }
    
    for (i = 0; i < mpq_i->block_table_entries; i++) {
        /****TODO****/
        /* Implement various checks of the block table. */
        mpq_i->blocks[i].block_offset = mpq_blocks[i].block_offset + (mpq_extblocks ? (((uint64_t) mpq_extblocks[i]) << 32): 0);
        mpq_i->blocks[i].block_size = mpq_blocks[i].block_size;
        mpq_i->blocks[i].file_size = mpq_blocks[i].file_size;
        mpq_i->blocks[i].flags = mpq_blocks[i].flags;
    }
    
    if (mpq_extblocks)
        free(mpq_extblocks);
    
    free(mpq_blocks);
    free(mpq_hashs);
    
    return mpq_a;
}

void mpqlib_printtables(mpq_archive_t * mpq_a) {
    int i;
    
    printf("Archive details:\n");
    printf("Header size:    %8d\n", mpq_a->mpq_i->header_size);
    printf("Archive size:   %8d\n", mpq_a->mpq_i->archive_size);
    printf("Format version: %8d\n", mpq_a->mpq_i->format_version);
    printf("Sector size:    %8d\n\n", mpq_a->mpq_i->sector_size);
    
    printf("Hash table offset:   %016llX\n", mpq_a->mpq_i->hash_table_offset);
    printf("Block table offset:  %016llX\n", mpq_a->mpq_i->block_table_offset);
    printf("Hash table entries:  %8d\n", mpq_a->mpq_i->hash_table_entries);
    printf("Block table entries: %8d\n", mpq_a->mpq_i->block_table_entries);

    printf("Extended block table offset:  %016llX\n\n\n", mpq_a->mpq_i->extended_block_table_offset);

    printf("Hash table dump.\n");
    printf("HashA      HashB      language   platform   index\n");
    printf("--------   --------   --------   --------   -----\n");
    for (i = 0; i < mpq_a->mpq_i->hash_table_entries; i++) {
        printf("%08X   %08X   %08X   %08X   %d\n", mpq_a->mpq_i->hashs[i].file_path_hasha, mpq_a->mpq_i->hashs[i].file_path_hashb, mpq_a->mpq_i->hashs[i].language, mpq_a->mpq_i->hashs[i].platform, mpq_a->mpq_i->hashs[i].file_block_index);
    }

    printf("\n\nBlock table dump.\n");
    printf("Entry      Block offset       size       filesize    flags\n");
    printf("--------   ----------------   --------   --------   --------\n");
    for (i = 0; i < mpq_a->mpq_i->block_table_entries; i++) {
        printf("%8d   %016llX   %8d   %8d   %08X\n", i, mpq_a->mpq_i->blocks[i].block_offset, mpq_a->mpq_i->blocks[i].block_size, mpq_a->mpq_i->blocks[i].file_size, mpq_a->mpq_i->blocks[i].flags);
    }
}

int mpqlib_find_hash_entry(mpq_archive_t * mpq_a, const char * name, uint32_t language, uint32_t platform) {
    int i;
    uint32_t hA, hB;
    
    hA = mpqlib_hashA_filename(name);
    hB = mpqlib_hashB_filename(name);
    
    for (i = 0; i < mpq_a->mpq_i->hash_table_entries; i++) {
        if ((mpq_a->mpq_i->hashs[i].file_path_hasha == hA) &&
            (mpq_a->mpq_i->hashs[i].file_path_hashb == hB) &&
            (mpq_a->mpq_i->hashs[i].language == language) &&
            (mpq_a->mpq_i->hashs[i].platform == platform)) {
            return mpq_a->mpq_i->hashs[i].file_block_index;
        }
    }
    
    return -1;
}

const block_t * __mpqlib_get_block_entry(mpq_archive_t * mpq_a, int entry) {
    if ((entry >= mpq_a->mpq_i->block_table_entries) || (entry < 0))
        return NULL;

    return mpq_a->mpq_i->blocks + entry;
}

int __mpqlib_seek(mpq_archive_t * mpq_a, off_t off) {
    if (lseek64(mpq_a->mpq_i->fd, off, SEEK_SET) != off)
        return 0;
    return 1;
}

int __mpqlib_read(mpq_archive_t * mpq_a, void * buffer, size_t size) {
    return read_data(mpq_a, buffer, size);
}