Subversion Repositories Games.Carmageddon

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 pmbaty 1
#include "image.h"
2
#include "CORE/FW/loader.h"
3
#include "CORE/FW/brlists.h"
4
#include "CORE/FW/fwsetup.h"
5
#include "CORE/FW/pattern.h"
6
#include "CORE/FW/resource.h"
7
#include "CORE/FW/scratch.h"
8
#include "CORE/HOST/himage.h"
9
#include "CORE/STD/brstdlib.h"
10
#include "harness/trace.h"
11
 
12
// IDA: br_boolean __cdecl BrImageAdd(br_image *img)
13
/*br_boolean*/void BrImageAdd(br_image* img) { // Pierre-Marie Baty -- fixed type
14
    LOG_TRACE("(%p)", img);
15
 
16
    BrAddHead(&fw.images, &img->node);
17
}
18
 
19
// IDA: br_boolean __cdecl BrImageRemove(br_image *img)
20
/*br_boolean*/void BrImageRemove(br_image* img) { // Pierre-Marie Baty -- fixed type
21
    LOG_TRACE("(%p)", img);
22
 
23
    BrRemove(&img->node);
24
}
25
 
26
// IDA: br_image* __cdecl BrImageFind(char *pattern)
27
br_image* BrImageFind(char* pattern) {
28
    char* c;
29
    br_image* img;
30
    LOG_TRACE("(\"%s\")", pattern);
31
 
32
    c = BrStrRChr(pattern, '.');
33
    if (c != NULL && (BrStrCmp(c, ".dll") == 0 || BrStrCmp(c, ".bdd") == 0|| BrStrCmp(c, "bed"))) {
34
        *c = '\0';
35
    }
36
    for (img = (br_image*)fw.images.head; img->node.next != NULL; img = (br_image*)img->node.next) {
37
        if (BrNamePatternMatch(pattern, img->identifier) != 0) {
38
            return img;
39
        }
40
    }
41
    return NULL;
42
}
43
 
44
// IDA: br_image* __usercall imageLoadHost@<EAX>(char *name@<EAX>)
45
br_image* imageLoadHost(char* name) {
46
    br_image* img;
47
    void* host_image;
48
    LOG_TRACE("(\"%s\")", name);
49
    NOT_IMPLEMENTED();
50
 
51
    host_image = HostImageLoad(name);
52
    if (host_image != NULL) {
53
        img = BrResAllocate(NULL, sizeof(br_image), BR_MEMORY_IMAGE);
54
        img->identifier = BrResStrDup(img, name);
55
        img->type = 3;
56
        img->type_pointer = host_image;
57
    }
58
    return img;
59
}
60
 
61
// IDA: br_image* __cdecl BrImageReference(char *name)
62
br_image* BrImageReference(char* name) {
63
    char* suffix;
64
    char* scratch;
65
    br_image* img;
66
    LOG_TRACE("(\"%s\")", name);
67
 
68
    scratch = BrScratchString();
69
    img = BrImageFind(name);
70
    if (img != NULL) {
71
        img->ref_count++;
72
        return img;
73
    }
74
    for (suffix = name; *suffix != '\0' && *suffix != '.'; suffix++) {
75
    }
76
    if (*suffix == '\0') {
77
        if (img == NULL) {
78
            BrStrCpy(scratch, name);
79
            BrStrCat(scratch, ".BDD");
80
            img = ImageLoad(name);
81
        }
82
        if (img == NULL) {
83
            img = imageLoadHost(scratch);
84
        }
85
        if (img == NULL) {
86
            BrStrCpy(scratch, name);
87
            BrStrCat(scratch, ".DLL");
88
            img = ImageLoad(scratch);
89
        }
90
    } else if (BrStrCmp(suffix, ".bdd") == 0 || BrStrCmp(suffix, ".bed") == 0) {
91
        if (img == NULL) {
92
            img = ImageLoad(name);
93
        }
94
        if (img == NULL) {
95
            img = imageLoadHost(name);
96
        }
97
    } else {
98
        if (img == NULL) {
99
            img = imageLoadHost(name);
100
        }
101
        if (img == NULL) {
102
            img = ImageLoad(name);
103
        }
104
    }
105
 
106
    if (img != NULL) {
107
        BrResAdd(fw.res, img);
108
        BrAddHead(&fw.images, &img->node);
109
    }
110
    return img;
111
}
112
 
