diff options
Diffstat (limited to 'php')
-rw-r--r-- | php/ByteBuffer.php | 495 | ||||
-rw-r--r-- | php/Constants.php | 25 | ||||
-rw-r--r-- | php/FlatbufferBuilder.php | 918 | ||||
-rw-r--r-- | php/Struct.php | 31 | ||||
-rw-r--r-- | php/Table.php | 129 |
5 files changed, 1598 insertions, 0 deletions
diff --git a/php/ByteBuffer.php b/php/ByteBuffer.php new file mode 100644 index 00000000..4a583b7a --- /dev/null +++ b/php/ByteBuffer.php @@ -0,0 +1,495 @@ +<?php +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Google\FlatBuffers; + +class ByteBuffer +{ + /** + * @var string $_buffer; + */ + public $_buffer; + + /** + * @var int $_pos; + */ + private $_pos; + + /** + * @var bool $_is_little_endian + */ + private static $_is_little_endian = null; + + public static function wrap($bytes) + { + $bb = new ByteBuffer(0); + $bb->_buffer = $bytes; + + return $bb; + } + + /** + * @param $size + */ + public function __construct($size) + { + $this->_buffer = str_repeat("\0", $size); + } + + /** + * @return int + */ + public function capacity() + { + return strlen($this->_buffer); + } + + /** + * @return int + */ + public function getPosition() + { + return $this->_pos; + } + + /** + * @param $pos + */ + public function setPosition($pos) + { + $this->_pos = $pos; + } + + /** + * + */ + public function reset() + { + $this->_pos = 0; + } + + /** + * @return int + */ + public function length() + { + return strlen($this->_buffer); + } + + /** + * @return string + */ + public function data() + { + return substr($this->_buffer, $this->_pos); + } + + /** + * @return bool + */ + public static function isLittleEndian() + { + if (ByteBuffer::$_is_little_endian === null) { + ByteBuffer::$_is_little_endian = unpack('S', "\x01\x00")[1] === 1; + } + + return ByteBuffer::$_is_little_endian; + } + + /** + * write little endian value to the buffer. + * + * @param $offset + * @param $count byte length + * @param $data actual values + */ + public function writeLittleEndian($offset, $count, $data) + { + if (ByteBuffer::isLittleEndian()) { + for ($i = 0; $i < $count; $i++) { + $this->_buffer[$offset + $i] = chr($data >> $i * 8); + } + } else { + for ($i = 0; $i < $count; $i++) { + $this->_buffer[$offset + $count - 1 - $i] = chr($data >> $i * 8); + } + } + } + + /** + * read little endian value from the buffer + * + * @param $offset + * @param $count acutal size + * @return int + */ + public function readLittleEndian($offset, $count, $force_bigendian = false) + { + $this->assertOffsetAndLength($offset, $count); + $r = 0; + + if (ByteBuffer::isLittleEndian() && $force_bigendian == false) { + for ($i = 0; $i < $count; $i++) { + $r |= ord($this->_buffer[$offset + $i]) << $i * 8; + } + } else { + for ($i = 0; $i < $count; $i++) { + $r |= ord($this->_buffer[$offset + $count -1 - $i]) << $i * 8; + } + } + + return $r; + } + + /** + * @param $offset + * @param $length + */ + public function assertOffsetAndLength($offset, $length) + { + if ($offset < 0 || + $offset >= strlen($this->_buffer) || + $offset + $length > strlen($this->_buffer)) { + throw new \OutOfRangeException(sprintf("offset: %d, length: %d, buffer; %d", $offset, $length, strlen($this->_buffer))); + } + } + + /** + * @param $offset + * @param $value + * @return mixed + */ + public function putSbyte($offset, $value) + { + self::validateValue(-128, 127, $value, "sbyte"); + + $length = strlen($value); + $this->assertOffsetAndLength($offset, $length); + return $this->_buffer[$offset] = $value; + } + + /** + * @param $offset + * @param $value + * @return mixed + */ + public function putByte($offset, $value) + { + self::validateValue(0, 255, $value, "byte"); + + $length = strlen($value); + $this->assertOffsetAndLength($offset, $length); + return $this->_buffer[$offset] = $value; + } + + /** + * @param $offset + * @param $value + */ + public function put($offset, $value) + { + $length = strlen($value); + $this->assertOffsetAndLength($offset, $length); + for ($i = 0; $i < $length; $i++) { + $this->_buffer[$offset + $i] = $value[$i]; + } + } + + /** + * @param $offset + * @param $value + */ + public function putShort($offset, $value) + { + self::validateValue(-32768, 32767, $value, "short"); + + $this->assertOffsetAndLength($offset, 2); + $this->writeLittleEndian($offset, 2, $value); + } + + /** + * @param $offset + * @param $value + */ + public function putUshort($offset, $value) + { + self::validateValue(0, 65535, $value, "short"); + + $this->assertOffsetAndLength($offset, 2); + $this->writeLittleEndian($offset, 2, $value); + } + + /** + * @param $offset + * @param $value + */ + public function putInt($offset, $value) + { + self::validateValue(~PHP_INT_MAX, PHP_INT_MAX, $value, "int"); + + $this->assertOffsetAndLength($offset, 4); + $this->writeLittleEndian($offset, 4, $value); + } + + /** + * @param $offset + * @param $value + */ + public function putUint($offset, $value) + { + // NOTE: We can't put big integer value. this is PHP limitation. + self::validateValue(0, PHP_INT_MAX, $value, "uint", " php has big numbers limitation. check your PHP_INT_MAX"); + + $this->assertOffsetAndLength($offset, 4); + $this->writeLittleEndian($offset, 4, $value); + } + + /** + * @param $offset + * @param $value + */ + public function putLong($offset, $value) + { + // NOTE: We can't put big integer value. this is PHP limitation. + self::validateValue(~PHP_INT_MAX, PHP_INT_MAX, $value, "long", " php has big numbers limitation. check your PHP_INT_MAX"); + + $this->assertOffsetAndLength($offset, 8); + $this->writeLittleEndian($offset, 8, $value); + } + + /** + * @param $offset + * @param $value + */ + public function putUlong($offset, $value) + { + // NOTE: We can't put big integer value. this is PHP limitation. + self::validateValue(0, PHP_INT_MAX, $value, "long", " php has big numbers limitation. check your PHP_INT_MAX"); + + $this->assertOffsetAndLength($offset, 8); + $this->writeLittleEndian($offset, 8, $value); + } + + /** + * @param $offset + * @param $value + */ + public function putFloat($offset, $value) + { + $this->assertOffsetAndLength($offset, 4); + + $floathelper = pack("f", $value); + $v = unpack("V", $floathelper); + $this->writeLittleEndian($offset, 4, $v[1]); + } + + /** + * @param $offset + * @param $value + */ + public function putDouble($offset, $value) + { + $this->assertOffsetAndLength($offset, 8); + + $floathelper = pack("d", $value); + $v = unpack("V*", $floathelper); + + $this->writeLittleEndian($offset, 4, $v[1]); + $this->writeLittleEndian($offset + 4, 4, $v[2]); + } + + /** + * @param $index + * @return mixed + */ + public function getByte($index) + { + return ord($this->_buffer[$index]); + } + + /** + * @param $index + * @return mixed + */ + public function getSbyte($index) + { + $v = unpack("c", $this->_buffer[$index]); + return $v[1]; + } + + /** + * @param $buffer + */ + public function getX(&$buffer) + { + for ($i = $this->_pos, $j = 0; $j < strlen($buffer); $i++, $j++) { + $buffer[$j] = $this->_buffer[$i]; + } + } + + /** + * @param $index + * @return mixed + */ + public function get($index) + { + $this->assertOffsetAndLength($index, 1); + return $this->_buffer[$index]; + } + + + /** + * @param $index + * @return mixed + */ + public function getBool($index) + { + return (bool)ord($this->_buffer[$index]); + } + + /** + * @param $index + * @return int + */ + public function getShort($index) + { + $result = $this->readLittleEndian($index, 2); + + return self::convertHelper(self::__SHORT, $result); + } + + /** + * @param $index + * @return int + */ + public function getUShort($index) + { + return $this->readLittleEndian($index, 2); + } + + /** + * @param $index + * @return int + */ + public function getInt($index) + { + $result = $this->readLittleEndian($index, 4); + + return self::convertHelper(self::__INT, $result); + } + + /** + * @param $index + * @return int + */ + public function getUint($index) + { + return $this->readLittleEndian($index, 4); + } + + /** + * @param $index + * @return int + */ + public function getLong($index) + { + $result = $this->readLittleEndian($index, 8); + + return self::convertHelper(self::__LONG, $result); + } + + /** + * @param $index + * @return int + */ + public function getUlong($index) + { + return $this->readLittleEndian($index, 8); + } + + /** + * @param $index + * @return mixed + */ + public function getFloat($index) + { + $i = $this->readLittleEndian($index, 4); + + return self::convertHelper(self::__FLOAT, $i); + } + + /** + * @param $index + * @return float + */ + public function getDouble($index) + { + $i = $this->readLittleEndian($index, 4); + $i2 = $this->readLittleEndian($index + 4, 4); + + return self::convertHelper(self::__DOUBLE, $i, $i2); + } + + const __SHORT = 1; + const __INT = 2; + const __LONG = 3; + const __FLOAT = 4; + const __DOUBLE = 5; + private static function convertHelper($type, $value, $value2 = null) { + // readLittleEndian construct unsigned integer value from bytes. we have to encode this value to + // correct bytes, and decode as expected types with `unpack` function. + // then it returns correct type value. + // see also: http://php.net/manual/en/function.pack.php + + switch ($type) { + case self::__SHORT: + $helper = pack("v", $value); + $v = unpack("s", $helper); + + return $v[1]; + break; + case self::__INT: + $helper = pack("V", $value); + $v = unpack("l", $helper); + return $v[1]; + break; + case self::__LONG: + $helper = pack("P", $value); + $v = unpack("q", $helper); + return $v[1]; + break; + case self::__FLOAT: + $inthelper = pack("V", $value); + $v = unpack("f", $inthelper); + return $v[1]; + break; + case self::__DOUBLE: + $inthelper = pack("VV", $value, $value2); + $v = unpack("d", $inthelper); + return $v[1]; + break; + default: + throw new \Exception(sprintf("unexpected type %d specified", $type)); + } + } + + private static function validateValue($min, $max, $value, $type, $additional_notes = "") { + if(!($min <= $value && $value <= $max)) { + throw new \InvalidArgumentException(sprintf("bad number %s for type %s.%s", $value, $type, $additional_notes)); + } + } +} diff --git a/php/Constants.php b/php/Constants.php new file mode 100644 index 00000000..ef3730d4 --- /dev/null +++ b/php/Constants.php @@ -0,0 +1,25 @@ +<?php +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Google\FlatBuffers; + +class Constants +{ + const SIZEOF_SHORT = 2; + const SIZEOF_INT = 4; + const FILE_IDENTIFIER_LENGTH = 4; +} diff --git a/php/FlatbufferBuilder.php b/php/FlatbufferBuilder.php new file mode 100644 index 00000000..b72a6d65 --- /dev/null +++ b/php/FlatbufferBuilder.php @@ -0,0 +1,918 @@ +<?php +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Google\FlatBuffers; + +class FlatbufferBuilder +{ + /** + * @var ByteBuffer $bb + */ + public $bb; + + /** + * @var int $space + */ + protected $space; + + /** + * @var int $minalign + */ + protected $minalign = 1; + + /** + * @var array $vtable + */ + protected $vtable; + + /** + * @var int $vtable_in_use + */ + protected $vtable_in_use = 0; + + /** + * @var bool $nested + */ + protected $nested = false; + + /** + * @var int $object_start + */ + protected $object_start; + + /** + * @var array $vtables + */ + protected $vtables = array(); + + /** + * @var int $num_vtables + */ + protected $num_vtables = 0; + + /** + * @var int $vector_num_elems + */ + protected $vector_num_elems = 0; + + /** + * @var bool $force_defaults + */ + protected $force_defaults = false; + + /** + * create flatbuffers builder + * + * @param $initial_size initial byte buffer size. + */ + public function __construct($initial_size) + { + if ($initial_size <= 0) { + $initial_size = 1; + } + $this->space = $initial_size; + $this->bb = $this->newByteBuffer($initial_size); + } + + /** + * create new bytebuffer + * + * @param $size + * @return ByteBuffer + */ + private function newByteBuffer($size) + { + return new ByteBuffer($size); + } + + /** + * returns current bytebuffer offset + * + * @return int + */ + public function offset() + { + return $this->bb->capacity() - $this->space; + } + + /** + * padding buffer + * + * @param $byte_size + */ + public function pad($byte_size) + { + for ($i = 0; $i < $byte_size; $i++) { + $this->bb->putByte(--$this->space, "\0"); + } + } + + /** + * prepare bytebuffer + * + * @param $size + * @param $additional_bytes + * @throws \Exception + */ + public function prep($size, $additional_bytes) + { + if ($size > $this->minalign) { + $this->minalign = $size; + } + + $align_size = ((~($this->bb->capacity() - $this->space + $additional_bytes)) + 1) & ($size - 1); + while ($this->space < $align_size + $size + $additional_bytes) { + $old_buf_size = $this->bb->capacity(); + $this->bb = $this->growByteBuffer($this->bb); + $this->space += $this->bb->capacity() - $old_buf_size; + } + + $this->pad($align_size); + } + + /** + * @param ByteBuffer $bb + * @return ByteBuffer + * @throws \Exception + */ + private static function growByteBuffer(ByteBuffer $bb) + { + $old_buf_size = $bb->capacity(); + if (($old_buf_size & 0xC0000000) != 0) { + throw new \Exception("FlatBuffers: cannot grow buffer beyond 2 gigabytes"); + } + $new_buf_size = $old_buf_size << 1; + + $bb->setPosition(0); + $nbb = new ByteBuffer($new_buf_size); + + $nbb->setPosition($new_buf_size - $old_buf_size); + + // TODO(chobie): is this little bit faster? + //$nbb->_buffer = substr_replace($nbb->_buffer, $bb->_buffer, $new_buf_size - $old_buf_size, strlen($bb->_buffer)); + for ($i = $new_buf_size - $old_buf_size, $j = 0; $j < strlen($bb->_buffer); $i++, $j++) { + $nbb->_buffer[$i] = $bb->_buffer[$j]; + } + + return $nbb; + } + + /** + * @param $x + */ + public function putBool($x) + { + $this->bb->put($this->space -= 1, chr((int)(bool)($x))); + } + + /** + * @param $x + */ + public function putByte($x) + { + $this->bb->put($this->space -= 1, chr($x)); + } + + /** + * @param $x + */ + public function putSbyte($x) + { + $this->bb->put($this->space -= 1, chr($x)); + } + + /** + * @param $x + */ + public function putShort($x) + { + $this->bb->putShort($this->space -= 2, $x); + } + + /** + * @param $x + */ + public function putUshort($x) + { + $this->bb->putUshort($this->space -= 2, $x); + } + + /** + * @param $x + */ + public function putInt($x) + { + $this->bb->putInt($this->space -= 4, $x); + } + + /** + * @param $x + */ + public function putUint($x) + { + if ($x > PHP_INT_MAX) { + throw new \InvalidArgumentException("your platform can't handling uint correctly. use 64bit machine."); + } + + $this->bb->putUint($this->space -= 4, $x); + } + + /** + * @param $x + */ + public function putLong($x) + { + if ($x > PHP_INT_MAX) { + throw new \InvalidArgumentException("your platform can't handling long correctly. use 64bit machine."); + } + + $this->bb->putLong($this->space -= 8, $x); + } + + /** + * @param $x + */ + public function putUlong($x) + { + if ($x > PHP_INT_MAX) { + throw new \InvalidArgumentException("your platform can't handling ulong correctly. this is php limitations. please wait extension release."); + } + + $this->bb->putUlong($this->space -= 8, $x); + } + + /** + * @param $x + */ + public function putFloat($x) + { + $this->bb->putFloat($this->space -= 4, $x); + } + + /** + * @param $x + */ + public function putDouble($x) + { + $this->bb->putDouble($this->space -= 8, $x); + } + + /** + * @param $x + */ + public function addBool($x) + { + $this->prep(1, 0); + $this->putBool($x); + } + + /** + * @param $x + */ + public function addByte($x) + { + $this->prep(1, 0); + $this->putByte($x); + } + + /** + * @param $x + */ + public function addSbyte($x) + { + $this->prep(1, 0); + $this->putSbyte($x); + } + + /** + * @param $x + */ + public function addShort($x) + { + $this->prep(2, 0); + $this->putShort($x); + } + + /** + * @param $x + */ + public function addUshort($x) + { + $this->prep(2, 0); + $this->putUshort($x); + } + + /** + * @param $x + */ + public function addInt($x) + { + $this->prep(4, 0); + $this->putInt($x); + } + + /** + * @param $x + */ + public function addUint($x) + { + $this->prep(4, 0); + $this->putUint($x); + } + + + /** + * @param $x + */ + public function addLong($x) + { + $this->prep(8, 0); + $this->putLong($x); + } + + /** + * @param $x + */ + public function addUlong($x) + { + $this->prep(8, 0); + $this->putUlong($x); + } + + /** + * @param $x + */ + public function addFloat($x) + { + $this->prep(4, 0); + $this->putFloat($x); + } + + /** + * @param $x + */ + public function addDouble($x) + { + $this->prep(8, 0); + $this->putDouble($x); + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addBoolX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addBool($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addByteX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addByte($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addSbyteX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addSbyte($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addShortX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addShort($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addUshortX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addUshort($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addIntX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addInt($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addUintX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addUint($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addLongX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addLong($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addUlongX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addUlong($x); + $this->slot($o); + } + } + + + /** + * @param $o + * @param $x + * @param $d + */ + public function addFloatX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addFloat($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + */ + public function addDoubleX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addDouble($x); + $this->slot($o); + } + } + + /** + * @param $o + * @param $x + * @param $d + * @throws \Exception + */ + public function addOffsetX($o, $x, $d) + { + if ($this->force_defaults || $x != $d) { + $this->addOffset($x); + $this->slot($o); + } + } + + /** + * @param $off + * @throws \Exception + */ + public function addOffset($off) + { + $this->prep(Constants::SIZEOF_INT, 0); // Ensure alignment is already done + if ($off > $this->offset()) { + throw new \Exception(""); + } + + $off = $this->offset() - $off + Constants::SIZEOF_INT; + $this->putInt($off); + } + + /** + * @param $elem_size + * @param $num_elems + * @param $alignment + * @throws \Exception + */ + public function startVector($elem_size, $num_elems, $alignment) + { + $this->notNested(); + $this->vector_num_elems = $num_elems; + $this->prep(Constants::SIZEOF_INT, $elem_size * $num_elems); + $this->prep($alignment, $elem_size * $num_elems); // Just in case alignemnt > int; + } + + /** + * @return int + */ + public function endVector() + { + $this->putUint($this->vector_num_elems); + return $this->offset(); + } + + protected function is_utf8($bytes) + { + $len = strlen($bytes); + if ($len < 1) { + /* NOTE: always return 1 when passed string is null */ + return true; + } + + for ($j = 0, $i = 0; $i < $len; $i++) { + // check ACII + if ($bytes[$j] == "\x09" || + $bytes[$j] == "\x0A" || + $bytes[$j] == "\x0D" || + ($bytes[$j] >= "\x20" && $bytes[$j] <= "\x7E")) { + $j++; + continue; + } + + /* non-overlong 2-byte */ + if ((($i+1) <= $len) && + ($bytes[$j] >= "\xC2" && $bytes[$j] <= "\xDF" && + ($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\xBF"))) { + $j += 2; + $i++; + continue; + } + + /* excluding overlongs */ + if ((($i + 2) <= $len) && + $bytes[$j] == "\xE0" && + ($bytes[$j+1] >= "\xA0" && $bytes[$j+1] <= "\xBF" && + ($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF"))) { + $bytes += 3; + $i +=2; + continue; + } + + /* straight 3-byte */ + if ((($i+2) <= $len) && + (($bytes[$j] >= "\xE1" && $bytes[$j] <= "\xEC") || + $bytes[$j] == "\xEE" || + $bytes[$j] = "\xEF") && + ($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\xBF") && + ($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF")) { + $j += 3; + $i += 2; + continue; + } + + /* excluding surrogates */ + if ((($i+2) <= $len) && + $bytes[$j] == "\xED" && + ($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\x9f" && + ($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF"))) { + $j += 3; + $i += 2; + continue; + } + + /* planes 1-3 */ + if ((($i + 3) <= $len) && + $bytes[$j] == "\xF0" && + ($bytes[$j+1] >= "\x90" && $bytes[$j+1] <= "\xBF") && + ($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF") && + ($bytes[$j+3] >= "\x80" && $bytes[$j+3] <= "\xBF")) { + $j += 4; + $i += 3; + continue; + } + + + /* planes 4-15 */ + if ((($i+3) <= $len) && + $bytes[$j] >= "\xF1" && $bytes[$j] <= "\xF3" && + $bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\xBF" && + $bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF" && + $bytes[$j+3] >= "\x80" && $bytes[$j+3] <= "\xBF" + ) { + $j += 4; + $i += 3; + continue; + } + + /* plane 16 */ + if ((($i+3) <= $len) && + $bytes[$j] == "\xF4" && + ($bytes[$j+1] >= "\x80" && $bytes[$j+1] <= "\x8F") && + ($bytes[$j+2] >= "\x80" && $bytes[$j+2] <= "\xBF") && + ($bytes[$j+3] >= "\x80" && $bytes[$j+3] <= "\xBF") + ) { + $bytes += 4; + $i += 3; + continue; + } + + + return false; + } + + return true; + } + + + /** + * @param $s + * @return int + * @throws \Exception + */ + public function createString($s) + { + if (!$this->is_utf8($s)) { + throw new \InvalidArgumentException("string must be utf-8 encoded value."); + } + + $this->notNested(); + $this->addByte(0); // null terminated + $this->startVector(1, strlen($s), 1); + $this->space -= strlen($s); + for ($i = $this->space, $j = 0 ; $j < strlen($s) ; $i++, $j++) { + $this->bb->_buffer[$i] = $s[$j]; + } + return $this->endVector(); + } + + /** + * @throws \Exception + */ + public function notNested() + { + if ($this->nested) { + throw new \Exception("FlatBuffers; object serialization must not be nested"); + } + } + + /** + * @param $obj + * @throws \Exception + */ + public function nested($obj) + { + if ($obj != $this->offset()) { + throw new \Exception("FlatBuffers: struct must be serialized inline"); + } + } + + /** + * @param $numfields + * @throws \Exception + */ + public function startObject($numfields) + { + $this->notNested(); + if ($this->vtable == null || count($this->vtable) < $numfields) { + $this->vtable = array(); + } + + $this->vtable_in_use = $numfields; + for ($i = 0; $i < $numfields; $i++) { + $this->vtable[$i] = 0; + } + + $this->nested = true; + $this->object_start = $this->offset(); + } + + /** + * @param $voffset + * @param $x + * @param $d + * @throws \Exception + */ + public function addStructX($voffset, $x, $d) + { + if ($x != $d) { + $this->nested($x); + $this->slot($voffset); + } + } + + /** + * @param $voffset + * @param $x + * @param $d + * @throws \Exception + */ + public function addStruct($voffset, $x, $d) + { + if ($x != $d) { + $this->nested($x); + $this->slot($voffset); + } + } + + /** + * @param $voffset + */ + public function slot($voffset) + { + $this->vtable[$voffset] = $this->offset(); + } + + /** + * @return int + * @throws \Exception + */ + public function endObject() + { + if ($this->vtable == null || !$this->nested) { + throw new \Exception("FlatBuffers: endObject called without startObject"); + } + + $this->addInt(0); + $vtableloc = $this->offset(); + + for ($i = $this->vtable_in_use -1; $i >= 0; $i--) { + $off = ($this->vtable[$i] != 0) ? $vtableloc - $this->vtable[$i] : 0; + $this->addShort($off); + } + + $standard_fields = 2; // the fields below + $this->addShort($vtableloc - $this->object_start); + $this->addShort(($this->vtable_in_use + $standard_fields) * Constants::SIZEOF_SHORT); + + // search for an existing vtable that matches the current one. + $existing_vtable = 0; + + for ($i = 0; $i < $this->num_vtables; $i++) { + $vt1 = $this->bb->capacity() - $this->vtables[$i]; + $vt2 = $this->space; + + $len = $this->bb->getShort($vt1); + + if ($len == $this->bb->getShort($vt2)) { + for ($j = Constants::SIZEOF_SHORT; $j < $len; $j += Constants::SIZEOF_SHORT) { + if ($this->bb->getShort($vt1 + $j) != $this->bb->getShort($vt2 + $j)) { + continue 2; + } + } + $existing_vtable = $this->vtables[$i]; + break; + } + } + + if ($existing_vtable != 0) { + // Found a match: + // Remove the current vtable + $this->space = $this->bb->capacity() - $vtableloc; + $this->bb->putInt($this->space, $existing_vtable - $vtableloc); + } else { + // No Match: + // Add the location of the current vtable to the list of vtables + if ($this->num_vtables == count($this->vtables)) { + $vtables = $this->vtables; + $this->vtables = array(); + // copy of + for ($i = 0; $i < count($vtables) * 2; $i++) { + $this->vtables[$i] = ($i < count($vtables)) ? $vtables[$i] : 0; + } + } + $this->vtables[$this->num_vtables++] = $this->offset(); + $this->bb->putInt($this->bb->capacity() - $vtableloc, $this->offset() - $vtableloc); + } + + $this->nested = false; + $this->vtable = null; + return $vtableloc; + } + + /** + * @param $table + * @param $field + * @throws \Exception + */ + public function required($table, $field) + { + $table_start = $this->bb->capacity() - $table; + $vtable_start = $table_start - $this->bb->getInt($table_start); + $ok = $this->bb->getShort($vtable_start + $field) != 0; + + if (!$ok) { + throw new \Exception("FlatBuffers: field " . $field . " must be set"); + } + } + + /** + * @param $root_table + * @throws \Exception + */ + public function finish($root_table, $identifier = null) + { + if ($identifier == null) { + $this->prep($this->minalign, Constants::SIZEOF_INT); + $this->addOffset($root_table); + $this->bb->setPosition($this->space); + } else { + $this->prep($this->minalign, Constants::SIZEOF_INT + Constants::FILE_IDENTIFIER_LENGTH); + if (strlen($identifier) != Constants::FILE_IDENTIFIER_LENGTH) { + throw new \InvalidArgumentException( + sprintf("FlatBuffers: file identifier must be length %d", + Constants::FILE_IDENTIFIER_LENGTH)); + } + + for ($i = Constants::FILE_IDENTIFIER_LENGTH - 1; $i >= 0; + $i--) { + $this->addByte(ord($identifier[$i])); + } + $this->finish($root_table); + } + } + + /** + * @param bool $forceDefaults + */ + public function forceDefaults($forceDefaults) + { + $this->force_defaults = $forceDefaults; + } + + /** + * @return ByteBuffer + */ + public function dataBuffer() + { + return $this->bb; + } + + /** + * @return int + */ + public function dataStart() + { + return $this->space; + } + + /** + * @return string + */ + public function sizedByteArray() + { + $start = $this->space; + $length = $this->bb->capacity() - $this->space; + + $result = str_repeat("\0", $length); + $this->bb->setPosition($start); + $this->bb->getX($result); + + return $result; + } +} diff --git a/php/Struct.php b/php/Struct.php new file mode 100644 index 00000000..94e712b2 --- /dev/null +++ b/php/Struct.php @@ -0,0 +1,31 @@ +<?php +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Google\FlatBuffers; + +abstract class Struct +{ + /** + * @var int $bb_pos + */ + protected $bb_pos; + + /** + * @var ByteBuffer $bb + */ + protected $bb; +} diff --git a/php/Table.php b/php/Table.php new file mode 100644 index 00000000..7f611456 --- /dev/null +++ b/php/Table.php @@ -0,0 +1,129 @@ +<?php +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Google\FlatBuffers; + +abstract class Table +{ + /** + * @var int $bb_pos + */ + protected $bb_pos; + /** + * @var ByteBuffer $bb + */ + protected $bb; + + public function __construct() + { + } + + /** + * returns actual vtable offset + * + * @param $vtable_offset + * @return int offset > 0 means exist value. 0 means not exist + */ + protected function __offset($vtable_offset) + { + $vtable = $this->bb_pos - $this->bb->getInt($this->bb_pos); + return $vtable_offset < $this->bb->getShort($vtable) ? $this->bb->getShort($vtable + $vtable_offset) : 0; + } + + /** + * @param $offset + * @return mixed + */ + protected function __indirect($offset) + { + return $offset + $this->bb->getInt($offset); + } + + /** + * fetch utf8 encoded string. + * + * @param $offset + * @return string + */ + protected function __string($offset) + { + $offset += $this->bb->getInt($offset); + $len = $this->bb->getInt($offset); + $startPos = $offset + Constants::SIZEOF_INT; + return substr($this->bb->_buffer, $startPos, $len); + } + + /** + * @param $offset + * @return int + */ + protected function __vector_len($offset) + { + $offset += $this->bb_pos; + $offset += $this->bb->getInt($offset); + return $this->bb->getInt($offset); + } + + /** + * @param $offset + * @return int + */ + protected function __vector($offset) + { + $offset += $this->bb_pos; + // data starts after the length + return $offset + $this->bb->getInt($offset) + Constants::SIZEOF_INT; + } + +// protected function __vector_as_bytebuffer($vector_offset, $elem_size) +// { +// } + + /** + * @param Table $table + * @param int $offset + * @return Table + */ + protected function __union($table, $offset) + { + $offset += $this->bb_pos; + $table->bb_pos = $offset + $this->bb->getInt($offset); + $table->bb = $this->bb; + return $table; + } + + /** + * @param ByteBuffer $bb + * @param string $ident + * @return bool + * @throws \ArgumentException + */ + protected static function __has_identifier($bb, $ident) + { + if (strlen($ident) != Constants::FILE_IDENTIFIER_LENGTH) { + throw new \ArgumentException("FlatBuffers: file identifier must be length " . Constants::FILE_IDENTIFIER_LENGTH); + } + + for ($i = 0; $i < 4; $i++) { + if ($ident[$i] != $bb->get($bb->getPosition() + Constants::SIZEOF_INT + $i)) { + return false; + } + } + + return true; + } +} |