-
Notifications
You must be signed in to change notification settings - Fork 7
/
ssdeep.go
139 lines (112 loc) · 2.8 KB
/
ssdeep.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
package ssdeep
/*
#cgo !windows LDFLAGS:-L/usr/local/lib/ -lfuzzy -ldl -I/usr/local/include/
#include <stdlib.h>
#include <fuzzy.h>
*/
import "C"
import (
"errors"
"unsafe"
)
type FuzzyState struct {
cstate *C.struct_fuzzy_state
}
/*
Construct a fuzzy_state object and return it.
*/
func New() (*FuzzyState, error) {
var cstate *C.struct_fuzzy_state
if cstate = C.fuzzy_new(); cstate == nil {
return nil, errors.New("")
}
return &FuzzyState{cstate}, nil
}
/*
Create a copy of a fuzzy_state object and return it.
*/
func (fs *FuzzyState) Clone() (*FuzzyState, error) {
var cstate *C.struct_fuzzy_state
if cstate = C.fuzzy_clone(fs.cstate); cstate == nil {
return nil, errors.New("")
}
return &FuzzyState{cstate}, nil
}
/*
Dispose a fuzzy state.
*/
func (fs *FuzzyState) Free() {
C.fuzzy_free(fs.cstate)
}
/*
Feed the data contained in the given buffer to the state.<F37>
*/
func (fs *FuzzyState) Update(str string) error {
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
length := C.size_t(len(str))
if C.fuzzy_update(fs.cstate, (*C.uchar)(unsafe.Pointer(cstr)), length) != 0 {
return errors.New("")
}
return nil
}
/*
Obtain the fuzzy hash from the state.
*/
func (fs *FuzzyState) Digest() (string, error) {
buf := (*C.char)(C.calloc(C.FUZZY_MAX_RESULT, 1))
defer C.free(unsafe.Pointer(buf))
if C.fuzzy_digest(fs.cstate, buf, C.uint(0)) != 0 {
return "", errors.New("")
}
return C.GoString(buf), nil
}
/*
Computes the match score between two fuzzy hash signatures.
*/
func Compare(str1, str2 string) (int, error) {
cstr1 := C.CString(str1)
defer C.free(unsafe.Pointer(cstr1))
cstr2 := C.CString(str2)
defer C.free(unsafe.Pointer(cstr2))
if score := C.fuzzy_compare(cstr1, cstr2); score >= 0 {
return int(score), nil
}
return -1, errors.New("")
}
/*
Obtain the fuzzy hash from the state.
*/
func HashString(str string) (string, error) {
buf := (*C.char)(C.calloc(C.FUZZY_MAX_RESULT, 1))
defer C.free(unsafe.Pointer(buf))
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
length := C.uint32_t(len(str))
if C.fuzzy_hash_buf((*C.uchar)(unsafe.Pointer(cstr)), length, buf) != 0 {
return "", errors.New("")
}
return C.GoString(buf), nil
}
/*
Compute the fuzzy hash of a file.
*/
func HashFilename(filename string) (string, error) {
buf := (*C.char)(C.calloc(C.FUZZY_MAX_RESULT, 1))
defer C.free(unsafe.Pointer(buf))
cf := C.CString(filename)
defer C.free(unsafe.Pointer(cf))
if C.fuzzy_hash_filename(cf, buf) != 0 {
return "", errors.New("")
}
return C.GoString(buf), nil
}
func HashBytes(b []byte) (string, error) {
buf := (*C.char)(C.calloc(C.FUZZY_MAX_RESULT, 1))
defer C.free(unsafe.Pointer(buf))
length := C.uint32_t(len(b))
if C.fuzzy_hash_buf((*C.uchar)(unsafe.Pointer(&b[0])), length, buf) != 0 {
return "", errors.New("")
}
return C.GoString(buf), nil
}