-
Notifications
You must be signed in to change notification settings - Fork 29
/
protobuf.js
1560 lines (1515 loc) · 56.8 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.
*/
var PROTO = {};
PROTO.DefineProperty = (function () {
var DefineProperty;
if (typeof(Object.defineProperty) != "undefined") {
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 {
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) {
console.log("DefineProperty test gave the wrong result "+testinst.x);
DefineProperty = undefined;
}
} catch (e) {
if (typeof(console)!="undefined" && console.log) {
console.log("DefineProperty should be supported, but threw "+e,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';
/**
* @constructor
*/
PROTO.I64 = function (msw, lsw, sign) {
this.msw = msw;
this.lsw = lsw;
if (typeof lsw === undefined) {
console.error("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+=2;
local_lsw=2147483647-this.lsw;
local_lsw+=2147483647;
local_lsw+=2;
}else {
local_msw=this.msw;
}
return new PROTO.I64(local_msw,local_lsw,1);
},
convertFromUnsigned:function() {
if(this.msw>=2147483648) {
return new PROTO.I64(this.msw-2147483648,this.lsw,-1);
}
return new PROTO.I64(this.msw,this.lsw,1);
},
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;
}
return new PROTO.I64(local_msw,local_lsw,1);
},
convertFromZigzag:function() {
if(this.msw&1) {
return new PROTO.I64((this.msw>>>1),
2147483648+(this.lsw>>>1),
(this.lsw&1)?-1:1);
}
return new PROTO.I64((this.msw>>>1),
(this.lsw>>>1),
(this.lsw&1)?-1:1);
},
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+=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);
},
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 = ((mynum-lsw)/4294967296);
return new PROTO.I64(msw, lsw, sign);
};
PROTO.I64.from32pair = function(msw, lsw, sign) {
return new PROTO.I64(msw, lsw, sign);
};
PROTO.I64.parseLEVar128 = function (stream) {
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 = 0;
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;
}
return new PROTO.I64(msw%4294967296,lsw,1);
};
PROTO.I64.parseLEBase256 = function (stream) {
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;
}
return new PROTO.I64(msw,lsw,1);
};
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);
};
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 = function(data){return this.decodeFloat(data, 23, 8);};
PROTO.BinaryParser.prototype.fromFloat = function(number){return this.encodeFloat(number, 23, 8);};
PROTO.BinaryParser.prototype.toDouble = function(data){return this.decodeFloat(data, 52, 11);};
PROTO.BinaryParser.prototype.fromDouble = 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.
console.log("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.
console.log("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.
console.log("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.
console.log("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.
console.log("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
console.log("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 {
console.log("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;
},
valid: function() {
return false;
}
};
/**
* @constructor
* @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) {
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
* @param {string=} b64string String to read from, or append to.
*/
(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','-','_'];
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_);
};
})();
PROTO.array =
(function() {
/** @constructor */
function ProtoArray(datatype, input) {
this.datatype_ = datatype.type();
this.length = 0;
if (input instanceof Array) {
for (var i=0;i<input.length;++i) {
this.push(input[i]);
}
}
};
ProtoArray.IsInitialized = function IsInitialized(val) {
return val.length > 0;
}
ProtoArray.prototype = {};
ProtoArray.prototype.push = function (var_args) {
if (arguments.length === 0) {
if (this.datatype_.composite) {
var newval = new this.datatype_;
this[this.length++] = newval;
return newval;
} else {
throw "Called add(undefined) for a non-composite";
}
} else {
for (var i = 0; i < arguments.length; i++) {
var newval = this.datatype_.Convert(arguments[i]);
if (this.datatype_.FromProto) {
newval = this.datatype_.FromProto(newval);
}
this[this.length++] = newval;
}
}
return arguments[0];
}
ProtoArray.prototype.set = function (index, newval) {
newval = this.datatype_.Convert(newval);
if (this.datatype_.FromProto) {
newval = this.datatype_.FromProto(newval);
}
if (index < this.length && index >= 0) {
this[index] = newval;
} else if (index == this.length) {
this[this.length++] = newval;
} else {
throw "Called ProtoArray.set with index "+index+" higher than length "+this.length;
}
return newval;
}
ProtoArray.prototype.clear = function (index, newval) {
this.length = 0;
}
return ProtoArray;
})();
PROTO.string = {
Convert: function(str) {
return ''+str;
},
wiretype: PROTO.wiretypes.lengthdelim,
SerializeToStream: function(str, stream) {
var arr = PROTO.encodeUTF8(str);
return PROTO.bytes.SerializeToStream(arr, stream);
},
ParseFromStream: function(stream) {
var arr = PROTO.bytes.ParseFromStream(stream);
return PROTO.decodeUTF8(arr);
},
toString: function(str) {return str;}
};
PROTO.bytes = {
Convert: function(arr) {
if (arr instanceof Array) {
return arr;
} else if (arr instanceof PROTO.ByteArrayStream) {
return arr.getArray();
} else if (arr.SerializeToStream) {
/* This is useful for messages (e.g. RPC calls) that embed
* other messages inside them using the bytes type.
*/
// FIXME: should we always allow this? Can this cause mistakes?
var tempStream = new PROTO.ByteArrayStream;
arr.SerializeToStream(tempStream);
return tempStream.getArray();
} else {
throw "Not a Byte Array: "+arr;
}
},
wiretype: PROTO.wiretypes.lengthdelim,
SerializeToStream: function(arr, stream) {
PROTO.int32.SerializeToStream(arr.length, stream);
stream.write(arr);
},
ParseFromStream: function(stream) {
var len = PROTO.int32.ParseFromStream(stream);
return stream.read(len);
},
toString: function(bytes) {return '['+bytes+']';}
};
(function() {
function makeclass(converter, serializer, parser) {
var myclass = {
Convert: converter,
wiretype: 0,
SerializeToStream: serializer,
ParseFromStream: parser,
toString: function(val) {return "" + val}
};
return myclass;
};
function convertU32(n) { //unsigned
if (n == NaN) {
throw "not a number: "+n;
}
n = Math.round(n);
if (n < 0) {
throw "uint32/fixed32 does not allow negative: "+n;
}
if (n > 4294967295) {
throw "uint32/fixed32 out of bounds: "+n;
}
return n;
};
function convertS32(n) { // signed
if (n == NaN) {
throw "not a number: "+n;
}
n = Math.round(n);
if (n > 2147483647 || n < -2147483648) {
throw "sfixed32/[s]int32 out of bounds: "+n;
}
return n;
};
function serializeFixed32(n, stream) {
if (n<0) n += 4294967296;
var arr = new Array(4);
for (var i = 0; i < 4; i++) {
arr[i] = n%256;
n >>>= 8;
}
stream.write(arr);
};
function parseSFixed32(stream) {
var n = 0;
var offset=1;
for (var i = 0; i < 4; i++) {
n += offset*stream.readByte();
offset *= 256;
}
return n;
};
function parseFixed32(stream) {
var n = parseSFixed32(stream);
if (n > 2147483647) {
n -= 4294967296;
}
return n;
};
function serializeInt32(n, stream) {
if (n < 0) {
serializeInt64(PROTO.I64.fromNumber(n),stream);
return;
}
// Loop once regardless of whether n is 0.
for (var i = 0; i==0 || (n && i < 5); i++) {
var byt = n%128;
n >>>= 7;
if (n) {
byt += 128;
}
stream.writeByte(byt);
}
};
function serializeSInt32(n, stream) {
if (n < 0) {
n = -n*2-1;
} else {
n = n*2;
}
serializeInt32(n, stream);
};
function parseUInt32(stream) {
var n = 0;
var endloop = false;
var offset=1;
for (var i = 0; !endloop && i < 5; i++) {
var byt = stream.readByte();
if (byt === undefined) {
console.log("read undefined byte from stream: n is "+n);
break;
}
if (byt < 128) {
endloop = true;
}
n += offset*(byt&(i==4?15:127));
offset *= 128;
}
return n;
};
function parseInt32(stream) {
var n = parseUInt32(stream);//snag the first 4 bytes
if (n > 2147483647) {
n -= 2147483647;
n -= 2147483647;
n -= 2;
}
return n;
};
function parseSInt32(stream) {
var n = parseUInt32(stream);
if (n & 1) {
return (n+1) / -2;
}
return n / 2;
}
PROTO.sfixed32 = makeclass(convertS32, serializeFixed32, parseSFixed32);
PROTO.fixed32 = makeclass(convertU32, serializeFixed32, parseFixed32);
PROTO.sfixed32.wiretype = PROTO.wiretypes.fixed32;
PROTO.fixed32.wiretype = PROTO.wiretypes.fixed32;
PROTO.int32 = makeclass(convertS32, serializeInt32, parseInt32);
PROTO.sint32 = makeclass(convertS32, serializeSInt32, parseSInt32);
PROTO.uint32 = makeclass(convertU32, serializeInt32, parseUInt32);
function convert64(n) {
if (n instanceof PROTO.I64) {
return n;
}
throw "64-bit integers must be PROTO.I64 objects!";
};
function serializeInt64(n, stream) {
stream.write(n.convertToUnsigned().serializeToLEVar128());
}
function serializeSInt64(n, stream) {
stream.write(n.convertToZigzag().serializeToLEVar128());
}
function serializeUInt64(n, stream) {
stream.write(n.serializeToLEVar128());
}
function serializeSFixed64(n, stream) {
stream.write(n.convertToUnsigned().serializeToLEBase256());
}
function serializeFixed64(n, stream) {
stream.write(n.serializeToLEBase256());
}
function parseSFixed64(stream) {
return PROTO.I64.parseLEBase256(stream).convertFromUnsigned();
}
function parseFixed64(stream) {
return PROTO.I64.parseLEBase256(stream);
}
function parseSInt64(stream) {
return PROTO.I64.parseLEVar128(stream).convertFromZigzag();
}
function parseInt64(stream) {
return PROTO.I64.parseLEVar128(stream).convertFromUnsigned();
}