mirror of
https://github.com/safing/portbase
synced 2025-09-16 18:19:50 +00:00
Initial commit after restructure
This commit is contained in:
commit
96ec15b39b
70 changed files with 6945 additions and 0 deletions
22
formats/varint/helpers.go
Normal file
22
formats/varint/helpers.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue