-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirent.go
168 lines (126 loc) · 3.22 KB
/
dirent.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package gc
import (
"bytes"
"encoding/binary"
"errors"
"io"
"time"
"github.com/bodgit/gc/internal/hash"
"github.com/bodgit/plumbing"
)
var errBadDirectoryChecksum = errors.New("bad directory checksum")
const (
entryReserved1Offset = 0x06
entryReserved2Offset = 0x3a
entryReserved2Size = 0x02
)
//nolint:gochecknoglobals
var epoch = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
type entry struct {
GameCode [4]byte
MakerCode [2]byte
_ byte
BannerFormat byte
Filename [32]byte
LastModified uint32
ImageDataOffset uint32
IconGfxFormat uint16
AnimationSpeed uint16
Permissions byte
CopyCounter byte
FirstBlock uint16
FileLength uint16
_ [entryReserved2Size]byte
CommentAddress uint32
}
func (e *entry) isEmpty() bool {
return *e == newEntry()
}
func (e *entry) gameCode() string {
return string(e.GameCode[:])
}
func (e *entry) makerCode() string {
return string(e.MakerCode[:])
}
func (e *entry) filename() string {
return string(bytes.TrimRight(e.Filename[:], "\x00"))
}
func (e *entry) lastModified() time.Time {
return epoch.Add(time.Second * time.Duration(e.LastModified))
}
func (e *entry) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
buf.Grow(binary.Size(e))
_ = binary.Write(buf, binary.BigEndian, e)
b := buf.Bytes()
b[entryReserved1Offset] = 0xff
for i := 0; i < entryReserved2Size; i++ {
b[entryReserved2Offset+i] = 0xff
}
return b, nil
}
func newEntry() entry {
e := entry{}
_ = binary.Read(plumbing.FillReader(0xff), binary.BigEndian, &e) //nolint:gomnd
return e
}
const (
maxEntries = 127
directoryReserved1Size = 0x003a
)
type directory struct {
Entries [maxEntries]entry
_ [directoryReserved1Size]byte
UpdateCounter uint16
Checksum [checksums][hash.Size]byte
}
func (d *directory) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
buf.Grow(binary.Size(d))
for _, e := range d.Entries {
b, _ := e.MarshalBinary()
_, _ = buf.Write(b)
}
//nolint:gomnd
_, _ = io.CopyN(buf, plumbing.FillReader(0xff), directoryReserved1Size)
_ = binary.Write(buf, binary.BigEndian, d.UpdateCounter)
_ = binary.Write(buf, binary.BigEndian, d.Checksum)
return buf.Bytes(), nil
}
func (d *directory) generateChecksums() ([]byte, []byte, error) {
b, err := d.MarshalBinary()
if err != nil {
return nil, nil, err
}
// Checksum everything upto the checksum fields
normal, inverted := checksum(b[:blockSize-checksums*hash.Size])
return normal, inverted, nil
}
func (d *directory) checksum() error {
normal, inverted, err := d.generateChecksums()
if err != nil {
return err
}
copy(d.Checksum[checksumNormal][:], normal)
copy(d.Checksum[checksumInverted][:], inverted)
return nil
}
func (d *directory) isValid() error {
normal, inverted, err := d.generateChecksums()
if err != nil {
return err
}
c1, c2 := d.Checksum[checksumNormal][:], d.Checksum[checksumInverted][:]
if !bytes.Equal(c1, normal) || !bytes.Equal(c2, inverted) {
return errBadDirectoryChecksum
}
return nil
}
func newDirectory(updateCounter uint16) directory {
d := directory{}
for i := range d.Entries {
d.Entries[i] = newEntry()
}
d.UpdateCounter = updateCounter
return d
}