Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: third_party/protobuf/php/src/Google/Protobuf/Internal/InputStream.php

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <?php
2
3 // Protocol Buffers - Google's data interchange format
4 // Copyright 2008 Google Inc. All rights reserved.
5 // https://developers.google.com/protocol-buffers/
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 namespace Google\Protobuf\Internal;
34
35 use Google\Protobuf\Internal\Uint64;
36
37 function combineInt32ToInt64($high, $low)
38 {
39 $isNeg = $high < 0;
40 if ($isNeg) {
41 $high = ~$high;
42 $low = ~$low;
43 $low++;
44 if (!$low) {
45 $high++;
46 }
47 }
48 $result = bcadd(bcmul($high, 4294967296), $low);
49 if ($isNeg) {
50 $result = bcsub(0, $result);
51 }
52 return $result;
53 }
54
55 class InputStream
56 {
57
58 private $buffer;
59 private $buffer_size_after_limit;
60 private $buffer_end;
61 private $current;
62 private $current_limit;
63 private $legitimate_message_end;
64 private $recursion_budget;
65 private $recursion_limit;
66 private $total_bytes_limit;
67 private $total_bytes_read;
68
69 const MAX_VARINT_BYTES = 10;
70 const MAX_VARINT32_BYTES = 5;
71 const DEFAULT_RECURSION_LIMIT = 100;
72 const DEFAULT_TOTAL_BYTES_LIMIT = 33554432; // 32 << 20, 32MB
73
74 public function __construct($buffer)
75 {
76 $start = 0;
77 $end = strlen($buffer);
78 $this->buffer = $buffer;
79 $this->buffer_size_after_limit = 0;
80 $this->buffer_end = $end;
81 $this->current = $start;
82 $this->current_limit = $end;
83 $this->legitimate_message_end = false;
84 $this->recursion_budget = self::DEFAULT_RECURSION_LIMIT;
85 $this->recursion_limit = self::DEFAULT_RECURSION_LIMIT;
86 $this->total_bytes_limit = self::DEFAULT_TOTAL_BYTES_LIMIT;
87 $this->total_bytes_read = $end - $start;
88 }
89
90 private function advance($amount)
91 {
92 $this->current += $amount;
93 }
94
95 private function bufferSize()
96 {
97 return $this->buffer_end - $this->current;
98 }
99
100 private function current()
101 {
102 return $this->total_bytes_read -
103 ($this->buffer_end - $this->current +
104 $this->buffer_size_after_limit);
105 }
106
107 private function recomputeBufferLimits()
108 {
109 $this->buffer_end += $this->buffer_size_after_limit;
110 $closest_limit = min($this->current_limit, $this->total_bytes_limit);
111 if ($closest_limit < $this->total_bytes_read) {
112 // The limit position is in the current buffer. We must adjust the
113 // buffer size accordingly.
114 $this->buffer_size_after_limit = $this->total_bytes_read -
115 $closest_limit;
116 $this->buffer_end -= $this->buffer_size_after_limit;
117 } else {
118 $this->buffer_size_after_limit = 0;
119 }
120 }
121
122 private function consumedEntireMessage()
123 {
124 return $this->legitimate_message_end;
125 }
126
127 /**
128 * Read uint32 into $var. Advance buffer with consumed bytes. If the
129 * contained varint is larger than 32 bits, discard the high order bits.
130 * @param $var.
131 */
132 public function readVarint32(&$var)
133 {
134 if (!$this->readVarint64($var)) {
135 return false;
136 }
137
138 if (PHP_INT_SIZE == 4) {
139 $var = bcmod($var, 4294967296);
140 } else {
141 $var &= 0xFFFFFFFF;
142 }
143
144 // Convert large uint32 to int32.
145 if ($var > 0x7FFFFFFF) {
146 if (PHP_INT_SIZE === 8) {
147 $var = $var | (0xFFFFFFFF << 32);
148 } else {
149 $var = bcsub($var, 4294967296);
150 }
151 }
152
153 $var = intval($var);
154 return true;
155 }
156
157 /**
158 * Read Uint64 into $var. Advance buffer with consumed bytes.
159 * @param $var.
160 */
161 public function readVarint64(&$var)
162 {
163 $high = 0;
164 $low = 0;
165 $count = 0;
166 $b = 0;
167
168 do {
169 if ($this->current === $this->buffer_end) {
170 return false;
171 }
172 if ($count === self::MAX_VARINT_BYTES) {
173 return false;
174 }
175 $b = ord($this->buffer[$this->current]);
176 $bits = 7 * $count;
177 if ($bits >= 32) {
178 $high |= (($b & 0x7F) << ($bits - 32));
179 } else if ($bits > 25){
180 $high_bits = $bits - 25;
181 $low = ($low | (($b & 0x7F) << $bits)) & (int) 0xFFFFFFFF;
182 $high = $b & ((0x1 << $high_bits) -1);
183 } else {
184 $low |= (($b & 0x7F) << $bits);
185 }
186
187 $this->advance(1);
188 $count += 1;
189 } while ($b & 0x80);
190
191 if (PHP_INT_SIZE == 4) {
192 $var = combineInt32ToInt64($high, $low);
193 } else {
194 $var = ($high & 0xFFFFFFFF) << 32 |
195 ($low & 0xFFFFFFFF);
196 }
197 return true;
198 }
199
200 /**
201 * Read int into $var. If the result is larger than the largest integer, $va r
202 * will be -1. Advance buffer with consumed bytes.
203 * @param $var.
204 */
205 public function readVarintSizeAsInt(&$var)
206 {
207 if (!$this->readVarint64($var)) {
208 return false;
209 }
210 $var = (int)$var;
211 return true;
212 }
213
214 /**
215 * Read 32-bit unsiged integer to $var. If the buffer has less than 4 bytes,
216 * return false. Advance buffer with consumed bytes.
217 * @param $var.
218 */
219 public function readLittleEndian32(&$var)
220 {
221 $data = null;
222 if (!$this->readRaw(4, $data)) {
223 return false;
224 }
225 $var = unpack('V', $data);
226 $var = $var[1];
227 return true;
228 }
229
230 /**
231 * Read 64-bit unsiged integer to $var. If the buffer has less than 8 bytes,
232 * return false. Advance buffer with consumed bytes.
233 * @param $var.
234 */
235 public function readLittleEndian64(&$var)
236 {
237 $data = null;
238 if (!$this->readRaw(4, $data)) {
239 return false;
240 }
241 $low = unpack('V', $data)[1];
242 if (!$this->readRaw(4, $data)) {
243 return false;
244 }
245 $high = unpack('V', $data)[1];
246 if (PHP_INT_SIZE == 4) {
247 $var = combineInt32ToInt64($high, $low);
248 } else {
249 $var = ($high << 32) | $low;
250 }
251 return true;
252 }
253
254 /**
255 * Read tag into $var. Advance buffer with consumed bytes.
256 * @param $var.
257 */
258 public function readTag()
259 {
260 if ($this->current === $this->buffer_end) {
261 // Make sure that it failed due to EOF, not because we hit
262 // total_bytes_limit, which, unlike normal limits, is not a valid
263 // place to end a message.
264 $current_position = $this->total_bytes_read -
265 $this->buffer_size_after_limit;
266 if ($current_position >= $this->total_bytes_limit) {
267 // Hit total_bytes_limit_. But if we also hit the normal limit,
268 // we're still OK.
269 $this->legitimate_message_end =
270 ($this->current_limit === $this->total_bytes_limit);
271 } else {
272 $this->legitimate_message_end = true;
273 }
274 return 0;
275 }
276
277 $result = 0;
278 // The larget tag is 2^29 - 1, which can be represented by int32.
279 $success = $this->readVarint32($result);
280 if ($success) {
281 return $result;
282 } else {
283 return 0;
284 }
285 }
286
287 public function readRaw($size, &$buffer)
288 {
289 $current_buffer_size = 0;
290 if ($this->bufferSize() < $size) {
291 return false;
292 }
293
294 $buffer = substr($this->buffer, $this->current, $size);
295 $this->advance($size);
296
297 return true;
298 }
299
300 /* Places a limit on the number of bytes that the stream may read, starting
301 * from the current position. Once the stream hits this limit, it will act
302 * like the end of the input has been reached until popLimit() is called.
303 *
304 * As the names imply, the stream conceptually has a stack of limits. The
305 * shortest limit on the stack is always enforced, even if it is not the top
306 * limit.
307 *
308 * The value returned by pushLimit() is opaque to the caller, and must be
309 * passed unchanged to the corresponding call to popLimit().
310 *
311 * @param integer $byte_limit
312 */
313 public function pushLimit($byte_limit)
314 {
315 // Current position relative to the beginning of the stream.
316 $current_position = $this->current();
317 $old_limit = $this->current_limit;
318
319 // security: byte_limit is possibly evil, so check for negative values
320 // and overflow.
321 if ($byte_limit >= 0 && $byte_limit <= PHP_INT_MAX - $current_position) {
322 $this->current_limit = $current_position + $byte_limit;
323 } else {
324 // Negative or overflow.
325 $this->current_limit = PHP_INT_MAX;
326 }
327
328 // We need to enforce all limits, not just the new one, so if the previo us
329 // limit was before the new requested limit, we continue to enforce the
330 // previous limit.
331 $this->current_limit = min($this->current_limit, $old_limit);
332
333 $this->recomputeBufferLimits();
334 return $old_limit;
335 }
336
337 /* The limit passed in is actually the *old* limit, which we returned from
338 * PushLimit().
339 *
340 * @param integer $byte_limit
341 */
342 public function popLimit($byte_limit)
343 {
344 $this->current_limit = $byte_limit;
345 $this->recomputeBufferLimits();
346 // We may no longer be at a legitimate message end. ReadTag() needs to
347 // be called again to find out.
348 $this->legitimate_message_end = false;
349 }
350
351 public function incrementRecursionDepthAndPushLimit(
352 $byte_limit, &$old_limit, &$recursion_budget)
353 {
354 $old_limit = $this->pushLimit($byte_limit);
355 $recursion_limit = --$this->recursion_limit;
356 }
357
358 public function decrementRecursionDepthAndPopLimit($byte_limit)
359 {
360 $result = $this->consumedEntireMessage();
361 $this->popLimit($byte_limit);
362 ++$this->recursion_budget;
363 return $result;
364 }
365
366 public function bytesUntilLimit()
367 {
368 if ($this->current_limit === PHP_INT_MAX) {
369 return -1;
370 }
371 return $this->current_limit - $this->current;
372 }
373 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698