vendor/google.golang.org/protobuf/encoding/protowire/wire.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    19 
    19 
    20 // Number represents the field number.
    20 // Number represents the field number.
    21 type Number int32
    21 type Number int32
    22 
    22 
    23 const (
    23 const (
    24 	MinValidNumber      Number = 1
    24 	MinValidNumber        Number = 1
    25 	FirstReservedNumber Number = 19000
    25 	FirstReservedNumber   Number = 19000
    26 	LastReservedNumber  Number = 19999
    26 	LastReservedNumber    Number = 19999
    27 	MaxValidNumber      Number = 1<<29 - 1
    27 	MaxValidNumber        Number = 1<<29 - 1
       
    28 	DefaultRecursionLimit        = 10000
    28 )
    29 )
    29 
    30 
    30 // IsValid reports whether the field number is semantically valid.
    31 // IsValid reports whether the field number is semantically valid.
    31 //
    32 //
    32 // Note that while numbers within the reserved range are semantically invalid,
    33 // Note that while numbers within the reserved range are semantically invalid,
    53 	errCodeTruncated
    54 	errCodeTruncated
    54 	errCodeFieldNumber
    55 	errCodeFieldNumber
    55 	errCodeOverflow
    56 	errCodeOverflow
    56 	errCodeReserved
    57 	errCodeReserved
    57 	errCodeEndGroup
    58 	errCodeEndGroup
       
    59 	errCodeRecursionDepth
    58 )
    60 )
    59 
    61 
    60 var (
    62 var (
    61 	errFieldNumber = errors.New("invalid field number")
    63 	errFieldNumber = errors.New("invalid field number")
    62 	errOverflow    = errors.New("variable length integer overflow")
    64 	errOverflow    = errors.New("variable length integer overflow")
   110 // This returns a negative length upon an error (see ParseError).
   112 // This returns a negative length upon an error (see ParseError).
   111 //
   113 //
   112 // When parsing a group, the length includes the end group marker and
   114 // When parsing a group, the length includes the end group marker and
   113 // the end group is verified to match the starting field number.
   115 // the end group is verified to match the starting field number.
   114 func ConsumeFieldValue(num Number, typ Type, b []byte) (n int) {
   116 func ConsumeFieldValue(num Number, typ Type, b []byte) (n int) {
       
   117 	return consumeFieldValueD(num, typ, b, DefaultRecursionLimit)
       
   118 }
       
   119 
       
   120 func consumeFieldValueD(num Number, typ Type, b []byte, depth int) (n int) {
   115 	switch typ {
   121 	switch typ {
   116 	case VarintType:
   122 	case VarintType:
   117 		_, n = ConsumeVarint(b)
   123 		_, n = ConsumeVarint(b)
   118 		return n
   124 		return n
   119 	case Fixed32Type:
   125 	case Fixed32Type:
   124 		return n
   130 		return n
   125 	case BytesType:
   131 	case BytesType:
   126 		_, n = ConsumeBytes(b)
   132 		_, n = ConsumeBytes(b)
   127 		return n
   133 		return n
   128 	case StartGroupType:
   134 	case StartGroupType:
       
   135 		if depth < 0 {
       
   136 			return errCodeRecursionDepth
       
   137 		}
   129 		n0 := len(b)
   138 		n0 := len(b)
   130 		for {
   139 		for {
   131 			num2, typ2, n := ConsumeTag(b)
   140 			num2, typ2, n := ConsumeTag(b)
   132 			if n < 0 {
   141 			if n < 0 {
   133 				return n // forward error code
   142 				return n // forward error code
   138 					return errCodeEndGroup
   147 					return errCodeEndGroup
   139 				}
   148 				}
   140 				return n0 - len(b)
   149 				return n0 - len(b)
   141 			}
   150 			}
   142 
   151 
   143 			n = ConsumeFieldValue(num2, typ2, b)
   152 			n = consumeFieldValueD(num2, typ2, b, depth-1)
   144 			if n < 0 {
   153 			if n < 0 {
   145 				return n // forward error code
   154 				return n // forward error code
   146 			}
   155 			}
   147 			b = b[n:]
   156 			b = b[n:]
   148 		}
   157 		}
   505 func EncodeTag(num Number, typ Type) uint64 {
   514 func EncodeTag(num Number, typ Type) uint64 {
   506 	return uint64(num)<<3 | uint64(typ&7)
   515 	return uint64(num)<<3 | uint64(typ&7)
   507 }
   516 }
   508 
   517 
   509 // DecodeZigZag decodes a zig-zag-encoded uint64 as an int64.
   518 // DecodeZigZag decodes a zig-zag-encoded uint64 as an int64.
       
   519 //
   510 //	Input:  {…,  5,  3,  1,  0,  2,  4,  6, …}
   520 //	Input:  {…,  5,  3,  1,  0,  2,  4,  6, …}
   511 //	Output: {…, -3, -2, -1,  0, +1, +2, +3, …}
   521 //	Output: {…, -3, -2, -1,  0, +1, +2, +3, …}
   512 func DecodeZigZag(x uint64) int64 {
   522 func DecodeZigZag(x uint64) int64 {
   513 	return int64(x>>1) ^ int64(x)<<63>>63
   523 	return int64(x>>1) ^ int64(x)<<63>>63
   514 }
   524 }
   515 
   525 
   516 // EncodeZigZag encodes an int64 as a zig-zag-encoded uint64.
   526 // EncodeZigZag encodes an int64 as a zig-zag-encoded uint64.
       
   527 //
   517 //	Input:  {…, -3, -2, -1,  0, +1, +2, +3, …}
   528 //	Input:  {…, -3, -2, -1,  0, +1, +2, +3, …}
   518 //	Output: {…,  5,  3,  1,  0,  2,  4,  6, …}
   529 //	Output: {…,  5,  3,  1,  0,  2,  4,  6, …}
   519 func EncodeZigZag(x int64) uint64 {
   530 func EncodeZigZag(x int64) uint64 {
   520 	return uint64(x<<1) ^ uint64(x>>63)
   531 	return uint64(x<<1) ^ uint64(x>>63)
   521 }
   532 }
   522 
   533 
   523 // DecodeBool decodes a uint64 as a bool.
   534 // DecodeBool decodes a uint64 as a bool.
       
   535 //
   524 //	Input:  {    0,    1,    2, …}
   536 //	Input:  {    0,    1,    2, …}
   525 //	Output: {false, true, true, …}
   537 //	Output: {false, true, true, …}
   526 func DecodeBool(x uint64) bool {
   538 func DecodeBool(x uint64) bool {
   527 	return x != 0
   539 	return x != 0
   528 }
   540 }
   529 
   541 
   530 // EncodeBool encodes a bool as a uint64.
   542 // EncodeBool encodes a bool as a uint64.
       
   543 //
   531 //	Input:  {false, true}
   544 //	Input:  {false, true}
   532 //	Output: {    0,    1}
   545 //	Output: {    0,    1}
   533 func EncodeBool(x bool) uint64 {
   546 func EncodeBool(x bool) uint64 {
   534 	if x {
   547 	if x {
   535 		return 1
   548 		return 1