Flatbuffers library added to the list of third party libraries.
This commit is contained in:
+493
@@ -0,0 +1,493 @@
|
||||
<?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)
|
||||
{
|
||||
// 2147483647 = (1 << 31) -1 = Maximum signed 32-bit int
|
||||
// -2147483648 = -1 << 31 = Minimum signed 32-bit int
|
||||
self::validateValue(-2147483648, 2147483647, $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.
|
||||
// 4294967295 = (1 << 32) -1 = Maximum unsigned 32-bin int
|
||||
self::validateValue(0, 4294967295, $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);
|
||||
|
||||
$sign = $index + (ByteBuffer::isLittleEndian() ? 1 : 0);
|
||||
$issigned = isset($this->_buffer[$sign]) && ord($this->_buffer[$sign]) & 0x80;
|
||||
|
||||
// 65536 = 1 << 16 = Maximum unsigned 16-bit int
|
||||
return $issigned ? $result - 65536 : $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);
|
||||
|
||||
$sign = $index + (ByteBuffer::isLittleEndian() ? 3 : 0);
|
||||
$issigned = isset($this->_buffer[$sign]) && ord($this->_buffer[$sign]) & 0x80;
|
||||
|
||||
if (PHP_INT_SIZE > 4) {
|
||||
// 4294967296 = 1 << 32 = Maximum unsigned 32-bit int
|
||||
return $issigned ? $result - 4294967296 : $result;
|
||||
} else {
|
||||
// 32bit / Windows treated number as signed integer.
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @return int
|
||||
*/
|
||||
public function getUint($index)
|
||||
{
|
||||
return $this->readLittleEndian($index, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
* @return int
|
||||
*/
|
||||
public function getLong($index)
|
||||
{
|
||||
return $this->readLittleEndian($index, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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::__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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -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;
|
||||
}
|
||||
+977
@@ -0,0 +1,977 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
/// @file
|
||||
/// @addtogroup flatbuffers_php_api
|
||||
/// @{
|
||||
|
||||
namespace Google\FlatBuffers;
|
||||
|
||||
final class FlatbufferBuilder
|
||||
{
|
||||
/**
|
||||
* Internal ByteBuffer for the FlatBuffer data.
|
||||
* @var ByteBuffer $bb
|
||||
*/
|
||||
public $bb;
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
/**
|
||||
* @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;
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* Create a FlatBufferBuilder with a given initial size.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
/**
|
||||
* create new bytebuffer
|
||||
*
|
||||
* @param $size
|
||||
* @return ByteBuffer
|
||||
*/
|
||||
private function newByteBuffer($size)
|
||||
{
|
||||
return new ByteBuffer($size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 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 handle 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 handle long correctly. Use a 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 handle ulong correctly. This is a php limitation. Please wait for the 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 $off
|
||||
*/
|
||||
public function putOffset($off)
|
||||
{
|
||||
$new_off = $this->offset() - $off + Constants::SIZEOF_INT;
|
||||
$this->putInt($new_off);
|
||||
}
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* Add a `bool` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `bool` to add to the buffer.
|
||||
*/
|
||||
public function addBool($x)
|
||||
{
|
||||
$this->prep(1, 0);
|
||||
$this->putBool($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a `byte` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `byte` to add to the buffer.
|
||||
*/
|
||||
public function addByte($x)
|
||||
{
|
||||
$this->prep(1, 0);
|
||||
$this->putByte($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a `signed byte` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `signed byte` to add to the buffer.
|
||||
*/
|
||||
public function addSbyte($x)
|
||||
{
|
||||
$this->prep(1, 0);
|
||||
$this->putSbyte($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a `short` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `short` to add to the buffer.
|
||||
*/
|
||||
public function addShort($x)
|
||||
{
|
||||
$this->prep(2, 0);
|
||||
$this->putShort($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an `unsigned short` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `unsigned short` to add to the buffer.
|
||||
*/
|
||||
public function addUshort($x)
|
||||
{
|
||||
$this->prep(2, 0);
|
||||
$this->putUshort($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `int` to add to the buffer.
|
||||
*/
|
||||
public function addInt($x)
|
||||
{
|
||||
$this->prep(4, 0);
|
||||
$this->putInt($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an `unsigned int` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `unsigned int` to add to the buffer.
|
||||
*/
|
||||
public function addUint($x)
|
||||
{
|
||||
$this->prep(4, 0);
|
||||
$this->putUint($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `long` to add to the buffer.
|
||||
*/
|
||||
public function addLong($x)
|
||||
{
|
||||
$this->prep(8, 0);
|
||||
$this->putLong($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an `unsigned long` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `unsigned long` to add to the buffer.
|
||||
*/
|
||||
public function addUlong($x)
|
||||
{
|
||||
$this->prep(8, 0);
|
||||
$this->putUlong($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a `float` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `float` to add to the buffer.
|
||||
*/
|
||||
public function addFloat($x)
|
||||
{
|
||||
$this->prep(4, 0);
|
||||
$this->putFloat($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a `double` to the buffer, properly aligned, and grows the buffer (if necessary).
|
||||
* @param $x The `double` to add to the buffer.
|
||||
*/
|
||||
public function addDouble($x)
|
||||
{
|
||||
$this->prep(8, 0);
|
||||
$this->putDouble($x);
|
||||
}
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* Adds on offset, relative to where it will be written.
|
||||
* @param $off The offset to add to the buffer.
|
||||
* @throws \Exception Throws an exception if `$off` is greater than the underlying ByteBuffer's
|
||||
* offest.
|
||||
*/
|
||||
public function addOffset($off)
|
||||
{
|
||||
$this->prep(Constants::SIZEOF_INT, 0); // Ensure alignment is already done
|
||||
if ($off > $this->offset()) {
|
||||
throw new \Exception("");
|
||||
}
|
||||
$this->putOffset($off);
|
||||
}
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
if (function_exists('mb_detect_encoding')) {
|
||||
return (bool) mb_detect_encoding($bytes, 'UTF-8', true);
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* Encode the string `$s` in the buffer using UTF-8.
|
||||
* @param string $s The string to encode.
|
||||
* @return int The offset in the buffer where the encoded string starts.
|
||||
* @throws InvalidArgumentException Thrown if the input string `$s` is not
|
||||
* UTF-8.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
/**
|
||||
* @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();
|
||||
|
||||
$i = $this->vtable_in_use -1;
|
||||
// Trim trailing zeroes.
|
||||
for (; $i >= 0 && $this->vtable[$i] == 0; $i--) {}
|
||||
$trimmed_size = $i + 1;
|
||||
for (; $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(($trimmed_size + $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");
|
||||
}
|
||||
}
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* Finalize a buffer, pointing to the given `$root_table`.
|
||||
* @param $root_table An offest to be added to the buffer.
|
||||
* @param $file_identifier A FlatBuffer file identifier to be added to the
|
||||
* buffer before `$root_table`. This defaults to `null`.
|
||||
* @throws InvalidArgumentException Thrown if an invalid `$identifier` is
|
||||
* given, where its length is not equal to
|
||||
* `Constants::FILE_IDENTIFIER_LENGTH`.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In order to save space, fields that are set to their default value don't
|
||||
* get serialized into the buffer.
|
||||
* @param bool $forceDefaults When set to `true`, always serializes default
|
||||
* values.
|
||||
*/
|
||||
public function forceDefaults($forceDefaults)
|
||||
{
|
||||
$this->force_defaults = $forceDefaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ByteBuffer representing the FlatBuffer.
|
||||
* @return ByteBuffer The ByteBuffer containing the FlatBuffer data.
|
||||
*/
|
||||
public function dataBuffer()
|
||||
{
|
||||
return $this->bb;
|
||||
}
|
||||
|
||||
/// @cond FLATBUFFERS_INTERNAL
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function dataStart()
|
||||
{
|
||||
return $this->space;
|
||||
}
|
||||
/// @endcond
|
||||
|
||||
/**
|
||||
* Utility function to copy and return the FlatBuffer data from the
|
||||
* underlying ByteBuffer.
|
||||
* @return string A string (representing a byte[]) that contains a copy
|
||||
* of the FlatBuffer data.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// @}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
<?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;
|
||||
|
||||
public function setByteBufferPos($pos)
|
||||
{
|
||||
$this->bb_pos = $pos;
|
||||
}
|
||||
|
||||
public function setByteBuffer($bb)
|
||||
{
|
||||
$this->bb = $bb;
|
||||
}
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
<?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()
|
||||
{
|
||||
}
|
||||
|
||||
public function setByteBufferPos($pos)
|
||||
{
|
||||
$this->bb_pos = $pos;
|
||||
}
|
||||
|
||||
public function setByteBuffer($bb)
|
||||
{
|
||||
$this->bb = $bb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_bytes($vector_offset, $elem_size=1)
|
||||
{
|
||||
$o = $this->__offset($vector_offset);
|
||||
if ($o == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return substr($this->bb->_buffer, $this->__vector($o), $this->__vector_len($o) * $elem_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $table
|
||||
* @param int $offset
|
||||
* @return Table
|
||||
*/
|
||||
protected function __union($table, $offset)
|
||||
{
|
||||
$offset += $this->bb_pos;
|
||||
$table->setByteBufferPos($offset + $this->bb->getInt($offset));
|
||||
$table->setByteBuffer($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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user