| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 1 | <?php | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | /* | 
|  | 3 | * Licensed to the Apache Software Foundation (ASF) under one | 
|  | 4 | * or more contributor license agreements. See the NOTICE file | 
|  | 5 | * distributed with this work for additional information | 
|  | 6 | * regarding copyright ownership. The ASF licenses this file | 
|  | 7 | * to you under the Apache License, Version 2.0 (the | 
|  | 8 | * "License"); you may not use this file except in compliance | 
|  | 9 | * with the License. You may obtain a copy of the License at | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 10 | * | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 11 | *   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | * Unless required by applicable law or agreed to in writing, | 
|  | 14 | * software distributed under the License is distributed on an | 
|  | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 16 | * KIND, either express or implied. See the License for the | 
|  | 17 | * specific language governing permissions and limitations | 
|  | 18 | * under the License. | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 19 | * | 
|  | 20 | * @package thrift.protocol | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 21 | */ | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 22 |  | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 23 | include_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; | 
|  | 24 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 25 | /** | 
|  | 26 | * Binary implementation of the Thrift protocol. | 
|  | 27 | * | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 28 | */ | 
|  | 29 | class TBinaryProtocol extends TProtocol { | 
|  | 30 |  | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 31 | const VERSION_MASK = 0xffff0000; | 
|  | 32 | const VERSION_1 = 0x80010000; | 
|  | 33 |  | 
| dweatherford | cf997a4 | 2008-03-04 01:08:23 +0000 | [diff] [blame] | 34 | protected $strictRead_ = false; | 
|  | 35 | protected $strictWrite_ = true; | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 36 |  | 
|  | 37 | public function __construct($trans, $strictRead=false, $strictWrite=true) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 38 | parent::__construct($trans); | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 39 | $this->strictRead_ = $strictRead; | 
|  | 40 | $this->strictWrite_ = $strictWrite; | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
|  | 43 | public function writeMessageBegin($name, $type, $seqid) { | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 44 | if ($this->strictWrite_) { | 
|  | 45 | $version = self::VERSION_1 | $type; | 
|  | 46 | return | 
|  | 47 | $this->writeI32($version) + | 
|  | 48 | $this->writeString($name) + | 
|  | 49 | $this->writeI32($seqid); | 
|  | 50 | } else { | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 51 | return | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 52 | $this->writeString($name) + | 
|  | 53 | $this->writeByte($type) + | 
|  | 54 | $this->writeI32($seqid); | 
|  | 55 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 58 | public function writeMessageEnd() { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 59 | return 0; | 
|  | 60 | } | 
|  | 61 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 62 | public function writeStructBegin($name) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 63 | return 0; | 
|  | 64 | } | 
|  | 65 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 66 | public function writeStructEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 67 | return 0; | 
|  | 68 | } | 
|  | 69 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 70 | public function writeFieldBegin($fieldName, $fieldType, $fieldId) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 71 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 72 | $this->writeByte($fieldType) + | 
|  | 73 | $this->writeI16($fieldId); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 76 | public function writeFieldEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 77 | return 0; | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 78 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 79 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 80 | public function writeFieldStop() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 81 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 82 | $this->writeByte(TType::STOP); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 85 | public function writeMapBegin($keyType, $valType, $size) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 86 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 87 | $this->writeByte($keyType) + | 
|  | 88 | $this->writeByte($valType) + | 
|  | 89 | $this->writeI32($size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 92 | public function writeMapEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 93 | return 0; | 
|  | 94 | } | 
|  | 95 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 96 | public function writeListBegin($elemType, $size) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 97 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 98 | $this->writeByte($elemType) + | 
|  | 99 | $this->writeI32($size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 102 | public function writeListEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 103 | return 0; | 
|  | 104 | } | 
|  | 105 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 106 | public function writeSetBegin($elemType, $size) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 107 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 108 | $this->writeByte($elemType) + | 
|  | 109 | $this->writeI32($size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 112 | public function writeSetEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 113 | return 0; | 
|  | 114 | } | 
|  | 115 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 116 | public function writeBool($value) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 117 | $data = pack('c', $value ? 1 : 0); | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 118 | $this->trans_->write($data, 1); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 119 | return 1; | 
|  | 120 | } | 
|  | 121 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 122 | public function writeByte($value) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 123 | $data = pack('c', $value); | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 124 | $this->trans_->write($data, 1); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 125 | return 1; | 
|  | 126 | } | 
|  | 127 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 128 | public function writeI16($value) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 129 | $data = pack('n', $value); | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 130 | $this->trans_->write($data, 2); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 131 | return 2; | 
|  | 132 | } | 
|  | 133 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 134 | public function writeI32($value) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 135 | $data = pack('N', $value); | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 136 | $this->trans_->write($data, 4); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 137 | return 4; | 
|  | 138 | } | 
|  | 139 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 140 | public function writeI64($value) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 141 | // If we are on a 32bit architecture we have to explicitly deal with | 
|  | 142 | // 64-bit twos-complement arithmetic since PHP wants to treat all ints | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 143 | // as signed and any int over 2^31 - 1 as a float | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 144 | if (PHP_INT_SIZE == 4) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 145 | $neg = $value < 0; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 146 |  | 
|  | 147 | if ($neg) { | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 148 | $value *= -1; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 149 | } | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 150 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 151 | $hi = (int)($value / 4294967296); | 
|  | 152 | $lo = (int)$value; | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 153 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 154 | if ($neg) { | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 155 | $hi = ~$hi; | 
|  | 156 | $lo = ~$lo; | 
|  | 157 | if (($lo & (int)0xffffffff) == (int)0xffffffff) { | 
|  | 158 | $lo = 0; | 
|  | 159 | $hi++; | 
|  | 160 | } else { | 
|  | 161 | $lo++; | 
|  | 162 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 163 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 164 | $data = pack('N2', $hi, $lo); | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 165 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 166 | } else { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 167 | $hi = $value >> 32; | 
|  | 168 | $lo = $value & 0xFFFFFFFF; | 
|  | 169 | $data = pack('N2', $hi, $lo); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 170 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 171 |  | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 172 | $this->trans_->write($data, 8); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 173 | return 8; | 
|  | 174 | } | 
|  | 175 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 176 | public function writeDouble($value) { | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 177 | $data = pack('d', $value); | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 178 | $this->trans_->write(strrev($data), 8); | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 179 | return 8; | 
|  | 180 | } | 
|  | 181 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 182 | public function writeString($value) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 183 | $len = strlen($value); | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 184 | $result = $this->writeI32($len); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 185 | if ($len) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 186 | $this->trans_->write($value, $len); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 187 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 188 | return $result + $len; | 
|  | 189 | } | 
|  | 190 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 191 | public function readMessageBegin(&$name, &$type, &$seqid) { | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 192 | $result = $this->readI32($sz); | 
|  | 193 | if ($sz < 0) { | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 194 | $version = (int) ($sz & self::VERSION_MASK); | 
|  | 195 | if ($version != (int) self::VERSION_1) { | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 196 | throw new TProtocolException('Bad version identifier: '.$sz, TProtocolException::BAD_VERSION); | 
|  | 197 | } | 
|  | 198 | $type = $sz & 0x000000ff; | 
|  | 199 | $result += | 
|  | 200 | $this->readString($name) + | 
|  | 201 | $this->readI32($seqid); | 
|  | 202 | } else { | 
|  | 203 | if ($this->strictRead_) { | 
|  | 204 | throw new TProtocolException('No version identifier, old protocol client?', TProtocolException::BAD_VERSION); | 
|  | 205 | } else { | 
|  | 206 | // Handle pre-versioned input | 
|  | 207 | $name = $this->trans_->readAll($sz); | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 208 | $result += | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 209 | $sz + | 
|  | 210 | $this->readByte($type) + | 
|  | 211 | $this->readI32($seqid); | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 | return $result; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 217 | public function readMessageEnd() { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 218 | return 0; | 
|  | 219 | } | 
|  | 220 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 221 | public function readStructBegin(&$name) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 222 | $name = ''; | 
|  | 223 | return 0; | 
|  | 224 | } | 
|  | 225 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 226 | public function readStructEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 227 | return 0; | 
|  | 228 | } | 
|  | 229 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 230 | public function readFieldBegin(&$name, &$fieldType, &$fieldId) { | 
|  | 231 | $result = $this->readByte($fieldType); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 232 | if ($fieldType == TType::STOP) { | 
|  | 233 | $fieldId = 0; | 
|  | 234 | return $result; | 
|  | 235 | } | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 236 | $result += $this->readI16($fieldId); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 237 | return $result; | 
|  | 238 | } | 
|  | 239 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 240 | public function readFieldEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 241 | return 0; | 
|  | 242 | } | 
|  | 243 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 244 | public function readMapBegin(&$keyType, &$valType, &$size) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 245 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 246 | $this->readByte($keyType) + | 
|  | 247 | $this->readByte($valType) + | 
|  | 248 | $this->readI32($size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 251 | public function readMapEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 252 | return 0; | 
|  | 253 | } | 
|  | 254 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 255 | public function readListBegin(&$elemType, &$size) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 256 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 257 | $this->readByte($elemType) + | 
|  | 258 | $this->readI32($size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 261 | public function readListEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 262 | return 0; | 
|  | 263 | } | 
|  | 264 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 265 | public function readSetBegin(&$elemType, &$size) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 266 | return | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 267 | $this->readByte($elemType) + | 
|  | 268 | $this->readI32($size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 269 | } | 
|  | 270 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 271 | public function readSetEnd() { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 272 | return 0; | 
|  | 273 | } | 
|  | 274 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 275 | public function readBool(&$value) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 276 | $data = $this->trans_->readAll(1); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 277 | $arr = unpack('c', $data); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 278 | $value = $arr[1] == 1; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 279 | return 1; | 
|  | 280 | } | 
|  | 281 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 282 | public function readByte(&$value) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 283 | $data = $this->trans_->readAll(1); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 284 | $arr = unpack('c', $data); | 
|  | 285 | $value = $arr[1]; | 
|  | 286 | return 1; | 
|  | 287 | } | 
|  | 288 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 289 | public function readI16(&$value) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 290 | $data = $this->trans_->readAll(2); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 291 | $arr = unpack('n', $data); | 
|  | 292 | $value = $arr[1]; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 293 | if ($value > 0x7fff) { | 
|  | 294 | $value = 0 - (($value - 1) ^ 0xffff); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 295 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 296 | return 2; | 
|  | 297 | } | 
|  | 298 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 299 | public function readI32(&$value) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 300 | $data = $this->trans_->readAll(4); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 301 | $arr = unpack('N', $data); | 
|  | 302 | $value = $arr[1]; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 303 | if ($value > 0x7fffffff) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 304 | $value = 0 - (($value - 1) ^ 0xffffffff); | 
|  | 305 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 306 | return 4; | 
|  | 307 | } | 
|  | 308 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 309 | public function readI64(&$value) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 310 | $data = $this->trans_->readAll(8); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 311 |  | 
|  | 312 | $arr = unpack('N2', $data); | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 313 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 314 | // If we are on a 32bit architecture we have to explicitly deal with | 
|  | 315 | // 64-bit twos-complement arithmetic since PHP wants to treat all ints | 
|  | 316 | // as signed and any int over 2^31 - 1 as a float | 
|  | 317 | if (PHP_INT_SIZE == 4) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 318 |  | 
|  | 319 | $hi = $arr[1]; | 
|  | 320 | $lo = $arr[2]; | 
|  | 321 | $isNeg = $hi  < 0; | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 322 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 323 | // Check for a negative | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 324 | if ($isNeg) { | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 325 | $hi = ~$hi & (int)0xffffffff; | 
|  | 326 | $lo = ~$lo & (int)0xffffffff; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 327 |  | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 328 | if ($lo == (int)0xffffffff) { | 
|  | 329 | $hi++; | 
|  | 330 | $lo = 0; | 
|  | 331 | } else { | 
|  | 332 | $lo++; | 
|  | 333 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 334 | } | 
|  | 335 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 336 | // Force 32bit words in excess of 2G to pe positive - we deal wigh sign | 
|  | 337 | // explicitly below | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 338 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 339 | if ($hi & (int)0x80000000) { | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 340 | $hi &= (int)0x7fffffff; | 
|  | 341 | $hi += 0x80000000; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 342 | } | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 343 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 344 | if ($lo & (int)0x80000000) { | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 345 | $lo &= (int)0x7fffffff; | 
|  | 346 | $lo += 0x80000000; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 347 | } | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 348 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 349 | $value = $hi * 4294967296 + $lo; | 
|  | 350 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 351 | if ($isNeg) { | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 352 | $value = 0 - $value; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 353 | } | 
|  | 354 | } else { | 
|  | 355 |  | 
| Mark Slee | 13a0d4a | 2007-04-03 03:16:11 +0000 | [diff] [blame] | 356 | // Upcast negatives in LSB bit | 
|  | 357 | if ($arr[2] & 0x80000000) { | 
|  | 358 | $arr[2] = $arr[2] & 0xffffffff; | 
|  | 359 | } | 
|  | 360 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 361 | // Check for a negative | 
|  | 362 | if ($arr[1] & 0x80000000) { | 
| Mark Slee | 13a0d4a | 2007-04-03 03:16:11 +0000 | [diff] [blame] | 363 | $arr[1] = $arr[1] & 0xffffffff; | 
|  | 364 | $arr[1] = $arr[1] ^ 0xffffffff; | 
|  | 365 | $arr[2] = $arr[2] ^ 0xffffffff; | 
|  | 366 | $value = 0 - $arr[1]*4294967296 - $arr[2] - 1; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 367 | } else { | 
| Mark Slee | 13a0d4a | 2007-04-03 03:16:11 +0000 | [diff] [blame] | 368 | $value = $arr[1]*4294967296 + $arr[2]; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 369 | } | 
|  | 370 | } | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 371 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 372 | return 8; | 
|  | 373 | } | 
|  | 374 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 375 | public function readDouble(&$value) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 376 | $data = strrev($this->trans_->readAll(8)); | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 377 | $arr = unpack('d', $data); | 
|  | 378 | $value = $arr[1]; | 
|  | 379 | return 8; | 
|  | 380 | } | 
|  | 381 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 382 | public function readString(&$value) { | 
|  | 383 | $result = $this->readI32($len); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 384 | if ($len) { | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 385 | $value = $this->trans_->readAll($len); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 386 | } else { | 
|  | 387 | $value = ''; | 
|  | 388 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 389 | return $result + $len; | 
|  | 390 | } | 
|  | 391 | } | 
|  | 392 |  | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 393 | /** | 
|  | 394 | * Binary Protocol Factory | 
|  | 395 | */ | 
|  | 396 | class TBinaryProtocolFactory implements TProtocolFactory { | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 397 | private $strictRead_ = false; | 
|  | 398 | private $strictWrite_ = false; | 
|  | 399 |  | 
|  | 400 | public function __construct($strictRead=false, $strictWrite=false) { | 
|  | 401 | $this->strictRead_ = $strictRead; | 
|  | 402 | $this->strictWrite_ = $strictWrite; | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 403 | } | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 404 |  | 
| Aditya Agarwal | 6a5bcaa | 2007-02-06 02:50:56 +0000 | [diff] [blame] | 405 | public function getProtocol($trans) { | 
| Bryan Duxbury | d3fceb0 | 2011-03-21 18:04:08 +0000 | [diff] [blame] | 406 | return new TBinaryProtocol($trans, $this->strictRead_, $this->strictWrite_); | 
| Mark Slee | 5f8237d | 2006-10-26 04:57:03 +0000 | [diff] [blame] | 407 | } | 
|  | 408 | } | 
|  | 409 |  | 
| dweatherford | 80940b7 | 2007-10-31 23:30:56 +0000 | [diff] [blame] | 410 | /** | 
|  | 411 | * Accelerated binary protocol: used in conjunction with the thrift_protocol | 
|  | 412 | * extension for faster deserialization | 
|  | 413 | */ | 
|  | 414 | class TBinaryProtocolAccelerated extends TBinaryProtocol { | 
|  | 415 | public function __construct($trans, $strictRead=false, $strictWrite=true) { | 
|  | 416 | // If the transport doesn't implement putBack, wrap it in a | 
|  | 417 | // TBufferedTransport (which does) | 
|  | 418 | if (!method_exists($trans, 'putBack')) { | 
|  | 419 | $trans = new TBufferedTransport($trans); | 
| Mark Slee | 276eea6 | 2007-11-20 02:06:47 +0000 | [diff] [blame] | 420 | } | 
| dweatherford | 80940b7 | 2007-10-31 23:30:56 +0000 | [diff] [blame] | 421 | parent::__construct($trans, $strictRead, $strictWrite); | 
|  | 422 | } | 
| dweatherford | cf997a4 | 2008-03-04 01:08:23 +0000 | [diff] [blame] | 423 | public function isStrictRead() { | 
|  | 424 | return $this->strictRead_; | 
|  | 425 | } | 
|  | 426 | public function isStrictWrite() { | 
|  | 427 | return $this->strictWrite_; | 
|  | 428 | } | 
| dweatherford | 80940b7 | 2007-10-31 23:30:56 +0000 | [diff] [blame] | 429 | } |