PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-listings-and-ads/vendor/google/protobuf/src/Google/Protobuf/Internal/CodedInputStream.php

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