forked from kdomanski/iso9660
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iso9660_datatypes.go
63 lines (51 loc) · 1.78 KB
/
iso9660_datatypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package iso9660
import (
"encoding/binary"
"fmt"
"io"
"strings"
)
func MarshalString(s string, padToLength int) []byte {
if len(s) > padToLength {
s = s[:padToLength]
}
missingPadding := padToLength - len(s)
s = s + strings.Repeat(" ", missingPadding)
return []byte(s)
}
// UnmarshalInt32LSBMSB decodes a 32-bit integer in both byte orders, as defined in ECMA-119 7.3.3
func UnmarshalInt32LSBMSB(data []byte) (int32, error) {
if len(data) < 8 {
return 0, io.ErrUnexpectedEOF
}
lsb := int32(binary.LittleEndian.Uint32(data[0:4]))
msb := int32(binary.BigEndian.Uint32(data[4:8]))
if lsb != msb {
return 0, fmt.Errorf("little-endian and big-endian value mismatch: %d != %d", lsb, msb)
}
return lsb, nil
}
// UnmarshalInt16LSBMSB decodes a 16-bit integer in both byte orders, as defined in ECMA-119 7.3.3
func UnmarshalInt16LSBMSB(data []byte) (int16, error) {
if len(data) < 4 {
return 0, io.ErrUnexpectedEOF
}
lsb := int16(binary.LittleEndian.Uint16(data[0:2]))
msb := int16(binary.BigEndian.Uint16(data[2:4]))
if lsb != msb {
return 0, fmt.Errorf("little-endian and big-endian value mismatch: %d != %d", lsb, msb)
}
return lsb, nil
}
// WriteInt32LSBMSB writes a 32-bit integer in both byte orders, as defined in ECMA-119 7.3.3
func WriteInt32LSBMSB(dst []byte, value int32) {
_ = dst[7] // early bounds check to guarantee safety of writes below
binary.LittleEndian.PutUint32(dst[0:4], uint32(value))
binary.BigEndian.PutUint32(dst[4:8], uint32(value))
}
// WriteInt16LSBMSB writes a 16-bit integer in both byte orders, as defined in ECMA-119 7.2.3
func WriteInt16LSBMSB(dst []byte, value int16) {
_ = dst[3] // early bounds check to guarantee safety of writes below
binary.LittleEndian.PutUint16(dst[0:2], uint16(value))
binary.BigEndian.PutUint16(dst[2:4], uint16(value))
}