| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php | 
|  | 2 |  | 
|  | 3 | /** For transport operations */ | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 4 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 5 |  | 
|  | 6 | /** | 
|  | 7 | * Binary implementation of the Thrift protocol. | 
|  | 8 | * | 
|  | 9 | * @package thrift.protocol | 
|  | 10 | * @author Mark Slee <mcslee@facebook.com> | 
|  | 11 | */ | 
|  | 12 | class TBinaryProtocol extends TProtocol { | 
|  | 13 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 14 | public function writeMessageBegin($out, $name, $type, $seqid) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 15 | return | 
|  | 16 | $this->writeString($out, $name) + | 
|  | 17 | $this->writeByte($out, $type) + | 
|  | 18 | $this->writeI32($out, $seqid); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 19 | } | 
|  | 20 |  | 
|  | 21 | public function writeMessageEnd($out) { | 
|  | 22 | return 0; | 
|  | 23 | } | 
|  | 24 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 25 | public function writeStructBegin($out, $name) { | 
|  | 26 | return 0; | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | public function writeStructEnd($out) { | 
|  | 30 | return 0; | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | public function writeFieldBegin($out, $fieldName, $fieldType, $fieldId) { | 
|  | 34 | return | 
|  | 35 | $this->writeByte($out, $fieldType) + | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 36 | $this->writeI16($out, $fieldId); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
|  | 39 | public function writeFieldEnd($out) { | 
|  | 40 | return 0; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | public function writeFieldStop($out) { | 
|  | 44 | return | 
|  | 45 | $this->writeByte($out, TType::STOP); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | public function writeMapBegin($out, $keyType, $valType, $size) { | 
|  | 49 | return | 
|  | 50 | $this->writeByte($out, $keyType) + | 
|  | 51 | $this->writeByte($out, $valType) + | 
|  | 52 | $this->writeI32($out, $size); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | public function writeMapEnd($out) { | 
|  | 56 | return 0; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | public function writeListBegin($out, $elemType, $size) { | 
|  | 60 | return | 
|  | 61 | $this->writeByte($out, $elemType) + | 
|  | 62 | $this->writeI32($out, $size); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | public function writeListEnd($out) { | 
|  | 66 | return 0; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | public function writeSetBegin($out, $elemType, $size) { | 
|  | 70 | return | 
|  | 71 | $this->writeByte($out, $elemType) + | 
|  | 72 | $this->writeI32($out, $size); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | public function writeSetEnd($out) { | 
|  | 76 | return 0; | 
|  | 77 | } | 
|  | 78 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 79 | public function writeBool($out, $value) { | 
|  | 80 | $data = pack('c', $value ? 1 : 0); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 81 | $out->write($data, 1); | 
|  | 82 | return 1; | 
|  | 83 | } | 
|  | 84 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 85 | public function writeByte($out, $value) { | 
|  | 86 | $data = pack('c', $value); | 
|  | 87 | $out->write($data, 1); | 
|  | 88 | return 1; | 
|  | 89 | } | 
|  | 90 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 91 | public function writeI16($out, $value) { | 
|  | 92 | $data = pack('n', $value); | 
|  | 93 | $out->write($data, 2); | 
|  | 94 | return 2; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | public function writeI32($out, $value) { | 
|  | 98 | $data = pack('N', $value); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 99 | $out->write($data, 4); | 
|  | 100 | return 4; | 
|  | 101 | } | 
|  | 102 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 103 | public function writeI64($out, $value) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 104 | // If we are on a 32bit architecture we have to explicitly deal with | 
|  | 105 | // 64-bit twos-complement arithmetic since PHP wants to treat all ints | 
|  | 106 | // as signed and any int over 2^31 - 1 as a float | 
|  | 107 | if (PHP_INT_SIZE == 4) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 108 | $neg = $value < 0; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | if ($neg) { | 
|  | 111 | $value *= -1; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 112 | } | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 113 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 114 | $hi = (int)($value / 4294967296); | 
|  | 115 | $lo = (int)$value; | 
|  | 116 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 117 | if ($neg) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 118 | $hi = ~$hi; | 
|  | 119 | $lo = ~$lo; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 120 | if (($lo & (int)0xffffffff) == (int)0xffffffff) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 121 | $lo = 0; | 
|  | 122 | $hi++; | 
|  | 123 | } else { | 
|  | 124 | $lo++; | 
|  | 125 | } | 
|  | 126 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 127 | $data = pack('N2', $hi, $lo); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 128 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 129 | } else { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 130 | $hi = $value >> 32; | 
|  | 131 | $lo = $value & 0xFFFFFFFF; | 
|  | 132 | $data = pack('N2', $hi, $lo); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 133 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 134 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 135 | $out->write($data, 8); | 
|  | 136 | return 8; | 
|  | 137 | } | 
|  | 138 |  | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 139 | public function writeDouble($out, $value) { | 
|  | 140 | $data = pack('d', $value); | 
|  | 141 | $out->write(strrev($data), 8); | 
|  | 142 | return 8; | 
|  | 143 | } | 
|  | 144 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 145 | public function writeString($out, $value) { | 
|  | 146 | $len = strlen($value); | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 147 | $result = $this->writeI32($out, $len); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 148 | $out->write($value, $len); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 149 | return $result + $len; | 
|  | 150 | } | 
|  | 151 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 152 | public function readMessageBegin($in, &$name, &$type, &$seqid) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 153 | return | 
|  | 154 | $this->readString($in, $name) + | 
|  | 155 | $this->readByte($in, $type) + | 
|  | 156 | $this->readI32($in, $seqid); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
|  | 159 | public function readMessageEnd($out) { | 
|  | 160 | return 0; | 
|  | 161 | } | 
|  | 162 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 163 | public function readStructBegin($in, &$name) { | 
|  | 164 | $name = ''; | 
|  | 165 | return 0; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | public function readStructEnd($in) { | 
|  | 169 | return 0; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | public function readFieldBegin($in, &$name, &$fieldType, &$fieldId) { | 
|  | 173 | $result = $this->readByte($in, $fieldType); | 
|  | 174 | if ($fieldType == TType::STOP) { | 
|  | 175 | $fieldId = 0; | 
|  | 176 | return $result; | 
|  | 177 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 178 | $result += $this->readI16($in, $fieldId); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 179 | return $result; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | public function readFieldEnd($in) { | 
|  | 183 | return 0; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | public function readMapBegin($in, &$keyType, &$valType, &$size) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 187 | return | 
|  | 188 | $this->readByte($in, $keyType) + | 
|  | 189 | $this->readByte($in, $valType) + | 
|  | 190 | $this->readI32($in, $size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
|  | 193 | public function readMapEnd($in) { | 
|  | 194 | return 0; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | public function readListBegin($in, &$elemType, &$size) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 198 | return | 
|  | 199 | $this->readByte($in, $elemType) + | 
|  | 200 | $this->readI32($in, $size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
|  | 203 | public function readListEnd($in) { | 
|  | 204 | return 0; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | public function readSetBegin($in, &$elemType, &$size) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 208 | return | 
|  | 209 | $this->readByte($in, $elemType) + | 
|  | 210 | $this->readI32($in, $size); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | public function readSetEnd($in) { | 
|  | 214 | return 0; | 
|  | 215 | } | 
|  | 216 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 217 | public function readBool($in, &$value) { | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 218 | $data = $in->readAll(1); | 
|  | 219 | $arr = unpack('c', $data); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 220 | $value = $arr[1] == 1; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 221 | return 1; | 
|  | 222 | } | 
|  | 223 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 224 | public function readByte($in, &$value) { | 
|  | 225 | $data = $in->readAll(1); | 
|  | 226 | $arr = unpack('c', $data); | 
|  | 227 | $value = $arr[1]; | 
|  | 228 | return 1; | 
|  | 229 | } | 
|  | 230 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 231 | public function readI16($in, &$value) { | 
|  | 232 | $data = $in->readAll(2); | 
|  | 233 | $arr = unpack('n', $data); | 
|  | 234 | $value = $arr[1]; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 235 | if ($value > 0x7fff) { | 
|  | 236 | $value = 0 - (($value - 1) ^ 0xffff); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 237 | } | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 238 | return 2; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | public function readI32($in, &$value) { | 
|  | 242 | $data = $in->readAll(4); | 
|  | 243 | $arr = unpack('N', $data); | 
|  | 244 | $value = $arr[1]; | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 245 | if ($value > 0x7fffffff) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 246 | $value = 0 - (($value - 1) ^ 0xffffffff); | 
|  | 247 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 248 | return 4; | 
|  | 249 | } | 
|  | 250 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 251 | public function readI64($in, &$value) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 252 | $data = $in->readAll(8); | 
|  | 253 |  | 
|  | 254 | $arr = unpack('N2', $data); | 
|  | 255 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 256 | // If we are on a 32bit architecture we have to explicitly deal with | 
|  | 257 | // 64-bit twos-complement arithmetic since PHP wants to treat all ints | 
|  | 258 | // as signed and any int over 2^31 - 1 as a float | 
|  | 259 | if (PHP_INT_SIZE == 4) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 260 |  | 
|  | 261 | $hi = $arr[1]; | 
|  | 262 | $lo = $arr[2]; | 
|  | 263 | $isNeg = $hi  < 0; | 
|  | 264 |  | 
|  | 265 | // Check for a negative | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 266 | if ($isNeg) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 267 | $hi = ~$hi & (int)0xffffffff; | 
|  | 268 | $lo = ~$lo & (int)0xffffffff; | 
|  | 269 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 270 | if ($lo == (int)0xffffffff) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 271 | $hi++; | 
|  | 272 | $lo = 0; | 
|  | 273 | } else { | 
|  | 274 | $lo++; | 
|  | 275 | } | 
|  | 276 | } | 
|  | 277 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 278 | // Force 32bit words in excess of 2G to pe positive - we deal wigh sign | 
|  | 279 | // explicitly below | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 280 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 281 | if ($hi & (int)0x80000000) { | 
|  | 282 | $hi &= (int)0x7fffffff; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 283 | $hi += 0x80000000; | 
|  | 284 | } | 
|  | 285 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 286 | if ($lo & (int)0x80000000) { | 
|  | 287 | $lo &= (int)0x7fffffff; | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 288 | $lo += 0x80000000; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | $value = $hi * 4294967296 + $lo; | 
|  | 292 |  | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 293 | if ($isNeg) { | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 294 | $value = 0 - $value; | 
|  | 295 | } | 
|  | 296 | } else { | 
|  | 297 |  | 
|  | 298 | // Check for a negative | 
|  | 299 | if ($arr[1] & 0x80000000) { | 
|  | 300 | $arr[1] = $arr[1] ^ 0xFFFFFFFF; | 
|  | 301 | $arr[2] = $arr[2] ^ 0xFFFFFFFF; | 
|  | 302 | $value = 0 - $arr[1]*4294967296 - $arr[2] - 1; | 
|  | 303 | } else { | 
|  | 304 | $value = $arr[1]*4294967296 + $arr[2]; | 
|  | 305 | } | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | return 8; | 
|  | 309 | } | 
|  | 310 |  | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 311 | public function readDouble($in, &$value) { | 
|  | 312 | $data = strrev($in->readAll(8)); | 
|  | 313 | $arr = unpack('d', $data); | 
|  | 314 | $value = $arr[1]; | 
|  | 315 | return 8; | 
|  | 316 | } | 
|  | 317 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 318 | public function readString($in, &$value) { | 
| Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 319 | $result = $this->readI32($in, $len); | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 320 | $value = $in->readAll($len); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 321 | return $result + $len; | 
|  | 322 | } | 
|  | 323 | } | 
|  | 324 |  | 
| Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 325 | ?> |