예제 #1
0
 function read($fp, &$limit = PHP_INT_MAX)
 {
     while (!feof($fp) && $limit > 0) {
         $tag = Protobuf::read_varint($fp, $limit);
         if ($tag === false) {
             break;
         }
         $wire = $tag & 0x7;
         $field = $tag >> 3;
         //var_dump("Log_Content: Found $field type " . Protobuf::get_wiretype($wire) . " $limit bytes left");
         switch ($field) {
             case 1:
                 ASSERT('$wire == 2');
                 $len = Protobuf::read_varint($fp, $limit);
                 if ($len === false) {
                     throw new Exception('Protobuf::read_varint returned false');
                 }
                 if ($len > 0) {
                     $tmp = fread($fp, $len);
                 } else {
                     $tmp = '';
                 }
                 if ($tmp === false) {
                     throw new Exception("fread({$len}) returned false");
                 }
                 $this->key_ = $tmp;
                 $limit -= $len;
                 break;
             case 2:
                 ASSERT('$wire == 2');
                 $len = Protobuf::read_varint($fp, $limit);
                 if ($len === false) {
                     throw new Exception('Protobuf::read_varint returned false');
                 }
                 if ($len > 0) {
                     $tmp = fread($fp, $len);
                 } else {
                     $tmp = '';
                 }
                 if ($tmp === false) {
                     throw new Exception("fread({$len}) returned false");
                 }
                 $this->value_ = $tmp;
                 $limit -= $len;
                 break;
             default:
                 $this->_unknown[$field . '-' . Protobuf::get_wiretype($wire)][] = Protobuf::read_field($fp, $wire, $limit);
         }
     }
     if (!$this->validateRequired()) {
         throw new Exception('Required fields are missing');
     }
 }
예제 #2
0
 /**
  * Read a unknown field from the stream and return its raw bytes
  */
 public static function read_field($fp, $wire_type, &$limit = null)
 {
     switch ($wire_type) {
         case 0:
             // varint
             return Protobuf::read_varint($fp, $limit);
         case 1:
             // 64bit
             $limit -= 8;
             return fread($fp, 8);
         case 2:
             // length delimited
             $len = Protobuf::read_varint($fp, $limit);
             $limit -= $len;
             return fread($fp, $len);
             //case 3: // Start group TODO we must keep looping until we find the closing end grou
             //case 4: // End group - We should never skip a end group!
             //	return 0; // Do nothing
         //case 3: // Start group TODO we must keep looping until we find the closing end grou
         //case 4: // End group - We should never skip a end group!
         //	return 0; // Do nothing
         case 5:
             // 32bit
             $limit -= 4;
             return fread($fp, 4);
         default:
             throw new Exception('read_unknown(' . ProtoBuf::get_wiretype($wire_type) . '): Unsupported wire_type');
     }
 }