@reversense/dxc-struct 1.0.7
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/.idea/modules.xml +8 -0
- package/.idea/project.iml +8 -0
- package/.idea/vcs.xml +6 -0
- package/CODEOWNERS +24 -0
- package/LICENSE +661 -0
- package/README.md +169 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +24 -0
- package/dist/src/Struct.d.ts +49 -0
- package/dist/src/Struct.js +140 -0
- package/dist/src/StructDecoder.d.ts +11 -0
- package/dist/src/StructDecoder.js +90 -0
- package/dist/src/StructEncoder.d.ts +10 -0
- package/dist/src/StructEncoder.js +92 -0
- package/dist/src/common.d.ts +4 -0
- package/dist/src/common.js +5 -0
- package/dist/src/error/MonitoredError.d.ts +14 -0
- package/dist/src/error/MonitoredError.js +33 -0
- package/dist/src/error/RuntimeException.d.ts +7 -0
- package/dist/src/error/RuntimeException.js +15 -0
- package/index.ts +47 -0
- package/package.json +37 -0
- package/package.json.bak +37 -0
- package/scripts/publish.sh +36 -0
- package/src/Struct.ts +380 -0
- package/src/StructDecoder.ts +199 -0
- package/src/StructEncoder.ts +185 -0
- package/src/common.ts +25 -0
- package/src/error/MonitoredError.ts +90 -0
- package/src/error/RuntimeException.ts +41 -0
- package/test/HelloWorld.test.ts +0 -0
- package/test/Struct.test.ts +251 -0
- package/test/StructDecoder-int.test.ts +133 -0
- package/test/StructDecoder-uint.test.ts +104 -0
- package/test/StructEncoder.test.ts +233 -0
- package/test/res/elf32_lib.so +0 -0
- package/test/res/elf64_ssh.bin +0 -0
- package/test/res/resources.arsc +0 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import * as _path_ from "path";
|
|
2
|
+
import * as _fs_ from "fs";
|
|
3
|
+
import {expect} from "chai";
|
|
4
|
+
import {Struct} from "../dist/index.js";
|
|
5
|
+
|
|
6
|
+
const ELF_MAGIC : string = '\u007FELF';
|
|
7
|
+
const ELF_MAGIC_ARRAY : number[] = [0x7F, 0x45, 0x4C, 0x46];
|
|
8
|
+
const ELF_HEADER_32B = {
|
|
9
|
+
'Magic': '\u007FELF',
|
|
10
|
+
'Class': 1,
|
|
11
|
+
'Data': 1,
|
|
12
|
+
'EI_Version': 1,
|
|
13
|
+
'OS_ABI': 0,
|
|
14
|
+
'ABI_Version': 0,
|
|
15
|
+
'Type': 3,
|
|
16
|
+
'Machine': 40,
|
|
17
|
+
'Version': 1,
|
|
18
|
+
'Entry_point_address': 0,
|
|
19
|
+
'Start_program_headers': 52,
|
|
20
|
+
'Start_section_headers': 4436,
|
|
21
|
+
'Flags': 83886080,
|
|
22
|
+
'Header_size': 52,
|
|
23
|
+
'Program_headers_size': 32,
|
|
24
|
+
'number_of_program_headers': 7,
|
|
25
|
+
'Section_headers_size': 40,
|
|
26
|
+
'Number_of_section_headers': 20,
|
|
27
|
+
'Section_header_string_table_index': 19
|
|
28
|
+
};
|
|
29
|
+
// Need to use BigInt to store precisely integers of more than 64 bits.
|
|
30
|
+
const ELF_HEADER_64B = {
|
|
31
|
+
'Magic': '\u007FELF',
|
|
32
|
+
'Class': 2,
|
|
33
|
+
'Data': 1,
|
|
34
|
+
'EI_Version': 1,
|
|
35
|
+
'OS_ABI': 0,
|
|
36
|
+
'ABI_Version': 0,
|
|
37
|
+
'Type': 3,
|
|
38
|
+
'Machine': 62,
|
|
39
|
+
'Version': 1,
|
|
40
|
+
'Entry_point_address': BigInt(79376),
|
|
41
|
+
'Start_program_headers': BigInt(64),
|
|
42
|
+
'Start_section_headers': BigInt(844904),
|
|
43
|
+
'Flags': 0,
|
|
44
|
+
'Header_size': 64,
|
|
45
|
+
'Program_headers_size': 56,
|
|
46
|
+
'number_of_program_headers': 13,
|
|
47
|
+
'Section_headers_size': 64,
|
|
48
|
+
'Number_of_section_headers': 31,
|
|
49
|
+
'Section_header_string_table_index': 30
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
describe('Struct:decoding individual format', function (){
|
|
53
|
+
|
|
54
|
+
it('Decoding unsigned byte B', function () {
|
|
55
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
56
|
+
const buf_unpack = Struct.unpack('B', buf);
|
|
57
|
+
expect(buf_unpack[0]).to.equal(buf.readUInt8(0));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('Decoding signed byte B', function () {
|
|
61
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
62
|
+
const buf_unpack = Struct.unpack('b', buf);
|
|
63
|
+
expect(buf_unpack[0]).to.equal(buf.readInt8(0));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('Decoding unsigned int I', function () {
|
|
67
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
68
|
+
const buf_unpack = Struct.unpack('>I', buf);
|
|
69
|
+
expect(buf_unpack[0]).to.equal(buf.readUInt32BE(0));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('Decoding signed int i', function () {
|
|
73
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
74
|
+
const buf_unpack = Struct.unpack('>i', buf);
|
|
75
|
+
expect(buf_unpack[0]).to.equal(buf.readInt32BE(0));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('Decoding unsigned int <I Little Endian', function () {
|
|
79
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
80
|
+
const buf_unpack = Struct.unpack('<I', buf);
|
|
81
|
+
expect(buf_unpack[0]).to.equal(buf.readUInt32LE(0));
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('Decoding signed int <i Little Endian', function () {
|
|
85
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
86
|
+
const buf_unpack = Struct.unpack('<i', buf);
|
|
87
|
+
expect(buf_unpack[0]).to.equal(buf.readInt32LE(0));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('Decoding unsigned long L', function () {
|
|
91
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff, 0x33]);
|
|
92
|
+
const buf_unpack = Struct.unpack('>L', buf);
|
|
93
|
+
expect(buf_unpack[0]).to.equal(buf.readUInt32BE(0));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('Decoding signed long l', function () {
|
|
97
|
+
const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff, 0x33]);
|
|
98
|
+
const buf_unpack = Struct.unpack('>l', buf);
|
|
99
|
+
expect(buf_unpack[0]).to.equal(buf.readInt32BE(0));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('Decoding unsigned long long Q', function () {
|
|
103
|
+
const buf = Buffer.from([0xf1, 0x01, 0x03, 0x00, 0x1d, 0x11, 0x17, 0x42, 0x33]);
|
|
104
|
+
const buf_unpack = Struct.unpack('>Q', buf);
|
|
105
|
+
expect(buf_unpack[0]).to.equal(buf.readBigUInt64BE(0));
|
|
106
|
+
// expect(buf_unpack[0]).to.equal(buf.readDoubleBE(0));
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('Decoding signed long long q -1', function () {
|
|
110
|
+
//const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xcd, 0x21, 0x97, 0x00]);
|
|
111
|
+
//const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
112
|
+
const buf = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x33]);
|
|
113
|
+
const buf_unpack : bigint[] = Struct.unpack('>q', buf);
|
|
114
|
+
expect(buf_unpack[0]).to.equal(buf.readBigInt64BE(0));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('Decoding signed long long q Lowest', function () {
|
|
118
|
+
//const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xcd, 0x21, 0x97, 0x00]);
|
|
119
|
+
//const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
120
|
+
const buf = Buffer.from([0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33]);
|
|
121
|
+
const buf_unpack : bigint[] = Struct.unpack('>q', buf);
|
|
122
|
+
expect(buf_unpack[0]).to.equal(buf.readBigInt64BE(0));
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('Decoding unsigned long long Q Highest', function () {
|
|
126
|
+
//const buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xcd, 0x21, 0x97, 0x00]);
|
|
127
|
+
//const buf = Buffer.from([0xf3, 0x21, 0x03, 0x00, 0xcd, 0x21, 0x97, 0xff]);
|
|
128
|
+
const buf = Buffer.from([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x33]);
|
|
129
|
+
const buf_unpack: bigint[] = Struct.unpack('>Q', buf);
|
|
130
|
+
expect(buf_unpack[0]).to.equal(buf.readBigUInt64BE(0));
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('False format error', function () {
|
|
134
|
+
const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
135
|
+
const buf_unpack = Struct.unpack('>Z', buf);
|
|
136
|
+
expect(buf_unpack).to.empty;
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('Struct:decoding Elf32 header', function () {
|
|
141
|
+
|
|
142
|
+
let BUFFER_1:any;
|
|
143
|
+
|
|
144
|
+
before(()=>{
|
|
145
|
+
BUFFER_1 = _fs_.readFileSync(_path_.join(process.cwd(),'test','res','elf32_lib.so'))
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('Decoding ELF32 Header', function () {
|
|
149
|
+
|
|
150
|
+
it('Decoding ELF Magic', function () {
|
|
151
|
+
const magic : number[] = Struct.unpack('<4s',BUFFER_1);
|
|
152
|
+
expect(magic[0]).to.eql(ELF_MAGIC);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('Decoding ELF Magic with number in format', function () {
|
|
156
|
+
const magic_array : number[] = Struct.unpack('<4B',BUFFER_1);
|
|
157
|
+
expect(magic_array).to.eql(ELF_MAGIC_ARRAY);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('Decoding entire ELF Header', function () {
|
|
161
|
+
const e_ident = Struct.unpack('<4sBBBBB',BUFFER_1);
|
|
162
|
+
const e_rest = Struct.unpack('<HHIIIIIHHHHHH',BUFFER_1.subarray(16));
|
|
163
|
+
const elf_header = {
|
|
164
|
+
'Magic': e_ident[0],
|
|
165
|
+
'Class': e_ident[1],
|
|
166
|
+
'Data': e_ident[2],
|
|
167
|
+
'EI_Version': e_ident[3],
|
|
168
|
+
'OS_ABI': e_ident[4],
|
|
169
|
+
'ABI_Version': e_ident[5],
|
|
170
|
+
'Type': e_rest[0],
|
|
171
|
+
'Machine': e_rest[1],
|
|
172
|
+
'Version': e_rest[2],
|
|
173
|
+
'Entry_point_address': e_rest[3],
|
|
174
|
+
'Start_program_headers': e_rest[4],
|
|
175
|
+
'Start_section_headers': e_rest[5],
|
|
176
|
+
'Flags': e_rest[6],
|
|
177
|
+
'Header_size': e_rest[7],
|
|
178
|
+
'Program_headers_size': e_rest[8],
|
|
179
|
+
'number_of_program_headers': e_rest[9],
|
|
180
|
+
'Section_headers_size': e_rest[10],
|
|
181
|
+
'Number_of_section_headers': e_rest[11],
|
|
182
|
+
'Section_header_string_table_index': e_rest[12]
|
|
183
|
+
};
|
|
184
|
+
expect(elf_header).to.eql(ELF_HEADER_32B);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
describe('Struct:decoding Elf64 header', function() {
|
|
191
|
+
let BUFFER_2:any;
|
|
192
|
+
before(()=>{
|
|
193
|
+
BUFFER_2 = _fs_.readFileSync(_path_.join(process.cwd(),'test','res','elf64_ssh.bin'))
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('Decoding ELF64 Header', function () {
|
|
197
|
+
|
|
198
|
+
it('Decoding entire ELF Header', function () {
|
|
199
|
+
const e_ident = Struct.unpack('<4sBBBBB',BUFFER_2);
|
|
200
|
+
const e_rest = Struct.unpack('<HHIQQQIHHHHHH',BUFFER_2.subarray(16));
|
|
201
|
+
const elf64_header = {
|
|
202
|
+
'Magic': e_ident[0],
|
|
203
|
+
'Class': e_ident[1],
|
|
204
|
+
'Data': e_ident[2],
|
|
205
|
+
'EI_Version': e_ident[3],
|
|
206
|
+
'OS_ABI': e_ident[4],
|
|
207
|
+
'ABI_Version': e_ident[5],
|
|
208
|
+
'Type': e_rest[0],
|
|
209
|
+
'Machine': e_rest[1],
|
|
210
|
+
'Version': e_rest[2],
|
|
211
|
+
'Entry_point_address': e_rest[3],
|
|
212
|
+
'Start_program_headers': e_rest[4],
|
|
213
|
+
'Start_section_headers': e_rest[5],
|
|
214
|
+
'Flags': e_rest[6],
|
|
215
|
+
'Header_size': e_rest[7],
|
|
216
|
+
'Program_headers_size': e_rest[8],
|
|
217
|
+
'number_of_program_headers': e_rest[9],
|
|
218
|
+
'Section_headers_size': e_rest[10],
|
|
219
|
+
'Number_of_section_headers': e_rest[11],
|
|
220
|
+
'Section_header_string_table_index': e_rest[12]
|
|
221
|
+
};
|
|
222
|
+
expect(elf64_header).to.eql(ELF_HEADER_64B);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
describe('Struct:decoding', function() {
|
|
229
|
+
|
|
230
|
+
let BUFFER_1:any;
|
|
231
|
+
|
|
232
|
+
before(()=>{
|
|
233
|
+
BUFFER_1 = _fs_.readFileSync(_path_.join(process.cwd(),'test','res','resources.arsc'))
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe('mixed', function () {
|
|
237
|
+
|
|
238
|
+
it('valid format', function () {
|
|
239
|
+
const data = Struct.unpack('<HHII',BUFFER_1);
|
|
240
|
+
|
|
241
|
+
const header = {
|
|
242
|
+
"Type Signature": data[0],
|
|
243
|
+
"Header Size": data[1],
|
|
244
|
+
"File Size": data[2],
|
|
245
|
+
"Package Count": data[3]
|
|
246
|
+
}
|
|
247
|
+
console.log(header);
|
|
248
|
+
console.log(Struct.calcLength('<HHII',BUFFER_1));
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {expect} from "chai";
|
|
2
|
+
import {Struct} from "../dist/index.js";
|
|
3
|
+
import {StructDecoder} from '../dist/src/StructDecoder.js';
|
|
4
|
+
import {Endianness} from "../dist/src/common.js";
|
|
5
|
+
|
|
6
|
+
// Tests sourced from https://github.com/nodejs/node/blob/v22.2.0/test/parallel/test-buffer-readint.js
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
describe('StructDecoder test int formats 8-16-32-64', function () {
|
|
10
|
+
|
|
11
|
+
// Test 8-bit integers
|
|
12
|
+
describe('int8', function () {
|
|
13
|
+
const pOp = Struct.OP['b'];
|
|
14
|
+
const data = Buffer.from([0x23, 0xab, 0x7c, 0xef]);
|
|
15
|
+
let endianness = Endianness.BIG_ENDIAN;
|
|
16
|
+
it('int8 several readings without offset, BE', () => {
|
|
17
|
+
expect(StructDecoder.int(endianness, pOp, data, 0)).to.equal(0x23);
|
|
18
|
+
data[0] = 0xff;
|
|
19
|
+
expect(StructDecoder.int(endianness, pOp, data, 0)).to.equal(-1);
|
|
20
|
+
data[0] = 0x87;
|
|
21
|
+
expect(StructDecoder.int(endianness, pOp, data, 0)).to.equal(-121);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('int8 several readings with offset BE', () => {
|
|
25
|
+
expect(StructDecoder.int(endianness, pOp, data, 1)).to.equal(-85);
|
|
26
|
+
expect(StructDecoder.int(endianness, pOp, data, 2)).to.equal(124);
|
|
27
|
+
expect(StructDecoder.int(endianness, pOp, data, 3)).to.equal(-17);
|
|
28
|
+
});
|
|
29
|
+
endianness = Endianness.LITTLE_ENDIAN;
|
|
30
|
+
it('int8 LE', () => {
|
|
31
|
+
expect(expect(StructDecoder.int(endianness, pOp, data, 1)).to.equal(-85));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Test 16-bit integers
|
|
36
|
+
describe('int16', function () {
|
|
37
|
+
const pOp = Struct.OP['h'];
|
|
38
|
+
let data = Buffer.from([0x16, 0x79, 0x65, 0x6e, 0x69, 0x78]);
|
|
39
|
+
it('int16 several readings with various endianness without offset', () => {
|
|
40
|
+
it('big endian', () => {
|
|
41
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x1679);
|
|
42
|
+
});
|
|
43
|
+
it('little endian', () => {
|
|
44
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(0x7916);
|
|
45
|
+
});
|
|
46
|
+
it('other values', () => {
|
|
47
|
+
data[0] = 0xff;
|
|
48
|
+
data[1] = 0x80;
|
|
49
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(-128);
|
|
50
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(-32513);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
data = Buffer.from([0x77, 0x65, 0x65, 0x6e, 0x69, 0x78]);
|
|
54
|
+
it('int16 several readings with offset BE', () => {
|
|
55
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x7765);
|
|
56
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 1)).to.equal(0x6565);
|
|
57
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 2)).to.equal(0x656e);
|
|
58
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 3)).to.equal(0x6e69);
|
|
59
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 4)).to.equal(0x6978);
|
|
60
|
+
});
|
|
61
|
+
it('int16 several readings with offset LE', () => {
|
|
62
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(0x6577);
|
|
63
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 1)).to.equal(0x6565);
|
|
64
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 2)).to.equal(0x6e65);
|
|
65
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 3)).to.equal(0x696e);
|
|
66
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 4)).to.equal(0x7869);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Test 32-bit integers
|
|
71
|
+
describe('int32', function () {
|
|
72
|
+
['i', 'l'].forEach((pFmt) => {
|
|
73
|
+
describe(`format ${pFmt}`, function () {
|
|
74
|
+
const pOp = Struct.OP[pFmt];
|
|
75
|
+
let data = Buffer.from([0x43, 0x53, 0x16, 0x79, 0x36, 0x17]);
|
|
76
|
+
it('int32 several readings with various endianness without offset', () => {
|
|
77
|
+
it('big endian', () => {
|
|
78
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x43531679);
|
|
79
|
+
});
|
|
80
|
+
it('little endian', () => {
|
|
81
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(0x79165343);
|
|
82
|
+
});
|
|
83
|
+
it('other values', () => {
|
|
84
|
+
data[0] = 0xff;
|
|
85
|
+
data[1] = 0xfe;
|
|
86
|
+
data[2] = 0xef;
|
|
87
|
+
data[3] = 0xfa;
|
|
88
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(-69638);
|
|
89
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(-84934913);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
data = Buffer.from([0x42, 0xc3, 0x95, 0xa9, 0x36, 0x17]);
|
|
93
|
+
it('int32 several readings with offset BE', () => {
|
|
94
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x42c395a9);
|
|
95
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 1)).to.equal(-1013601994);
|
|
96
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 2)).to.equal(-1784072681);
|
|
97
|
+
});
|
|
98
|
+
it('int32 several readings with offset LE', () => {
|
|
99
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(-1449802942);
|
|
100
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 1)).to.equal(917083587);
|
|
101
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 2)).to.equal(389458325);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
/*
|
|
108
|
+
//TODO add Out of Bound errors in StructDecoder
|
|
109
|
+
// Unit test for Read Out of Bound
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
describe('OOB', function () {
|
|
113
|
+
const buffer = [0x21, 0xda, 0x2c, 0x55]//Buffer.alloc(4);
|
|
114
|
+
const pEndianness = Endianness.BIG_ENDIAN;
|
|
115
|
+
|
|
116
|
+
[['b', 'Int8'], ['h', 'Int16BE'], ['i', 'Int32BE']].forEach((fmt) => {
|
|
117
|
+
|
|
118
|
+
let pFmt = fmt[0];
|
|
119
|
+
let bFmt = fmt[1];
|
|
120
|
+
// Verify that default offset works fine.
|
|
121
|
+
let pOp = Struct.OP[pFmt];
|
|
122
|
+
[Infinity, -1, -4294967295].forEach((offset) => {
|
|
123
|
+
it(`Test OOB with ${offset} in ${pFmt}`, ()=> {
|
|
124
|
+
expect(() => StructDecoder.int(pEndianness, pOp, buffer, offset))
|
|
125
|
+
.to.throw(RuntimeException.READ_OOB().message);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
});
|
|
133
|
+
// */
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {expect} from "chai";
|
|
2
|
+
import {Struct} from "../dist/index.js";
|
|
3
|
+
import {StructDecoder} from '../dist/src/StructDecoder.js';
|
|
4
|
+
import {Endianness} from "../dist/src/common.js";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
describe('StructDecoder test uint formats 8-16-32-64', function () {
|
|
8
|
+
|
|
9
|
+
// Test 8-bit unsigned integers
|
|
10
|
+
describe('uint8', function () {
|
|
11
|
+
const pOp = Struct.OP['B'];
|
|
12
|
+
const data = Buffer.from([0x23, 0xab, 0x7c, 0xef]);
|
|
13
|
+
let endianness = Endianness.BIG_ENDIAN;
|
|
14
|
+
it('uint8 several readings without offset, BE', () => {
|
|
15
|
+
expect(StructDecoder.int(endianness, pOp, data, 0)).to.equal(0x23);
|
|
16
|
+
data[0] = 0xff;
|
|
17
|
+
expect(StructDecoder.int(endianness, pOp, data, 0)).to.equal(255);
|
|
18
|
+
data[0] = 0x87;
|
|
19
|
+
expect(StructDecoder.int(endianness, pOp, data, 0)).to.equal(135);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('uint8 several readings with offset BE', () => {
|
|
23
|
+
expect(StructDecoder.int(endianness, pOp, data, 1)).to.equal(171);
|
|
24
|
+
expect(StructDecoder.int(endianness, pOp, data, 2)).to.equal(124);
|
|
25
|
+
expect(StructDecoder.int(endianness, pOp, data, 3)).to.equal(239);
|
|
26
|
+
});
|
|
27
|
+
endianness = Endianness.LITTLE_ENDIAN;
|
|
28
|
+
it('uint8 LE', () => {
|
|
29
|
+
expect(expect(StructDecoder.int(endianness, pOp, data, 1)).to.equal(171));
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Test 16-bit unsigned integers
|
|
34
|
+
describe('uint16', function () {
|
|
35
|
+
const pOp = Struct.OP['H'];
|
|
36
|
+
let data = Buffer.from([0x16, 0x79, 0x65, 0x6e, 0x69, 0x78]);
|
|
37
|
+
it('uint16 several readings with various endianness without offset', () => {
|
|
38
|
+
it('big endian', () => {
|
|
39
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x1679);
|
|
40
|
+
});
|
|
41
|
+
it('little endian', () => {
|
|
42
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(0x7916);
|
|
43
|
+
});
|
|
44
|
+
it('other values', () => {
|
|
45
|
+
data[0] = 0xff;
|
|
46
|
+
data[1] = 0x80;
|
|
47
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(65408);
|
|
48
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(33023);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
data = Buffer.from([0x77, 0x65, 0x65, 0x6e, 0x69, 0x78]);
|
|
52
|
+
it('uint16 various reading with offset BE', () => {
|
|
53
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x7765);
|
|
54
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 1)).to.equal(0x6565);
|
|
55
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 2)).to.equal(0x656e);
|
|
56
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 3)).to.equal(0x6e69);
|
|
57
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 4)).to.equal(0x6978);
|
|
58
|
+
});
|
|
59
|
+
it('uint16 various reading with offset LE', () => {
|
|
60
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(0x6577);
|
|
61
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 1)).to.equal(0x6565);
|
|
62
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 2)).to.equal(0x6e65);
|
|
63
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 3)).to.equal(0x696e);
|
|
64
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 4)).to.equal(0x7869);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Test 32-bit unsigned integers
|
|
69
|
+
describe('uint32', function () {
|
|
70
|
+
['I', 'L'].forEach((pFmt) => {
|
|
71
|
+
describe(`format ${pFmt}`, function () {
|
|
72
|
+
const pOp = Struct.OP[pFmt];
|
|
73
|
+
let data = Buffer.from([0x43, 0x53, 0x16, 0x79, 0x36, 0x17]);
|
|
74
|
+
it('several readings with various endianness without offset', () => {
|
|
75
|
+
it('big endian', () => {
|
|
76
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x43531679);
|
|
77
|
+
});
|
|
78
|
+
it('little endian', () => {
|
|
79
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(0x79165343);
|
|
80
|
+
});
|
|
81
|
+
it('other values', () => {
|
|
82
|
+
data[0] = 0xff;
|
|
83
|
+
data[1] = 0xfe;
|
|
84
|
+
data[2] = 0xef;
|
|
85
|
+
data[3] = 0xfa;
|
|
86
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(4294897658);
|
|
87
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(4278188783);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
data = Buffer.from([0x42, 0xc3, 0x95, 0xa9, 0x36, 0x17]);
|
|
91
|
+
it('uint32 several readings with offset BE', () => {
|
|
92
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 0)).to.equal(0x42c395a9);
|
|
93
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 1)).to.equal(3281365302);
|
|
94
|
+
expect(StructDecoder.int(Endianness.BIG_ENDIAN, pOp, data, 2)).to.equal(2510894615);
|
|
95
|
+
});
|
|
96
|
+
it('uint32 several readings with offset LE', () => {
|
|
97
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 0)).to.equal(2845164354);
|
|
98
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 1)).to.equal(917083587);
|
|
99
|
+
expect(StructDecoder.int(Endianness.LITTLE_ENDIAN, pOp, data, 2)).to.equal(389458325);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|