OLD | NEW |
(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 class OutputStream |
| 36 { |
| 37 |
| 38 private $buffer; |
| 39 private $buffer_size; |
| 40 private $current; |
| 41 |
| 42 const MAX_VARINT32_BYTES = 5; |
| 43 const MAX_VARINT64_BYTES = 10; |
| 44 |
| 45 public function __construct($size) |
| 46 { |
| 47 $this->current = 0; |
| 48 $this->buffer_size = $size; |
| 49 $this->buffer = str_repeat(chr(0), $this->buffer_size); |
| 50 } |
| 51 |
| 52 public function getData() |
| 53 { |
| 54 return $this->buffer; |
| 55 } |
| 56 |
| 57 public function writeVarint32($value) |
| 58 { |
| 59 $bytes = str_repeat(chr(0), self::MAX_VARINT32_BYTES); |
| 60 $size = self::writeVarintToArray($value, $bytes, true); |
| 61 return $this->writeRaw($bytes, $size); |
| 62 } |
| 63 |
| 64 public function writeVarint64($value) |
| 65 { |
| 66 $bytes = str_repeat(chr(0), self::MAX_VARINT64_BYTES); |
| 67 $size = self::writeVarintToArray($value, $bytes); |
| 68 return $this->writeRaw($bytes, $size); |
| 69 } |
| 70 |
| 71 public function writeLittleEndian32($value) |
| 72 { |
| 73 $bytes = str_repeat(chr(0), 4); |
| 74 $size = self::writeLittleEndian32ToArray($value, $bytes); |
| 75 return $this->writeRaw($bytes, $size); |
| 76 } |
| 77 |
| 78 public function writeLittleEndian64($value) |
| 79 { |
| 80 $bytes = str_repeat(chr(0), 8); |
| 81 $size = self::writeLittleEndian64ToArray($value, $bytes); |
| 82 return $this->writeRaw($bytes, $size); |
| 83 } |
| 84 |
| 85 public function writeTag($tag) |
| 86 { |
| 87 return $this->writeVarint32($tag); |
| 88 } |
| 89 |
| 90 public function writeRaw($data, $size) |
| 91 { |
| 92 if ($this->buffer_size < $size) { |
| 93 trigger_error("Output stream doesn't have enough buffer."); |
| 94 return false; |
| 95 } |
| 96 |
| 97 for ($i = 0; $i < $size; $i++) { |
| 98 $this->buffer[$this->current] = $data[$i]; |
| 99 $this->current++; |
| 100 $this->buffer_size--; |
| 101 } |
| 102 return true; |
| 103 } |
| 104 |
| 105 private static function writeVarintToArray($value, &$buffer, $trim = false) |
| 106 { |
| 107 $current = 0; |
| 108 |
| 109 $high = 0; |
| 110 $low = 0; |
| 111 if (PHP_INT_SIZE == 4) { |
| 112 GPBUtil::divideInt64ToInt32($value, $high, $low, $trim); |
| 113 } else { |
| 114 if ($trim) { |
| 115 $low = $value & 0xFFFFFFFF; |
| 116 } else { |
| 117 $low = $value; |
| 118 } |
| 119 } |
| 120 |
| 121 while ($low >= 0x80 || $low < 0) { |
| 122 $buffer[$current] = chr($low | 0x80); |
| 123 $value = ($value >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7)); |
| 124 $carry = ($high & 0x7F) << ((PHP_INT_SIZE << 3) - 7); |
| 125 $high = ($high >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7)); |
| 126 $low = (($low >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7)) | $carry)
; |
| 127 $current++; |
| 128 } |
| 129 $buffer[$current] = chr($low); |
| 130 return $current + 1; |
| 131 } |
| 132 |
| 133 private static function writeLittleEndian32ToArray($value, &$buffer) |
| 134 { |
| 135 $buffer[0] = chr($value & 0x000000FF); |
| 136 $buffer[1] = chr(($value >> 8) & 0x000000FF); |
| 137 $buffer[2] = chr(($value >> 16) & 0x000000FF); |
| 138 $buffer[3] = chr(($value >> 24) & 0x000000FF); |
| 139 return 4; |
| 140 } |
| 141 |
| 142 private static function writeLittleEndian64ToArray($value, &$buffer) |
| 143 { |
| 144 $high = 0; |
| 145 $low = 0; |
| 146 if (PHP_INT_SIZE == 4) { |
| 147 GPBUtil::divideInt64ToInt32($value, $high, $low); |
| 148 } else { |
| 149 $low = $value & 0xFFFFFFFF; |
| 150 $high = ($value >> 32) & 0xFFFFFFFF; |
| 151 } |
| 152 |
| 153 $buffer[0] = chr($low & 0x000000FF); |
| 154 $buffer[1] = chr(($low >> 8) & 0x000000FF); |
| 155 $buffer[2] = chr(($low >> 16) & 0x000000FF); |
| 156 $buffer[3] = chr(($low >> 24) & 0x000000FF); |
| 157 $buffer[4] = chr($high & 0x000000FF); |
| 158 $buffer[5] = chr(($high >> 8) & 0x000000FF); |
| 159 $buffer[6] = chr(($high >> 16) & 0x000000FF); |
| 160 $buffer[7] = chr(($high >> 24) & 0x000000FF); |
| 161 return 8; |
| 162 } |
| 163 |
| 164 } |
OLD | NEW |