| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace Google\FlatBuffers; |
| 23 |
|
| 24 |
final class FlatbufferBuilder |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public $bb; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
protected $space; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
protected $minalign = 1; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected $vtable; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected $vtable_in_use = 0; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected $nested = false; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected $object_start; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected $vtables = array(); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected $num_vtables = 0; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected $vector_num_elems = 0; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
protected $force_defaults = false; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function __construct($initial_size) |
| 90 |
{ |
| 91 |
if ($initial_size <= 0) { |
| 92 |
$initial_size = 1; |
| 93 |
} |
| 94 |
$this->space = $initial_size; |
| 95 |
$this->bb = $this->newByteBuffer($initial_size); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private function newByteBuffer($size) |
| 106 |
{ |
| 107 |
return new ByteBuffer($size); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function offset() |
| 116 |
{ |
| 117 |
return $this->bb->capacity() - $this->space; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function pad($byte_size) |
| 126 |
{ |
| 127 |
for ($i = 0; $i < $byte_size; $i++) { |
| 128 |
$this->bb->putByte(--$this->space, "\0"); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function prep($size, $additional_bytes) |
| 140 |
{ |
| 141 |
if ($size > $this->minalign) { |
| 142 |
$this->minalign = $size; |
| 143 |
} |
| 144 |
|
| 145 |
$align_size = ((~($this->bb->capacity() - $this->space + $additional_bytes)) + 1) & ($size - 1); |
| 146 |
while ($this->space < $align_size + $size + $additional_bytes) { |
| 147 |
$old_buf_size = $this->bb->capacity(); |
| 148 |
$this->bb = $this->growByteBuffer($this->bb); |
| 149 |
$this->space += $this->bb->capacity() - $old_buf_size; |
| 150 |
} |
| 151 |
|
| 152 |
$this->pad($align_size); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private static function growByteBuffer(ByteBuffer $bb) |
| 161 |
{ |
| 162 |
$old_buf_size = $bb->capacity(); |
| 163 |
if (($old_buf_size & 0xC0000000) != 0) { |
| 164 |
throw new \Exception("FlatBuffers: cannot grow buffer beyond 2 gigabytes"); |
| 165 |
} |
| 166 |
$new_buf_size = $old_buf_size << 1; |
| 167 |
|
| 168 |
$bb->setPosition(0); |
| 169 |
$nbb = new ByteBuffer($new_buf_size); |
| 170 |
|
| 171 |
$nbb->setPosition($new_buf_size - $old_buf_size); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
for ($i = $new_buf_size - $old_buf_size, $j = 0; $j < strlen($bb->_buffer); $i++, $j++) { |
| 176 |
$nbb->_buffer[$i] = $bb->_buffer[$j]; |
| 177 |
} |
| 178 |
|
| 179 |
return $nbb; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function putBool($x) |
| 186 |
{ |
| 187 |
$this->bb->put($this->space -= 1, chr((int)(bool)($x))); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function putByte($x) |
| 194 |
{ |
| 195 |
$this->bb->put($this->space -= 1, chr($x)); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function putSbyte($x) |
| 202 |
{ |
| 203 |
$this->bb->put($this->space -= 1, chr($x)); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public function putShort($x) |
| 210 |
{ |
| 211 |
$this->bb->putShort($this->space -= 2, $x); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function putUshort($x) |
| 218 |
{ |
| 219 |
$this->bb->putUshort($this->space -= 2, $x); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function putInt($x) |
| 226 |
{ |
| 227 |
$this->bb->putInt($this->space -= 4, $x); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function putUint($x) |
| 234 |
{ |
| 235 |
if ($x > PHP_INT_MAX) { |
| 236 |
throw new \InvalidArgumentException("your platform can't handle uint correctly. use 64bit machine."); |
| 237 |
} |
| 238 |
|
| 239 |
$this->bb->putUint($this->space -= 4, $x); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function putLong($x) |
| 246 |
{ |
| 247 |
if ($x > PHP_INT_MAX) { |
| 248 |
throw new \InvalidArgumentException("Your platform can't handle long correctly. Use a 64bit machine."); |
| 249 |
} |
| 250 |
|
| 251 |
$this->bb->putLong($this->space -= 8, $x); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public function putUlong($x) |
| 258 |
{ |
| 259 |
if ($x > PHP_INT_MAX) { |
| 260 |
throw new \InvalidArgumentException("Your platform can't handle ulong correctly. This is a php limitation. Please wait for the extension release."); |
| 261 |
} |
| 262 |
|
| 263 |
$this->bb->putUlong($this->space -= 8, $x); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function putFloat($x) |
| 270 |
{ |
| 271 |
$this->bb->putFloat($this->space -= 4, $x); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function putDouble($x) |
| 278 |
{ |
| 279 |
$this->bb->putDouble($this->space -= 8, $x); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function putOffset($off) |
| 286 |
{ |
| 287 |
$new_off = $this->offset() - $off + Constants::SIZEOF_INT; |
| 288 |
$this->putInt($new_off); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function addBool($x) |
| 297 |
{ |
| 298 |
$this->prep(1, 0); |
| 299 |
$this->putBool($x); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public function addByte($x) |
| 307 |
{ |
| 308 |
$this->prep(1, 0); |
| 309 |
$this->putByte($x); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function addSbyte($x) |
| 317 |
{ |
| 318 |
$this->prep(1, 0); |
| 319 |
$this->putSbyte($x); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function addShort($x) |
| 327 |
{ |
| 328 |
$this->prep(2, 0); |
| 329 |
$this->putShort($x); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function addUshort($x) |
| 337 |
{ |
| 338 |
$this->prep(2, 0); |
| 339 |
$this->putUshort($x); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public function addInt($x) |
| 347 |
{ |
| 348 |
$this->prep(4, 0); |
| 349 |
$this->putInt($x); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public function addUint($x) |
| 357 |
{ |
| 358 |
$this->prep(4, 0); |
| 359 |
$this->putUint($x); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function addLong($x) |
| 367 |
{ |
| 368 |
$this->prep(8, 0); |
| 369 |
$this->putLong($x); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function addUlong($x) |
| 377 |
{ |
| 378 |
$this->prep(8, 0); |
| 379 |
$this->putUlong($x); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public function addFloat($x) |
| 387 |
{ |
| 388 |
$this->prep(4, 0); |
| 389 |
$this->putFloat($x); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public function addDouble($x) |
| 397 |
{ |
| 398 |
$this->prep(8, 0); |
| 399 |
$this->putDouble($x); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public function addBoolX($o, $x, $d) |
| 409 |
{ |
| 410 |
if ($this->force_defaults || $x != $d) { |
| 411 |
$this->addBool($x); |
| 412 |
$this->slot($o); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public function addByteX($o, $x, $d) |
| 422 |
{ |
| 423 |
if ($this->force_defaults || $x != $d) { |
| 424 |
$this->addByte($x); |
| 425 |
$this->slot($o); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public function addSbyteX($o, $x, $d) |
| 435 |
{ |
| 436 |
if ($this->force_defaults || $x != $d) { |
| 437 |
$this->addSbyte($x); |
| 438 |
$this->slot($o); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public function addShortX($o, $x, $d) |
| 448 |
{ |
| 449 |
if ($this->force_defaults || $x != $d) { |
| 450 |
$this->addShort($x); |
| 451 |
$this->slot($o); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public function addUshortX($o, $x, $d) |
| 461 |
{ |
| 462 |
if ($this->force_defaults || $x != $d) { |
| 463 |
$this->addUshort($x); |
| 464 |
$this->slot($o); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public function addIntX($o, $x, $d) |
| 474 |
{ |
| 475 |
if ($this->force_defaults || $x != $d) { |
| 476 |
$this->addInt($x); |
| 477 |
$this->slot($o); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function addUintX($o, $x, $d) |
| 487 |
{ |
| 488 |
if ($this->force_defaults || $x != $d) { |
| 489 |
$this->addUint($x); |
| 490 |
$this->slot($o); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public function addLongX($o, $x, $d) |
| 500 |
{ |
| 501 |
if ($this->force_defaults || $x != $d) { |
| 502 |
$this->addLong($x); |
| 503 |
$this->slot($o); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public function addUlongX($o, $x, $d) |
| 513 |
{ |
| 514 |
if ($this->force_defaults || $x != $d) { |
| 515 |
$this->addUlong($x); |
| 516 |
$this->slot($o); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public function addFloatX($o, $x, $d) |
| 527 |
{ |
| 528 |
if ($this->force_defaults || $x != $d) { |
| 529 |
$this->addFloat($x); |
| 530 |
$this->slot($o); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public function addDoubleX($o, $x, $d) |
| 540 |
{ |
| 541 |
if ($this->force_defaults || $x != $d) { |
| 542 |
$this->addDouble($x); |
| 543 |
$this->slot($o); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
public function addOffsetX($o, $x, $d) |
| 554 |
{ |
| 555 |
if ($this->force_defaults || $x != $d) { |
| 556 |
$this->addOffset($x); |
| 557 |
$this->slot($o); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public function addOffset($off) |
| 569 |
{ |
| 570 |
$this->prep(Constants::SIZEOF_INT, 0); |
| 571 |
if ($off > $this->offset()) { |
| 572 |
throw new \Exception(""); |
| 573 |
} |
| 574 |
$this->putOffset($off); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public function startVector($elem_size, $num_elems, $alignment) |
| 585 |
{ |
| 586 |
$this->notNested(); |
| 587 |
$this->vector_num_elems = $num_elems; |
| 588 |
$this->prep(Constants::SIZEOF_INT, $elem_size * $num_elems); |
| 589 |
$this->prep($alignment, $elem_size * $num_elems); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public function endVector() |
| 596 |
{ |
| 597 |
$this->putUint($this->vector_num_elems); |
| 598 |
return $this->offset(); |
| 599 |
} |
| 600 |
|
| 601 |
protected function is_utf8($bytes) |
| 602 |
{ |
| 603 |
if (function_exists('mb_detect_encoding')) { |
| 604 |
return (bool) mb_detect_encoding($bytes, 'UTF-8', true); |
| 605 |
} |
| 606 |
|
| 607 |
$len = strlen($bytes); |
| 608 |
if ($len < 1) { |
| 609 |
|
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
for ($j = 0, $i = 0; $i < $len; $i++) { |
| 614 |
|
| 615 |
if ($bytes[$j] == "\x09" || |
| 616 |
$bytes[$j] == "\x0A" || |
| 617 |
$bytes[$j] == "\x0D" || |
| 618 |
($bytes[$j] >= "\x20" && $bytes[$j] <= "\x7E")) { |
| 619 |
$j++; |
| 620 |
continue; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
if ((($i+1) <= $len) && |
| 625 |
($bytes[$j] >= "\xC2" && $bytes[$j] <= "\xDF" && |
| 626 |
($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\xBF"))) { |
| 627 |
$j += 2; |
| 628 |
$i++; |
| 629 |
continue; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
if ((($i + 2) <= $len) && |
| 634 |
$bytes[$j] == "\xE0" && |
| 635 |
($bytes[$j+1] >= "\xA0" && $bytes[$j+1] <= "\xBF" && |
| 636 |
($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF"))) { |
| 637 |
$bytes += 3; |
| 638 |
$i +=2; |
| 639 |
continue; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
if ((($i+2) <= $len) && |
| 644 |
(($bytes[$j] >= "\xE1" && $bytes[$j] <= "\xEC") || |
| 645 |
$bytes[$j] == "\xEE" || |
| 646 |
$bytes[$j] = "\xEF") && |
| 647 |
($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\xBF") && |
| 648 |
($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF")) { |
| 649 |
$j += 3; |
| 650 |
$i += 2; |
| 651 |
continue; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
if ((($i+2) <= $len) && |
| 656 |
$bytes[$j] == "\xED" && |
| 657 |
($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\x9f" && |
| 658 |
($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF"))) { |
| 659 |
$j += 3; |
| 660 |
$i += 2; |
| 661 |
continue; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
if ((($i + 3) <= $len) && |
| 666 |
$bytes[$j] == "\xF0" && |
| 667 |
($bytes[$j+1] >= "\x90" && $bytes[$j+1] <= "\xBF") && |
| 668 |
($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF") && |
| 669 |
($bytes[$j+3] >= "\x80" && $bytes[$j+3] <= "\xBF")) { |
| 670 |
$j += 4; |
| 671 |
$i += 3; |
| 672 |
continue; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
if ((($i+3) <= $len) && |
| 678 |
$bytes[$j] >= "\xF1" && $bytes[$j] <= "\xF3" && |
| 679 |
$bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\xBF" && |
| 680 |
$bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF" && |
| 681 |
$bytes[$j+3] >= "\x80" && $bytes[$j+3] <= "\xBF" |
| 682 |
) { |
| 683 |
$j += 4; |
| 684 |
$i += 3; |
| 685 |
continue; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
if ((($i+3) <= $len) && |
| 690 |
$bytes[$j] == "\xF4" && |
| 691 |
($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\x8F") && |
| 692 |
($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF") && |
| 693 |
($bytes[$j+3] >= "\x80" && $bytes[$j+3] <= "\xBF") |
| 694 |
) { |
| 695 |
$bytes += 4; |
| 696 |
$i += 3; |
| 697 |
continue; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
return false; |
| 702 |
} |
| 703 |
|
| 704 |
return true; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public function createString($s) |
| 716 |
{ |
| 717 |
if (!$this->is_utf8($s)) { |
| 718 |
throw new \InvalidArgumentException("string must be utf-8 encoded value."); |
| 719 |
} |
| 720 |
|
| 721 |
$this->notNested(); |
| 722 |
$this->addByte(0); |
| 723 |
$this->startVector(1, strlen($s), 1); |
| 724 |
$this->space -= strlen($s); |
| 725 |
for ($i = $this->space, $j = 0 ; $j < strlen($s) ; $i++, $j++) { |
| 726 |
$this->bb->_buffer[$i] = $s[$j]; |
| 727 |
} |
| 728 |
return $this->endVector(); |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
public function notNested() |
| 736 |
{ |
| 737 |
if ($this->nested) { |
| 738 |
throw new \Exception("FlatBuffers; object serialization must not be nested"); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
public function nested($obj) |
| 747 |
{ |
| 748 |
if ($obj != $this->offset()) { |
| 749 |
throw new \Exception("FlatBuffers: struct must be serialized inline"); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
public function startObject($numfields) |
| 758 |
{ |
| 759 |
$this->notNested(); |
| 760 |
if ($this->vtable == null || count($this->vtable) < $numfields) { |
| 761 |
$this->vtable = array(); |
| 762 |
} |
| 763 |
|
| 764 |
$this->vtable_in_use = $numfields; |
| 765 |
for ($i = 0; $i < $numfields; $i++) { |
| 766 |
$this->vtable[$i] = 0; |
| 767 |
} |
| 768 |
|
| 769 |
$this->nested = true; |
| 770 |
$this->object_start = $this->offset(); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
public function addStructX($voffset, $x, $d) |
| 780 |
{ |
| 781 |
if ($x != $d) { |
| 782 |
$this->nested($x); |
| 783 |
$this->slot($voffset); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
public function addStruct($voffset, $x, $d) |
| 794 |
{ |
| 795 |
if ($x != $d) { |
| 796 |
$this->nested($x); |
| 797 |
$this->slot($voffset); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
public function slot($voffset) |
| 805 |
{ |
| 806 |
$this->vtable[$voffset] = $this->offset(); |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
public function endObject() |
| 814 |
{ |
| 815 |
if ($this->vtable == null || !$this->nested) { |
| 816 |
throw new \Exception("FlatBuffers: endObject called without startObject"); |
| 817 |
} |
| 818 |
|
| 819 |
$this->addInt(0); |
| 820 |
$vtableloc = $this->offset(); |
| 821 |
|
| 822 |
$i = $this->vtable_in_use -1; |
| 823 |
|
| 824 |
for (; $i >= 0 && $this->vtable[$i] == 0; $i--) {} |
| 825 |
$trimmed_size = $i + 1; |
| 826 |
for (; $i >= 0; $i--) { |
| 827 |
$off = ($this->vtable[$i] != 0) ? $vtableloc - $this->vtable[$i] : 0; |
| 828 |
$this->addShort($off); |
| 829 |
} |
| 830 |
|
| 831 |
$standard_fields = 2; |
| 832 |
$this->addShort($vtableloc - $this->object_start); |
| 833 |
$this->addShort(($trimmed_size + $standard_fields) * Constants::SIZEOF_SHORT); |
| 834 |
|
| 835 |
|
| 836 |
$existing_vtable = 0; |
| 837 |
|
| 838 |
for ($i = 0; $i < $this->num_vtables; $i++) { |
| 839 |
$vt1 = $this->bb->capacity() - $this->vtables[$i]; |
| 840 |
$vt2 = $this->space; |
| 841 |
|
| 842 |
$len = $this->bb->getShort($vt1); |
| 843 |
|
| 844 |
if ($len == $this->bb->getShort($vt2)) { |
| 845 |
for ($j = Constants::SIZEOF_SHORT; $j < $len; $j += Constants::SIZEOF_SHORT) { |
| 846 |
if ($this->bb->getShort($vt1 + $j) != $this->bb->getShort($vt2 + $j)) { |
| 847 |
continue 2; |
| 848 |
} |
| 849 |
} |
| 850 |
$existing_vtable = $this->vtables[$i]; |
| 851 |
break; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
if ($existing_vtable != 0) { |
| 856 |
|
| 857 |
|
| 858 |
$this->space = $this->bb->capacity() - $vtableloc; |
| 859 |
$this->bb->putInt($this->space, $existing_vtable - $vtableloc); |
| 860 |
} else { |
| 861 |
|
| 862 |
|
| 863 |
if ($this->num_vtables == count($this->vtables)) { |
| 864 |
$vtables = $this->vtables; |
| 865 |
$this->vtables = array(); |
| 866 |
|
| 867 |
for ($i = 0; $i < count($vtables) * 2; $i++) { |
| 868 |
$this->vtables[$i] = ($i < count($vtables)) ? $vtables[$i] : 0; |
| 869 |
} |
| 870 |
} |
| 871 |
$this->vtables[$this->num_vtables++] = $this->offset(); |
| 872 |
$this->bb->putInt($this->bb->capacity() - $vtableloc, $this->offset() - $vtableloc); |
| 873 |
} |
| 874 |
|
| 875 |
$this->nested = false; |
| 876 |
$this->vtable = null; |
| 877 |
return $vtableloc; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
public function required($table, $field) |
| 886 |
{ |
| 887 |
$table_start = $this->bb->capacity() - $table; |
| 888 |
$vtable_start = $table_start - $this->bb->getInt($table_start); |
| 889 |
$ok = $this->bb->getShort($vtable_start + $field) != 0; |
| 890 |
|
| 891 |
if (!$ok) { |
| 892 |
throw new \Exception("FlatBuffers: field " . $field . " must be set"); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
public function finish($root_table, $identifier = null) |
| 907 |
{ |
| 908 |
if ($identifier == null) { |
| 909 |
$this->prep($this->minalign, Constants::SIZEOF_INT); |
| 910 |
$this->addOffset($root_table); |
| 911 |
$this->bb->setPosition($this->space); |
| 912 |
} else { |
| 913 |
$this->prep($this->minalign, Constants::SIZEOF_INT + Constants::FILE_IDENTIFIER_LENGTH); |
| 914 |
if (strlen($identifier) != Constants::FILE_IDENTIFIER_LENGTH) { |
| 915 |
throw new \InvalidArgumentException( |
| 916 |
sprintf("FlatBuffers: file identifier must be length %d", |
| 917 |
Constants::FILE_IDENTIFIER_LENGTH)); |
| 918 |
} |
| 919 |
|
| 920 |
for ($i = Constants::FILE_IDENTIFIER_LENGTH - 1; $i >= 0; |
| 921 |
$i--) { |
| 922 |
$this->addByte(ord($identifier[$i])); |
| 923 |
} |
| 924 |
$this->finish($root_table); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
public function forceDefaults($forceDefaults) |
| 935 |
{ |
| 936 |
$this->force_defaults = $forceDefaults; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
public function dataBuffer() |
| 944 |
{ |
| 945 |
return $this->bb; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
public function dataStart() |
| 953 |
{ |
| 954 |
return $this->space; |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
public function sizedByteArray() |
| 965 |
{ |
| 966 |
$start = $this->space; |
| 967 |
$length = $this->bb->capacity() - $this->space; |
| 968 |
|
| 969 |
$result = str_repeat("\0", $length); |
| 970 |
$this->bb->setPosition($start); |
| 971 |
$this->bb->getX($result); |
| 972 |
|
| 973 |
return $result; |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
|