-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathelpreader.c
249 lines (226 loc) · 5.53 KB
/
elpreader.c
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
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "siphash.h"
/*
TAG:
DWORD ELP1
DWORD baseversion
DWORD lastversion / HEADER_ADLER32
DWORD HEADER_CSIZE
DWORD HEADER_N
DWORD BODY_SIZE
FILE(s):
FILEINFO(s):
BYTES[8] NAMEHASH
DWORD OFFSET , FFFFFFFF(add SALT)
DWORD CSIZE , SALT32 , 0 (patch)
DWORD ADLER32 , (patch index)
*/
#define HEADER_SIZE 24
#define INFO_SIZE 20
#define HASH_SIZE 8
#define SALT_OFFSET 0xffffffff
#define PATCH_OFFSET 0xfffffffe
struct elp_file {
uint8_t hash[HASH_SIZE];
uint32_t offset;
uint32_t csize;
};
struct elp_package {
FILE *f;
int n;
struct elp_file * ef;
};
static uint32_t
read_dword(FILE *f) {
uint8_t v[4] = { 0,0,0,0 };
fread(v,1,4,f);
return v[0] | v[1] << 8 | v[2] << 16 | v[3] << 24;
}
static int
elp_close(lua_State *L) {
struct elp_package *elp = luaL_checkudata(L, 1, "elpack");
if (elp->f == NULL)
return 0;
fclose(elp->f);
elp->f = NULL;
free(elp->ef);
elp->ef = NULL;
return 0;
}
static uint32_t
u8tole32(const char *ptr) {
const uint8_t * p = (const uint8_t *)ptr;
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}
static int
elp_init(lua_State *L) {
struct elp_package *elp = luaL_checkudata(L, 1, "elpack");
if (elp->f == NULL || elp->ef != NULL) {
return luaL_error(L, "Init failed");
}
size_t sz = 0;
const char * header = luaL_checklstring(L, 2, &sz);
if (sz != elp->n * INFO_SIZE) {
return luaL_error(L, "Invalid header");
}
int i;
elp->ef = malloc(elp->n * sizeof(struct elp_file));
for (i=0;i<elp->n;i++) {
memcpy(elp->ef[i].hash, header, HASH_SIZE);
if (i>0) {
if (memcmp(elp->ef[i].hash, elp->ef[i-1].hash, HASH_SIZE) <= 0) {
free(elp->ef);
elp->ef = NULL;
return luaL_error(L, "The hash in header is not ascending");
}
}
elp->ef[i].offset = u8tole32(header + HASH_SIZE);
elp->ef[i].csize = u8tole32(header + HASH_SIZE + 4);
if (elp->ef[i].offset == SALT_OFFSET && elp->ef[i].csize == 0) {
elp->ef[i].offset = PATCH_OFFSET;
elp->ef[i].csize = u8tole32(header + HASH_SIZE + 8);
}
header += INFO_SIZE;
}
return 0;
}
static struct elp_file *
lookup_hash(struct elp_package *elp, uint8_t hash[HASH_SIZE]) {
int begin = 0;
int end = elp->n;
while (begin < end) {
int middle = (end + begin) / 2;
struct elp_file * ef = &elp->ef[middle];
int c = memcmp(hash, ef->hash, HASH_SIZE);
if (c == 0) {
return ef;
}
if (c > 0) {
begin = middle + 1;
} else {
end = middle;
}
}
return NULL;
}
static int
elp_load(lua_State *L) {
struct elp_package *elp = luaL_checkudata(L, 1, "elpack");
if (elp->f == NULL || elp->ef == NULL) {
return luaL_error(L, "Init first");
}
size_t sz = 0;
const char * name = luaL_checklstring(L, 2, &sz);
struct elp_file *ef = NULL;
if (lua_type(L, 3) == LUA_TNUMBER) {
int index = lua_tointeger(L, 3);
if (index < 0 || index >= elp->n) {
return luaL_error(L, "Invalid index %d", index);
}
ef = &elp->ef[index];
} else {
uint8_t hash[HASH_SIZE];
uint8_t k[16];
memset(k, 0, 16);
siphash(hash, (const uint8_t *)name, sz, k);
ef = lookup_hash(elp, hash);
if (ef == NULL)
return 0;
if (ef->offset == SALT_OFFSET) {
// try again, add salt.
uint8_t salt[4];
salt[0] = ef->csize & 0xff;
salt[1] = (ef->csize >> 8) & 0xff;
salt[2] = (ef->csize >> 16) & 0xff;
salt[3] = (ef->csize >> 24) & 0xff;
siphashsalt(k, (const char *)salt, 4);
siphash(hash, (const uint8_t *)name, sz, k);
ef = lookup_hash(elp, hash);
if (ef == NULL) {
return 0;
}
if (ef->offset == SALT_OFFSET) {
return 0;
}
}
}
if (ef->offset == PATCH_OFFSET) {
lua_pushinteger(L, ef->csize);
return 1;
}
if (fseek(elp->f, ef->offset + HEADER_SIZE, SEEK_SET) != 0) {
return luaL_error(L, "Can't seek %s", name);
}
luaL_Buffer buff;
char * tmp = luaL_buffinitsize(L, &buff, ef->csize);
int n = fread(tmp, 1, ef->csize, elp->f);
if (n != ef->csize) {
return luaL_error(L, "Can't read %s", name);
}
luaL_addsize(&buff, ef->csize);
luaL_pushresult(&buff);
return 1;
}
static int
elp_open(lua_State *L) {
const char * filename = luaL_checkstring(L,1); // filename
FILE *f = fopen(filename, "rb");
if (f == NULL)
return luaL_error(L, "Can't open %s", filename);
uint8_t tag[4] = { 0,0,0,0 };
fread(tag, 1, 4, f);
if (memcmp(tag, "ELP1",4) != 0)
return luaL_error(L, "Invalid elp file %s", filename);
uint32_t v1 = read_dword(f);
uint32_t v2 = read_dword(f);
uint32_t header_size = read_dword(f);
uint32_t header_n = read_dword(f);
uint32_t body_size = read_dword(f);
if (fseek(f, body_size + HEADER_SIZE, SEEK_SET) != 0) {
return luaL_error(L, "Damaged file %s", filename);
}
luaL_Buffer buff;
char *tmp = luaL_buffinitsize(L, &buff, header_size);
int n = fread(tmp, 1, header_size, f);
if (n != header_size) {
fclose(f);
return luaL_error(L, "Damaged file header %s", filename);
}
luaL_addsize(&buff, header_size);
luaL_pushresult(&buff);
struct elp_package *elp = lua_newuserdata(L, sizeof(*elp));
elp->f = f;
elp->n = header_n;
elp->ef = NULL;
if (luaL_newmetatable(L, "elpack")) {
lua_pushcfunction(L, elp_close);
lua_setfield(L, -2, "__gc");
luaL_Reg l[] = {
{ "close", elp_close },
{ "init", elp_init },
{ "load", elp_load },
{ NULL, NULL },
};
luaL_newlib(L, l);
lua_setfield(L, -2, "__index");
}
lua_setmetatable(L, -2);
lua_insert(L, -2);
lua_pushinteger(L, v1);
lua_pushinteger(L, v2);
return 4;
}
void
lua_reader(lua_State *L) {
luaL_Reg l[] = {
{ "open", elp_open },
{ NULL, NULL },
};
luaL_setfuncs(L, l, 0);
}