CodedOutputStream.php 5.2 KB

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