InputStream.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. use Google\Protobuf\Internal\Uint64;
  33. function combineInt32ToInt64($high, $low)
  34. {
  35. $isNeg = $high < 0;
  36. if ($isNeg) {
  37. $high = ~$high;
  38. $low = ~$low;
  39. $low++;
  40. if (!$low) {
  41. $high++;
  42. }
  43. }
  44. $result = bcadd(bcmul($high, 4294967296), $low);
  45. if ($low < 0) {
  46. $result = bcadd($result, 4294967296);
  47. }
  48. if ($isNeg) {
  49. $result = bcsub(0, $result);
  50. }
  51. return $result;
  52. }
  53. class InputStream
  54. {
  55. private $buffer;
  56. private $buffer_size_after_limit;
  57. private $buffer_end;
  58. private $current;
  59. private $current_limit;
  60. private $legitimate_message_end;
  61. private $recursion_budget;
  62. private $recursion_limit;
  63. private $total_bytes_limit;
  64. private $total_bytes_read;
  65. const MAX_VARINT_BYTES = 10;
  66. const DEFAULT_RECURSION_LIMIT = 100;
  67. const DEFAULT_TOTAL_BYTES_LIMIT = 33554432; // 32 << 20, 32MB
  68. public function __construct($buffer)
  69. {
  70. $start = 0;
  71. $end = strlen($buffer);
  72. $this->buffer = $buffer;
  73. $this->buffer_size_after_limit = 0;
  74. $this->buffer_end = $end;
  75. $this->current = $start;
  76. $this->current_limit = $end;
  77. $this->legitimate_message_end = false;
  78. $this->recursion_budget = self::DEFAULT_RECURSION_LIMIT;
  79. $this->recursion_limit = self::DEFAULT_RECURSION_LIMIT;
  80. $this->total_bytes_limit = self::DEFAULT_TOTAL_BYTES_LIMIT;
  81. $this->total_bytes_read = $end - $start;
  82. }
  83. private function advance($amount)
  84. {
  85. $this->current += $amount;
  86. }
  87. private function bufferSize()
  88. {
  89. return $this->buffer_end - $this->current;
  90. }
  91. private function current()
  92. {
  93. return $this->total_bytes_read -
  94. ($this->buffer_end - $this->current +
  95. $this->buffer_size_after_limit);
  96. }
  97. private function recomputeBufferLimits()
  98. {
  99. $this->buffer_end += $this->buffer_size_after_limit;
  100. $closest_limit = min($this->current_limit, $this->total_bytes_limit);
  101. if ($closest_limit < $this->total_bytes_read) {
  102. // The limit position is in the current buffer. We must adjust the
  103. // buffer size accordingly.
  104. $this->buffer_size_after_limit = $this->total_bytes_read -
  105. $closest_limit;
  106. $this->buffer_end -= $this->buffer_size_after_limit;
  107. } else {
  108. $this->buffer_size_after_limit = 0;
  109. }
  110. }
  111. private function consumedEntireMessage()
  112. {
  113. return $this->legitimate_message_end;
  114. }
  115. /**
  116. * Read uint32 into $var. Advance buffer with consumed bytes. If the
  117. * contained varint is larger than 32 bits, discard the high order bits.
  118. * @param $var.
  119. */
  120. public function readVarint32(&$var)
  121. {
  122. if (!$this->readVarint64($var)) {
  123. return false;
  124. }
  125. if (PHP_INT_SIZE == 4) {
  126. $var = bcmod($var, 4294967296);
  127. } else {
  128. $var &= 0xFFFFFFFF;
  129. }
  130. // Convert large uint32 to int32.
  131. if ($var > 0x7FFFFFFF) {
  132. if (PHP_INT_SIZE === 8) {
  133. $var = $var | (0xFFFFFFFF << 32);
  134. } else {
  135. $var = bcsub($var, 4294967296);
  136. }
  137. }
  138. $var = intval($var);
  139. return true;
  140. }
  141. /**
  142. * Read Uint64 into $var. Advance buffer with consumed bytes.
  143. * @param $var.
  144. */
  145. public function readVarint64(&$var)
  146. {
  147. $count = 0;
  148. if (PHP_INT_SIZE == 4) {
  149. $high = 0;
  150. $low = 0;
  151. $b = 0;
  152. do {
  153. if ($this->current === $this->buffer_end) {
  154. return false;
  155. }
  156. if ($count === self::MAX_VARINT_BYTES) {
  157. return false;
  158. }
  159. $b = ord($this->buffer[$this->current]);
  160. $bits = 7 * $count;
  161. if ($bits >= 32) {
  162. $high |= (($b & 0x7F) << ($bits - 32));
  163. } else if ($bits > 25){
  164. // $bits is 28 in this case.
  165. $low |= (($b & 0x7F) << 28);
  166. $high = ($b & 0x7F) >> 4;
  167. } else {
  168. $low |= (($b & 0x7F) << $bits);
  169. }
  170. $this->advance(1);
  171. $count += 1;
  172. } while ($b & 0x80);
  173. $var = combineInt32ToInt64($high, $low);
  174. } else {
  175. $result = 0;
  176. $shift = 0;
  177. do {
  178. if ($this->current === $this->buffer_end) {
  179. return false;
  180. }
  181. if ($count === self::MAX_VARINT_BYTES) {
  182. return false;
  183. }
  184. $byte = ord($this->buffer[$this->current]);
  185. $result |= ($byte & 0x7f) << $shift;
  186. $shift += 7;
  187. $this->advance(1);
  188. $count += 1;
  189. } while ($byte > 0x7f);
  190. $var = $result;
  191. }
  192. return true;
  193. }
  194. /**
  195. * Read int into $var. If the result is larger than the largest integer, $var
  196. * will be -1. Advance buffer with consumed bytes.
  197. * @param $var.
  198. */
  199. public function readVarintSizeAsInt(&$var)
  200. {
  201. if (!$this->readVarint64($var)) {
  202. return false;
  203. }
  204. $var = (int)$var;
  205. return true;
  206. }
  207. /**
  208. * Read 32-bit unsiged integer to $var. If the buffer has less than 4 bytes,
  209. * return false. Advance buffer with consumed bytes.
  210. * @param $var.
  211. */
  212. public function readLittleEndian32(&$var)
  213. {
  214. $data = null;
  215. if (!$this->readRaw(4, $data)) {
  216. return false;
  217. }
  218. $var = unpack('V', $data);
  219. $var = $var[1];
  220. return true;
  221. }
  222. /**
  223. * Read 64-bit unsiged integer to $var. If the buffer has less than 8 bytes,
  224. * return false. Advance buffer with consumed bytes.
  225. * @param $var.
  226. */
  227. public function readLittleEndian64(&$var)
  228. {
  229. $data = null;
  230. if (!$this->readRaw(4, $data)) {
  231. return false;
  232. }
  233. $low = unpack('V', $data)[1];
  234. if (!$this->readRaw(4, $data)) {
  235. return false;
  236. }
  237. $high = unpack('V', $data)[1];
  238. if (PHP_INT_SIZE == 4) {
  239. $var = combineInt32ToInt64($high, $low);
  240. } else {
  241. $var = ($high << 32) | $low;
  242. }
  243. return true;
  244. }
  245. /**
  246. * Read tag into $var. Advance buffer with consumed bytes.
  247. * @param $var.
  248. */
  249. public function readTag()
  250. {
  251. if ($this->current === $this->buffer_end) {
  252. // Make sure that it failed due to EOF, not because we hit
  253. // total_bytes_limit, which, unlike normal limits, is not a valid
  254. // place to end a message.
  255. $current_position = $this->total_bytes_read -
  256. $this->buffer_size_after_limit;
  257. if ($current_position >= $this->total_bytes_limit) {
  258. // Hit total_bytes_limit_. But if we also hit the normal limit,
  259. // we're still OK.
  260. $this->legitimate_message_end =
  261. ($this->current_limit === $this->total_bytes_limit);
  262. } else {
  263. $this->legitimate_message_end = true;
  264. }
  265. return 0;
  266. }
  267. $result = 0;
  268. // The larget tag is 2^29 - 1, which can be represented by int32.
  269. $success = $this->readVarint32($result);
  270. if ($success) {
  271. return $result;
  272. } else {
  273. return 0;
  274. }
  275. }
  276. public function readRaw($size, &$buffer)
  277. {
  278. $current_buffer_size = 0;
  279. if ($this->bufferSize() < $size) {
  280. return false;
  281. }
  282. $buffer = substr($this->buffer, $this->current, $size);
  283. $this->advance($size);
  284. return true;
  285. }
  286. /* Places a limit on the number of bytes that the stream may read, starting
  287. * from the current position. Once the stream hits this limit, it will act
  288. * like the end of the input has been reached until popLimit() is called.
  289. *
  290. * As the names imply, the stream conceptually has a stack of limits. The
  291. * shortest limit on the stack is always enforced, even if it is not the top
  292. * limit.
  293. *
  294. * The value returned by pushLimit() is opaque to the caller, and must be
  295. * passed unchanged to the corresponding call to popLimit().
  296. *
  297. * @param integer $byte_limit
  298. * @throws Exception Fail to push limit.
  299. */
  300. public function pushLimit($byte_limit)
  301. {
  302. // Current position relative to the beginning of the stream.
  303. $current_position = $this->current();
  304. $old_limit = $this->current_limit;
  305. // security: byte_limit is possibly evil, so check for negative values
  306. // and overflow.
  307. if ($byte_limit >= 0 &&
  308. $byte_limit <= PHP_INT_MAX - $current_position &&
  309. $byte_limit <= $this->current_limit - $current_position) {
  310. $this->current_limit = $current_position + $byte_limit;
  311. $this->recomputeBufferLimits();
  312. } else {
  313. throw new GPBDecodeException("Fail to push limit.");
  314. }
  315. return $old_limit;
  316. }
  317. /* The limit passed in is actually the *old* limit, which we returned from
  318. * PushLimit().
  319. *
  320. * @param integer $byte_limit
  321. */
  322. public function popLimit($byte_limit)
  323. {
  324. $this->current_limit = $byte_limit;
  325. $this->recomputeBufferLimits();
  326. // We may no longer be at a legitimate message end. ReadTag() needs to
  327. // be called again to find out.
  328. $this->legitimate_message_end = false;
  329. }
  330. public function incrementRecursionDepthAndPushLimit(
  331. $byte_limit, &$old_limit, &$recursion_budget)
  332. {
  333. $old_limit = $this->pushLimit($byte_limit);
  334. $recursion_limit = --$this->recursion_limit;
  335. }
  336. public function decrementRecursionDepthAndPopLimit($byte_limit)
  337. {
  338. $result = $this->consumedEntireMessage();
  339. $this->popLimit($byte_limit);
  340. ++$this->recursion_budget;
  341. return $result;
  342. }
  343. public function bytesUntilLimit()
  344. {
  345. if ($this->current_limit === PHP_INT_MAX) {
  346. return -1;
  347. }
  348. return $this->current_limit - $this->current;
  349. }
  350. }