Documentation ¶
Overview ¶
Package binutil contains some helpful utilities for reading binary data from byte slices.
Index ¶
- func Duplicate(in []byte) []byte
- func IsOnlyZeroes(data []byte) bool
- type BinReader
- func (r *BinReader) Byte(offset int) byte
- func (r *BinReader) ByteOrder() binary.ByteOrder
- func (r *BinReader) Data() []byte
- func (r *BinReader) Length() int
- func (r *BinReader) Read(offset int, length int) []byte
- func (r *BinReader) ReadFrom(offset int) []byte
- func (r *BinReader) Reader(offset int, length int) *BinReader
- func (r *BinReader) ReaderFrom(offset int) *BinReader
- func (r *BinReader) Uint16(offset int) uint16
- func (r *BinReader) Uint32(offset int) uint32
- func (r *BinReader) Uint64(offset int) uint64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsOnlyZeroes ¶
IsOnlyZeroes return true when the input data is all bytes of zero value and false if any of the bytes has a nonzero value.
Types ¶
type BinReader ¶
type BinReader struct {
// contains filtered or unexported fields
}
BinReader helps to read data from a byte slice using an offset and a data length (instead two offsets when using a slice expression). For example b[2:4] yields the same as Read(2, 2) using a BinReader over b. Also some convenient methods are provided to read integer values using a binary.ByteOrder from the slice directly.
Note that methods that return a []byte may not necessarily copy the data, so modifying the returned slice may also affect the data in the BinReader.
Methods will panic when any offset or length is outside of the bounds of the original data.
func NewBigEndianReader ¶
NewLittleEndianReader creates a BinReader over data using binary.BigEndian. The data slice is stored directly, no copy is made, so modifying the original slice will also affect the returned BinReader.
func NewBinReader ¶
NewBinReader creates a BinReader over data using the specified binary.ByteOrder. The data slice is stored directly, no copy is made, so modifying the original slice will also affect the returned BinReader.
func NewLittleEndianReader ¶
NewLittleEndianReader creates a BinReader over data using binary.LittleEndian. The data slice is stored directly, no copy is made, so modifying the original slice will also affect the returned BinReader.
func (*BinReader) Read ¶
Read reads an amount of bytes as specified by length from the provided offset. The returned slice's length is the same as the specified length.
func (*BinReader) Reader ¶
Reader returns a new BinReader over the data read by Read(offset, length) using the same ByteOrder as this reader. There is no guarantee a copy of the data is made, so modifying the new reader's data may affect the original.
func (*BinReader) ReaderFrom ¶
ReaderFrom returns a BinReader over the data read by ReadFrom(offset) using the same ByteOrder as this reader. There is no guarantee a copy of the data is made, so modifying the new reader's data may affect the original.
func (*BinReader) Uint16 ¶
Uint16 reads 2 bytes from the provided offset and parses them into a uint16 using the provided ByteOrder.