forked from sdemontfort/go-mimemagic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
54 lines (48 loc) · 932 Bytes
/
types.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
package mimemagic
import "bytes"
type section struct {
Name string
matchers []matcher
}
type matcher struct {
Level, Offset, Range int
Value []byte
Mask []byte
}
func Match(guess string, dat []byte) string {
if s := smap[guess]; s != nil && matchSection(s, dat) {
return s.Name
}
for i := 0; i < len(sarr); i++ {
if matchSection(&sarr[i], dat) {
return sarr[i].Name
}
}
return ""
}
func matchSection(s *section, b []byte) bool {
lvl := 0
state := false
for i := 0; i < len(s.matchers); i++ {
m := &s.matchers[i]
if m.Level <= lvl && state {
return true
}
if m.Level > lvl && !state {
continue
}
state = false
lvl = m.Level
if len(b) < m.Offset+len(m.Value) {
continue
}
max := m.Offset + len(m.Value) + m.Range
if max > len(b) {
max = len(b)
}
if bytes.Index(b[m.Offset:max], m.Value) >= 0 {
state = true
}
}
return state
}