42 lines
917 B
Go
42 lines
917 B
Go
package internal
|
|
|
|
// ProtoBuffer decodes a subset of the protobuf wire format for MMKV payloads.
|
|
|
|
type ProtoBuffer struct {
|
|
buf []byte
|
|
idx int
|
|
}
|
|
|
|
// NewProtoBuffer returns a buffer whose unread portion is buf.
|
|
func NewProtoBuffer(buf []byte) *ProtoBuffer {
|
|
return &ProtoBuffer{buf: buf}
|
|
}
|
|
|
|
// DecodeStringBytes consumes a length-prefixed string.
|
|
func (b *ProtoBuffer) DecodeStringBytes() (string, error) {
|
|
v, n := ConsumeString(b.buf[b.idx:])
|
|
if n < 0 {
|
|
return "", parseError(n)
|
|
}
|
|
b.idx += n
|
|
return v, nil
|
|
}
|
|
|
|
// DecodeRawBytes consumes a length-prefixed bytes field.
|
|
func (b *ProtoBuffer) DecodeRawBytes(alloc bool) ([]byte, error) {
|
|
v, n := ConsumeBytes(b.buf[b.idx:])
|
|
if n < 0 {
|
|
return nil, parseError(n)
|
|
}
|
|
b.idx += n
|
|
if alloc {
|
|
v = append([]byte(nil), v...)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// Unread returns the unread portion of the buffer.
|
|
func (b *ProtoBuffer) Unread() []byte {
|
|
return b.buf[b.idx:]
|
|
}
|