Skip to content

Commit

Permalink
Fix long encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhkr committed Feb 10, 2025
1 parent fe0261d commit aea815f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lang/php/lib/Datum/AvroIOBinaryDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function decodeLongFromArray($bytes)
$n |= (($b & 0x7f) << $shift);
$shift += 7;
}
return (($n >> 1) ^ -($n & 1));
return ($n >> 1) ^ (($n >> 63) << 63) ^ -($n & 1);
}

/**
Expand Down
23 changes: 17 additions & 6 deletions lang/php/lib/Datum/AvroIOBinaryEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,24 @@ public static function encodeLong($n)
{
$n = (int) $n;
$n = ($n << 1) ^ ($n >> 63);
$str = '';
while (0 != ($n & ~0x7F)) {
$str .= chr(($n & 0x7F) | 0x80);
$n >>= 7;

if ($n >= 0 && $n < 0x80) {
return chr($n);
}
$str .= chr($n);
return $str;

$buf = [];
if (($n & ~0x7F) != 0) {
$buf[] = ($n | 0x80) & 0xFF;
$n = ($n >> 7) ^ (($n >> 63) << 57); // unsigned shift right ($n >>> 7)

while ($n > 0x7F) {
$buf[] = ($n | 0x80) & 0xFF;
$n >>= 7; // $n is always positive here
}
}

$buf[] = $n;
return pack("C*", ...$buf);
}

/**
Expand Down
36 changes: 32 additions & 4 deletions lang/php/test/DatumIOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
use Apache\Avro\Schema\AvroSchema;
use PHPUnit\Framework\TestCase;

/**
* @covers AvroIOBinaryDecoder
* @covers AvroIOBinaryEncoder
* @covers AvroIODatumReader
* @covers AvroIODatumWriter
*/
class DatumIOTest extends TestCase
{
/**
Expand Down Expand Up @@ -68,11 +74,33 @@ function data_provider()
array('"int"', 1, "\002"),
array('"int"', 2147483647, "\xFE\xFF\xFF\xFF\x0F"),

// array('"long"', (int) -9223372036854775808, "\001"),
array('"long"', (int) -9223372036854775808, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
array('"long"', -(1<<62), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F"),
array('"long"', -(1<<61), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x3F"),
array('"long"', -4294967295, "\xFD\xFF\xFF\xFF\x1F"),
array('"long"', -1<<24, "\xFF\xFF\xFF\x0F"),
array('"long"', -1<<16, "\xFF\xFF\x07"),
array('"long"', -255, "\xFD\x03"),
array('"long"', -128, "\xFF\x01"),
array('"long"', -127, "\xFD\x01"),
array('"long"', -10, "\x13"),
array('"long"', -3, "\005"),
array('"long"', -2, "\003"),
array('"long"', -1, "\001"),
array('"long"', 0, "\000"),
array('"long"', 1, "\002"),
// array('"long"', 9223372036854775807, "\002")
array('"long"', 0, "\000"),
array('"long"', 1, "\002"),
array('"long"', 2, "\004"),
array('"long"', 3, "\006"),
array('"long"', 10, "\x14"),
array('"long"', 127, "\xFE\x01"),
array('"long"', 128, "\x80\x02"),
array('"long"', 255, "\xFE\x03"),
array('"long"', 1<<16, "\x80\x80\x08"),
array('"long"', 1<<24, "\x80\x80\x80\x10"),
array('"long"', 4294967295, "\xFE\xFF\xFF\xFF\x1F"),
array('"long"', 1<<61, "\x80\x80\x80\x80\x80\x80\x80\x80\x40"),
array('"long"', 1<<62, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01"),
array('"long"', 9223372036854775807, "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),

array('"float"', (float) -10.0, "\000\000 \301"),
array('"float"', (float) -1.0, "\000\000\200\277"),
Expand Down

0 comments on commit aea815f

Please sign in to comment.