Protobuf-PHP icon indicating copy to clipboard operation
Protobuf-PHP copied to clipboard

Int64 wrong parsing.

Open Hiller opened this issue 9 years ago • 3 comments

Hi. I found that Int64 parsing from binary reader is wrong. it should be done like this.

    public function varint()
    {
        $result = $shift = 0;
        $bytes=array();

        do {
            $byte = $this->byte();
            $bytes[]=$byte;
        } while ($byte > 0x7f);
        for ($j = count($bytes)-1; $j >= 0; $j--)
        {
            $result = $result * 128 + ($bytes[$j] & 0x7F);
        }
        return $result;
    }

Hiller avatar May 08 '15 03:05 Hiller

I think the problem is perhaps you're running on a PHP interpreter with 32bit integers, could you check the value for PHP_INT_SIZE?

The current implementation relies on bit arithmetic to improve the performance which in PHP always result on an integer, in your solution it uses standard arithmetic which for big numbers will end up resulting on a floating point number which can end up loosing precision.

Perhaps the library can be changed to fallback to your decoding implementation if after the 4th byte we're still decoding more ($byte > 0x7f). That way we would still use the in theory faster bit arithmetic for most common cases but support floating point if we go out of the integer range.

drslump avatar May 08 '15 10:05 drslump

Integer max size for me is 9223372036854775807 but when i'm trying to decode message wich contains timestamp in microseconds, i receive wrong number. Using this function i'm getting it right. Just for comparison: my way: public 'timestamp' => float 1428581286157 old way: public 'timestamp' => float 79193446 Even with 32 bit integer this number should be much greater

Hiller avatar May 08 '15 18:05 Hiller

This problem is solved now?

Sufir avatar Dec 19 '16 09:12 Sufir