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