@isopodlabs/binary_libs 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/clr.js DELETED
@@ -1,664 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.CLR = void 0;
27
- const binary = __importStar(require("@isopodlabs/binary"));
28
- const pe = __importStar(require("./pe"));
29
- //-----------------------------------------------------------------------------
30
- // CLR
31
- //-----------------------------------------------------------------------------
32
- const HEAP = {
33
- String: 0,
34
- GUID: 1,
35
- Blob: 2,
36
- UserString: 3,
37
- };
38
- const CLR_FLAGS = {
39
- FLAGS_ILONLY: 0x00000001,
40
- FLAGS_32BITREQUIRED: 0x00000002,
41
- FLAGS_IL_LIBRARY: 0x00000004,
42
- FLAGS_STRONGNAMESIGNED: 0x00000008,
43
- FLAGS_NATIVE_ENTRYPOINT: 0x00000010,
44
- FLAGS_TRACKDEBUGDATA: 0x00010000,
45
- };
46
- const CLR_HEADER = {
47
- cb: binary.UINT32_LE,
48
- MajorRuntimeVersion: binary.UINT16_LE,
49
- MinorRuntimeVersion: binary.UINT16_LE,
50
- MetaData: pe.DATA_DIRECTORY,
51
- Flags: binary.UINT32_LE,
52
- EntryPoint: binary.UINT32_LE,
53
- Resources: pe.DATA_DIRECTORY,
54
- StrongNameSignature: pe.DATA_DIRECTORY,
55
- CodeManagerTable: pe.DATA_DIRECTORY,
56
- VTableFixups: pe.DATA_DIRECTORY,
57
- ExportAddressTableJumps: pe.DATA_DIRECTORY,
58
- //ManagedNativeHeader: pe.DATA_DIRECTORY,
59
- };
60
- const STREAM_HDR = {
61
- Offset: binary.UINT32_LE,
62
- Size: binary.UINT32_LE,
63
- Name: binary.NullTerminatedStringType,
64
- unused: binary.AlignType(4),
65
- };
66
- const METADATA_ROOT = {
67
- Signature: binary.UINT32_LE,
68
- MajorVersion: binary.UINT16_LE,
69
- MinorVersion: binary.UINT16_LE,
70
- Reserved: binary.UINT32_LE,
71
- Version: binary.StringType(binary.UINT32_LE, 'utf8', true),
72
- unknown: binary.UINT16_LE,
73
- Streams: binary.ArrayType(binary.UINT16_LE, STREAM_HDR)
74
- };
75
- const CLR_TABLES = {
76
- Reserved: binary.UINT32_LE,
77
- MajorVersion: binary.UINT8,
78
- MinorVersion: binary.UINT8,
79
- HeapSizes: binary.UINT8,
80
- Reserved2: binary.UINT8,
81
- Valid: binary.UINT64_LE,
82
- Sorted: binary.UINT64_LE, // Bit vector of sorted tables.
83
- };
84
- var TABLE;
85
- (function (TABLE) {
86
- TABLE[TABLE["Module"] = 0] = "Module";
87
- TABLE[TABLE["TypeRef"] = 1] = "TypeRef";
88
- TABLE[TABLE["TypeDef"] = 2] = "TypeDef";
89
- // Unused = 0x03,
90
- TABLE[TABLE["Field"] = 4] = "Field";
91
- // Unused = 0x05,
92
- TABLE[TABLE["MethodDef"] = 6] = "MethodDef";
93
- // Unused = 0x07,
94
- TABLE[TABLE["Param"] = 8] = "Param";
95
- TABLE[TABLE["InterfaceImpl"] = 9] = "InterfaceImpl";
96
- TABLE[TABLE["MemberRef"] = 10] = "MemberRef";
97
- TABLE[TABLE["Constant"] = 11] = "Constant";
98
- TABLE[TABLE["CustomAttribute"] = 12] = "CustomAttribute";
99
- TABLE[TABLE["FieldMarshal"] = 13] = "FieldMarshal";
100
- TABLE[TABLE["DeclSecurity"] = 14] = "DeclSecurity";
101
- TABLE[TABLE["ClassLayout"] = 15] = "ClassLayout";
102
- TABLE[TABLE["FieldLayout"] = 16] = "FieldLayout";
103
- TABLE[TABLE["StandAloneSig"] = 17] = "StandAloneSig";
104
- TABLE[TABLE["EventMap"] = 18] = "EventMap";
105
- // Unused = 0x13,
106
- TABLE[TABLE["Event"] = 20] = "Event";
107
- TABLE[TABLE["PropertyMap"] = 21] = "PropertyMap";
108
- // Unused = 0x16,
109
- TABLE[TABLE["Property"] = 23] = "Property";
110
- TABLE[TABLE["MethodSemantics"] = 24] = "MethodSemantics";
111
- TABLE[TABLE["MethodImpl"] = 25] = "MethodImpl";
112
- TABLE[TABLE["ModuleRef"] = 26] = "ModuleRef";
113
- TABLE[TABLE["TypeSpec"] = 27] = "TypeSpec";
114
- TABLE[TABLE["ImplMap"] = 28] = "ImplMap";
115
- TABLE[TABLE["FieldRVA"] = 29] = "FieldRVA";
116
- // Unused = 0x1e,
117
- // Unused = 0x1f,
118
- TABLE[TABLE["Assembly"] = 32] = "Assembly";
119
- TABLE[TABLE["AssemblyProcessor"] = 33] = "AssemblyProcessor";
120
- TABLE[TABLE["AssemblyOS"] = 34] = "AssemblyOS";
121
- TABLE[TABLE["AssemblyRef"] = 35] = "AssemblyRef";
122
- TABLE[TABLE["AssemblyRefProcessor"] = 36] = "AssemblyRefProcessor";
123
- TABLE[TABLE["AssemblyRefOS"] = 37] = "AssemblyRefOS";
124
- TABLE[TABLE["File"] = 38] = "File";
125
- TABLE[TABLE["ExportedType"] = 39] = "ExportedType";
126
- TABLE[TABLE["ManifestResource"] = 40] = "ManifestResource";
127
- TABLE[TABLE["NestedClass"] = 41] = "NestedClass";
128
- TABLE[TABLE["GenericParam"] = 42] = "GenericParam";
129
- TABLE[TABLE["MethodSpec"] = 43] = "MethodSpec";
130
- TABLE[TABLE["GenericParamConstraint"] = 44] = "GenericParamConstraint";
131
- })(TABLE || (TABLE = {}));
132
- function bytesToGuid(bytes) {
133
- // Convert each byte to a two-digit hexadecimal string
134
- const hexArray = Array.from(bytes, byte => ('0' + (byte & 0xFF).toString(16)).slice(-2));
135
- // Join the hex strings into the standard GUID format
136
- return hexArray.slice(0, 4).join('') + '-' +
137
- hexArray.slice(4, 6).join('') + '-' +
138
- hexArray.slice(6, 8).join('') + '-' +
139
- hexArray.slice(8, 10).join('') + '-' +
140
- hexArray.slice(10, 16).join('');
141
- }
142
- class clr_stream extends binary.stream {
143
- heaps;
144
- heap_sizes;
145
- table_counts;
146
- constructor(buffer, heaps, heap_sizes, table_counts) {
147
- super(buffer);
148
- this.heaps = heaps;
149
- this.heap_sizes = heap_sizes;
150
- this.table_counts = table_counts;
151
- }
152
- getOffset(big) {
153
- return (big ? binary.UINT32_LE : binary.UINT16_LE).get(this);
154
- }
155
- getHeap(heap) {
156
- return this.heaps[heap].subarray(this.getOffset(!!(this.heap_sizes & (1 << heap))));
157
- }
158
- getIndex(table) {
159
- return this.getOffset(this.table_counts[table] > 0xffff);
160
- }
161
- getCodedIndex(B, trans) {
162
- const thresh = 0xffff >> B;
163
- for (const i of trans) {
164
- if (this.table_counts[i] > thresh)
165
- return binary.UINT32_LE.get(this);
166
- }
167
- return binary.UINT16_LE.get(this);
168
- }
169
- getString() {
170
- const mem = this.getHeap(HEAP.String);
171
- const n = mem.indexOf(0);
172
- return String.fromCharCode(...mem.subarray(0, n));
173
- }
174
- getGUID() {
175
- return bytesToGuid(this.getHeap(HEAP.GUID));
176
- }
177
- getBlob() {
178
- return this.getHeap(HEAP.Blob);
179
- }
180
- }
181
- class clr_dummy extends binary.dummy {
182
- heap_sizes;
183
- table_counts;
184
- constructor(heap_sizes, table_counts) {
185
- super();
186
- this.heap_sizes = heap_sizes;
187
- this.table_counts = table_counts;
188
- }
189
- getOffset(big) {
190
- return (big ? binary.UINT32_LE : binary.UINT16_LE).get(this);
191
- }
192
- getHeap(heap) {
193
- return this.getOffset(!!(this.heap_sizes & (1 << heap)));
194
- }
195
- getIndex(table) {
196
- return this.getOffset(this.table_counts[table] > 0xffff);
197
- }
198
- getCodedIndex(B, trans) {
199
- const thresh = 0xffff >> B;
200
- for (const i of trans) {
201
- if (this.table_counts[i] > thresh)
202
- return binary.UINT32_LE.get(this);
203
- }
204
- return binary.UINT16_LE.get(this);
205
- }
206
- getString() { return this.getHeap(HEAP.String); }
207
- getGUID() { return this.getHeap(HEAP.GUID); }
208
- getBlob() { return this.getHeap(HEAP.Blob); }
209
- }
210
- const clr_String = {
211
- get(s) { return s.getString(); },
212
- put(s, v) { }
213
- };
214
- const clr_GUID = {
215
- get(s) { return s.getGUID(); },
216
- put(s, v) { }
217
- };
218
- const clr_Blob = {
219
- get(s) { return s.getBlob(); },
220
- put(s, v) { }
221
- };
222
- const Signature = clr_Blob;
223
- const CustomAttributeValue = clr_Blob;
224
- const clr_Code = binary.UINT32_LE;
225
- class Indexed {
226
- table;
227
- constructor(table) {
228
- this.table = table;
229
- }
230
- get(s) { return s.getIndex(this.table); }
231
- }
232
- class IndexedList extends Indexed {
233
- get(s) { return s.getIndex(this.table); }
234
- }
235
- class CodedIndex {
236
- trans;
237
- B;
238
- constructor(trans, B) {
239
- this.trans = trans;
240
- this.B = B;
241
- }
242
- get(s) { return s.getCodedIndex(this.B, this.trans); }
243
- }
244
- const TypeDefOrRef = new CodedIndex([TABLE.TypeDef, TABLE.TypeRef, TABLE.TypeSpec], 2);
245
- const HasConstant = new CodedIndex([TABLE.Field, TABLE.Param, TABLE.Property], 2);
246
- const HasCustomAttribute = new CodedIndex([
247
- TABLE.MethodDef, TABLE.Field, TABLE.TypeRef, TABLE.TypeDef, TABLE.Param, TABLE.InterfaceImpl, TABLE.MemberRef, TABLE.Module, TABLE.DeclSecurity, TABLE.Property, TABLE.Event, TABLE.StandAloneSig,
248
- TABLE.ModuleRef, TABLE.TypeSpec, TABLE.Assembly, TABLE.AssemblyRef, TABLE.File, TABLE.ExportedType, TABLE.ManifestResource, TABLE.GenericParam, TABLE.GenericParamConstraint, TABLE.MethodSpec,
249
- ], 5);
250
- const HasFieldMarshall = new CodedIndex([TABLE.Field, TABLE.Param], 1);
251
- const HasDeclSecurity = new CodedIndex([TABLE.TypeDef, TABLE.MethodDef, TABLE.Assembly], 2);
252
- const MemberRefParent = new CodedIndex([TABLE.TypeDef, TABLE.TypeRef, TABLE.ModuleRef, TABLE.MethodDef, TABLE.TypeSpec], 1);
253
- const HasSemantics = new CodedIndex([TABLE.Event, TABLE.Property], 1);
254
- const MethodDefOrRef = new CodedIndex([TABLE.MethodDef, TABLE.MemberRef], 1);
255
- const MemberForwarded = new CodedIndex([TABLE.Field, TABLE.MethodDef], 1);
256
- const Implementation = new CodedIndex([TABLE.File, TABLE.AssemblyRef, TABLE.ExportedType], 2);
257
- const CustomAttributeType = new CodedIndex([0, 0, TABLE.MethodDef, TABLE.MemberRef], 3);
258
- const TypeOrMethodDef = new CodedIndex([TABLE.TypeDef, TABLE.MethodDef], 1);
259
- const ResolutionScope = new CodedIndex([TABLE.Module, TABLE.ModuleRef, TABLE.AssemblyRef, TABLE.TypeRef], 2);
260
- const ENTRY_Module = {
261
- generation: binary.UINT16_LE,
262
- name: clr_String,
263
- mvid: clr_GUID,
264
- encid: clr_GUID,
265
- encbaseid: clr_GUID,
266
- };
267
- const ENTRY_TypeRef = {
268
- scope: ResolutionScope,
269
- name: clr_String,
270
- namespce: clr_String,
271
- };
272
- const ENTRY_TypeDef = {
273
- flags: binary.UINT32_LE,
274
- name: clr_String,
275
- namespce: clr_String,
276
- extends: TypeDefOrRef,
277
- fields: new IndexedList(TABLE.Field),
278
- methods: new IndexedList(TABLE.MethodDef),
279
- };
280
- const ENTRY_Field = {
281
- flags: binary.UINT16_LE,
282
- name: clr_String,
283
- signature: Signature,
284
- };
285
- const ENTRY_MethodDef = {
286
- code: clr_Code,
287
- implflags: binary.UINT16_LE,
288
- flags: binary.UINT16_LE,
289
- name: clr_String,
290
- signature: Signature,
291
- paramlist: new IndexedList(TABLE.Param),
292
- };
293
- const ENTRY_Param = {
294
- flags: binary.UINT16_LE,
295
- sequence: binary.UINT16_LE,
296
- name: clr_String,
297
- };
298
- const ENTRY_InterfaceImpl = {
299
- clss: new Indexed(TABLE.TypeDef),
300
- interfce: TypeDefOrRef,
301
- };
302
- const ENTRY_MemberRef = {
303
- clss: MemberRefParent,
304
- name: clr_String,
305
- signature: Signature,
306
- };
307
- const ENTRY_Constant = {
308
- type: binary.UINT16_LE,
309
- parent: HasConstant,
310
- value: clr_Blob,
311
- };
312
- const ENTRY_CustomAttribute = {
313
- parent: HasCustomAttribute,
314
- type: CustomAttributeType,
315
- value: CustomAttributeValue,
316
- };
317
- const ENTRY_FieldMarshal = {
318
- parent: HasFieldMarshall,
319
- native_type: clr_Blob,
320
- };
321
- const ENTRY_DeclSecurity = {
322
- action: binary.UINT16_LE,
323
- parent: HasDeclSecurity,
324
- permission_set: clr_Blob,
325
- };
326
- const ENTRY_ClassLayout = {
327
- packing_size: binary.UINT16_LE,
328
- class_size: binary.UINT32_LE,
329
- parent: new Indexed(TABLE.TypeDef),
330
- };
331
- const ENTRY_FieldLayout = {
332
- offset: binary.UINT32_LE,
333
- field: new Indexed(TABLE.Field),
334
- };
335
- const ENTRY_StandAloneSig = {
336
- signature: Signature,
337
- };
338
- const ENTRY_EventMap = {
339
- parent: new Indexed(TABLE.TypeDef),
340
- event_list: new IndexedList(TABLE.Event),
341
- };
342
- const ENTRY_Event = {
343
- flags: binary.UINT16_LE,
344
- name: clr_String,
345
- event_type: TypeDefOrRef,
346
- };
347
- const ENTRY_PropertyMap = {
348
- parent: new Indexed(TABLE.TypeDef),
349
- property_list: new IndexedList(TABLE.Property),
350
- };
351
- const ENTRY_Property = {
352
- flags: binary.UINT16_LE,
353
- name: clr_String,
354
- type: Signature,
355
- };
356
- const ENTRY_MethodSemantics = {
357
- flags: binary.UINT16_LE,
358
- method: new Indexed(TABLE.MethodDef),
359
- association: HasSemantics,
360
- };
361
- const ENTRY_MethodImpl = {
362
- clss: new Indexed(TABLE.TypeDef),
363
- method_body: MethodDefOrRef,
364
- method_declaration: MethodDefOrRef,
365
- };
366
- const ENTRY_ModuleRef = {
367
- name: clr_String,
368
- };
369
- const ENTRY_TypeSpec = {
370
- signature: clr_Blob,
371
- };
372
- const ENTRY_ImplMap = {
373
- flags: binary.UINT16_LE,
374
- member_forwarded: MemberForwarded,
375
- name: clr_String,
376
- scope: new Indexed(TABLE.ModuleRef),
377
- };
378
- const ENTRY_FieldRVA = {
379
- rva: binary.UINT32_LE,
380
- field: new Indexed(TABLE.Field),
381
- };
382
- const ENTRY_Assembly = {
383
- hashalg: binary.UINT32_LE,
384
- major: binary.UINT16_LE,
385
- minor: binary.UINT16_LE,
386
- build: binary.UINT16_LE,
387
- rev: binary.UINT16_LE,
388
- flags: binary.UINT32_LE,
389
- publickey: clr_Blob,
390
- name: clr_String,
391
- culture: clr_String,
392
- };
393
- const ENTRY_AssemblyProcessor = {
394
- processor: binary.UINT32_LE,
395
- };
396
- const ENTRY_AssemblyOS = {
397
- platform: binary.UINT32_LE,
398
- minor: binary.UINT32_LE,
399
- major: binary.UINT32_LE,
400
- };
401
- const ENTRY_AssemblyRef = {
402
- major: binary.UINT16_LE,
403
- minor: binary.UINT16_LE,
404
- build: binary.UINT16_LE,
405
- rev: binary.UINT16_LE,
406
- flags: binary.UINT32_LE,
407
- publickey: clr_Blob,
408
- name: clr_String,
409
- culture: clr_String,
410
- hashvalue: clr_Blob,
411
- };
412
- const ENTRY_AssemblyRefProcessor = {
413
- processor: binary.UINT32_LE,
414
- assembly: new Indexed(TABLE.AssemblyRef),
415
- };
416
- const ENTRY_AssemblyRefOS = {
417
- platform: binary.UINT32_LE,
418
- major: binary.UINT32_LE,
419
- minor: binary.UINT32_LE,
420
- assembly: new Indexed(TABLE.AssemblyRef),
421
- };
422
- const ENTRY_File = {
423
- flags: binary.UINT32_LE,
424
- name: clr_String,
425
- hash: clr_Blob,
426
- };
427
- const ENTRY_ExportedType = {
428
- flags: binary.UINT32_LE,
429
- typedef_id: binary.UINT32_LE,
430
- name: clr_String,
431
- namespce: clr_String,
432
- implementation: Implementation,
433
- };
434
- const ENTRY_ManifestResource = {
435
- data: binary.UINT32_LE,
436
- flags: binary.UINT32_LE,
437
- name: clr_String,
438
- implementation: Implementation,
439
- };
440
- const ENTRY_NestedClass = {
441
- nested_class: new Indexed(TABLE.TypeDef),
442
- enclosing_class: new Indexed(TABLE.TypeDef),
443
- };
444
- const ENTRY_GenericParam = {
445
- number: binary.UINT16_LE,
446
- flags: binary.UINT16_LE,
447
- owner: TypeOrMethodDef,
448
- name: clr_String,
449
- };
450
- const ENTRY_MethodSpec = {
451
- method: MethodDefOrRef,
452
- instantiation: Signature,
453
- };
454
- const ENTRY_GenericParamConstraint = {
455
- owner: new Indexed(TABLE.GenericParam),
456
- constraint: TypeDefOrRef,
457
- };
458
- const TableReaders = {
459
- [TABLE.Module]: ENTRY_Module,
460
- [TABLE.TypeRef]: ENTRY_TypeRef,
461
- [TABLE.TypeDef]: ENTRY_TypeDef,
462
- [TABLE.Field]: ENTRY_Field,
463
- [TABLE.MethodDef]: ENTRY_MethodDef,
464
- [TABLE.Param]: ENTRY_Param,
465
- [TABLE.InterfaceImpl]: ENTRY_InterfaceImpl,
466
- [TABLE.MemberRef]: ENTRY_MemberRef,
467
- [TABLE.Constant]: ENTRY_Constant,
468
- [TABLE.CustomAttribute]: ENTRY_CustomAttribute,
469
- [TABLE.FieldMarshal]: ENTRY_FieldMarshal,
470
- [TABLE.DeclSecurity]: ENTRY_DeclSecurity,
471
- [TABLE.ClassLayout]: ENTRY_ClassLayout,
472
- [TABLE.FieldLayout]: ENTRY_FieldLayout,
473
- [TABLE.StandAloneSig]: ENTRY_StandAloneSig,
474
- [TABLE.EventMap]: ENTRY_EventMap,
475
- [TABLE.Event]: ENTRY_Event,
476
- [TABLE.PropertyMap]: ENTRY_PropertyMap,
477
- [TABLE.Property]: ENTRY_Property,
478
- [TABLE.MethodSemantics]: ENTRY_MethodSemantics,
479
- [TABLE.MethodImpl]: ENTRY_MethodImpl,
480
- [TABLE.ModuleRef]: ENTRY_ModuleRef,
481
- [TABLE.TypeSpec]: ENTRY_TypeSpec,
482
- [TABLE.ImplMap]: ENTRY_ImplMap,
483
- [TABLE.FieldRVA]: ENTRY_FieldRVA,
484
- [TABLE.Assembly]: ENTRY_Assembly,
485
- [TABLE.AssemblyProcessor]: ENTRY_AssemblyProcessor,
486
- [TABLE.AssemblyOS]: ENTRY_AssemblyOS,
487
- [TABLE.AssemblyRef]: ENTRY_AssemblyRef,
488
- [TABLE.AssemblyRefProcessor]: ENTRY_AssemblyRefProcessor,
489
- [TABLE.AssemblyRefOS]: ENTRY_AssemblyRefOS,
490
- [TABLE.File]: ENTRY_File,
491
- [TABLE.ExportedType]: ENTRY_ExportedType,
492
- [TABLE.ManifestResource]: ENTRY_ManifestResource,
493
- [TABLE.NestedClass]: ENTRY_NestedClass,
494
- [TABLE.GenericParam]: ENTRY_GenericParam,
495
- [TABLE.MethodSpec]: ENTRY_MethodSpec,
496
- [TABLE.GenericParamConstraint]: ENTRY_GenericParamConstraint,
497
- };
498
- const ResourceManagerHeader = {
499
- magic: binary.UINT32_LE,
500
- version: binary.UINT32_LE,
501
- skip: binary.UINT32_LE,
502
- };
503
- const pascal_string = binary.StringType(binary.UINT8);
504
- const ResourceManager = {
505
- reader: pascal_string,
506
- set: pascal_string,
507
- version: binary.UINT32_LE,
508
- num_resources: binary.UINT32_LE,
509
- types: binary.ArrayType(binary.UINT32_LE, pascal_string),
510
- };
511
- const ResourceEntry = {
512
- name: binary.StringType(binary.UINT8, 'utf16le', false, 1),
513
- offset: binary.UINT32_LE,
514
- };
515
- class CLR {
516
- header;
517
- table_info;
518
- heaps = [];
519
- tables = {};
520
- raw;
521
- Resources;
522
- /*
523
- static async load(dll: string) {
524
- const p = await pe.PE.load(dll);
525
- if (p) {
526
- const clr_data = p.GetDataDir(p.opt.DataDirectory.CLR_DESCRIPTOR);
527
- if (clr_data)
528
- return new CLR(p, clr_data);
529
- }
530
- }
531
- */
532
- constructor(pe, clr_data) {
533
- try {
534
- this.header = binary.read(new binary.stream(clr_data), CLR_HEADER);
535
- const meta_data = pe.GetDataDir(this.header.MetaData);
536
- const meta_root = meta_data && binary.read(new binary.stream(meta_data.data), METADATA_ROOT);
537
- if (meta_root?.Signature != binary.utils.stringCode('BSJB'))
538
- console.log("oops");
539
- let table_data;
540
- for (const h of meta_root.Streams) {
541
- const mem = meta_data.data.subarray(h.Offset, h.Offset + h.Size);
542
- switch (h.Name) {
543
- case "#~":
544
- table_data = mem;
545
- break;
546
- case "#Strings":
547
- this.heaps[HEAP.String] = mem;
548
- break;
549
- case "#US":
550
- this.heaps[HEAP.UserString] = mem;
551
- break;
552
- case "#GUID":
553
- this.heaps[HEAP.GUID] = mem;
554
- break;
555
- case "#Blob":
556
- this.heaps[HEAP.Blob] = mem;
557
- break;
558
- }
559
- }
560
- if (table_data) {
561
- const stream = new binary.stream(table_data);
562
- this.table_info = binary.read(stream, CLR_TABLES);
563
- const table_counts = [];
564
- //read counts
565
- for (let b = this.table_info.Valid; b; b = binary.utils.clearLowest(b)) {
566
- const i = binary.utils.lowestSetIndex(b);
567
- table_counts[i] = binary.UINT32_LE.get(stream);
568
- }
569
- this.raw = stream.remainder();
570
- //const stream2 = new clr_stream(stream.remainder(), this.heaps, table_info.HeapSizes, this.tables.map(i => i.count));
571
- const stream1 = new clr_dummy(this.table_info.HeapSizes, table_counts);
572
- let offset = 0;
573
- for (let b = this.table_info.Valid; b; b = binary.utils.clearLowest(b)) {
574
- const i = binary.utils.lowestSetIndex(b);
575
- stream1.seek(0);
576
- binary.read(stream1, TableReaders[i]);
577
- this.tables[i] = { offset, count: table_counts[i], size: stream1.tell() };
578
- offset += this.tables[i].size * this.tables[i].count;
579
- }
580
- this.Resources = pe.GetDataDir(this.header.Resources)?.data;
581
- }
582
- }
583
- catch (e) {
584
- console.log(e);
585
- }
586
- }
587
- getEntry(t, i) {
588
- const table = this.tables[t];
589
- if (table) {
590
- const stream2 = new clr_stream(this.raw, this.heaps, this.table_info.HeapSizes, Object.values(this.tables).map(i => i.count));
591
- stream2.seek(table.offset + i * table.size);
592
- return binary.read(stream2, TableReaders[t]);
593
- }
594
- }
595
- getTable(t) {
596
- const table = this.tables[t];
597
- if (table) {
598
- const stream2 = new clr_stream(this.raw, this.heaps, this.table_info.HeapSizes, Object.values(this.tables).map(i => i.count));
599
- stream2.seek(table.offset);
600
- const result = [];
601
- for (let i = 0; i < table.count; i++)
602
- result.push(binary.read(stream2, TableReaders[t]));
603
- return result;
604
- }
605
- }
606
- getResources(block) {
607
- if (this.Resources) {
608
- for (const i of this.getTable(TABLE.ManifestResource)) {
609
- if (i.name == block) {
610
- const data0 = new binary.stream(this.Resources.subarray(i.data));
611
- const size = binary.UINT32_LE.get(data0);
612
- return getResources(data0.read_buffer(size));
613
- }
614
- }
615
- }
616
- }
617
- getResource(block, name) {
618
- return this.getResources(block)?.[name];
619
- }
620
- allResources() {
621
- if (this.Resources) {
622
- const result = {};
623
- for (const i of this.getTable(TABLE.ManifestResource)) {
624
- const data0 = new binary.stream(this.Resources.subarray(i.data));
625
- const size = binary.UINT32_LE.get(data0);
626
- const resources = getResources(data0.read_buffer(size));
627
- if (resources)
628
- Object.assign(result, resources);
629
- }
630
- return result;
631
- }
632
- }
633
- }
634
- exports.CLR = CLR;
635
- function getResources(data) {
636
- const stream = new binary.stream(data);
637
- const manager0 = binary.read(stream, ResourceManagerHeader);
638
- if (manager0.magic == 0xBEEFCACE) {
639
- const manager = binary.read_more(stream, ResourceManager, manager0);
640
- stream.align(8);
641
- const hashes = binary.readn(stream, binary.UINT32_LE, manager.num_resources);
642
- const offsets = binary.readn(stream, binary.UINT32_LE, manager.num_resources);
643
- const start = binary.UINT32_LE.get(stream);
644
- const entries = binary.readn(stream, ResourceEntry, manager.num_resources);
645
- const resources = {};
646
- const decoder = new TextDecoder('utf-8');
647
- for (let j = 0; j < manager.num_resources; j++) {
648
- const from = start + entries[j].offset;
649
- resources[entries[j].name] = data[from] == 1
650
- ? decoder.decode(data.subarray(from + 2, from + 2 + data[from + 1]))
651
- : data.subarray(from, j < manager.num_resources - 1 ? start + entries[j + 1].offset : data.length);
652
- }
653
- return resources;
654
- }
655
- }
656
- pe.DIRECTORIES.CLR_DESCRIPTOR.read = (pe, data) => {
657
- function fix_names(table) {
658
- if ('name' in table[0])
659
- return Object.fromEntries(table.map(i => [i.name, i]));
660
- return table;
661
- }
662
- const clr = new CLR(pe, data.data);
663
- return Object.fromEntries(Object.entries(clr.tables).map(([k, v]) => [TABLE[+k], fix_names(clr.getTable(+k))]));
664
- };
package/dist/elf.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import * as binary from '@isopodlabs/binary';
2
- export declare class ELFFile {
3
- segments: [string, any][];
4
- sections: [string, any][];
5
- symbols?: [string, any][];
6
- dynamic_symbols?: [string, any][];
7
- header: any;
8
- static check(data: Uint8Array): boolean;
9
- constructor(data: Uint8Array);
10
- getSymbols(type: binary.Type, data: Uint8Array, names: Uint8Array): [string, any][];
11
- }