GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
Binary Class Reference

Static Public Member Functions

static readTriad ($str)
 
static writeTriad ($value)
 
static readLTriad ($str)
 
static writeLTriad ($value)
 
static readBool ($b)
 
static writeBool ($b)
 
static readByte ($c, $signed=true)
 
static writeByte ($c)
 
static readShort ($str)
 
static readSignedShort ($str)
 
static writeShort ($value)
 
static readLShort ($str)
 
static readSignedLShort ($str)
 
static writeLShort ($value)
 
static readInt ($str)
 
static writeInt ($value)
 
static readLInt ($str)
 
static writeLInt ($value)
 
static readFloat ($str, int $accuracy=-1)
 
static writeFloat ($value)
 
static readLFloat ($str, int $accuracy=-1)
 
static writeLFloat ($value)
 
static printFloat ($value)
 
static readDouble ($str)
 
static writeDouble ($value)
 
static readLDouble ($str)
 
static writeLDouble ($value)
 
static readLong ($x)
 
static writeLong ($value)
 
static readLLong ($str)
 
static writeLLong ($value)
 
static readVarInt ($stream)
 
static readUnsignedVarInt ($stream)
 
static writeVarInt ($v)
 
static writeUnsignedVarInt ($value)
 

Data Fields

const BIG_ENDIAN = 0x00
 
const LITTLE_ENDIAN = 0x01
 

Member Function Documentation

◆ printFloat()

static printFloat (   $value)
static
Parameters
$value
Returns
mixed
326  {
327  return preg_replace("/(\\.\\d+?)0+$/", "$1", sprintf("%F", $value));
328  }

◆ readBool()

static readBool (   $b)
static

Reads a byte boolean

Parameters
$b
Returns
bool
96  {
97  return self::readByte($b, false) === 0 ? false : true;
98  }

◆ readByte()

static readByte (   $c,
  $signed = true 
)
static

Reads an unsigned/signed byte

Parameters
string$c
bool$signed
Returns
int
119  {
120  self::checkLength($c, 1);
121  $b = ord($c{0});
122 
123  if($signed){
124  if(PHP_INT_SIZE === 8){
125  return $b << 56 >> 56;
126  }else{
127  return $b << 24 >> 24;
128  }
129  }else{
130  return $b;
131  }
132  }

◆ readDouble()

static readDouble (   $str)
static
Parameters
$str
Returns
mixed
335  {
336  self::checkLength($str, 8);
337 
338  return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", $str)[1] : unpack("d", strrev($str))[1];
339  }

◆ readFloat()

static readFloat (   $str,
int  $accuracy = -1 
)
static
Parameters
$str
int$accuracy
Returns
float
277  {
278  self::checkLength($str, 4);
279  $value = ENDIANNESS === self::BIG_ENDIAN ? unpack("f", $str)[1] : unpack("f", strrev($str))[1];
280  if($accuracy > -1){
281  return round($value, $accuracy);
282  }else{
283  return $value;
284  }
285  }

◆ readInt()

static readInt (   $str)
static
Parameters
$str
Returns
int
230  {
231  self::checkLength($str, 4);
232  if(PHP_INT_SIZE === 8){
233  return unpack("N", $str)[1] << 32 >> 32;
234  }else{
235  return unpack("N", $str)[1];
236  }
237  }

◆ readLDouble()

static readLDouble (   $str)
static
Parameters
$str
Returns
mixed
355  {
356  self::checkLength($str, 8);
357 
358  return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", strrev($str))[1] : unpack("d", $str)[1];
359  }

◆ readLFloat()

static readLFloat (   $str,
int  $accuracy = -1 
)
static
Parameters
$str
int$accuracy
Returns
float
302  {
303  self::checkLength($str, 4);
304  $value = ENDIANNESS === self::BIG_ENDIAN ? unpack("f", strrev($str))[1] : unpack("f", $str)[1];
305  if($accuracy > -1){
306  return round($value, $accuracy);
307  }else{
308  return $value;
309  }
310  }

◆ readLInt()

