Thrift now a TLP - INFRA-3116
git-svn-id: https://svn.apache.org/repos/asf/thrift/branches/0.1.x@1028168 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/php/src/protocol/TBinaryProtocol.php b/lib/php/src/protocol/TBinaryProtocol.php
new file mode 100644
index 0000000..31bbbf9
--- /dev/null
+++ b/lib/php/src/protocol/TBinaryProtocol.php
@@ -0,0 +1,431 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * @package thrift.protocol
+ */
+
+include_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
+
+/**
+ * Binary implementation of the Thrift protocol.
+ *
+ */
+class TBinaryProtocol extends TProtocol {
+
+ const VERSION_MASK = 0xffff0000;
+ const VERSION_1 = 0x80010000;
+
+ protected $strictRead_ = false;
+ protected $strictWrite_ = true;
+
+ public function __construct($trans, $strictRead=false, $strictWrite=true) {
+ parent::__construct($trans);
+ $this->strictRead_ = $strictRead;
+ $this->strictWrite_ = $strictWrite;
+ }
+
+ public function writeMessageBegin($name, $type, $seqid) {
+ if ($this->strictWrite_) {
+ $version = self::VERSION_1 | $type;
+ return
+ $this->writeI32($version) +
+ $this->writeString($name) +
+ $this->writeI32($seqid);
+ } else {
+ return
+ $this->writeString($name) +
+ $this->writeByte($type) +
+ $this->writeI32($seqid);
+ }
+ }
+
+ public function writeMessageEnd() {
+ return 0;
+ }
+
+ public function writeStructBegin($name) {
+ return 0;
+ }
+
+ public function writeStructEnd() {
+ return 0;
+ }
+
+ public function writeFieldBegin($fieldName, $fieldType, $fieldId) {
+ return
+ $this->writeByte($fieldType) +
+ $this->writeI16($fieldId);
+ }
+
+ public function writeFieldEnd() {
+ return 0;
+ }
+
+ public function writeFieldStop() {
+ return
+ $this->writeByte(TType::STOP);
+ }
+
+ public function writeMapBegin($keyType, $valType, $size) {
+ return
+ $this->writeByte($keyType) +
+ $this->writeByte($valType) +
+ $this->writeI32($size);
+ }
+
+ public function writeMapEnd() {
+ return 0;
+ }
+
+ public function writeListBegin($elemType, $size) {
+ return
+ $this->writeByte($elemType) +
+ $this->writeI32($size);
+ }
+
+ public function writeListEnd() {
+ return 0;
+ }
+
+ public function writeSetBegin($elemType, $size) {
+ return
+ $this->writeByte($elemType) +
+ $this->writeI32($size);
+ }
+
+ public function writeSetEnd() {
+ return 0;
+ }
+
+ public function writeBool($value) {
+ $data = pack('c', $value ? 1 : 0);
+ $this->trans_->write($data, 1);
+ return 1;
+ }
+
+ public function writeByte($value) {
+ $data = pack('c', $value);
+ $this->trans_->write($data, 1);
+ return 1;
+ }
+
+ public function writeI16($value) {
+ $data = pack('n', $value);
+ $this->trans_->write($data, 2);
+ return 2;
+ }
+
+ public function writeI32($value) {
+ $data = pack('N', $value);
+ $this->trans_->write($data, 4);
+ return 4;
+ }
+
+ public function writeI64($value) {
+ // If we are on a 32bit architecture we have to explicitly deal with
+ // 64-bit twos-complement arithmetic since PHP wants to treat all ints
+ // as signed and any int over 2^31 - 1 as a float
+ if (PHP_INT_SIZE == 4) {
+ $neg = $value < 0;
+
+ if ($neg) {
+ $value *= -1;
+ }
+
+ $hi = (int)($value / 4294967296);
+ $lo = (int)$value;
+
+ if ($neg) {
+ $hi = ~$hi;
+ $lo = ~$lo;
+ if (($lo & (int)0xffffffff) == (int)0xffffffff) {
+ $lo = 0;
+ $hi++;
+ } else {
+ $lo++;
+ }
+ }
+ $data = pack('N2', $hi, $lo);
+
+ } else {
+ $hi = $value >> 32;
+ $lo = $value & 0xFFFFFFFF;
+ $data = pack('N2', $hi, $lo);
+ }
+
+ $this->trans_->write($data, 8);
+ return 8;
+ }
+
+ public function writeDouble($value) {
+ $data = pack('d', $value);
+ $this->trans_->write(strrev($data), 8);
+ return 8;
+ }
+
+ public function writeString($value) {
+ $len = strlen($value);
+ $result = $this->writeI32($len);
+ if ($len) {
+ $this->trans_->write($value, $len);
+ }
+ return $result + $len;
+ }
+
+ public function readMessageBegin(&$name, &$type, &$seqid) {
+ $result = $this->readI32($sz);
+ if ($sz < 0) {
+ $version = (int) ($sz & self::VERSION_MASK);
+ if ($version != (int) self::VERSION_1) {
+ throw new TProtocolException('Bad version identifier: '.$sz, TProtocolException::BAD_VERSION);
+ }
+ $type = $sz & 0x000000ff;
+ $result +=
+ $this->readString($name) +
+ $this->readI32($seqid);
+ } else {
+ if ($this->strictRead_) {
+ throw new TProtocolException('No version identifier, old protocol client?', TProtocolException::BAD_VERSION);
+ } else {
+ // Handle pre-versioned input
+ $name = $this->trans_->readAll($sz);
+ $result +=
+ $sz +
+ $this->readByte($type) +
+ $this->readI32($seqid);
+ }
+ }
+ return $result;
+ }
+
+ public function readMessageEnd() {
+ return 0;
+ }
+
+ public function readStructBegin(&$name) {
+ $name = '';
+ return 0;
+ }
+
+ public function readStructEnd() {
+ return 0;
+ }
+
+ public function readFieldBegin(&$name, &$fieldType, &$fieldId) {
+ $result = $this->readByte($fieldType);
+ if ($fieldType == TType::STOP) {
+ $fieldId = 0;
+ return $result;
+ }
+ $result += $this->readI16($fieldId);
+ return $result;
+ }
+
+ public function readFieldEnd() {
+ return 0;
+ }
+
+ public function readMapBegin(&$keyType, &$valType, &$size) {
+ return
+ $this->readByte($keyType) +
+ $this->readByte($valType) +
+ $this->readI32($size);
+ }
+
+ public function readMapEnd() {
+ return 0;
+ }
+
+ public function readListBegin(&$elemType, &$size) {
+ return
+ $this->readByte($elemType) +
+ $this->readI32($size);
+ }
+
+ public function readListEnd() {
+ return 0;
+ }
+
+ public function readSetBegin(&$elemType, &$size) {
+ return
+ $this->readByte($elemType) +
+ $this->readI32($size);
+ }
+
+ public function readSetEnd() {
+ return 0;
+ }
+
+ public function readBool(&$value) {
+ $data = $this->trans_->readAll(1);
+ $arr = unpack('c', $data);
+ $value = $arr[1] == 1;
+ return 1;
+ }
+
+ public function readByte(&$value) {
+ $data = $this->trans_->readAll(1);
+ $arr = unpack('c', $data);
+ $value = $arr[1];
+ return 1;
+ }
+
+ public function readI16(&$value) {
+ $data = $this->trans_->readAll(2);
+ $arr = unpack('n', $data);
+ $value = $arr[1];
+ if ($value > 0x7fff) {
+ $value = 0 - (($value - 1) ^ 0xffff);
+ }
+ return 2;
+ }
+
+ public function readI32(&$value) {
+ $data = $this->trans_->readAll(4);
+ $arr = unpack('N', $data);
+ $value = $arr[1];
+ if ($value > 0x7fffffff) {
+ $value = 0 - (($value - 1) ^ 0xffffffff);
+ }
+ return 4;
+ }
+
+ public function readI64(&$value) {
+ $data = $this->trans_->readAll(8);
+
+ $arr = unpack('N2', $data);
+
+ // If we are on a 32bit architecture we have to explicitly deal with
+ // 64-bit twos-complement arithmetic since PHP wants to treat all ints
+ // as signed and any int over 2^31 - 1 as a float
+ if (PHP_INT_SIZE == 4) {
+
+ $hi = $arr[1];
+ $lo = $arr[2];
+ $isNeg = $hi < 0;
+
+ // Check for a negative
+ if ($isNeg) {
+ $hi = ~$hi & (int)0xffffffff;
+ $lo = ~$lo & (int)0xffffffff;
+
+ if ($lo == (int)0xffffffff) {
+ $hi++;
+ $lo = 0;
+ } else {
+ $lo++;
+ }
+ }
+
+ // Force 32bit words in excess of 2G to pe positive - we deal wigh sign
+ // explicitly below
+
+ if ($hi & (int)0x80000000) {
+ $hi &= (int)0x7fffffff;
+ $hi += 0x80000000;
+ }
+
+ if ($lo & (int)0x80000000) {
+ $lo &= (int)0x7fffffff;
+ $lo += 0x80000000;
+ }
+
+ $value = $hi * 4294967296 + $lo;
+
+ if ($isNeg) {
+ $value = 0 - $value;
+ }
+ } else {
+
+ // Upcast negatives in LSB bit
+ if ($arr[2] & 0x80000000) {
+ $arr[2] = $arr[2] & 0xffffffff;
+ }
+
+ // Check for a negative
+ if ($arr[1] & 0x80000000) {
+ $arr[1] = $arr[1] & 0xffffffff;
+ $arr[1] = $arr[1] ^ 0xffffffff;
+ $arr[2] = $arr[2] ^ 0xffffffff;
+ $value = 0 - $arr[1]*4294967296 - $arr[2] - 1;
+ } else {
+ $value = $arr[1]*4294967296 + $arr[2];
+ }
+ }
+
+ return 8;
+ }
+
+ public function readDouble(&$value) {
+ $data = strrev($this->trans_->readAll(8));
+ $arr = unpack('d', $data);
+ $value = $arr[1];
+ return 8;
+ }
+
+ public function readString(&$value) {
+ $result = $this->readI32($len);
+ if ($len) {
+ $value = $this->trans_->readAll($len);
+ } else {
+ $value = '';
+ }
+ return $result + $len;
+ }
+}
+
+/**
+ * Binary Protocol Factory
+ */
+class TBinaryProtocolFactory implements TProtocolFactory {
+ private $strictRead_ = false;
+ private $strictWrite_ = false;
+
+ public function __construct($strictRead=false, $strictWrite=false) {
+ $this->strictRead_ = $strictRead;
+ $this->strictWrite_ = $strictWrite;
+ }
+
+ public function getProtocol($trans) {
+ return new TBinaryProtocol($trans, $this->strictRead, $this->strictWrite);
+ }
+}
+
+/**
+ * Accelerated binary protocol: used in conjunction with the thrift_protocol
+ * extension for faster deserialization
+ */
+class TBinaryProtocolAccelerated extends TBinaryProtocol {
+ public function __construct($trans, $strictRead=false, $strictWrite=true) {
+ // If the transport doesn't implement putBack, wrap it in a
+ // TBufferedTransport (which does)
+ if (!method_exists($trans, 'putBack')) {
+ $trans = new TBufferedTransport($trans);
+ }
+ parent::__construct($trans, $strictRead, $strictWrite);
+ }
+ public function isStrictRead() {
+ return $this->strictRead_;
+ }
+ public function isStrictWrite() {
+ return $this->strictWrite_;
+ }
+}
+
+?>
diff --git a/lib/php/src/protocol/TProtocol.php b/lib/php/src/protocol/TProtocol.php
new file mode 100644
index 0000000..e9ff41a
--- /dev/null
+++ b/lib/php/src/protocol/TProtocol.php
@@ -0,0 +1,377 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * @package thrift.protocol
+ */
+
+
+/**
+ * Protocol module. Contains all the types and definitions needed to implement
+ * a protocol encoder/decoder.
+ *
+ * @package thrift.protocol
+ */
+
+/**
+ * Protocol exceptions
+ */
+class TProtocolException extends TException {
+ const UNKNOWN = 0;
+ const INVALID_DATA = 1;
+ const NEGATIVE_SIZE = 2;
+ const SIZE_LIMIT = 3;
+ const BAD_VERSION = 4;
+
+ function __construct($message=null, $code=0) {
+ parent::__construct($message, $code);
+ }
+}
+
+/**
+ * Protocol base class module.
+ */
+abstract class TProtocol {
+ // The below may seem silly, but it is to get around the problem that the
+ // "instanceof" operator can only take in a T_VARIABLE and not a T_STRING
+ // or T_CONSTANT_ENCAPSED_STRING. Using "is_a()" instead of "instanceof" is
+ // a workaround but is deprecated in PHP5. This is used in the generated
+ // deserialization code.
+ static $TBINARYPROTOCOLACCELERATED = 'TBinaryProtocolAccelerated';
+
+ /**
+ * Underlying transport
+ *
+ * @var TTransport
+ */
+ protected $trans_;
+
+ /**
+ * Constructor
+ */
+ protected function __construct($trans) {
+ $this->trans_ = $trans;
+ }
+
+ /**
+ * Accessor for transport
+ *
+ * @return TTransport
+ */
+ public function getTransport() {
+ return $this->trans_;
+ }
+
+ /**
+ * Writes the message header
+ *
+ * @param string $name Function name
+ * @param int $type message type TMessageType::CALL or TMessageType::REPLY
+ * @param int $seqid The sequence id of this message
+ */
+ public abstract function writeMessageBegin($name, $type, $seqid);
+
+ /**
+ * Close the message
+ */
+ public abstract function writeMessageEnd();
+
+ /**
+ * Writes a struct header.
+ *
+ * @param string $name Struct name
+ * @throws TException on write error
+ * @return int How many bytes written
+ */
+ public abstract function writeStructBegin($name);
+
+ /**
+ * Close a struct.
+ *
+ * @throws TException on write error
+ * @return int How many bytes written
+ */
+ public abstract function writeStructEnd();
+
+ /*
+ * Starts a field.
+ *
+ * @param string $name Field name
+ * @param int $type Field type
+ * @param int $fid Field id
+ * @throws TException on write error
+ * @return int How many bytes written
+ */
+ public abstract function writeFieldBegin($fieldName, $fieldType, $fieldId);
+
+ public abstract function writeFieldEnd();
+
+ public abstract function writeFieldStop();
+
+ public abstract function writeMapBegin($keyType, $valType, $size);
+
+ public abstract function writeMapEnd();
+
+ public abstract function writeListBegin($elemType, $size);
+
+ public abstract function writeListEnd();
+
+ public abstract function writeSetBegin($elemType, $size);
+
+ public abstract function writeSetEnd();
+
+ public abstract function writeBool($bool);
+
+ public abstract function writeByte($byte);
+
+ public abstract function writeI16($i16);
+
+ public abstract function writeI32($i32);
+
+ public abstract function writeI64($i64);
+
+ public abstract function writeDouble($dub);
+
+ public abstract function writeString($str);
+
+ /**
+ * Reads the message header
+ *
+ * @param string $name Function name
+ * @param int $type message type TMessageType::CALL or TMessageType::REPLY
+ * @parem int $seqid The sequence id of this message
+ */
+ public abstract function readMessageBegin(&$name, &$type, &$seqid);
+
+ /**
+ * Read the close of message
+ */
+ public abstract function readMessageEnd();
+
+ public abstract function readStructBegin(&$name);
+
+ public abstract function readStructEnd();
+
+ public abstract function readFieldBegin(&$name, &$fieldType, &$fieldId);
+
+ public abstract function readFieldEnd();
+
+ public abstract function readMapBegin(&$keyType, &$valType, &$size);
+
+ public abstract function readMapEnd();
+
+ public abstract function readListBegin(&$elemType, &$size);
+
+ public abstract function readListEnd();
+
+ public abstract function readSetBegin(&$elemType, &$size);
+
+ public abstract function readSetEnd();
+
+ public abstract function readBool(&$bool);
+
+ public abstract function readByte(&$byte);
+
+ public abstract function readI16(&$i16);
+
+ public abstract function readI32(&$i32);
+
+ public abstract function readI64(&$i64);
+
+ public abstract function readDouble(&$dub);
+
+ public abstract function readString(&$str);
+
+ /**
+ * The skip function is a utility to parse over unrecognized date without
+ * causing corruption.
+ *
+ * @param TType $type What type is it
+ */
+ public function skip($type) {
+ switch ($type) {
+ case TType::BOOL:
+ return $this->readBool($bool);
+ case TType::BYTE:
+ return $this->readByte($byte);
+ case TType::I16:
+ return $this->readI16($i16);
+ case TType::I32:
+ return $this->readI32($i32);
+ case TType::I64:
+ return $this->readI64($i64);
+ case TType::DOUBLE:
+ return $this->readDouble($dub);
+ case TType::STRING:
+ return $this->readString($str);
+ case TType::STRUCT:
+ {
+ $result = $this->readStructBegin($name);
+ while (true) {
+ $result += $this->readFieldBegin($name, $ftype, $fid);
+ if ($ftype == TType::STOP) {
+ break;
+ }
+ $result += $this->skip($ftype);
+ $result += $this->readFieldEnd();
+ }
+ $result += $this->readStructEnd();
+ return $result;
+ }
+ case TType::MAP:
+ {
+ $result = $this->readMapBegin($keyType, $valType, $size);
+ for ($i = 0; $i < $size; $i++) {
+ $result += $this->skip($keyType);
+ $result += $this->skip($valType);
+ }
+ $result += $this->readMapEnd();
+ return $result;
+ }
+ case TType::SET:
+ {
+ $result = $this->readSetBegin($elemType, $size);
+ for ($i = 0; $i < $size; $i++) {
+ $result += $this->skip($elemType);
+ }
+ $result += $this->readSetEnd();
+ return $result;
+ }
+ case TType::LST:
+ {
+ $result = $this->readListBegin($elemType, $size);
+ for ($i = 0; $i < $size; $i++) {
+ $result += $this->skip($elemType);
+ }
+ $result += $this->readListEnd();
+ return $result;
+ }
+ default:
+ return 0;
+ }
+ }
+
+ /**
+ * Utility for skipping binary data
+ *
+ * @param TTransport $itrans TTransport object
+ * @param int $type Field type
+ */
+ public static function skipBinary($itrans, $type) {
+ switch ($type) {
+ case TType::BOOL:
+ return $itrans->readAll(1);
+ case TType::BYTE:
+ return $itrans->readAll(1);
+ case TType::I16:
+ return $itrans->readAll(2);
+ case TType::I32:
+ return $itrans->readAll(4);
+ case TType::I64:
+ return $itrans->readAll(8);
+ case TType::DOUBLE:
+ return $itrans->readAll(8);
+ case TType::STRING:
+ $len = unpack('N', $itrans->readAll(4));
+ $len = $len[1];
+ if ($len > 0x7fffffff) {
+ $len = 0 - (($len - 1) ^ 0xffffffff);
+ }
+ return 4 + $itrans->readAll($len);
+ case TType::STRUCT:
+ {
+ $result = 0;
+ while (true) {
+ $ftype = 0;
+ $fid = 0;
+ $data = $itrans->readAll(1);
+ $arr = unpack('c', $data);
+ $ftype = $arr[1];
+ if ($ftype == TType::STOP) {
+ break;
+ }
+ // I16 field id
+ $result += $itrans->readAll(2);
+ $result += self::skipBinary($itrans, $ftype);
+ }
+ return $result;
+ }
+ case TType::MAP:
+ {
+ // Ktype
+ $data = $itrans->readAll(1);
+ $arr = unpack('c', $data);
+ $ktype = $arr[1];
+ // Vtype
+ $data = $itrans->readAll(1);
+ $arr = unpack('c', $data);
+ $vtype = $arr[1];
+ // Size
+ $data = $itrans->readAll(4);
+ $arr = unpack('N', $data);
+ $size = $arr[1];
+ if ($size > 0x7fffffff) {
+ $size = 0 - (($size - 1) ^ 0xffffffff);
+ }
+ $result = 6;
+ for ($i = 0; $i < $size; $i++) {
+ $result += self::skipBinary($itrans, $ktype);
+ $result += self::skipBinary($itrans, $vtype);
+ }
+ return $result;
+ }
+ case TType::SET:
+ case TType::LST:
+ {
+ // Vtype
+ $data = $itrans->readAll(1);
+ $arr = unpack('c', $data);
+ $vtype = $arr[1];
+ // Size
+ $data = $itrans->readAll(4);
+ $arr = unpack('N', $data);
+ $size = $arr[1];
+ if ($size > 0x7fffffff) {
+ $size = 0 - (($size - 1) ^ 0xffffffff);
+ }
+ $result = 5;
+ for ($i = 0; $i < $size; $i++) {
+ $result += self::skipBinary($itrans, $vtype);
+ }
+ return $result;
+ }
+ default:
+ return 0;
+ }
+ }
+}
+
+/**
+ * Protocol factory creates protocol objects from transports
+ */
+interface TProtocolFactory {
+ /**
+ * Build a protocol from the base transport
+ *
+ * @return TProtcol protocol
+ */
+ public function getProtocol($trans);
+}
+
+
+?>