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
45 changes: 0 additions & 45 deletions src/CBOR/CBOR.php

This file was deleted.

80 changes: 80 additions & 0 deletions src/CBOR/MajorTypes/ByteString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace ATProto\Core\CBOR\MajorTypes;

use ValueError;

class ByteString
{
public const int MAJOR_TYPE = 0x02;

public static function validate(string $input): bool
{
return ((ord($input[0]) >> 5) & 0x07) === self::MAJOR_TYPE;
}

public static function encode(string $input): string
{
$len = strlen($input);

if ($len <= 0x17) {
$header = chr((self::MAJOR_TYPE << 5) | $len);
} else if ($len <= 0xFF) {
$header = chr((self::MAJOR_TYPE << 5) | 24) . chr($len);
} else if ($len <= 0xFFFF) {
$header = chr((self::MAJOR_TYPE << 5) | 25) . pack('n', $len);
} else if ($len <= 0xFFFFFFFF) {
$header = chr((self::MAJOR_TYPE << 5) | 26) . pack('N', $len);
} else {
$header = chr((self::MAJOR_TYPE << 5) | 27) . pack('J', $len);
}

return $header . $input;
}

public static function decode(string $input): string
{
if (! self::validate($input)) {
throw new ValueError("Invalid CBOR ByteString major type.");
}

$addInfo = ord($input[0]) & 0x1F;
$offset = 1;

if ($addInfo <= 23) {
$length = $addInfo;
}

if ($addInfo === 24) {
$length = ord($input[$offset]);
$offset += 1;
}

if ($addInfo === 25) {
$length = unpack('n', substr($input, $offset, 2))[1];
$offset += 2;
}

if ($addInfo === 26) {
$length = unpack('N', substr($input, $offset, 4))[1];
$offset += 4;
}

if ($addInfo === 27) {
$length = unpack('J', substr($input, $offset, 8))[1];
$offset += 8;
}

if (! isset($length)) {
throw new \ValueError('Invalid CBOR ByteString length information.');
}

$text = substr($input, $offset, $length);

if (strlen($text) !== $length) {
throw new \ValueError("Invalid CBOR ByteString length mismatch.");
}

return $text;
}
}
78 changes: 0 additions & 78 deletions tests/Unit/CBOR/CBORTest.php

This file was deleted.

81 changes: 81 additions & 0 deletions tests/Unit/CBOR/MajorTypes/ByteStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Tests\Unit\CBOR\MajorTypes;

use ATProto\Core\CBOR\MajorTypes\ByteString;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class ByteStringTest extends TestCase
{
#[DataProvider('provideValidCases')]
public function testValidate(string $input, string $header): void
{
$this->assertTrue(ByteString::validate($header . $input));
}

public static function provideValidCases(): array
{
return [
['f', "\x41"],
['fo', "\x42"],
['foo', "\x43"],
['foob', "\x44"],
['fooba', "\x45"],
['foobar', "\x46"],
[
str_repeat("This is a longer string.", 4),
"\x58\x60"
],
[
str_repeat("This is a longer string.", 12),
"\x59\x01\x20"
],
[
str_repeat("This is a longer string.", 2731),
"\x5A\x00\x01\x00\x08"
],
];
}

#[DataProvider('provideValidCases')]
public function testEncode(string $input, string $header): void
{
$expected = $header . $input;
$actual = ByteString::encode($input);

$this->assertSame(bin2hex($expected), bin2hex($actual));
}

#[DataProvider('provideValidCases')]
public function testDecode(string $input, string $header): void
{
$actual = ByteString::decode($header . $input);

$this->assertSame($input, $actual);
}

public function testDecodeThrowsExceptionForInvalidMajorType(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage("Invalid CBOR ByteString major type.");

ByteString::decode("\x0C");
}

public function testDecodeThrowsExceptionForLengthMismatch(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage("Invalid CBOR ByteString length mismatch.");

ByteString::decode("\x45\x66\x6F\x6F\x62");
}

public function testDecodeThrowsExceptionForInvalidAdditionalInformation(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage("Invalid CBOR ByteString length information.");

ByteString::decode("\x5F");
}
}
Loading