Skip to content
Merged
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
42 changes: 4 additions & 38 deletions src/Internal/Buffer/ByteBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@

namespace Thesis\Protobuf\Internal\Buffer;

use Thesis\Protobuf\Exception\BufferUnderflow;

/**
* An append-only output buffer.
*
* @internal
*/
final class ByteBuffer implements
WriteBuffer,
ReadBuffer,
\Stringable
{
/** @var non-negative-int */
private int $length;
private int $length = 0;

public function __construct(
private string $buffer = '',
) {
$this->length = \strlen($this->buffer);
}
private string $buffer = '';

#[\Override]
public function write(string $value): void
Expand All @@ -30,35 +25,6 @@ public function write(string $value): void
$this->length += \strlen($value);
}

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

return $value;
}

#[\Override]
public function read(int $n): string
{
if ($this->length < $n) {
throw new BufferUnderflow("Expected '{$n}' bytes, but the buffer only has '{$this->length}' bytes.");
}

/** @var non-empty-string $buffer */
$buffer = substr($this->buffer, 0, $n);
$this->buffer = substr($this->buffer, $n);

/** @phpstan-ignore assign.propertyType */
$this->length -= $n;

return $buffer;
}

#[\Override]
public function flush(): string
{
$buffer = $this->buffer;
Expand Down
10 changes: 9 additions & 1 deletion src/Internal/Buffer/ReadBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ public function peek(int $n): string;
*/
public function read(int $n): string;

public function flush(): string;
/**
* Limits the buffer to the next `length` bytes, e.g. to the body of a nested message: count() and reads
* see only them. Returns the previous limit to restore with popLimit() once they are read.
*
* @throws BufferUnderflow if fewer than `length` bytes are left
*/
public function pushLimit(int $length): int;

public function popLimit(int $limit): void;
}
85 changes: 85 additions & 0 deletions src/Internal/Buffer/Reader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf\Internal\Buffer;

use Thesis\Protobuf\Exception\BufferUnderflow;

/**
* Reads the input in place: a cursor moves over the original string and a nested record is read by narrowing
* the limit, so no part of the input is copied except the values returned.
*
* @internal
*/
final class Reader implements ReadBuffer
{
/** @var non-negative-int */
private int $offset = 0;

/** @var non-negative-int */
private int $limit;

public function __construct(
private readonly string $bytes,
) {
$this->limit = \strlen($bytes);
}

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

/** @var non-empty-string */
return substr($this->bytes, $this->offset, min($n, $this->limit - $this->offset));
}

#[\Override]
public function read(int $n): string
{
$available = $this->limit - $this->offset;
if ($available < $n) {
throw new BufferUnderflow("Expected '{$n}' bytes, but the buffer only has '{$available}' bytes.");
}

/** @var non-empty-string $value */
$value = substr($this->bytes, $this->offset, $n);
$this->offset += $n;

return $value;
}

#[\Override]
public function pushLimit(int $length): int
{
$available = $this->limit - $this->offset;
if ($length < 0 || $length > $available) {
throw new BufferUnderflow("Expected '{$length}' bytes, but the buffer only has '{$available}' bytes.");
}

$limit = $this->limit;
$this->limit = $this->offset + $length;

return $limit;
}

#[\Override]
public function popLimit(int $limit): void
{
// A limit pushed later, or restored out of order, would lie behind the cursor or past the input.
\assert($limit >= $this->offset && $limit <= \strlen($this->bytes), 'Limits must be restored in reverse order.');

$this->limit = $limit;
}

#[\Override]
public function count(): int
{
// The cursor never passes the limit: read() and pushLimit() check the bytes available.
/** @var non-negative-int */
return $this->limit - $this->offset;
}
}
18 changes: 8 additions & 10 deletions src/Internal/Buffer/buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use Thesis\Protobuf\Internal\Wire;

/**
* Writes the contents of `src` to `dst` as a length-delimited record.
*
* @internal
*/
function copy(ReadBuffer $src, WriteBuffer $dst): void
function copy(ByteBuffer $src, WriteBuffer $dst): void
{
$written = $src->flush();
SerdeVarint::T->serialize($dst, new Number(\strlen($written)));
Expand All @@ -22,17 +24,13 @@ function copy(ReadBuffer $src, WriteBuffer $dst): void
}

/**
* Reads the length of a length-delimited record and limits the buffer to its body.
*
* @internal
* @return int the previous limit, to restore with popLimit() once the body is read
* @throws MalformedMessage
*/
function slice(ReadBuffer $src): ReadBuffer
function enterRecord(ReadBuffer $buffer): int
{
$dst = new ByteBuffer();
$length = (int) Wire\readVarint($src)->value;

if ($length > 0) {
$dst->write($src->read($length));
}

return $dst;
return $buffer->pushLimit((int) Wire\readVarint($buffer)->value);
}
4 changes: 3 additions & 1 deletion src/Internal/Serde/DeserializeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public function deserialize(ReadBuffer $buffer): array
$values = [];

