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