-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
704 lines (678 loc) · 21 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
'use strict'; /*jshint node:true*/
var os = require('os');
var bintag = module.exports = Object.assign(tag(''), {
tag,
LE: tag('LE:'),
BE: tag('BE:'),
hex: tag('x:'),
});
function tag(options){
let tag_fn = function(parts){
let values = new Array(arguments.length-1);
for (let i = 1; i<arguments.length; i++)
values[i-1] = arguments[i];
return new Template(options, parts, values).create();
};
tag_fn.compile = function(parts){
let values = new Array(arguments.length-1);
for (let i = 1; i<arguments.length; i++)
values[i-1] = arguments[i];
return new Template(options, parts, values);
};
tag_fn.tag = more_options=>tag(options+' '+more_options);
return tag_fn;
}
function parse(parts, values)
{
function* characters(){
for (let i = 0; i<parts.length; i++)
{
if (i)
yield new Value(values[i-1]);
for (let c of parts[i])
yield c;
}
}
function* tokens(){
let cur = [];
function token(){
let res = cur;
cur = [];
return res;
}
for (let c of characters())
{
switch (c)
{
case '#':
if (cur.length)
yield token();
cur.push(c);
break;
case '*':
case ':':
case '(':
cur.push(c);
yield token();
break;
case ')':
if (cur.length)
yield token();
cur.push(c);
yield token();
break;
default:
if (/[\s]/.test(c))
{
if (cur.length)
yield token();
}
else
cur.push(c);
}
}
if (cur.length)
yield token();
}
let groups = [undefined], gen = tokens();
function* group(inner, context){
let repeater;
function wrap_repeater(item){
if (repeater!==undefined)
{
item = new ItemRepeat(context, repeater, item);
repeater = undefined;
}
return item;
}
for (let step = gen.next(); !step.done; step = gen.next())
{
let token = step.value;
if (token.length==1 && token[0]===')')
{
if (!inner || repeater!==undefined)
throw new ParseError('Unexpected', token);
return;
}
if (token.length==3 && token[1]==='E' && token[2]===':')
{
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
switch (token[0])
{
case 'B':
case 'L':
context.endianness = token[0]+'E';
continue;
default:
throw new ParseError('Unrecognized option',
token.slice(0, -1));
}
}
if (token[token.length-1]===':')
{
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
switch (token[0])
{
case 'i':
if (token.length<=2)
break;
context.format = new FormatInt(
parse_int(token.slice(1, -1)));
continue;
case 'f':
case 'd':
if (token.length!=2)
break;
context.format = new FormatFloat(token[0]);
continue;
case 'x':
if (token.length!=2)
break;
context.format = new FormatHex();
continue;
case 'a':
case 'u':
case 'U':
context.format = new FormatString(token.slice(0, -1));
continue;
}
throw new ParseError('Unrecognized option',
token.slice(0, -1));
}
if (token.length==1 && token[0] instanceof Value)
{
yield wrap_repeater(new ItemValue(context, token[0].value));
continue;
}
if (token[token.length-1]==='*')
{
if (token.length==1)
throw new ParseError('Syntax error', token);
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
repeater = parse_int(token.slice(0, -1));
continue;
}
if (token[0]==='#')
{
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
if (token.length==2 && token[1] instanceof Value)
{
yield new ItemLength(context,
new ItemValue(context, token[1].value));
continue;
}
if (token.length==2 && token[1]==='(')
{
let index = groups.length;
groups.push(undefined);
let item = groups[index] = new ItemGroup(group(true,
new Context(context)));
yield new ItemLength(context, item);
continue;
}
yield new ItemGroupLength(context, groups,
parse_int(token.slice(1)));
continue;
}
if (token[0]==='@')
{
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
if (token.length==1 || token[1] instanceof Value)
throw new ParseError('Syntax error', token);
yield new ItemGroupOffset(context, groups,
parse_int(token.slice(1)));
continue;
}
if (token.length==1 && token[0]==='(')
{
let index = groups.length;
groups.push(undefined);
yield groups[index] = wrap_repeater(
new ItemGroup(group(true, new Context(context))));
continue;
}
if (token[0]==='!')
{
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
yield new ItemAlign(context,
token.length>1 ? parse_int(token.slice(1)) : undefined);
continue;
}
if (token[0]==='=')
{
if (repeater!==undefined)
throw new ParseError('Unexpected', token);
yield new ItemSkip(context, parse_int(token.slice(1)));
continue;
}
if (context.format)
{
yield wrap_repeater(new ItemValue(context,
context.format.parse(token)));
continue;
}
throw new ParseError('Format not specified', token);
}
if (inner || repeater!==undefined)
throw new ParseError('Premature end of template');
}
groups[0] = new ItemGroup(group(false, new Context()));
groups[0].bind(0);
return groups;
}
function* flatten(obj)
{
if (typeof obj=='object' && obj!==null && obj[Symbol.iterator]
&& !(obj instanceof Buffer))
{
for (let item of obj)
yield *flatten(item);
}
else
yield obj;
}
function parse_plain(token)
{
for (let c of token)
{
if (c instanceof Value)
throw new ParseError('Unexpected substitution', token);
}
return token.join('');
}
function parse_int(token)
{
if (token.length==1 && token[0] instanceof Value)
return Math.trunc(+token[0].value);
let plain = parse_plain(token);
if (/^([-+]?\d+|0x[\da-f]+)$/i.test(plain))
return +plain;
throw new ParseError('Integer expected', token);
}
function parse_num(token)
{
if (token.length==1 && token[0] instanceof Value)
return +token[0].value;
let plain = parse_plain(token);
let res = +plain;
if (isNaN(res) && plain!='NaN')
throw new ParseError('Number expected', token);
return res;
}
class Template {
constructor(options, parts, values){
if (options)
{
parts = parts.slice();
parts[0] = options+' '+parts[0];
}
parse(parts, values).forEach(
(group, i) => this[i] = new TemplateGroup(group));
}
create(){
let buf = new Buffer(this.length);
this.write(buf);
return buf;
}
get length(){ return this[0].group.length; }
write(buf, offset){
let root = this[0].group;
root.encode(buf, offset||0);
return root.length;
}
}
class TemplateGroup {
constructor(group){ this.group = group; }
get length(){ return this.group.inner_length; }
get offset(){ return this.group.offset; }
}
class Value {
constructor(value){ this.value = value; }
toString(){ return ''; }
}
class Context {
constructor(orig){
this.endianness = orig ? orig.endianness : os.endianness();
this.format = orig && orig.format;
}
}
class Format {
constructor(align){ this.align = align; }
parse(token){ throw new ParseError('Syntax error', token); }
}
class FormatInt extends Format {
constructor(size){
if (size<1 || size>6)
throw new ParseError('Unsupported integer size: '+size);
super(size);
}
parse(token){ return parse_int(token); }
length(value){ return this.align; }
encode(value, endianness, buf, offset){
value = +value;
if (!isFinite(value))
throw new ParseError('Integer expected');
switch (endianness){
case 'LE':
if (value<0)
buf.writeIntLE(value, offset, this.align);
else
buf.writeUIntLE(value, offset, this.align);
break;
case 'BE':
if (value<0)
buf.writeIntBE(value, offset, this.align);
else
buf.writeUIntBE(value, offset, this.align);
break;
}
return this.align;
}
}
class FormatFloat extends Format {
constructor(type){ super(type=='f' ? 4 : 8); }
parse(token){ return parse_num(token); }
length(value){ return this.align; }
encode(value, endianness, buf, offset){
value = +value;
if (isNaN(value))
value = NaN;
switch (endianness+this.align)
{
case 'LE4': buf.writeFloatLE(value, offset); break;
case 'BE4': buf.writeFloatBE(value, offset); break;
case 'LE8': buf.writeDoubleLE(value, offset); break;
case 'BE8': buf.writeDoubleBE(value, offset); break;
}
return this.align;
}
}
class FormatHex extends Format {
constructor(){ super(1); }
parse(token){
let res = [];
for (let i = 0; i<token.length; i++)
{
if (token[i] instanceof Value)
res.push(token[i].value);
else
{
let pair = token[i]+token[i+1];
if (!/^[\da-f]{2}$/i.test(pair))
throw new ParseError('Invalid hex sequence', token);
res.push(parseInt(pair, 16));
i++;
}
}
return res;
}
length(value){ return 1; }
encode(value, endianness, buf, offset){
value = +value;
if (!isFinite(value))
throw new ParseError('Value is not an integer');
value = value|0;
if (value<-128 || value>255)
throw new ParseError('Value out of byte range: '+value);
buf[offset] = value;
return 1;
}
}
class FormatString extends Format {
constructor(spec){
super(spec[0]=='U' ? 2 : 1);
this.type = spec[0];
this.size = 0;
this.terminated = 0;
this.meta_size = 0;
this.limit = 0;
this.encoding = undefined;
switch (this.type)
{
case 'a': this.encoding = 'ascii'; break;
case 'u': this.encoding = 'utf8'; break;
case 'U': this.encoding = 'utf16le'; break;
}
let pos = 1;
let size_str = '';
while (pos<spec.length && /\d/.test(spec[pos]))
size_str += spec[pos++];
if (size_str)
this.size = +size_str;
else if (spec[pos] instanceof Value)
this.size = +spec[pos++].value;
if (!isFinite(this.size) || this.size<0)
throw new ParseError('Invalid string size', spec);
this.size = this.size|0;
this.limit = this.size;
if (spec[pos]==='z')
{
pos++;
this.terminated = this.type=='U' ? 2 : 1;
}
else if (spec[pos]==='p')
{
let meta_size_str = '';
pos++;
while (pos<spec.length && /\d/.test(spec[pos]))
meta_size_str += spec[pos++];
if (meta_size_str)
this.meta_size = +meta_size_str;
else if (spec[pos] instanceof Value)
this.meta_size = +spec[pos++].value;
else
throw new ParseError('Invalid string format', spec);
if (!isFinite(this.meta_size)
|| this.meta_size<1 || this.meta_size>4)
{
throw new ParseError('Invalid string format', spec);
}
this.meta_size = this.meta_size|0;
if (this.size && this.size<=this.meta_size)
throw new ParseError('Invalid string format', spec);
if (this.meta_size<4)
{
let limit = 1<<(this.meta_size*8);
this.limit = this.limit ? Math.min(this.limit, limit) : limit;
}
}
if (pos!=spec.length)
throw new ParseError('Invalid string format', spec);
}
length(value){
if (this.size)
return this.size;
let res = Buffer.byteLength(String(value), this.encoding)
+this.terminated+this.meta_size;
return this.limit ? Math.min(res, this.limit) : res;
}
encode(value, endianness, buf, offset){
let len = this.length(value);
let start = offset+this.meta_size;
let max = len-this.meta_size-this.terminated;
let bytes = buf.write(String(value), start, max, this.encoding);
if (this.type=='U' && endianness=='BE')
{
for (let i = start; i<start+bytes; i+=2)
buf.writeUInt16BE(buf.readUInt16LE(i), i);
}
if (this.meta_size)
{
let size = this.type=='U' ? bytes/2 : bytes;
switch (endianness)
{
case 'LE': buf.writeUIntLE(size, offset, this.meta_size); break;
case 'BE': buf.writeUIntBE(size, offset, this.meta_size); break;
}
}
buf.fill(0, start+bytes, offset+len);
return len;
}
}
class Item {
constructor(context){
this.context = new Context(context);
this.offset = undefined;
this.length = 0;
}
bind(offset){ this.offset = offset; }
get inner_length(){ return this.length; }
}
class ItemValue extends Item {
constructor(context, value){
super(context);
this.value = value;
if (this.value && typeof this.value.next=='function')
this.value = Array.from(this.value);
}
bind(offset){
super.bind(offset);
for (let item of flatten(this.value))
{
if (item instanceof Buffer)
this.length += item.length;
else if (item instanceof Template)
this.length += item.length;
else
{
if (!this.context.format)
throw new ParseError('Format not specified');
this.length += this.context.format.length(item);
}
}
}
encode(buf, base){
let offset = base+this.offset;
for (let item of flatten(this.value))
{
if (item instanceof Buffer)
offset += item.copy(buf, offset);
else if (item instanceof Template)
offset += item.write(buf, offset);
else
{
offset += this.context.format.encode(item,
this.context.endianness, buf, offset);
}
}
}
}
class ItemAlign extends Item {
constructor(context, align){
super(context);
this.align = align;
if (align===undefined)
{
if (!this.context.format)
throw new ParseError('Format not specified');
this.align = this.context.format.align;
}
if (this.align<1)
throw new ParseError('Invalid alignment: '+this.align);
}
bind(offset){
super.bind(offset);
this.length = this.align - this.offset%this.align;
if (this.length==this.align)
this.length = 0;
}
encode(buf, base){
let offset = base+this.offset;
buf.fill(0, offset, offset+this.length);
}
}
class ItemSkip extends Item {
constructor(context, target){
super(context);
this.target = target;
}
bind(offset){
super.bind(offset);
this.length = this.target-this.offset;
if (this.length<0)
{
throw new ParseError('Cannot skip from offset '+this.offset
+' to offset '+this.target);
}
}
encode(buf, base){
let offset = base+this.offset;
buf.fill(0, offset, offset+this.length);
}
}
class ItemRepeat extends Item {
constructor(context, repeat, item){
super(context);
this.repeat = repeat;
this.item = item;
if (this.repeat<0)
throw new ParseError('Negative repeat count: '+this.repeat);
}
bind(offset){
super.bind(offset);
this.item.bind(0);
this.length = this.repeat*this.item.length;
}
encode(buf, base){
let offset = base+this.offset;
for (let i = 0; i<this.repeat; i++)
{
this.item.encode(buf, offset);
offset += this.item.length;
}
}
get inner_length(){ return this.item.length; }
}
class ItemLength extends Item {
constructor(context, item){
super(context);
this.item = item;
}
bind(offset){
super.bind(offset);
if (!this.context.format)
throw new ParseError('Format not specified');
this.length = this.context.format.length(0);
this.item.bind(0);
}
encode(buf, base){
let offset = base+this.offset;
this.context.format.encode(this.item.length, this.context.endianness,
buf, offset);
}
}
class ItemGroupLength extends Item {
constructor(context, groups, index){
super(context);
this.groups = groups;
this.index = index;
}
bind(offset){
super.bind(offset);
if (!this.context.format)
throw new ParseError('Format not specified');
this.length = this.context.format.length(0);
if (this.index<0 || this.index>=this.groups.length)
throw new ParseError('Invalid reference: #'+this.index);
}
encode(buf, base){
let offset = base+this.offset;
this.context.format.encode(this.groups[this.index].inner_length,
this.context.endianness, buf, offset);
}
}
class ItemGroupOffset extends Item {
constructor(context, groups, index){
super(context);
this.groups = groups;
this.index = index;
}
bind(offset){
super.bind(offset);
if (!this.context.format)
throw new ParseError('Format not specified');
this.length = this.context.format.length(0);
if (this.index<0 || this.index>=this.groups.length)
throw new ParseError('Invalid reference: @'+this.index);
}
encode(buf, base){
let offset = base+this.offset;
this.context.format.encode(this.groups[this.index].offset,
this.context.endianness, buf, offset);
}
}
class ItemGroup extends Item {
constructor(items){
super();
this.items = Array.from(items);
}
bind(offset){
super.bind(offset);
for (let item of this.items)
{
item.bind(offset+this.length);
this.length += item.length;
}
}
encode(buf, base){
for (let item of this.items)
item.encode(buf, base);
}
}
class ParseError extends Error {
constructor(message, token){
if (token)
{
message += ': ';
for (let c of token)
message += c instanceof Value ? '${...}' : c;
}
super('bintag template parse error: '+message);
}
}