Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"php": "^8.4",
"ext-bcmath": "*",
"psr/simple-cache": "^3.0",
"thesis/endian": "^0.3.3",
"thesis/varint": "^0.2.0"
"thesis/endian": "^0.3.3"
},
"require-dev": {
"ext-filter": "*",
Expand Down
18 changes: 13 additions & 5 deletions src/Internal/Buffer/ReadBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
namespace Thesis\Protobuf\Internal\Buffer;

use Thesis\Protobuf\Exception\BufferUnderflow;
use Thesis\Protobuf\Exception\MalformedVarint;

/**
* @internal
*/
interface ReadBuffer extends \Countable
{
/**
* Peek should not move the cursor or remove data from the buffer.
* It should only return a `non-empty-string` of up to `n` bytes in size or throw an exception only if the buffer is empty.
* Reads a varint as the 64 bits it encodes: values of 2^63 and above come back as negative ints.
* Bits above the 64th, which only the 10th byte can carry, are dropped.
*
* @throws BufferUnderflow if the input or the current limit ends inside the varint
* @throws MalformedVarint if the varint does not end within 10 bytes
*/
public function readVarint(): int;

/**
* Like readVarint(), but leaves the cursor where it is.
*
* @param positive-int $n
* @return non-empty-string
* @throws BufferUnderflow
* @throws MalformedVarint
*/
public function peek(int $n): string;
public function peekVarint(): int;

/**
* Read must remove `n` bytes of data from the buffer and return a `non-empty-string`, or throw an exception
Expand Down
46 changes: 40 additions & 6 deletions src/Internal/Buffer/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Thesis\Protobuf\Internal\Buffer;

use Thesis\Protobuf\Exception\BufferUnderflow;
use Thesis\Protobuf\Exception\MalformedVarint;

/**
* Reads the input in place: a cursor moves over the original string and a nested record is read by narrowing
Expand All @@ -27,14 +28,21 @@ public function __construct(
}

#[\Override]
public function peek(int $n): string
public function readVarint(): int
{
if ($this->offset >= $this->limit) {
throw new BufferUnderflow('Buffer is empty.');
}
$offset = $this->offset;
$value = $this->decodeVarint($offset);
$this->offset = $offset;

return $value;
}

/** @var non-empty-string */
return substr($this->bytes, $this->offset, min($n, $this->limit - $this->offset));
#[\Override]
public function peekVarint(): int
{
$offset = $this->offset;

return $this->decodeVarint($offset);
}

#[\Override]
Expand Down Expand Up @@ -75,6 +83,32 @@ public function popLimit(int $limit): void
$this->limit = $limit;
}

/**
* @param non-negative-int $offset where the varint starts; on return, where it ends
* @throws BufferUnderflow
* @throws MalformedVarint
*/
private function decodeVarint(int &$offset): int
{
$value = 0;

// Shifting a PHP int left drops the bits above the 64th, so the 10th byte contributes only its low bit.
for ($shift = 0; $shift < 70; $shift += 7) {
if ($offset >= $this->limit) {
throw new BufferUnderflow('Unexpected end of input: truncated varint.');
}

$byte = \ord($this->bytes[$offset++]);
$value |= ($byte & 0x7F) << $shift;

if ($byte < 0x80) {
return $value;
}
}

throw new MalformedVarint('Malformed varint: longer than 10 bytes.');
}

#[\Override]
public function count(): int
{
Expand Down
9 changes: 5 additions & 4 deletions src/Internal/Buffer/buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Thesis\Protobuf\Internal\Buffer;

use BcMath\Number;
use Thesis\Protobuf\Exception\MalformedMessage;
use Thesis\Protobuf\Internal\Serde\SerdeVarint;
use Thesis\Protobuf\Internal\Wire;

/**
Expand All @@ -17,7 +15,7 @@
function copy(ByteBuffer $src, WriteBuffer $dst): void
{
$written = $src->flush();
SerdeVarint::T->serialize($dst, new Number(\strlen($written)));
Wire\writeVarint($dst, \strlen($written));
if ($written !== '') {
$dst->write($written);
}
Expand All @@ -32,5 +30,8 @@ function copy(ByteBuffer $src, WriteBuffer $dst): void
*/
function enterRecord(ReadBuffer $buffer): int
{
return $buffer->pushLimit((int) Wire\readVarint($buffer)->value);
$length = $buffer->readVarint();

// A negative length is a varint of 2^63 or above: far more than any input holds.
return $buffer->pushLimit($length >= 0 ? $length : PHP_INT_MAX);
}
20 changes: 6 additions & 14 deletions src/Internal/Serde/DeserializeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Thesis\Protobuf\Internal\Serde;

use BcMath\Number;
use Thesis\Protobuf\Internal\Buffer\ReadBuffer;
use Thesis\Protobuf\Internal\Wire;

/**
* @internal
Expand All @@ -27,22 +27,14 @@ public function __construct(
#[\Override]
public function deserialize(ReadBuffer $buffer): \BackedEnum|UnknownEnumValue
{
/** @var ?Number $p31 */
static $p31;
$p31 ??= new Number(2)->pow(31);

/** @var ?Number $p32 */
static $p32;
$p32 ??= new Number(2)->pow(32);

$raw = SerdeVarint::T->deserialize($buffer);
$raw = $buffer->readVarint();

// Enum numbers are int32: negative ones arrive sign-extended to 64 bits.
$num = $raw->mod($p32);
if ($num->compare($p31) >= 0) {
$num -= $p32;
$num = $raw & 0xFFFF_FFFF;
if ($num > 0x7FFF_FFFF) {
$num -= 0x1_0000_0000;
}

return $this->enum::tryFrom((int) $num->value) ?? new UnknownEnumValue($raw);
return $this->enum::tryFrom($num) ?? new UnknownEnumValue(Wire\toUnsigned($raw));
}
}
8 changes: 3 additions & 5 deletions src/Internal/Serde/SerdeBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Thesis\Protobuf\Internal\Serde;

use BcMath\Number;
use Thesis\Protobuf\Internal\Buffer\ReadBuffer;
use Thesis\Protobuf\Internal\Buffer\WriteBuffer;
use Thesis\Protobuf\Internal\Wire;

/**
* @internal
Expand All @@ -22,14 +22,12 @@ enum SerdeBool implements
#[\Override]
public function serialize(WriteBuffer $buffer, mixed $value): void
{
SerdeVarint::T->serialize($buffer, new Number((int) $value));
Wire\writeVarint($buffer, $value ? 1 : 0);
}

#[\Override]
public function deserialize(ReadBuffer $buffer): bool
{
$num = SerdeVarint::T->deserialize($buffer);

return (int) $num->value !== 0;
return $buffer->readVarint() !== 0;
}
}
11 changes: 8 additions & 3 deletions src/Internal/Serde/SerdeFixed64.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Thesis\Protobuf\Internal\Serde;

use BcMath\Number;
use Thesis\Endian;
use Thesis\Protobuf\Internal\Buffer\ReadBuffer;
use Thesis\Protobuf\Internal\Buffer\WriteBuffer;
use Thesis\Protobuf\Internal\Wire;

/**
* @internal
Expand All @@ -23,12 +23,17 @@ enum SerdeFixed64 implements
#[\Override]
public function serialize(WriteBuffer $buffer, mixed $value): void
{
$buffer->write(Endian\Order::Little->packUint64(Range::uint64($value, 'fixed64')));
/** @var non-empty-string $bytes */
$bytes = pack('P', Wire\fromUnsigned(Range::uint64($value, 'fixed64')));
$buffer->write($bytes);
}

