Skip to content

Commit

Permalink
Fix long encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhkr committed Jan 27, 2025
1 parent fe0261d commit 8fbc05c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 20 deletions.
60 changes: 60 additions & 0 deletions lang/php/lib/AvroVarint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache\Avro;

/**
* Varint encoding/decoding for 64bit php
*
* @package Avro
*/
class AvroVarint {
public static function encodeLong(int $n): string {
if ($n >= 0 && $n < 0x80) {
return chr($n);
}

$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);
}

public static function decodeLong(array $bytes): int {
$b = array_shift($bytes);
$n = $b & 0x7f;
$shift = 7;
while (0 != ($b & 0x80)) {
$b = array_shift($bytes);
$n |= (($b & 0x7f) << $shift);
$shift += 7;
}
return ($n >> 7) ^ (($n >> 63) << 57) ^ -($n & 1);
}
}
11 changes: 2 additions & 9 deletions lang/php/lib/Datum/AvroIOBinaryDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Apache\Avro\AvroException;
use Apache\Avro\AvroGMP;
use Apache\Avro\AvroIO;
use Apache\Avro\AvroVarint;

/**
* Decodes and reads Avro data from an AvroIO object encoded using
Expand Down Expand Up @@ -116,15 +117,7 @@ public function readLong()
*/
public static function decodeLongFromArray($bytes)
{
$b = array_shift($bytes);
$n = $b & 0x7f;
$shift = 7;
while (0 != ($b & 0x80)) {
$b = array_shift($bytes);
$n |= (($b & 0x7f) << $shift);
$shift += 7;
}
return (($n >> 1) ^ -($n & 1));
return AvroVarint::decodeLong($bytes);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions lang/php/lib/Datum/AvroIOBinaryEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Apache\Avro\Avro;
use Apache\Avro\AvroGMP;
use Apache\Avro\AvroIO;
use Apache\Avro\AvroVarint;

/**
* Encodes and writes Avro data to an AvroIO object using
Expand Down Expand Up @@ -101,13 +102,7 @@ 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;
}
$str .= chr($n);
return $str;
return AvroVarint::encodeLong($n);
}

/**
Expand Down
30 changes: 26 additions & 4 deletions lang/php/test/DatumIOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,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 8fbc05c

Please sign in to comment.