-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidb.go
155 lines (149 loc) · 2.93 KB
/
idb.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"strconv"
)
type entry struct {
ty rune
mode uint
user string
group string
path string
sum uint64
size uint64
cmpsize uint64
offset uint64
symval string
}
func getField(line []byte) (x, rest []byte) {
i := bytes.IndexByte(line, ' ')
if i == -1 {
x = line
} else {
x = line[:i]
rest = line[i+1:]
}
return
}
func getFieldP(line []byte) (x, p, rest []byte, err error) {
i := bytes.IndexByte(line, ' ')
if i == -1 {
x = line
} else {
x = line[:i]
rest = line[i+1:]
}
i = bytes.IndexByte(x, '(')
if i == -1 {
return x, p, rest, nil
}
x = line[:i]
rest = line[i+1:]
i = bytes.IndexByte(rest, ')')
if i == -1 {
return nil, nil, nil, errors.New("missing ')'")
}
p = rest[:i]
rest = rest[i+1:]
switch {
case len(rest) == 0:
case rest[0] == ' ':
rest = rest[1:]
default:
return nil, nil, nil, errors.New("no space after ')'")
}
return x, p, rest, nil
}
func parseEntry(line []byte, curoff *uint64) (e entry, err error) {
var f []byte
f, line = getField(line)
if len(f) != 1 || f[0] < 'a' || 'z' < f[0] {
return e, fmt.Errorf("invalid type: %q", f)
}
e.ty = rune(f[0])
f, line = getField(line)
mode, err := strconv.ParseUint(string(f), 8, strconv.IntSize)
if err != nil {
return e, fmt.Errorf("invalid mode: %q", f)
}
e.mode = uint(mode)
f, line = getField(line)
e.user = string(f)
f, line = getField(line)
e.group = string(f)
f, line = getField(line)
e.path = string(f)
_, line = getField(line)
var p []byte
for len(line) != 0 {
f, p, line, err = getFieldP(line)
if err != nil {
return e, err
}
if p == nil {
continue
}
var x uint64
switch string(f) {
case "sum":
x, err = strconv.ParseUint(string(p), 10, 64)
if err != nil {
return e, fmt.Errorf("invalid sum: %q", p)
}
e.sum = x
case "size":
x, err = strconv.ParseUint(string(p), 10, 64)
if err != nil {
return e, fmt.Errorf("invalid size: %q", p)
}
e.size = x
case "cmpsize":
x, err = strconv.ParseUint(string(p), 10, 64)
if err != nil {
return e, fmt.Errorf("invalid cmpsize: %q", p)
}
e.cmpsize = x
case "symval":
e.symval = string(p)
case "f", "exitop", "nohist", "nostrip", "mach", "postop", "config":
default:
fmt.Printf("UNKNOWN: %q\n", f)
}
}
if e.ty == 'f' {
e.offset = *curoff
*curoff += uint64(len(e.path)) + 2
if e.cmpsize > 0 {
*curoff += e.cmpsize
} else {
*curoff += e.size
}
}
return e, nil
}
func readIDB(name string) ([]entry, error) {
fp, err := os.Open(name)
if err != nil {
return nil, err
}
defer fp.Close()
sc := bufio.NewScanner(fp)
var curoff uint64 = 13
var r []entry
for lineno := 1; sc.Scan(); lineno++ {
line := sc.Bytes()
e, err := parseEntry(line, &curoff)
if err != nil {
return nil, fmt.Errorf("%s:%d: %v", name, lineno, err)
}
r = append(r, e)
}
if err := sc.Err(); err != nil {
return nil, err
}
return r, nil
}