@isopodlabs/binary_libs 1.1.1 → 1.2.1

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/elf.js CHANGED
@@ -778,3 +778,4 @@ class ELFFile {
778
778
  }
779
779
  }
780
780
  exports.ELFFile = ELFFile;
781
+ //# sourceMappingURL=elf.js.map
package/dist/mach.js CHANGED
@@ -1082,3 +1082,4 @@ export function freestanding(s: binary.stream, cmd: CMD) {
1082
1082
  return binary.read(s, cmd_table[cmd]);
1083
1083
  }
1084
1084
  */
1085
+ //# sourceMappingURL=mach.js.map
package/dist/pe.d.ts CHANGED
@@ -1,4 +1,62 @@
1
1
  import * as binary from '@isopodlabs/binary';
2
+ declare const FILE_HEADER: {
3
+ Machine: binary.TypeT<string>;
4
+ NumberOfSections: binary.TypeT<number>;
5
+ TimeDateStamp: binary.TypeT<number>;
6
+ PointerToSymbolTable: binary.TypeT<number>;
7
+ NumberOfSymbols: binary.TypeT<number>;
8
+ SizeOfOptionalHeader: binary.TypeT<number>;
9
+ Characteristics: binary.TypeT<number>;
10
+ };
11
+ declare const Section_base: (new (s: binary._stream) => {
12
+ Name: string;
13
+ VirtualSize: number;
14
+ VirtualAddress: binary.hex<number | bigint>;
15
+ SizeOfRawData: number;
16
+ PointerToRawData: binary.hex<number | bigint>;
17
+ PointerToRelocations: binary.hex<number | bigint>;
18
+ PointerToLinenumbers: binary.hex<number | bigint>;
19
+ NumberOfRelocations: number;
20
+ NumberOfLinenumbers: number;
21
+ Characteristics: Record<string, bigint | boolean> | Record<string, number | boolean>;
22
+ }) & {
23
+ get: <X extends abstract new (...args: any) => any>(this: X, s: binary._stream) => InstanceType<X>;
24
+ };
25
+ export declare class Section extends Section_base {
26
+ data?: binary.MappedMemory;
27
+ constructor(r: binary.stream);
28
+ get flags(): number;
29
+ }
30
+ export declare class COFF {
31
+ static check(data: Uint8Array): boolean;
32
+ header: binary.ReadType<typeof FILE_HEADER>;
33
+ opt?: binary.ReadType<typeof OPTIONAL_HEADER> & (binary.ReadType<typeof OPTIONAL_HEADER32> | binary.ReadType<typeof OPTIONAL_HEADER64>);
34
+ sections: Section[];
35
+ constructor(data: Uint8Array);
36
+ }
37
+ declare const COFFSymbol_base: (new (s: binary._stream) => {
38
+ a: number;
39
+ b: number;
40
+ c: number;
41
+ Architecture: string;
42
+ Id: number;
43
+ Length: number;
44
+ Value: number;
45
+ Type: number;
46
+ Symbol: string;
47
+ Module: string;
48
+ }) & {
49
+ get: <X extends abstract new (...args: any) => any>(this: X, s: binary._stream) => InstanceType<X>;
50
+ };
51
+ export declare class COFFSymbol extends COFFSymbol_base {
52
+ static check(data: Uint8Array): boolean;
53
+ constructor(data: Uint8Array);
54
+ }
55
+ export declare class pe_stream extends binary.stream {
56
+ pe: PE;
57
+ constructor(pe: PE, data: Uint8Array);
58
+ get_rva(): Uint8Array | undefined;
59
+ }
2
60
  declare const DOS_HEADER: {
3
61
  magic: binary.TypeT<number>;
4
62
  cblp: binary.TypeT<number>;
@@ -22,30 +80,6 @@ declare const EXE_HEADER: {
22
80
  res2: binary.TypeT<number[]>;
23
81
  lfanew: binary.TypeT<number>;
24
82
  };
25
- export declare class pe_stream extends binary.stream {
26
- pe: PE;
27
- constructor(pe: PE, data: Uint8Array);
28
- get_rva(): Uint8Array | undefined;
29
- }
30
- declare const Section_base: (new (s: binary._stream) => {
31
- Name: string;
32
- VirtualSize: number;
33
- VirtualAddress: binary.hex<number | bigint>;
34
- SizeOfRawData: number;
35
- PointerToRawData: binary.hex<number | bigint>;
36
- PointerToRelocations: binary.hex<number | bigint>;
37
- PointerToLinenumbers: binary.hex<number | bigint>;
38
- NumberOfRelocations: number;
39
- NumberOfLinenumbers: number;
40
- Characteristics: Record<string, bigint | boolean> | Record<string, number | boolean>;
41
- }) & {
42
- get: <X extends abstract new (...args: any) => any>(this: X, s: binary._stream) => InstanceType<X>;
43
- };
44
- declare class Section extends Section_base {
45
- data?: binary.MappedMemory;
46
- constructor(r: binary.stream);
47
- get flags(): number;
48
- }
49
83
  interface DirectoryInfo {
50
84
  read?: (pe: PE, data: binary.MappedMemory) => any;
51
85
  }
package/dist/pe.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.DLLImports = exports.PE = exports.DATA_DIRECTORY = exports.DIRECTORIES = exports.pe_stream = void 0;
26
+ exports.DLLImports = exports.PE = exports.DATA_DIRECTORY = exports.DIRECTORIES = exports.pe_stream = exports.COFFSymbol = exports.COFF = exports.Section = void 0;
27
27
  exports.ReadExports = ReadExports;
28
28
  exports.ReadImports = ReadImports;
29
29
  exports.ReadResourceDirectory = ReadResourceDirectory;
@@ -40,59 +40,32 @@ const TIMEDATE = binary.as(uint32, MyDate);
40
40
  //-----------------------------------------------------------------------------
41
41
  // COFF
42
42
  //-----------------------------------------------------------------------------
43
- const DOS_HEADER = {
44
- magic: uint16,
45
- cblp: uint16,
46
- cp: uint16,
47
- crlc: uint16,
48
- cparhdr: uint16,
49
- minalloc: uint16,
50
- maxalloc: binary.asHex(uint16),
51
- ss: uint16,
52
- sp: uint16,
53
- csum: uint16,
54
- ip: uint16,
55
- cs: uint16,
56
- lfarlc: uint16,
57
- ovno: uint16,
58
- };
59
- const EXE_HEADER = {
60
- res: binary.ArrayType(4, uint16),
61
- oemid: uint16,
62
- oeminfo: uint16,
63
- res2: binary.ArrayType(10, uint16),
64
- lfanew: binary.INT32_LE,
65
- };
66
- //-----------------------------------------------------------------------------
67
- // PE
68
- //-----------------------------------------------------------------------------
69
- class pe_stream extends binary.stream {
70
- pe;
71
- constructor(pe, data) {
72
- super(data);
73
- this.pe = pe;
74
- }
75
- get_rva() { return this.pe.GetDataRVA(uint32.get(this))?.data; }
76
- }
77
- exports.pe_stream = pe_stream;
78
- const RVA_STRING = {
79
- get(s) { return binary.utils.decodeTextTo0(s.get_rva(), 'utf8'); },
80
- put(_s) { }
81
- };
82
- const RVA_ARRAY16 = {
83
- get(s) { return binary.utils.to16(s.get_rva()); },
84
- put(_s) { }
85
- };
86
- const RVA_ARRAY32 = {
87
- get(s) { return binary.utils.to32(s.get_rva()); },
88
- put(_s) { }
89
- };
90
- const RVA_ARRAY64 = {
91
- get(s) { return binary.utils.to64(s.get_rva()); },
92
- put(_s) { }
43
+ const MACHINES = {
44
+ UNKNOWN: 0x0,
45
+ AM33: 0x1d3,
46
+ AMD64: 0x8664,
47
+ ARM: 0x1c0,
48
+ ARMV7: 0x1c4,
49
+ EBC: 0xebc,
50
+ I386: 0x14c,
51
+ IA64: 0x200,
52
+ M32R: 0x9041,
53
+ MIPS16: 0x266,
54
+ MIPSFPU: 0x366,
55
+ MIPSFPU16: 0x466,
56
+ POWERPC: 0x1f0,
57
+ POWERPCFP: 0x1f1,
58
+ R4000: 0x166,
59
+ SH3: 0x1a2,
60
+ SH3DSP: 0x1a3,
61
+ SH4: 0x1a6,
62
+ SH5: 0x1a8,
63
+ THUMB: 0x1c2,
64
+ WCEMIPSV2: 0x169,
93
65
  };
66
+ const MACHINE = binary.asEnum(uint16, MACHINES);
94
67
  const FILE_HEADER = {
95
- Machine: uint16,
68
+ Machine: MACHINE,
96
69
  NumberOfSections: uint16,
97
70
  TimeDateStamp: uint32,
98
71
  PointerToSymbolTable: uint32,
@@ -173,6 +146,103 @@ class Section extends binary.ReadClass({
173
146
  | (this.Characteristics.MEM_EXECUTE ? binary.MappedMemory.EXECUTE : 0);
174
147
  }
175
148
  }
149
+ exports.Section = Section;
150
+ class COFF {
151
+ static check(data) {
152
+ const header = binary.read(new binary.stream(data), FILE_HEADER);
153
+ return MACHINES[header.Machine] !== undefined;
154
+ }
155
+ header;
156
+ opt;
157
+ sections;
158
+ constructor(data) {
159
+ const file = new binary.stream(data);
160
+ this.header = binary.read(file, FILE_HEADER);
161
+ if (this.header.SizeOfOptionalHeader) {
162
+ console.log("COFF: SizeOfOptionalHeader", this.header.SizeOfOptionalHeader);
163
+ }
164
+ this.sections = Array.from({ length: this.header.NumberOfSections }, () => new Section(file));
165
+ }
166
+ }
167
+ exports.COFF = COFF;
168
+ // these appear in libs:
169
+ const SYMBOL = {
170
+ a: uint16, //0
171
+ b: uint16, //0xffff
172
+ c: uint16, //0
173
+ Architecture: MACHINE, // 0x14C for x86, 0x8664 for x64
174
+ Id: uint32,
175
+ Length: uint32,
176
+ //union {
177
+ // Hint: uint16,
178
+ // Ordinal: uint16,
179
+ Value: uint16,
180
+ //}
181
+ Type: uint16,
182
+ Symbol: binary.NullTerminatedStringType(),
183
+ Module: binary.NullTerminatedStringType(),
184
+ };
185
+ class COFFSymbol extends binary.ReadClass(SYMBOL) {
186
+ static check(data) {
187
+ const test = binary.read(new binary.stream(data), SYMBOL);
188
+ return test.a === 0 && test.b === 0xffff && test.c === 0 && test.Architecture != 'UNKNOWN';
189
+ }
190
+ constructor(data) {
191
+ super(new binary.stream(data));
192
+ }
193
+ }
194
+ exports.COFFSymbol = COFFSymbol;
195
+ //-----------------------------------------------------------------------------
196
+ // PE
197
+ //-----------------------------------------------------------------------------
198
+ class pe_stream extends binary.stream {
199
+ pe;
200
+ constructor(pe, data) {
201
+ super(data);
202
+ this.pe = pe;
203
+ }
204
+ get_rva() { return this.pe.GetDataRVA(uint32.get(this))?.data; }
205
+ }
206
+ exports.pe_stream = pe_stream;
207
+ const RVA_STRING = {
208
+ get(s) { return binary.utils.decodeTextTo0(s.get_rva(), 'utf8'); },
209
+ put(_s) { }
210
+ };
211
+ const RVA_ARRAY16 = {
212
+ get(s) { return binary.utils.to16(s.get_rva()); },
213
+ put(_s) { }
214
+ };
215
+ const RVA_ARRAY32 = {
216
+ get(s) { return binary.utils.to32(s.get_rva()); },
217
+ put(_s) { }
218
+ };
219
+ const RVA_ARRAY64 = {
220
+ get(s) { return binary.utils.to64(s.get_rva()); },
221
+ put(_s) { }
222
+ };
223
+ const DOS_HEADER = {
224
+ magic: uint16,
225
+ cblp: uint16,
226
+ cp: uint16,
227
+ crlc: uint16,
228
+ cparhdr: uint16,
229
+ minalloc: uint16,
230
+ maxalloc: binary.asHex(uint16),
231
+ ss: uint16,
232
+ sp: uint16,
233
+ csum: uint16,
234
+ ip: uint16,
235
+ cs: uint16,
236
+ lfarlc: uint16,
237
+ ovno: uint16,
238
+ };
239
+ const EXE_HEADER = {
240
+ res: binary.ArrayType(4, uint16),
241
+ oemid: uint16,
242
+ oeminfo: uint16,
243
+ res2: binary.ArrayType(10, uint16),
244
+ lfanew: binary.INT32_LE,
245
+ };
176
246
  exports.DIRECTORIES = {
177
247
  EXPORT: { read: (pe, data) => ReadExports(new pe_stream(pe, data.data)) },
178
248
  IMPORT: { read: (pe, data) => ReadImports(new pe_stream(pe, data.data)) },
@@ -480,3 +550,4 @@ function ReadResourceDirectory(file, data, type = 0) {
480
550
  }
481
551
  return result;
482
552
  }
553
+ //# sourceMappingURL=pe.js.map
package/package.json CHANGED
@@ -1,45 +1,49 @@
1
- {
2
- "name": "@isopodlabs/binary_libs",
3
- "description": "A package for reading various library file format with TypeScript.",
4
- "exports": {
5
- "./arch": "./dist/arch.js",
6
- "./clr": "./dist/clr.js",
7
- "./CompoundDocument": "./dist/CompoundDocument.js",
8
- "./elf": "./dist/elf.js",
9
- "./mach": "./dist/mach.js",
10
- "./pe": "./dist/pe.js"
11
- },
12
- "files": [
13
- "dist",
14
- "README.md"
15
- ],
16
- "scripts": {
17
- "build": "tsc",
18
- "lint": "eslint \"src/**/*.ts\"",
19
- "test": "echo \"No tests specified\" && exit 0",
20
- "patch": "ts-patch install"
21
- },
22
- "keywords": [
23
- "binary",
24
- "typescript",
25
- "data",
26
- "read",
27
- "write"
28
- ],
29
- "author": "Adrian Stephens",
30
- "license": "MIT",
31
- "devDependencies": {
32
- "@types/node": "^22.13.8",
33
- "@typescript-eslint/eslint-plugin": "^8.26.0",
34
- "@typescript-eslint/parser": "^8.26.0",
35
- "eslint": "^9.21.0",
36
- "ts-node": "^10.9.2",
37
- "ts-patch": "^3.3.0",
38
- "typescript": "~5.5.0",
39
- "typescript-eslint": "^8.26.0"
40
- },
41
- "dependencies": {
42
- "@isopodlabs/binary": "^1.4.0"
43
- },
44
- "version": "1.1.1"
45
- }
1
+ {
2
+ "name": "@isopodlabs/binary_libs",
3
+ "description": "A package for reading various library file format with TypeScript.",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/adrianstephens/ts-binary-libs.git"
7
+ },
8
+ "exports": {
9
+ "./arch": "./dist/arch.js",
10
+ "./clr": "./dist/clr.js",
11
+ "./CompoundDocument": "./dist/CompoundDocument.js",
12
+ "./elf": "./dist/elf.js",
13
+ "./mach": "./dist/mach.js",
14
+ "./pe": "./dist/pe.js"
15
+ },
16
+ "files": [
17
+ "dist/**/*.{js,d.ts}",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "lint": "eslint \"src/**/*.ts\"",
23
+ "test": "echo \"No tests specified\" && exit 0",
24
+ "patch": "ts-patch install"
25
+ },
26
+ "keywords": [
27
+ "binary",
28
+ "typescript",
29
+ "data",
30
+ "read",
31
+ "write"
32
+ ],
33
+ "author": "Adrian Stephens",
34
+ "license": "MIT",
35
+ "devDependencies": {
36
+ "@types/node": "^22.13.8",
37
+ "@typescript-eslint/eslint-plugin": "^8.26.0",
38
+ "@typescript-eslint/parser": "^8.26.0",
39
+ "eslint": "^9.21.0",
40
+ "ts-node": "^10.9.2",
41
+ "ts-patch": "^3.3.0",
42
+ "typescript": "~5.5.0",
43
+ "typescript-eslint": "^8.26.0"
44
+ },
45
+ "dependencies": {
46
+ "@isopodlabs/binary": "^1.4.1"
47
+ },
48
+ "version": "1.2.1"
49
+ }