Skip to content

Commit

Permalink
Return first byte when it's less than 127
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Oct 15, 2020
1 parent 8a4b007 commit 528e4f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/RLP.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,19 @@ public function decode(string $input)
*/
protected function decodeData(string $input)
{
$firstByte = hexdec(mb_substr($input, 0, 2));
$firstByte = mb_substr($input, 0, 2);
$firstByteDec = hexdec($firstByte);

if ($firstByte <= 0x7f) {
if ($firstByteDec <= 0x7f) {
return [
'data' => dechex($firstByte),
'data' => $firstByte,
'remainder' => mb_substr($input, 2)
];
} elseif ($firstByte <= 0xb7) {
$length = $firstByte - 0x7f;
} elseif ($firstByteDec <= 0xb7) {
$length = $firstByteDec - 0x7f;
$data = '';

if ($firstByte !== 0x80) {
if ($firstByteDec !== 0x80) {
$data = mb_substr($input, 2, ($length - 1) * 2);
}
$firstByteData = hexdec(mb_substr($data, 0, 2));
Expand All @@ -135,8 +136,8 @@ protected function decodeData(string $input)
'data' => $data,
'remainder' => mb_substr($input, $length * 2)
];
} elseif ($firstByte <= 0xbf) {
$llength = $firstByte - 0xb6;
} elseif ($firstByteDec <= 0xbf) {
$llength = $firstByteDec - 0xb6;
$hexLength = mb_substr($input, 2, ($llength - 1) * 2);

if ($hexLength === '00') {
Expand All @@ -152,8 +153,8 @@ protected function decodeData(string $input)
'data' => $data,
'remainder' => mb_substr($input, ($length + $llength) * 2)
];
} elseif ($firstByte <= 0xf7) {
$length = $firstByte - 0xbf;
} elseif ($firstByteDec <= 0xf7) {
$length = $firstByteDec - 0xbf;
$innerRemainder = mb_substr($input, 2, ($length - 1) * 2);
$decoded = [];

Expand All @@ -167,7 +168,7 @@ protected function decodeData(string $input)
'remainder' => mb_substr($input, $length * 2)
];
} else {
$llength = $firstByte - 0xf6;
$llength = $firstByteDec - 0xf6;
$hexLength = mb_substr($input, 2, ($llength - 1) * 2);
$decoded = [];

Expand Down
2 changes: 1 addition & 1 deletion test/unit/RLPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RLPTest extends TestCase
], [
"decoded" => "0x00",
"encoded" => "00",
"rlpdecoded" => "0"
"rlpdecoded" => "00"
], [
"decoded" => "0x0400",
"encoded" => "820400",
Expand Down

0 comments on commit 528e4f7

Please sign in to comment.