aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/src/Google/Protobuf/Internal
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2016-09-15 11:09:01 -0700
committerGravatar GitHub <noreply@github.com>2016-09-15 11:09:01 -0700
commite0e54661f76183684dca66694967a60cbb10f04e (patch)
tree0a2a20f984705fdf6ef13de8829901b80f88efb5 /php/src/Google/Protobuf/Internal
parent86fcd879b38505446799b2f2a2929415ddad620a (diff)
Check in php implementation. (#2052)
This pull request includes two implementation: C extension and PHP package. Both implementations support encode/decode of singular, repeated and map fields.
Diffstat (limited to 'php/src/Google/Protobuf/Internal')
-rw-r--r--php/src/Google/Protobuf/Internal/DescriptorPool.php162
-rw-r--r--php/src/Google/Protobuf/Internal/EnumBuilderContext.php63
-rw-r--r--php/src/Google/Protobuf/Internal/GPBLabel.php40
-rw-r--r--php/src/Google/Protobuf/Internal/GPBType.php55
-rw-r--r--php/src/Google/Protobuf/Internal/GPBUtil.php161
-rw-r--r--php/src/Google/Protobuf/Internal/GPBWire.php583
-rw-r--r--php/src/Google/Protobuf/Internal/InputStream.php323
-rw-r--r--php/src/Google/Protobuf/Internal/MapEntry.php57
-rw-r--r--php/src/Google/Protobuf/Internal/MapField.php321
-rw-r--r--php/src/Google/Protobuf/Internal/Message.php671
-rw-r--r--php/src/Google/Protobuf/Internal/MessageBuilderContext.php120
-rw-r--r--php/src/Google/Protobuf/Internal/OneofField.php77
-rw-r--r--php/src/Google/Protobuf/Internal/OutputStream.php143
-rw-r--r--php/src/Google/Protobuf/Internal/RepeatedField.php303
-rw-r--r--php/src/Google/Protobuf/Internal/Type.php175
15 files changed, 3254 insertions, 0 deletions
diff --git a/php/src/Google/Protobuf/Internal/DescriptorPool.php b/php/src/Google/Protobuf/Internal/DescriptorPool.php
new file mode 100644
index 00000000..23b304ac
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/DescriptorPool.php
@@ -0,0 +1,162 @@
+<?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;
+
+use Google\Protobuf\Internal\Descriptor;
+use Google\Protobuf\Internal\FileDescriptor;
+use Google\Protobuf\Internal\FileDescriptorSet;
+use Google\Protobuf\Internal\MessageBuilderContext;
+use Google\Protobuf\Internal\EnumBuilderContext;
+
+class DescriptorPool
+{
+ private static $pool;
+ // Map from message names to sub-maps, which are maps from field numbers to
+ // field descriptors.
+ private $class_to_desc = [];
+ private $class_to_enum_desc = [];
+ private $proto_to_class = [];
+
+ public static function getGeneratedPool()
+ {
+ if (!isset(self::$pool)) {
+ self::$pool = new DescriptorPool();
+ }
+ return self::$pool;
+ }
+
+ public function internalAddGeneratedFile($data)
+ {
+ $files = new FileDescriptorSet();
+ $files->decode($data);
+ $file = FileDescriptor::buildFromProto($files->getFile()[0]);
+
+ foreach ($file->getMessageType() as &$desc) {
+ $this->addDescriptor($desc);
+ }
+ unset($desc);
+
+ foreach ($file->getEnumType() as &$desc) {
+ $this->addEnumDescriptor($desc);
+ }
+ unset($desc);
+
+ foreach ($file->getMessageType() as &$desc) {
+ $this->crossLink($desc);
+ }
+ unset($desc);
+ }
+
+ public function addMessage($name, $klass)
+ {
+ return new MessageBuilderContext($name, $klass, $this);
+ }
+
+ public function addEnum($name, $klass)
+ {
+ return new EnumBuilderContext($name, $klass, $this);
+ }
+
+ public function addDescriptor($descriptor)
+ {
+ $this->proto_to_class[$descriptor->getFullName()] =
+ $descriptor->getClass();
+ $this->class_to_desc[$descriptor->getClass()] = $descriptor;
+ foreach ($descriptor->getNestedType() as $nested_type) {
+ $this->addDescriptor($nested_type);
+ }
+ }
+
+ public function addEnumDescriptor($descriptor)
+ {
+ $this->proto_to_class[$descriptor->getFullName()] =
+ $descriptor->getClass();
+ $this->class_to_enum_desc[$descriptor->getClass()] = $descriptor;
+ }
+
+ public function getDescriptorByClassName($klass)
+ {
+ return $this->class_to_desc[$klass];
+ }
+
+ public function getEnumDescriptorByClassName($klass)
+ {
+ return $this->class_to_enum_desc[$klass];
+ }
+
+ public function getDescriptorByProtoName($proto)
+ {
+ $klass = $this->proto_to_class[$proto];
+ return $this->class_to_desc[$klass];
+ }
+
+ public function getEnumDescriptorByProtoName($proto)
+ {
+ $klass = $this->proto_to_class[$proto];
+ return $this->class_to_enum_desc[$klass];
+ }
+
+ private function crossLink(&$desc)
+ {
+ foreach ($desc->getField() as &$field) {
+ switch ($field->getType()) {
+ case GPBType::MESSAGE:
+ $proto = $field->getMessageType();
+ $field->setMessageType(
+ $this->getDescriptorByProtoName($proto));
+ break;
+ case GPBType::ENUM:
+ $proto = $field->getEnumType();
+ $field->setEnumType(
+ $this->getEnumDescriptorByProtoName($proto));
+ break;
+ default:
+ break;
+ }
+ }
+ unset($field);
+
+ foreach ($desc->getNestedType() as &$nested_type) {
+ $this->crossLink($nested_type);
+ }
+ unset($nested_type);
+ }
+
+ public function finish()
+ {
+ foreach ($this->class_to_desc as $klass => &$desc) {
+ $this->crossLink($desc);
+ }
+ unset($desc);
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/EnumBuilderContext.php b/php/src/Google/Protobuf/Internal/EnumBuilderContext.php
new file mode 100644
index 00000000..c1dac24d
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/EnumBuilderContext.php
@@ -0,0 +1,63 @@
+<?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;
+
+use Google\Protobuf\Internal\EnumDescriptor;
+use Google\Protobuf\Internal\EnumValueDescriptor;
+
+class EnumBuilderContext
+{
+
+ private $descriptor;
+ private $pool;
+
+ public function __construct($full_name, $klass, $pool)
+ {
+ $this->descriptor = new EnumDescriptor();
+ $this->descriptor->setFullName($full_name);
+ $this->descriptor->setClass($klass);
+ $this->pool = $pool;
+ }
+
+ public function value($name, $number)
+ {
+ $value = new EnumValueDescriptor();
+ $this->descriptor->addValue($number, $value);
+ return $this;
+ }
+
+ public function finalizeToPool()
+ {
+ $this->pool->addEnumDescriptor($this->descriptor);
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/GPBLabel.php b/php/src/Google/Protobuf/Internal/GPBLabel.php
new file mode 100644
index 00000000..0fb23841
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/GPBLabel.php
@@ -0,0 +1,40 @@
+<?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 GPBLabel
+{
+ const OPTIONAL = 1;
+ const REQUIRED = 2;
+ const REPEATED = 3;
+}
diff --git a/php/src/Google/Protobuf/Internal/GPBType.php b/php/src/Google/Protobuf/Internal/GPBType.php
new file mode 100644
index 00000000..fa849ceb
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/GPBType.php
@@ -0,0 +1,55 @@
+<?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 GPBType
+{
+ const DOUBLE = 1;
+ const FLOAT = 2;
+ const INT64 = 3;
+ const UINT64 = 4;
+ const INT32 = 5;
+ const FIXED64 = 6;
+ const FIXED32 = 7;
+ const BOOL = 8;
+ const STRING = 9;
+ const GROUP = 10;
+ const MESSAGE = 11;
+ const BYTES = 12;
+ const UINT32 = 13;
+ const ENUM = 14;
+ const SFIXED32 = 15;
+ const SFIXED64 = 16;
+ const SINT32 = 17;
+ const SINT64 = 18;
+}
diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php
new file mode 100644
index 00000000..417a9729
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/GPBUtil.php
@@ -0,0 +1,161 @@
+<?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;
+
+use Google\Protobuf\Internal\GPBType;
+use Google\Protobuf\Internal\RepeatedField;
+
+class GPBUtil
+{
+
+ public static function checkString(&$var, $check_utf8)
+ {
+ if (is_array($var) || is_object($var)) {
+ trigger_error("Expect string.", E_USER_ERROR);
+ return;
+ }
+ if (!is_string($var)) {
+ $var = strval($var);
+ }
+ if ($check_utf8 && !preg_match('//u', $var)) {
+ trigger_error("Expect utf-8 encoding.", E_USER_ERROR);
+ return;
+ }
+ }
+
+ public static function checkEnum(&$var)
+ {
+ static::checkInt32($var);
+ }
+
+ public static function checkInt32(&$var)
+ {
+ if (is_numeric($var)) {
+ $var = intval($var);
+ } else {
+ trigger_error("Expect integer.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkUint32(&$var)
+ {
+ if (is_numeric($var)) {
+ $var = intval($var);
+ if (PHP_INT_SIZE === 8) {
+ $var |= ((-(($var >> 31) & 0x1)) & ~0xFFFFFFFF);
+ }
+ } else {
+ trigger_error("Expect integer.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkInt64(&$var)
+ {
+ if (is_numeric($var)) {
+ $var = intval($var);
+ } else {
+ trigger_error("Expect integer.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkUint64(&$var)
+ {
+ if (is_numeric($var)) {
+ $var = intval($var);
+ } else {
+ trigger_error("Expect integer.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkFloat(&$var)
+ {
+ if (is_float($var) || is_numeric($var)) {
+ $var = floatval($var);
+ } else {
+ trigger_error("Expect float.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkDouble(&$var)
+ {
+ if (is_float($var) || is_numeric($var)) {
+ $var = floatval($var);
+ } else {
+ trigger_error("Expect float.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkBool(&$var)
+ {
+ if (is_array($var) || is_object($var)) {
+ trigger_error("Expect boolean.", E_USER_ERROR);
+ return;
+ }
+ $var = boolval($var);
+ }
+
+ public static function checkMessage(&$var, $klass)
+ {
+ if (!$var instanceof $klass && !is_null($var)) {
+ trigger_error("Expect message.", E_USER_ERROR);
+ }
+ }
+
+ public static function checkRepeatedField(&$var, $type, $klass = null)
+ {
+ if (!$var instanceof RepeatedField) {
+ trigger_error("Expect repeated field.", E_USER_ERROR);
+ }
+ if ($var->getType() != $type) {
+ trigger_error(
+ "Expect repeated field of different type.",
+ E_USER_ERROR);
+ }
+ if ($var->getType() === GPBType::MESSAGE &&
+ $var->getClass() !== $klass) {
+ trigger_error(
+ "Expect repeated field of different message.",
+ E_USER_ERROR);
+ }
+ }
+
+ public static function Int64($value)
+ {
+ return new Int64($value);
+ }
+
+ public static function Uint64($value)
+ {
+ return new Uint64($value);
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/GPBWire.php b/php/src/Google/Protobuf/Internal/GPBWire.php
new file mode 100644
index 00000000..0e741e15
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/GPBWire.php
@@ -0,0 +1,583 @@
+<?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;
+
+use Google\Protobuf\Internal\GPBUtil;
+use Google\Protobuf\Internal\Int64;
+use Google\Protobuf\Internal\Uint64;
+
+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)
+ {
+ // Fill high 32 bits.
+ if (PHP_INT_SIZE === 8) {
+ $int32 |= ((($int32 << 32) >> 31) & (0xFFFFFFFF << 32));
+ }
+
+ $uint32 = ($int32 << 1) ^ ($int32 >> 31);
+
+ // Fill high 32 bits.
+ if (PHP_INT_SIZE === 8) {
+ $uint32 |= ((($uint32 << 32) >> 31) & (0xFFFFFFFF << 32));
+ }
+
+ return $uint32;
+ }
+
+ 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)
+ {
+ $a = $int64->copy()->leftShift(1);
+ $b = $int64->copy()->rightShift(63);
+ $result = $a->bitXor($b);
+ $uint64 = Uint64::newValue($result->high, $result->low);
+ return $uint64;
+ }
+
+ public static function zigZagDecode64($uint64)
+ {
+ $a = $uint64->copy()->rightShift(1);
+ $b = $uint64->oddMask();
+ $result = $a->bitXor($b);
+ $int64 = Int64::newValue($result->high, $result->low);
+ return $int64;
+ }
+
+ public static function readInt32(&$input, &$value)
+ {
+ return $input->readVarint32($value);
+ }
+
+ public static function readInt64(&$input, &$value)
+ {
+ return $input->readVarint64($value);
+ }
+
+ 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)
+ {
+ if (!self::readFixed64($input, $value)) {
+ return false;
+ }
+ $value = Int64::newValue($value->high, $value->low);
+ return true;
+ }
+
+ 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->high === 0 && $value->low === 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);
+ }
+
+ public static function writeInt64(&$output, $value)
+ {
+ return $output->writeVarint64($value);
+ }
+
+ public static function writeUint32(&$output, $value)
+ {
+ return $output->writeVarint32($value);
+ }
+
+ public static function writeUint64(&$output, $value)
+ {
+ return $output->writeVarint64($value);
+ }
+
+ public static function writeSint32(&$output, $value)
+ {
+ $value = GPBWire::zigZagEncode32($value);
+ return $output->writeVarint64($value);
+ }
+
+ public static function writeSint64(&$output, $value)
+ {
+ $value = GPBWire::zigZagEncode64(GPBUtil::Int64($value));
+ return $output->writeVarint64($value->toInteger());
+ }
+
+ 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);
+ } else {
+ return $output->writeVarint32(0);
+ }
+ }
+
+ 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)) {
+ return false;
+ }
+ return $output->writeRaw($value, $size);
+ }
+
+ public static function writeMessage(&$output, $value)
+ {
+ $size = $value->byteSize();
+ if (!$output->writeVarint32($size)) {
+ 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)
+ {
+ if ($value < 0) {
+ 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 = GPBUtil::Int64($value);
+ $value = self::zigZagEncode64($value);
+ return self::varint64Size($value->toInteger());
+ }
+
+ public static function varint64Size($value)
+ {
+ 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 (!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;
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/InputStream.php b/php/src/Google/Protobuf/Internal/InputStream.php
new file mode 100644
index 00000000..18d07075
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/InputStream.php
@@ -0,0 +1,323 @@
+<?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;
+
+use Google\Protobuf\Internal\Uint64;
+
+class InputStream
+{
+
+ private $buffer;
+ private $buffer_size_after_limit;
+ private $buffer_end;
+ private $current;
+ private $current_limit;
+ private $legitimate_message_end;
+ private $recursion_budget;
+ private $recursion_limit;
+ private $total_bytes_limit;
+ 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
+
+ public function __construct($buffer)
+ {
+ $start = 0;
+ $end = strlen($buffer);
+ $this->buffer = $buffer;
+ $this->buffer_size_after_limit = 0;
+ $this->buffer_end = $end;
+ $this->current = $start;
+ $this->current_limit = $end;
+ $this->legitimate_message_end = false;
+ $this->recursion_budget = self::DEFAULT_RECURSION_LIMIT;
+ $this->recursion_limit = self::DEFAULT_RECURSION_LIMIT;
+ $this->total_bytes_limit = self::DEFAULT_TOTAL_BYTES_LIMIT;
+ $this->total_bytes_read = $end - $start;
+ }
+
+ private function advance($amount)
+ {
+ $this->current += $amount;
+ }
+
+ private function bufferSize()
+ {
+ return $this->buffer_end - $this->current;
+ }
+
+ private function current()
+ {
+ return $this->total_bytes_read -
+ ($this->buffer_end - $this->current +
+ $this->buffer_size_after_limit);
+ }
+
+ private function recomputeBufferLimits()
+ {
+ $this->buffer_end += $this->buffer_size_after_limit;
+ $closest_limit = min($this->current_limit, $this->total_bytes_limit);
+ if ($closest_limit < $this->total_bytes_read) {
+ // The limit position is in the current buffer. We must adjust the
+ // buffer size accordingly.
+ $this->buffer_size_after_limit = $this->total_bytes_read -
+ $closest_limit;
+ $this->buffer_end -= $this->buffer_size_after_limit;
+ } else {
+ $this->buffer_size_after_limit = 0;
+ }
+ }
+
+ private function consumedEntireMessage()
+ {
+ return $this->legitimate_message_end;
+ }
+
+ /**
+ * Read uint32 into $var. Advance buffer with consumed bytes. If the
+ * contained varint is larger than 32 bits, discard the high order bits.
+ * @param $var.
+ */
+ public function readVarint32(&$var)
+ {
+ if (!$this->readVarint64($var)) {
+ return false;
+ }
+ $var = $var->toInteger() & 0xFFFFFFFF;
+ // Convert large uint32 to int32.
+ if (PHP_INT_SIZE === 8 && ($var > 0x7FFFFFFF)) {
+ $var = $var | (0xFFFFFFFF << 32);
+ }
+ return true;
+ }
+
+ /**
+ * Read Uint64 into $var. Advance buffer with consumed bytes.
+ * @param $var.
+ */
+ public function readVarint64(&$var)
+ {
+ $result = new Uint64(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]);
+ $result->bitOr((new Uint64($b & 0x7F))->leftShift(7 * $count));
+ $this->advance(1);
+ $count += 1;
+ } while ($b & 0x80);
+
+ $var = $result;
+ return true;
+ }
+
+ /**
+ * Read int into $var. If the result is larger than the largest integer, $var
+ * will be -1. Advance buffer with consumed bytes.
+ * @param $var.
+ */
+ public function readVarintSizeAsInt(&$var)
+ {
+ if (!$this->readVarint64($var)) {
+ return false;
+ }
+ $var = $var->toInteger();
+ return true;
+ }
+
+ /**
+ * Read 32-bit unsiged integer to $var. If the buffer has less than 4 bytes,
+ * return false. Advance buffer with consumed bytes.
+ * @param $var.
+ */
+ public function readLittleEndian32(&$var)
+ {
+ $data = null;
+ if (!$this->readRaw(4, $data)) {
+ return false;
+ }
+ $var = unpack('V', $data);
+ $var = $var[1];
+ return true;
+ }
+
+ /**
+ * Read 64-bit unsiged integer to $var. If the buffer has less than 8 bytes,
+ * return false. Advance buffer with consumed bytes.
+ * @param $var.
+ */
+ public function readLittleEndian64(&$var)
+ {
+ $data = null;
+ if (!$this->readRaw(4, $data)) {
+ return false;
+ }
+ $low = unpack('V', $data)[1];
+ if (!$this->readRaw(4, $data)) {
+ return false;
+ }
+ $high = unpack('V', $data)[1];
+ $var = Uint64::newValue($high, $low);
+ return true;
+ }
+
+ /**
+ * Read tag into $var. Advance buffer with consumed bytes.
+ * @param $var.
+ */
+ public function readTag()
+ {
+ if ($this->current === $this->buffer_end) {
+ // Make sure that it failed due to EOF, not because we hit
+ // total_bytes_limit, which, unlike normal limits, is not a valid
+ // place to end a message.
+ $current_position = $this->total_bytes_read -
+ $this->buffer_size_after_limit;
+ if ($current_position >= $this->total_bytes_limit) {
+ // Hit total_bytes_limit_. But if we also hit the normal limit,
+ // we're still OK.
+ $this->legitimate_message_end =
+ ($this->current_limit === $this->total_bytes_limit);
+ } else {
+ $this->legitimate_message_end = true;
+ }
+ return 0;
+ }
+
+ $result = 0;
+ // The larget tag is 2^29 - 1, which can be represented by int32.
+ $success = $this->readVarint32($result);
+ if ($success) {
+ return $result;
+ } else {
+ return 0;
+ }
+ }
+
+ public function readRaw($size, &$buffer)
+ {
+ $current_buffer_size = 0;
+ if ($this->bufferSize() < $size) {
+ return false;
+ }
+
+ $buffer = substr($this->buffer, $this->current, $size);
+ $this->advance($size);
+
+ return true;
+ }
+
+ /* Places a limit on the number of bytes that the stream may read, starting
+ * from the current position. Once the stream hits this limit, it will act
+ * like the end of the input has been reached until popLimit() is called.
+ *
+ * As the names imply, the stream conceptually has a stack of limits. The
+ * shortest limit on the stack is always enforced, even if it is not the top
+ * limit.
+ *
+ * The value returned by pushLimit() is opaque to the caller, and must be
+ * passed unchanged to the corresponding call to popLimit().
+ *
+ * @param integer $byte_limit
+ */
+ public function pushLimit($byte_limit)
+ {
+ // Current position relative to the beginning of the stream.
+ $current_position = $this->current();
+ $old_limit = $this->current_limit;
+
+ // security: byte_limit is possibly evil, so check for negative values
+ // and overflow.
+ if ($byte_limit >= 0 && $byte_limit <= PHP_INT_MAX - $current_position) {
+ $this->current_limit = $current_position + $byte_limit;
+ } else {
+ // Negative or overflow.
+ $this->current_limit = PHP_INT_MAX;
+ }
+
+ // We need to enforce all limits, not just the new one, so if the previous
+ // limit was before the new requested limit, we continue to enforce the
+ // previous limit.
+ $this->current_limit = min($this->current_limit, $old_limit);
+
+ $this->recomputeBufferLimits();
+ return $old_limit;
+ }
+
+ /* The limit passed in is actually the *old* limit, which we returned from
+ * PushLimit().
+ *
+ * @param integer $byte_limit
+ */
+ public function popLimit($byte_limit)
+ {
+ $this->current_limit = $byte_limit;
+ $this->recomputeBufferLimits();
+ // We may no longer be at a legitimate message end. ReadTag() needs to
+ // be called again to find out.
+ $this->legitimate_message_end = false;
+ }
+
+ public function incrementRecursionDepthAndPushLimit(
+ $byte_limit, &$old_limit, &$recursion_budget)
+ {
+ $old_limit = $this->pushLimit($byte_limit);
+ $recursion_limit = --$this->recursion_limit;
+ }
+
+ public function decrementRecursionDepthAndPopLimit($byte_limit)
+ {
+ $result = $this->consumedEntireMessage();
+ $this->popLimit($byte_limit);
+ ++$this->recursion_budget;
+ return $result;
+ }
+
+ public function bytesUntilLimit()
+ {
+ if ($this->current_limit === PHP_INT_MAX) {
+ return -1;
+ }
+ return $this->current_limit - $this->current;
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/MapEntry.php b/php/src/Google/Protobuf/Internal/MapEntry.php
new file mode 100644
index 00000000..926645e1
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/MapEntry.php
@@ -0,0 +1,57 @@
+<?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;
+
+use Google\Protobuf\Internal\Message;
+
+class MapEntry extends Message
+{
+ public $key;
+ public $value;
+
+ public function setKey(&$key) {
+ $this->key = $key;
+ }
+
+ public function getKey() {
+ return $this->key;
+ }
+
+ public function setValue(&$value) {
+ $this->value = $value;
+ }
+
+ public function getValue() {
+ return $this->value;
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php
new file mode 100644
index 00000000..14ee7ebe
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/MapField.php
@@ -0,0 +1,321 @@
+<?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.
+
+/**
+ * MapField and MapFieldIter are used by generated protocol message classes to
+ * manipulate map fields.
+ */
+
+namespace Google\Protobuf\Internal;
+
+/**
+ * MapFieldIter is used to iterate MapField. It is also need for the foreach
+ * syntax.
+ */
+class MapFieldIter implements \Iterator
+{
+
+ /**
+ * @ignore
+ */
+ private $container;
+
+ /**
+ * Create iterator instance for MapField.
+ *
+ * @param MapField The MapField instance for which this iterator is
+ * created.
+ * @ignore
+ */
+ public function __construct($container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * Reset the status of the iterator
+ *
+ * @return void
+ */
+ public function rewind()
+ {
+ return reset($this->container);
+ }
+
+ /**
+ * Return the element at the current position.
+ *
+ * @return object The element at the current position.
+ */
+ public function current()
+ {
+ return current($this->container);
+ }
+
+ /**
+ * Return the current key.
+ *
+ * @return object The current key.
+ */
+ public function key()
+ {
+ return key($this->container);
+ }
+
+ /**
+ * Move to the next position.
+ *
+ * @return void
+ */
+ public function next()
+ {
+ return next($this->container);
+ }
+
+ /**
+ * Check whether there are more elements to iterate.
+ *
+ * @return bool True if there are more elements to iterate.
+ */
+ public function valid()
+ {
+ return key($this->container) !== null;
+ }
+}
+
+/**
+ * @ignore
+ */
+function checkKey($key_type, &$key)
+{
+ switch ($key_type) {
+ case GPBType::INT32:
+ GPBUtil::checkInt32($key);
+ break;
+ case GPBType::UINT32:
+ GPBUtil::checkUint32($key);
+ break;
+ case GPBType::INT64:
+ GPBUtil::checkInt64($key);
+ break;
+ case GPBType::UINT64:
+ GPBUtil::checkUint64($key);
+ break;
+ case GPBType::FIXED64:
+ GPBUtil::checkUint64($key);
+ break;
+ case GPBType::FIXED32:
+ GPBUtil::checkUint32($key);
+ break;
+ case GPBType::SFIXED64:
+ GPBUtil::checkInt64($key);
+ break;
+ case GPBType::SFIXED32:
+ GPBUtil::checkInt32($key);
+ break;
+ case GPBType::SINT64:
+ GPBUtil::checkInt64($key);
+ break;
+ case GPBType::SINT32:
+ GPBUtil::checkInt32($key);
+ break;
+ case GPBType::BOOL:
+ GPBUtil::checkBool($key);
+ break;
+ case GPBType::STRING:
+ GPBUtil::checkString($key, true);
+ break;
+ default:
+ var_dump($key_type);
+ trigger_error(
+ "Given type cannot be map key.",
+ E_USER_ERROR);
+ break;
+ }
+}
+
+/**
+ * MapField is used by generated protocol message classes to manipulate map
+ * fields. It can be used like native PHP array.
+ */
+class MapField implements \ArrayAccess, \IteratorAggregate, \Countable
+{
+ /**
+ * @ignore
+ */
+ private $container;
+ /**
+ * @ignore
+ */
+ private $key_type;
+ /**
+ * @ignore
+ */
+ private $value_type;
+ /**
+ * @ignore
+ */
+ private $value_klass;
+
+ /**
+ * Constructs an instance of MapField.
+ *
+ * @param long $key_type Type of the stored key element.
+ * @param long $value_type Type of the stored value element.
+ * @param string $klass Message/Enum class name of value instance
+ * (message/enum fields only).
+ * @ignore
+ */
+ public function __construct($key_type, $value_type, $klass = null)
+ {
+ $this->container = [];
+ $this->key_type = $key_type;
+ $this->value_type = $value_type;
+ $this->klass = $klass;
+ }
+
+ /**
+ * Return the element at the given key.
+ *
+ * This will also be called for: $ele = $arr[$key]
+ *
+ * @param object $key The key of the element to be fetched.
+ * @return object The stored element at given key.
+ * @throws ErrorException Invalid type for index.
+ * @throws ErrorException Non-existing index.
+ */
+ public function offsetGet($key)
+ {
+ return $this->container[$key];
+ }
+
+ /**
+ * Assign the element at the given key.
+ *
+ * This will also be called for: $arr[$key] = $value
+ *
+ * @param object $key The key of the element to be fetched.
+ * @param object $value The element to be assigned.
+ * @return void
+ * @throws ErrorException Invalid type for key.
+ * @throws ErrorException Invalid type for value.
+ * @throws ErrorException Non-existing key.
+ */
+ public function offsetSet($key, $value)
+ {
+ checkKey($this->key_type, $key);
+
+ switch ($this->value_type) {
+ case GPBType::INT32:
+ GPBUtil::checkInt32($value);
+ break;
+ case GPBType::UINT32:
+ GPBUtil::checkUint32($value);
+ break;
+ case GPBType::INT64:
+ GPBUtil::checkInt64($value);
+ break;
+ case GPBType::UINT64:
+ GPBUtil::checkUint64($value);
+ break;
+ case GPBType::FLOAT:
+ GPBUtil::checkFloat($value);
+ break;
+ case GPBType::DOUBLE:
+ GPBUtil::checkDouble($value);
+ break;
+ case GPBType::BOOL:
+ GPBUtil::checkBool($value);
+ break;
+ case GPBType::STRING:
+ GPBUtil::checkString($value, true);
+ break;
+ case GPBType::MESSAGE:
+ GPBUtil::checkMessage($value, $this->klass);
+ break;
+ default:
+ break;
+ }
+
+ $this->container[$key] = $value;
+ }
+
+ /**
+ * Remove the element at the given key.
+ *
+ * This will also be called for: unset($arr)
+ *
+ * @param object $key The key of the element to be removed.
+ * @return void
+ * @throws ErrorException Invalid type for key.
+ */
+ public function offsetUnset($key)
+ {
+ checkKey($this->key_type, $key);
+ unset($this->container[$key]);
+ }
+
+ /**
+ * Check the existence of the element at the given key.
+ *
+ * This will also be called for: isset($arr)
+ *
+ * @param object $key The key of the element to be removed.
+ * @return bool True if the element at the given key exists.
+ * @throws ErrorException Invalid type for key.
+ */
+ public function offsetExists($key)
+ {
+ checkKey($this->key_type, $key);
+ return isset($this->container[$key]);
+ }
+
+ /**
+ * @ignore
+ */
+ public function getIterator()
+ {
+ return new MapFieldIter($this->container);
+ }
+
+ /**
+ * Return the number of stored elements.
+ *
+ * This will also be called for: count($arr)
+ *
+ * @return integer The number of stored elements.
+ */
+ public function count()
+ {
+ return count($this->container);
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php
new file mode 100644
index 00000000..a8de6a11
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/Message.php
@@ -0,0 +1,671 @@
+<?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.
+
+/**
+ * Defines Message, the parent class extended by all protocol message classes.
+ */
+
+namespace Google\Protobuf\Internal;
+
+use Google\Protobuf\Internal\InputStream;
+use Google\Protobuf\Internal\OutputStream;
+use Google\Protobuf\Internal\DescriptorPool;
+use Google\Protobuf\Internal\GPBLabel;
+use Google\Protobuf\Internal\GPBType;
+use Google\Protobuf\Internal\GPBWire;
+use Google\Protobuf\Internal\MapEntry;
+use Google\Protobuf\Internal\RepeatedField;
+
+/**
+ * Parent class of all proto messages. Users should not instantiate this class
+ * or extend this class or its child classes by their own. See the comment of
+ * specific functions for more details.
+ */
+class Message
+{
+
+ /**
+ * @ignore
+ */
+ private $desc;
+
+ /**
+ * @ignore
+ */
+ public function __construct($desc = NULL)
+ {
+ // MapEntry message is shared by all types of map fields, whose
+ // descriptors are different from each other. Thus, we cannot find a
+ // specific descriptor from the descriptor pool.
+ if (get_class($this) === 'Google\Protobuf\Internal\MapEntry') {
+ $this->desc = $desc;
+ return;
+ }
+ $pool = DescriptorPool::getGeneratedPool();
+ $this->desc = $pool->getDescriptorByClassName(get_class($this));
+ foreach ($this->desc->getField() as $field) {
+ $setter = $field->getSetter();
+ if ($field->isMap()) {
+ $message_type = $field->getMessageType();
+ $key_field = $message_type->getFieldByNumber(1);
+ $value_field = $message_type->getFieldByNumber(2);
+ switch ($value_field->getType()) {
+ case GPBType::MESSAGE:
+ case GPBType::GROUP:
+ $this->$setter(
+ new MapField(
+ $key_field->getType(),
+ $value_field->getType(),
+ $value_field->getMessageType()->getClass()));
+ break;
+ case GPBType::ENUM:
+ $this->$setter(
+ new MapField(
+ $key_field->getType(),
+ $value_field->getType(),
+ $value_field->getEnumType()->getClass()));
+ break;
+ default:
+ $this->$setter(new MapField($key_field->getType(),
+ $value_field->getType()));
+ break;
+ }
+ } else if ($field->getLabel() === GPBLabel::REPEATED) {
+ switch ($field->getType()) {
+ case GPBType::MESSAGE:
+ case GPBType::GROUP:
+ $this->$setter(
+ new RepeatedField(
+ $field->getType(),
+ $field->getMessageType()->getClass()));
+ break;
+ case GPBType::ENUM:
+ $this->$setter(
+ new RepeatedField(
+ $field->getType(),
+ $field->getEnumType()->getClass()));
+ break;
+ default:
+ $this->$setter(new RepeatedField($field->getType()));
+ break;
+ }
+ } else if ($field->getOneofIndex() !== -1) {
+ $oneof = $this->desc->getOneofDecl()[$field->getOneofIndex()];
+ $oneof_name = $oneof->getName();
+ $this->$oneof_name = new OneofField($oneof);
+ }
+ }
+ }
+
+ protected function readOneof($number)
+ {
+ $field = $this->desc->getFieldByNumber($number);
+ $oneof = $this->desc->getOneofDecl()[$field->getOneofIndex()];
+ $oneof_name = $oneof->getName();
+ $oneof_field = $this->$oneof_name;
+ if ($number === $oneof_field->getNumber()) {
+ return $oneof_field->getValue();
+ } else {
+ return $this->defaultValue($field);
+ }
+ }
+
+ protected function writeOneof($number, $value)
+ {
+ $field = $this->desc->getFieldByNumber($number);
+ $oneof = $this->desc->getOneofDecl()[$field->getOneofIndex()];
+ $oneof_name = $oneof->getName();
+ $oneof_field = $this->$oneof_name;
+ $oneof_field->setValue($value);
+ $oneof_field->setFieldName($field->getName());
+ $oneof_field->setNumber($number);
+ }
+
+ /**
+ * @ignore
+ */
+ private function defaultValue($field)
+ {
+ $value = null;
+
+ switch ($field->getType()) {
+ case GPBType::DOUBLE:
+ case GPBType::FLOAT:
+ return 0.0;
+ case GPBType::UINT32:
+ case GPBType::UINT64:
+ case GPBType::INT32:
+ case GPBType::INT64:
+ case GPBType::FIXED32:
+ case GPBType::FIXED64:
+ case GPBType::SFIXED32:
+ case GPBType::SFIXED64:
+ case GPBType::SINT32:
+ case GPBType::SINT64:
+ case GPBType::ENUM:
+ return 0;
+ case GPBType::BOOL:
+ return false;
+ case GPBType::STRING:
+ case GPBType::BYTES:
+ return "";
+ case GPBType::GROUP:
+ case GPBType::MESSAGE:
+ return null;
+ default:
+ user_error("Unsupported type.");
+ return false;
+ }
+ }
+
+ /**
+ * @ignore
+ */
+ private static function parseFieldFromStreamNoTag($input, $field, &$value)
+ {
+ switch ($field->getType()) {
+ case GPBType::DOUBLE:
+ if (!GPBWire::readDouble($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::FLOAT:
+ if (!GPBWire::readFloat($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::INT64:
+ if (!GPBWire::readInt64($input, $value)) {
+ return false;
+ }
+ $value = $value->toInteger();
+ break;
+ case GPBType::UINT64:
+ if (!GPBWire::readUint64($input, $value)) {
+ return false;
+ }
+ $value = $value->toInteger();
+ break;
+ case GPBType::INT32:
+ if (!GPBWire::readInt32($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::FIXED64:
+ if (!GPBWire::readFixed64($input, $value)) {
+ return false;
+ }
+ $value = $value->toInteger();
+ break;
+ case GPBType::FIXED32:
+ if (!GPBWire::readFixed32($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::BOOL:
+ if (!GPBWire::readBool($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::STRING:
+ // TODO(teboring): Add utf-8 check.
+ if (!GPBWire::readString($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::GROUP:
+ echo "GROUP\xA";
+ trigger_error("Not implemented.", E_ERROR);
+ break;
+ case GPBType::MESSAGE:
+ if ($field->isMap()) {
+ $value = new MapEntry($field->getMessageType());
+ } else {
+ $klass = $field->getMessageType()->getClass();
+ $value = new $klass;
+ }
+ if (!GPBWire::readMessage($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::BYTES:
+ if (!GPBWire::readString($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::UINT32:
+ if (!GPBWire::readUint32($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::ENUM:
+ // TODO(teboring): Check unknown enum value.
+ if (!GPBWire::readInt32($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::SFIXED32:
+ if (!GPBWire::readSfixed32($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::SFIXED64:
+ if (!GPBWire::readSfixed64($input, $value)) {
+ return false;
+ }
+ $value = $value->toInteger();
+ break;
+ case GPBType::SINT32:
+ if (!GPBWire::readSint32($input, $value)) {
+ return false;
+ }
+ break;
+ case GPBType::SINT64:
+ if (!GPBWire::readSint64($input, $value)) {
+ return false;
+ }
+ $value = $value->toInteger();
+ break;
+ default:
+ user_error("Unsupported type.");
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @ignore
+ */
+ private function parseFieldFromStream($tag, $input, $field)
+ {
+ $value = null;
+ $field_type = $field->getType();
+
+ $value_format = GPBWire::UNKNOWN;
+ if (GPBWire::getTagWireType($tag) ===
+ GPBWire::getWireType($field_type)) {
+ $value_format = GPBWire::NORMAL_FORMAT;
+ } elseif ($field->isPackable() &&
+ GPBWire::getTagWireType($tag) ===
+ GPBWire::WIRETYPE_LENGTH_DELIMITED) {
+ $value_format = GPBWire::PACKED_FORMAT;
+ }
+
+ if ($value_format === GPBWire::NORMAL_FORMAT) {
+ if (!self::parseFieldFromStreamNoTag($input, $field, $value)) {
+ return false;
+ }
+ } elseif ($value_format === GPBWire::PACKED_FORMAT) {
+ $length = 0;
+ if (!GPBWire::readInt32($input, $length)) {
+ return false;
+ }
+ $limit = $input->pushLimit($length);
+ $getter = $field->getGetter();
+ while ($input->bytesUntilLimit() > 0) {
+ if (!self::parseFieldFromStreamNoTag($input, $field, $value)) {
+ return false;
+ }
+ $this->$getter()[] = $value;
+ }
+ $input->popLimit($limit);
+ return true;
+ } else {
+ return false;
+ }
+
+ if ($field->isMap()) {
+ $getter = $field->getGetter();
+ $this->$getter()[$value->getKey()] = $value->getValue();
+ } else if ($field->isRepeated()) {
+ $getter = $field->getGetter();
+ $this->$getter()[] = $value;
+ } else {
+ $setter = $field->getSetter();
+ $this->$setter($value);
+ }
+
+ return true;
+ }
+
+ /**
+ * Parses a protocol buffer contained in a string.
+ *
+ * This function takes a string in the (non-human-readable) binary wire
+ * format, matching the encoding output by encode().
+ *
+ * @param string $data Binary protobuf data.
+ * @return bool Return true on success.
+ */
+ public function decode($data)
+ {
+ $input = new InputStream($data);
+ $this->parseFromStream($input);
+ }
+
+ /**
+ * @ignore
+ */
+ public function parseFromStream($input)
+ {
+ while (true) {
+ $tag = $input->readTag();
+ // End of input. This is a valid place to end, so return true.
+ if ($tag === 0) {
+ return true;
+ }
+
+ $number = GPBWire::getTagFieldNumber($tag);
+ $field = $this->desc->getFieldByNumber($number);
+
+ if (!$this->parseFieldFromStream($tag, $input, $field)) {
+ return false;
+ }
+ }
+ }
+
+ /**
+ * @ignore
+ */
+ private function serializeSingularFieldToStream($field, &$output)
+ {
+ if (!$this->existField($field)) {
+ return true;
+ }
+ $getter = $field->getGetter();
+ $value = $this->$getter();
+ if (!GPBWire::serializeFieldToStream($value, $field, true, $output)) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @ignore
+ */
+ private function serializeRepeatedFieldToStream($field, &$output)
+ {
+ $getter = $field->getGetter();
+ $values = $this->$getter();
+ $count = count($values);
+ if ($count === 0) {
+ return true;
+ }
+
+ $packed = $field->getPacked();
+ if ($packed) {
+ if (!GPBWire::writeTag(
+ $output,
+ GPBWire::makeTag($field->getNumber(), GPBType::STRING))) {
+ return false;
+ }
+ $size = 0;
+ foreach ($values as $value) {
+ $size += $this->fieldDataOnlyByteSize($field, $value);
+ }
+ if (!$output->writeVarint32($size)) {
+ return false;
+ }
+ }
+
+ foreach ($values as $value) {
+ if (!GPBWire::serializeFieldToStream(
+ $value,
+ $field,
+ !$packed,
+ $output)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * @ignore
+ */
+ private function serializeMapFieldToStream($field, $output)
+ {
+ $getter = $field->getGetter();
+ $values = $this->$getter();
+ $count = count($values);
+ if ($count === 0) {
+ return true;
+ }
+
+ foreach ($values as $key => $value) {
+ $map_entry = new MapEntry($field->getMessageType());
+ $map_entry->setKey($key);
+ $map_entry->setValue($value);
+ if (!GPBWire::serializeFieldToStream(
+ $map_entry,
+ $field,
+ true,
+ $output)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * @ignore
+ */
+ private function serializeFieldToStream(&$output, $field)
+ {
+ if ($field->isMap()) {
+ return $this->serializeMapFieldToStream($field, $output);
+ } elseif ($field->isRepeated()) {
+ return $this->serializeRepeatedFieldToStream($field, $output);
+ } else {
+ return $this->serializeSingularFieldToStream($field, $output);
+ }
+ }
+
+ /**
+ * @ignore
+ */
+ public function serializeToStream(&$output)
+ {
+ $fields = $this->desc->getField();
+ foreach ($fields as $field) {
+ if (!$this->serializeFieldToStream($output, $field)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Serialize the message to string.
+ * @return string Serialized binary protobuf data.
+ */
+ public function encode()
+ {
+ $output = new OutputStream($this->byteSize());
+ $this->serializeToStream($output);
+ return $output->getData();
+ }
+
+ /**
+ * @ignore
+ */
+ private function existField($field)
+ {
+ $getter = $field->getGetter();
+ $value = $this->$getter();
+ return $value !== $this->defaultValue($field);
+ }
+
+ /**
+ * @ignore
+ */
+ private function repeatedFieldDataOnlyByteSize($field)
+ {
+ $size = 0;
+
+ $getter = $field->getGetter();
+ $values = $this->$getter();
+ $count = count($values);
+ if ($count !== 0) {
+ $size += $count * GPBWire::tagSize($field);
+ foreach ($values as $value) {
+ $size += $this->singularFieldDataOnlyByteSize($field);
+ }
+ }
+ }
+
+ /**
+ * @ignore
+ */
+ private function fieldDataOnlyByteSize($field, $value)
+ {
+ $size = 0;
+
+ switch ($field->getType()) {
+ case GPBType::BOOL:
+ $size += 1;
+ break;
+ case GPBType::FLOAT:
+ case GPBType::FIXED32:
+ case GPBType::SFIXED32:
+ $size += 4;
+ break;
+ case GPBType::DOUBLE:
+ case GPBType::FIXED64:
+ case GPBType::SFIXED64:
+ $size += 8;
+ break;
+ case GPBType::UINT32:
+ case GPBType::INT32:
+ case GPBType::ENUM:
+ $size += GPBWire::varint32Size($value);
+ break;
+ case GPBType::UINT64:
+ case GPBType::INT64:
+ $size += GPBWire::varint64Size($value);
+ break;
+ case GPBType::SINT32:
+ $size += GPBWire::sint32Size($value);
+ break;
+ case GPBType::SINT64:
+ $size += GPBWire::sint64Size($value);
+ break;
+ case GPBType::STRING:
+ case GPBType::BYTES:
+ $size += strlen($value);
+ $size += GPBWire::varint32Size($size);
+ break;
+ case GPBType::MESSAGE:
+ $size += $value->byteSize();
+ $size += GPBWire::varint32Size($size);
+ break;
+ case GPBType::GROUP:
+ // TODO(teboring): Add support.
+ user_error("Unsupported type.");
+ break;
+ default:
+ user_error("Unsupported type.");
+ return 0;
+ }
+
+ return $size;
+ }
+
+ /**
+ * @ignore
+ */
+ private function fieldByteSize($field)
+ {
+ $size = 0;
+ if ($field->isMap()) {
+ $getter = $field->getGetter();
+ $values = $this->$getter();
+ $count = count($values);
+ if ($count !== 0) {
+ $size += $count * GPBWire::tagSize($field);
+ $message_type = $field->getMessageType();
+ $key_field = $message_type->getFieldByNumber(1);
+ $value_field = $message_type->getFieldByNumber(2);
+ foreach ($values as $key => $value) {
+ $data_size = 0;
+ $data_size += $this->fieldDataOnlyByteSize($key_field, $key);
+ $data_size += $this->fieldDataOnlyByteSize(
+ $value_field,
+ $value);
+ $data_size += GPBWire::tagSize($key_field);
+ $data_size += GPBWire::tagSize($value_field);
+ $size += GPBWire::varint32Size($data_size) + $data_size;
+ }
+ }
+ } elseif ($field->isRepeated()) {
+ $getter = $field->getGetter();
+ $values = $this->$getter();
+ $count = count($values);
+ if ($count !== 0) {
+ if ($field->getPacked()) {
+ $data_size = 0;
+ foreach ($values as $value) {
+ $data_size += $this->fieldDataOnlyByteSize($field, $value);
+ }
+ $size += GPBWire::tagSize($field);
+ $size += GPBWire::varint32Size($data_size);
+ $size += $data_size;
+ } else {
+ $size += $count * GPBWire::tagSize($field);
+ foreach ($values as $value) {
+ $size += $this->fieldDataOnlyByteSize($field, $value);
+ }
+ }
+ }
+ } elseif ($this->existField($field)) {
+ $size += GPBWire::tagSize($field);
+ $getter = $field->getGetter();
+ $value = $this->$getter();
+ $size += $this->fieldDataOnlyByteSize($field, $value);
+ }
+ return $size;
+ }
+
+ /**
+ * @ignore
+ */
+ public function byteSize()
+ {
+ $size = 0;
+
+ $fields = $this->desc->getField();
+ foreach ($fields as $field) {
+ $size += $this->fieldByteSize($field);
+ }
+ return $size;
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/MessageBuilderContext.php b/php/src/Google/Protobuf/Internal/MessageBuilderContext.php
new file mode 100644
index 00000000..2724d267
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/MessageBuilderContext.php
@@ -0,0 +1,120 @@
+<?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;
+
+use Google\Protobuf\Internal\GPBLabel;
+use Google\Protobuf\Internal\GPBType;
+use Google\Protobuf\Internal\Descriptor;
+use Google\Protobuf\Internal\FieldDescriptor;
+
+class MessageBuilderContext
+{
+
+ private $descriptor;
+ private $pool;
+
+ public function __construct($full_name, $klass, $pool)
+ {
+ $this->descriptor = new Descriptor();
+ $this->descriptor->setFullName($full_name);
+ $this->descriptor->setClass($klass);
+ $this->pool = $pool;
+ }
+
+ private function getFieldDescriptor($name, $label, $type,
+ $number, $type_name = null)
+ {
+ $field = new FieldDescriptor();
+ $field->setName($name);
+ $camel_name = implode('', array_map('ucwords', explode('_', $name)));
+ $field->setGetter('get' . $camel_name);
+ $field->setSetter('set' . $camel_name);
+ $field->setType($type);
+ $field->setNumber($number);
+ $field->setLabel($label);
+
+ // At this time, the message/enum type may have not been added to pool.
+ // So we use the type name as place holder and will replace it with the
+ // actual descriptor in cross building.
+ switch ($type) {
+ case GPBType::MESSAGE:
+ $field->setMessageType($type_name);
+ break;
+ case GPBType::ENUM:
+ $field->setEnumType($type_name);
+ break;
+ default:
+ break;
+ }
+
+ return $field;
+ }
+
+ public function optional($name, $type, $number, $type_name = null)
+ {
+ $this->descriptor->addField($this->getFieldDescriptor(
+ $name,
+ GPBLabel::OPTIONAL,
+ $type,
+ $number,
+ $type_name));
+ return $this;
+ }
+
+ public function repeated($name, $type, $number, $type_name = null)
+ {
+ $this->descriptor->addField($this->getFieldDescriptor(
+ $name,
+ GPBLabel::REPEATED,
+ $type,
+ $number,
+ $type_name));
+ return $this;
+ }
+
+ public function required($name, $type, $number, $type_name = null)
+ {
+ $this->descriptor->addField($this->getFieldDescriptor(
+ $name,
+ GPBLabel::REQUIRED,
+ $type,
+ $number,
+ $type_name));
+ return $this;
+ }
+
+ public function finalizeToPool()
+ {
+ $this->pool->addDescriptor($this->descriptor);
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/OneofField.php b/php/src/Google/Protobuf/Internal/OneofField.php
new file mode 100644
index 00000000..2c689e83
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/OneofField.php
@@ -0,0 +1,77 @@
+<?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 OneofField
+{
+
+ private $desc;
+ private $field_name;
+ private $number = 0;
+ private $value;
+
+ public function __construct($desc)
+ {
+ $this->desc = $desc;
+ }
+
+ public function setValue($value)
+ {
+ $this->value = $value;
+ }
+
+ public function getValue()
+ {
+ return $this->value;
+ }
+
+ public function setFieldName($field_name)
+ {
+ $this->field_name = $field_name;
+ }
+
+ public function getFieldName()
+ {
+ return $this->field_name;
+ }
+
+ public function setNumber($number)
+ {
+ $this->number = $number;
+ }
+
+ public function getNumber()
+ {
+ return $this->number;
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/OutputStream.php b/php/src/Google/Protobuf/Internal/OutputStream.php
new file mode 100644
index 00000000..fcc5ce6d
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/OutputStream.php
@@ -0,0 +1,143 @@
+<?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 OutputStream
+{
+
+ private $buffer;
+ private $buffer_size;
+ private $current;
+
+ const MAX_VARINT32_BYTES = 5;
+ const MAX_VARINT64_BYTES = 10;
+
+ public function __construct($size)
+ {
+ $this->current = 0;
+ $this->buffer_size = $size;
+ $this->buffer = str_repeat(chr(0), $this->buffer_size);
+ }
+
+ public function getData()
+ {
+ return $this->buffer;
+ }
+
+ public function writeVarint32($value)
+ {
+ $bytes = str_repeat(chr(0), self::MAX_VARINT32_BYTES);
+ $size = self::writeVarintToArray($value, $bytes, true);
+ return $this->writeRaw($bytes, $size);
+ }
+
+ public function writeVarint64($value)
+ {
+ $bytes = str_repeat(chr(0), self::MAX_VARINT64_BYTES);
+ $size = self::writeVarintToArray($value, $bytes);
+ return $this->writeRaw($bytes, $size);
+ }
+
+ public function writeLittleEndian32($value)
+ {
+ $bytes = str_repeat(chr(0), 4);
+ $size = self::writeLittleEndian32ToArray($value, $bytes);
+ return $this->writeRaw($bytes, $size);
+ }
+
+ public function writeLittleEndian64($value)
+ {
+ $bytes = str_repeat(chr(0), 8);
+ $size = self::writeLittleEndian64ToArray($value, $bytes);
+ return $this->writeRaw($bytes, $size);
+ }
+
+ public function writeTag($tag)
+ {
+ return $this->writeVarint32($tag);
+ }
+
+ public function writeRaw($data, $size)
+ {
+ if ($this->buffer_size < $size) {
+ var_dump($this->buffer_size);
+ var_dump($size);
+ trigger_error("Output stream doesn't have enough buffer.");
+ return false;
+ }
+
+ for ($i = 0; $i < $size; $i++) {
+ $this->buffer[$this->current] = $data[$i];
+ $this->current++;
+ $this->buffer_size--;
+ }
+ return true;
+ }
+
+ private static function writeVarintToArray($value, &$buffer, $trim = false)
+ {
+ $current = 0;
+ if ($trim) {
+ $value &= 0xFFFFFFFF;
+ }
+ while ($value >= 0x80 || $value < 0) {
+ $buffer[$current] = chr($value | 0x80);
+ $value = ($value >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7));
+ $current++;
+ }
+ $buffer[$current] = chr($value);
+ return $current + 1;
+ }
+
+ private static function writeLittleEndian32ToArray($value, &$buffer)
+ {
+ $buffer[0] = chr($value & 0x000000FF);
+ $buffer[1] = chr(($value >> 8) & 0x000000FF);
+ $buffer[2] = chr(($value >> 16) & 0x000000FF);
+ $buffer[3] = chr(($value >> 24) & 0x000000FF);
+ return 4;
+ }
+
+ private static function writeLittleEndian64ToArray($value, &$buffer)
+ {
+ $buffer[0] = chr($value & 0x000000FF);
+ $buffer[1] = chr(($value >> 8) & 0x000000FF);
+ $buffer[2] = chr(($value >> 16) & 0x000000FF);
+ $buffer[3] = chr(($value >> 24) & 0x000000FF);
+ $buffer[4] = chr(($value >> 32) & 0x000000FF);
+ $buffer[5] = chr(($value >> 40) & 0x000000FF);
+ $buffer[6] = chr(($value >> 48) & 0x000000FF);
+ $buffer[7] = chr(($value >> 56) & 0x000000FF);
+ return 8;
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php
new file mode 100644
index 00000000..0dc5d9d2
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/RepeatedField.php
@@ -0,0 +1,303 @@
+<?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.
+
+/**
+ * RepeatedField and RepeatedFieldIter are used by generated protocol message
+ * classes to manipulate repeated fields.
+ */
+
+namespace Google\Protobuf\Internal;
+
+use Google\Protobuf\Internal\GPBType;
+use Google\Protobuf\Internal\GPBUtil;
+
+/**
+ * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the
+ * foreach syntax.
+ */
+class RepeatedFieldIter implements \Iterator
+{
+
+ /**
+ * @ignore
+ */
+ private $position;
+ /**
+ * @ignore
+ */
+ private $container;
+
+ /**
+ * Create iterator instance for RepeatedField.
+ *
+ * @param RepeatedField The RepeatedField instance for which this iterator
+ * is created.
+ * @ignore
+ */
+ public function __construct($container)
+ {
+ $this->position = 0;
+ $this->container = $container;
+ }
+
+ /**
+ * Reset the status of the iterator
+ *
+ * @return void
+ */
+ public function rewind()
+ {
+ $this->position = 0;
+ }
+
+ /**
+ * Return the element at the current position.
+ *
+ * @return object The element at the current position.
+ */
+ public function current()
+ {
+ return $this->container[$this->position];
+ }
+
+ /**
+ * Return the current position.
+ *
+ * @return integer The current position.
+ */
+ public function key()
+ {
+ return $this->position;
+ }
+
+ /**
+ * Move to the next position.
+ *
+ * @return void
+ */
+ public function next()
+ {
+ ++$this->position;
+ }
+
+ /**
+ * Check whether there are more elements to iterate.
+ *
+ * @return bool True if there are more elements to iterate.
+ */
+ public function valid()
+ {
+ return isset($this->container[$this->position]);
+ }
+}
+
+/**
+ * RepeatedField is used by generated protocol message classes to manipulate
+ * repeated fields. It can be used like native PHP array.
+ */
+class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable
+{
+
+ /**
+ * @ignore
+ */
+ private $container;
+ /**
+ * @ignore
+ */
+ private $type;
+ /**
+ * @ignore
+ */
+ private $klass;
+
+ /**
+ * Constructs an instance of RepeatedField.
+ *
+ * @param long $type Type of the stored element.
+ * @param string $klass Message/Enum class name (message/enum fields only).
+ * @ignore
+ */
+ public function __construct($type, $klass = null)
+ {
+ $this->container = [];
+ $this->type = $type;
+ $this->klass = $klass;
+ }
+
+ /**
+ * @ignore
+ */
+ public function getType()
+ {
+ return $this->type;
+ }
+
+ /**
+ * @ignore
+ */
+ public function getClass()
+ {
+ return $this->klass;
+ }
+
+ /**
+ * Return the element at the given index.
+ *
+ * This will also be called for: $ele = $arr[0]
+ *
+ * @param long $offset The index of the element to be fetched.
+ * @return object The stored element at given index.
+ * @throws ErrorException Invalid type for index.
+ * @throws ErrorException Non-existing index.
+ */
+ public function offsetGet($offset)
+ {
+ return $this->container[$offset];
+ }
+
+ /**
+ * Assign the element at the given index.
+ *
+ * This will also be called for: $arr []= $ele and $arr[0] = ele
+ *
+ * @param long $offset The index of the element to be assigned.
+ * @param object $value The element to be assigned.
+ * @return void
+ * @throws ErrorException Invalid type for index.
+ * @throws ErrorException Non-existing index.
+ * @throws ErrorException Incorrect type of the element.
+ */
+ public function offsetSet($offset, $value)
+ {
+ switch ($this->type) {
+ case GPBType::INT32:
+ GPBUtil::checkInt32($value);
+ break;
+ case GPBType::UINT32:
+ GPBUtil::checkUint32($value);
+ break;
+ case GPBType::INT64:
+ GPBUtil::checkInt64($value);
+ break;
+ case GPBType::UINT64:
+ GPBUtil::checkUint64($value);
+ break;
+ case GPBType::FLOAT:
+ GPBUtil::checkFloat($value);
+ break;
+ case GPBType::DOUBLE:
+ GPBUtil::checkDouble($value);
+ break;
+ case GPBType::BOOL:
+ GPBUtil::checkBool($value);
+ break;
+ case GPBType::STRING:
+ GPBUtil::checkString($value, true);
+ break;
+ case GPBType::MESSAGE:
+ GPBUtil::checkMessage($value, $this->klass);
+ break;
+ default:
+ break;
+ }
+ if (is_null($offset)) {
+ $this->container[] = $value;
+ } else {
+ $count = count($this->container);
+ if (!is_numeric($offset) || $offset < 0 || $offset >= $count) {
+ trigger_error(
+ "Cannot modify element at the given index",
+ E_USER_ERROR);
+ return;
+ }
+ $this->container[$offset] = $value;
+ }
+ }
+
+ /**
+ * Remove the element at the given index.
+ *
+ * This will also be called for: unset($arr)
+ *
+ * @param long $offset The index of the element to be removed.
+ * @return void
+ * @throws ErrorException Invalid type for index.
+ * @throws ErrorException The element to be removed is not at the end of the
+ * RepeatedField.
+ */
+ public function offsetUnset($offset)
+ {
+ $count = count($this->container);
+ if (!is_numeric($offset) || $count === 0 || $offset !== $count - 1) {
+ trigger_error(
+ "Cannot remove element at the given index",
+ E_USER_ERROR);
+ return;
+ }
+ array_pop($this->container);
+ }
+
+ /**
+ * Check the existence of the element at the given index.
+ *
+ * This will also be called for: isset($arr)
+ *
+ * @param long $offset The index of the element to be removed.
+ * @return bool True if the element at the given offset exists.
+ * @throws ErrorException Invalid type for index.
+ */
+ public function offsetExists($offset)
+ {
+ return isset($this->container[$offset]);
+ }
+
+ /**
+ * @ignore
+ */
+ public function getIterator()
+ {
+ return new RepeatedFieldIter($this->container);
+ }
+
+ /**
+ * Return the number of stored elements.
+ *
+ * This will also be called for: count($arr)
+ *
+ * @return integer The number of stored elements.
+ */
+ public function count()
+ {
+ return count($this->container);
+ }
+}
diff --git a/php/src/Google/Protobuf/Internal/Type.php b/php/src/Google/Protobuf/Internal/Type.php
new file mode 100644
index 00000000..088f0e0c
--- /dev/null
+++ b/php/src/Google/Protobuf/Internal/Type.php
@@ -0,0 +1,175 @@
+<?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 GPBInteger
+{
+ public $high = 0;
+ public $low = 0;
+
+ public function __construct($value = 0)
+ {
+ $this->low = $value & 0xFFFFFFFF;
+ if (PHP_INT_SIZE === 8) {
+ $this->high = ($value >> 32) & 0xFFFFFFFF;
+ }
+ }
+
+ // Return 0 for unsigned integers and 1 for signed integers.
+ protected function sign()
+ {
+ trigger_error("Not implemented", E_ERROR);
+ }
+
+ public function leftShift($count)
+ {
+ if ($count > 63) {
+ $this->low = 0;
+ $this->high = 0;
+ return;
+ }
+ if ($count > 32) {
+ $this->high = $this->low;
+ $this->low = 0;
+ $count -= 32;
+ }
+ $mask = (1 << $count) - 1;
+ $this->high = (($this->high << $count) & 0xFFFFFFFF) |
+ (($this->low >> (32 - $count)) & $mask);
+ $this->low = ($this->low << $count) & 0xFFFFFFFF;
+
+ $this->high &= 0xFFFFFFFF;
+ $this->low &= 0xFFFFFFFF;
+ return $this;
+ }
+
+ public function rightShift($count)
+ {
+ $sign = (($this->high & 0x80000000) >> 31) & $this->sign();
+ if ($count > 63) {
+ $this->low = -$sign;
+ $this->high = -$sign;
+ return;
+ }
+ if ($count > 32) {
+ $this->low = $this->high;
+ $this->high = -$sign;
+ $count -= 32;
+ }
+ $this->low = (($this->low >> $count) & 0xFFFFFFFF) |
+ (($this->high << (32 - $count)) & 0xFFFFFFFF);
+ $this->high = (($this->high >> $count) | (-$sign << $count));
+
+ $this->high &= 0xFFFFFFFF;
+ $this->low &= 0xFFFFFFFF;
+
+ return $this;
+ }
+
+ public function bitOr($var)
+ {
+ $this->high |= $var->high;
+ $this->low |= $var->low;
+ return $this;
+ }
+
+ public function bitXor($var)
+ {
+ $this->high ^= $var->high;
+ $this->low ^= $var->low;
+ return $this;
+ }
+
+ public function bitAnd($var)
+ {
+ $this->high &= $var->high;
+ $this->low &= $var->low;
+ return $this;
+ }
+
+ // Even: all zero; Odd: all one.
+ public function oddMask()
+ {
+ $low = (-($this->low & 1)) & 0xFFFFFFFF;
+ $high = $low;
+ return UInt64::newValue($high, $low);
+ }
+
+ public function toInteger()
+ {
+ if (PHP_INT_SIZE === 8) {
+ return ($this->high << 32) | $this->low;
+ } else {
+ return $this->low;
+ }
+ }
+
+ public function copy()
+ {
+ return static::newValue($this->high, $this->low);
+ }
+}
+
+class Uint64 extends GPBInteger
+{
+
+ public static function newValue($high, $low)
+ {
+ $uint64 = new Uint64(0);
+ $uint64->high = $high;
+ $uint64->low = $low;
+ return $uint64;
+ }
+
+ protected function sign()
+ {
+ return 0;
+ }
+}
+
+class Int64 extends GPBInteger
+{
+
+ public static function newValue($high, $low)
+ {
+ $int64 = new Int64(0);
+ $int64->high = $high;
+ $int64->low = $low;
+ return $int64;
+ }
+
+ protected function sign()
+ {
+ return 1;
+ }
+}