Source File
wire.go
Belonging Package
google.golang.org/protobuf/encoding/protowire
package protowire
import (
)
type Number int32
const (
MinValidNumber Number = 1
FirstReservedNumber Number = 19000
LastReservedNumber Number = 19999
MaxValidNumber Number = 1<<29 - 1
)
func ( Number) () bool {
return MinValidNumber <= && < FirstReservedNumber || LastReservedNumber < && <= MaxValidNumber
}
type Type int8
const (
VarintType Type = 0
Fixed32Type Type = 5
Fixed64Type Type = 1
BytesType Type = 2
StartGroupType Type = 3
EndGroupType Type = 4
)
const (
_ = -iota
errCodeTruncated
errCodeFieldNumber
errCodeOverflow
errCodeReserved
errCodeEndGroup
)
var (
errFieldNumber = errors.New("invalid field number")
errOverflow = errors.New("variable length integer overflow")
errReserved = errors.New("cannot parse reserved wire type")
errEndGroup = errors.New("mismatching end group marker")
errParse = errors.New("parse error")
)
func ( int) error {
if >= 0 {
return nil
}
switch {
case errCodeTruncated:
return io.ErrUnexpectedEOF
case errCodeFieldNumber:
return errFieldNumber
case errCodeOverflow:
return errOverflow
case errCodeReserved:
return errReserved
case errCodeEndGroup:
return errEndGroup
default:
return errParse
}
}
func ( []byte) (Number, Type, int) {
, , := ConsumeTag()
if < 0 {
return 0, 0, // forward error code
}
:= ConsumeFieldValue(, , [:])
if < 0 {
return 0, 0, // forward error code
}
return , , +
}
func ( Number, Type, []byte) ( int) {
switch {
case VarintType:
_, = ConsumeVarint()
return
case Fixed32Type:
_, = ConsumeFixed32()
return
case Fixed64Type:
_, = ConsumeFixed64()
return
case BytesType:
_, = ConsumeBytes()
return
case StartGroupType:
:= len()
for {
, , := ConsumeTag()
if < 0 {
return // forward error code
}
= [:]
if == EndGroupType {
if != {
return errCodeEndGroup
}
return - len()
}
= (, , )
if < 0 {
return // forward error code
}
= [:]
}
case EndGroupType:
return errCodeEndGroup
default:
return errCodeReserved
}
}
func ( []byte) (Number, Type, int) {
, := ConsumeVarint()
if < 0 {
return 0, 0, // forward error code
}
, := DecodeTag()
if < MinValidNumber {
return 0, 0, errCodeFieldNumber
}
return , ,
}
func ( Number) int {
return SizeVarint(EncodeTag(, 0)) // wire type has no effect on size
}
func ( []byte, uint64) []byte {
switch {
case < 1<<7:
= append(, byte())
case < 1<<14:
= append(,
byte((>>0)&0x7f|0x80),
byte(>>7))
case < 1<<21:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte(>>14))
case < 1<<28:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte(>>21))
case < 1<<35:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte((>>21)&0x7f|0x80),
byte(>>28))
case < 1<<42:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte((>>21)&0x7f|0x80),
byte((>>28)&0x7f|0x80),
byte(>>35))
case < 1<<49:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte((>>21)&0x7f|0x80),
byte((>>28)&0x7f|0x80),
byte((>>35)&0x7f|0x80),
byte(>>42))
case < 1<<56:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte((>>21)&0x7f|0x80),
byte((>>28)&0x7f|0x80),
byte((>>35)&0x7f|0x80),
byte((>>42)&0x7f|0x80),
byte(>>49))
case < 1<<63:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte((>>21)&0x7f|0x80),
byte((>>28)&0x7f|0x80),
byte((>>35)&0x7f|0x80),
byte((>>42)&0x7f|0x80),
byte((>>49)&0x7f|0x80),
byte(>>56))
default:
= append(,
byte((>>0)&0x7f|0x80),
byte((>>7)&0x7f|0x80),
byte((>>14)&0x7f|0x80),
byte((>>21)&0x7f|0x80),
byte((>>28)&0x7f|0x80),
byte((>>35)&0x7f|0x80),
byte((>>42)&0x7f|0x80),
byte((>>49)&0x7f|0x80),
byte((>>56)&0x7f|0x80),
1)
}
return
}
func ( []byte) ( uint64, int) {
var uint64
if len() <= 0 {
return 0, errCodeTruncated
}
= uint64([0])
if < 0x80 {
return , 1
}
-= 0x80
if len() <= 1 {
return 0, errCodeTruncated
}
= uint64([1])
+= << 7
if < 0x80 {
return , 2
}
-= 0x80 << 7
if len() <= 2 {
return 0, errCodeTruncated
}
= uint64([2])
+= << 14
if < 0x80 {
return , 3
}
-= 0x80 << 14
if len() <= 3 {
return 0, errCodeTruncated
}
= uint64([3])
+= << 21
if < 0x80 {
return , 4
}
-= 0x80 << 21
if len() <= 4 {
return 0, errCodeTruncated
}
= uint64([4])
+= << 28
if < 0x80 {
return , 5
}
-= 0x80 << 28
if len() <= 5 {
return 0, errCodeTruncated
}
= uint64([5])
+= << 35
if < 0x80 {
return , 6
}
-= 0x80 << 35
if len() <= 6 {
return 0, errCodeTruncated
}
= uint64([6])
+= << 42
if < 0x80 {
return , 7
}
-= 0x80 << 42
if len() <= 7 {
return 0, errCodeTruncated
}
= uint64([7])
+= << 49
if < 0x80 {
return , 8
}
-= 0x80 << 49
if len() <= 8 {
return 0, errCodeTruncated
}
= uint64([8])
+= << 56
if < 0x80 {
return , 9
}
-= 0x80 << 56
if len() <= 9 {
return 0, errCodeTruncated
}
= uint64([9])
+= << 63
if < 2 {
return , 10
}
return 0, errCodeOverflow
}
func () int {
return 4
}
func () int {
return 8
}
func ( []byte) ( []byte, int) {
, := ConsumeVarint()
if < 0 {
return nil, // forward error code
}
if > uint64(len([:])) {
return nil, errCodeTruncated
}
return [:][:], + int()
}
func ( int) int {
return SizeVarint(uint64()) +
}
func ( []byte) ( string, int) {
, := ConsumeBytes()
return string(),
}
func ( []byte, Number, []byte) []byte {
return AppendVarint(append(, ...), EncodeTag(, EndGroupType))
}
func ( Number, []byte) ( []byte, int) {
= ConsumeFieldValue(, StartGroupType, )
if < 0 {
return nil, // forward error code
}
= [:]
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |