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