#[\Override]
public function deserialize(ReadBuffer $buffer): Number
{
return Endian\Order::Little->unpackUint64($buffer->read(8));
/** @var array{1: int} $value */
$value = unpack('P', $buffer->read(8));

return Wire\toUnsigned($value[1]);
}
}
22 changes: 5 additions & 17 deletions src/Internal/Serde/SerdeInt32.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Thesis\Protobuf\Internal\Serde;

use BcMath\Number;
use Thesis\Protobuf\Internal\Buffer\ReadBuffer;
use Thesis\Protobuf\Internal\Buffer\WriteBuffer;
use Thesis\Protobuf\Internal\Wire;

/**
* @internal
Expand All @@ -23,27 +23,15 @@ enum SerdeInt32 implements
public function serialize(WriteBuffer $buffer, mixed $value): void
{
// A negative int32 is encoded as its 64-bit sign extension, exactly like an int64.
SerdeInt64::T->serialize($buffer, Range::int32($value, 'int32'));
Wire\writeVarint($buffer, Range::int32($value, 'int32'));
}

#[\Override]
public function deserialize(ReadBuffer $buffer): int
{
/** @var ?Number $p31 */
static $p31;
$p31 ??= new Number(2)->pow(31);
// An int32 is the low 32 bits of the varint, sign-extended.
$value = $buffer->readVarint() & 0xFFFF_FFFF;