static readLInt (   $str)
static
Parameters
$str
Returns
int
253  {
254  self::checkLength($str, 4);
255  if(PHP_INT_SIZE === 8){
256  return unpack("V", $str)[1] << 32 >> 32;
257  }else{
258  return unpack("V", $str)[1];
259  }
260  }

◆ readLLong()

static readLLong (   $str)
static
Parameters
$str
Returns
int|string
425  {
426  return self::readLong(strrev($str));
427  }

◆ readLong()

static readLong (   $x)
static
Parameters
$x
Returns
int|string
375  {
376  self::checkLength($x, 8);
377  if(PHP_INT_SIZE === 8){
378  $int = unpack("N*", $x);
379 
380  return ($int[1] << 32) | $int[2];
381  }else{
382  $value = "0";
383  for($i = 0; $i < 8; $i += 2){
384  $value = bcmul($value, "65536", 0);
385  $value = bcadd($value, self::readShort(substr($x, $i, 2)), 0);
386  }
387 
388  if(bccomp($value, "9223372036854775807") == 1){
389  $value = bcadd($value, "-18446744073709551616");
390  }
391 
392  return $value;
393  }
394  }

◆ readLShort()

static readLShort (   $str)
static

Reads a 16-bit unsigned little-endian number

Parameters
$str
Returns
int
192  {
193  self::checkLength($str, 2);
194 
195  return unpack("v", $str)[1];
196  }

◆ readLTriad()

static readLTriad (   $str)
static

Reads a 3-byte little-endian number

Parameters
$str
Returns
mixed
72  {
73  self::checkLength($str, 3);
74 
75  return unpack("V", $str . "\x00")[1];
76  }

◆ readShort()

static readShort (   $str)
static

Reads a 16-bit unsigned big-endian number

Parameters
$str
Returns
int
152  {
153  self::checkLength($str, 2);
154 
155  return unpack("n", $str)[1];
156  }

◆ readSignedLShort()

static readSignedLShort (   $str)
static

Reads a 16-bit signed little-endian number

Parameters
$str
Returns
int
205  {
206  self::checkLength($str, 2);
207  if(PHP_INT_SIZE === 8){
208  return unpack("v", $str)[1] << 48 >> 48;
209  }else{
210  return unpack("v", $str)[1] << 16 >> 16;
211  }
212  }

◆ readSignedShort()

static readSignedShort (   $str)
static

Reads a 16-bit signed big-endian number

Parameters
$str
Returns
int
165  {
166  self::checkLength($str, 2);
167  if(PHP_INT_SIZE === 8){
168  return unpack("n", $str)[1] << 48 >> 48;
169  }else{
170  return unpack("n", $str)[1] << 16 >> 16;
171  }
172  }

◆ readTriad()

static readTriad (   $str)
static

Reads a 3-byte big-endian number

Parameters
$str
Returns
mixed
48  {
49  self::checkLength($str, 3);
50 
51  return unpack("N", "\x00" . $str)[1];
52  }

◆ readUnsignedVarInt()

static readUnsignedVarInt (   $stream)
static
Parameters
$stream
Returns
int
458  {
459  $value = 0;
460  $i = 0;
461  do{
462  if($i > 63){
463  throw new \InvalidArgumentException("Varint did not terminate after 10 bytes!");
464  }
465  $value |= ((($b = $stream->getByte()) & 0x7f) << $i);
466  $i += 7;
467  }while($b & 0x80);
468 
469  return $value;
470  }

◆ readVarInt()

static readVarInt (   $stream)
static
Parameters
$stream
Returns
int
445  {
446  $shift = PHP_INT_SIZE === 8 ? 63 : 31;
447  $raw = self::readUnsignedVarInt($stream);
448  $temp = ((($raw << $shift) >> $shift) ^ $raw) >> 1;
449 
450  return $temp ^ ($raw & (1 << $shift));
451  }

◆ writeBool()

static writeBool (   $b)
static

Writes a byte boolean

Parameters
$b
Returns
bool|string
107  {
108  return self::writeByte($b === true ? 1 : 0);
109  }

◆ writeByte()

static writeByte (   $c)
static

Writes an unsigned/signed byte

Parameters
$c
Returns
string
141  {
142  return chr($c);
143  }

