mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
22 lines
704 B
Go
22 lines
704 B
Go
package varint
|
|
|
|
import "errors"
|
|
|
|
// PrependLength prepends the varint encoded length of the byte slice to itself.
|
|
func PrependLength(data []byte) []byte {
|
|
return append(Pack64(uint64(len(data))), data...)
|
|
}
|
|
|
|
// GetNextBlock extract the integer from the beginning of the given byte slice and returns the remaining bytes, the extracted integer, and whether there was an error.
|
|
func GetNextBlock(data []byte) ([]byte, int, error) {
|
|
l, n, err := Unpack64(data)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
length := int(l)
|
|
totalLength := length + n
|
|
if totalLength > len(data) {
|
|
return nil, 0, errors.New("varint: not enough data for given block length")
|
|
}
|
|
return data[n:totalLength], totalLength, nil
|
|
}
|