-
Notifications
You must be signed in to change notification settings - Fork 1
/
iter.go
329 lines (261 loc) · 6.84 KB
/
iter.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package dextk
import (
"errors"
"fmt"
)
var (
ErrIterEnd = errors.New("iter end reached")
ErrBadClass = errors.New("malformed class def")
)
type StringIter struct {
r *Reader
pos uint32
}
type StringNode struct {
Id uint32
Value String
}
func (r *Reader) StringIter() *StringIter {
return &StringIter{
r: r,
pos: 0,
}
}
func (i *StringIter) Seek(pos uint32) {
i.pos = pos
}
func (i *StringIter) HasNext() bool {
return i.pos < i.r.StringIDCount
}
func (i *StringIter) Next() (StringNode, error) {
var res StringNode
res.Id = i.pos
if i.pos >= i.r.StringIDCount {
return res, ErrIterEnd
}
value, err := i.r.ReadString(i.pos)
if err != nil {
return res, err
}
res.Value = value
i.pos++
return res, nil
}
type ClassIter struct {
r *Reader
pos uint32
}
type ClassNode struct {
Id uint32
Name String
AccessFlags uint32
SuperClass String
Interfaces []String
SourceFile String
StaticFields []FieldNode
InstanceFields []FieldNode
DirectMethods []MethodNode
VirtualMethods []MethodNode
}
type FieldNode struct {
Id uint32
AccessFlags uint32
Type TypeDescriptor
Name String
}
type MethodNode struct {
Id uint32
AccessFlags uint32
Name String
Shorty String
ReturnType TypeDescriptor
Params []TypeDescriptor
CodeOff uint32
}
func (r *Reader) ClassIter() *ClassIter {
return &ClassIter{
r: r,
pos: 0,
}
}
func (i *ClassIter) Seek(pos uint32) {
i.pos = pos
}
func (i *ClassIter) HasNext() bool {
return i.pos < i.r.ClassDefCount
}
func (i *ClassIter) Next() (ClassNode, error) {
if i.pos >= i.r.ClassDefCount {
return ClassNode{}, ErrIterEnd
}
res, err := i.r.ReadClassAndParse(i.pos)
if err != nil {
return res, err
}
i.pos++
return res, nil
}
func (r *Reader) ReadClassAndParse(id uint32) (ClassNode, error) {
var res ClassNode
if id >= r.ClassDefCount {
return res, ErrInvalidClassDefID
}
res.Id = id
def, err := r.ReadClassDef(id)
if err != nil {
return res, fmt.Errorf("%w: %w", ErrBadClass, err)
}
res.AccessFlags = def.AccessFlags
// Read class name
{
parsedDesc, err := r.ReadTypeAndParse(def.ClassTypeID)
if err != nil {
return res, fmt.Errorf("%w: bad class type: %w", ErrBadClass, err)
}
if !parsedDesc.IsClass() || parsedDesc.IsArray() {
return res, fmt.Errorf("%w: invalid descriptor", ErrBadClass)
}
res.Name = parsedDesc.ClassName
}
// Parse superclass
if def.SuperclassTypeID != NoIndex {
parsedDesc, err := r.ReadTypeAndParse(def.SuperclassTypeID)
if err != nil {
return res, fmt.Errorf("%w: bad superclass type: %w", ErrBadClass, err)
}
if !parsedDesc.IsClass() || parsedDesc.IsArray() {
return res, fmt.Errorf("%w: invalid superclass descriptor", ErrBadClass)
}
res.SuperClass = parsedDesc.ClassName
}
// Parse interfaces
if def.InterfacesTypeListOff != 0 {
list, err := r.ReadTypeList(def.InterfacesTypeListOff)
if err != nil {
return res, fmt.Errorf("%w: bad interface list: %w", ErrBadClass, err)
}
res.Interfaces = make([]String, len(list.TypeIds))
for p, id := range list.TypeIds {
parsedDesc, err := r.ReadTypeAndParse(uint32(id))
if err != nil {
return res, fmt.Errorf("%w: bad iface type: %w", ErrBadClass, err)
}
if !parsedDesc.IsClass() || parsedDesc.IsArray() {
return res, fmt.Errorf("%w: invalid iface descriptor", ErrBadClass)
}
res.Interfaces[p] = parsedDesc.ClassName
}
}
// Parse source file
if def.SourceFileStringID != NoIndex {
res.SourceFile, err = r.ReadString(def.SourceFileStringID)
if err != nil {
return res, fmt.Errorf("%w: bad src file: %w", ErrBadClass, err)
}
}
// Parse annotations
if def.AnnotationsDirectoryOff != 0 {
// TODO: Parse annotations
}
// Parse data
if def.ClassDataOff != 0 {
data, err := r.ReadClassData(def.ClassDataOff)
if err != nil {
return res, fmt.Errorf("%w: bad data: %w", ErrBadClass, err)
}
res.StaticFields, err = r.parseFields(def.ClassTypeID, data.StaticFields)
if err != nil {
return res, err
}
res.InstanceFields, err = r.parseFields(def.ClassTypeID, data.InstanceFields)
if err != nil {
return res, err
}
res.DirectMethods, err = r.parseMethods(def.ClassTypeID, data.DirectMethods)
if err != nil {
return res, err
}
res.VirtualMethods, err = r.parseMethods(def.ClassTypeID, data.VirtualMethods)
if err != nil {
return res, err
}
}
return res, nil
}
func (r *Reader) parseFields(classTypeId uint32, input []EncodedField) ([]FieldNode, error) {
res := make([]FieldNode, len(input))
for i, f := range input {
fp, err := r.ReadField(f.FieldID)
if err != nil {
return res, fmt.Errorf("%w: bad field: %w", ErrBadClass, err)
}
if classTypeId != uint32(fp.ClassTypeID) {
return res, fmt.Errorf("%w: bad field: mismatch class id", ErrBadClass)
}
name, err := r.ReadString(fp.NameStringID)
if err != nil {
return res, fmt.Errorf("%w: bad field name: %w", ErrBadClass, err)
}
def, err := r.ReadTypeAndParse(uint32(fp.TypeID))
if err != nil {
return res, fmt.Errorf("%w: bad field type: %w", ErrBadClass, err)
}
res[i] = FieldNode{
Id: f.FieldID,
AccessFlags: f.AccessFlags,
Type: def,
Name: name,
}
}
return res, nil
}
func (r *Reader) parseMethods(classTypeId uint32, input []EncodedMethod) ([]MethodNode, error) {
res := make([]MethodNode, len(input))
for i, m := range input {
mp, err := r.ReadMethod(m.MethodID)
if err != nil {
return res, fmt.Errorf("%w: bad method: %w", ErrBadClass, err)
}
if classTypeId != uint32(mp.ClassTypeID) {
return res, fmt.Errorf("%w: bad method: mismatch class id", ErrBadClass)
}
name, err := r.ReadString(mp.NameStringID)
if err != nil {
return res, fmt.Errorf("%w: bad method name: %w", ErrBadClass, err)
}
proto, err := r.ReadProto(uint32(mp.ProtoID))
if err != nil {
return res, fmt.Errorf("%w: bad method proto: %w", ErrBadClass, err)
}
shorty, err := r.ReadString(proto.ShortyStringID)
if err != nil {
return res, fmt.Errorf("%w: bad method shorty: %w", ErrBadClass, err)
}
retDef, err := r.ReadTypeAndParse(proto.ReturnTypeID)
if err != nil {
return res, fmt.Errorf("%w: bad method type: %w", ErrBadClass, err)
}
res[i] = MethodNode{
Id: m.MethodID,
AccessFlags: m.AccessFlags,
Name: name,
Shorty: shorty,
ReturnType: retDef,
CodeOff: m.CodeOff,
}
if proto.ParametersTypeListOff != 0 {
plist, err := r.ReadTypeList(proto.ParametersTypeListOff)
if err != nil {
return res, fmt.Errorf("%w: bad method params: %w", ErrBadClass, err)
}
res[i].Params = make([]TypeDescriptor, len(plist.TypeIds))
for j, tid := range plist.TypeIds {
res[i].Params[j], err = r.ReadTypeAndParse(uint32(tid))
if err != nil {
return res, fmt.Errorf("%w: bad method paramm: %w", ErrBadClass, err)
}
}
}
}
return res, nil
}