-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
55 lines (49 loc) · 1.04 KB
/
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
53
54
55
package d2editor
import (
"github.com/vitalick/go-d2editor/bitworker"
)
const defaultMagic = 0xAA55AA55
//Header ...
type Header struct {
Magic uint32 `json:"-"`
Version uint32 `json:"version"`
Filesize uint32 `json:"filesize"`
Checksum uint32 `json:"checksum"`
}
//NewEmptyHeader returns empty Header
func NewEmptyHeader(version uint) *Header {
return &Header{defaultMagic, uint32(version), 0, 0}
}
//NewHeader returns Header from packed bytes
func NewHeader(br *bitworker.BitReader) (*Header, error) {
h := &Header{}
var err error
h.Magic, err = br.ReadNextBitsUint32()
if err != nil {
return nil, err
}
h.Version, err = br.ReadNextBitsUint32()
if err != nil {
return nil, err
}
h.Filesize, err = br.ReadNextBitsUint32()
if err != nil {
return nil, err
}
h.Checksum, err = br.ReadNextBitsUint32()
if err != nil {
return nil, err
}
if h.Magic == 0 {
h.Magic = defaultMagic
}
return h, nil
}
func ChecksumAppend(b byte, c int) int {
secondVal := 0
if c < 0 {
secondVal = 1
}
c = int(b) + c*2 + secondVal
return c
}