Пример #1
0
 public static function serializeFieldToStream($value, $field, $need_tag, &$output)
 {
     if ($need_tag) {
         if (!GPBWire::writeTag($output, self::makeTag($field->getNumber(), $field->getType()))) {
             return false;
         }
     }
     switch ($field->getType()) {
         case GPBType::DOUBLE:
             if (!GPBWire::writeDouble($output, $value)) {
                 return false;
             }
             break;
         case GPBType::FLOAT:
             if (!GPBWire::writeFloat($output, $value)) {
                 return false;
             }
             break;
         case GPBType::INT64:
             if (!GPBWire::writeInt64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::UINT64:
             if (!GPBWire::writeUint64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::INT32:
             if (!GPBWire::writeInt32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::FIXED32:
             if (!GPBWire::writeFixed32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::FIXED64:
             if (!GPBWire::writeFixed64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::BOOL:
             if (!GPBWire::writeBool($output, $value)) {
                 return false;
             }
             break;
         case GPBType::STRING:
             if (!GPBWire::writeString($output, $value)) {
                 return false;
             }
             break;
             //    case GPBType::GROUP:
             //      echo "GROUP\xA";
             //      trigger_error("Not implemented.", E_ERROR);
             //      break;
         //    case GPBType::GROUP:
         //      echo "GROUP\xA";
         //      trigger_error("Not implemented.", E_ERROR);
         //      break;
         case GPBType::MESSAGE:
             if (!GPBWire::writeMessage($output, $value)) {
                 return false;
             }
             break;
         case GPBType::BYTES:
             if (!GPBWire::writeBytes($output, $value)) {
                 return false;
             }
             break;
         case GPBType::UINT32:
             if (!GPBWire::writeUint32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::ENUM:
             if (!GPBWire::writeInt32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SFIXED32:
             if (!GPBWire::writeSfixed32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SFIXED64:
             if (!GPBWire::writeSfixed64($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SINT32:
             if (!GPBWire::writeSint32($output, $value)) {
                 return false;
             }
             break;
         case GPBType::SINT64:
             if (!GPBWire::writeSint64($output, $value)) {
                 return false;
             }
             break;
         default:
             user_error("Unsupported type.");
             return false;
     }
     return true;
 }
 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)));
 }
Пример #3
0
 /**
  * @ignore
  */
 private function fieldByteSize($field)
 {
     $size = 0;
     if ($field->isMap()) {
         $getter = $field->getGetter();
         $values = $this->{$getter}();
         $count = count($values);
         if ($count !== 0) {
             $size += $count * GPBWire::tagSize($field);
             $message_type = $field->getMessageType();
             $key_field = $message_type->getFieldByNumber(1);
             $value_field = $message_type->getFieldByNumber(2);
             foreach ($values as $key => $value) {
                 $data_size = 0;
                 $data_size += $this->fieldDataOnlyByteSize($key_field, $key);
                 $data_size += $this->fieldDataOnlyByteSize($value_field, $value);
                 $data_size += GPBWire::tagSize($key_field);
                 $data_size += GPBWire::tagSize($value_field);
                 $size += GPBWire::varint32Size($data_size) + $data_size;
             }
         }
     } elseif ($field->isRepeated()) {
         $getter = $field->getGetter();
         $values = $this->{$getter}();
         $count = count($values);
         if ($count !== 0) {
             if ($field->getPacked()) {
                 $data_size = 0;
                 foreach ($values as $value) {
                     $data_size += $this->fieldDataOnlyByteSize($field, $value);
                 }
                 $size += GPBWire::tagSize($field);
                 $size += GPBWire::varint32Size($data_size);
                 $size += $data_size;
             } else {
                 $size += $count * GPBWire::tagSize($field);
                 foreach ($values as $value) {
                     $size += $this->fieldDataOnlyByteSize($field, $value);
                 }
             }
         }
     } elseif ($this->existField($field)) {
         $size += GPBWire::tagSize($field);
         $getter = $field->getGetter();
         $value = $this->{$getter}();
         $size += $this->fieldDataOnlyByteSize($field, $value);
     }
     return $size;
 }
Пример #4
0
 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((int) -2147483648, GPBWire::zigZagDecode32(0xffffffff));
     if (PHP_INT_SIZE == 4) {
         $this->assertSame('0', GPBWire::zigZagEncode64(0));
         $this->assertSame('1', GPBWire::zigZagEncode64(-1));
         $this->assertSame('2', GPBWire::zigZagEncode64(1));
         $this->assertSame('3', GPBWire::zigZagEncode64(-2));
         $this->assertSame('2147483646', GPBWire::zigZagEncode64(0x3fffffff));
         $this->assertSame('2147483647', GPBWire::zigZagEncode64(-1073741824));
         // 0xFFFFFFFFC0000000
         $this->assertSame('4294967294', GPBWire::zigZagEncode64(2147483647));
         // 0x7FFFFFFF
         $this->assertSame('4294967295', GPBWire::zigZagEncode64(-2147483648));
         // 0xFFFFFFFF80000000
         $this->assertSame('18446744073709551614', GPBWire::zigZagEncode64("9223372036854775807"));
         $this->assertSame('18446744073709551615', GPBWire::zigZagEncode64("-9223372036854775808"));
         $this->assertSame('0', GPBWire::zigZagDecode64(0));
         $this->assertSame('-1', GPBWire::zigZagDecode64(1));
         $this->assertSame('1', GPBWire::zigZagDecode64(2));
         $this->assertSame('-2', GPBWire::zigZagDecode64(3));
     } else {
         $this->assertSame(0, GPBWire::zigZagEncode64(0));
         $this->assertSame(1, GPBWire::zigZagEncode64(-1));
         $this->assertSame(2, GPBWire::zigZagEncode64(1));
         $this->assertSame(3, GPBWire::zigZagEncode64(-2));
         $this->assertSame(0x7ffffffe, GPBWire::zigZagEncode64(0x3fffffff));
         $this->assertSame(0x7fffffff, GPBWire::zigZagEncode64(1.844674407263581E+19));
         $this->assertSame(0xfffffffe, GPBWire::zigZagEncode64(0x7fffffff));
         $this->assertSame(0xffffffff, GPBWire::zigZagEncode64(1.8446744071562068E+19));
         $this->assertSame(-2, GPBWire::zigZagEncode64(0x7fffffffffffffff));
         $this->assertSame(-1, GPBWire::zigZagEncode64(9.223372036854776E+18));
         $this->assertSame(0, GPBWire::zigZagDecode64(0));
         $this->assertSame(-1, GPBWire::zigZagDecode64(1));
         $this->assertSame(1, GPBWire::zigZagDecode64(2));
         $this->assertSame(-2, GPBWire::zigZagDecode64(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)));
 }