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