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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | <?php namespace DrSlump\Protobuf\Codec\Binary; /** * Implements reading primitives for Protobuf binary streams * * Important: There are no checks in place for overflows, so you must * be aware of PHP's integer and floating point limits. * * @note Protobuf uses little-endian order */ class Reader { /** @var resource */ protected $_fd; /** * Create a new reader from a file descriptor or a string of bytes * * @param resource|string $fdOrString */ public function __construct($fdOrString) { if (is_resource($fdOrString)) { $this->_fd = $fdOrString; } else { // @todo Could this be faster by using a custom String wrapper? $this->_fd = fopen('data://text/plain,' . urlencode($fdOrString), 'rb'); } } public function __destruct() { fclose($this->_fd); } /** * Obtain a number of bytes from the string * * @throws \RuntimeException * @param int $length * @return string */ public function read($length) { // Protect against 0 byte reads when an EOF if ($length < 1) return ''; $bytes = fread($this->_fd, $length); if (FALSE === $bytes) { throw new \RuntimeException('Failed to read ' . $length . ' bytes'); } return $bytes; } /** * Check if we have reached the end of the stream * * @return bool */ public function eof() { return feof($this->_fd); } /** * Obtain the current position in the stream * * @return int */ public function pos() { return ftell($this->_fd); } /** * Obtain a byte * * @return int */ public function byte() { return ord($this->read(1)); } /** * Decode a varint * * @return int */ public function varint() { $result = $shift = 0; do { $byte = $this->byte(); $result |= ($byte & 0x7f) << $shift; $shift += 7; } while ($byte > 0x7f); return $result; } /** * Decodes a zigzag integer of the given bits * * @param int $bits - Either 32 or 64 */ public function zigzag() { $number = $this->varint(); return ($number >> 1) ^ (-($number & 1)); } /** * Decode a fixed 32bit integer with sign * * @return int */ public function sFixed32() { $bytes = $this->read(4); if ($this->isBigEndian()) { $bytes = strrev($bytes); } list(, $result) = unpack('l', $bytes); return $result; } /** * Decode a fixed 32bit integer without sign * * @return int */ public function fixed32() { $bytes = $this->read(4); if (PHP_INT_SIZE < 8) { list(, $lo, $hi) = unpack('v*', $bytes); $result = $hi << 16 | $lo; } else { list(, $result) = unpack('V*', $bytes); } return $result; } /** * Decode a fixed 62bit integer with sign * * @return int */ public function sFixed64() { $bytes = $this->read(8); list(, $lo0, $lo1, $hi0, $hi1) = unpack('v*', $bytes); return ($hi1 << 16 | $hi0) << 32 | ($lo1 << 16 | $lo0); } /** * Decode a fixed 62bit integer without sign * * @return int */ public function fixed64() { return $this->sFixed64(); } /** * Decode a 32bit float * * @return float */ public function float() { $bytes = $this->read(4); if ($this->isBigEndian()) { $bytes = strrev($bytes); } list(, $result) = unpack('f', $bytes); return $result; } /** * Decode a 64bit double * * @return float */ public function double() { $bytes = $this->read(8); if ($this->isBigEndian()) { $bytes = strrev($bytes); } list(, $result) = unpack('d', $bytes); return $result; } /** * Check if the current architecture is Big Endian * * @return bool */ public function isBigEndian() { static $endianness; if (NULL === $endianness) { list(,$result) = unpack('L', pack('V', 1)); $endianness = $result !== 1; } return $endianness; } } |