mirror of
https://github.com/pmmp/ext-encoding.git
synced 2024-11-23 13:36:25 +00:00
56673dd789
the main benefit of this is the increased difficulty of accidentally using the wrong byte order due to auto complete - now choosing byte order is decided by the first character typed. however, it also reduces the verbosity of the API, though I'm not sure about the 'LE' and 'BE' class names.
53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
--TEST--
|
|
read(Un)SignedVar(Int|Long)() must terminate when too many bytes are given
|
|
--DESCRIPTION--
|
|
VarInt reading works by checking if the MSB (most significant bit) is set on the current byte.
|
|
If it is, it reads another byte.
|
|
|
|
However, this means that it's possible for a string of bytes longer than the max size of a varint/varlong to appear,
|
|
potentially locking up the read for a long time if the max number of bytes isn't capped.
|
|
|
|
This test verifies that the varint reader functions bail out if there are too many consecutive bytes with MSB set.
|
|
--EXTENSIONS--
|
|
encoding
|
|
--FILE--
|
|
<?php
|
|
|
|
use pmmp\encoding\ByteBuffer;
|
|
use pmmp\encoding\DataDecodeException;
|
|
use pmmp\encoding\VarInt;
|
|
|
|
$shortBuf = str_repeat("\x80", 6);
|
|
$longBuf = str_repeat("\x80", 11);
|
|
|
|
try{
|
|
VarInt::readUnsignedInt(new ByteBuffer($shortBuf));
|
|
}catch(DataDecodeException $e){
|
|
echo "uv32: " . $e->getMessage() . PHP_EOL;
|
|
}
|
|
|
|
try{
|
|
VarInt::readSignedInt(new ByteBuffer($shortBuf));
|
|
}catch(DataDecodeException $e){
|
|
echo "sv32: " . $e->getMessage() . PHP_EOL;
|
|
}
|
|
|
|
try{
|
|
VarInt::readUnsignedLong(new ByteBuffer($longBuf));
|
|
}catch(DataDecodeException $e){
|
|
echo "uv64: " . $e->getMessage() . PHP_EOL;
|
|
}
|
|
|
|
try{
|
|
VarInt::readSignedLong(new ByteBuffer($longBuf));
|
|
}catch(DataDecodeException $e){
|
|
echo "sv64: " . $e->getMessage() . PHP_EOL;
|
|
}
|
|
|
|
?>
|
|
--EXPECT--
|
|
uv32: VarInt did not terminate after 5 bytes!
|
|
sv32: VarInt did not terminate after 5 bytes!
|
|
uv64: VarInt did not terminate after 10 bytes!
|
|
sv64: VarInt did not terminate after 10 bytes!
|