/** @var ?Number $p32 */
static $p32;
$p32 ??= new Number(2)->pow(32);

$num = SerdeVarint::T->deserialize($buffer);
$num = $num->mod($p32);

if ($num->compare($p31) >= 0) {
$num -= $p32;
}

return (int) $num->value;
return $value > 0x7FFF_FFFF ? $value - 0x1_0000_0000 : $value;
}
}
40 changes: 4 additions & 36 deletions src/Internal/Serde/SerdeInt64.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Thesis\Protobuf\Internal\Serde;

use BcMath\Number;
use Thesis\Protobuf\Internal\Buffer\ReadBuffer;
use Thesis\Protobuf\Internal\Buffer\WriteBuffer;
use Thesis\Protobuf\Internal\Wire;

/**
* @internal
Expand All @@ -22,45 +22,13 @@ enum SerdeInt64 implements
#[\Override]
public function serialize(WriteBuffer $buffer, mixed $value): void
{
/** @var ?Number $p64 */
static $p64;
$p64 ??= new Number(2)->pow(64);

/** @var ?Number $p63 */
static $p63;
$p63 ??= new Number(2)->pow(63);

$num = new Number($value)->mod($p64);

if ($num->compare($p63) >= 0) {
$num -= $p64;
}

if ($num->compare(0) < 0) {
$num += $p64;
}

SerdeVarint::T->serialize($buffer, $num);
// A negative int64 is written as its 64 bits: a 10-byte varint.
Wire\writeVarint($buffer, $value);
}

#[\Override]
public function deserialize(ReadBuffer $buffer): int
{
/** @var ?Number $p63 */
static $p63;
$p63 ??= new Number(2)->pow(63);

/** @var ?Number $p64 */
static $p64;
$p64 ??= new Number(2)->pow(64);

$num = SerdeVarint::T->deserialize($buffer);
$num = $num->mod($p64);

if ($num->compare($p63) >= 0) {
$num -= $p64;
}

return (int) $num->value;
return $buffer->readVarint();
}
}
11 changes: 7 additions & 4 deletions src/Internal/Serde/SerdeSFixed64.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Thesis\Protobuf\Internal\Serde;

use BcMath\Number;
use Thesis\Endian;
use Thesis\Protobuf\Internal\Buffer\ReadBuffer;
use Thesis\Protobuf\Internal\Buffer\WriteBuffer;

Expand All @@ -23,12 +21,17 @@ enum SerdeSFixed64 implements
#[\Override]
public function serialize(WriteBuffer $buffer, mixed $value): void
{
$buffer->write(Endian\Order::Little->packInt64(new Number($value)));
/** @var non-empty-string $bytes */
$bytes = pack('P', $value);
$buffer->write($bytes);
}

#[\Override]
public function deserialize(ReadBuffer $buffer): int
{
return (int) Endian\Order::Little->unpackInt64($buffer->read(8))->value;
/** @var array{1: int} $value */
$value = unpack('P', $buffer->read(8));

return $value[1];
}
}
Loading
Loading