summaryrefslogtreecommitdiff
path: root/mpq-fs.c
blob: ae9ed02f0b61077aa73fa942497cab1282f53980 (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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mpq-fs.h"

#define MAX_FNAME 2048

typedef struct directory_list_t {
    struct directory_list_t * child;
    char * name;
    struct mpq_archive_t * mpq_a;
    int entry;
    struct directory_list_t * next;
} directory_list;

static directory_list root_dir = {
    NULL, "", NULL, -1, NULL
};

static directory_list * root = &root_dir;

static char * strtoupper(char * _str) {
    char * str = _str;
    while (*str) {
        *str = toupper(*str);
        str++;
    }
    return _str;
}

/*
 * The 'new' keyword is abusive: the function returns an existing entry if it matches, allowing overrites.
 */
static directory_list * new_directory_list(directory_list * parent, struct mpq_archive_t * mpq_a, const char * name) {
    directory_list * r;
    
    for (r = parent->child; r; r = r->next) {
        if (!strcasecmp(r->name, name))
            break;
    }
    
    if (!r) {
        r = (directory_list *) malloc(sizeof(directory_list));
        r->child = NULL;
        r->next = parent->child;
        parent->child = r;
        r->name = strtoupper(strdup(name));
    }
    r->mpq_a = mpq_a;
    r->entry = -1;
    
    return r;
}

/*
 * Recursively adds a new entry into our directory structure.
 */
static directory_list * add_file_r(directory_list * parent, struct mpq_archive_t * mpq_a, char * fname, int entry) {
    char * bs;
    directory_list * r;
    int tail = 0;
    
    bs = strchr(fname, '\\');
    
    if (bs) {
        *bs = 0;
    } else {
        tail = 1;
    }
    if (!(r = new_directory_list(parent, mpq_a, fname)))
        return NULL;
    
    if (tail) {
        r->entry = entry;
        return r;
    }
    
    return add_file_r(r, mpq_a, bs + 1, entry);
}

static directory_list * add_file(struct mpq_archive_t * mpq_a, char * fname) {
    int entry;
    
    if ((entry = mpqlib_find_hash_entry_by_name(mpq_a, fname, 0, 0)) < 0)
        return NULL;
    
    return add_file_r(root, mpq_a, fname, entry);
}

/*
 * Adds an opened archive file into the system, and try to automagically import the list file.
 */
void mpqlib_fs_add_archive(struct mpq_archive_t * mpq_a) {
    struct mpq_file_t * listfile;
    char * buffer;
    int s;
    
    if (!(listfile = mpqlib_open_filename(mpq_a, "(listfile)")))
        return;
    
    s = mpqlib_seek(listfile, 0, MPQLIB_SEEK_END);
    mpqlib_seek(listfile, 0, MPQLIB_SEEK_SET);

    if (!(buffer = (char *) malloc(s + 1))) {
        mpqlib_close(listfile);
        return;
    }
    
    buffer[s] = 0;
    mpqlib_read(listfile, buffer, s);
    mpqlib_fs_attach_listfile(mpq_a, buffer);
    
    free(buffer);
    mpqlib_close(listfile);
}

/*
 * Generalistic function to add an archive to the directory structure using a custom listfile.
 */
void mpqlib_fs_attach_listfile(struct mpq_archive_t * mpq_a, const char * listfile) {
    char fname[MAX_FNAME];
    const char * p;
    char * fnp;
    
    for (p = listfile, fnp = fname; *p; p++) {
        switch (*p) {
        /* Each entry in the listfile may be separated by CR, LF, and/or ';'. */
        case '\r':
        case '\n':
        case ';':
            *fnp = 0;
            if (fnp != fname)
                add_file(mpq_a, fname);
            fnp = fname;
            break;
        default:
            *(fnp++) = *p;
            break;
        }
    }
}

/*
 * Recursively find a file.
 */
static directory_list * find_file_r(directory_list * parent, char * fname) {
    char * bs;
    directory_list * r;
    int tail = 0;
    
    bs = strchr(fname, '\\');
    
    if (bs) {
        *bs = 0;
    } else {
        tail = 1;
    }
    
    for (r = parent->child; r; r = r->next) {
        if (!strcmp(fname, r->name)) {
            if (tail) {
                return r;
            } else {
                return find_file_r(r, bs + 1);
            }
        }
    }
    
    return NULL;
}

static directory_list * find_file(char * fname) {
    return find_file_r(root, fname);
}

struct mpq_file_t * mpqlib_fs_open(const char * _fname) {
    char * fname = strtoupper(strdup(_fname));
    directory_list * entry;
    
    entry = find_file(fname);
    
    free(fname);

    if (entry) {
        return mpqlib_open_file(entry->mpq_a, entry->entry);
    }
    
    return NULL;
}