-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.c
354 lines (309 loc) · 10.5 KB
/
lib.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
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
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <unicode/ustring.h>
#include "lib.h"
#include "pbl_types.h"
#include "pool_alloc.h"
#include "debug.h"
struct library_private;
// self contained, no dependance on struct ent_(a|u)
struct lib_entry_private{
struct lib_entry pub;
struct library_private *lib;
struct lib_entry_private *next;
uint32_t start_offset;
uint16_t comment_len;
struct dat *dat;
uint16_t block_offset;
uint32_t remaining;
};
struct directory{
struct directory *left;
struct directory *right;
uint32_t offset;
struct nod *nod;
const char *first;
const char *last;
struct lib_entry_private *first_ent;
};
struct library_private{
struct library pub;
struct pool *pool;
int fd;
uint32_t scc_info;
uint32_t scc_length;
struct directory root;
};
struct library *lib_open(const char *filename){
int fd = open(filename, O_RDONLY);
DEBUGF(LIB, "open(%s, O_RDONLY) = %d", filename, fd);
if (fd<0)
return NULL;
off_t header_offset = -1;
off_t len = lseek(fd, 0, SEEK_END);
assert(len>=0);
DEBUGF(LIB, "len %04x", (unsigned)len);
len = (len & ~(BLOCK_SIZE -1)) - BLOCK_SIZE;
unsigned i;
// look at trailing blocks until we find a TRL* or other valid block header
for (i=0;i<20;i++){
lseek(fd, len, SEEK_SET);
struct dat dat;
read(fd, &dat, sizeof(dat));
DEBUGF(LIB, "%04x @%04x", *(uint32_t*)&dat.type, (unsigned)len);
if (strncmp(dat.type, TRL, 4)==0){
header_offset = dat.next_offset;
break;
}
// any other kind of block, stop looking
if (dat.type[3]=='*'
&& (strncmp(dat.type, DAT, 4)==0
|| strncmp(dat.type, NOD, 4)==0
|| strncmp(dat.type, FRE, 4)==0)){
header_offset = 0;
break;
}
len-=BLOCK_SIZE;
}
// no block header?
assert(header_offset>=0);
lseek(fd, header_offset, SEEK_SET);
union{
struct file_header_a ansi;
struct file_header_u unicode;
}header;
// read the HDR block
read(fd, &header, sizeof(header));
assert(strncmp(header.ansi.type, HDR, 4)==0);
uint8_t unicode;
if (strncmp(header.ansi.pb, "PowerBuilder", 14)==0){
unicode=0;
}else{
UErrorCode status = U_ZERO_ERROR;
char PB[14];
u_strToUTF8(PB, sizeof PB, NULL, header.unicode.pb, -1, &status);
assert(!U_FAILURE(status));
assert(strncmp(PB, "PowerBuilder", 14)==0);
unicode=1;
}
// use a single memory pool for the lifetime of the library struct.
// se we can duplicate string buffers, and quickly throw them all away when we're done
struct pool *pool = pool_create();
struct library_private *lib = pool_alloc_type(pool, struct library_private);
memset(&lib->root, 0, sizeof(lib->root));
lib->pool = pool;
lib->fd = fd;
lib->pub.unicode = unicode;
if (unicode){
lib->pub.comment = pool_dup_u(pool, header.unicode.comment);
lib->pub.version = pool_dup_u(pool, header.unicode.version);
lib->pub.timestamp = header.unicode.timestamp;
lib->pub.filetype = header.unicode.filetype;
lib->scc_info = header.unicode.scc_info;
lib->scc_length = header.unicode.scc_length;
lib->root.offset = header_offset+0x600;
}else{
lib->pub.comment = pool_dup(pool, header.ansi.comment);
lib->pub.version = pool_dup(pool, header.ansi.version);
lib->pub.timestamp = header.ansi.timestamp;
lib->pub.filetype = header.ansi.filetype;
lib->scc_info = header.ansi.scc_info;
lib->scc_length = header.ansi.scc_length;
lib->root.offset = header_offset+0x400;
}
lib->pub.filename = pool_dup(pool, filename);
return (struct library *)lib;
}
void lib_close(struct library *library){
assert(library);
struct library_private *lib = (struct library_private *)library;
close(lib->fd);
pool_release(lib->pool);
}
static void read_dir(struct library_private *lib, struct directory *dir){
if (dir->nod)
return;
dir->nod = pool_alloc_type(lib->pool, struct nod);
lseek(lib->fd, dir->offset, SEEK_SET);
read(lib->fd, dir->nod, sizeof(struct nod));
assert(strncmp(dir->nod->type, NOD, 4)==0);
if (dir->nod->no_entries){
if (lib->pub.unicode){
dir->first = pool_dup_u(lib->pool, (const UChar *)&dir->nod->raw[dir->nod->first_name]);
dir->last = pool_dup_u(lib->pool, (const UChar *)&dir->nod->raw[dir->nod->last_name]);
}else{
dir->first = (const char *)&dir->nod->raw[dir->nod->first_name];
dir->last = (const char *)&dir->nod->raw[dir->nod->last_name];
}
}
DEBUGF(LIB, "Read dir @%x, %u entries (first %s, last %s)",
dir->offset, dir->nod->no_entries, dir->first, dir->last);
}
static struct directory * dir_left(struct library_private *lib, struct directory *dir){
read_dir(lib, dir);
if (!dir->left && dir->nod->left_offset){
dir->left = pool_alloc_type(lib->pool, struct directory);
memset(dir->left, 0, sizeof(struct directory));
dir->left->offset = dir->nod->left_offset;
DEBUGF(LIB, "Init left @%x",dir->left->offset);
}
return dir->left;
}
static struct directory * dir_right(struct library_private *lib, struct directory *dir){
read_dir(lib, dir);
if (!dir->right && dir->nod->right_offset){
dir->right = pool_alloc_type(lib->pool, struct directory);
memset(dir->right, 0, sizeof(struct directory));
dir->right->offset = dir->nod->right_offset;
DEBUGF(LIB, "Init right @%x",dir->right->offset);
}
return dir->right;
}
static void read_ents(struct library_private *lib, struct directory *dir){
read_dir(lib, dir);
if (!dir->nod->no_entries || dir->first_ent)
return;
struct lib_entry_private **ptr = &dir->first_ent;
uint8_t *data = dir->nod->ent_start;
unsigned index;
for(index=0; index < dir->nod->no_entries; index++){
struct lib_entry_private *entry = (*ptr) = pool_alloc_type(lib->pool, struct lib_entry_private);
memset(entry, 0, sizeof(struct lib_entry_private));
ptr = &entry->next;
entry->lib = lib;
if (lib->pub.unicode){
struct ent_u *ent = (struct ent_u *)data;
assert(strncmp(ent->type, ENT, 4)==0);
//DUMP(data, sizeof(struct ent_u) + ent->name_len);
data += sizeof(struct ent_u);
entry->pub.name=pool_dupn_u(lib->pool, (UChar *)data, ent->name_len -2);
entry->pub.length=ent->length;
entry->pub.timestamp=ent->timestamp;
entry->start_offset=ent->first_block;
entry->comment_len=ent->comment_len;
data += ent->name_len;
}else{
struct ent_a *ent = (struct ent_a *)data;
assert(strncmp(ent->type, ENT, 4)==0);
data += sizeof(struct ent_a);
//DUMP(data, sizeof(struct ent_a) + ent->name_len);
entry->pub.name=(char *)data;
entry->pub.length=ent->length;
entry->pub.timestamp=ent->timestamp;
entry->start_offset=ent->first_block;
entry->comment_len=ent->comment_len;
data += ent->name_len;
}
//DEBUGF(LIB, "Parsed ent %s @%lx", entry->pub.name, (data - dir->nod->raw) + dir->offset);
assert(data - dir->nod->raw < (long)sizeof(*dir->nod));
}
DEBUGF(LIB, "Read ent's for dir @%x",dir->offset);
}
static void read_ent_comment(struct lib_entry_private *ent){
if (!ent->comment_len || ent->pub.comment)
return;
lseek(ent->lib->fd, ent->start_offset, SEEK_SET);
struct dat dat;
read(ent->lib->fd, &dat, sizeof(dat));
assert(strncmp(dat.type, DAT, 4)==0);
if (ent->lib->pub.unicode){
ent->pub.comment = pool_dupn_u(ent->lib->pool, (UChar*)dat.data, ent->comment_len);
}else{
ent->pub.comment = pool_dupn(ent->lib->pool, (char*)dat.data, ent->comment_len);
}
}
static void dir_enum(struct library_private *lib, struct directory *dir, entry_callback callback, void *context){
if (!dir)
return;
dir_enum(lib, dir_left(lib, dir), callback, context);
read_ents(lib, dir);
struct lib_entry_private *entry = dir->first_ent;
while(entry){
read_ent_comment(entry);
callback((struct lib_entry *)entry, context);
entry = entry->next;
}
dir_enum(lib, dir_right(lib, dir), callback, context);
}
void lib_enumerate(struct library *library, entry_callback callback, void *context){
struct library_private *lib = (struct library_private *)library;
dir_enum(lib, &lib->root, callback, context);
}
struct lib_entry *lib_find(struct library *library, const char *entry_name){
struct library_private *lib = (struct library_private *)library;
struct directory *dir = &lib->root;
while(dir){
read_dir(lib, dir);
if (!dir->nod->no_entries)
break;
if (strcmp(entry_name, dir->first)<0){
dir = dir_left(lib, dir);
}else if(strcmp(entry_name, dir->last)>0){
dir = dir_right(lib, dir);
}else{
read_ents(lib, dir);
struct lib_entry_private *entry = dir->first_ent;
while(entry){
if (strcmp(entry->pub.name, entry_name)==0){
DEBUGF(LIB, "Found %s", entry_name);
read_ent_comment(entry);
return (struct lib_entry *)entry;
}
entry = entry->next;
}
DEBUGF(LIB, "%s NOT FOUND", entry_name);
return NULL;
}
}
DEBUGF(LIB, "%s NOT FOUND", entry_name);
return NULL;
}
size_t lib_entry_read(struct lib_entry *entry, uint8_t *buffer, size_t len){
assert(entry);
assert(buffer);
struct lib_entry_private *ent = (struct lib_entry_private *)entry;
if (!ent->dat){
if (!ent->pub.length)
return 0;
ent->dat = pool_alloc_type(ent->lib->pool, struct dat);
assert(ent->dat);
// read the first block
assert(ent->start_offset);
lseek(ent->lib->fd, ent->start_offset, SEEK_SET);
read(ent->lib->fd, ent->dat, sizeof(struct dat));
ent->remaining = ent->pub.length;
assert(strncmp(ent->dat->type, DAT, 4)==0);
assert(ent->remaining >= ent->dat->length);
// skip the file comment
ent->block_offset = ent->comment_len;
ent->remaining -= ent->dat->length;
}
size_t bytes_read = 0;
while(bytes_read < len){
if (ent->block_offset < ent->dat->length){
// copy data bytes from previously read data
uint16_t remain = ent->dat->length - ent->block_offset;
if (remain > (len - bytes_read))
remain = len - bytes_read;
memcpy(&buffer[bytes_read], &ent->dat->data[ent->block_offset], remain);
ent->block_offset += remain;
bytes_read += remain;
} else if(!ent->remaining) {
break;
} else {
// read more data
assert(ent->dat->next_offset);
lseek(ent->lib->fd, ent->dat->next_offset, SEEK_SET);
read(ent->lib->fd, ent->dat, sizeof(struct dat));
assert(strncmp(ent->dat->type, DAT, 4)==0);
assert(ent->remaining >= ent->dat->length);
ent->block_offset = 0;
ent->remaining -= ent->dat->length;
}
}
DUMP(RAWREAD, buffer, bytes_read);
return bytes_read;
}