mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-11 12:27:51 +00:00
Add varint length checks (10 bytes, currently no proper varlong support)
This commit is contained in:
parent
2ffbb452bb
commit
2dfca14714
@ -436,12 +436,12 @@ class Binary{
|
|||||||
return self::readLong(strrev($str));
|
return self::readLong(strrev($str));
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: varlong, varint length checks
|
|
||||||
|
|
||||||
public static function writeLLong($value){
|
public static function writeLLong($value){
|
||||||
return strrev(self::writeLong($value));
|
return strrev(self::writeLong($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: proper varlong support
|
||||||
|
|
||||||
public static function readVarInt($stream){
|
public static function readVarInt($stream){
|
||||||
$shift = PHP_INT_SIZE === 8 ? 63 : 31;
|
$shift = PHP_INT_SIZE === 8 ? 63 : 31;
|
||||||
$raw = self::readUnsignedVarInt($stream);
|
$raw = self::readUnsignedVarInt($stream);
|
||||||
@ -453,6 +453,9 @@ class Binary{
|
|||||||
$value = 0;
|
$value = 0;
|
||||||
$i = 0;
|
$i = 0;
|
||||||
do{
|
do{
|
||||||
|
if($i > 63){
|
||||||
|
throw new \InvalidArgumentException("Varint did not terminate after 10 bytes!");
|
||||||
|
}
|
||||||
$value |= ((($b = $stream->getByte()) & 0x7f) << $i);
|
$value |= ((($b = $stream->getByte()) & 0x7f) << $i);
|
||||||
$i += 7;
|
$i += 7;
|
||||||
}while($b & 0x80);
|
}while($b & 0x80);
|
||||||
@ -466,13 +469,18 @@ class Binary{
|
|||||||
|
|
||||||
public static function writeUnsignedVarInt($v){
|
public static function writeUnsignedVarInt($v){
|
||||||
$buf = "";
|
$buf = "";
|
||||||
|
$loops = 0;
|
||||||
do{
|
do{
|
||||||
|
if($loops > 9){
|
||||||
|
throw new \InvalidArgumentException("Varint cannot be longer than 10 bytes!"); //for safety reasons
|
||||||
|
}
|
||||||
$w = $v & 0x7f;
|
$w = $v & 0x7f;
|
||||||
if(($v >> 7) !== 0){
|
if(($v >> 7) !== 0){
|
||||||
$w = $v | 0x80;
|
$w = $v | 0x80;
|
||||||
}
|
}
|
||||||
$buf .= self::writeByte($w);
|
$buf .= self::writeByte($w);
|
||||||
$v = (($v >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
|
$v = (($v >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
|
||||||
|
++$loops;
|
||||||
}while($v);
|
}while($v);
|
||||||
|
|
||||||
return $buf;
|
return $buf;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user