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
16 changes: 14 additions & 2 deletions src/CBOR/CBOR.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ATProto\Core\CBOR;

use ATProto\Core\CBOR\MajorTypes\TextString;
use ATProto\Core\CBOR\MajorTypes\UnsignedInteger;

class CBOR
Expand All @@ -21,13 +22,24 @@ public static function encode(string|int|array $data): string
case 'integer':
return UnsignedInteger::encode($data);
break;
case 'string':
return TextString::encode($data);
break;
}

throw new \ValueError("Unsupported type: " . gettype($data));
}

public static function decode(string $data): int
public static function decode(string $data): int|string
{
return UnsignedInteger::decode((string) $data);
if (TextString::validate($data)) {
return TextString::decode($data);
}

if (UnsignedInteger::validate($data)) {
return UnsignedInteger::decode($data);
}

throw new \ValueError("Unsupported type.");
}
}
75 changes: 75 additions & 0 deletions src/CBOR/MajorTypes/TextString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types = 1);

/**
* This file is part of the ATProto Core package.
*
* (c) Core Branch
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

namespace ATProto\Core\CBOR\MajorTypes;

class TextString
{
public static function encode(string $input): string
{
$length = strlen($input);

if ($length <= 23) {
$header = chr((0x03 << 5) | $length);
} elseif ($length <= 0xFF) {
$header = chr((0x03 << 5) | 24) . chr($length);
} elseif ($length <= 0xFFFF) {
$header = chr((0x03 << 5) | 25) . pack('n', $length);
} elseif ($length <= 0xFFFFFFFF) {
$header = chr((0x03 << 5) | 26) . pack('N', $length);
} else {
$header = chr((0x03 << 5) | 27) . pack('J', $length);
}

return $header . $input;
}

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

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

if ($additionalInfo <= 23) {
$length = $additionalInfo;
} elseif ($additionalInfo === 24) {
$length = ord($input[$offset]);
$offset += 1;
} elseif ($additionalInfo === 25) {
$length = unpack('n', substr($input, $offset, 2))[1];
$offset += 2;
} elseif ($additionalInfo === 26) {
$length = unpack('N', substr($input, $offset, 4))[1];
$offset += 4;
} elseif ($additionalInfo === 27) {
$length = unpack('J', substr($input, $offset, 8))[1];
$offset += 8;
} else {
throw new \ValueError('Invalid CBOR TextString length information.');
}

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

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

return $text;
}

public static function validate(string $input): bool
{
return ((ord($input[0]) >> 5) & 0x07) === 0x03;
}
}
15 changes: 9 additions & 6 deletions src/CBOR/MajorTypes/UnsignedInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ class UnsignedInteger
{
public static function decode(string $data): int
{
$firstByte = ord($data[0]);
$majorType = ($firstByte >> 5) & 0x07;
$additionalInfo = $firstByte & 0x1F;

if ($majorType !== 0) {
throw new ValueError("Invalid major type for unsigned integer: $majorType");
if (! self::validate($data)) {
throw new ValueError("Invalid major type for unsigned integer.");
}

$additionalInfo = ord($data[0]) & 0x1F;

if ($additionalInfo <= 23) {
$value = $additionalInfo;
} elseif ($additionalInfo === 24) {
Expand Down Expand Up @@ -73,4 +71,9 @@ public static function encode(int $value): string

return $prefixedPack('J', "\x1B");
}

public static function validate(string $input): bool
{
return ((ord($input[0]) >> 5) & 0x07) === 0x00;
}
}
24 changes: 20 additions & 4 deletions tests/Unit/CBOR/CBORTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,36 @@
class CBORTest extends TestCase
{
#[DataProvider('validCases')]
public function testEncode(int $data, string $expected): void
public function testEncode(int|string $data, string $expected): void
{
$encoded = CBOR::encode($data);
$this->assertSame($expected, $encoded);
}

#[DataProvider('validCases')]
public function testDecode(int $expected, string $data): void
public function testDecode(int|string $expected, string $data): void
{
$actual = CBOR::decode($data);

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

public function testCBORDecodeThrowsExceptionWhenPassedUnsupportedType(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage('Unsupported type.');

CBOR::decode("\x80"); // CBOR array
}

public function testCBOREncodeThrowsExceptionWhenPassedUnsupportedType(): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage('Unsupported type: array');

CBOR::encode(array());
}

/**
* @return array[]
*/
Expand All @@ -42,8 +58,8 @@ public static function validCases(): array
[1, hex2bin('01')], // 1 encoded as CBOR unsigned integer
[10, hex2bin('0a')], // 10 encoded as CBOR unsigned integer

// // String test cases
// [['hello'], hex2bin('6568656c6c6f')], // "hello" encoded as CBOR text string
// String test cases
['hello', hex2bin('6568656C6C6F')], // "hello" encoded as CBOR text string
//
// // Boolean test cases
// [[true], hex2bin('f5')], // true encoded as CBOR special type
Expand Down
87 changes: 87 additions & 0 deletions tests/Unit/CBOR/MajorTypes/TextStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types = 1);

/**
* This file is part of the ATProto Core package.
*
* (c) Core Branch
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

namespace Tests\Unit\CBOR\MajorTypes;

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

class TextStringTest extends TestCase
{
#[DataProvider('provideValidCases')]
public function testEncodeProducesCorrectCBORRepresentation(string $input, string $expectedHeader): void
{
$actual = bin2hex(TextString::encode($input));
$expected = bin2hex($expectedHeader . $input);

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

#[DataProvider('provideValidCases')]
public function testDecodeExtractsOriginalStringFromCBORRepresentation(string $input, string $header): void
{
$encoded = $header . $input;

$actual = TextString::decode($encoded);
$expected = $input;

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

public static function provideValidCases(): array
{
return [
["f", "\x61"],
["fo", "\x62"],
["foo", "\x63"],
["foob", "\x64"],
["fooba", "\x65"],
["foobar", "\x66"],
[
"This is a longer string. This is a longer string. This is a longer string. This is a longer string.",
"\x78\x63"
],
[
"This is a longer string. This is a longer string. This is a longer string. This is a longer string.
This is a longer string. This is a longer string. This is a longer string. This is a longer string.
This is a longer string. This is a longer string. This is a longer string. This is a longer string.",
"\x79\x01\x4D"
]
];
}

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

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

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

// Encoded length is 5, but actual length is 4
TextString::decode("\x65\x66\x6F\x6F\x62");
}

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

// Additional info 28 is invalid for text strings
TextString::decode("\x7C\x01");
}
}
2 changes: 1 addition & 1 deletion tests/Unit/CBOR/MajorTypes/UnsignedIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testEncodeThrowsAnExceptionForValueGreaterThanIntMax(): void
public function testDecodeThrowsAnExceptionWhenPassedInvalidValue(string $case): void
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage("Invalid major type for unsigned integer: ");
$this->expectExceptionMessage("Invalid major type for unsigned integer.");

UnsignedInteger::decode($case);
}
Expand Down
Loading