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