forked from danielrh/protojs
-
Notifications
You must be signed in to change notification settings - Fork 24
/
protobuf.js
1900 lines (1851 loc) · 67.4 KB
/
protobuf.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
/* ProtoJS - Protocol buffers for Javascript.
* protobuf.js
*
* Copyright (c) 2009-2010, Patrick Reiter Horn
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of ProtoJS nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
var PROTO = {};
PROTO.IsArray = (function() {
if (typeof(Uint8Array) != "undefined") {
return function(arr) {
return arr instanceof Array || arr instanceof Uint8Array;
};
} else {
return function(arr) {
return arr instanceof Array;
};
}
})();
PROTO.DefineProperty = (function () {
var DefineProperty;
if (typeof(Object.defineProperty) != "undefined") {
/**
* @suppress {missingProperties}
*/
DefineProperty = function(prototype, property, getter, setter) {
Object.defineProperty(prototype, property, {
'get': getter, 'set': setter,
'enumerable': true, 'configurable': false});
};
} else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) {
DefineProperty = function(prototype, property, getter, setter) {
if (typeof getter !== 'undefined') {
prototype.__defineGetter__(property, getter);
}
if (typeof setter !== 'undefined') {
prototype.__defineSetter__(property, setter);
}
};
}
// IE8's Object.defineProperty method might be broken.
// Make sure DefineProperty works before returning it.
if (DefineProperty) {
try {
/**
* @constructor
*/
var TestClass = function(){};
DefineProperty(TestClass.prototype, "x",
function(){return this.xval*2;},
function(newx){this.xval=newx;});
var testinst = new TestClass;
testinst.x = 5;
if (testinst.x != 10) {
PROTO.warn("DefineProperty test gave the wrong result "+testinst.x);
DefineProperty = undefined;
}
} catch (e) {
PROTO.warn("DefineProperty should be supported, but threw "+e);
DefineProperty = undefined;
}
}
return DefineProperty;
})();
/** Clones a PROTO type object. Does not work on arbitrary javascript objects.
For example, can be used to copy the "bytes" class and make a custom toString method.
*/
PROTO.cloneType = function(f) {
var ret = {};
for (var x in f) {
ret[x] = f[x];
}
return ret;
}
PROTO.wiretypes = {
varint: 0,
fixed64: 1,
lengthdelim: 2,
fixed32: 5
};
PROTO.optional = 'optional';
PROTO.repeated = 'repeated';
PROTO.required = 'required';
/**
* @param {string} s
*/
PROTO.warn = function (s) {
if (typeof(self.console)!="undefined" && self.console.log) {
self.console.log(s);
}
};
/**
* @constructor
*/
PROTO.I64 = function (msw, lsw, sign) {
/**
* @type {number}
*/
this.msw = msw;
/**
* @type {number}
*/
this.lsw = lsw;
if (typeof lsw === undefined) {
PROTO.warn("Too few arguments passed to I64 constructor: perhaps you meant PROTO.I64.fromNumber()");
throw ("Too few arguments passed to I64 constructor: perhaps you meant PROTO.I64.fromNumber()");
}
if (sign === true) sign = -1;
if (!sign) sign = 1;
this.sign = sign;
};
PROTO.I64.prototype = {
toNumber: function() {
return (this.msw*4294967296 + this.lsw)*this.sign;
},
toString: function() {
//return this.toNumber();
function zeros(len){
var retval="";
for (var i=0;i<len;++i) {
retval+="0";
}
return retval;
}
var firstHalf=this.msw.toString(16);
var secondHalf=this.lsw.toString(16);
var sign = (this.sign==-1 ? "-" : "");
return sign+"0x"+zeros(8-firstHalf.length)+firstHalf+zeros(8-secondHalf.length)+secondHalf;
},
equals: function(other) {
return this.sign==other.sign&&this.msw==other.msw&&this.lsw==other.lsw;
},
hash: function() {
return (this.sign*this.msw)+"_"+this.lsw;
},
convertToUnsigned: function() {
var local_lsw;
local_lsw=this.lsw;
var local_msw;
if (this.sign<0) {
local_msw=2147483647-this.msw;
local_msw+=2147483647;
local_msw+=1;
local_lsw=2147483647-this.lsw;
local_lsw+=2147483647;
local_lsw+=2;
if (local_lsw==4294967296) {
local_lsw=0;
local_msw+=1;
}
}else {
local_msw=this.msw;
}
return new PROTO.I64(local_msw,local_lsw,1);
},
convertFromUnsigned:function() {
if(this.msw>=2147483648) {
var local_msw = 4294967295-this.msw;
var local_lsw = 4294967295-this.lsw+1;
if (local_lsw>4294967295) {
local_lsw-=4294967296;
local_msw+=1;
}
return new PROTO.I64(local_msw,local_lsw,-1);
}
return new PROTO.I64(this.msw,this.lsw,this.sign);
},
convertToZigzag: function() {
var local_lsw;
if (this.sign<0) {
local_lsw=this.lsw*2-1;
}else {
local_lsw=this.lsw*2;
}
var local_msw=this.msw*2;
if (local_lsw>4294967295){
local_msw+=1;
local_lsw-=4294967296;
}
if (local_lsw<0){
local_msw-=1;
local_lsw+=4294967296;
}
return new PROTO.I64(local_msw,local_lsw,1);
},
convertFromZigzag:function() {
var retval;
if(this.msw&1) {//carry the bit from the most significant to the least by adding 2^31 to lsw
retval = new PROTO.I64((this.msw>>>1),
2147483648+(this.lsw>>>1),
(this.lsw&1)?-1:1);
} else {
retval = new PROTO.I64((this.msw>>>1),
(this.lsw>>>1),
(this.lsw&1)?-1:1);
}
if (retval.sign==-1) {
retval.lsw+=1;
if (retval.lsw>4294967295) {
retval.msw+=1;
retval.lsw-=4294967296;
}
}
return retval;
},
serializeToLEBase256: function() {
var arr = new Array(8);
var temp=this.lsw;
for (var i = 0; i < 4; i++) {
arr[i] = (temp&255);
temp=(temp>>8);
}
temp = this.msw;
for (var i = 4; i < 8; i++) {
arr[i] = (temp&255);
temp=(temp>>8);
}
return arr;
},
serializeToLEVar128: function() {
var arr = new Array(1);
var temp=this.lsw;
for (var i = 0; i < 4; i++) {
arr[i] = (temp&127);
temp=(temp>>>7);
if(temp==0&&this.msw==0) return arr;
arr[i]+=128;
}
arr[4] = (temp&15) | ((this.msw&7)<<4);
temp=(this.msw>>>3);
if (temp==0) return arr;
arr[4]+=128;
for (var i = 5; i<10; i++) {
arr[i] = (temp&127);
temp=(temp>>>7);
if(temp==0) return arr;
arr[i]+=128;
}
return arr;
},
unsigned_add:function(other) {
var temp=this.lsw+other.lsw;
var local_msw=this.msw+other.msw;
var local_lsw=temp%4294967296;
temp-=local_lsw;
local_msw+=Math.floor(temp/4294967296);
return new PROTO.I64(local_msw,local_lsw,this.sign);
},
sub : function(other) {
if (other.sign!=this.sign) {
return this.unsigned_add(other);
}
if (other.msw>this.msw || (other.msw==this.msw&&other.lsw>this.lsw)) {
var retval=other.sub(this);
retval.sign=-this.sign;
return retval;
}
var local_lsw=this.lsw-other.lsw;
var local_msw=this.msw-other.msw;
if (local_lsw<0) {
local_lsw+=4294967296;
local_msw-=1;
}
return new PROTO.I64(local_msw,local_lsw,this.sign);
},
/**
* @param {PROTO.I64} other
*/
less:function(other){
if (other.sign!=this.sign) {
return this.sign<0;
}
/**
* @type {PROTO.I64}
*/
var a=this;
/**
* @type {PROTO.I64}
*/
var b=other;
if (this.sign<0) {
b=this;a=other;
}
if (a.msw==b.msw)
return a.lsw<b.lsw;
if (a.msw<b.msw)
return true;
return false;
},
unsigned_less:function(other){
var a=this,b=other;
if (a.msw==b.msw)
return a.lsw<b.lsw;
if (a.msw<b.msw)
return true;
return false;
},
add : function(other) {
if (other.sign<0 && this.sign>=0)
return this.sub(new PROTO.I64(other.msw,other.lsw,-other.sign));
if (other.sign>=0 && this.sign<0)
return other.sub(new PROTO.I64(this.msw,this.lsw,-this.sign));
return this.unsigned_add(other);
}
};
PROTO.I64.fromNumber = function(mynum) {
var sign = (mynum < 0) ? -1 : 1;
mynum *= sign;
var lsw = (mynum%4294967296);
var msw = Math.floor((mynum-lsw)/4294967296);
return new PROTO.I64(msw, lsw, sign);
};
PROTO.I64.from32pair = function(msw, lsw, sign) {
return new PROTO.I64(msw, lsw, sign);
};
/**
* @param {PROTO.Stream} stream
* @param {PROTO.I64=} float64toassignto
*/
PROTO.I64.parseLEVar128 = function (stream, float64toassignto) {
var retval = float64toassignto||new PROTO.I64(0,0,1);
var n = 0;
var endloop = false;
var offset=1;
for (var i = 0; !endloop && i < 5; i++) {
var byt = stream.readByte();
if (byt >= 128) {
byt -= 128;
} else {
endloop = true;
}
n += offset*byt;
offset *= 128;
}
var lsw=n%4294967296;
var msw = Math.floor((n - lsw) / 4294967296);
offset=8;
for (var i = 0; !endloop && i < 5; i++) {
var byt = stream.readByte();
if (byt >= 128) {
byt -= 128;
} else {
endloop = true;
}
msw += offset*byt;
offset *= 128;
}
retval.msw=msw%4294967296;
retval.lsw=lsw;
retval.sign=1;
return retval;
};
/**
* @param {PROTO.Stream} stream
* @param {PROTO.I64=} float64toassignto
*/
PROTO.I64.parseLEBase256 = function (stream, float64toassignto) {
var retval = float64toassignto||new PROTO.I64(0,0,1);
var n = 0;
var endloop = false;
var offset=1;
for (var i = 0; i < 4; i++) {
var byt = stream.readByte();
n += offset*byt;
offset *= 256;
}
var lsw=n;
var msw=0;
offset=1;
for (var i = 0; i < 4; i++) {
var byt = stream.readByte();
msw += offset*byt;
offset *= 256;
}
retval.msw=msw;
retval.lsw=lsw;
retval.sign=1;
return retval;
};
PROTO.I64.ONE = new PROTO.I64.fromNumber(1);
PROTO.I64.ZERO = new PROTO.I64.fromNumber(0);
/**
* + Jonas Raoni Soares Silva
* http://jsfromhell.com/classes/binary-parser [rev. #1]
* @constructor
*/
PROTO.BinaryParser = function(bigEndian, allowExceptions){
this.bigEndian = bigEndian, this.allowExceptions = allowExceptions;
};
PROTO.BinaryParser.prototype.encodeFloat = function(number, precisionBits, exponentBits){
var n;
var bias = Math.pow(2, exponentBits - 1) - 1, minExp = -bias + 1, maxExp = bias, minUnnormExp = minExp - precisionBits,
status = isNaN(n = parseFloat(number)) || n == -Infinity || n == +Infinity ? n : 0,
exp = 0, len = 2 * bias + 1 + precisionBits + 3, bin = new Array(len),
signal = (n = status !== 0 ? 0 : n) < 0;
n = Math.abs(n);
var intPart = Math.floor(n), floatPart = n - intPart, i, lastBit, rounded, j, result, r;
for(i = len; i; bin[--i] = 0){}
for(i = bias + 2; intPart && i; bin[--i] = intPart % 2, intPart = Math.floor(intPart / 2)){}
for(i = bias + 1; floatPart > 0 && i; (bin[++i] = ((floatPart *= 2) >= 1) - 0) && --floatPart){}
for(i = -1; ++i < len && !bin[i];){}
if(bin[(lastBit = precisionBits - 1 + (i = (exp = bias + 1 - i) >= minExp && exp <= maxExp ? i + 1 : bias + 1 - (exp = minExp - 1))) + 1]){
if(!(rounded = bin[lastBit]))
for(j = lastBit + 2; !rounded && j < len; rounded = bin[j++]){}
for(j = lastBit + 1; rounded && --j >= 0; (bin[j] = !bin[j] - 0) && (rounded = 0)){}
}
for(i = i - 2 < 0 ? -1 : i - 3; ++i < len && !bin[i];){}
(exp = bias + 1 - i) >= minExp && exp <= maxExp ? ++i : exp < minExp &&
(exp != bias + 1 - len && exp < minUnnormExp && this.warn("encodeFloat::float underflow"), i = bias + 1 - (exp = minExp - 1));
(intPart || status !== 0) && (this.warn(intPart ? "encodeFloat::float overflow" : "encodeFloat::" + status),
exp = maxExp + 1, i = bias + 2, status == -Infinity ? signal = 1 : isNaN(status) && (bin[i] = 1));
for(n = Math.abs(exp + bias), j = exponentBits + 1, result = ""; --j; result = (n % 2) + result, n = n >>= 1){}
for(n = 0, j = 0, i = (result = (signal ? "1" : "0") + result + bin.slice(i, i + precisionBits).join("")).length, r = [];
i; n += (1 << j) * result.charAt(--i), j == 7 && (r[r.length] = n, n = 0), j = (j + 1) % 8){}
return (this.bigEndian ? r.reverse() : r);
};
PROTO.BinaryParser.prototype.encodeInt = function(number, bits, signed){
var max = Math.pow(2, bits), r = [];
(number >= max || number < -(max >> 1)) && this.warn("encodeInt::overflow") && (number = 0);
number < 0 && (number += max);
for(; number; r[r.length] = number % 256, number = Math.floor(number / 256)){}
for(bits = -(-bits >> 3) - r.length; bits--;){}
return (this.bigEndian ? r.reverse() : r);
};
(function () {
var buffer8byte = new ArrayBuffer(8);
var buffer4byte = new ArrayBuffer(4);
var f64buffer = new DataView(buffer8byte,0,8);
var f32buffer = new DataView(buffer4byte,0,4);
var u8buffer64 = new Uint8Array(buffer8byte);
var u8buffer32 = new Uint8Array(buffer4byte);
PROTO.BinaryParser.prototype.encodeFloat32 = function(data) {
f32buffer.setFloat32(0,data,true);
return u8buffer32;
}
PROTO.BinaryParser.prototype.encodeFloat64 = function(data) {
f64buffer.setFloat64(0,data,true);
return u8buffer64;
}
PROTO.BinaryParser.prototype.decodeFloat32 = function(data) {
var len=data.length;
if (len>4) len=4;
for (var i=0;i<len;++i) {
u8buffer32[i]=data[i];
}
return f32buffer.getFloat32(0,true);
}
PROTO.BinaryParser.prototype.decodeFloat64 = function(data) {
var len=data.length;
if (len>8) len=8;
for (var i=0;i<len;++i) {
u8buffer64[i]=data[i];
}
return f64buffer.getFloat64(0,true);
}
})();
PROTO.BinaryParser.prototype.decodeFloat = function(data, precisionBits, exponentBits){
var b = new this.Buffer(this.bigEndian, data);
PROTO.BinaryParser.prototype.checkBuffer.call(b, precisionBits + exponentBits + 1);
var bias = Math.pow(2, exponentBits - 1) - 1, signal = PROTO.BinaryParser.prototype.readBits.call(b,precisionBits + exponentBits, 1);
var exponent = PROTO.BinaryParser.prototype.readBits.call(b,precisionBits, exponentBits), significand = 0;
var divisor = 2;
var curByte = b.buffer.length + (-precisionBits >> 3) - 1;
var byteValue, startBit, mask;
do
for(byteValue = b.buffer[ ++curByte ], startBit = precisionBits % 8 || 8, mask = 1 << startBit;
mask >>= 1; (byteValue & mask) && (significand += 1 / divisor), divisor *= 2){}
while((precisionBits -= startBit));
return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity
: (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand
: Math.pow(2, exponent - bias) * (1 + significand) : 0);
};
PROTO.BinaryParser.prototype.decodeInt = function(data, bits, signed){
var b = new this.Buffer(this.bigEndian, data), x = b.readBits(0, bits), max = Math.pow(2, bits);
return signed && x >= max / 2 ? x - max : x;
};
PROTO.BinaryParser.prototype.Buffer = function(bigEndian, buffer){
this.bigEndian = bigEndian || 0;
this.buffer = [];
PROTO.BinaryParser.prototype.setBuffer.call(this,buffer);
};
PROTO.BinaryParser.prototype.readBits = function(start, length){
//shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
function shl(a, b){
for(++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1){}
return a;
}
if(start < 0 || length <= 0)
return 0;
PROTO.BinaryParser.prototype.checkBuffer.call(this, start + length);
for(var offsetLeft, offsetRight = start % 8, curByte = this.buffer.length - (start >> 3) - 1,
lastByte = this.buffer.length + (-(start + length) >> 3), diff = curByte - lastByte,
sum = ((this.buffer[ curByte ] >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1))
+ (diff && (offsetLeft = (start + length) % 8) ? (this.buffer[ lastByte++ ] & ((1 << offsetLeft) - 1))
<< (diff-- << 3) - offsetRight : 0); diff; sum += shl(this.buffer[ lastByte++ ], (diff-- << 3) - offsetRight)
){}
return sum;
};
PROTO.BinaryParser.prototype.setBuffer = function(data){
if(data){
for(var l, i = l = data.length, b = this.buffer = new Array(l); i; b[l - i] = data[--i]){}
this.bigEndian && b.reverse();
}
};
PROTO.BinaryParser.prototype.hasNeededBits = function(neededBits){
return this.buffer.length >= -(-neededBits >> 3);
};
PROTO.BinaryParser.prototype.checkBuffer = function(neededBits){
if(!PROTO.BinaryParser.prototype.hasNeededBits.call(this,neededBits))
throw new Error("checkBuffer::missing bytes");
};
PROTO.BinaryParser.prototype.warn = function(msg){
if(this.allowExceptions)
throw new Error(msg);
return 1;
};
PROTO.BinaryParser.prototype.toSmall = function(data){return this.decodeInt(data, 8, true);};
PROTO.BinaryParser.prototype.fromSmall = function(number){return this.encodeInt(number, 8, true);};
PROTO.BinaryParser.prototype.toByte = function(data){return this.decodeInt(data, 8, false);};
PROTO.BinaryParser.prototype.fromByte = function(number){return this.encodeInt(number, 8, false);};
PROTO.BinaryParser.prototype.toShort = function(data){return this.decodeInt(data, 16, true);};
PROTO.BinaryParser.prototype.fromShort = function(number){return this.encodeInt(number, 16, true);};
PROTO.BinaryParser.prototype.toWord = function(data){return this.decodeInt(data, 16, false);};
PROTO.BinaryParser.prototype.fromWord = function(number){return this.encodeInt(number, 16, false);};
PROTO.BinaryParser.prototype.toInt = function(data){return this.decodeInt(data, 32, true);};
PROTO.BinaryParser.prototype.fromInt = function(number){return this.encodeInt(number, 32, true);};
PROTO.BinaryParser.prototype.toDWord = function(data){return this.decodeInt(data, 32, false);};
PROTO.BinaryParser.prototype.fromDWord = function(number){return this.encodeInt(number, 32, false);};
PROTO.BinaryParser.prototype.toFloat = typeof(Float32Array) != "undefined"?PROTO.BinaryParser.prototype.decodeFloat32:function(data){return this.decodeFloat(data, 23, 8);};
PROTO.BinaryParser.prototype.fromFloat = typeof(Float32Array) != "undefined"?PROTO.BinaryParser.prototype.encodeFloat32:function(number){return this.encodeFloat(number, 23, 8);};
PROTO.BinaryParser.prototype.toDouble = typeof(Float64Array) != "undefined"?PROTO.BinaryParser.prototype.decodeFloat64:function(data){return this.decodeFloat(data, 52, 11);};
PROTO.BinaryParser.prototype.fromDouble = typeof(Float64Array) != "undefined"?PROTO.BinaryParser.prototype.encodeFloat64:function(number){return this.encodeFloat(number, 52, 11);};
PROTO.binaryParser = new PROTO.BinaryParser(false,false);
PROTO.encodeUTF8 = function(str) {
var strlen = str.length;
var u8 = [];
var c, nextc;
var x, y, z;
for (var i = 0; i < strlen; i++) {
c = str.charCodeAt(i);
if ((c & 0xff80) == 0) {
// ASCII
u8.push(c);
} else {
if ((c & 0xfc00) == 0xD800) {
nextc = str.charCodeAt(i+1);
if ((nextc & 0xfc00) == 0xDC00) {
// UTF-16 Surrogate pair
c = (((c & 0x03ff)<<10) | (nextc & 0x3ff)) + 0x10000;
i++;
} else {
// error.
PROTO.warn("Error decoding surrogate pair: "+c+"; "+nextc);
}
}
x = c&0xff;
y = c&0xff00;
z = c&0xff0000;
// Encode UCS code into UTF-8
if (c <= 0x0007ff) {
u8.push(0xc0 | (y>>6) | (x>>6));
u8.push(0x80 | (x&63));
} else if (c <= 0x00ffff) {
u8.push(0xe0 | (y>>12));
u8.push(0x80 | ((y>>6)&63) | (x>>6));
u8.push(0x80 | (x&63));
} else if (c <= 0x10ffff) {
u8.push(0xf0 | (z>>18));
u8.push(0x80 | ((z>>12)&63) | (y>>12));
u8.push(0x80 | ((y>>6)&63) | (x>>6));
u8.push(0x80 | (x&63));
} else {
// error.
PROTO.warn("Error encoding to utf8: "+c+" is greater than U+10ffff");
u8.push("?".charCodeAt(0));
}
}
}
return u8;
}
PROTO.decodeUTF8 = function(u8) {
var u8len = u8.length;
var str = "";
var c, b2, b3, b4;
for (var i = 0; i < u8len; i++) {
c = u8[i];
if ((c&0x80) == 0x00) {
} else if ((c&0xf8) == 0xf0) {
// 4 bytes: U+10000 - U+10FFFF
b2 = u8[i+1];
b3 = u8[i+2];
b4 = u8[i+3];
if ((b2&0xc0) == 0x80 && (b3&0xc0) == 0x80 && (b4&0xc0) == 0x80) {
c = (c&7)<<18 | (b2&63)<<12 | (b3&63)<<6 | (b4&63);
i+=3;
} else {
// error.
PROTO.warn("Error decoding from utf8: "+c+","+b2+","+b3+","+b4);
continue;
}
} else if ((c&0xf0)==0xe0) {
// 3 bytes: U+0800 - U+FFFF
b2 = u8[i+1];
b3 = u8[i+2];
if ((b2&0xc0) == 0x80 && (b3&0xc0) == 0x80) {
c = (c&15)<<12 | (b2&63)<<6 | (b3&63);
i+=2;
} else {
// error.
PROTO.warn("Error decoding from utf8: "+c+","+b2+","+b3);
continue;
}
} else if ((c&0xe0)==0xc0) {
// 2 bytes: U+0080 - U+07FF
b2 = u8[i+1];
if ((b2&0xc0) == 0x80) {
c = (c&31)<<6 | (b2&63);
i+=1;
} else {
// error.
PROTO.warn("Error decoding from utf8: "+c+","+b2);
continue;
}
} else {
// error.
// 80-BF: Second, third, or fourth byte of a multi-byte sequence
// F5-FF: Start of 4, 5, or 6 byte sequence
PROTO.warn("Error decoding from utf8: "+c+" encountered not in multi-byte sequence");
continue;
}
if (c <= 0xffff) {
str += String.fromCharCode(c);
} else if (c > 0xffff && c <= 0x10ffff) {
// Must be encoded into UTF-16 surrogate pair.
c -= 0x10000;
str += (String.fromCharCode(0xD800 | (c>>10)) + String.fromCharCode(0xDC00 | (c&1023)));
} else {
PROTO.warn("Error encoding surrogate pair: "+c+" is greater than U+10ffff");
}
}
return str;
}
/**
* @constructor
*/
PROTO.Stream = function () {
this.write_pos_ = 0;
this.read_pos_ = 0;
};
PROTO.Stream.prototype = {
read: function(amt) {
var result = [];
for (var i = 0; i < amt; ++i) {
var byt = this.readByte();
if (byt === null) {
break;
}
result.push(byt);
}
return result;
},
write: function(array) {
for (var i = 0; i < array.length; i++) {
this.writeByte(array[i]);
}
},
readByte: function() {
return null;
},
writeByte: function(byt) {
this.write_pos_ += 1;
},
readPosition: function() {
return this.read_pos_;
},
setReadPosition: function(pos) {
this.read_pos_=pos;
},
writePosition: function() {
return this.write_pos_;
},
valid: function() {
return false;
}
};
/**
* @constructor
* @extends {PROTO.Stream}
* @param {Array=} arr Existing byte array to read from, or append to.
*/
PROTO.ByteArrayStream = function(arr) {
this.array_ = arr || new Array();
this.read_pos_ = 0;
this.write_pos_ = this.array_.length;
};
PROTO.ByteArrayStream.prototype = new PROTO.Stream();
PROTO.ByteArrayStream.prototype.read = function(amt) {
if (this.read_pos_+amt > this.array_.length) {
// incomplete stream.
//throw new Error("Read past end of protobuf ByteArrayStream: "+
// this.array_.length+" < "+this.read_pos_+amt);
return null;
}
var ret = this.array_.slice(this.read_pos_, this.read_pos_+amt);
this.read_pos_ += amt;
return ret;
};
PROTO.ByteArrayStream.prototype.write = function(arr) {
Array.prototype.push.apply(this.array_, arr);
this.write_pos_ = this.array_.length;
};
PROTO.ByteArrayStream.prototype.readByte = function() {
return this.array_[this.read_pos_ ++];
};
PROTO.ByteArrayStream.prototype.writeByte = function(byt) {
this.array_.push(byt);
this.write_pos_ = this.array_.length;
};
PROTO.ByteArrayStream.prototype.valid = function() {
return this.read_pos_ < this.array_.length;
};
PROTO.ByteArrayStream.prototype.getArray = function() {
return this.array_;
};
/**
* @constructor
*/
PROTO.Uint8ArrayStream = function(arr) {
this.array_ = arr || new Uint8Array(4096);
this.read_pos_ = 0;
this.write_pos_ = 0;
}
PROTO.Uint8ArrayStream.prototype._realloc = function(new_size) {
this.array_ = new Uint8Array(Math.max(new_size, this.array_.length)
+ this.array_.length);
}
PROTO.Uint8ArrayStream.prototype.read = function(amt) {
if (this.read_pos_+amt > this.array_.length) {
return null;
}
var ret = this.array_.subarray(this.read_pos_, this.read_pos_+amt);
this.read_pos_ += amt;
return ret;
};
PROTO.Uint8ArrayStream.prototype.write = function(arr) {
if (this.write_pos_ + arr.length > this.array_.length) {
this._realloc(this.write_pos_ + arr.length);
}
this.array_.set(arr, this.write_pos_);
this.write_pos_ += arr.length;
};
PROTO.Uint8ArrayStream.prototype.readByte = function() {
return this.array_[this.read_pos_ ++];
};
PROTO.Uint8ArrayStream.prototype.writeByte = function(byt) {
if (this.write_pos_ >= this.array_.length) {
this._realloc(this.write_pos_ + 1);
}
this.array_[this.write_pos_++] = byt;
};
PROTO.Uint8ArrayStream.prototype.valid = function() {
return this.read_pos_ < this.array_.length;
};
PROTO.Uint8ArrayStream.prototype.getArray = function() {
return this.array_.subarray(0, this.write_pos_);
};
PROTO.CreateArrayStream = function(arr) {
if (arr instanceof Array) {
return new PROTO.ByteArrayStream(arr);
} else {
return new PROTO.Uint8ArrayStream(arr);
}
};
(function(){
var FromB64AlphaMinus43=[
62,-1,62,-1,63,52,53,54,55,56,57,58,59,60,61,
-1,-1,-1,-1,-1,-1,-1,
0,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,
-1,-1,-1,-1,63,-1,
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];
var ToB64Alpha=[
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'];
var ToB64Alpha_URLSafe=[
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','-','_'];
/**
* @constructor
* @extends {PROTO.Stream}
* @param {string=} b64string String to read from, or append to.
*/
PROTO.Base64Stream = function(b64string) {
this.alphabet = ToB64Alpha;
this.string_ = b64string || '';
this.read_pos_ = 0;
this.read_incomplete_value_ = 0;
this.read_needed_bits_ = 8;
this.write_extra_bits_ = 0;
this.write_incomplete_value_ = 0;
this.fixString();
};
PROTO.Base64Stream.prototype = new PROTO.Stream();
PROTO.Base64Stream.prototype.setURLSafe = function() {
this.alphabet = ToB64Alpha_URLSafe;
};
PROTO.Base64Stream.prototype.fixString = function() {
var len = this.string_.length;
if (this.string_[len-1]=='=') {
var n = 4;
var cutoff = 2;
if (this.string_[len-cutoff]=='=') {
n = 2;
cutoff = 3;
}
this.write_extra_bits_ = n;
this.write_incomplete_value_ = FromB64AlphaMinus43[
this.string_.charCodeAt(len-cutoff)-43];
this.write_incomplete_value_ >>= (6-n);
this.string_ = this.string_.substring(0,len-cutoff);
}
};
PROTO.Base64Stream.prototype.readByte = function() {
var next6bits;
var n = this.read_needed_bits_;
while (next6bits === undefined || next6bits == -1) {
if (this.read_pos_ >= this.string_.length) {
if (this.valid()) {
next6bits = this.write_incomplete_value_ << (6-n);
this.read_pos_++;
break;
} else {
return null;
}
}
next6bits = FromB64AlphaMinus43[
this.string_.charCodeAt(this.read_pos_++)-43];
}
if (n == 8) {
this.read_incomplete_value_ = next6bits;
this.read_needed_bits_ = 2;
return this.readByte();
}
var ret = this.read_incomplete_value_<<n;
ret |= next6bits>>(6-n);
this.read_incomplete_value_ = next6bits&((1<<(6-n))-1);
this.read_needed_bits_ += 2;
return ret;
};
PROTO.Base64Stream.prototype.writeByte = function(byt) {
this.write_extra_bits_ += 2;
var n = this.write_extra_bits_;
this.string_ += this.alphabet[
byt>>n | this.write_incomplete_value_<<(8-n)];
this.write_incomplete_value_ = (byt&((1<<n)-1));
if (n == 6) {
this.string_ += this.alphabet[this.write_incomplete_value_];
this.write_extra_bits_ = 0;
this.write_incomplete_value_ = 0;
}
if (this.string_.length%77==76) {
this.string_ += "\n";
}
};
PROTO.Base64Stream.prototype.getString = function() {
var len = this.string_.length;
var retstr = this.string_;
var n = this.write_extra_bits_;
if (n > 0) {
retstr += this.alphabet[this.write_incomplete_value_<<(6-n)];
if (n==2) {
retstr += "==";
} else if (n==4) {
retstr += "=";
}
}
return retstr;
};
PROTO.Base64Stream.prototype.valid = function() {
return (this.read_pos_ < this.string_.length) ||
(this.read_pos_==this.string_.length && this.write_extra_bits_);
};
})();
if (typeof(ArrayBuffer) !== "undefined" && typeof(Uint8Array) !== "undefined") {
/**
* @constructor
* @extends {PROTO.Stream}
* @param {Array|ArrayBuffer|TypedArray} arr
* @param {number=} length
*/
PROTO.ArrayBufferStream = function(arr, length) {
this.array_buffer_ = arr || new ArrayBuffer(1024);
this.length_ = length || 0;
this.array_ = new Uint8Array(this.array_buffer_);
this.read_pos = 0;
};
PROTO.ArrayBufferStream.prototype = new PROTO.Stream();
PROTO.ArrayBufferStream.prototype._realloc = function(min_length) {
var old_array = this.array_;
var length = this.length_;
var new_buf_length = old_array.length + min_length;
this.array_buffer_ = new ArrayBuffer(new_buf_length);
var new_array = new Uint8Array(this.array_buffer_);
for (var i = 0; i < length; i++) {
new_array[i] = old_array[i];
}
this.array_ = new_array;
};
PROTO.ArrayBufferStream.prototype.read = function(amt) {
if (this.read_pos_+amt > this.length_) {
// incomplete stream.
//throw new Error("Read past end of protobuf ArrayBufferStream: "+
// this.array_.length+" < "+this.read_pos_+amt);
return null;
}
var ret = this.array_.subarray(this.read_pos_, this.read_pos_+amt);
this.read_pos_ += amt;
// FIXME
var ret_as_array = new Array(amt);
for (var i = 0; i < amt; i++) {
ret_as_array[i] = ret[i];
}
return ret_as_array;
};
PROTO.ArrayBufferStream.prototype.write = function(arr) {
var si = 0;
var di = this.length_;
if (this.length_ + arr.length > this.array_.length) {
this._realloc(this.length_ + arr.length);
}
this.length_ += arr.length;
var dest = this.array_;
var len = arr.length;
for (;si < len; si++,di++) {
dest[di] = arr[si];
}
};
PROTO.ArrayBufferStream.prototype.readByte = function() {
return this.array_[this.read_pos_ ++];