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
|
// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package binstruct
import (
"fmt"
"reflect"
"strconv"
"strings"
"git.lukeshu.com/go/typedsync"
"git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil"
)
type End struct{}
var endType = reflect.TypeOf(End{})
type tag struct {
skip bool
off int
siz int
}
func parseStructTag(str string) (tag, error) {
var ret tag
for _, part := range strings.Split(str, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
if part == "-" {
return tag{skip: true}, nil
}
keyval := strings.SplitN(part, "=", 2)
if len(keyval) != 2 {
return tag{}, fmt.Errorf("option is not a key=value pair: %q", part)
}
key := keyval[0]
val := keyval[1]
switch key {
case "off":
vint, err := strconv.ParseInt(val, 0, 0)
if err != nil {
return tag{}, err
}
ret.off = int(vint)
case "siz":
vint, err := strconv.ParseInt(val, 0, 0)
if err != nil {
return tag{}, err
}
ret.siz = int(vint)
default:
return tag{}, fmt.Errorf("unrecognized option %q", key)
}
}
return ret, nil
}
type structHandler struct {
name string
Size int
fields []structField
}
type structField struct {
name string
isUnmarshaler bool
tag
}
func (sh structHandler) Unmarshal(dat []byte, dst reflect.Value) (int, error) {
if err := binutil.NeedNBytes(dat, sh.Size); err != nil {
return 0, fmt.Errorf("struct %q %w", sh.name, err)
}
var n int
for i, field := range sh.fields {
if field.skip {
continue
}
_n, err := unmarshal(dat[n:], dst.Field(i), field.isUnmarshaler)
if err != nil {
if _n >= 0 {
n += _n
}
return n, fmt.Errorf("struct %q field %v %q: %w",
sh.name, i, field.name, err)
}
if _n != field.siz {
return n, fmt.Errorf("struct %q field %v %q: consumed %v bytes but should have consumed %v bytes",
sh.name, i, field.name, _n, field.siz)
}
n += _n
}
return n, nil
}
func (sh structHandler) Marshal(val reflect.Value) ([]byte, error) {
ret := make([]byte, 0, sh.Size)
for i, field := range sh.fields {
if field.skip {
continue
}
bs, err := Marshal(val.Field(i).Interface())
ret = append(ret, bs...)
if err != nil {
return ret, fmt.Errorf("struct %q field %v %q: %w",
sh.name, i, field.name, err)
}
}
return ret, nil
}
func genStructHandler(structInfo reflect.Type) (structHandler, error) {
var ret structHandler
ret.name = structInfo.String()
var curOffset, endOffset int
for i := 0; i < structInfo.NumField(); i++ {
fieldInfo := structInfo.Field(i)
if fieldInfo.Anonymous && fieldInfo.Type != endType {
err := fmt.Errorf("binstruct does not support embedded fields")
return ret, fmt.Errorf("struct %q field %v %q: %w",
ret.name, i, fieldInfo.Name, err)
}
fieldTag, err := parseStructTag(fieldInfo.Tag.Get("bin"))
if err != nil {
return ret, fmt.Errorf("struct %q field %v %q: %w",
ret.name, i, fieldInfo.Name, err)
}
if fieldTag.skip {
ret.fields = append(ret.fields, structField{
tag: fieldTag,
name: fieldInfo.Name,
})
continue
}
if fieldTag.off != curOffset {
err := fmt.Errorf("tag says off=%#x but curOffset=%#x", fieldTag.off, curOffset)
return ret, fmt.Errorf("struct %q field %v %q: %w",
ret.name, i, fieldInfo.Name, err)
}
if fieldInfo.Type == endType {
endOffset = curOffset
}
fieldSize, err := staticSize(fieldInfo.Type)
if err != nil {
return ret, fmt.Errorf("struct %q field %v %q: %w",
ret.name, i, fieldInfo.Name, err)
}
if fieldTag.siz != fieldSize {
err := fmt.Errorf("tag says siz=%#x but StaticSize(typ)=%#x", fieldTag.siz, fieldSize)
return ret, fmt.Errorf("struct %q field %v %q: %w",
ret.name, i, fieldInfo.Name, err)
}
curOffset += fieldTag.siz
ret.fields = append(ret.fields, structField{
name: fieldInfo.Name,
isUnmarshaler: reflect.PtrTo(fieldInfo.Type).Implements(unmarshalerType),
tag: fieldTag,
})
}
ret.Size = curOffset
if ret.Size != endOffset {
return ret, fmt.Errorf("struct %q: .Size=%v but endOffset=%v",
ret.name, ret.Size, endOffset)
}
return ret, nil
}
var structCache typedsync.CacheMap[reflect.Type, structHandler]
func getStructHandler(typ reflect.Type) structHandler {
ret, _ := structCache.LoadOrCompute(typ, func(typ reflect.Type) structHandler {
h, err := genStructHandler(typ)
if err != nil {
panic(&InvalidTypeError{
Type: typ,
Err: err,
})
}
return h
})
return ret
}
|