forked from TritonDataCenter/node-ctype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctio.js
1409 lines (1207 loc) · 39.8 KB
/
ctio.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
/*
* rm - Feb 2011
* ctio.js:
*
* A simple way to read and write simple ctypes. Of course, as you'll find the
* code isn't as simple as it might appear. The following types are currently
* supported in big and little endian formats:
*
* uint8_t int8_t
* uint16_t int16_t
* uint32_t int32_t
* float (single precision IEEE 754)
* double (double precision IEEE 754)
*
* This is designed to work in Node and v8. It may in fact work in other
* Javascript interpreters (that'd be pretty neat), but it hasn't been tested.
* If you find that it does in fact work, that's pretty cool. Try and pass word
* back to the original author.
*
* Note to the reader: If you're tabstop isn't set to 8, parts of this may look
* weird.
*/
/*
* Numbers in Javascript have a secret: all numbers must be represented with an
* IEEE-754 double. The double has a mantissa with a length of 52 bits with an
* implicit one. Thus the range of integers that can be represented is limited
* to the size of the mantissa, this makes reading and writing 64-bit integers
* difficult, but far from impossible.
*
* Another side effect of this representation is what happens when you use the
* bitwise operators, i.e. shift left, shift right, and, or, etc. In Javascript,
* each operand and the result is cast to a signed 32-bit number. However, in
* the case of >>> the values are cast to an unsigned number.
*/
/*
* A reminder on endian related issues:
*
* Big Endian: MSB -> First byte
* Little Endian: MSB->Last byte
*/
/*
* An 8 bit unsigned integer involves doing no significant work.
*/
function ruint8(buffer, endian, offset)
{
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
return (buffer[offset]);
}
/*
* For 16 bit unsigned numbers we can do all the casting that we want to do.
*/
function ruint16(buffer, endian, offset)
{
var val = 0;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 1 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
if (endian == 'big') {
val = buffer[offset] << 8;
val |= buffer[offset+1];
} else {
val = buffer[offset];
val |= buffer[offset+1] << 8;
}
return (val);
}
/*
* Because most bitshifting is done using signed numbers, if we would go into
* the realm where we use that 32nd bit, we'll end up going into the negative
* range. i.e.:
* > 200 << 24
* -939524096
*
* Not the value you'd expect. To work around this, we instead do a
* multiplication by (1 << 24) which does the same thing, but in a way that
* ensures we don't lose that bit.
*/
/*
* Handle the case of losing our MSBit
*/
function fixu32(upper, lower)
{
return ((upper * (1 << 24)) + lower);
}
function ruint32(buffer, endian, offset)
{
var val = 0;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 3 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
if (endian == 'big') {
val = buffer[offset+1] << 16;
val |= buffer[offset+2] << 8;
val |= buffer[offset+3];
val = fixu32(buffer[offset], val);
} else {
val = buffer[offset+2] << 16;
val |= buffer[offset+1] << 8;
val |= buffer[offset];
val = fixu32(buffer[offset+3], val);
}
return (val);
}
/*
* Reads a 64-bit unsigned number. The astue observer will note that this
* doesn't quite work. Javascript has chosen to only have numbers that can be
* represented by a double. A double only has 52 bits of mantissa with an
* implicit 1, thus we have up to 53 bits to represent an integer. However, 2^53
* doesn't quite give us what we want. Isn't 53 bits enough for anyone? What
* could you have possibly wanted to represent that was larger than that? Oh,
* maybe a size? You mean we bypassed the 4 GB limit on file sizes, when did
* that happen?
*
* To get around this egregious language issue, we're going to instead construct
* an array of two 32 bit unsigned integers. Where arr[0] << 32 + arr[1] would
* give the actual number. However, note that the above code probably won't
* produce the desired results because of the way Javascript numbers are
* doubles.
*/
function ruint64(buffer, endian, offset)
{
var val = new Array(2);
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 7 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
if (endian == 'big') {
val[0] = ruint32(buffer, endian, offset);
val[1] = ruint32(buffer, endian, offset+4);
} else {
val[0] = ruint32(buffer, endian, offset+4);
val[1] = ruint32(buffer, endian, offset);
}
return (val);
}
/*
* Signed integer types, yay team! A reminder on how two's complement actually
* works. The first bit is the signed bit, i.e. tells us whether or not the
* number should be positive or negative. If the two's complement value is
* positive, then we're done, as it's equivalent to the unsigned representation.
*
* Now if the number is positive, you're pretty much done, you can just leverage
* the unsigned translations and return those. Unfortunately, negative numbers
* aren't quite that straightforward.
*
* At first glance, one might be inclined to use the traditional formula to
* translate binary numbers between the positive and negative values in two's
* complement. (Though it doesn't quite work for the most negative value)
* Mainly:
* - invert all the bits
* - add one to the result
*
* Of course, this doesn't quite work in Javascript. Take for example the value
* of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
* course, Javascript will do the following:
*
* > ~0xff80
* -65409
*
* Whoh there, Javascript, that's not quite right. But wait, according to
* Javascript that's perfectly correct. When Javascript ends up seeing the
* constant 0xff80, it has no notion that it is actually a signed number. It
* assumes that we've input the unsigned value 0xff80. Thus, when it does the
* binary negation, it casts it into a signed value, (positive 0xff80). Then
* when you perform binary negation on that, it turns it into a negative number.
*
* Instead, we're going to have to use the following general formula, that works
* in a rather Javascript friendly way. I'm glad we don't support this kind of
* weird numbering scheme in the kernel.
*
* (BIT-MAX - (unsigned)val + 1) * -1
*
* The astute observer, may think that this doesn't make sense for 8-bit numbers
* (really it isn't necessary for them). However, when you get 16-bit numbers,
* you do. Let's go back to our prior example and see how this will look:
*
* (0xffff - 0xff80 + 1) * -1
* (0x007f + 1) * -1
* (0x0080) * -1
*
* Doing it this way ends up allowing us to treat it appropriately in
* Javascript. Sigh, that's really quite ugly for what should just be a few bit
* shifts, ~ and &.
*/
/*
* Endianness doesn't matter for 8-bit signed values. We could in fact optimize
* this case because the more traditional methods work, but for consistency,
* we'll keep doing this the same way.
*/
function rsint8(buffer, endian, offset)
{
var neg;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
neg = buffer[offset] & 0x80;
if (!neg)
return (buffer[offset]);
return ((0xff - buffer[offset] + 1) * -1);
}
/*
* The 16-bit version requires a bit more effort. In this case, we can leverage
* our unsigned code to generate the value we want to return.
*/
function rsint16(buffer, endian, offset)
{
var neg, val;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 1 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = ruint16(buffer, endian, offset);
neg = val & 0x8000;
if (!neg)
return (val);
return ((0xffff - val + 1) * -1);
}
/*
* We really shouldn't leverage our 32-bit code here and instead utilize the
* fact that we know that since these are signed numbers, we can do all the
* shifting and binary anding to generate the 32-bit number. But, for
* consistency we'll do the same. If we want to do otherwise, we should instead
* make the 32 bit unsigned code do the optimization. But as long as there
* aren't floats secretly under the hood for that, we /should/ be okay.
*/
function rsint32(buffer, endian, offset)
{
var neg, val;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 3 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = ruint32(buffer, endian, offset);
neg = val & 0x80000000;
if (!neg)
return (val);
return ((0xffffffff - val + 1) * -1);
}
/*
* The signed version of this code suffers from all of the same problems of the
* other 64 bit version.
*/
function rsint64(buffer, endian, offset)
{
var neg, val;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 3 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = ruint64(buffer, endian, offset);
neg = val[0] & 0x80000000;
if (!neg)
return (val);
val[0] = (0xffffffff - val[0]) * -1;
val[1] = (0xffffffff - val[1] + 1) * -1;
return (val);
}
/*
* We now move onto IEEE 754: The traditional form for floating point numbers
* and what is secretly hiding at the heart of everything in this. I really hope
* that someone is actually using this, as otherwise, this effort is probably
* going to be more wasted.
*
* One might be tempted to use parseFloat here, but that wouldn't work at all
* for several reasons. Mostly due to the way floats actually work, and
* parseFloat only actually works in base 10. I don't see base 10 anywhere near
* this file.
*
* In this case we'll implement the single and double precision versions. The
* quadruple precision, while probably useful, wouldn't really be accepted by
* Javascript, so let's not even waste our time.
*
* So let's review how this format looks like. A single precision value is 32
* bits and has three parts:
* - Sign bit
* - Exponent (Using bias notation)
* - Mantissa
*
* |s|eeeeeeee|mmmmmmmmmmmmmmmmmmmmmmmmm|
* 31| 30-23 | 22 - 0 |
*
* The exponent is stored in a biased input. The bias in this case 127.
* Therefore, our exponent is equal to the 8-bit value - 127.
*
* By default, a number is normalized in IEEE, that means that the mantissa has
* an implicit one that we don't see. So really the value stored is 1.m.
* However, if the exponent is all zeros, then instead we have to shift
* everything to the right one and there is no more implicit one.
*
* Special values:
* - Positive Infinity:
* Sign: 0
* Exponent: All 1s
* Mantissa: 0
* - Negative Infinity:
* Sign: 1
* Exponent: All 1s
* Mantissa: 0
* - NaN:
* Sign: *
* Exponent: All 1s
* Mantissa: non-zero
* - Zero:
* Sign: *
* Exponent: All 0s
* Mantissa: 0
*
* In the case of zero, the sign bit determines whether we get a positive or
* negative zero. However, since Javascript cannot determine the difference
* between the two: i.e. -0 == 0, we just always return 0.
*
*/
function rfloat(buffer, endian, offset)
{
var bytes = [];
var sign, exponent, mantissa, val;
var bias = 127;
var maxexp = 0xff;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 3 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
/* Normalize the bytes to be in endian order */
if (endian == 'big') {
bytes[0] = buffer[offset];
bytes[1] = buffer[offset+1];
bytes[2] = buffer[offset+2];
bytes[3] = buffer[offset+3];
} else {
bytes[3] = buffer[offset];
bytes[2] = buffer[offset+1];
bytes[1] = buffer[offset+2];
bytes[0] = buffer[offset+3];
}
sign = bytes[0] & 0x80;
exponent = (bytes[0] & 0x7f) << 1;
exponent |= (bytes[1] & 0x80) >>> 7;
mantissa = (bytes[1] & 0x7f) << 16;
mantissa |= bytes[2] << 8;
mantissa |= bytes[3];
/* Check for special cases before we do general parsing */
if (!sign && exponent == maxexp && mantissa === 0)
return (Number.POSITIVE_INFINITY);
if (sign && exponent == maxexp && mantissa === 0)
return (Number.NEGATIVE_INFINITY);
if (exponent == maxexp && mantissa !== 0)
return (Number.NaN);
/*
* Javascript really doesn't have support for positive or negative zero.
* So we're not going to try and give it to you. That would be just
* plain weird. Besides -0 == 0.
*/
if (exponent === 0 && mantissa === 0)
return (0);
/*
* Now we can deal with the bias and the determine whether the mantissa
* has the implicit one or not.
*/
exponent -= bias;
if (exponent == -bias) {
exponent++;
val = 0;
} else {
val = 1;
}
val = (val + mantissa * Math.pow(2, -23)) * Math.pow(2, exponent);
if (sign)
val *= -1;
return (val);
}
/*
* Doubles in IEEE 754 are like their brothers except for a few changes and
* increases in size:
* - The exponent is now 11 bits
* - The mantissa is now 52 bits
* - The bias is now 1023
*
* |s|eeeeeeeeeee|mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm|
* 63| 62 - 52 | 51 - 0 |
* 63| 62 - 52 | 51 - 0 |
*
* While the size has increased a fair amount, we're going to end up keeping the
* same general formula for calculating the final value. As a reminder, this
* formula is:
*
* (-1)^s * (n + m) * 2^(e-b)
*
* Where:
* s is the sign bit
* n is (exponent > 0) ? 1 : 0 -- Determines whether we're normalized
* or not
* m is the mantissa
* e is the exponent specified
* b is the bias for the exponent
*
*/
function rdouble(buffer, endian, offset)
{
var bytes = [];
var sign, exponent, mantissa, val, lowmant;
var bias = 1023;
var maxexp = 0x7ff;
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 7 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
/* Normalize the bytes to be in endian order */
if (endian == 'big') {
bytes[0] = buffer[offset];
bytes[1] = buffer[offset+1];
bytes[2] = buffer[offset+2];
bytes[3] = buffer[offset+3];
bytes[4] = buffer[offset+4];
bytes[5] = buffer[offset+5];
bytes[6] = buffer[offset+6];
bytes[7] = buffer[offset+7];
} else {
bytes[7] = buffer[offset];
bytes[6] = buffer[offset+1];
bytes[5] = buffer[offset+2];
bytes[4] = buffer[offset+3];
bytes[3] = buffer[offset+4];
bytes[2] = buffer[offset+5];
bytes[1] = buffer[offset+6];
bytes[0] = buffer[offset+7];
}
/*
* We can construct the exponent and mantissa the same way as we did in
* the case of a float, just increase the range of the exponent.
*/
sign = bytes[0] & 0x80;
exponent = (bytes[0] & 0x7f) << 4;
exponent |= (bytes[1] & 0xf0) >>> 4;
/*
* This is going to be ugly but then again, we're dealing with IEEE 754.
* This could probably be done as a node add on in a few lines of C++,
* but oh we'll, we've made it this far so let's be native the rest of
* the way...
*
* What we're going to do is break the mantissa into two parts, the
* lower 24 bits and the upper 28 bits. We'll multiply the upper 28 bits
* by the appropriate power and then add in the lower 24-bits. Not
* really that great. It's pretty much a giant kludge to deal with
* Javascript eccentricities around numbers.
*/
lowmant = bytes[7];
lowmant |= bytes[6] << 8;
lowmant |= bytes[5] << 16;
mantissa = bytes[4];
mantissa |= bytes[3] << 8;
mantissa |= bytes[2] << 16;
mantissa |= (bytes[1] & 0x0f) << 24;
mantissa *= Math.pow(2, 24); /* Equivalent to << 24, but JS compat */
mantissa += lowmant;
/* Check for special cases before we do general parsing */
if (!sign && exponent == maxexp && mantissa === 0)
return (Number.POSITIVE_INFINITY);
if (sign && exponent == maxexp && mantissa === 0)
return (Number.NEGATIVE_INFINITY);
if (exponent == maxexp && mantissa !== 0)
return (Number.NaN);
/*
* Javascript really doesn't have support for positive or negative zero.
* So we're not going to try and give it to you. That would be just
* plain weird. Besides -0 == 0.
*/
if (exponent === 0 && mantissa === 0)
return (0);
/*
* Now we can deal with the bias and the determine whether the mantissa
* has the implicit one or not.
*/
exponent -= bias;
if (exponent == -bias) {
exponent++;
val = 0;
} else {
val = 1;
}
val = (val + mantissa * Math.pow(2, -52)) * Math.pow(2, exponent);
if (sign)
val *= -1;
return (val);
}
/*
* Now that we have gone through the pain of reading the individual types, we're
* probably going to want some way to write these back. None of this is going to
* be good. But since we have Javascript numbers this should certainly be more
* interesting. Though we can constrain this end a little bit more in what is
* valid. For now, let's go back to our friends the unsigned value.
*/
/*
* Unsigned numbers seem deceptively easy. Here are the general steps and rules
* that we are going to take:
* - If the number is negative, throw an Error
* - Truncate any floating point portion
* - Take the modulus of the number in our base
* - Write it out to the buffer in the endian format requested at the offset
*/
/*
* We have to make sure that the value is a valid integer. This means that it is
* non-negative. It has no fractional component and that it does not exceed the
* maximum allowed value.
*
* value The number to check for validity
*
* max The maximum value
*/
function prepuint(value, max)
{
if (typeof (value) != 'number')
throw (new (Error('cannot write a non-number as a number')));
if (value < 0)
throw (new Error('specified a negative value for writing an ' +
'unsigned value'));
if (value > max)
throw (new Error('value is larger than maximum value for ' +
'type'));
if (Math.floor(value) !== value)
throw (new Error('value has a fractional component'));
return (value);
}
/*
* 8-bit version, classy. We can ignore endianness which is good.
*/
function wuint8(value, endian, buffer, offset)
{
var val;
if (value === undefined)
throw (new Error('missing value'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = prepuint(value, 0xff);
buffer[offset] = val;
}
/*
* Pretty much the same as the 8-bit version, just this time we need to worry
* about endian related issues.
*/
function wuint16(value, endian, buffer, offset)
{
var val;
if (value === undefined)
throw (new Error('missing value'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 1 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = prepuint(value, 0xffff);
if (endian == 'big') {
buffer[offset] = (val & 0xff00) >>> 8;
buffer[offset+1] = val & 0x00ff;
} else {
buffer[offset+1] = (val & 0xff00) >>> 8;
buffer[offset] = val & 0x00ff;
}
}
/*
* The 32-bit version is going to have to be a little different unfortunately.
* We can't quite bitshift to get the largest byte, because that would end up
* getting us caught by the signed values.
*
* And yes, we do want to subtract out the lower part by default. This means
* that when we do the division, it will be treated as a bit shift and we won't
* end up generating a floating point value. If we did generate a floating point
* value we'd have to truncate it intelligently, this saves us that problem and
* may even be somewhat faster under the hood.
*/
function wuint32(value, endian, buffer, offset)
{
var val;
if (value === undefined)
throw (new Error('missing value'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 3 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = prepuint(value, 0xffffffff);
if (endian == 'big') {
buffer[offset] = (val - (val & 0x00ffffff)) / Math.pow(2, 24);
buffer[offset+1] = (val >>> 16) & 0xff;
buffer[offset+2] = (val >>> 8) & 0xff;
buffer[offset+3] = val & 0xff;
} else {
buffer[offset+3] = (val - (val & 0x00ffffff)) /
Math.pow(2, 24);
buffer[offset+2] = (val >>> 16) & 0xff;
buffer[offset+1] = (val >>> 8) & 0xff;
buffer[offset] = val & 0xff;
}
}
/*
* Unlike the other versions, we expect the value to be in the form of two
* arrays where value[0] << 32 + value[1] would result in the value that we
* want.
*/
function wuint64(value, endian, buffer, offset)
{
if (value === undefined)
throw (new Error('missing value'));
if (!(value instanceof Array))
throw (new Error('value must be an array'));
if (value.length != 2)
throw (new Error('value must be an array of length 2'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 7 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
prepuint(value[0], 0xffffffff);
prepuint(value[1], 0xffffffff);
if (endian == 'big') {
wuint32(value[0], endian, buffer, offset);
wuint32(value[1], endian, buffer, offset+4);
} else {
wuint32(value[0], endian, buffer, offset+4);
wuint32(value[1], endian, buffer, offset);
}
}
/*
* We now move onto our friends in the signed number category. Unlike unsigned
* numbers, we're going to have to worry a bit more about how we put values into
* arrays. Since we are only worrying about signed 32-bit values, we're in
* slightly better shape. Unfortunately, we really can't do our favorite binary
* & in this system. It really seems to do the wrong thing. For example:
*
* > -32 & 0xff
* 224
*
* What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of
* this aren't treated as a signed number. Ultimately a bad thing.
*
* What we're going to want to do is basically create the unsigned equivalent of
* our representation and pass that off to the wuint* functions. To do that
* we're going to do the following:
*
* - if the value is positive
* we can pass it directly off to the equivalent wuint
* - if the value is negative
* we do the following computation:
* mb + val + 1, where
* mb is the maximum unsigned value in that byte size
* val is the Javascript negative integer
*
*
* As a concrete value, take -128. In signed 16 bits this would be 0xff80. If
* you do out the computations:
*
* 0xffff - 128 + 1
* 0xffff - 127
* 0xff80
*
* You can then encode this value as the signed version. This is really rather
* hacky, but it should work and get the job done which is our goal here.
*
* Thus the overall flow is:
* - Truncate the floating point part of the number
* - We don't have to take the modulus, because the unsigned versions will
* take care of that for us. And we don't have to worry about that
* potentially causing bad things to happen because of sign extension
* - Pass it off to the appropriate unsigned version, potentially modifying
* the negative portions as necessary.
*/
/*
* A series of checks to make sure we actually have a signed 32-bit number
*/
function prepsint(value, max, min)
{
if (typeof (value) != 'number')
throw (new (Error('cannot write a non-number as a number')));
if (value > max)
throw (new Error('value larger than maximum allowed value'));
if (value < min)
throw (new Error('value smaller than minimum allowed value'));
if (Math.floor(value) !== value)
throw (new Error('value has a fractional component'));
return (value);
}
/*
* The 8-bit version of the signed value. Overall, fairly straightforward.
*/
function wsint8(value, endian, buffer, offset)
{
var val;
if (value === undefined)
throw (new Error('missing value'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = prepsint(value, 0x7f, -0xf0);
if (val >= 0)
wuint8(val, endian, buffer, offset);
else
wuint8(0xff + val + 1, endian, buffer, offset);
}
/*
* The 16-bit version of the signed value. Also, fairly straightforward.
*/
function wsint16(value, endian, buffer, offset)
{
var val;
if (value === undefined)
throw (new Error('missing value'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 1 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = prepsint(value, 0x7fff, -0xf000);
if (val >= 0)
wuint16(val, endian, buffer, offset);
else
wuint16(0xffff + val + 1, endian, buffer, offset);
}
/*
* We can do this relatively easily by leveraging the code used for 32-bit
* unsigned code.
*/
function wsint32(value, endian, buffer, offset)
{
var val;
if (value === undefined)
throw (new Error('missing value'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 3 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
val = prepsint(value, 0x7fffffff, -0xf0000000);
if (val >= 0)
wuint32(val, endian, buffer, offset);
else
wuint32(0xffffffff + val + 1, endian, buffer, offset);
}
/*
* The signed 64 bit integer should by in the same format as when received.
* Mainly it should ensure that the value is an array of two integers where
* value[0] << 32 + value[1] is the desired number.
*/
function wsint64(value, endian, buffer, offset)
{
var vals = new Array(2);
if (value === undefined)
throw (new Error('missing value'));
if (!(value instanceof Array))
throw (new Error('value must be an array'));
if (value.length != 2)
throw (new Error('value must be an array of length 2'));
if (endian === undefined)
throw (new Error('missing endian'));
if (buffer === undefined)
throw (new Error('missing buffer'));
if (offset === undefined)
throw (new Error('missing offset'));
if (offset + 7 >= buffer.length)
throw (new Error('Trying to read beyond buffer length'));
prepsint(value[0], 0x7fffffff, -0xf0000000);
prepsint(value[1], 0xffffffff, -0xffffffff);