diff options
author | Sufir <Sufir@users.noreply.github.com> | 2017-01-08 22:50:50 +0300 |
---|---|---|
committer | Paul Yang <TeBoring@users.noreply.github.com> | 2017-01-08 11:50:50 -0800 |
commit | 2bddffc993db05df57b6ebaea18f42c7b31753e9 (patch) | |
tree | 13a07943fc23b0ebf7682c246281989880d5e9e7 /php/src/Google/Protobuf | |
parent | 2c16f6979a03be84e359a09b92b28c87d57e5afc (diff) |
PHP fix int64 decoding (#2516)
* fix int64 decoding
* fix int64 decoding + tests
Diffstat (limited to 'php/src/Google/Protobuf')
-rw-r--r-- | php/src/Google/Protobuf/Internal/InputStream.php | 75 |
1 files changed, 47 insertions, 28 deletions
diff --git a/php/src/Google/Protobuf/Internal/InputStream.php b/php/src/Google/Protobuf/Internal/InputStream.php index 6d6c74e9..c5a76d5d 100644 --- a/php/src/Google/Protobuf/Internal/InputStream.php +++ b/php/src/Google/Protobuf/Internal/InputStream.php @@ -160,40 +160,59 @@ class InputStream */ public function readVarint64(&$var) { - $high = 0; - $low = 0; $count = 0; - $b = 0; - - do { - if ($this->current === $this->buffer_end) { - return false; - } - if ($count === self::MAX_VARINT_BYTES) { - return false; - } - $b = ord($this->buffer[$this->current]); - $bits = 7 * $count; - if ($bits >= 32) { - $high |= (($b & 0x7F) << ($bits - 32)); - } else if ($bits > 25){ - $high_bits = $bits - 25; - $low = ($low | (($b & 0x7F) << $bits)) & (int) 0xFFFFFFFF; - $high = $b & ((0x1 << $high_bits) -1); - } else { - $low |= (($b & 0x7F) << $bits); - } - - $this->advance(1); - $count += 1; - } while ($b & 0x80); if (PHP_INT_SIZE == 4) { + $high = 0; + $low = 0; + $b = 0; + + do { + if ($this->current === $this->buffer_end) { + return false; + } + if ($count === self::MAX_VARINT_BYTES) { + return false; + } + $b = ord($this->buffer[$this->current]); + $bits = 7 * $count; + if ($bits >= 32) { + $high |= (($b & 0x7F) << ($bits - 32)); + } else if ($bits > 25){ + $high_bits = $bits - 25; + $low = ($low | (($b & 0x7F) << $bits)) & (int) 0xFFFFFFFF; + $high = $b & ((0x1 << $high_bits) -1); + } else { + $low |= (($b & 0x7F) << $bits); + } + + $this->advance(1); + $count += 1; + } while ($b & 0x80); + $var = combineInt32ToInt64($high, $low); } else { - $var = ($high & 0xFFFFFFFF) << 32 | - ($low & 0xFFFFFFFF); + $result = 0; + $shift = 0; + + do { + if ($this->current === $this->buffer_end) { + return false; + } + if ($count === self::MAX_VARINT_BYTES) { + return false; + } + + $byte = ord($this->buffer[$this->current]); + $result |= ($byte & 0x7f) << $shift; + $shift += 7; + $this->advance(1); + $count += 1; + } while ($byte > 0x7f); + + $var = $result; } + return true; } |