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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | <?php namespace DrSlump\Protobuf\Codec; use DrSlump\Protobuf; class Binary implements Protobuf\CodecInterface { const WIRE_VARINT = 0; const WIRE_FIXED64 = 1; const WIRE_LENGTH = 2; const WIRE_GROUP_START = 3; const WIRE_GROUP_END = 4; const WIRE_FIXED32 = 5; const WIRE_UNKNOWN = -1; /** * @param \DrSlump\Protobuf\Message $message * @return string */ public function encode(Protobuf\Message $message) { return $this->encodeMessage($message); } /** * @param String|Message $message * @param String $data * @return \DrSlump\Protobuf\Message */ public function decode(Protobuf\Message $message, $data) { return $this->decodeMessage($message, $data); } protected function encodeMessage(Protobuf\Message $message) { $writer = new Binary\Writer(); // Get message descriptor $descriptor = Protobuf::getRegistry()->getDescriptor($message); foreach ($descriptor->getFields() as $tag=>$field) { $empty = !$message->_has($tag); if ($field->isRequired() && $empty) { throw new \UnexpectedValueException( 'Message ' . get_class($message) . '\'s field tag ' . $tag . '(' . $field->getName() . ') is required but has no value' ); } // Skip empty fields if ($empty) { continue; } $type = $field->getType(); $wire = $field->isPacked() ? self::WIRE_LENGTH : $this->getWireType($type, null); // Compute key with tag number and wire type $key = $tag << 3 | $wire; $value = $message->_get($tag); if ($field->isRepeated()) { // Packed fields are encoded as a length-delimited stream containing // the concatenated encoding of each value. if ($field->isPacked() && !empty($value)) { $subwriter = new Binary\Writer(); foreach($value as $val) { $this->encodeSimpleType($subwriter, $type, $val); } $data = $subwriter->getBytes(); $writer->varint($key); $writer->varint(strlen($data)); $writer->write($data); } else { foreach($value as $val) { if ($type !== Protobuf::TYPE_MESSAGE) { $writer->varint($key); $this->encodeSimpleType($writer, $type, $val); } else { $writer->varint($key); $data = $this->encodeMessage($val); $writer->varint(strlen($data)); $writer->write($data); } } } } else { if ($type !== Protobuf::TYPE_MESSAGE) { $writer->varint($key); $this->encodeSimpleType($writer, $type, $value); } else { $writer->varint($key); $data = $this->encodeMessage($value); $writer->varint(strlen($data)); $writer->write($data); } } } return $writer->getBytes(); } protected function encodeSimpleType($writer, $type, $value) { switch ($type) { case Protobuf::TYPE_INT32: case Protobuf::TYPE_INT64: case Protobuf::TYPE_UINT64: case Protobuf::TYPE_UINT32: $writer->varint($value); break; case Protobuf::TYPE_SINT32: // ZigZag $writer->zigzag($value, 32); break; case Protobuf::TYPE_SINT64 : // ZigZag $writer->zigzag($value, 64); break; case Protobuf::TYPE_DOUBLE: $writer->double($value); break; case Protobuf::TYPE_FIXED64: $writer->fixed64($value); break; case Protobuf::TYPE_SFIXED64: $writer->sFixed64($value); break; case Protobuf::TYPE_FLOAT: $writer->float($value); break; case Protobuf::TYPE_FIXED32: $writer->fixed32($value); break; case Protobuf::TYPE_SFIXED32: $writer->sFixed32($value); break; case Protobuf::TYPE_BOOL: $writer->varint($value ? 1 : 0); break; case Protobuf::TYPE_STRING: case Protobuf::TYPE_BYTES: $writer->varint(strlen($value)); $writer->write($value); break; case Protobuf::TYPE_MESSAGE: // Messages are not supported in this method return null; case Protobuf::TYPE_ENUM: $writer->varint($value); break; default: throw new \Exception('Unknown field type ' . $type); } } protected function decodeMessage(\DrSlump\Protobuf\Message $message, $data) { /** @var $message \DrSlump\Protobuf\Message */ /** @var $descriptor \DrSlump\Protobuf\Descriptor */ // Create a binary reader for the data $reader = new Protobuf\Codec\Binary\Reader($data); // Get message descriptor $descriptor = Protobuf::getRegistry()->getDescriptor($message); while (!$reader->eof()) { // Get initial varint with tag number and wire type $key = $reader->varint(); if ($reader->eof()) break; $wire = $key & 0x7; $tag = $key >> 3; // Find the matching field for the tag number $field = $descriptor->getField($tag); if (!$field) { $data = $this->decodeUnknown($reader, $wire); $unknown = new Binary\Unknown($tag, $wire, $data); $message->addUnknown($unknown); continue; } $type = $field->getType(); // Check if we are dealing with a packed stream, we cannot rely on the packed // flag of the message since we cannot be certain if the creator of the message // was using it. if ($wire === self::WIRE_LENGTH && $field->isRepeated() && $this->isPackable($type)) { $length = $reader->varint(); $until = $reader->pos() + $length; while ($reader->pos() < $until) { $item = $this->decodeSimpleType($reader, $type, self::WIRE_VARINT); $message->_add($tag, $item); } } else { // Assert wire and type match $this->assertWireType($wire, $type); // Check if it's a sub-message if ($type === Protobuf::TYPE_MESSAGE) { $submessage = $field->getReference(); $submessage = new $submessage; $length = $this->decodeSimpleType($reader, Protobuf::TYPE_INT64, self::WIRE_VARINT); $data = $reader->read($length); $value = $this->decodeMessage($submessage, $data); } else { $value = $this->decodeSimpleType($reader, $type, $wire); } // Support non-packed repeated fields if ($field->isRepeated()) { $message->_add($tag, $value); } else { $message->_set($tag, $value); } } } return $message; } protected function isPackable($type) { static $packable = array( Protobuf::TYPE_INT64, Protobuf::TYPE_UINT64, Protobuf::TYPE_INT32, Protobuf::TYPE_UINT32, Protobuf::TYPE_SINT32, Protobuf::TYPE_SINT64, Protobuf::TYPE_DOUBLE, Protobuf::TYPE_FIXED64, Protobuf::TYPE_SFIXED64, Protobuf::TYPE_FLOAT, Protobuf::TYPE_FIXED32, Protobuf::TYPE_SFIXED32, Protobuf::TYPE_BOOL, Protobuf::TYPE_ENUM ); return in_array($type, $packable); } protected function decodeUnknown($reader, $wire) { switch ($wire) { case self::WIRE_VARINT: return $reader->varint(); case self::WIRE_LENGTH: $length = $reader->varint(); return $reader->read($length); case self::WIRE_FIXED32: return $reader->fixed32(); case self::WIRE_FIXED64: return $reader->fixed64(); case self::WIRE_GROUP_START: case self::WIRE_GROUP_END: throw new \RuntimeException('Groups are deprecated in Protocol Buffers and unsupported by this library'); default: throw new \RuntimeException('Unsupported wire type (' . $wire . ') while consuming unknown field'); } } protected function assertWireType($wire, $type) { $expected = $this->getWireType($type, $wire); if ($wire !== $expected) { throw new \RuntimeException("Expected wire type $expected but got $wire for type $type"); } } protected function getWireType($type, $default) { switch ($type) { case Protobuf::TYPE_INT32: case Protobuf::TYPE_INT64: case Protobuf::TYPE_UINT32: case Protobuf::TYPE_UINT64: case Protobuf::TYPE_SINT32: case Protobuf::TYPE_SINT64: case Protobuf::TYPE_BOOL: case Protobuf::TYPE_ENUM: return self::WIRE_VARINT; case Protobuf::TYPE_FIXED64: case Protobuf::TYPE_SFIXED64: case Protobuf::TYPE_DOUBLE: return self::WIRE_FIXED64; case Protobuf::TYPE_STRING: case Protobuf::TYPE_BYTES: case Protobuf::TYPE_MESSAGE: return self::WIRE_LENGTH; case Protobuf::TYPE_FIXED32: case Protobuf::TYPE_SFIXED32: case Protobuf::TYPE_FLOAT: return self::WIRE_FIXED32; default: // Unknown fields just return the reported wire type return $default; } } protected function decodeSimpleType($reader |