- var_dump: documentation ( source)
- str_repeat: documentation ( source)
- ord: documentation ( source)
<?php
function readUnsignedVarLong(string $buffer, int &$offset) : int{
$value = 0;
for($i = 0; $i <= 63; $i += 7){
if(!isset($buffer[$offset])){
throw new Exception("No bytes left in buffer");
}
$b = ord($buffer[$offset++]);
$value |= (($b & 0x7f) << $i);
if(($b & 0x80) === 0){
return $value;
}
}
throw new Exception("VarLong did not terminate after 10 bytes!");
}
var_dump(readUnsignedVarLong(str_repeat("\255", 10)));