if ($this->isPacked) {
$buffer = Buffer\slice($buffer);
$limit = Buffer\enterRecord($buffer);

while (\count($buffer) > 0) {
$values[] = $this->deserializer->deserialize($buffer);
}

$buffer->popLimit($limit);
} else {
$values[] = $this->deserializer->deserialize($buffer);

Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Serde/DeserializeMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
#[\Override]
public function deserialize(ReadBuffer $buffer): Message
{
$buffer = Buffer\slice($buffer);
$limit = Buffer\enterRecord($buffer);

/** @var list<FieldDescriptor<*>> $descriptors */
$descriptors = [];
Expand Down Expand Up @@ -65,6 +65,8 @@ public function deserialize(ReadBuffer $buffer): Message
);
}

$buffer->popLimit($limit);

return new Message($descriptors, $unknowns);
}
}
3 changes: 2 additions & 1 deletion src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Thesis\Protobuf\Exception\MalformedMessage;
use Thesis\Protobuf\Internal\Buffer\ByteBuffer;
use Thesis\Protobuf\Internal\Buffer\Reader;
use Thesis\Protobuf\Internal\Serde\UnknownEnumValue;
use Thesis\Protobuf\Internal\Wire;
use Thesis\Protobuf\Type\MessageT;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function serialize(Message $message): string
*/
public function deserialize(MessageT $type, string $bytes): Message
{
$buffer = new ByteBuffer($bytes);
$buffer = new Reader($bytes);

/** @var list<FieldDescriptor<*>> $descriptors */
$descriptors = [];
Expand Down
127 changes: 127 additions & 0 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

namespace Thesis\Protobuf;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Thesis\Protobuf\Exception\BufferUnderflow;
use Thesis\Protobuf\Internal\Buffer\ByteBuffer;
use Thesis\Protobuf\Internal\Buffer\Reader;

#[CoversClass(Reader::class)]
#[CoversClass(ByteBuffer::class)]
final class BufferTest extends TestCase
{
public function testPeekDoesNotMoveTheCursor(): void
{
$buffer = new Reader('test');
self::assertCount(4, $buffer);
self::assertSame('te', $buffer->peek(2));
self::assertCount(4, $buffer);
self::assertSame('test', $buffer->peek(5));
self::assertCount(4, $buffer);
self::assertSame('test', $buffer->read(4));
self::assertCount(0, $buffer);
}

public function testPeekAtTheEndFails(): void
{
$buffer = new Reader('t');
$buffer->read(1);

$this->expectExceptionObject(new BufferUnderflow('Buffer is empty.'));
$buffer->peek(1);
}

public function testReadMovesTheCursor(): void
{
$buffer = new Reader('test');
self::assertSame('te', $buffer->read(2));
self::assertSame('st', $buffer->read(2));

$this->expectExceptionObject(new BufferUnderflow("Expected '1' bytes, but the buffer only has '0' bytes."));
$buffer->read(1);
}

public function testLimitNarrowsAndRestoresTheBuffer(): void
{
$buffer = new Reader('abcdef');
self::assertSame('a', $buffer->read(1));

$limit = $buffer->pushLimit(3);
self::assertCount(3, $buffer);
self::assertSame('bcd', $buffer->peek(10), 'peek stops at the limit');
self::assertSame('bc', $buffer->read(2));

$inner = $buffer->pushLimit(1);
self::assertSame('d', $buffer->read(1));
self::assertCount(0, $buffer);
$buffer->popLimit($inner);
self::assertCount(0, $buffer, 'the outer limit is reached too');

$buffer->popLimit($limit);
self::assertCount(2, $buffer);
self::assertSame('ef', $buffer->read(2));
}

public function testReadBeyondTheLimitFails(): void
{
$buffer = new Reader('abcdef');
$buffer->pushLimit(2);

$this->expectExceptionObject(new BufferUnderflow("Expected '3' bytes, but the buffer only has '2' bytes."));
$buffer->read(3);
}

public function testLimitBeyondTheEndFails(): void
{
$buffer = new Reader('abc');
$buffer->read(1);

$this->expectExceptionObject(new BufferUnderflow("Expected '3' bytes, but the buffer only has '2' bytes."));
$buffer->pushLimit(3);
}

public function testNegativeLimitFails(): void
{
$this->expectException(BufferUnderflow::class);

new Reader('abc')->pushLimit(-1);
}

public function testLimitsRestoredOutOfOrderAreCaught(): void
{
$buffer = new Reader('abcdef');
$buffer->pushLimit(4);
$inner = $buffer->pushLimit(2);
$buffer->read(2);
$buffer->popLimit($inner);
$buffer->read(1);

// The inner limit (2) is behind the cursor (3) now: restoring it again is a bug.
$this->expectException(\AssertionError::class);
$this->expectExceptionMessage('Limits must be restored in reverse order.');
$buffer->popLimit(2);
}

public function testLimitPastTheInputIsCaught(): void
{
$this->expectException(\AssertionError::class);

new Reader('abc')->popLimit(4);
}

public function testWriterAppendsAndFlushes(): void
{
$buffer = new ByteBuffer();
$buffer->write('te');
$buffer->write('st');
self::assertCount(4, $buffer);
self::assertSame('test', (string) $buffer);
self::assertSame('test', $buffer->flush());
self::assertCount(0, $buffer);
self::assertSame('', (string) $buffer);
}
}
Loading
Loading