aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/src/Google/Protobuf/Internal
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2017-02-07 21:33:28 -0800
committerGravatar Paul Yang <TeBoring@users.noreply.github.com>2017-03-15 14:16:49 -0700
commitf23869c6154d8b083ee3417fac277bc25e13a4ac (patch)
tree2e9ea1cfcc88211ac1e364155d696c808acb11ba /php/src/Google/Protobuf/Internal
parent014a5507fb4b1ccc12f35ff313b8a04c05d69b7f (diff)
Bug fix: When encoding, negative int32 values should be padded to int64 (#2660)
in order to be wire compatible.
Diffstat (limited to 'php/src/Google/Protobuf/Internal')
-rw-r--r--php/src/Google/Protobuf/Internal/GPBWire.php8
-rw-r--r--php/src/Google/Protobuf/Internal/InputStream.php1
-rw-r--r--php/src/Google/Protobuf/Internal/Message.php4
-rw-r--r--php/src/Google/Protobuf/Internal/OutputStream.php15
4 files changed, 14 insertions, 14 deletions
diff --git a/php/src/Google/Protobuf/Internal/GPBWire.php b/php/src/Google/Protobuf/Internal/GPBWire.php
index f75e0861..67eb1bee 100644
--- a/php/src/Google/Protobuf/Internal/GPBWire.php
+++ b/php/src/Google/Protobuf/Internal/GPBWire.php
@@ -403,10 +403,14 @@ class GPBWire
return self::varint32Size($tag);
}
- public static function varint32Size($value)
+ public static function varint32Size($value, $sign_extended = false)
{
if ($value < 0) {
- return 5;
+ if ($sign_extended) {
+ return 10;
+ } else {
+ return 5;
+ }
}
if ($value < (1 << 7)) {
return 1;
diff --git a/php/src/Google/Protobuf/Internal/InputStream.php b/php/src/Google/Protobuf/Internal/InputStream.php
index bf052c2f..de5ca978 100644
--- a/php/src/Google/Protobuf/Internal/InputStream.php
+++ b/php/src/Google/Protobuf/Internal/InputStream.php
@@ -70,7 +70,6 @@ class InputStream
private $total_bytes_read;
const MAX_VARINT_BYTES = 10;
- const MAX_VARINT32_BYTES = 5;
const DEFAULT_RECURSION_LIMIT = 100;
const DEFAULT_TOTAL_BYTES_LIMIT = 33554432; // 32 << 20, 32MB
diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php
index ca4fde02..887c86ca 100644
--- a/php/src/Google/Protobuf/Internal/Message.php
+++ b/php/src/Google/Protobuf/Internal/Message.php
@@ -772,9 +772,11 @@ class Message
case GPBType::SFIXED64:
$size += 8;
break;
- case GPBType::UINT32:
case GPBType::INT32:
case GPBType::ENUM:
+ $size += GPBWire::varint32Size($value, true);
+ break;
+ case GPBType::UINT32:
$size += GPBWire::varint32Size($value);
break;
case GPBType::UINT64:
diff --git a/php/src/Google/Protobuf/Internal/OutputStream.php b/php/src/Google/Protobuf/Internal/OutputStream.php
index 587ac352..8c6d9b68 100644
--- a/php/src/Google/Protobuf/Internal/OutputStream.php
+++ b/php/src/Google/Protobuf/Internal/OutputStream.php
@@ -39,7 +39,6 @@ class OutputStream
private $buffer_size;
private $current;
- const MAX_VARINT32_BYTES = 5;
const MAX_VARINT64_BYTES = 10;
public function __construct($size)
@@ -56,8 +55,8 @@ class OutputStream
public function writeVarint32($value)
{
- $bytes = str_repeat(chr(0), self::MAX_VARINT32_BYTES);
- $size = self::writeVarintToArray($value, $bytes, true);
+ $bytes = str_repeat(chr(0), self::MAX_VARINT64_BYTES);
+ $size = self::writeVarintToArray($value, $bytes);
return $this->writeRaw($bytes, $size);
}
@@ -102,20 +101,16 @@ class OutputStream
return true;
}
- private static function writeVarintToArray($value, &$buffer, $trim = false)
+ private static function writeVarintToArray($value, &$buffer)
{
$current = 0;
$high = 0;
$low = 0;
if (PHP_INT_SIZE == 4) {
- GPBUtil::divideInt64ToInt32($value, $high, $low, $trim);
+ GPBUtil::divideInt64ToInt32($value, $high, $low);
} else {
- if ($trim) {
- $low = $value & 0xFFFFFFFF;
- } else {
- $low = $value;
- }
+ $low = $value;
}
while ($low >= 0x80 || $low < 0) {