-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
1388 lines (1274 loc) · 49.8 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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var parse = require('./pattern').parse,
ieee754 = require('./ieee754'),
util = require('util'),
__slice = [].slice;
function classify () {
var i, I, name;
for (i = 0, I = arguments.length; i < I; i++) {
name = arguments[i].name;
if (name[0] == "_")
this.__defineGetter__(name.slice(1), arguments[i]);
else
this[arguments[i].name] = arguments[i];
}
return this;
}
// The default transforms built into Packet.
var transforms =
// Convert the value to and from the given encoding.
{ str: function (encoding, parsing, field, value) {
var i, I, ascii = /^ascii$/i.test(encoding);
if (parsing) {
value = new Buffer(value);
// Broken and waiting on [297](http://github.com/ry/node/issues/issue/297).
// If the top bit is set, it is not ASCII, so we zero the value.
if (ascii) {
for (i = 0, I = value.length; i < I; i++) {
if (value[i] & 0x80) value[i] = 0;
}
encoding = "utf8"
}
var length = value.length;
return value.toString(encoding, 0, length);
} else {
var buffer = new Buffer(value, encoding);
if (ascii) {
for (var i = 0, I = buffer.length; i < I; i++) {
if (value.charAt(i) == '\u0000') buffer[i] = 0;
}
}
return buffer;
}
}
// Convert to and from ASCII.
, ascii: function (parsing, field, value) {
return transforms.str("ascii", parsing, field, value);
}
// Convert to and from UTF-8.
, utf8: function (parsing, field, value) {
return transforms.str("utf8", parsing, field, value);
}
// Add padding to a value before you write it to stream.
, pad: function (character, length, parsing, field, value) {
if (! parsing) {
while (value.length < length) value = character + value;
}
return value;
}
// Convert a text value from alphanumeric to integer.
, atoi: function (base, parsing, field, value) {
return parsing ? parseInt(value, base) : value.toString(base);
}
// Convert a text value from alphanumeric to float.
, atof: function (parsing, field, value) {
return parsing ? parseFloat(value) : value.toString();
}
};
/*
function die () {
console.log.apply(console, [].slice.call(arguments, 0));
process.exit(1);
}
function say () { console.log.apply(console, [].slice.call(arguments, 0)) }
*/
// The `Definition` class is contained by the `Serializer` and parser. We expose
// public methods by explicitly adding the methods to the `Serializer` or
// `Parser` when we create them. The `Definition` class references only enclosed
// variables, but it does use prototypical inheritance to extend the collections
// of packet patterns and transforms.
function Source () {
this._lines = [];
this._indent = 0;
this._hoisted = [];
}
Source.prototype.indent = function () {
this._indent++;
}
Source.prototype.dedent = function () {
this._indent--;
}
Source.prototype._indentation = function () {
return new Array(this._indent + 1).join(' ')
}
Source.prototype.line = function () {
var line = __slice.call(arguments).join('');
if (line.length) this._lines.push(this._indentation() + line);
else this._lines.push('');
}
Source.prototype.text = function () {
var text = __slice.call(arguments).join('');
this._lines[this._lines.length - 1] += text;
}
Source.prototype.consume = function (prefix, source) {
if (!source) {
source = prefix;
prefix = null;
}
if (prefix == null) {
var indentation = this._indentation();
this._lines.push.apply(this._lines, source._lines.map(function (line) {
return indentation + line;
}))
} else {
this.text(prefix);
this.text(source._lines[0]);
}
this.hoist.apply(this, source._hoisted);
source._lines.length = 0;
source._hoisted.length = 0;
}
Source.prototype.hoist = function () {
__slice.call(arguments).forEach(function (variable) {
if (!~this._hoisted.indexOf(variable)) this._hoisted.push(variable);
}.bind(this));
}
Source.prototype.block = function (source) {
source = source.split(/\n/);
source.shift();
source.pop();
var spaces = source[0].length - source[0].replace(/^\s+/, '').length;
source.forEach(function (line) {
line = line.substring(spaces).replace(/\s+$/, '');
if (/\S/.test(line)) this.line(line);
else this.line(line);
}.bind(this));
}
Source.prototype.replace = function () {
var last = this._lines[this._lines.length - 1];
last = last.replace.apply(last, __slice.call(arguments));
this._lines[this._lines.length - 1] = last;
}
Source.prototype.define = function () {
var source = new Source;
source.line('function (', __slice.call(arguments).join(', '), ') {')
source.indent();
if (this._hoisted.length) {
source.line('var ', this._hoisted.join(', '), ';');
source.line();
}
source.consume(this);
source.dedent();
source.line('}');
return String(source);
}
Source.prototype.toString = function () {
if (this._lines.length > 1) {
return this._lines.join('\n') + '\n';
} else {
return this._lines[0];
}
}
function Definition (context, packets, transforms, options) {
packets = Object.create(packets);
transforms = Object.create(transforms);
options = options || {};
if (!('compile' in options)) options.compile = true;
var precompiler = options.precompiler || function (pattern, source) {
return Function.call(Function,
'incremental', 'pattern', 'ieee754', 'callback', source.join('\n'));
}
function precompile (pattern) {
if (!options.compile) return;
function unsigned (source, variable, field, increment) {
var little = field.endianness == 'l',
bytes = field.bytes,
bite = little ? 0 : bytes - 1,
direction = little ? 1 : -1,
stop = little ? bytes : -1,
line = new Source,
assign;
var sign = '0x80' + new Array(field.bytes).join('00');
var mask = '0xff' + new Array(field.bytes).join('ff');
if (field.signed) {
source.hoist('value');
source.line('value =');
assign = true;
} else {
source.line(variable);
}
if (bytes != 1) {
source.indent();
}
while (bite != stop) {
if (increment) {
line.line('buffer[start++]');
} else {
line.line('buffer[start + ', offset++, ']');
}
line.text(' * ' + Math.pow(256, bite) + ' +');
line.replace(/ \* 1 \+$/, ' +');
line.replace(/start \+ 0/, 'start');
if (bytes == 1) {
source.consume(' ', line);
} else {
source.consume(line);
}
bite += direction;
}
source.replace(/ \+$/, ';');
if (bytes != 1) {
source.dedent();
}
if (field.signed) {
source.line(variable + ' !(value & ' + sign + ') ? value : (' + mask + ' - value + 1) * -1;');
}
return source;
}
function floating (source, variable, field) {
var little = field.endianness == 'l',
suffix = little ? 'LE' : 'BE',
bytes = field.bytes,
size = bytes == 4 ? options.buffersOnly ? 'Float' : 'Single' : 'Double',
bite = little ? 0 : bytes - 1,
direction = little ? 1 : -1,
gather = new Source,
stop = little ? bytes : -1,
line;
source.hoist('value');
if (options.buffersOnly) {
source.line(variable + ' buffer.read' + size + suffix + '(' + offset + ', true)');
} else {
gather.line('value = new Array(' + bytes + ');');
while (bite != stop) {
line = 'value[' + bite + '] = buffer[start + ' + (offset++) + '];'
line = line.replace(/start \+ 0/, 'start');
gather.line(line);
bite += direction;
}
source.consume(gather);
source.line(variable + ' ieee754.fromIEEE754' + size + '(value.reverse());');
}
return source;
}
function element (assign, variable, field, increment) {
switch (field.type) {
case "f":
return floating(assign, variable, field, increment);
default:
return unsigned(assign, variable, field, increment);
}
}
function rangeCheck (condition) {
var source = new Source;
source.block(' \n\
if (' + condition + ') { \n\
return incremental.call(this, buffer, start, end, pattern, 0, object, callback); \n\
} \n\
')
source.line();
return source;
}
var method = new Source;
var section = new Source;
var offset = 0;
var sum = 0;
var sums = [];
var fixed = true;
var length = [];
method.hoist('object = {}');
function fix () {
if (!fixed) fixed = true;
}
function unfix () {
}
// TODO: Slice buffers. If signed, then slice and fixup.
// TODO: With a fixed length buffer for a terminated array, give up early,
// not at each test.
// TODO: Would it be faster to have the same scan for a terminator, then
// come back and iterate? No because in the best case, we're not giving up.
// How about, if it's not fixed, give up and increment? So, once we're
// moving the `start`, we give up on trying to have nice fixed numbers.
var offset = 0;
pattern.forEach(function (field, index) {
if (field.lengthEncoding) return;
if (field.endianness == 'x') {
fix();
sum += field.bytes * field.repeat;
offset += field.bytes * field.repeat;
} else if (field.arrayed) {
section.hoist('array');
if (index && pattern[index - 1].lengthEncoding) {
var counter = pattern[index - 1];
method.hoist('first = start');
if (!~length.indexOf('(start - first)')) length.push('(start - first)');
method.hoist('count');
method.hoist('i');
element(section, 'count = ', counter, true);
section.line('object[' + JSON.stringify(field.name) + '] = array = [];');
section.line('for (i = 0; i < count; i++) {');
element(section, ' array[i] =', field, true);
section.line('}');
section.line();
} else if (field.terminator) {
section.line('object[' + JSON.stringify(field.name) + '] =');
if (sum) {
rangeCheck(source, 'end - start < ' + sum);
sums.push(sum);
sum = 0;
}
section.hoist('value');
section.hoist('first = start');
section.text(' array = [];');
if (field.terminator.length == 1) {
section.line('for (;;) {');
if (field.repeat != Number.MAX_VALUE) {
section.line(' if (array.length == ' + field.repeat + ') break;');
}
section.indent();
if (field.bytes == 1) {
section.consume(rangeCheck('start == end'));
} else {
section.consume(rangeCheck('start + ' + (field.bytes - 1) + ' >= end'));
}
element(section, 'value =', field);
section.line(' start += ' + field.bytes + ';');
section.line(' if (value == ' + field.terminator[0] + ') break;');
section.line(' array.push(value);');
section.dedent();
section.line('}')
} else {
section.line('for (;;) {');
section.indent();
if (field.bytes == 1) {
section.consume(rangeCheck('start == end'));
} else {
section.consume(rangeCheck('start + ' + (field.bytes - 1) + ' >= end'));
}
if (field.repeat != Number.MAX_VALUE) {
section.line('if (array.length == ' + field.repeat + ') break;');
}
var condition = field.terminator.map(function (value, index) {
return '' + value + ' == array[array.length - ' +
Math.abs((index - field.terminator.length)) + ']';
}).join(' && ')
element(section, 'value =', field);
section.block('\n\
start += ' + field.bytes + '; \n\
array.push(value); \n\
if (' + condition + ') { \n\
array.splice(-' + field.terminator.length + '); \n\
break; \n\
} \n\
');
section.dedent();
section.line('}')
}
if (!~length.indexOf('(start - first)')) length.push('(start - first)');
if (field.repeat != Number.MAX_VALUE) {
// TODO: Ugly. Faster with offset to track count instead of array length?
// TODO: Slice buffers.
section.line('start += ' + field.repeat + ' - array.length;');
section.line('if (array.length < ' + field.repeat + ') {');
section.line(' start -= ' + field.terminator.length + ';');
section.line('}');
}
offset = 0;
} else {
section.line('object[' + JSON.stringify(field.name) + '] =');
section.text(' array = new Array(' + field.repeat + ');');
for (var i = 0, I = field.repeat; i < I; i++) {
element(section, 'array[' + i + '] =', field);
}
sum += field.bytes * field.repeat;
}
} else {
element(section, 'object[' + JSON.stringify(field.name) + '] =', field);
sum += field.bytes;
}
section.line();
});
if (sum) {
var range = rangeCheck('end - start < ' + sum);
method.consume(range);
method.consume(section);
sums.push(sum);
sum = 0;
} else {
method.consume(section);
}
if (sums.length) {
length.push(sums.reduce(function (sum, value) { return sum + value }, 0));
}
method.block(' \n\
this.length = ' + length.join(' + ') + '; \n\
\n\
this.parse = null \n\
\n\
callback(object); \n\
\n\
if (this.parse) { \n\
return ' + length.join(' + ') + ' + this.parse(buffer, ' + length.join(' + ') + ', end);\n\
} else { \n\
return ' + length.join(' + ') + '; \n\
} \n\
')
var parser = ('return ' + method.define('buffer', 'start', 'end')).split(/\n/);
parser.pop();
pattern.builder = precompiler(pattern, parser);
}
function compile (pattern) {
var parsed = parse(pattern);
parsed.hasAlternation = parsed.some(function (field) { return field.alternation });
if (parsed.every(function (part) {
return ! part.alternation &&
! part.packing &&
! part.pipeline
})) {
precompile(parsed);
}
return parsed;
}
function packet (name, pattern) {
packets[name] = compile(pattern);
}
function transform (name, procedure) {
transforms[name] = procedure;
}
function pattern (nameOrPattern) {
return packets[nameOrPattern] || compile(nameOrPattern);
}
function createParser (context) {
return new Parser(new Definition(context, packets, transforms, options), options);
}
function createSerializer (context) {
return new Serializer(new Definition(context, packets, transforms, options), options);
}
function extend (object) {
return classify.call(object, packet, transform, createParser, createSerializer);
}
// Execute the pipeline of transforms for the `pattern` on the `value`.
function pipeline (outgoing, pattern, value, reverse) {
var i, I, j, J, by, pipeline, parameters, transform;
// Run the pipelines for parsing.
if (pipeline = pattern.pipeline) {
if (reverse) {
i = pipeline.length - 1, I = -1, by = -1;
} else {
i = 0, I = pipeline.length; by = 1;
}
while (i != I) {
transform = pipeline[i];
parameters = [];
for (j = 0, J = transform.parameters.length; j < J; j++) {
parameters.push(transform.parameters[j])
}
parameters.push(outgoing, pattern, value);
value = transforms[transform.name].apply(null, parameters);
i += by;
}
}
return value;
}
this.context = context;
return classify.call(this, packet, transform, pattern, pipeline, extend);
}
//#### Parser
// Construct a `Parser` around the given `definition`.
function Parser (definition, options) {
var increment, valueOffset, terminal, terminated, terminator, value,
skipping, repeat, step, index, arrayed,
pattern, patternIndex, context = definition.context || this, fields, _callback;
options = (options || { directory: './t/generated'});
// Sets the next packet to extract from the stream by providing a packet name
// defined by the `packet` function or else a packet pattern to parse. The
// `callback` will be invoked when the packet has been extracted from the
// buffer or buffers given to `parse`.
//
function extract (nameOrPattern, callback) {
var _pattern = definition.pattern(nameOrPattern), named;
patternIndex = 0;
_callback = callback;
pattern = _pattern.slice();
fields = {};
this.length = 0;
// At one point, you thought you could have a test for the arity of the
// function, and if it was greater than `1`, you'd call the callback
// positionally, regardless of named parameters. Then you realized that the
// `=>` operator in CoffeeScript would use a bind function with no
// arguments, and read the argument array. If you do decide to go back to
// arity override, then greater than one is the trigger. However, on
// reflection, I don't see that the flexibility is useful, and I do believe
// that it will generate at least one bug report that will take a lot of
// hashing out only to close with "oh, no, you hit upon a "hidden feature".
function isNamed (field) {
return field.named || (field.packing && field.packing.some(isNamed))
|| (field.alternation && field.alternation.some(isNamed));
}
named = pattern.some(isNamed);
function incremental (buffer, start, end, pattern, index, object, callback) {
this.parse = createGenericParser(definition, pattern, index, callback, object, true);
return this.parse(buffer, start, end);
}
if (named) {
var __callback = callback;
} else {
var __callback = function (object) {
var array = [];
flatten(pattern, object, array);
callback.apply(this, array);
}
}
if (_pattern.builder) {
this.parse = _pattern.builder(incremental, pattern, ieee754, __callback);
} else {
this.parse = createGenericParser(definition, pattern, 0, __callback, {}, true);
}
}
return classify.call(definition.extend(this), extract);
}
function createGenericParser (definition, pattern, patternIndex, _callback, fields) {
var increment, valueOffset, terminal, terminated, terminator, value,
skipping, repeat, step, index, arrayed,
pattern, patternIndex, context = definition.context || this, fields, _callback;
//#### parser.parse(buffer[, start][, length])
// The `parse` method reads from the buffer, returning when the current pattern
// is read, or the end of the buffer is reached.
// Prepare the parser for the next field in the pattern.
function nextField () {
var field = pattern[patternIndex];
repeat = field.repeat;
index = 0;
skipping = null;
terminated = ! field.terminator;
terminator = field.terminator && field.terminator[field.terminator.length - 1];
if (field.arrayed && field.endianness != "x") arrayed = [];
}
// Prepare the parser to parse the next value in the stream. It initializes
// the value to a zero integer value or an array. This method accounts for
// skipping, for skipped patterns.
function nextValue () {
// Get the next pattern.
var field = pattern[patternIndex];
// If skipping, skip over the count of bytes.
if (field.endianness == "x") {
skipping = field.bytes;
// Otherwise, create the empty value.
} else {
value = field.exploded ? [] : 0;
}
var little = field.endianness == 'l';
var bytes = field.bytes;
terminal = little ? bytes : -1;
valueOffset = little ? 0 : bytes - 1;
increment = little ? 1 : -1;
}
nextField();
nextValue();
// Read from the `buffer` for the given `start` offset and `length`.
return function (buffer, bufferOffset, bufferEnd) {
// Initialize the loop counters. Initialize unspecified parameters with their
// defaults.
var bufferOffset = bufferOffset, bufferEnd = bufferEnd, bytes, bits, field;
var start = bufferOffset;
// We set the pattern to null when all the fields have been read, so while
// there is a pattern to fill and bytes to read.
PATTERN: while (pattern != null && bufferOffset < bufferEnd) {
field = pattern[patternIndex];
// If we are skipping, we advance over all the skipped bytes or to the end
// of the current buffer.
if (skipping != null) {
var advance = Math.min(skipping, bufferEnd - bufferOffset);
var begin = bufferOffset;
bufferOffset += advance;
skipping -= advance;
this.length += advance;
// If we have more bytes to skip, then break because we've consumed the
// entire buffer.
if (skipping) break;
else skipping = null
} else {
// If the pattern is exploded, the value we're populating is an array.
if (field.exploded) {
for (;;) {
value[valueOffset] = buffer[bufferOffset];
bufferOffset++;
valueOffset += increment;
this.length++;
if (valueOffset == terminal) break;
if (bufferOffset == bufferEnd) break PATTERN;
}
// Otherwise we're packing bytes into an unsigned integer, the most
// common case.
} else {
for (;;) {
value += Math.pow(256, valueOffset) * buffer[bufferOffset];
bufferOffset++;
valueOffset += increment
this.length++;
if (valueOffset == terminal) break;
if (bufferOffset == bufferEnd) break PATTERN;
}
}
// Unpack the field value. Perform our basic transformations. That is,
// convert from a byte array to a JavaScript primitive.
//
// Resist the urge to implement these conversions with pipelines. It
// keeps occurring to you, but those transitions are at a higher level
// of abstraction, primarily for operations on gathered byte arrays.
// These transitions need to take place immediately to populate those
// arrays.
// By default, value is as it is.
bytes = value;
// Convert to float or double.
if (field.type == "f") {
if (field.bits == 32)
value = ieee754.fromIEEE754Single(bytes.reverse())
else
value = ieee754.fromIEEE754Double(bytes.reverse())
// Get the two's compliment signed value.
} else if (field.signed) {
value = 0;
if ((bytes[bytes.length - 1] & 0x80) == 0x80) {
var top = bytes.length - 1
for (i = 0; i < top; i++)
value += (~bytes[i] & 0xff) * Math.pow(256, i)
// To get the two's compliment as a positive value you use
// `~1 & 0xff == 254`. For example: `~1 == -2`.
value += (~(bytes[top] & 0x7f) & 0xff & 0x7f) * Math.pow(256, top);
value += 1;
value *= -1;
} else {
// Not really necessary, the bit about top.
top = bytes.length - 1
for (i = 0; i < top; i++)
value += (bytes[i] & 0xff) * Math.pow(256, i);
value += (bytes[top] & 0x7f) * Math.pow(256, top);
}
}
// If the current field is arrayed, we keep track of the array we're
// building after a pause through member variable.
if (field.arrayed) arrayed.push(value);
}
// If we've not yet hit our terminator, check for the terminator. If we've
// hit the terminator, and we do not have a maximum size to fill, then
// terminate by setting up the array to terminate.
//
// A length value of the maximum number value means to repeat until the
// terminator, but a specific length value means that the zero terminated
// string occupies a field that has a fixed length, so we need to skip the
// unused bytes.
if (! terminated) {
if (terminator == value) {
terminated = true;
var t = pattern[patternIndex].terminator;
for (i = 1, I = t.length; i <= I; i++) {
if (arrayed[arrayed.length - i] != t[t.length - i]) {
terminated = false
break
}
}
if (terminated) {
for (i = 0, I + t.length; i < I; i++) {
arrayed.pop();
}
terminated = true;
if (repeat == Number.MAX_VALUE) {
repeat = index + 1;
} else {
skipping = (repeat - (++index)) * field.bytes
if (skipping) {
repeat = index + 1;
continue
}
}
}
}
}
// If we are reading an arrayed pattern and we have not read all of the
// array elements, we repeat the current field type.
if (++index < repeat) {
nextValue();
// Otherwise, we've got a complete field value, either a JavaScript
// primitive or raw bytes as an array.
} else {
// If we're not skipping, push the field value after running it through
// the pipeline.
if (field.endianness != "x") {
var packing;
// If the field is a bit packed field, unpack the values and push them
// onto the field list.
if (packing = field.packing) {
var length = field.bits;
for (i = 0, I = packing.length; i < I; i++) {
var pack = packing[i];
length -= pack.bits;
if (pack.endianness == "b") {
var unpacked = Math.floor(value / Math.pow(2, length));
unpacked = unpacked % Math.pow(2, pack.bits);
// If signed, we convert from two's compliment.
if (pack.signed) {
var mask = Math.pow(2, pack.bits - 1)
if (unpacked & mask)
unpacked = -(~(unpacked - 1) & (mask * 2 - 1))
}
fields[pack.name] = unpacked;
}
}
// If the value is a length encoding, we set the repeat value for the
// subsequent array of values. If we have a zero length encoding, we
// push an empty array through the pipeline, and move on to the next
// field.
} else if (field.lengthEncoding) {
if ((pattern[patternIndex + 1].repeat = value) == 0) {
fields[pattern[patternIndex + 1].name] = definition.pipeline(true, field, [], false)
patternIndex++
}
// If the value is used as a switch for an alternation, we run through
// the different possible branches, updating the pattern with the
// pattern of the first branch that matches. We then re-read the bytes
// used to determine the conditional outcome.
} else if (field.alternation) {
// This makes no sense now.I wonder if it is called.
// unless field.signed
// value = (Math.pow(256, i) * b for b, i in arrayed)
var i, I, branch;
for (i = 0, I = field.alternation.length; i < I; i++) {
branch = field.alternation[i];
if (branch.read.minimum <= value &&
value <= branch.read.maximum &&
(value & branch.read.mask) == branch.read.mask)
break;
}
if (branch.failed)
throw new Error("Cannot match branch.");
bytes = arrayed.slice(0);
this.length -= bytes.length;
pattern.splice.apply(pattern, [ patternIndex, 1 ].concat(branch.pattern));
nextField();
nextValue();
this.parse(bytes, 0, bytes.length);
continue;
// Otherwise, the value is what it is, so run it through the user
// supplied transformation pipeline, and push it onto the list of
// fields.
} else {
if (field.arrayed) value = arrayed;
fields[field.name] = definition.pipeline(true, field, value, false);
}
}
// If we have read all of the pattern fields, call the associated
// callback. We add the parser and the user supplied additional
// arguments onto the callback arguments.
//
// The pattern is set to null, our terminal condition, because the
// callback may specify a subsequent packet to parse.
if (++patternIndex == pattern.length) {
pattern = null;
this.parse = null;
_callback.call(context, fields);
if (this.parse) {
bufferOffset += this.parse(buffer, bufferOffset, bufferEnd);
}
// Otherwise we proceed to the next field in the packet pattern.
} else {
nextField();
nextValue();
}
}
}
// Return the count of bytes read.
return bufferOffset - start;
}
}
function flatten (pattern, fields, array) {
pattern.forEach(function (field) {
if (field.packing) {
flatten(field.packing, fields, array);
} else if (!field.lengthEncoding && field.endianness != 'x') {
array.push(fields[field.name]);
}
});
}
module.exports.Parser = Parser;
// Construct a `Serializer` around the given `definition`.
function Serializer(definition) {
var serializer = this, terminal, valueOffset, increment, array, value, bytesWritten = 0,
skipping, repeat, outgoing, index, terminated, terminates, pattern,
incoming, named,
patternIndex, context = definition.context || this, padding, _callback;
function _length () { return bytesWritten }
// function reset () { bytesWritten = 0 }
// Prepare the parser for the next field in the pattern.
function nextField () {
var field = pattern[patternIndex]
repeat = field.repeat;
terminated = ! field.terminator;
terminates = ! terminated;
index = 0;
padding = null;
// Can't I keep separate indexes? Do I need that zero?
if (field.endianness == "x") {
if (field.padding != null)
padding = field.padding
}
}
// Prepare the parser to serialize the next value to the stream. It
// initializes Initialize the next field value to serialize. In the case of an
// arrayed value, we will use the next value in the array. This method will
// adjust the pattern for alteration. It will pack a bit packed integer. It
// will covert the field to a byte array for floats and signed negative
// numbers.
function nextValue () {
var i, I, packing, count, length, pack, unpacked, range, mask;
var field = pattern[patternIndex];
// If we are skipping without filling we note the count of bytes to skip,
// otherwise we prepare our value.
if (field.endianness == "x" && padding == null) {
skipping = field.bytes
} else {
// If we're filling, we write the fill value.
if (padding != null) {
value = padding;
// If the field is arrayed, we get the next value in the array.
} else if (field.arrayed) {
if (index == 0) {
array = incoming[field.name];
array = definition.pipeline(false, field, array, true)
}
value = array[index];
// If the field is bit packed, we update the `outgoing` array of values
// by packing zero, one or more values into a single value. We will also
// check for bits filled with a pattern specified filler value and pack
// that in there as well.
} else if (packing = field.packing) {
count = 0, value = 0, length = field.bits;
for (i = 0, I = packing.length; i < I; i++) {
pack = packing[i];
length -= pack.bits;
if (pack.endianness == "b" || pack.padding != null) {
unpacked = pack.padding != null ? pack.padding : incoming[pack.name];
if (pack.signed) {
range = Math.pow(2, pack.bits - 1)
if (!( (-range) <= unpacked && unpacked <= range - 1))
throw new Error("value " + unpacked + " will not fit in " + pack.bits + " bits");
if (unpacked < 0) {
mask = range * 2 - 1
unpacked = (~(- unpacked) + 1) & mask
}
}
value += unpacked * Math.pow(2, length)
}
}
// If the current field is a length encoded array, then the length of the
// current array value is the next value, otherwise, we have the simple
// case, the value is the current value.
} else {
if (field.lengthEncoding) {
value = incoming[pattern[patternIndex + 1].name].length;
pattern[patternIndex + 1].repeat = value;
} else {
value = incoming[field.name];
}
}
// If the array is not an unsigned integer, we might have to convert it.
if (field.exploded) {
// Convert a float into its IEEE 754 representation.
if (field.type == "f") {
if (field.bits == 32)
value = ieee754.toIEEE754Single(value).reverse();
else
value = ieee754.toIEEE754Double(value).reverse();
// Convert a signed integer into its two's compliment representation.
} else if (field.signed) {
var copy = Math.abs(value);
var bytes = [];
// FIXME If the value is greater than zero, we can just change the
// pattern to packed.
for (i = 0, I = field.bytes; i < I; i++) {
var pow = Math.pow(256, i)
bytes[i] = Math.floor(copy / pow % (pow * 256))
}
if (value < 0) {
var carry = 1;
for (i = 0, I = bytes.length; i < I; i++) {
bytes[i] = (~bytes[i] & 0xff) + carry;
if (bytes[i] == 256) bytes[i] = 0;
else carry = 0;
}
}
value = bytes;
}
}
var little = field.endianness == 'l';
var bytes = field.bytes;
terminal = little ? bytes : -1;
valueOffset = little ? 0 : bytes - 1;
increment = little ? 1 : -1;
if (field.pipeline && !field.arrayed) {
value = definition.pipeline(false, field, value, true)
}
}
}
function serialize () {
// TODO: We're copying because of positional parameters and alternation.
var shiftable = __slice.call(arguments),
// TODO: Rename `_pattern`.
prototype = definition.pattern(shiftable.shift()),
callback = typeof shiftable[shiftable.length - 1] == 'function' ? shiftable.pop() : void(0),
skip = 0,
field, value, alternate, i, I, j, J, k, K, alternates;
// TODO: Hate that all this has to pass for the common case.
named = (shiftable.length == 1
&& typeof shiftable[0] == "object"
&& ! (shiftable[0] instanceof Array));