-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
158 lines (142 loc) · 3.23 KB
/
tree.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
package magic
import (
"bytes"
"errors"
)
var (
NODE *Node
NODEForText *Node
ErrNoSignature = errors.New("No signatures in definition")
)
type Node struct {
Signature []byte
Offset int
Extension string
Children []*Node
caseInsensitive bool
}
func NewNode(caseInsensitive bool) *Node {
return &Node{caseInsensitive: caseInsensitive}
}
func (n *Node) Insert(d *Definition) {
if len(d.Signatures) == 0 {
return
}
d = copyDefinition(d, n.caseInsensitive)
for {
if n.Signature == nil && n.Children == nil {
n.Signature = d.Signatures[0].b
n.Offset = d.Signatures[0].Offset
if len(d.Signatures) > 1 {
child := &Node{caseInsensitive: n.caseInsensitive}
n.Children = append(n.Children, child)
n = child
d.Signatures = d.Signatures[1:]
continue
}
n.Extension = d.Extension
return
}
i := n.commonLength(d.Signatures[0].b)
// Split edge
if i < len(n.Signature) {
child := &Node{
Signature: n.Signature[i:],
Offset: n.Offset + i,
Children: n.Children,
Extension: n.Extension,
caseInsensitive: n.caseInsensitive,
}
n.Signature = n.Signature[:i]
n.Extension = ""
n.Children = []*Node{child}
}
// Add a child
if i < len(d.Signatures[0].b) {
child := n.findChild(d.Signatures[0])
if child == nil {
d.Signatures[0].b = d.Signatures[0].b[i:]
d.Signatures[0].Offset += i
child = &Node{caseInsensitive: n.caseInsensitive}
n.Children = append(n.Children, child)
}
n = child
} else if i == len(d.Signatures[0].b) {
if len(d.Signatures) == 1 {
n.Extension = d.Extension
return
}
d.Signatures = d.Signatures[1:]
child := n.findChild(d.Signatures[0])
if child == nil {
child = &Node{
Offset: d.Signatures[0].Offset,
caseInsensitive: n.caseInsensitive,
}
n.Children = append(n.Children, child)
}
n = child
}
}
}
func (n *Node) Match(b []byte) string {
if n.caseInsensitive {
b = bytes.ToLower(b)
}
return n.match(b[n.Offset:])
}
func (n *Node) match(b []byte) (Extension string) {
if !bytes.HasPrefix(b, n.Signature) {
return
}
for _, child := range n.Children {
offset := child.Offset - n.Offset
if len(b) < offset+len(child.Signature) || b[offset] != child.Signature[0] {
continue
}
Extension = child.match(b[offset:])
if Extension != "" {
return
}
}
return n.Extension
}
func (n *Node) commonLength(b []byte) (i int) {
limit := len(n.Signature)
if x := len(b); x < limit {
limit = x
}
for i < limit && n.Signature[i] == b[i] {
i++
}
return
}
func (n *Node) findChild(s *Signature) *Node {
for _, child := range n.Children {
if child.Offset == s.Offset && child.Signature[0] == s.b[0] {
return child
}
}
return nil
}
func copyDefinition(src *Definition, toLower bool) *Definition {
return &Definition{
Extension: src.Extension,
Signatures: copySignatures(src.Signatures, toLower),
}
}
func copySignatures(src []*Signature, toLower bool) (dst []*Signature) {
dst = make([]*Signature, len(src))
for i, s := range src {
dst[i] = &Signature{
Offset: s.Offset,
HEX: s.HEX,
String: s.String,
b: s.b,
}
if toLower {
dst[i].b = bytes.ToLower(dst[i].b)
}
}
return
}