init: dec-music 项目初始化
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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:]
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package internal
|
||||
|
||||
// Minimal protobuf wire helpers (bytes/string fields only).
|
||||
|
||||
const (
|
||||
errCodeOverflow = -2
|
||||
errCodeTruncated = -1
|
||||
)
|
||||
|
||||
func consumeVarint(b []byte) (uint64, int) {
|
||||
var x uint64
|
||||
var s uint
|
||||
for i, c := range b {
|
||||
if c < 0x80 {
|
||||
if i > 9 || i == 9 && c > 1 {
|
||||
return 0, errCodeOverflow
|
||||
}
|
||||
return x | uint64(c)<<s, i + 1
|
||||
}
|
||||
x |= uint64(c&0x7f) << s
|
||||
s += 7
|
||||
}
|
||||
return 0, errCodeTruncated
|
||||
}
|
||||
|
||||
// ConsumeBytes parses a length-delimited bytes field.
|
||||
func ConsumeBytes(b []byte) ([]byte, int) {
|
||||
v, n := consumeVarint(b)
|
||||
if n < 0 {
|
||||
return nil, n
|
||||
}
|
||||
if v > uint64(len(b)-n) {
|
||||
return nil, errCodeOverflow
|
||||
}
|
||||
return b[n : n+int(v)], n + int(v)
|
||||
}
|
||||
|
||||
// ConsumeString parses a length-delimited string field.
|
||||
func ConsumeString(b []byte) (string, int) {
|
||||
v, n := ConsumeBytes(b)
|
||||
if n < 0 {
|
||||
return "", n
|
||||
}
|
||||
return string(v), n
|
||||
}
|
||||
|
||||
func parseError(code int) error {
|
||||
switch code {
|
||||
case errCodeOverflow:
|
||||
return errOverflow
|
||||
case errCodeTruncated:
|
||||
return errTruncated
|
||||
default:
|
||||
return errTruncated
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
errOverflow = &parseErr{"overflow"}
|
||||
errTruncated = &parseErr{"truncated"}
|
||||
)
|
||||
|
||||
type parseErr struct{ msg string }
|
||||
|
||||
func (e *parseErr) Error() string { return "protowire: " + e.msg }
|
||||
Reference in New Issue
Block a user