github.com/cosmos/cosmos-proto@v1.0.0-beta.3/runtime/runtime.go (about)

     1  package runtime
     2  
     3  import (
     4  	"fmt"
     5  	"google.golang.org/protobuf/proto"
     6  	"google.golang.org/protobuf/runtime/protoiface"
     7  	"io"
     8  	"math/bits"
     9  )
    10  
    11  func Sov(x uint64) (n int) {
    12  	return (bits.Len64(x|1) + 6) / 7
    13  }
    14  func Soz(x uint64) (n int) {
    15  	return Sov((x << 1) ^ uint64(int64(x)>>63))
    16  }
    17  
    18  func EncodeVarint(dAtA []byte, offset int, v uint64) int {
    19  	offset -= Sov(v)
    20  	base := offset
    21  	for v >= 1<<7 {
    22  		dAtA[offset] = uint8(v&0x7f | 0x80)
    23  		v >>= 7
    24  		offset++
    25  	}
    26  	dAtA[offset] = uint8(v)
    27  	return base
    28  }
    29  
    30  func Skip(dAtA []byte) (n int, err error) {
    31  	l := len(dAtA)
    32  	iNdEx := 0
    33  	depth := 0
    34  	for iNdEx < l {
    35  		var wire uint64
    36  		for shift := uint(0); ; shift += 7 {
    37  			if shift >= 64 {
    38  				return 0, ErrIntOverflow
    39  			}
    40  			if iNdEx >= l {
    41  				return 0, io.ErrUnexpectedEOF
    42  			}
    43  			b := dAtA[iNdEx]
    44  			iNdEx++
    45  			wire |= (uint64(b) & 0x7F) << shift
    46  			if b < 0x80 {
    47  				break
    48  			}
    49  		}
    50  		wireType := int(wire & 0x7)
    51  		switch wireType {
    52  		case 0:
    53  			for shift := uint(0); ; shift += 7 {
    54  				if shift >= 64 {
    55  					return 0, ErrIntOverflow
    56  				}
    57  				if iNdEx >= l {
    58  					return 0, io.ErrUnexpectedEOF
    59  				}
    60  				iNdEx++
    61  				if dAtA[iNdEx-1] < 0x80 {
    62  					break
    63  				}
    64  			}
    65  		case 1:
    66  			iNdEx += 8
    67  		case 2:
    68  			var length int
    69  			for shift := uint(0); ; shift += 7 {
    70  				if shift >= 64 {
    71  					return 0, ErrIntOverflow
    72  				}
    73  				if iNdEx >= l {
    74  					return 0, io.ErrUnexpectedEOF
    75  				}
    76  				b := dAtA[iNdEx]
    77  				iNdEx++
    78  				length |= (int(b) & 0x7F) << shift
    79  				if b < 0x80 {
    80  					break
    81  				}
    82  			}
    83  			if length < 0 {
    84  				return 0, ErrInvalidLength
    85  			}
    86  			iNdEx += length
    87  		case 3:
    88  			depth++
    89  		case 4:
    90  			if depth == 0 {
    91  				return 0, ErrUnexpectedEndOfGroup
    92  			}
    93  			depth--
    94  		case 5:
    95  			iNdEx += 4
    96  		default:
    97  			return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
    98  		}
    99  		if iNdEx < 0 {
   100  			return 0, ErrInvalidLength
   101  		}
   102  		if depth == 0 {
   103  			return iNdEx, nil
   104  		}
   105  	}
   106  	return 0, io.ErrUnexpectedEOF
   107  }
   108  
   109  func SizeInputToOptions(input protoiface.SizeInput) proto.MarshalOptions {
   110  	return proto.MarshalOptions{
   111  		NoUnkeyedLiterals: input.NoUnkeyedLiterals,
   112  		AllowPartial:      true,
   113  		Deterministic:     input.Flags&protoiface.MarshalDeterministic != 0,
   114  		UseCachedSize:     input.Flags&protoiface.MarshalUseCachedSize != 0,
   115  	}
   116  }
   117  
   118  func MarshalInputToOptions(input protoiface.MarshalInput) proto.MarshalOptions {
   119  	return proto.MarshalOptions{
   120  		NoUnkeyedLiterals: input.NoUnkeyedLiterals,
   121  		AllowPartial:      true, // defaults to true as the required fields check is done after the marshalling
   122  		Deterministic:     input.Flags&protoiface.MarshalDeterministic != 0,
   123  		UseCachedSize:     input.Flags&protoiface.MarshalUseCachedSize != 0,
   124  	}
   125  }
   126  
   127  func UnmarshalInputToOptions(input protoiface.UnmarshalInput) proto.UnmarshalOptions {
   128  	return proto.UnmarshalOptions{
   129  		NoUnkeyedLiterals: input.NoUnkeyedLiterals,
   130  		Merge:             false,
   131  		AllowPartial:      true, // defaults to true as the required fields check is done after the unmarshalling
   132  		DiscardUnknown:    input.Flags&protoiface.UnmarshalDiscardUnknown != 0,
   133  		Resolver:          input.Resolver,
   134  	}
   135  }
   136  
   137  var (
   138  	ErrInvalidLength        = fmt.Errorf("proto: negative length found during unmarshaling")
   139  	ErrIntOverflow          = fmt.Errorf("proto: integer overflow")
   140  	ErrUnexpectedEndOfGroup = fmt.Errorf("proto: unexpected end of group")
   141  )