113
// IDA: void* __usercall imageLookupName@<EAX>(br_image *img@<EAX>, char *name@<EDX>, br_uint_32 hint@<EBX>)
114
void* imageLookupName(br_image* img, char* name, br_uint_32 hint) {
115
    int c;
116
    int limit;
117
    int base;
118
    LOG_TRACE("(%p, \"%s\", %d)", img, name, hint);
119
 
120
    if (hint < img->n_names && BrStrCmp(name, img->names[hint]) == 0) {
121
        return img->functions[img->name_ordinals[hint]];
122
    }
123
    base = 0;
124
    limit = img->n_names;
125
    while (1) {
126
        if (limit == 0) {
127
            return NULL;
128
        }
129
        c = BrStrCmp(name, img->names[base + limit / 2]);
130
        if (c == 0) {
131
            return img->functions[img->name_ordinals[base + limit / 2]];
132
        } else if (c < 0) {
133
            continue;
134
        } else {
135
            base += limit / 2 + 1;
136
            limit = limit - (limit / 2 + 1);
137
        }
138
    }
139
}
140
 
141
// IDA: void* __cdecl BrImageLookupName(br_image *img, char *name, br_uint_32 hint)
142
void* BrImageLookupName(br_image* img, char* name, br_uint_32 hint) {
143
    char* scratch;
144
    void* p;
145
    LOG_TRACE("(%p, \"%s\", %d)", img, name, hint);
146
 
147
    scratch = BrScratchString();
148
    if (img->type == 3) {
149
        return HostImageLookupName(img->type_pointer, name, hint);
150
    }
151
    p = imageLookupName(img, name, hint);
152
    if (p != NULL) {
153
        return p;
154
    }
155
    if (*name == '_') {
156
        p = imageLookupName(img, &name[1], hint);
157
        if (p != NULL) {
158
            return p;
159
        }
160
    }
161
    *scratch = '_';
162
    BrStrCpy(&scratch[1], name);
163
    return imageLookupName(img, scratch, hint);
164
}
165
 
166
// IDA: void* __cdecl BrImageLookupOrdinal(br_image *img, br_uint_32 ordinal)
167
void* BrImageLookupOrdinal(br_image* img, br_uint_32 ordinal) {
168
    LOG_TRACE("(%p, %d)", img, ordinal);
169
 
170
    if (img->type == 3) {
171
        return HostImageLookupOrdinal(img->type_pointer, ordinal);
172
    }
173
    if (img->n_functions < (ordinal - img->ordinal_base)) {
174
        return NULL;
175
    }
176
    return img->functions[ordinal - img->ordinal_base];
177
}
178
 
179
// IDA: void __cdecl BrImageDereference(br_image *image)
180
void BrImageDereference(br_image* image) {
181
    LOG_TRACE("(%p)", image);
182
 
183
    image->ref_count--;
184
 
185
    if (image->ref_count <= 0) {
186
        switch (image->type) {
187
        case 2:
188
            break;
189
        case 3:
190
            HostImageUnload(image->type_pointer);
191
            // fall through
192
        default:
193
            BrRemove(&image->node);
194
            BrResFree(image);
195
        }
196
 
197
    }
198
}
199
 
200
// IDA: void __cdecl BrImageFree(br_image *image)
201
void BrImageFree(br_image* image) {
202
    int i;
203
    LOG_TRACE("(%p)", image);
204
 
205
    for (i = 0; i < image->n_imports; i++) {
206
        BrImageDereference(image->imports[i]);
207
    }
208
}
209
 
210
// IDA: void __cdecl _BrImageFree(void *res, br_uint_8 res_class, br_size_t size)
211
void _BrImageFree(void* res, br_uint_8 res_class, br_size_t size) {
212
    LOG_TRACE("(%p, %d, %d)", res, res_class, size);
213
 
214
    BrImageFree(res);
215
}