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