-
Notifications
You must be signed in to change notification settings - Fork 0
/
vol_header.go
52 lines (45 loc) · 1.09 KB
/
vol_header.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
package bakemono
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
)
type VolHeaderFooter struct {
Magic uint32
CreateUnixTime int64
WritePos Offset
MajorVersion uint32
MinorVersion uint32
SyncSerial uint64
DirsChecksum uint32
Checksum uint32
}
func (v *VolHeaderFooter) GenerateChecksum() uint32 {
return crc32.ChecksumIEEE([]byte(fmt.Sprintf("%v,%v,%v,%v,%v,%v", v.Magic, v.CreateUnixTime, v.WritePos, v.MajorVersion, v.MinorVersion, v.SyncSerial)))
}
func (v *VolHeaderFooter) MarshalBinary() (data []byte, err error) {
buf := &bytes.Buffer{}
v.Magic = MagicBocchi
v.Checksum = v.GenerateChecksum()
err = binary.Write(buf, binary.BigEndian, *v)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (v *VolHeaderFooter) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.BigEndian, v)
if err != nil {
return err
}
if v.Magic != MagicBocchi {
return errors.New("invalid magic")
}
if v.Checksum != v.GenerateChecksum() {
return errors.New("invalid checksum")
}
return nil
}