◆ writeDouble()

static writeDouble (   $value)
static
Parameters
$value
Returns
string
346  {
347  return ENDIANNESS === self::BIG_ENDIAN ? pack("d", $value) : strrev(pack("d", $value));
348  }

◆ writeFloat()

static writeFloat (   $value)
static
Parameters
$value
Returns
string
292  {
293  return ENDIANNESS === self::BIG_ENDIAN ? pack("f", $value) : strrev(pack("f", $value));
294  }

◆ writeInt()

static writeInt (   $value)
static
Parameters
$value
Returns
string
244  {
245  return pack("N", $value);
246  }

◆ writeLDouble()

static writeLDouble (   $value)
static
Parameters
$value
Returns
string
366  {
367  return ENDIANNESS === self::BIG_ENDIAN ? strrev(pack("d", $value)) : pack("d", $value);
368  }

◆ writeLFloat()

static writeLFloat (   $value)
static
Parameters
$value
Returns
string
317  {
318  return ENDIANNESS === self::BIG_ENDIAN ? strrev(pack("f", $value)) : pack("f", $value);
319  }

◆ writeLInt()

static writeLInt (   $value)
static
Parameters
$value
Returns
string
267  {
268  return pack("V", $value);
269  }

◆ writeLLong()

static writeLLong (   $value)
static
Parameters
$value
Returns
string
434  {
435  return strrev(self::writeLong($value));
436  }

◆ writeLong()

static writeLong (   $value)
static
Parameters
$value
Returns
string
401  {
402  if(PHP_INT_SIZE === 8){
403  return pack("NN", $value >> 32, $value & 0xFFFFFFFF);
404  }else{
405  $x = "";
406 
407  if(bccomp($value, "0") == -1){
408  $value = bcadd($value, "18446744073709551616");
409  }
410 
411  $x .= self::writeShort(bcmod(bcdiv($value, "281474976710656"), "65536"));
412  $x .= self::writeShort(bcmod(bcdiv($value, "4294967296"), "65536"));
413  $x .= self::writeShort(bcmod(bcdiv($value, "65536"), "65536"));
414  $x .= self::writeShort(bcmod($value, "65536"));
415 
416  return $x;
417  }
418  }

◆ writeLShort()

static writeLShort (   $value)
static

Writes a 16-bit signed/unsigned little-endian number

Parameters
$value
Returns
string
221  {
222  return pack("v", $value);
223  }

◆ writeLTriad()

static writeLTriad (   $value)
static

Writes a 3-byte little-endian number

Parameters
$value
Returns
string
85  {
86  return substr(pack("V", $value), 0, -1);
87  }

◆ writeShort()

static writeShort (   $value)
static

Writes a 16-bit signed/unsigned big-endian number

Parameters
$value
Returns
string
181  {
182  return pack("n", $value);
183  }

◆ writeTriad()

static writeTriad (   $value)
static

Writes a 3-byte big-endian number

Parameters
$value
Returns
string
61  {
62  return substr(pack("N", $value), 1);
63  }

◆ writeUnsignedVarInt()

static writeUnsignedVarInt (   $value)
static
Parameters
$value
Returns
string
486  {
487  $buf = "";
488  for($i = 0; $i < 10; ++$i){
489  if(($value >> 7) !== 0){
490  $buf .= chr($value | 0x80); //Let chr() take the last byte of this, it's faster than adding another & 0x7f.
491  }else{
492  $buf .= chr($value & 0x7f);
493 
494  return $buf;
495  }
496  $value = (($value >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
497  }
498  throw new \InvalidArgumentException("Value too large to be encoded as a varint");
499  }

◆ writeVarInt()

static writeVarInt (   $v)
static
Parameters
$v
Returns
string
477  {
478  return self::writeUnsignedVarInt(($v << 1) ^ ($v >> (PHP_INT_SIZE === 8 ? 63 : 31)));
479  }

Field Documentation

◆ BIG_ENDIAN

const BIG_ENDIAN = 0x00

◆ LITTLE_ENDIAN

const LITTLE_ENDIAN = 0x01

The documentation for this class was generated from the following file: