-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
458 lines (403 loc) · 12.6 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
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
/**
* Default export `Struct`.
*/
// export default Struct;
module.exports = exports = Struct;
// compatibility
exports.Struct = Struct;
function byteField(p, offset) {
this.length = 1;
this.offset = offset;
this.get = function () {
return p.buf[offset];
}
this.set = function (val) {
p.buf[offset] = val;
}
}
function boolField(p, offset, length) {
this.length = length;
this.offset = offset;
this.get = function() {
return (p.buf[offset] > 0);
}
this.set = function (val) {
p.buf[offset] = val ? 1 : 0;
}
}
function intField(p, offset, length, le, signed) {
this.length = length;
this.offset = offset;
function bec(cb) {
for (var i = 0; i < length; i++)
cb(i, length - i - 1);
}
function lec(cb) {
for (var i = 0; i < length; i++)
cb(i, i);
}
function getUVal(bor) {
var val = 0;
bor(function (i, o) {
val += Math.pow(256, o) * p.buf[offset + i];
})
return val;
}
function getSVal(bor) {
var val = getUVal(bor);
if ((p.buf[offset + (le ? (length - 1) : 0)] & 0x80) == 0x80) {
val -= Math.pow(256, length);
}
return val;
}
function setVal(bor, val) {
bor(function (i, o) {
p.buf[offset + i] = Math.floor(val / Math.pow(256, o)) & 0xff;
});
}
var
nativeSuff = (signed?'':'U') + 'Int' + (length * 8) + (le?'LE':'BE'),
readMethod = Buffer.prototype['read' + nativeSuff], writeMethod = Buffer.prototype['write' + nativeSuff];
if (!readMethod) {
this.get = function () {
var bor = le ? lec : bec;
return (signed ? getSVal(bor) : getUVal(bor));
}
}
else {
this.get = function () {
return readMethod.call(p.buf, offset);
};
}
if (!writeMethod) {
this.set = function (val) {
var bor = le ? lec : bec;
setVal(bor, val);
}
}
else {
this.set = function (val) {
writeMethod.call(p.buf, val, offset);
}
}
}
function floatField(p, offset, le) {
this.length = 4;
this.offset = offset;
this.get = function () {
return le ? p.buf.readFloatLE(offset) : p.buf.readFloatBE(offset);
}
this.set = function (val) {
return le ? p.buf.writeFloatLE(val, offset) : p.buf.writeFloatBE(val, offset);
}
}
function doubleField(p, offset, le) {
this.length = 8;
this.offset = offset;
this.get = function () {
return le ? p.buf.readDoubleLE(offset) : p.buf.readDoubleBE(offset);
}
this.set = function (val) {
return le ? p.buf.writeDoubleLE(val, offset) : p.buf.writeDoubleBE(val, offset);
}
}
function charField(p, offset, length, encoding, secure) {
var self = this;
self.length = length;
self.offset = offset;
self.encoding = encoding;
self.secure = secure;
self.get = function () {
if (!length)
return;
var result = p.buf.toString(self.encoding, offset, (offset + length));
var strlen = result.indexOf("\0");
if (strlen == -1) {
return result;
} else {
return result.slice(0, strlen);
}
}
self.set = function (val) {
if (!length)
return;
// Be string is terminated with the null char, else troncate it
if (secure === true) {
// Append \0 to the string
val += "\0";
if (val.length >= length) {
val = val.substring(0, length - 1);
val += "\0";
}
// Write to buffer
p.buf.write(val, offset, val.length, self.encoding);
// Fill rest of the buffer with \0
var remainSpace = (length - val.length);
if (remainSpace > 0) {
p.buf.fill(0, (offset + val.length), offset + length);
}
} else {
// Trust Buffer class to write the string into the buffer
p.buf.write(val, offset, length, self.encoding);
}
}
}
function structField(p, offset, struct) {
this.length = struct.length();
this.offset = offset;
this.get = function () {
return struct;
}
this.set = function (val) {
struct.set(val);
}
this.allocate = function () {
struct._setBuff(p.buf.slice(offset, offset + struct.length()));
}
}
function arrayField(p, offset, len, type) {
var as = Struct();
var args = [].slice.call(arguments, 4);
args.unshift(0);
for (var i = 0; i < len; i++) {
if (type instanceof Struct) {
as.struct(i, type.clone());
} else if (type in as) {
args[0] = i;
as[type].apply(as, args);
}
}
this.length = as.length();
this.offset = offset;
this.allocate = function () {
as._setBuff(p.buf.slice(offset, offset + as.length()));
}
this.get = function () {
return as;
}
this.set = function (val) {
as.set(val);
}
}
function Struct() {
if (!(this instanceof Struct))
return new Struct;
var priv = {
buf : {},
allocated : false,
len : 0,
fields : {},
closures : []
}, self = this;
function checkAllocated() {
if (priv.allocated)
throw new Error('Cant change struct after allocation');
}
// Create handlers for various float Field Variants
[true, false].forEach(function (le) {
self['float' + (le ? 'le' : 'be')] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
var n = 4;
p.fields[key] = new floatField(p, p.len, le);
p.len += n;
});
return this;
}
});
// Create handlers for various double Field Variants
[true, false].forEach(function (le) {
self['double' + (le ? 'le' : 'be')] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
var n = 8;
p.fields[key] = new doubleField(p, p.len, le);
p.len += n;
});
return this;
}
});
// Create handlers for various Bool Field Variants
[1, 2, 3, 4].forEach(function (n) {
self['bool' + (n == 1 ? '' : n)] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new boolField(p, p.len, n);
p.len += n;
});
return this;
}
});
// Create handlers for various Integer Field Variants
[1, 2, 3, 4, 6, 8].forEach(function (n) {
[true, false].forEach(function (le) {
[true, false].forEach(function (signed) {
var name = 'word' + (n * 8) + (signed ? 'S' : 'U') + (le ? 'le' : 'be');
self[name] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new intField(p, p.len, n, le, signed);
p.len += n;
});
return this;
};
});
});
});
this.word8 = this.word8Ule;
['chars', 'charsnt'].forEach(function (c) {
self[c] = function (key, length, encoding) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new charField(p, p.len, length, encoding || 'ascii', (c == 'charsnt'));
p.len += length;
});
return this;
}
});
this.struct = function (key, struct) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new structField(p, p.len, struct.clone());
p.len += p.fields[key].length;
});
return this;
}
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
this.array = function (key, length, type) {
checkAllocated();
var args = [].slice.call(arguments, 1);
args.unshift(null);
args.unshift(null);
priv.closures.push(function (p) {
args[0] = p;
args[1] = p.len;
p.fields[key] = construct(arrayField, args);
p.len += p.fields[key].length;
});
return this;
}
var beenHere = false;
function applyClosures(p) {
if (beenHere)
return;
p.closures.forEach(function (el) {
el(p);
});
beenHere = true;
}
function allocateFields() {
for (var key in priv.fields) {
if ('allocate' in priv.fields[key])
priv.fields[key].allocate();
}
}
this._setBuff = this.setBuffer = function (buff, buffLength) {
applyClosures(priv);
if (typeof (buffLength) === 'number') {
if (buffLength > buff.length) {
throw new Error('Invalid specified buffer size !');
}
priv.buf = buff.slice(0, buffLength);
} else {
priv.buf = buff;
}
if (priv.buf.length < priv.len) {
throw new Error('Buffer size too small for struct layout !');
}
allocateFields();
priv.allocated = true;
}
this.allocate = function () {
applyClosures(priv);
if (Buffer.alloc) {
priv.buf = Buffer.alloc(priv.len);
} else {
priv.buf = new Buffer(priv.len);
priv.buf.fill(0);
}
allocateFields();
priv.allocated = true;
return this;
}
this._getPriv = function () {
return priv;
}
this.getOffset = function (field) {
if (priv.fields[field]) return priv.fields[field].offset;
}
this.clone = function () {
var c = new Struct;
var p = c._getPriv();
p.closures = priv.closures.slice(0);
return c;
}
this.length = function () {
applyClosures(priv);
return priv.len;
}
this.get = function (key) {
if (key in priv.fields) {
return priv.fields[key].get();
} else
throw new Error('Can not find field ' + key);
}
this.set = function (key, val) {
if (arguments.length == 2) {
if (key in priv.fields) {
priv.fields[key].set(val);
} else
throw new Error('Can not find field ' + key);
} else if (Buffer.isBuffer(key)) {
this._setBuff(key);
} else {
for (var k in key) {
this.set(k, key[k]);
}
}
}
this.buffer = function () {
return priv.buf;
}
function getFields() {
var fields = {};
Object.keys(priv.fields).forEach(function (key) {
var setFunc, getFunc;
if (priv.fields[key] instanceof structField ||
priv.fields[key] instanceof arrayField) {
getFunc = function () {
return priv.fields[key].get().fields;
};
setFunc = function (newVal) {
self.set(key, newVal);
};
}
else {
getFunc = priv.fields[key].get;
setFunc = priv.fields[key].set;
};
Object.defineProperty(fields, key, {
get : getFunc,
set : setFunc,
enumerable : true
});
});
return fields;
};
var _fields;
Object.defineProperty(this, 'fields', {
get : function () {
if (_fields)
return _fields;
return (_fields = getFields());
},
enumerable : true,
configurable : true
});
}