Add length check assertions

This commit is contained in:
Tatsuyuki Ishi 2016-10-04 13:13:27 +01:00 committed by Dylan K. Taylor
parent 9cde63a327
commit affed33066

View File

@ -30,6 +30,9 @@ class Binary{
const BIG_ENDIAN = 0x00;
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
@ -39,6 +42,7 @@ class Binary{
* @return mixed
*/
public static function readTriad($str){
self::checkLength($str, 3);
return unpack("N", "\x00" . $str)[1];
}
@ -61,6 +65,7 @@ class Binary{
* @return mixed
*/
public static function readLTriad($str){
self::checkLength($str, 3);
return unpack("V", $str . "\x00")[1];
}
@ -228,6 +233,7 @@ class Binary{
* @return int
*/
public static function readByte($c, $signed = true){
self::checkLength($c, 1);
$b = ord($c{0});
if($signed){
@ -260,6 +266,7 @@ class Binary{
* @return int
*/
public static function readShort($str){
self::checkLength($str, 2);
return unpack("n", $str)[1];
}
@ -271,6 +278,7 @@ class Binary{
* @return int
*/
public static function readSignedShort($str){
self::checkLength($str, 2);
if(PHP_INT_SIZE === 8){
return unpack("n", $str)[1] << 48 >> 48;
}else{
@ -297,6 +305,7 @@ class Binary{
* @return int
*/
public static function readLShort($str){
self::checkLength($str, 2);
return unpack("v", $str)[1];
}
@ -308,6 +317,7 @@ class Binary{
* @return int
*/
public static function readSignedLShort($str){
self::checkLength($str, 2);
if(PHP_INT_SIZE === 8){
return unpack("v", $str)[1] << 48 >> 48;
}else{
@ -327,6 +337,7 @@ class Binary{
}
public static function readInt($str){
self::checkLength($str, 4);
if(PHP_INT_SIZE === 8){
return unpack("N", $str)[1] << 32 >> 32;
}else{
@ -339,6 +350,7 @@ class Binary{
}
public static function readLInt($str){
self::checkLength($str, 4);
if(PHP_INT_SIZE === 8){
return unpack("V", $str)[1] << 32 >> 32;
}else{
@ -351,6 +363,7 @@ class Binary{
}
public static function readFloat($str){
self::checkLength($str, 4);
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){
self::checkLength($str, 4);
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){
self::checkLength($str, 8);
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){
self::checkLength($str, 8);
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){
self::checkLength($x, 8);
if(PHP_INT_SIZE === 8){
$int = unpack("N*", $x);
return ($int[1] << 32) | $int[2];
@ -428,7 +445,7 @@ class Binary{
return self::readLong(strrev($str));
}
//TODO: varlong, length checks
//TODO: varlong, varint length checks
public static function writeLLong($value){
return strrev(self::writeLong($value));