GPBWire.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. namespace Google\Protobuf\Internal;
  32. class GPBWire
  33. {
  34. const TAG_TYPE_BITS = 3;
  35. const WIRETYPE_VARINT = 0;
  36. const WIRETYPE_FIXED64 = 1;
  37. const WIRETYPE_LENGTH_DELIMITED = 2;
  38. const WIRETYPE_START_GROUP = 3;
  39. const WIRETYPE_END_GROUP = 4;
  40. const WIRETYPE_FIXED32 = 5;
  41. const UNKNOWN = 0;
  42. const NORMAL_FORMAT = 1;
  43. const PACKED_FORMAT = 2;
  44. public static function getTagFieldNumber($tag)
  45. {
  46. return ($tag >> self::TAG_TYPE_BITS) &
  47. (1 << ((PHP_INT_SIZE * 8) - self::TAG_TYPE_BITS)) - 1;
  48. }
  49. public static function getTagWireType($tag)
  50. {
  51. return $tag & 0x7;
  52. }
  53. public static function getWireType($type)
  54. {
  55. switch ($type) {
  56. case GPBType::FLOAT:
  57. case GPBType::FIXED32:
  58. case GPBType::SFIXED32:
  59. return self::WIRETYPE_FIXED32;
  60. case GPBType::DOUBLE:
  61. case GPBType::FIXED64:
  62. case GPBType::SFIXED64:
  63. return self::WIRETYPE_FIXED64;
  64. case GPBType::UINT32:
  65. case GPBType::UINT64:
  66. case GPBType::INT32:
  67. case GPBType::INT64:
  68. case GPBType::SINT32:
  69. case GPBType::SINT64:
  70. case GPBType::ENUM:
  71. case GPBType::BOOL:
  72. return self::WIRETYPE_VARINT;
  73. case GPBType::STRING:
  74. case GPBType::BYTES:
  75. case GPBType::MESSAGE:
  76. return self::WIRETYPE_LENGTH_DELIMITED;
  77. case GPBType::GROUP:
  78. user_error("Unsupported type.");
  79. return 0;
  80. default:
  81. user_error("Unsupported type.");
  82. return 0;
  83. }
  84. }
  85. // ZigZag Transform: Encodes signed integers so that they can be effectively
  86. // used with varint encoding.
  87. //
  88. // varint operates on unsigned integers, encoding smaller numbers into fewer
  89. // bytes. If you try to use it on a signed integer, it will treat this
  90. // number as a very large unsigned integer, which means that even small
  91. // signed numbers like -1 will take the maximum number of bytes (10) to
  92. // encode. zigZagEncode() maps signed integers to unsigned in such a way
  93. // that those with a small absolute value will have smaller encoded values,
  94. // making them appropriate for encoding using varint.
  95. //
  96. // int32 -> uint32
  97. // -------------------------
  98. // 0 -> 0
  99. // -1 -> 1
  100. // 1 -> 2
  101. // -2 -> 3
  102. // ... -> ...
  103. // 2147483647 -> 4294967294
  104. // -2147483648 -> 4294967295
  105. //
  106. // >> encode >>
  107. // << decode <<
  108. public static function zigZagEncode32($int32)
  109. {
  110. // Fill high 32 bits.
  111. if (PHP_INT_SIZE === 8) {
  112. $int32 |= ((($int32 << 32) >> 31) & (0xFFFFFFFF << 32));
  113. }
  114. $uint32 = ($int32 << 1) ^ ($int32 >> 31);
  115. // Fill high 32 bits.
  116. if (PHP_INT_SIZE === 8) {
  117. $uint32 |= ((($uint32 << 32) >> 31) & (0xFFFFFFFF << 32));
  118. }
  119. return $uint32;
  120. }
  121. public static function zigZagDecode32($uint32)
  122. {
  123. // Fill high 32 bits.
  124. if (PHP_INT_SIZE === 8) {
  125. $uint32 |= ($uint32 & 0xFFFFFFFF);
  126. }
  127. $int32 = (($uint32 >> 1) & 0x7FFFFFFF) ^ (-($uint32 & 1));
  128. return $int32;
  129. }
  130. public static function zigZagEncode64($int64)
  131. {
  132. if (PHP_INT_SIZE == 4) {
  133. if (bccomp($int64, 0) >= 0) {
  134. return bcmul($int64, 2);
  135. } else {
  136. return bcsub(bcmul(bcsub(0, $int64), 2), 1);
  137. }
  138. } else {
  139. return ($int64 << 1) ^ ($int64 >> 63);
  140. }
  141. }
  142. public static function zigZagDecode64($uint64)
  143. {
  144. if (PHP_INT_SIZE == 4) {
  145. if (bcmod($uint64, 2) == 0) {
  146. return bcdiv($uint64, 2, 0);
  147. } else {
  148. return bcsub(0, bcdiv(bcadd($uint64, 1), 2, 0));
  149. }
  150. } else {
  151. return (($uint64 >> 1) & 0x7FFFFFFFFFFFFFFF) ^ (-($uint64 & 1));
  152. }
  153. }
  154. public static function readInt32(&$input, &$value)
  155. {
  156. return $input->readVarint32($value);
  157. }
  158. public static function readInt64(&$input, &$value)
  159. {
  160. return $input->readVarint64($value);
  161. }
  162. public static function readUint32(&$input, &$value)
  163. {
  164. return self::readInt32($input, $value);
  165. }
  166. public static function readUint64(&$input, &$value)
  167. {
  168. return self::readInt64($input, $value);
  169. }
  170. public static function readSint32(&$input, &$value)
  171. {
  172. if (!$input->readVarint32($value)) {
  173. return false;
  174. }
  175. $value = GPBWire::zigZagDecode32($value);
  176. return true;
  177. }
  178. public static function readSint64(&$input, &$value)
  179. {
  180. if (!$input->readVarint64($value)) {
  181. return false;
  182. }
  183. $value = GPBWire::zigZagDecode64($value);
  184. return true;
  185. }
  186. public static function readFixed32(&$input, &$value)
  187. {
  188. return $input->readLittleEndian32($value);
  189. }
  190. public static function readFixed64(&$input, &$value)
  191. {
  192. return $input->readLittleEndian64($value);
  193. }
  194. public static function readSfixed32(&$input, &$value)
  195. {
  196. if (!self::readFixed32($input, $value)) {
  197. return false;
  198. }
  199. if (PHP_INT_SIZE === 8) {
  200. $value |= (-($value >> 31) << 32);
  201. }
  202. return true;
  203. }
  204. public static function readSfixed64(&$input, &$value)
  205. {
  206. return $input->readLittleEndian64($value);
  207. }
  208. public static function readFloat(&$input, &$value)
  209. {
  210. $data = null;
  211. if (!$input->readRaw(4, $data)) {
  212. return false;
  213. }
  214. $value = unpack('f', $data)[1];
  215. return true;
  216. }
  217. public static function readDouble(&$input, &$value)
  218. {
  219. $data = null;
  220. if (!$input->readRaw(8, $data)) {
  221. return false;
  222. }
  223. $value = unpack('d', $data)[1];
  224. return true;
  225. }
  226. public static function readBool(&$input, &$value)
  227. {
  228. if (!$input->readVarint64($value)) {
  229. return false;
  230. }
  231. if ($value == 0) {
  232. $value = false;
  233. } else {
  234. $value = true;
  235. }
  236. return true;
  237. }
  238. public static function readString(&$input, &$value)
  239. {
  240. $length = 0;
  241. return $input->readVarintSizeAsInt($length) && $input->readRaw($length, $value);
  242. }
  243. public static function readMessage(&$input, &$message)
  244. {
  245. $length = 0;
  246. if (!$input->readVarintSizeAsInt($length)) {
  247. return false;
  248. }
  249. $old_limit = 0;
  250. $recursion_limit = 0;
  251. $input->incrementRecursionDepthAndPushLimit(
  252. $length,
  253. $old_limit,
  254. $recursion_limit);
  255. if ($recursion_limit < 0 || !$message->parseFromStream($input)) {
  256. return false;
  257. }
  258. return $input->decrementRecursionDepthAndPopLimit($old_limit);
  259. }
  260. public static function writeTag(&$output, $tag)
  261. {
  262. return $output->writeTag($tag);
  263. }
  264. public static function writeInt32(&$output, $value)
  265. {
  266. return $output->writeVarint32($value);
  267. }
  268. public static function writeInt64(&$output, $value)
  269. {
  270. return $output->writeVarint64($value);
  271. }
  272. public static function writeUint32(&$output, $value)
  273. {
  274. return $output->writeVarint32($value);
  275. }
  276. public static function writeUint64(&$output, $value)
  277. {
  278. return $output->writeVarint64($value);
  279. }
  280. public static function writeSint32(&$output, $value)
  281. {
  282. $value = GPBWire::zigZagEncode32($value);
  283. return $output->writeVarint64($value);
  284. }
  285. public static function writeSint64(&$output, $value)
  286. {
  287. $value = GPBWire::zigZagEncode64($value);
  288. return $output->writeVarint64($value);
  289. }
  290. public static function writeFixed32(&$output, $value)
  291. {
  292. return $output->writeLittleEndian32($value);
  293. }
  294. public static function writeFixed64(&$output, $value)
  295. {
  296. return $output->writeLittleEndian64($value);
  297. }
  298. public static function writeSfixed32(&$output, $value)
  299. {
  300. return $output->writeLittleEndian32($value);
  301. }
  302. public static function writeSfixed64(&$output, $value)
  303. {
  304. return $output->writeLittleEndian64($value);
  305. }
  306. public static function writeBool(&$output, $value)
  307. {
  308. if ($value) {
  309. return $output->writeVarint32(1);
  310. } else {
  311. return $output->writeVarint32(0);
  312. }
  313. }
  314. public static function writeFloat(&$output, $value)
  315. {
  316. $data = pack("f", $value);
  317. return $output->writeRaw($data, 4);
  318. }
  319. public static function writeDouble(&$output, $value)
  320. {
  321. $data = pack("d", $value);
  322. return $output->writeRaw($data, 8);
  323. }
  324. public static function writeString(&$output, $value)
  325. {
  326. return self::writeBytes($output, $value);
  327. }
  328. public static function writeBytes(&$output, $value)
  329. {
  330. $size = strlen($value);
  331. if (!$output->writeVarint32($size)) {
  332. return false;
  333. }
  334. return $output->writeRaw($value, $size);
  335. }
  336. public static function writeMessage(&$output, $value)
  337. {
  338. $size = $value->byteSize();
  339. if (!$output->writeVarint32($size)) {
  340. return false;
  341. }
  342. return $value->serializeToStream($output);
  343. }
  344. public static function makeTag($number, $type)
  345. {
  346. return ($number << 3) | self::getWireType($type);
  347. }
  348. public static function tagSize($field)
  349. {
  350. $tag = self::makeTag($field->getNumber(), $field->getType());
  351. return self::varint32Size($tag);
  352. }
  353. public static function varint32Size($value)
  354. {
  355. if ($value < 0) {
  356. return 5;
  357. }
  358. if ($value < (1 << 7)) {
  359. return 1;
  360. }
  361. if ($value < (1 << 14)) {
  362. return 2;
  363. }
  364. if ($value < (1 << 21)) {
  365. return 3;
  366. }
  367. if ($value < (1 << 28)) {
  368. return 4;
  369. }
  370. return 5;
  371. }
  372. public static function sint32Size($value)
  373. {
  374. $value = self::zigZagEncode32($value);
  375. return self::varint32Size($value);
  376. }
  377. public static function sint64Size($value)
  378. {
  379. $value = self::zigZagEncode64($value);
  380. return self::varint64Size($value);
  381. }
  382. public static function varint64Size($value)
  383. {
  384. if ($value < 0) {
  385. return 10;
  386. }
  387. if ($value < (1 << 7)) {
  388. return 1;
  389. }
  390. if ($value < (1 << 14)) {
  391. return 2;
  392. }
  393. if ($value < (1 << 21)) {
  394. return 3;
  395. }
  396. if ($value < (1 << 28)) {
  397. return 4;
  398. }
  399. if ($value < (1 << 35)) {
  400. return 5;
  401. }
  402. if ($value < (1 << 42)) {
  403. return 6;
  404. }
  405. if ($value < (1 << 49)) {
  406. return 7;
  407. }
  408. if ($value < (1 << 56)) {
  409. return 8;
  410. }
  411. return 9;
  412. }
  413. public static function serializeFieldToStream(
  414. $value,
  415. $field,
  416. $need_tag,
  417. &$output)
  418. {
  419. if ($need_tag) {
  420. if (!GPBWire::writeTag(
  421. $output,
  422. self::makeTag(
  423. $field->getNumber(),
  424. $field->getType()))) {
  425. return false;
  426. }
  427. }
  428. switch ($field->getType()) {
  429. case GPBType::DOUBLE:
  430. if (!GPBWire::writeDouble($output, $value)) {
  431. return false;
  432. }
  433. break;
  434. case GPBType::FLOAT:
  435. if (!GPBWire::writeFloat($output, $value)) {
  436. return false;
  437. }
  438. break;
  439. case GPBType::INT64:
  440. if (!GPBWire::writeInt64($output, $value)) {
  441. return false;
  442. }
  443. break;
  444. case GPBType::UINT64:
  445. if (!GPBWire::writeUint64($output, $value)) {
  446. return false;
  447. }
  448. break;
  449. case GPBType::INT32:
  450. if (!GPBWire::writeInt32($output, $value)) {
  451. return false;
  452. }
  453. break;
  454. case GPBType::FIXED32:
  455. if (!GPBWire::writeFixed32($output, $value)) {
  456. return false;
  457. }
  458. break;
  459. case GPBType::FIXED64:
  460. if (!GPBWire::writeFixed64($output, $value)) {
  461. return false;
  462. }
  463. break;
  464. case GPBType::BOOL:
  465. if (!GPBWire::writeBool($output, $value)) {
  466. return false;
  467. }
  468. break;
  469. case GPBType::STRING:
  470. if (!GPBWire::writeString($output, $value)) {
  471. return false;
  472. }
  473. break;
  474. // case GPBType::GROUP:
  475. // echo "GROUP\xA";
  476. // trigger_error("Not implemented.", E_ERROR);
  477. // break;
  478. case GPBType::MESSAGE:
  479. if (!GPBWire::writeMessage($output, $value)) {
  480. return false;
  481. }
  482. break;
  483. case GPBType::BYTES:
  484. if (!GPBWire::writeBytes($output, $value)) {
  485. return false;
  486. }
  487. break;
  488. case GPBType::UINT32:
  489. if (!GPBWire::writeUint32($output, $value)) {
  490. return false;
  491. }
  492. break;
  493. case GPBType::ENUM:
  494. if (!GPBWire::writeInt32($output, $value)) {
  495. return false;
  496. }
  497. break;
  498. case GPBType::SFIXED32:
  499. if (!GPBWire::writeSfixed32($output, $value)) {
  500. return false;
  501. }
  502. break;
  503. case GPBType::SFIXED64:
  504. if (!GPBWire::writeSfixed64($output, $value)) {
  505. return false;
  506. }
  507. break;
  508. case GPBType::SINT32:
  509. if (!GPBWire::writeSint32($output, $value)) {
  510. return false;
  511. }
  512. break;
  513. case GPBType::SINT64:
  514. if (!GPBWire::writeSint64($output, $value)) {
  515. return false;
  516. }
  517. break;
  518. default:
  519. user_error("Unsupported type.");
  520. return false;
  521. }
  522. return true;
  523. }
  524. }