TBinaryProtocol.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. * @package thrift.protocol
  21. */
  22. namespace Thrift\Protocol;
  23. use Thrift\Type\TType;
  24. use Thrift\Exception\TProtocolException;
  25. use Thrift\Factory\TStringFuncFactory;
  26. /**
  27. * Binary implementation of the Thrift protocol.
  28. *
  29. */
  30. class TBinaryProtocol extends TProtocol
  31. {
  32. const VERSION_MASK = 0xffff0000;
  33. const VERSION_1 = 0x80010000;
  34. protected $strictRead_ = false;
  35. protected $strictWrite_ = true;
  36. public function __construct($trans, $strictRead=false, $strictWrite=true)
  37. {
  38. parent::__construct($trans);
  39. $this->strictRead_ = $strictRead;
  40. $this->strictWrite_ = $strictWrite;
  41. }
  42. public function writeMessageBegin($name, $type, $seqid)
  43. {
  44. if ($this->strictWrite_) {
  45. $version = self::VERSION_1 | $type;
  46. return
  47. $this->writeI32($version) +
  48. $this->writeString($name) +
  49. $this->writeI32($seqid);
  50. } else {
  51. return
  52. $this->writeString($name) +
  53. $this->writeByte($type) +
  54. $this->writeI32($seqid);
  55. }
  56. }
  57. public function writeMessageEnd()
  58. {
  59. return 0;
  60. }
  61. public function writeStructBegin($name)
  62. {
  63. return 0;
  64. }
  65. public function writeStructEnd()
  66. {
  67. return 0;
  68. }
  69. public function writeFieldBegin($fieldName, $fieldType, $fieldId)
  70. {
  71. return
  72. $this->writeByte($fieldType) +
  73. $this->writeI16($fieldId);
  74. }
  75. public function writeFieldEnd()
  76. {
  77. return 0;
  78. }
  79. public function writeFieldStop()
  80. {
  81. return
  82. $this->writeByte(TType::STOP);
  83. }
  84. public function writeMapBegin($keyType, $valType, $size)
  85. {
  86. return
  87. $this->writeByte($keyType) +
  88. $this->writeByte($valType) +
  89. $this->writeI32($size);
  90. }
  91. public function writeMapEnd()
  92. {
  93. return 0;
  94. }
  95. public function writeListBegin($elemType, $size)
  96. {
  97. return
  98. $this->writeByte($elemType) +
  99. $this->writeI32($size);
  100. }
  101. public function writeListEnd()
  102. {
  103. return 0;
  104. }
  105. public function writeSetBegin($elemType, $size)
  106. {
  107. return
  108. $this->writeByte($elemType) +
  109. $this->writeI32($size);
  110. }
  111. public function writeSetEnd()
  112. {
  113. return 0;
  114. }
  115. public function writeBool($value)
  116. {
  117. $data = pack('c', $value ? 1 : 0);
  118. $this->trans_->write($data, 1);
  119. return 1;
  120. }
  121. public function writeByte($value)
  122. {
  123. $data = pack('c', $value);
  124. $this->trans_->write($data, 1);
  125. return 1;
  126. }
  127. public function writeI16($value)
  128. {
  129. $data = pack('n', $value);
  130. $this->trans_->write($data, 2);
  131. return 2;
  132. }
  133. public function writeI32($value)
  134. {
  135. $data = pack('N', $value);
  136. $this->trans_->write($data, 4);
  137. return 4;
  138. }
  139. public function writeI64($value)
  140. {
  141. // If we are on a 32bit architecture we have to explicitly deal with
  142. // 64-bit twos-complement arithmetic since PHP wants to treat all ints
  143. // as signed and any int over 2^31 - 1 as a float
  144. if (PHP_INT_SIZE == 4) {
  145. $neg = $value < 0;
  146. if ($neg) {
  147. $value *= -1;
  148. }
  149. $hi = (int) ($value / 4294967296);
  150. $lo = (int) $value;
  151. if ($neg) {
  152. $hi = ~$hi;
  153. $lo = ~$lo;
  154. if (($lo & (int) 0xffffffff) == (int) 0xffffffff) {
  155. $lo = 0;
  156. $hi++;
  157. } else {
  158. $lo++;
  159. }
  160. }
  161. $data = pack('N2', $hi, $lo);
  162. } else {
  163. $hi = $value >> 32;
  164. $lo = $value & 0xFFFFFFFF;
  165. $data = pack('N2', $hi, $lo);
  166. }
  167. $this->trans_->write($data, 8);
  168. return 8;
  169. }
  170. public function writeDouble($value)
  171. {
  172. $data = pack('d', $value);
  173. $this->trans_->write(strrev($data), 8);
  174. return 8;
  175. }
  176. public function writeString($value)
  177. {
  178. $len = TStringFuncFactory::create()->strlen($value);
  179. $result = $this->writeI32($len);
  180. if ($len) {
  181. $this->trans_->write($value, $len);
  182. }
  183. return $result + $len;
  184. }
  185. public function readMessageBegin(&$name, &$type, &$seqid)
  186. {
  187. $result = $this->readI32($sz);
  188. if ($sz < 0) {
  189. $version = (int) ($sz & self::VERSION_MASK);
  190. if ($version != (int) self::VERSION_1) {
  191. throw new TProtocolException('Bad version identifier: '.$sz, TProtocolException::BAD_VERSION);
  192. }
  193. $type = $sz & 0x000000ff;
  194. $result +=
  195. $this->readString($name) +
  196. $this->readI32($seqid);
  197. } else {
  198. if ($this->strictRead_) {
  199. throw new TProtocolException('No version identifier, old protocol client?', TProtocolException::BAD_VERSION);
  200. } else {
  201. // Handle pre-versioned input
  202. $name = $this->trans_->readAll($sz);
  203. $result +=
  204. $sz +
  205. $this->readByte($type) +
  206. $this->readI32($seqid);
  207. }
  208. }
  209. return $result;
  210. }
  211. public function readMessageEnd()
  212. {
  213. return 0;
  214. }
  215. public function readStructBegin(&$name)
  216. {
  217. $name = '';
  218. return 0;
  219. }
  220. public function readStructEnd()
  221. {
  222. return 0;
  223. }
  224. public function readFieldBegin(&$name, &$fieldType, &$fieldId)
  225. {
  226. $result = $this->readByte($fieldType);
  227. if ($fieldType == TType::STOP) {
  228. $fieldId = 0;
  229. return $result;
  230. }
  231. $result += $this->readI16($fieldId);
  232. return $result;
  233. }
  234. public function readFieldEnd()
  235. {
  236. return 0;
  237. }
  238. public function readMapBegin(&$keyType, &$valType, &$size)
  239. {
  240. return
  241. $this->readByte($keyType) +
  242. $this->readByte($valType) +
  243. $this->readI32($size);
  244. }
  245. public function readMapEnd()
  246. {
  247. return 0;
  248. }
  249. public function readListBegin(&$elemType, &$size)
  250. {
  251. return
  252. $this->readByte($elemType) +
  253. $this->readI32($size);
  254. }
  255. public function readListEnd()
  256. {
  257. return 0;
  258. }
  259. public function readSetBegin(&$elemType, &$size)
  260. {
  261. return
  262. $this->readByte($elemType) +
  263. $this->readI32($size);
  264. }
  265. public function readSetEnd()
  266. {
  267. return 0;
  268. }
  269. public function readBool(&$value)
  270. {
  271. $data = $this->trans_->readAll(1);
  272. $arr = unpack('c', $data);
  273. $value = $arr[1] == 1;
  274. return 1;
  275. }
  276. public function readByte(&$value)
  277. {
  278. $data = $this->trans_->readAll(1);
  279. $arr = unpack('c', $data);
  280. $value = $arr[1];
  281. return 1;
  282. }
  283. public function readI16(&$value)
  284. {
  285. $data = $this->trans_->readAll(2);
  286. $arr = unpack('n', $data);
  287. $value = $arr[1];
  288. if ($value > 0x7fff) {
  289. $value = 0 - (($value - 1) ^ 0xffff);
  290. }
  291. return 2;
  292. }
  293. public function readI32(&$value)
  294. {
  295. $data = $this->trans_->readAll(4);
  296. $arr = unpack('N', $data);
  297. $value = $arr[1];
  298. if ($value > 0x7fffffff) {
  299. $value = 0 - (($value - 1) ^ 0xffffffff);
  300. }
  301. return 4;
  302. }
  303. public function readI64(&$value)
  304. {
  305. $data = $this->trans_->readAll(8);
  306. $arr = unpack('N2', $data);
  307. // If we are on a 32bit architecture we have to explicitly deal with
  308. // 64-bit twos-complement arithmetic since PHP wants to treat all ints
  309. // as signed and any int over 2^31 - 1 as a float
  310. if (PHP_INT_SIZE == 4) {
  311. $hi = $arr[1];
  312. $lo = $arr[2];
  313. $isNeg = $hi < 0;
  314. // Check for a negative
  315. if ($isNeg) {
  316. $hi = ~$hi & (int) 0xffffffff;
  317. $lo = ~$lo & (int) 0xffffffff;
  318. if ($lo == (int) 0xffffffff) {
  319. $hi++;
  320. $lo = 0;
  321. } else {
  322. $lo++;
  323. }
  324. }
  325. // Force 32bit words in excess of 2G to pe positive - we deal wigh sign
  326. // explicitly below
  327. if ($hi & (int) 0x80000000) {
  328. $hi &= (int) 0x7fffffff;
  329. $hi += 0x80000000;
  330. }
  331. if ($lo & (int) 0x80000000) {
  332. $lo &= (int) 0x7fffffff;
  333. $lo += 0x80000000;
  334. }
  335. $value = $hi * 4294967296 + $lo;
  336. if ($isNeg) {
  337. $value = 0 - $value;
  338. }
  339. } else {
  340. // Upcast negatives in LSB bit
  341. if ($arr[2] & 0x80000000) {
  342. $arr[2] = $arr[2] & 0xffffffff;
  343. }
  344. // Check for a negative
  345. if ($arr[1] & 0x80000000) {
  346. $arr[1] = $arr[1] & 0xffffffff;
  347. $arr[1] = $arr[1] ^ 0xffffffff;
  348. $arr[2] = $arr[2] ^ 0xffffffff;
  349. $value = 0 - $arr[1]*4294967296 - $arr[2] - 1;
  350. } else {
  351. $value = $arr[1]*4294967296 + $arr[2];
  352. }
  353. }
  354. return 8;
  355. }
  356. public function readDouble(&$value)
  357. {
  358. $data = strrev($this->trans_->readAll(8));
  359. $arr = unpack('d', $data);
  360. $value = $arr[1];
  361. return 8;
  362. }
  363. public function readString(&$value)
  364. {
  365. $result = $this->readI32($len);
  366. if ($len) {
  367. $value = $this->trans_->readAll($len);
  368. } else {
  369. $value = '';
  370. }
  371. return $result + $len;
  372. }
  373. }