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) { next_offset := offset + 1 if next_offset > len(buf) { return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset} } return buf[offset], next_offset, nil } // getU16 returns the bigEndian uint16 from a byte array at offset. func getU16(buf []byte, offset int) (uint16, int, error) { next_offset := offset + 2 if next_offset > len(buf) { return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset} } return binary.BigEndian.Uint16(buf[offset:]), next_offset, nil } // getU32 returns the bigEndian uint32 from a byte array at offset. func getU32(buf []byte, offset int) (uint32, int, error) { next_offset := offset + 4 if next_offset > len(buf) { return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset} } return binary.BigEndian.Uint32(buf[offset:]), next_offset, 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) { next_offset := offset + length if next_offset > len(buf) { return nil, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset} } return buf[offset:next_offset], next_offset, nil }