package magna import ( "encoding/binary" ) // getU8 returns the first byte from a byte array at offset. func getU8(buf []byte, offset int) (uint8, int, error) { nextOffset := offset + 1 if nextOffset > len(buf) { return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: nextOffset} } return buf[offset], nextOffset, nil } // getU16 returns the bigEndian uint16 from a byte array at offset. func getU16(buf []byte, offset int) (uint16, int, error) { nextOffset := offset + 2 if nextOffset > len(buf) { return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: nextOffset} } return binary.BigEndian.Uint16(buf[offset:]), nextOffset, nil } // getU32 returns the bigEndian uint32 from a byte array at offset. func getU32(buf []byte, offset int) (uint32, int, error) { nextOffset := offset + 4 if nextOffset > len(buf) { return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: nextOffset} } return binary.BigEndian.Uint32(buf[offset:]), nextOffset, nil } // getSlice returns a slice of bytes from a byte array at an offset and of length. func getSlice(buf []byte, offset int, length int) ([]byte, int, error) { nextOffset := offset + length if nextOffset > len(buf) { return nil, len(buf), &BufferOverflowError{Length: len(buf), Offset: nextOffset} } return buf[offset:nextOffset], nextOffset, nil }