summaryrefslogtreecommitdiff
path: root/mpq-file.c
blob: 9cc778827d631528b8d301b7c3e62d06e2be8f4c (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
#include <stdlib.h>
#include <memory.h>
#include "mpq-bios.h"
#include "mpq-file.h"
#include "mpq-errors.h"
#include "int-bios.h"
#include "errors.h"

struct mpq_file_t {
    struct mpq_archive_t * mpq_a;
    int entry;
    int number_of_sectors;
    int remaining_bytes;
    int current_sector;
    uint32_t cursor;
    uint64_t offset;
    uint32_t file_size;
    uint32_t sector_size;

    char * buffer;
    uint64_t * offsets;
};

struct mpq_file_t * mpqlib_open_file(struct mpq_archive_t * mpq_a, int entry) {
    struct mpq_file_t * r;
    int i;
    
    if (mpqlib_ioctl(mpq_a, MPQLIB_IOCTL_ENTRY_EXISTS, entry)) {
	return NULL;
    }
    
    if (!(r = (struct mpq_file_t *) malloc(sizeof(struct mpq_file_t)))) {
	__mpqlib_errno = MPQLIB_ERROR_MEMORY;
	return NULL;
    }
    
    r->sector_size = mpqlib_ioctl(mpq_a, MPQLIB_IOCTL_GET_SECTOR_SIZE, 0);
    
    if (!(r->buffer = malloc(r->sector_size))) {
	free(r);
	__mpqlib_errno = MPQLIB_ERROR_MEMORY;
	return NULL;
    }
    
    r->entry = entry;
    r->file_size = mpqlib_ioctl(mpq_a, MPQLIB_IOCTL_GET_FILE_SIZE, entry);
    r->offset = mpqlib_ioctl(mpq_a, MPQLIB_IOCTL_GET_BLOCK_OFFSET, entry);
    r->number_of_sectors = r->file_size / r->sector_size;
    if ((r->remaining_bytes = (r->file_size % r->sector_size))) {
	r->number_of_sectors++;
    }
    r->current_sector = -1;
    r->cursor = 0;
    
    if (!(r->offsets = (uint64_t *) malloc(r->number_of_sectors * sizeof(uint64_t)))) {
	free(r->buffer);
	free(r);
	__mpqlib_errno = MPQLIB_ERROR_MEMORY;
	return NULL;
    }
    
    __mpqlib_seek(mpq_a, r->offset);
    for (i = 0; i < r->number_of_sectors; i++) {
	uint32_t o;
	__mpqlib_read(mpq_a, &o, 4);
	r->offsets[i] = r->offset + o;
    }
    
    return r;
}

struct mpq_file_t * mpqlib_open_filename(struct mpq_archive_t * mpq_a, const char * fname) {
    int e;
    
    if ((e = mpqlib_find_hash_entry_by_name(mpq_a, fname, 0, 0)) < 0)
	return NULL;

    return mpqlib_open_file(mpq_a, e);
}

void mpqlib_close_file(struct mpq_file_t * mpq_f) {
    free(mpq_f->buffer);
    free(mpq_f->offsets);
    free(mpq_f);
}

static void cache_sector(struct mpq_file_t * mpq_f, int sector) {
    if (mpq_f->current_sector != sector) {
        __mpqlib_seek(mpq_f->mpq_a, mpq_f->offsets[sector]);
	__mpqlib_read(mpq_f->mpq_a, mpq_f->buffer, mpq_f->sector_size);
	mpq_f->current_sector = sector;
    }
}

uint32_t mpqlib_read(struct mpq_file_t * mpq_f, void * buffer, uint32_t size) {
    int sector_begin, sector_end;
    uint32_t offset_begin, offset_end;
    
    uint32_t first_sector_begins, last_sector_ends;
    
    if ((size + mpq_f->cursor) >= mpq_f->file_size)
	size = mpq_f->file_size - mpq_f->cursor;

    offset_begin = mpq_f->cursor;
    sector_begin = offset_begin / mpq_f->sector_size;
    first_sector_begins = offset_begin % mpq_f->sector_size;
    
    offset_end = size + mpq_f->cursor;
    sector_end = offset_end / mpq_f->sector_size;
    last_sector_ends = offset_end % mpq_f->sector_size;
    
    if (!(offset_end % mpq_f->sector_size)) {
	sector_end--;
	last_sector_ends = mpq_f->sector_size;
    }
    
    if (sector_begin == sector_end) {
	cache_sector(mpq_f, sector_begin);
	memcpy(buffer, mpq_f->buffer + first_sector_begins, size);
	return size;
    }

    /* ... */

    return size;
}

uint32_t mpqlib_seek(struct mpq_file_t * mpq_f, int32_t offset, enum mpqlib_file_seek_t wheel) {
    switch (wheel) {
    case MPQLIB_SEEK_SET:
	if (offset < 0)
	    offset = 0;
	mpq_f->cursor = offset;
	break;
    case MPQLIB_SEEK_CUR:
	if ((-offset) >= mpq_f->cursor)
	    offset = -mpq_f->cursor;
	mpq_f->cursor += offset;
	break;
    case MPQLIB_SEEK_END:
	if (offset > 0)
	    offset = 0;
	if ((-offset) >= mpq_f->file_size)
	    offset = -mpq_f->file_size;
	mpq_f->cursor += offset;
	break;
    default:
	__mpqlib_errno = MPQLIB_ERROR_UNKNOWN;
	return 0xffffffff;
    }
    if (mpq_f->cursor >= mpq_f->file_size)
	mpq_f->cursor = mpq_f->file_size;
    return mpq_f->cursor;
}