Exemple #1
0
 /**
  * Read 64-bit unsiged integer to $var. If the buffer has less than 8 bytes,
  * return false. Advance buffer with consumed bytes.
  * @param $var.
  */
 public function readLittleEndian64(&$var)
 {
     $data = null;
     if (!$this->readRaw(4, $data)) {
         return false;
     }
     $low = unpack('V', $data)[1];
     if (!$this->readRaw(4, $data)) {
         return false;
     }
     $high = unpack('V', $data)[1];
     $var = Uint64::newValue($high, $low);
     return true;
 }
Exemple #2
0
 public static function zigZagEncode64($int64)
 {
     $a = $int64->copy()->leftShift(1);
     $b = $int64->copy()->rightShift(63);
     $result = $a->bitXor($b);
     $uint64 = Uint64::newValue($result->high, $result->low);
     return $uint64;
 }
 public function testZigZagEncodeDecode()
 {
     $this->assertSame(0, GPBWire::zigZagEncode32(0));
     $this->assertSame(1, GPBWire::zigZagEncode32(-1));
     $this->assertSame(2, GPBWire::zigZagEncode32(1));
     $this->assertSame(3, GPBWire::zigZagEncode32(-2));
     $this->assertSame(0x7ffffffe, GPBWire::zigZagEncode32(0x3fffffff));
     $this->assertSame(0x7fffffff, GPBWire::zigZagEncode32(0xc0000000));
     $this->assertSame(-2, GPBWire::zigZagEncode32(0x7fffffff));
     $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
     $this->assertSame(0, GPBWire::zigZagDecode32(0));
     $this->assertSame(-1, GPBWire::zigZagDecode32(1));
     $this->assertSame(1, GPBWire::zigZagDecode32(2));
     $this->assertSame(-2, GPBWire::zigZagDecode32(3));
     $this->assertSame(0x3fffffff, GPBWire::zigZagDecode32(0x7ffffffe));
     $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7fffffff));
     $this->assertSame(0x7fffffff, GPBWire::zigZagDecode32(0xfffffffe));
     $this->assertSame(-2147483648, GPBWire::zigZagDecode32(0xffffffff));
     $this->assertEquals(GPBUtil::Uint64(0), GPBWire::zigZagEncode64(GPBUtil::Int64(0)));
     $this->assertEquals(GPBUtil::Uint64(1), GPBWire::zigZagEncode64(GPBUtil::Int64(-1)));
     $this->assertEquals(GPBUtil::Uint64(2), GPBWire::zigZagEncode64(GPBUtil::Int64(1)));
     $this->assertEquals(GPBUtil::Uint64(3), GPBWire::zigZagEncode64(GPBUtil::Int64(-2)));
     $this->assertEquals(GPBUtil::Uint64(0x7ffffffe), GPBWire::zigZagEncode64(GPBUtil::Int64(0x3fffffff)));
     $this->assertEquals(GPBUtil::Uint64(0x7fffffff), GPBWire::zigZagEncode64(GPBUtil::Int64(1.844674407263581E+19)));
     $this->assertEquals(GPBUtil::Uint64(0xfffffffe), GPBWire::zigZagEncode64(GPBUtil::Int64(0x7fffffff)));
     $this->assertEquals(GPBUtil::Uint64(0xffffffff), GPBWire::zigZagEncode64(GPBUtil::Int64(1.8446744071562068E+19)));
     $this->assertEquals(Uint64::newValue(4294967295, 4294967294), GPBWire::zigZagEncode64(GPBUtil::Int64(0x7fffffffffffffff)));
     $this->assertEquals(Uint64::newValue(4294967295, 4294967295), GPBWire::zigZagEncode64(GPBUtil::Int64(9.223372036854776E+18)));
     $this->assertEquals(GPBUtil::Int64(0), GPBWire::zigZagDecode64(GPBUtil::Uint64(0)));
     $this->assertEquals(GPBUtil::Int64(-1), GPBWire::zigZagDecode64(GPBUtil::Uint64(1)));
     $this->assertEquals(GPBUtil::Int64(1), GPBWire::zigZagDecode64(GPBUtil::Uint64(2)));
     $this->assertEquals(GPBUtil::Int64(-2), GPBWire::zigZagDecode64(GPBUtil::Uint64(3)));
     // Round trip
     $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
     $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
     $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
     $this->assertSame(14927, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
     $this->assertSame(-3612, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
 }