11 | 11 |
#else
|
12 | 12 |
# define BYTES_VALUE_CHAR "s"
|
13 | 13 |
#endif
|
14 | |
|
15 | |
#define MIN_TLSH_LEN 512
|
16 | 14 |
|
17 | 15 |
static char tlsh_doc[] =
|
18 | 16 |
"TLSH C version - similarity matching and searching";
|
|
50 | 48 |
}
|
51 | 49 |
|
52 | 50 |
Tlsh tlsh1, tlsh2;
|
53 | |
tlsh1.fromTlshStr(hash1);
|
54 | |
tlsh2.fromTlshStr(hash2);
|
|
51 |
if (tlsh1.fromTlshStr(hash1) != 0) {
|
|
52 |
return PyErr_Format(PyExc_ValueError, "argument %s is not a TLSH hex string", hash1);
|
|
53 |
}
|
|
54 |
if (tlsh2.fromTlshStr(hash2) != 0) {
|
|
55 |
return PyErr_Format(PyExc_ValueError, "argument %s is not a TLSH hex string", hash2);
|
|
56 |
}
|
55 | 57 |
|
56 | 58 |
int score = tlsh1.totalDiff(&tlsh2);
|
57 | 59 |
|
|
66 | 68 |
}
|
67 | 69 |
|
68 | 70 |
Tlsh tlsh1, tlsh2;
|
69 | |
tlsh1.fromTlshStr(hash1);
|
70 | |
tlsh2.fromTlshStr(hash2);
|
|
71 |
if (tlsh1.fromTlshStr(hash1) != 0) {
|
|
72 |
return PyErr_Format(PyExc_ValueError, "argument %s is not a TLSH hex string", hash1);
|
|
73 |
}
|
|
74 |
if (tlsh2.fromTlshStr(hash2) != 0) {
|
|
75 |
return PyErr_Format(PyExc_ValueError, "argument %s is not a TLSH hex string", hash2);
|
|
76 |
}
|
71 | 77 |
|
72 | 78 |
int score = tlsh1.totalDiff(&tlsh2, false);
|
73 | 79 |
|
|
90 | 96 |
Tlsh tlsh;
|
91 | 97 |
} tlsh_TlshObject;
|
92 | 98 |
|
|
99 |
static PyObject * Tlsh_fromTlshStr(tlsh_TlshObject *, PyObject *);
|
93 | 100 |
static PyObject * Tlsh_update(tlsh_TlshObject *, PyObject *);
|
94 | 101 |
static PyObject * Tlsh_final(tlsh_TlshObject *);
|
95 | 102 |
static PyObject * Tlsh_hexdigest(tlsh_TlshObject *);
|
96 | 103 |
static PyObject * Tlsh_diff(tlsh_TlshObject *, PyObject *);
|
97 | 104 |
|
98 | 105 |
static PyMethodDef Tlsh_methods[] = {
|
|
106 |
{"fromTlshStr", (PyCFunction) Tlsh_fromTlshStr, METH_VARARGS,
|
|
107 |
"Create a TLSH instance from a hex string."
|
|
108 |
},
|
99 | 109 |
{"update", (PyCFunction) Tlsh_update, METH_VARARGS,
|
100 | 110 |
"Update the TLSH with the given string."
|
101 | 111 |
},
|
|
156 | 166 |
};
|
157 | 167 |
|
158 | 168 |
static PyObject *
|
|
169 |
Tlsh_fromTlshStr(tlsh_TlshObject *self, PyObject *args)
|
|
170 |
{
|
|
171 |
char *str;
|
|
172 |
Py_ssize_t len;
|
|
173 |
|
|
174 |
PyObject *arg;
|
|
175 |
|
|
176 |
if (PyTuple_Size(args) != 1)
|
|
177 |
return PyErr_Format(PyExc_TypeError, "function takes exactly 1 argument (%lu given)", PyTuple_Size(args));
|
|
178 |
|
|
179 |
arg = PyTuple_GetItem(args, 0);
|
|
180 |
if (PyBytes_AsStringAndSize(arg, &str, &len) == -1) {
|
|
181 |
PyErr_SetString(PyExc_ValueError, "argument is not a TLSH hex string");
|
|
182 |
return NULL;
|
|
183 |
}
|
|
184 |
|
|
185 |
if (len != TLSH_STRING_LEN) {
|
|
186 |
PyErr_SetString(PyExc_ValueError, "argument length incorrect: not a TLSH hex string");
|
|
187 |
return NULL;
|
|
188 |
}
|
|
189 |
|
|
190 |
if (self->tlsh.fromTlshStr(str) != 0) {
|
|
191 |
PyErr_SetString(PyExc_ValueError, "argument value incorrect: not a TLSH hex string");
|
|
192 |
return NULL;
|
|
193 |
}
|
|
194 |
self->finalized = true;
|
|
195 |
|
|
196 |
Py_RETURN_NONE;
|
|
197 |
}
|
|
198 |
|
|
199 |
static PyObject *
|
159 | 200 |
Tlsh_update(tlsh_TlshObject *self, PyObject *args)
|
160 | 201 |
{
|
161 | 202 |
const char *str;
|
|
168 | 209 |
PyErr_SetString(PyExc_ValueError, "final() has already been called");
|
169 | 210 |
return NULL;
|
170 | 211 |
}
|
171 | |
if (self->required_data < MIN_TLSH_LEN) {
|
172 | |
self->required_data += len > MIN_TLSH_LEN ? MIN_TLSH_LEN : len;
|
|
212 |
if (self->required_data < MIN_DATA_LENGTH) {
|
|
213 |
self->required_data += len > MIN_DATA_LENGTH ? MIN_DATA_LENGTH : len;
|
173 | 214 |
}
|
174 | 215 |
|
175 | 216 |
self->tlsh.update((unsigned char *) str, (unsigned int) len);
|
|
184 | 225 |
PyErr_SetString(PyExc_ValueError, "final() has already been called");
|
185 | 226 |
return NULL;
|
186 | 227 |
}
|
187 | |
if (self->required_data < MIN_TLSH_LEN) {
|
188 | |
return PyErr_Format(PyExc_ValueError, "less than %u of input", MIN_TLSH_LEN);
|
|
228 |
if (self->required_data < MIN_DATA_LENGTH) {
|
|
229 |
return PyErr_Format(PyExc_ValueError, "less than %u of input", MIN_DATA_LENGTH);
|
189 | 230 |
}
|
190 | 231 |
self->finalized = true;
|
191 | 232 |
self->tlsh.final();
|