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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | <?php namespace DrSlump\Protobuf\Codec\Binary; /** * Implements writing primitives for Protobuf binary streams * * @note Protobuf uses little-endian order */ class Writer { /** @var resource */ protected $_fd; public function __construct() { $this->_fd = fopen('php://memory', 'wb'); } public function __destruct() { fclose($this->_fd); } /** * Get the current bytes in the stream * * @return string */ public function getBytes() { fseek($this->_fd, 0, SEEK_SET); return stream_get_contents($this->_fd); } /** * Store the given bytes in the stream * * @throws \RuntimeException * @param string $bytes * @param int $length */ public function write($bytes, $length = null) { if ($length === NULL) { $length = strlen($bytes); } $written = fwrite($this->_fd, $bytes, $length); if ($written !== $length) { throw new \RuntimeException('Failed to write ' . $length . ' bytes'); } } /** * Store a single byte * * @param int $value */ public function byte($value) { $this->write(chr($value), 1); } /** * Store an integer encoded as varint * * @throws \OutOfBoundsException * @param int $value */ public function varint($value) { // Small values do not need to be encoded if ($value >= 0 && $value < 0x80) { $this->byte($value); return; } // Build an array of bytes with the encoded values if ($value > 0) { $values = array(); while ($value > 0) { $values[] = 0x80 | ($value & 0x7f); $value = $value >> 7; } } else if (function_exists('gmp_init')) { $value = PHP_INT_SIZE < 8 ? gmp_and($value, '0x0ffffffffffffffff') : sprintf('%u', $value); $values = $this->varint_gmp($value); } else if (PHP_INT_SIZE < 8) { throw new \OutOfBoundsException( "PHP versions compiled with 32bit integers can only support negative integer encoding with GMP extension ($value was given)" ); } else if (function_exists('bccomp')) { $value = sprintf('%u', $value); $values = $this->varint_bc($value); } else { throw new \OutOfBoundsException("Varints of negative integers are only supported with GMP or BC big integers PHP extensions ($value was given)"); } // Remove the MSB flag from the last byte $values[count($values)-1] &= 0x7f; // Convert the byte sized ints to actual bytes in a string //$bytes = implode('', array_map('chr', $values)); $bytes = call_user_func_array('pack', array_merge(array('C*'), $values));; $this->write($bytes); } public function varint_gmp($value) { static $x00, $x7f, $x80; if (NULL === $x00) { $x00 = \gmp_init(0x00); $x7f = \gmp_init(0x7f); $x80 = \gmp_init(0x80); } $values = array(); while (\gmp_cmp($value, $x00) > 0) { $values[] = \gmp_intval(\gmp_and($value, $x7f)) | 0x80; $value = \gmp_div_q($value, $x80); } return $values; } public function varint_bc($value) { $values = array(); while (\bccomp($value, 0, 0) > 0) { // Get the last 7bits of the number $bin = ''; $dec = $value; do { $rest = bcmod($dec, 2); $dec = bcdiv($dec, 2, 0); $bin = $rest . $bin; } while ($dec > 0 && strlen($bin) < 7); // Pack as a decimal and apply the flag $values[] = intval($bin, 2) | 0x80; $value = bcdiv($value, 0x80, 0); } return $values; } /** * Encodes an integer with zigzag * * @param int $value * @param int $base Either 32 or 64 bits */ public function zigzag($value, $base = 32) { $value = ($value << 1) ^ ($value >> $base-1); $this->varint($value); } /** * Encode an integer as a fixed of 32bits with sign * * @param int $value */ public function sFixed32($value) { $bytes = pack('l*', $value); if ($this->isBigEndian()) { $bytes = strrev($bytes); } $this->write($bytes, 4); } /** * Encode an integer as a fixed of 32bits without sign * * @param int $value */ public function fixed32($value) { $bytes = pack('V*', $value); $this->write($bytes, 4); } /** * Encode an integer as a fixed of 64bits with sign * * @param int $value */ public function sFixed64($value) { if ($value >= 0) { $this->fixed64($value); } else if (function_exists('gmp_init')) { $this->sFixed64_gmp($value); } else if (function_exists('bcadd')) { $this->sFixed64_bc($value); } else { throw new \OutOfBoundsException("The signed Fixed64 type with negative integers is only supported with GMP or BC big integers PHP extensions ($value was given)"); } } public function sFixed64_gmp($value) { static $xff, $x100; if (NULL === $xff) { $xff = gmp_init(0xff); $x100 = gmp_init(0x100); } $value = PHP_INT_SIZE < 8 ? gmp_and($value, '0x0ffffffffffffffff') : gmp_init(sprintf('%u', $value)); $bytes = ''; for ($i=0; $i<8; $i++) { $bytes .= chr(gmp_intval(gmp_and($value, $xff))); $value = gmp_div_q($value, $x100); } $this->write($bytes); } public function sFixed64_bc($value) { if (PHP_INT_SIZE < 8) { throw new \OutOfBoundsException( "PHP versions compiled with 32bit integers can only support negative integer encoding with GMP extension ($value was given)" ); } $value = sprintf('%u', $value); $bytes = ''; for ($i=0; $i<8; $i++) { // Get the last 8bits of the number $bin = ''; $dec = $value; do { $bin = bcmod($dec, 2) . $bin; $dec = bcdiv($dec, 2, 0); } while (strlen($bin) < 8); // Pack the byte $bytes .= chr(intval($bin, 2)); $value = bcdiv($value, 0x100, 0); } $this->write($bytes); } /** * Encode an integer as a fixed of 64bits without sign * * @param int $value */ public function fixed64($value) { $bytes = pack('V*', $value & 0xffffffff, $value / (0xffffffff+1)); $this->write($bytes, 8); } /** * Encode a number as a 32bit float * * @param float $value */ public function float($value) { $bytes = pack('f*', $value); if ($this->isBigEndian()) { $bytes = strrev($bytes); } $this->write($bytes, 4); } /** * Encode a number as a 64bit double * * @param float $value */ public function double($value) { $bytes = pack('d*', $value); if ($this->isBigEndian()) { $bytes = strrev($bytes); } $this->write($bytes, 8); } /** * Checks 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; } } |