1package magna
2
3import (
4 "encoding/binary"
5)
6
7// getU8 returns the first byte from a byte array at offset.
8func getU8(buf []byte, offset int) (uint8, int, error) {
9 next_offset := offset + 1
10 if next_offset > len(buf) {
11 return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset}
12 }
13
14 return buf[offset], next_offset, nil
15}
16
17// getU16 returns the bigEndian uint16 from a byte array at offset.
18func getU16(buf []byte, offset int) (uint16, int, error) {
19 next_offset := offset + 2
20 if next_offset > len(buf) {
21 return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset}
22 }
23
24 return binary.BigEndian.Uint16(buf[offset:]), next_offset, nil
25}
26
27// getU32 returns the bigEndian uint32 from a byte array at offset.
28func getU32(buf []byte, offset int) (uint32, int, error) {
29 next_offset := offset + 4
30 if next_offset > len(buf) {
31 return 0, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset}
32 }
33
34 return binary.BigEndian.Uint32(buf[offset:]), next_offset, nil
35}
36
37// getSlice returns a slice of bytes from a byte array at an offset and of length.
38func getSlice(buf []byte, offset int, length int) ([]byte, int, error) {
39 next_offset := offset + length
40 if next_offset > len(buf) {
41 return nil, len(buf), &BufferOverflowError{Length: len(buf), Offset: next_offset}
42 }
43
44 return buf[offset:next_offset], next_offset, nil
45}