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
|
<?php
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// 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 Google Inc. 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.
namespace Google\Protobuf\Internal;
class GPBWire
{
const TAG_TYPE_BITS = 3;
const WIRETYPE_VARINT = 0;
const WIRETYPE_FIXED64 = 1;
const WIRETYPE_LENGTH_DELIMITED = 2;
const WIRETYPE_START_GROUP = 3;
const WIRETYPE_END_GROUP = 4;
const WIRETYPE_FIXED32 = 5;
const UNKNOWN = 0;
const NORMAL_FORMAT = 1;
const PACKED_FORMAT = 2;
public static function getTagFieldNumber($tag)
{
return ($tag >> self::TAG_TYPE_BITS) &
(1 << ((PHP_INT_SIZE * 8) - self::TAG_TYPE_BITS)) - 1;
}
public static function getTagWireType($tag)
{
return $tag & 0x7;
}
public static function getWireType($type)
{
switch ($type) {
case GPBType::FLOAT:
case GPBType::FIXED32:
case GPBType::SFIXED32:
return self::WIRETYPE_FIXED32;
case GPBType::DOUBLE:
case GPBType::FIXED64:
case GPBType::SFIXED64:
return self::WIRETYPE_FIXED64;
case GPBType::UINT32:
case GPBType::UINT64:
case GPBType::INT32:
case GPBType::INT64:
case GPBType::SINT32:
case GPBType::SINT64:
case GPBType::ENUM:
case GPBType::BOOL:
return self::WIRETYPE_VARINT;
case GPBType::STRING:
case GPBType::BYTES:
case GPBType::MESSAGE:
return self::WIRETYPE_LENGTH_DELIMITED;
case GPBType::GROUP:
user_error("Unsupported type.");
return 0;
default:
user_error("Unsupported type.");
return 0;
}
}
// ZigZag Transform: Encodes signed integers so that they can be effectively
// used with varint encoding.
//
// varint operates on unsigned integers, encoding smaller numbers into fewer
// bytes. If you try to use it on a signed integer, it will treat this
// number as a very large unsigned integer, which means that even small
// signed numbers like -1 will take the maximum number of bytes (10) to
// encode. zigZagEncode() maps signed integers to unsigned in such a way
// that those with a small absolute value will have smaller encoded values,
// making them appropriate for encoding using varint.
//
// int32 -> uint32
// -------------------------
// 0 -> 0
// -1 -> 1
// 1 -> 2
// -2 -> 3
// ... -> ...
// 2147483647 -> 4294967294
// -2147483648 -> 4294967295
//
// >> encode >>
// << decode <<
public static function zigZagEncode32($int32)
{
if (PHP_INT_SIZE == 8) {
$trim_int32 = $int32 & 0xFFFFFFFF;
return (($trim_int32 << 1) ^ ($int32 << 32 >> 63)) & 0xFFFFFFFF;
} else {
return ($int32 << 1) ^ ($int32 >> 31);
}
}
public static function zigZagDecode32($uint32)
{
// Fill high 32 bits.
if (PHP_INT_SIZE === 8) {
$uint32 |= ($uint32 & 0xFFFFFFFF);
}
$int32 = (($uint32 >> 1) & 0x7FFFFFFF) ^ (-($uint32 & 1));
return $int32;
}
public static function zigZagEncode64($int64)
{
if (PHP_INT_SIZE == 4) {
if (bccomp($int64, 0) >= 0) {
return bcmul($int64, 2);
} else {
return bcsub(bcmul(bcsub(0, $int64), 2), 1);
}
} else {
return ($int64 << 1) ^ ($int64 >> 63);
}
}
public static function zigZagDecode64($uint64)
{
if (PHP_INT_SIZE == 4) {
if (bcmod($uint64, 2) == 0) {
return bcdiv($uint64, 2, 0);
} else {
return bcsub(0, bcdiv(bcadd($uint64, 1), 2, 0));
}
} else {
return (($uint64 >> 1) & 0x7FFFFFFFFFFFFFFF) ^ (-($uint64 & 1));
}
}
public static function readInt32(&$input, &$value)
{
return $input->readVarint32($value);
}
public static function readInt64(&$input, &$value)
{
$success = $input->readVarint64($value);
if (PHP_INT_SIZE == 4 && bccomp($value, "9223372036854775807") > 0) {
$value = bcsub($value, "18446744073709551616");
}
return $success;
}
public static function readUint32(&$input, &$value)
{
return self::readInt32($input, $value);
}
public static function readUint64(&$input, &$value)
{
return self::readInt64($input, $value);
}
public static function readSint32(&$input, &$value)
{
if (!$input->readVarint32($value)) {
return false;
}
$value = GPBWire::zigZagDecode32($value);
return true;
}
public static function readSint64(&$input, &$value)
{
if (!$input->readVarint64($value)) {
return false;
}
$value = GPBWire::zigZagDecode64($value);
return true;
}
public static function readFixed32(&$input, &$value)
{
return $input->readLittleEndian32($value);
}
public static function readFixed64(&$input, &$value)
{
return $input->readLittleEndian64($value);
}
public static function readSfixed32(&$input, &$value)
{
if (!self::readFixed32($input, $value)) {
return false;
}
if (PHP_INT_SIZE === 8) {
$value |= (-($value >> 31) << 32);
}
return true;
}
public static function readSfixed64(&$input, &$value)
{
$success = $input->readLittleEndian64($value);
if (PHP_INT_SIZE == 4 && bccomp($value, "9223372036854775807") > 0) {
$value = bcsub($value, "18446744073709551616");
}
return $success;
}
public static function readFloat(&$input, &$value)
{
$data = null;
if (!$input->readRaw(4, $data)) {
return false;
}
$value = unpack('f', $data)[1];
return true;
}
public static function readDouble(&$input, &$value)
{
$data = null;
if (!$input->readRaw(8, $data)) {
return false;
}
$value = unpack('d', $data)[1];
return true;
}
public static function readBool(&$input, &$value)
{
if (!$input->readVarint64($value)) {
return false;
}
if ($value == 0) {
$value = false;
} else {
$value = true;
}
return true;
}
public static function readString(&$input, &$value)
{
$length = 0;
return $input->readVarintSizeAsInt($length) && $input->readRaw($length, $value);
}
public static function readMessage(&$input, &$message)
{
$length = 0;
if (!$input->readVarintSizeAsInt($length)) {
return false;
}
$old_limit = 0;
$recursion_limit = 0;
$input->incrementRecursionDepthAndPushLimit(
$length,
$old_limit,
$recursion_limit);
if ($recursion_limit < 0 || !$message->parseFromStream($input)) {
return false;
}
return $input->decrementRecursionDepthAndPopLimit($old_limit);
}
public static function writeTag(&$output, $tag)
{
return $output->writeTag($tag);
}
public static function writeInt32(&$output, $value)
{
return $output->writeVarint32($value, false);
}
public static function writeInt64(&$output, $value)
{
return $output->writeVarint64($value);
}
public static function writeUint32(&$output, $value)
{
return $output->writeVarint32($value, true);
}
public static function writeUint64(&$output, $value)
{
return $output->writeVarint64($value);
}
public static function writeSint32(&$output, $value)
{
$value = GPBWire::zigZagEncode32($value);
return $output->writeVarint32($value, true);
}
public static function writeSint64(&$output, $value)
{
$value = GPBWire::zigZagEncode64($value);
return $output->writeVarint64($value);
}
public static function writeFixed32(&$output, $value)
{
return $output->writeLittleEndian32($value);
}
public static function writeFixed64(&$output, $value)
{
return $output->writeLittleEndian64($value);
}
public static function writeSfixed32(&$output, $value)
{
return $output->writeLittleEndian32($value);
}
public static function writeSfixed64(&$output, $value)
{
return $output->writeLittleEndian64($value);
}
public static function writeBool(&$output, $value)
{
if ($value) {
return $output->writeVarint32(1, true);
} else {
return $output->writeVarint32(0, true);
}
}
public static function writeFloat(&$output, $value)
{
$data = pack("f", $value);
return $output->writeRaw($data, 4);
}
public static function writeDouble(&$output, $value)
{
$data = pack("d", $value);
return $output->writeRaw($data, 8);
}
public static function writeString(&$output, $value)
{
return self::writeBytes($output, $value);
}
public static function writeBytes(&$output, $value)
{
$size = strlen($value);
if (!$output->writeVarint32($size, true)) {
return false;
}
return $output->writeRaw($value, $size);
}
public static function writeMessage(&$output, $value)
{
$size = $value->byteSize();
if (!$output->writeVarint32($size, true)) {
return false;
}
return $value->serializeToStream($output);
}
public static function makeTag($number, $type)
{
return ($number << 3) | self::getWireType($type);
}
public static function tagSize($field)
{
$tag = self::makeTag($field->getNumber(), $field->getType());
return self::varint32Size($tag);
}
public static function varint32Size($value, $sign_extended = false)
{
if ($value < 0) {
if ($sign_extended) {
return 10;
} else {
return 5;
}
}
if ($value < (1 << 7)) {
return 1;
}
if ($value < (1 << 14)) {
return 2;
}
if ($value < (1 << 21)) {
return 3;
}
if ($value < (1 << 28)) {
return 4;
}
return 5;
}
public static function sint32Size($value)
{
$value = self::zigZagEncode32($value);
return self::varint32Size($value);
}
public static function sint64Size($value)
{
$value = self::zigZagEncode64($value);
return self::varint64Size($value);
}
public static function varint64Size($value)
{
if (PHP_INT_SIZE == 4) {
if (bccomp($value, 0) < 0 ||
bccomp($value, "9223372036854775807") > 0) {
return 10;
}
if (bccomp($value, 1 << 7) < 0) {
return 1;
}
if (bccomp($value, 1 << 14) < 0) {
return 2;
}
if (bccomp($value, 1 << 21) < 0) {
return 3;
}
if (bccomp($value, 1 << 28) < 0) {
return 4;
}
if (bccomp($value, '34359738368') < 0) {
return 5;
}
if (bccomp($value, '4398046511104') < 0) {
return 6;
}
if (bccomp($value, '562949953421312') < 0) {
return 7;
}
if (bccomp($value, '72057594037927936') < 0) {
return 8;
}
return 9;
} else {
if ($value < 0) {
return 10;
}
if ($value < (1 << 7)) {
return 1;
}
if ($value < (1 << 14)) {
return 2;
}
if ($value < (1 << 21)) {
return 3;
}
if ($value < (1 << 28)) {
return 4;
}
if ($value < (1 << 35)) {
return 5;
}
if ($value < (1 << 42)) {
return 6;
}
if ($value < (1 << 49)) {
return 7;
}
if ($value < (1 << 56)) {
return 8;
}
return 9;
}
}
public static function serializeFieldToStream(
$value,
$field,
$need_tag,
&$output)
{
if ($need_tag) {
if (!GPBWire::writeTag(
$output,
self::makeTag(
$field->getNumber(),
$field->getType()))) {
return false;
}
}
switch ($field->getType()) {
case GPBType::DOUBLE:
if (!GPBWire::writeDouble($output, $value)) {
return false;
}
break;
case GPBType::FLOAT:
if (!GPBWire::writeFloat($output, $value)) {
return false;
}
break;
case GPBType::INT64:
if (!GPBWire::writeInt64($output, $value)) {
return false;
}
break;
case GPBType::UINT64:
if (!GPBWire::writeUint64($output, $value)) {
return false;
}
break;
case GPBType::INT32:
if (!GPBWire::writeInt32($output, $value)) {
return false;
}
break;
case GPBType::FIXED32:
if (!GPBWire::writeFixed32($output, $value)) {
return false;
}
break;
case GPBType::FIXED64:
if (!GPBWire::writeFixed64($output, $value)) {
return false;
}
break;
case GPBType::BOOL:
if (!GPBWire::writeBool($output, $value)) {
return false;
}
break;
case GPBType::STRING:
if (!GPBWire::writeString($output, $value)) {
return false;
}
break;
// case GPBType::GROUP:
// echo "GROUP\xA";
// trigger_error("Not implemented.", E_ERROR);
// break;
case GPBType::MESSAGE:
if (!GPBWire::writeMessage($output, $value)) {
return false;
}
break;
case GPBType::BYTES:
if (!GPBWire::writeBytes($output, $value)) {
return false;
}
break;
case GPBType::UINT32:
if (PHP_INT_SIZE === 8 && $value < 0) {
$value += 4294967296;
}
if (!GPBWire::writeUint32($output, $value)) {
return false;
}
break;
case GPBType::ENUM:
if (!GPBWire::writeInt32($output, $value)) {
return false;
}
break;
case GPBType::SFIXED32:
if (!GPBWire::writeSfixed32($output, $value)) {
return false;
}
break;
case GPBType::SFIXED64:
if (!GPBWire::writeSfixed64($output, $value)) {
return false;
}
break;
case GPBType::SINT32:
if (!GPBWire::writeSint32($output, $value)) {
return false;
}
break;
case GPBType::SINT64:
if (!GPBWire::writeSint64($output, $value)) {
return false;
}
break;
default:
user_error("Unsupported type.");
return false;
}
return true;
}
}
|