You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
785 B
PHTML
37 lines
785 B
PHTML
5 years ago
|
<?php
|
||
|
|
||
|
|
||
|
namespace CubiStore\Web\Utils;
|
||
|
|
||
|
use RuntimeException;
|
||
|
|
||
|
class Bytes
|
||
|
{
|
||
|
public static function readUint2LE($bytes, $offset = 0)
|
||
|
{
|
||
|
return static::unpackFirst('v', $bytes, $offset, 'int2');
|
||
|
}
|
||
|
|
||
|
public static function readUint4LE($bytes, $offset = 0)
|
||
|
{
|
||
|
return static::unpackFirst('V', $bytes, $offset, 'int4');
|
||
|
}
|
||
|
|
||
|
public static function readUint8LE($bytes, $offset = 0)
|
||
|
{
|
||
|
return static::unpackFirst('P', $bytes, $offset, 'int8');
|
||
|
}
|
||
|
|
||
|
|
||
|
private static function unpackFirst($format, $data, $offset, $what = 'bytes')
|
||
|
{
|
||
|
$result = unpack($format, $data, $offset);
|
||
|
|
||
|
if (!$result) {
|
||
|
throw new RuntimeException("Failed reading $what from buffer");
|
||
|
}
|
||
|
|
||
|
return reset($result);
|
||
|
}
|
||
|
}
|