You need to sign in or sign up before continuing.
CodedOutputStream.php 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php

// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace Google\Protobuf\Internal;

35
class CodedOutputStream
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
{

    private $buffer;
    private $buffer_size;
    private $current;

    const MAX_VARINT64_BYTES = 10;

    public function __construct($size)
    {
        $this->current = 0;
        $this->buffer_size = $size;
        $this->buffer = str_repeat(chr(0), $this->buffer_size);
    }

    public function getData()
    {
        return $this->buffer;
    }

56
    public function writeVarint32($value, $trim)
57
    {
58
        $bytes = str_repeat(chr(0), self::MAX_VARINT64_BYTES);
59
        $size = self::writeVarintToArray($value, $bytes, $trim);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        return $this->writeRaw($bytes, $size);
    }

    public function writeVarint64($value)
    {
        $bytes = str_repeat(chr(0), self::MAX_VARINT64_BYTES);
        $size = self::writeVarintToArray($value, $bytes);
        return $this->writeRaw($bytes, $size);
    }

    public function writeLittleEndian32($value)
    {
        $bytes = str_repeat(chr(0), 4);
        $size = self::writeLittleEndian32ToArray($value, $bytes);
        return $this->writeRaw($bytes, $size);
    }

    public function writeLittleEndian64($value)
    {
        $bytes = str_repeat(chr(0), 8);
        $size = self::writeLittleEndian64ToArray($value, $bytes);
        return $this->writeRaw($bytes, $size);
    }

    public function writeTag($tag)
    {
86
        return $this->writeVarint32($tag, true);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    }

    public function writeRaw($data, $size)
    {
        if ($this->buffer_size < $size) {
            trigger_error("Output stream doesn't have enough buffer.");
            return false;
        }

        for ($i = 0; $i < $size; $i++) {
            $this->buffer[$this->current] = $data[$i];
            $this->current++;
            $this->buffer_size--;
        }
        return true;
    }

104
    public static function writeVarintToArray($value, &$buffer, $trim = false)
105 106
    {
        $current = 0;
107 108 109 110

        $high = 0;
        $low = 0;
        if (PHP_INT_SIZE == 4) {
111
            GPBUtil::divideInt64ToInt32($value, $high, $low, $trim);
112
        } else {
113
            $low = $value;
114
        }
115

116
        while (($low >= 0x80 || $low < 0) || $high != 0) {
117
            $buffer[$current] = chr($low | 0x80);
118
            $value = ($value >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7));
119 120 121
            $carry = ($high & 0x7F) << ((PHP_INT_SIZE << 3) - 7);
            $high = ($high >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7));
            $low = (($low >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7)) | $carry);
122 123
            $current++;
        }
124
        $buffer[$current] = chr($low);
125 126 127 128 129 130 131 132 133 134 135 136 137 138
        return $current + 1;
    }

    private static function writeLittleEndian32ToArray($value, &$buffer)
    {
        $buffer[0] = chr($value & 0x000000FF);
        $buffer[1] = chr(($value >> 8) & 0x000000FF);
        $buffer[2] = chr(($value >> 16) & 0x000000FF);
        $buffer[3] = chr(($value >> 24) & 0x000000FF);
        return 4;
    }

    private static function writeLittleEndian64ToArray($value, &$buffer)
    {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        $high = 0;
        $low = 0;
        if (PHP_INT_SIZE == 4) {
            GPBUtil::divideInt64ToInt32($value, $high, $low);
        } else {
            $low = $value & 0xFFFFFFFF;
            $high = ($value >> 32) & 0xFFFFFFFF;
        }

        $buffer[0] = chr($low & 0x000000FF);
        $buffer[1] = chr(($low >> 8) & 0x000000FF);
        $buffer[2] = chr(($low >> 16) & 0x000000FF);
        $buffer[3] = chr(($low >> 24) & 0x000000FF);
        $buffer[4] = chr($high & 0x000000FF);
        $buffer[5] = chr(($high >> 8) & 0x000000FF);
        $buffer[6] = chr(($high >> 16) & 0x000000FF);
        $buffer[7] = chr(($high >> 24) & 0x000000FF);
156 157
        return 8;
    }
158

159
}