-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsections.go
281 lines (247 loc) · 6.35 KB
/
sections.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package goteafiles
import (
"encoding/binary"
"fmt"
"github.com/satori/go.uuid"
"io"
"reflect"
)
type ItemSection struct {
Info ItemSectionInfo
Fields []ItemSectionField
}
type ItemSectionInfo struct {
ItemSize int32
ItemTypeName string
FieldCount int32
}
type ItemSectionField struct {
Index int32
Type int32
Offset int32
Name string
}
func (is *ItemSection) Read(r io.Reader, order binary.ByteOrder) error {
err := binary.Read(r, order, &is.Info.ItemSize)
if err != nil { return err }
is.Info.ItemTypeName, err = readText(r, order)
if err != nil { return err }
err = binary.Read(r, order, &is.Info.FieldCount)
if err != nil { return err }
var i int32
for i = 0; i < is.Info.FieldCount; i++ {
f := ItemSectionField{
Index: i,
}
err = binary.Read(r, order, &f.Type)
if err != nil { return err }
err = binary.Read(r, order, &f.Offset)
if err != nil { return err }
f.Name, err = readText(r, order)
if err != nil { return err }
is.Fields = append(is.Fields, f)
}
return nil
}
func (is *ItemSection) Write(w io.Writer, order binary.ByteOrder) error {
err := binary.Write(w, order, is.Info.ItemSize)
if err != nil { return err }
err = writeText(w, order, is.Info.ItemTypeName)
if err != nil { return err }
err = binary.Write(w, order, is.Info.FieldCount)
if err != nil { return err }
for _, field := range is.Fields {
err = binary.Write(w, order, field.Type)
if err != nil { return err }
err = binary.Write(w, order, field.Offset)
if err != nil { return err }
err = writeText(w, order, field.Name)
if err != nil { return err }
}
return nil
}
func (is *ItemSection) Size() int64 {
var size int64 = 0
// ItemSize
size += 4
// ItemTypeName
size += textSize(is.Info.ItemTypeName)
// FieldCount
size += 4
for _, field := range is.Fields {
// FieldType
size += 4
// FieldOffset
size += 4
// FieldName
size += textSize(field.Name)
}
return size
}
type NameValueSection struct {
NameValues map[string]interface{}
}
func defaultNameValueSection() *NameValueSection {
return &NameValueSection{
NameValues: make(map[string]interface{}),
}
}
func (nv *NameValueSection) Read(r io.Reader, order binary.ByteOrder) error {
nv.NameValues = make(map[string]interface{})
var count int32
err := binary.Read(r, order, &count)
if err != nil { return err }
var i int32
for i = 0; i < count; i += 1 {
name, err := readText(r, order)
if err != nil { return err }
var kind int32
err = binary.Read(r, order, &kind)
if err != nil { return err }
switch kind {
case NAME_VALUE_INT32:
var value int32
err = binary.Read(r, order, &value)
if err != nil { return err }
nv.NameValues[name] = value
case NAME_VALUE_TEXT:
value, err := readText(r, order)
if err != nil { return err }
nv.NameValues[name] = value
case NAME_VALUE_DOUBLE:
var value float64
err = binary.Read(r, order, &value)
if err != nil { return err }
nv.NameValues[name] = value
case NAME_VALUE_UUID:
bytes := make([]byte, 16)
err = binary.Read(r, order, bytes)
if err != nil { return err }
value, err := uuid.FromBytes(bytes)
if err != nil { return err }
nv.NameValues[name] = value
case NAME_VALUE_UINT64:
var value uint64
err = binary.Read(r, order, &value)
if err != nil { return err }
nv.NameValues[name] = value
default:
return fmt.Errorf("unknown name value kind %d", kind)
}
}
return nil
}
func (nv *NameValueSection) Write(w io.Writer, order binary.ByteOrder) error {
var count = int32(len(nv.NameValues))
err := binary.Write(w, order, count)
if err != nil { return err }
for key, val := range nv.NameValues {
err = writeText(w, order, key)
if err != nil { return err }
nameValueType := typeToNameValueType[reflect.TypeOf(val).String()]
err := binary.Write(w, order, nameValueType)
if err != nil { return err }
if nameValueType == NAME_VALUE_TEXT {
err = writeText(w, order, val.(string))
} else {
err = binary.Write(w, order, val)
}
if err != nil { return err }
}
return nil
}
func (nv *NameValueSection) Size() int64 {
var size int64 = 0
// Count
size += 4
for name, val := range nv.NameValues {
// Name
size += textSize(name)
// ValueType
size += 4
// Value
if reflect.TypeOf(val).Kind() == reflect.String {
size += textSize(val.(string))
} else {
size += int64(reflect.TypeOf(val).Size())
}
}
return size
}
type TimeSection struct {
Epoch int64
TicksPerDay int64
Count int32
Offsets []int32
}
// default to micro second with no time field
func defaultTimeSection() *TimeSection {
return &TimeSection{
Epoch: 719162,
TicksPerDay: 86400000000,
Count: 0,
Offsets: nil,
}
}
func (ts *TimeSection) Read(r io.Reader, order binary.ByteOrder) error {
err := binary.Read(r, order, &ts.Epoch)
if err != nil { return err }
err = binary.Read(r, order, &ts.TicksPerDay)
if err != nil { return err }
err = binary.Read(r, order, &ts.Count)
if err != nil { return err }
for i := 0; i < int(ts.Count); i += 1 {
var offset int32
err = binary.Read(r, order, &offset)
if err != nil { return err }
ts.Offsets = append(ts.Offsets, offset)
}
return nil
}
func (ts *TimeSection) Write(w io.Writer, order binary.ByteOrder) error {
err := binary.Write(w, order, ts.Epoch)
if err != nil { return err }
err = binary.Write(w, order, ts.TicksPerDay)
if err != nil { return err }
err = binary.Write(w, order, ts.Count)
if err != nil { return err }
for _, offset := range ts.Offsets {
err = binary.Write(w, order, offset)
if err != nil { return err }
}
return nil
}
func (ts *TimeSection) Size() int64 {
var size int64 = 0
// Epoch
size += 8
// TicksPerDay
size += 8
// Count
size += 4
// Offsets
size += 4 * int64(len(ts.Offsets))
return size
}
type ContentDescriptionSection struct {
ContentDescription string
}
// default to micro second with no time field
func defaultContentDescriptionSection() *ContentDescriptionSection {
return &ContentDescriptionSection{
ContentDescription: "",
}
}
func (s *ContentDescriptionSection) Read(r io.Reader, order binary.ByteOrder) error {
var err error
s.ContentDescription, err = readText(r, order)
return err
}
func (s *ContentDescriptionSection) Write(r io.Writer, order binary.ByteOrder) error {
return writeText(r, order, s.ContentDescription)
}
func (s *ContentDescriptionSection) Size() int64 {
var size int64 = 0
size += textSize(s.ContentDescription)
return size
}