forked from pivotal-cf/cred-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
45 lines (38 loc) · 933 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
package mimemagic
import "bytes"
//import "fmt"
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 }
// fmt.Printf("%-30s %#v state=%v\n",s.Name,m,state)
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
}