-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
420 lines (350 loc) · 11.4 KB
/
index.js
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
const ReadContext = require('./ReadContext.js');
const WriteContext = require('./WriteContext.js');
/**
* @todo Fatal rules option
* @todo Validate rules on writing
* @todo raw data type
*/
class Struct {
/**
* Creates a new struct definition
* @param {=String} name Optional name. Only used for debugging purposes
*/
constructor(name = "") {
this.members = [];
this.arrays = [];
this.references = [];
this.rules = [];
this.statics = [];
this.name = name;
}
/**
* Adds a member to load
* @param {Type} type Datatype of the member to load
* @param {string} name Name of the member
* @returns {Struct}
*/
addMember(type, name) {
if (isNaN(type.SIZE)) throw new Error("Element with no fixed size is not allowed as struct member (" + name + ")!",);
let offset = this.members.reduce((size, mem) => size+mem.SIZE, 0);
this.members.push({type, name, offset});
return this;
}
/**
* Adds a an array to load from values inside this struct. The order is not important
* @param {Type} type Datatype of the member to load
* @param {string} name Name of the member
* @param {any} offsetMemberName Number or Name of the member which stores the address of the array
* @param {any} countMemberName Number or name of the member which stores the length of the array
* @param {Boolean} relative Is the address in the target member relative to the structs address?
* @returns {Struct}
*/
addArray(type, name, offsetMemberName, countMemberName, relative = false) {
if (typeof offsetMemberName == 'number') {
let value = offsetMemberName;
offsetMemberName = "_" + name + "_offset";
this.addStatic(offsetMemberName, value);
}
if (typeof countMemberName == 'number') {
let value = countMemberName;
countMemberName = "_" + name + "_count";
this.addStatic(countMemberName, value);
}
this.arrays.push({
name,
offsetMemberName,
countMemberName,
type,
relative
});
return this;
}
/**
* Adds a reference. References will appear as own members in the out data.
* @param {*} type Type of the reference
* @param {string} name Name of the new data member
* @param {string} memberName Name of the address containg existing data member
* @param {Boolean} relative Is the adress relative to the structs address?
* @returns {Struct}
*/
addReference(type, name, memberName, relative = false) {
if (typeof memberName == 'number') {
let value = memberName;
memberName = "_" + name + "_offset";
this.addStatic(memberName, value);
}
this.references.push({
type,
name,
memberName,
relative
});
return this;
}
/**
* Adds a static value. (will not be written to, or read from buffer)
* @param {string} name
* @param {any} value
*/
addStatic(name, value) {
this.statics.push({
name,
value
});
return this;
}
/**
* Adds a rule. Rules give extra validation options
* @param {Rule} rule The rule to add. Rules can be generated with the static methods of Struct.RULES
* @returns {Struct} itself
*/
addRule(rule) {
this.rules.push({
rule
});
return this;
}
/**
* Converts a buffer to an object with the structs structure
* @param {Buffer} buffer Buffer to read from
* @param {=number} offset Offset byte to start reading from
* @returns {Object} The data that was read
*/
read(buffer, offset = 0, readContext = null) {
let data = {};
let address = offset;
if (!readContext) readContext = new ReadContext({}, buffer);
let path = readContext.path;
for (let _static of this.statics) {
data[_static.name] = _static.value;
}
for (let member of this.members) {
readContext.path = path + "." + member.name;
data[member.name] = member.type.read(buffer, address, readContext);
address += member.type.SIZE;
}
for (let array of this.arrays) {
let arrayOffset = data[array.offsetMemberName];
let arrayCount = data[array.countMemberName];
if (array.relative) arrayOffset += offset;
let arr = [];
for (let i = 0; i < arrayCount; i++) {
readContext.path = path + "." + array.name + "[" + i + "]";
arr.push(
array.type.read(buffer, arrayOffset + i * array.type.SIZE, readContext)
);
}
// Does double mark some fields that are only read once :/
readContext.markAreaAsRead(arrayOffset, arrayCount * array.type.SIZE);
data[array.name] = arr;
if (readContext) {
readContext.arrays.push({
name: array.name,
start: arrayOffset,
count: arrayCount,
length: arrayCount * array.type.SIZE,
path: path + "." + array.name
});
}
}
for (let reference of this.references) {
readContext.path = path + "." + reference.name;
try {
let referenceOffset = data[reference.memberName];
if (reference.relative) referenceOffset += offset;
if (referenceOffset in readContext.referenceOffsets) {
data[reference.name] = readContext.referenceOffsets[referenceOffset];
} else {
let ref = reference.type.read(buffer, referenceOffset, readContext);
data[reference.name] = ref;
readContext.referenceOffsets[referenceOffset] = ref;
}
} catch (e) {
readContext.addError(e.message);
}
}
for (let i = 0; i < this.rules.length; i++) {
let rule = this.rules[i];
let response = rule.rule(data, buffer);
if (response) {
readContext.path = path + ":rule[" + i + "]";
readContext.addError(response);
}
}
if (readContext) {
readContext.markAreaAsRead(offset, this.SIZE);
if (readContext.hideReferenceValues) {
// Remove all values that are only used as pointers or other data structure data to keep the result clean
for (let array of this.arrays) {
if (typeof array.offsetMemberName == 'string') delete data[array.offsetMemberName];
if (typeof array.countMemberName == 'string') delete data[array.countMemberName];
}
for (let reference of this.references) {
if (typeof reference.memberName == 'string') delete data[reference.memberName];
}
}
}
return data;
}
/**
* Calculates the size needed for a buffer, to store the given object.
* @param {any} object The data object
* @returns {number} Size in bytes
*/
getWriteSize(object) {
let size = this.SIZE;
for (let array of this.arrays) {
let type = array.type;
for (let item of object[array.name]) {
if (type.getWriteSize) {
size += type.getWriteSize(item);
} else {
size += type.SIZE;
}
}
}
for (let reference of this.references) {
let type = reference.type;
if (type.getWriteSize) {
size += type.getWriteSize(object[reference.name]);
} else {
size += type.SIZE;
}
}
return size;
}
/**
* Writes data to an buffer, using this structure
* @param {any} object Data holding object
* @param {=WriteContext} context Internally used for the write process
* @param {=number} offset Offset, for start writing
*/
write(object, context = null, offset = 0) {
if (!context) {
context = new WriteContext({
bufferSize: this.getWriteSize(object)
});
context.allocate(this.SIZE);
}
let p = context.path;
for (let array of this.arrays) {
context.path = p + "." + array.name;
if (!(array.name in object)) {
context.addError("Attribute does not exists!");
continue;
}
let data = object[array.name];
if (!Array.isArray(data)) {
context.addError("Attribute is not an array!");
continue;
}
let arrayOffset = context.allocate(data.length * array.type.SIZE);
if (typeof array.offsetMemberName == 'string') {
object[array.offsetMemberName] = arrayOffset;
if (array.relative) {
object[array.offsetMemberName] -= offset;
}
}
if (typeof array.countMemberName == 'string') {
object[array.countMemberName] = data.length;
}
for (let index in data) {
context.path = p + "." + array.name + "[" + index + "]";
array.type.write(data[index], context, arrayOffset + index * array.type.SIZE);
}
}
for (let reference of this.references) {
context.path = p + "." + reference.name;
if (!(reference.name in object)) {
context.addError("Attribute does not exists!");
continue;
}
let data = object[reference.name];
let type = reference.type;
let size = type.SIZE;
if (type.getWriteSize) {
size += type.getWriteSize(object[reference.name]);
}
let referenceOffset = context.allocate(size);
if (typeof reference.memberName == 'string') {
object[reference.memberName] = referenceOffset;
if (reference.relative) {
object[reference.memberName] -= offset;
}
}
context.path = p + "." + reference.name;
type.write(data, context, referenceOffset);
}
for (let member of this.members) {
context.path = p + "." + member.name;
if (!(member.name in object)) {
context.addError("Attribute does not exists!");
continue;
}
context.path = p + "." + member.name;
member.type.write(object[member.name], context, offset + this.getOffsetByName(member.name));
}
return context;
}
/**
* backwards compatibility. Use .read instead!
*/
import() {
this.read(...arguments);
}
/**
* backwards compatibility. Use .readContext instead!
*/
report(buffer, offset, options = { monitorUsage: true }) {
return this.readContext(...arguments);
}
/**
* Parses an givien buffer. Returns the read context. It will contain the extracted data as well as some statistics like how many bytes were read and what errors occoured.
* @param {*} buffer Buffer to read from
* @param {*} offset Offset byte to start reading from
* @returns {ReadContext} The ReadContext
*/
readContext(buffer, offset, options) {
let context = new ReadContext(buffer, options);
context.data = this.read(buffer, offset, context);
context.checkForArrayCollisions();
return context;
}
/**
* Validates a structure
* @param {Buffer} buffer Buffer to read from
* @param {number} offset Byte offset in the buffer to begin reading from
* @returns {Boolean} True when valid
*/
validate(buffer, offset = 0) {
try {
this.read(buffer, offset)
return true;
} catch (e) {
return false;
}
}
/**
* The size, an instance of this struct will occupy. This does not contain the content of arrays.
* @returns {number}
*/
get SIZE() {
return this.members.reduce((val, member) => val + member.type.SIZE, 0);
}
/**
* Returns the relative offset of an attribute in this struct definition
* @param {string} name Name of the attribute
* @returns {number} Relative offset in bytes
*/
getOffsetByName(name) {
let address = 0;
for (let member of this.members) {
if (member.name == name) return address;
address += member.type.SIZE
}
return NaN;
}
}
Struct.TYPES = require('./types.js');
Struct.RULES = require('./rules.js');
module.exports = Struct;