1 <?php 2 /* 3 * Copyright 2015 Google Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 namespace Google\FlatBuffers; 19 20 abstract class Table 21 { 22 /** 23 * @var int $bb_pos 24 */ 25 protected $bb_pos; 26 /** 27 * @var ByteBuffer $bb 28 */ 29 protected $bb; 30 31 public function __construct() 32 { 33 } 34 35 /** 36 * returns actual vtable offset 37 * 38 * @param $vtable_offset 39 * @return int offset > 0 means exist value. 0 means not exist 40 */ 41 protected function __offset($vtable_offset) 42 { 43 $vtable = $this->bb_pos - $this->bb->getInt($this->bb_pos); 44 return $vtable_offset < $this->bb->getShort($vtable) ? $this->bb->getShort($vtable + $vtable_offset) : 0; 45 } 46 47 /** 48 * @param $offset 49 * @return mixed 50 */ 51 protected function __indirect($offset) 52 { 53 return $offset + $this->bb->getInt($offset); 54 } 55 56 /** 57 * fetch utf8 encoded string. 58 * 59 * @param $offset 60 * @return string 61 */ 62 protected function __string($offset) 63 { 64 $offset += $this->bb->getInt($offset); 65 $len = $this->bb->getInt($offset); 66 $startPos = $offset + Constants::SIZEOF_INT; 67 return substr($this->bb->_buffer, $startPos, $len); 68 } 69 70 /** 71 * @param $offset 72 * @return int 73 */ 74 protected function __vector_len($offset) 75 { 76 $offset += $this->bb_pos; 77 $offset += $this->bb->getInt($offset); 78 return $this->bb->getInt($offset); 79 } 80 81 /** 82 * @param $offset 83 * @return int 84 */ 85 protected function __vector($offset) 86 { 87 $offset += $this->bb_pos; 88 // data starts after the length 89 return $offset + $this->bb->getInt($offset) + Constants::SIZEOF_INT; 90 } 91 92 protected function __vector_as_bytes($vector_offset, $elem_size=1) 93 { 94 $o = $this->__offset($vector_offset); 95 if ($o == 0) { 96 return null; 97 } 98 99 return substr($this->bb->_buffer, $this->__vector($o), $this->__vector_len($o) * $elem_size); 100 } 101 102 /** 103 * @param Table $table 104 * @param int $offset 105 * @return Table 106 */ 107 protected function __union($table, $offset) 108 { 109 $offset += $this->bb_pos; 110 $table->bb_pos = $offset + $this->bb->getInt($offset); 111 $table->bb = $this->bb; 112 return $table; 113 } 114 115 /** 116 * @param ByteBuffer $bb 117 * @param string $ident 118 * @return bool 119 * @throws \ArgumentException 120 */ 121 protected static function __has_identifier($bb, $ident) 122 { 123 if (strlen($ident) != Constants::FILE_IDENTIFIER_LENGTH) { 124 throw new \ArgumentException("FlatBuffers: file identifier must be length " . Constants::FILE_IDENTIFIER_LENGTH); 125 } 126 127 for ($i = 0; $i < 4; $i++) { 128 if ($ident[$i] != $bb->get($bb->getPosition() + Constants::SIZEOF_INT + $i)) { 129 return false; 130 } 131 } 132 133 return true; 134 } 135 } 136