-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.java
527 lines (432 loc) · 13.9 KB
/
Library.java
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* Created on Nov 22, 2006
* Nov 22, 2006 3:52:36 AM
*/
/*
*
* This class deals with the Library segments.
*
* For library files, the first OMF Segment MUST be
* a of type LIBDICT (0x08). Orca Makelib gives it a
* name of "LIBRARY", but that's not necessary.
*
* The LIBRARY segment contains 3 LCONST records, each
* of which is a repeating record (1 & 3 are variable length)
*
* 1) filename information. Format is :
* [
* file_number (16 bit)
* file_name (pstring)
* ]
*
* 2) offset information. Format is :
* [
* name_offset (32 bit, offset into record 3)
* file_number (16 bit, matches record 1)
* private_flag (16 bit)
* data_offset (32 bit, absolute offset into the file to the start
* of this segment.)
* ]
*
* 3) symbol name information. Format is :
* [
* pstring symbol_name
* ]
*
* strings are always pstrings (ie, LABLEN will always be ignored)
*
* each file added to a library is given a non-0 number which is used
* to keep track of the symbol/segments and the file they originally came
* from. record 3 is sorted alphabetically, which may or may not be
* of significance.
*
*/
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import omf.*;
/*
* TODO -- due to entry records, etc, 1 OMF segment may be
* referenced under multiple names... we need a nie method to only
* load each segment once... ie store file position (offset), read
* a segment, then map it to the appropriate symbols.
*/
public class Library
{
Library()
{
FileList = new ArrayList<FileRec>();
FileList.add(0, null); // prevent 0 from being used.
SymbolList = new ArrayList<SymbolRec>();
fDirty = false;
}
Library(File f)
{
this();
if (!f.exists()) return;
try
{
FileInputStream io = new FileInputStream(f);
byte[] buffer;
buffer = new byte[io.available()];
io.read(buffer);
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
OMF_Segment omf = new OMF_Segment(bais);
// TODO -- verify if mark/reset work or not.
if (omf.Kind() == OMF.KIND_LIBDICT)
LoadDictionary(bais, omf);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private boolean LoadDictionary(InputStream io, OMF_Segment omf)
{
ListIterator<OMF_Opcode >iter = omf.Opcodes();
int i = 0;
boolean error = false;
byte[] offset_data = null;
for( ; iter.hasNext(); )
{
OMF_Opcode op = iter.next();
if (op instanceof OMF_Eof) break;
if (op instanceof OMF_Const)
{
byte[] data = ((OMF_Const)op).Data();
int j = 0;
switch (i++)
{
// files.
case 0:
while (j < data.length)
{
int num = read16(data, j);
j += 2;
int len = read8(data, j);
j++;
String s = new String(data, j, len);
j += len;
// may be out of order.
for (int k = FileList.size(); k <= num; k++)
{
FileList.add(k, null);
}
FileRec f = new FileRec(s);
f.FileNumber = num;
FileList.set(num, f);
}
break;
case 1:
offset_data = data;
break;
case 2:
// now we can processe the offset_data from above.
while (j < offset_data.length)
{
SymbolRec s = new SymbolRec();
int offset = read32(offset_data, j);
j += 4;
int k = read8(data, offset);
s.SymbolName = new String(data, offset + 1, k);
s.FileNumber = read16(offset_data, j);
j += 2;
FileRec f = FileList.get(s.FileNumber);
s.FileName = f.FileName;
s.Private = read16(offset_data, j) != 0;
j += 2;
s.Offset = read32(offset_data, j);
j += 4;
f.Symbols.add(s);
SymbolList.add(s);
}
break;
default:
error = true;
}
}
else error = true;
}
// now load the actual segments.
for (SymbolRec s : SymbolList)
{
try
{
io.reset();
io.skip(s.Offset);
s.Segment = new OMF_Segment(io);
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
if (s.Segment == null)
return false;
}
return !error;
}
SymbolRec FindSymbol(String name)
{
return FindSymbol(name, 0);
}
SymbolRec FindSymbol(String name, int fnum)
{
Iterator<SymbolRec> iter;
if (fnum != 0)
{
FileRec f = FileList.get(fnum);
iter = f.Symbols.iterator();
}
else
{
iter = SymbolList.iterator();
}
for (; iter.hasNext(); )
{
SymbolRec s = iter.next();
if (s.SymbolName != name) continue;
if (s.Private && (s.FileNumber != fnum))
return null;
return s;
}
return null;
}
int NewFile(String filename)
{
FileRec r = new FileRec(filename);
FileList.add(r);
r.FileNumber = FileList.size() - 1;
return r.FileNumber;
}
private boolean AddSymbol(FileRec f, OMF_Segment segment)
{
// TODO -- check if name is unique?
SymbolRec s = new SymbolRec();
s.SymbolName = segment.SegmentName();
s.FileNumber = f.FileNumber;
s.FileName = f.FileName;
s.Private = segment.Private();
s.Offset = 0; // TODO -???
s.Segment = segment;
f.Symbols.add(s);
SymbolList.add(s);
fDirty = true;
return true;
}
boolean AddSymbol(String filename, OMF_Segment segment)
{
FileRec f = FindFileRec(filename);
if (f == null)
{
f = new FileRec(filename);
FileList.add(f);
f.FileNumber = FileList.size() - 1;
}
return AddSymbol(f, segment);
}
boolean AddSymbol(int fnum, OMF_Segment segment)
{
FileRec f = FileList.get(fnum);
if (f == null) return false;
return AddSymbol(f, segment);
}
/*
* remove the file....
*/
boolean RemoveFile(String filename)
{
FileRec f = FindFileRec(filename);
if (f == null) return false;
int num = f.FileNumber;
FileList.set(f.FileNumber, null);
for (int i = SymbolList.size(); i > 0; i--)
{
SymbolRec s = SymbolList.get(i - 1);
if (s.FileNumber == num)
{
FileList.remove(i - 1);
}
}
fDirty = true;
return true;
}
/*
* extract the file ... does not remove from the library,
*/
boolean ExtractFile(String filename, FileOutputStream out)
{
FileRec f = FindFileRec(filename);
if (f == null) return false;
for (SymbolRec s : f.Symbols)
{
if (!s.Segment.Save(out))
return false;
}
return true;
}
boolean AddFile(String filename, File in)
{
return true;
}
void Save(File Outfile)
{
Optimize();
/*
* TODO -- sort the filelist alphabetically, create header
* segment with 3 LCONST records, then save each segment.
* after saving, overwrite the const records with the offset
* values.
*
* Alternate approach:
* write the segments to a temporary file, keeping track of
* segment lengths for offset information. Then write the
* Library header to a new file and merge the temp. file to it.
*/
}
/*
* in case of deletion, renumber any files.
*/
private void Optimize()
{
boolean delta = false;
for (int i = FileList.size() - 1; i > 0; i--)
{
FileRec f = FileList.get(i);
if (f == null)
{
FileList.remove(i);
delta = true;
}
}
if (delta)
{
for (int i = FileList.size() - 1; i > 0; i--)
{
FileRec f = FileList.get(i);
f.FileNumber = i;
// renumber symbols as well.
for (SymbolRec s : f.Symbols)
{
s.FileNumber = i;
}
}
}
}
private FileRec FindFileRec(String name)
{
if (name == null || name.length() == 0) return null;
for (FileRec f : FileList)
{
if (f == null) continue;
if (f.FileName.equals(name)) return f;
}
return null;
}
// I hate the lack of an unsigned type.
private static int read32(byte[] bytes, int offset)
{
int x1 = bytes[offset++] & 0x00ff;
int x2 = bytes[offset++] & 0x00ff;
int x3 = bytes[offset++] & 0x00ff;
int x4 = bytes[offset++] & 0x00ff;
return x1 + (x2 << 8) + (x3 << 16) + (x4 << 24);
}
private static int read16(byte[] bytes, int offset)
{
int x1 = bytes[offset++] & 0x00ff;
int x2 = bytes[offset++] & 0x00ff;
return x1 + (x2 << 8);
}
private static int read8(byte[] bytes, int offset)
{
return bytes[offset] & 0x00ff;
}
public Iterator<SymbolRec> Symbols()
{
return SymbolList.iterator();
}
public Iterator<FileRec> Files()
{
return FileList.iterator();
}
/*
public Iterator<SymbolRec> iterator_()
{
class SRIterator implements Iterator<SymbolRec>
{
ArrayList<FileRec> fList;
private int fIndex;
private Iterator<SymbolRec> fIter;
private boolean fEof;
public SRIterator(ArrayList<FileRec> files)
{
fList = files;
fIndex = 0;
fIter = null;
fEof = false;
}
public boolean hasNext()
{
if (fEof) return true;
for (;;)
{
if (fIter == null)
{
fIndex++;
if (fIndex >= fList.size())
{
fEof = true;
return false;
}
FileRec f = fList.get(fIndex);
if (f == null) continue;
if (f.SymolList.size() == 0) continue;
fIter = f.SymbolList.iterator();
}
if (fIter.hasNext()) return true;
}
}
public SymbolRec next()
{
if (!hasNext()) return null;
return fIter.next();
}
public void remove()
{
if (fIter != null)
fIter.remove();
}
}
return new SRIterator(FileList);
}
*/
class SymbolRec
{
String SymbolName;
String FileName;
int FileNumber;
boolean Private;
int Offset;
OMF_Segment Segment;
}
class FileRec
{
String FileName;
int FileNumber;
ArrayList<SymbolRec> Symbols;
FileRec(String name)
{
Symbols = new ArrayList<SymbolRec>();
FileName = name;
}
}
private ArrayList<FileRec> FileList;
private ArrayList<SymbolRec> SymbolList;
private boolean fDirty;
}