mirror of
https://github.com/safing/portbase
synced 2025-04-17 16:09:08 +00:00
Improve varint errors, add helper for getting encoded size
This commit is contained in:
parent
a01df19cd0
commit
17fcefb403
2 changed files with 34 additions and 5 deletions
formats/varint
|
@ -20,3 +20,29 @@ func GetNextBlock(data []byte) ([]byte, int, error) {
|
|||
}
|
||||
return data[n:totalLength], totalLength, nil
|
||||
}
|
||||
|
||||
// EncodedSize returns the size required to varint-encode an uint.
|
||||
func EncodedSize(n uint64) (size int) {
|
||||
switch {
|
||||
case n < 128:
|
||||
return 1
|
||||
case n < 16384:
|
||||
return 2
|
||||
case n < 2097152:
|
||||
return 3
|
||||
case n < 268435456:
|
||||
return 4
|
||||
case n < 34359738368:
|
||||
return 5
|
||||
case n < 4398046511104:
|
||||
return 6
|
||||
case n < 562949953421312:
|
||||
return 7
|
||||
case n < 72057594037927936:
|
||||
return 8
|
||||
case n < 9223372036854775808:
|
||||
return 9
|
||||
default:
|
||||
return 10
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,9 @@ import (
|
|||
"errors"
|
||||
)
|
||||
|
||||
// ErrBufTooSmall is returned when there is not enough data for parsing a varint.
|
||||
var ErrBufTooSmall = errors.New("varint: buf too small")
|
||||
|
||||
// Pack8 packs a uint8 into a VarInt.
|
||||
func Pack8(n uint8) []byte {
|
||||
if n < 128 {
|
||||
|
@ -37,13 +40,13 @@ func Pack64(n uint64) []byte {
|
|||
// Unpack8 unpacks a VarInt into a uint8. It returns the extracted int, how many bytes were used and an error.
|
||||
func Unpack8(blob []byte) (uint8, int, error) {
|
||||
if len(blob) < 1 {
|
||||
return 0, 0, errors.New("varint: buf has zero length")
|
||||
return 0, 0, ErrBufTooSmall
|
||||
}
|
||||
if blob[0] < 128 {
|
||||
return blob[0], 1, nil
|
||||
}
|
||||
if len(blob) < 2 {
|
||||
return 0, 0, errors.New("varint: buf too small")
|
||||
return 0, 0, ErrBufTooSmall
|
||||
}
|
||||
if blob[1] != 0x01 {
|
||||
return 0, 0, errors.New("varint: encoded integer greater than 255 (uint8)")
|
||||
|
@ -55,7 +58,7 @@ func Unpack8(blob []byte) (uint8, int, error) {
|
|||
func Unpack16(blob []byte) (uint16, int, error) {
|
||||
n, r := binary.Uvarint(blob)
|
||||
if r == 0 {
|
||||
return 0, 0, errors.New("varint: buf too small")
|
||||
return 0, 0, ErrBufTooSmall
|
||||
}
|
||||
if r < 0 {
|
||||
return 0, 0, errors.New("varint: encoded integer greater than 18446744073709551615 (uint64)")
|
||||
|
@ -70,7 +73,7 @@ func Unpack16(blob []byte) (uint16, int, error) {
|
|||
func Unpack32(blob []byte) (uint32, int, error) {
|
||||
n, r := binary.Uvarint(blob)
|
||||
if r == 0 {
|
||||
return 0, 0, errors.New("varint: buf too small")
|
||||
return 0, 0, ErrBufTooSmall
|
||||
}
|
||||
if r < 0 {
|
||||
return 0, 0, errors.New("varint: encoded integer greater than 18446744073709551615 (uint64)")
|
||||
|
@ -85,7 +88,7 @@ func Unpack32(blob []byte) (uint32, int, error) {
|
|||
func Unpack64(blob []byte) (uint64, int, error) {
|
||||
n, r := binary.Uvarint(blob)
|
||||
if r == 0 {
|
||||
return 0, 0, errors.New("varint: buf too small")
|
||||
return 0, 0, ErrBufTooSmall
|
||||
}
|
||||
if r < 0 {
|
||||
return 0, 0, errors.New("varint: encoded integer greater than 18446744073709551615 (uint64)")
|
||||
|
|
Loading…
Add table
Reference in a new issue