forked from asalih/grapeSQLI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFingerprints.go
100 lines (82 loc) · 1.85 KB
/
Fingerprints.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
package GSQLI
import (
"encoding/json"
"io/ioutil"
"sort"
"strings"
)
func UnmarshalSqlifingerprint(data []byte) (Sqlifingerprint, error) {
var r Sqlifingerprint
err := json.Unmarshal(data, &r)
return r, err
}
func (r *Sqlifingerprint) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Sqlifingerprint struct {
Charmap []string `json:"charmap"`
Fingerprints []string `json:"fingerprints"`
Keywords map[string]Keyword `json:"keywords"`
}
type Keyword string
const (
A Keyword = "A"
B Keyword = "B"
E Keyword = "E"
Empty Keyword = "&"
F Keyword = "f"
K Keyword = "k"
KeywordT Keyword = "T"
N Keyword = "n"
O Keyword = "o"
T Keyword = "t"
The1 Keyword = "1"
U Keyword = "U"
V Keyword = "v"
)
type keyword_t struct {
word string
vtype byte
}
var (
fingerprints Sqlifingerprint
szKeywordMap map[string]keyword_t = map[string]keyword_t{}
)
func init() {
for _, vk := range szkeywords {
szKeywordMap[strings.ToUpper(vk.word)] = vk
}
}
func initData(body []byte) error {
sf, err := UnmarshalSqlifingerprint(body)
if err != nil {
return err
}
fingerprints = sf
// 做个排序
sort.Strings(fingerprints.Fingerprints)
return nil
}
// 在二分查找中,数据必须做足够的排序,且排序必须是升序
func LoadData(filename string) error {
body, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return initData(body)
}
func Lookup(key string) int {
if len(fingerprints.Fingerprints) == 0 {
return 0
}
// 由于SORT内的SEARCH就是二分查找法,所以不必单独编写
upKey := strings.ToUpper(key)
pos := sort.SearchStrings(fingerprints.Fingerprints, upKey)
if pos == -1 {
return 0
}
if strings.Compare(fingerprints.Fingerprints[pos], upKey) == 0 {
return pos
}
return 0
}