forked from vandrito/node-binstruct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
binstruct.js
453 lines (419 loc) · 13 KB
/
binstruct.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
//
// int64/uint64 support is limited in javascript. Each struct can specify
// a strategy for handling 64-bit values that occur in the data definition
// by passing the int64mode option.
//
// strict: Create a js 'number'; throw Error if the 64-bit number doesn't fit
// lossy: Create a js 'number'; use Infinity or -Infinity if it doesn't fit
// copy: Read 64-bit number into an 8-byte buffer
// slice: Return an 8-byte "slice" of the original buffer
// skip: Ignore 64-bit fields altogether
//
var int64modes = exports.int64modes = {
strict:'strict number',
lossy:'lossy number',
copy:'copy buffer',
slice:'slice buffer',
int64:'int64',
skip:'skip'
};
//
// Create a struct def
//
function StructDef(opts) {
this.name = opts && opts.name;
this.fields = [];
this.size = 0;
this.littleEndian = !!(opts && opts.littleEndian);
this.noAssert = !!(opts && opts.noAssert);
if(opts && 'int64mode' in opts) {
this.int64mode = opts.int64mode;
} else {
this.int64mode = int64modes.strict;
}
this.Wrapper = function BufferWrapper(buf) {
Object.defineProperty(this, '_buffer', {value:buf});
};
Object.defineProperty(this.Wrapper.prototype,
'_def', {value:this,writable:false});
Object.defineProperty(this.Wrapper.prototype,
'_fields', {get:function() { return this._def.fields; }});
// Check if each field equals its default value
this.Wrapper.prototype.checkValues = function() {
var wrapper = this;
var assert = require('assert');
this._fields.forEach(function(f) {
if('value' in f) {
assert.equal(wrapper[f.name], f.value, f.name);
}
});
return this;
};
// Write default values into the fields
this.Wrapper.prototype.writeValues = function() {
var wrapper = this;
this._fields.forEach(function(f) {
if('value' in f) {
wrapper[f.name] = f.value;
}
});
return this;
};
}
StructDef.prototype.field = function defineField(f, struct) {
var offset = f.offset = this.size;
this.size += f.size;
this.fields.push(f);
var desc = {
enumerable:true
};
var name = f.name;
var readImpl = f.read;
if(readImpl) {
desc.get = function() {
return readImpl.apply(this._buffer, [offset, this.noAssert, this, f]);
};
}
var writeImpl = f.write;
if(writeImpl) {
desc.set = function(value) {
return writeImpl.apply(this._buffer, [value, offset, this.noAssert, this, f]);
};
}
Object.defineProperty(this.Wrapper.prototype, name, desc);
Object.defineProperty(this, name, { value: f});
return this;
};
function createBufferReader(size) {
var reader;
reader = function readbuffer(offset, noAssert, def, field) {
// "this" should be a Buffer
if(!Buffer.isBuffer(this)) throw new Error('Should be applied to a buffer!');
if(!noAssert && offset + size > this.length) {
throw new Error('Field runs beyond the length of the buffer.');
}
return this.slice(offset, offset + size);
};
return reader;
}
function createBufferWriter(size) {
var writer = function writebuffer(val, offset, noAssert) {
// "this" is a Buffer
if(Buffer.isBuffer(val)) {
if(val.length != size) {
throw new Error('Buffer used as string field must be' + size + ' bytes long!');
}
val.copy(this, offset);
}
};
return writer;
}
function createStringReader(size) {
var reader;
reader = function readString(offset, noAssert, def, field) {
// "this" should be a Buffer
if(!Buffer.isBuffer(this)) throw new Error('Should be applied to a buffer!');
if(!noAssert && offset + size > this.length) {
throw new Error('Field runs beyond the length of the buffer.');
}
return this.toString('utf8', offset, offset + size);
};
return reader;
}
function createStringWriter(size) {
var writer = function writeString(val, offset, noAssert) {
// "this" is a Buffer
if(typeof(val) === 'string') {
this.fill(0, offset, offset + size);
this.write(val, offset, noAssert);
} else if(Buffer.isBuffer(val)) {
if(val.length != size) {
throw new Error('Buffer used as string field must be' + size + ' bytes long!');
}
val.copy(this, offset);
}
};
return writer;
}
function createInt64Reader(signed, littleEndian) {
var reader;
reader = function readInt64AsNumber(offset, noAssert, def, field) {
// "this" should be a Buffer
if(!Buffer.isBuffer(this)) throw new Error('Should be applied to a buffer!');
if(!noAssert && offset + 8 > this.length) {
throw new Error('Field runs beyond the length of the buffer.');
}
var int64mode = field.int64mode || def.int64mode || int64modes.strict;
if(int64mode === 'lossy number' || int64mode === 'strict number') {
var hi, lo;
if(littleEndian) {
hi = this.readUInt32LE(offset+4, noAssert);
lo = this.readUInt32LE(offset+0, noAssert);
} else {
hi = this.readUInt32BE(offset+0, noAssert);
lo = this.readUInt32BE(offset+4, noAssert);
}
// Does it fit in a the 53-bits supported by javascript numbers?
// hi contains the upper 32 bits, only 21 of which can be used,
// or 20 for unsigned numbers.
var lostBits = hi & 0xFFF00000;
// If the lost bits are all zero we're OK
// Also for a signed negative number the lost bits can be all
// one and we're OK
if(lostBits !== 0 && (!signed || lostBits !== 0xFFF00000)) {
// If the mode is "strict" then verify we don't lose any bits when
// we truncate the number to fit into a floating point number.
if(int64mode === 'strict number') {
// Data will be lost ... !
throw new Error('64-bit number too large for javascript number data type; bytes: '+this.toString('hex', offset, offset+8)+(littleEndian?' (little endian)':' (big endian)'));
} else {
if(!signed || (hi & 0x80000000) == 0) {
return Infinity;
} else {
return -Infinity;
}
}
}
return ((hi & 0x001FFFFF) << 32) | lo & 0xFFFFFFFF;
} else if(int64mode === 'slice buffer') {
return this.slice(offset, offset+8);
} else if(int64mode === 'copy buffer') {
var result = new Buffer(8);
this.copy(result, 0, offset, offset+8);
return result;
} else if(int64mode === 'skip') {
} else {
throw new Error('Unsupported int64mode: '+int64mode);
}
};
return reader;
};
function createInt64Writer(signed, littleEndian) {
var writer = function writeInt64(val, offset, noAssert) {
// "this" is a Buffer
if(val instanceof Number) {
var hi = val >> 32;
var lo = val & 0xFFFFFFFF;
this.writeUInt32(hi, offset+(littleEndian?4:0), noAssert);
this.writeUInt32(lo, offset+(littleEndian?0:4), noAssert);
} else if(Buffer.isBuffer(val)) {
if(val.length != 8) {
throw new Error('Buffer used as int64 field must be 8 bytes long!');
}
val.copy(this, offset);
}
};
return writer;
};
function setupDefiners() {
function defNumberTypeDefaultEndian(nameNoEndian) {
// Add one without the le/be suffix that uses the
// default endian. Both le and be definers should
// be set up when this is called.
var beDefiner = StructDef.prototype[nameNoEndian + 'be'];
var leDefiner = StructDef.prototype[nameNoEndian + 'le'];
StructDef.prototype[nameNoEndian] = function defineIntFieldDefaultEndian() {
var args = Array.prototype.slice.call(arguments);
var fn = this.littleEndian?leDefiner:beDefiner;
fn.apply(this, args);
return this;
};
}
function defBufferType() {
var lowerCaseTypeName = 'buffer';
var definer = function defineBufferField() {
var f = {
type: lowerCaseTypeName,
name: arguments[0],
size: 0,
read: undefined,
write: undefined
};
if (Buffer.isBuffer(arguments[1])) {
f.value = arguments[1];
f.size = arguments[1].length;
f.read = createBufferReader(f.size);
f.write = createBufferWriter(f.size);
} else if (typeof(arguments[1]) === 'number') {
f.size = arguments[1];
f.read = createBufferReader(f.size);
f.write = createBufferWriter(f.size);
} else if (typeof(arguments[1]) === 'object') {
for (var p in arguments[1]) {
f[p] = arguments[1][p];
}
f.read = createBufferReader(f.size);
f.write = createBufferWriter(f.size);
} else {
throw new Error('Unexpected argument ' + arguments[1] + ' with type ' + typeof(arguments[1]));
}
this.field(f);
return this;
};
StructDef.prototype[lowerCaseTypeName] = definer;
}
function defStringType() {
var lowerCaseTypeName = 'string';
var definer = function defineStringField() {
var f = {
type: lowerCaseTypeName,
name: arguments[0],
size: 0,
read: undefined,
write: undefined
};
if (Buffer.isBuffer(arguments[1])) {
f.value = arguments[1];
f.size = arguments[1].length;
f.read = createStringReader(f.size);
f.write = createStringWriter(f.size);
} else if (typeof(arguments[1]) === 'string') {
f.value = new Buffer(arguments[0]);
f.size = arguments[1].length;
f.read = createStringReader(f.size);
f.write = createStringWriter(f.size);
} else if (typeof(arguments[1]) === 'int') {
f.size = arguments[1];
f.read = createStringReader(f.size);
f.write = createStringWriter(f.size);
} else if (typeof(arguments[1]) === 'object') {
for (var p in arguments[1]) {
f[p] = arguments[1][p];
}
f.read = createStringReader(f.size);
f.write = createStringWriter(f.size);
} else {
throw new Error('Unexpected argument ' + arguments[1] + ' with type ' + typeof(arguments[1]));
}
this.field(f);
return this;
};
StructDef.prototype[lowerCaseTypeName] = definer;
}
function defNumberType(upperCaseTypeName, size, reader, writer) {
var readImpl = reader || Buffer.prototype['read'+upperCaseTypeName];
var writeImpl = writer || Buffer.prototype['write'+upperCaseTypeName];
var lowerCaseTypeName = upperCaseTypeName.toLowerCase();
var definer = function defineIntField() {
var f = {
type:lowerCaseTypeName,
name:this.fields.length,
size:size,
read:readImpl,
write:writeImpl
};
for(var i=0; i < arguments.length; i++) {
var arg = arguments[i];
if(!arg) {
// Ignore null, false, etc I guess ...
} else if(Buffer.isBuffer(arg) || typeof(arg) === 'number') {
f.value = arg;
} else if(typeof(arg) === 'string') {
f.name = arg;
} else if(typeof(arg) === 'object') {
Object.keys(arg).forEach(function(k) {
f[k] = arg[k];
});
} else {
throw new Error('Unexpected argument '+arg);
}
}
this.field(f);
return this;
};
StructDef.prototype[lowerCaseTypeName] = definer;
if(lowerCaseTypeName === 'uint8')
StructDef.prototype.byte = definer;
}
var intBits = [8, 16, 32, 64];
for(var i=0; i < 16; i++) {
var bits = intBits[(i>>2) % 4];
var signed = (i & 1) === 1;
var littleEndian = (i & 2) === 2;
if(littleEndian && bits === 8)
continue; // No endian-ness on single bytes
var upperCaseTypeName = (signed?"":"U")+"Int"+bits+(bits === 8?"":littleEndian?"LE":"BE");
var reader = null;
var writer = null;
if(bits == 64) {
reader = createInt64Reader(signed, littleEndian);
writer = createInt64Writer(signed, littleEndian);
}
defNumberType(upperCaseTypeName, bits >> 3, reader, writer);
// If we've now done both little and big endian, add the
// method that has no be/le suffix and uses the default
// endian-ness for this structure.
if(littleEndian) {
defNumberTypeDefaultEndian(upperCaseTypeName.slice(0, upperCaseTypeName.length-2).toLowerCase());
}
}
// Add float and double
for(var i=0; i < 4; i++) {
var littleEndian = (i & 1) === 1;
var double = (i >> 1) & 1 === 1;
var name = double ? "Double" : "Float";
var bits = double ? 64 : 32;
var upperCaseTypeName = name+(littleEndian?"LE":"BE");
defNumberType(upperCaseTypeName, bits >> 3);
if(littleEndian) {
defNumberTypeDefaultEndian(name.toLowerCase());
}
}
// Add string
defStringType();
// Add buffer
defBufferType();
}
setupDefiners();
StructDef.prototype.checkSize = function(expectedSize) {
if(expectedSize !== this.size) {
require('assert').fail(this.size, this.expectedSize, 'Wrong size', '!==');
}
return this;
};
StructDef.prototype.wrap = function wrapBuf(buf) {
var wrapper = new (this.Wrapper)(buf);
return wrapper;
};
StructDef.prototype.unpack = StructDef.prototype.read = function readFieldsFromBuf(buf, offset, noAssert) {
var data = {};
if(typeof(noAssert) === 'undefined') {
noAssert = this.noAssert;
}
if(typeof(offset) === 'undefined') {
offset = 0;
}
this.fields.forEach(function readField(f) {
var readImpl = f.read;
if(readImpl) {
data[f.name] = readImpl.apply(buf, [offset + f.offset, noAssert]);
}
});
return data;
};
StructDef.prototype.pack = StructDef.prototype.write = function writeFieldsIntoBuf(data, buf, offset, noAssert) {
if(typeof(noAssert) === 'undefined') {
noAssert = this.noAssert;
}
if(typeof(offset) === 'undefined') {
offset = 0;
}
if(typeof(buf) === 'undefined') {
buf = new Buffer(this.size + offset);
}
if(typeof(data) == 'undefined') {
data = {}; // Write all zeroes and defaults
}
this.fields.forEach(function writeField(f) {
var writeImpl = f.write;
if(writeImpl) {
var value = (f.name in data ? data[f.name] : f.value) || 0;
writeImpl.apply(buf, [value, offset + f.offset, noAssert]);
}
});
return buf;
};
exports.def = function createNewStructDef(opts) {
var def = new StructDef(opts);
return def;
};