mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-14 15:35:31 +00:00
Add length check assertions
This commit is contained in:
parent
9cde63a327
commit
affed33066
@ -30,6 +30,9 @@ class Binary{
|
|||||||
const BIG_ENDIAN = 0x00;
|
const BIG_ENDIAN = 0x00;
|
||||||
const LITTLE_ENDIAN = 0x01;
|
const LITTLE_ENDIAN = 0x01;
|
||||||
|
|
||||||
|
private static function checkLength($str, $expect){
|
||||||
|
assert(($len = strlen($str)) === $expect, "Expected $expect bytes, got $len");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a 3-byte big-endian number
|
* Reads a 3-byte big-endian number
|
||||||
@ -39,6 +42,7 @@ class Binary{
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function readTriad($str){
|
public static function readTriad($str){
|
||||||
|
self::checkLength($str, 3);
|
||||||
return unpack("N", "\x00" . $str)[1];
|
return unpack("N", "\x00" . $str)[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +65,7 @@ class Binary{
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function readLTriad($str){
|
public static function readLTriad($str){
|
||||||
|
self::checkLength($str, 3);
|
||||||
return unpack("V", $str . "\x00")[1];
|
return unpack("V", $str . "\x00")[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,6 +233,7 @@ class Binary{
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function readByte($c, $signed = true){
|
public static function readByte($c, $signed = true){
|
||||||
|
self::checkLength($c, 1);
|
||||||
$b = ord($c{0});
|
$b = ord($c{0});
|
||||||
|
|
||||||
if($signed){
|
if($signed){
|
||||||
@ -260,6 +266,7 @@ class Binary{
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function readShort($str){
|
public static function readShort($str){
|
||||||
|
self::checkLength($str, 2);
|
||||||
return unpack("n", $str)[1];
|
return unpack("n", $str)[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,6 +278,7 @@ class Binary{
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function readSignedShort($str){
|
public static function readSignedShort($str){
|
||||||
|
self::checkLength($str, 2);
|
||||||
if(PHP_INT_SIZE === 8){
|
if(PHP_INT_SIZE === 8){
|
||||||
return unpack("n", $str)[1] << 48 >> 48;
|
return unpack("n", $str)[1] << 48 >> 48;
|
||||||
}else{
|
}else{
|
||||||
@ -297,6 +305,7 @@ class Binary{
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function readLShort($str){
|
public static function readLShort($str){
|
||||||
|
self::checkLength($str, 2);
|
||||||
return unpack("v", $str)[1];
|
return unpack("v", $str)[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,6 +317,7 @@ class Binary{
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function readSignedLShort($str){
|
public static function readSignedLShort($str){
|
||||||
|
self::checkLength($str, 2);
|
||||||
if(PHP_INT_SIZE === 8){
|
if(PHP_INT_SIZE === 8){
|
||||||
return unpack("v", $str)[1] << 48 >> 48;
|
return unpack("v", $str)[1] << 48 >> 48;
|
||||||
}else{
|
}else{
|
||||||
@ -327,6 +337,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readInt($str){
|
public static function readInt($str){
|
||||||
|
self::checkLength($str, 4);
|
||||||
if(PHP_INT_SIZE === 8){
|
if(PHP_INT_SIZE === 8){
|
||||||
return unpack("N", $str)[1] << 32 >> 32;
|
return unpack("N", $str)[1] << 32 >> 32;
|
||||||
}else{
|
}else{
|
||||||
@ -339,6 +350,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readLInt($str){
|
public static function readLInt($str){
|
||||||
|
self::checkLength($str, 4);
|
||||||
if(PHP_INT_SIZE === 8){
|
if(PHP_INT_SIZE === 8){
|
||||||
return unpack("V", $str)[1] << 32 >> 32;
|
return unpack("V", $str)[1] << 32 >> 32;
|
||||||
}else{
|
}else{
|
||||||
@ -351,6 +363,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readFloat($str){
|
public static function readFloat($str){
|
||||||
|
self::checkLength($str, 4);
|
||||||
return ENDIANNESS === self::BIG_ENDIAN ? unpack("f", $str)[1] : unpack("f", strrev($str))[1];
|
return ENDIANNESS === self::BIG_ENDIAN ? unpack("f", $str)[1] : unpack("f", strrev($str))[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,6 +372,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readLFloat($str){
|
public static function readLFloat($str){
|
||||||
|
self::checkLength($str, 4);
|
||||||
return ENDIANNESS === self::BIG_ENDIAN ? unpack("f", strrev($str))[1] : unpack("f", $str)[1];
|
return ENDIANNESS === self::BIG_ENDIAN ? unpack("f", strrev($str))[1] : unpack("f", $str)[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,6 +385,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readDouble($str){
|
public static function readDouble($str){
|
||||||
|
self::checkLength($str, 8);
|
||||||
return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", $str)[1] : unpack("d", strrev($str))[1];
|
return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", $str)[1] : unpack("d", strrev($str))[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,6 +394,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readLDouble($str){
|
public static function readLDouble($str){
|
||||||
|
self::checkLength($str, 8);
|
||||||
return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", strrev($str))[1] : unpack("d", $str)[1];
|
return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", strrev($str))[1] : unpack("d", $str)[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,6 +403,7 @@ class Binary{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function readLong($x){
|
public static function readLong($x){
|
||||||
|
self::checkLength($x, 8);
|
||||||
if(PHP_INT_SIZE === 8){
|
if(PHP_INT_SIZE === 8){
|
||||||
$int = unpack("N*", $x);
|
$int = unpack("N*", $x);
|
||||||
return ($int[1] << 32) | $int[2];
|
return ($int[1] << 32) | $int[2];
|
||||||
@ -428,7 +445,7 @@ class Binary{
|
|||||||
return self::readLong(strrev($str));
|
return self::readLong(strrev($str));
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: varlong, length checks
|
//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));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user