-
Notifications
You must be signed in to change notification settings - Fork 33
/
pydeep.c
147 lines (133 loc) · 4.08 KB
/
pydeep.c
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
140
141
142
143
144
145
146
147
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <fuzzy.h>
#include <unistd.h>
#include <bytesobject.h>
#define PYDEEP_VERSION "0.4"
static PyObject *pydeepError;
static PyObject * pydeep_hash_file(PyObject *self, PyObject *args){
FILE *inputFile;
PyObject *ssdeepHash = NULL;
char *hashResult = NULL;
char *filename;
int ret;
if (!PyArg_ParseTuple(args, "s", &filename)){
return NULL;
}
if( access( filename, F_OK ) == -1 ) {
// File does not exist
PyErr_SetString(pydeepError, "File does not exist");
return NULL;
}
inputFile = fopen(filename, "rb");
if( inputFile <=0 ){
// We could not open the file
PyErr_SetString(pydeepError, "Error opening file");
return NULL;
}
// Allocate return buffers for hash
hashResult = (char *)malloc(FUZZY_MAX_RESULT);
if (hashResult == NULL){
PyErr_SetString(pydeepError, "Error allocating malloc buffer");
return NULL;
}
ret = fuzzy_hash_file(inputFile, hashResult);
if (ret != 0){
free(hashResult);
fclose(inputFile);
PyErr_SetString(pydeepError, "Error in fuzzy_hash!");
return NULL;
}
ssdeepHash = PyBytes_FromString(hashResult);
free(hashResult);
fclose(inputFile);
return ssdeepHash;
}
static PyObject * pydeep_hash_buf(PyObject *self, PyObject *args){
PyObject *ssdeepHash= NULL;
Py_ssize_t stringSize = 0;
char *inputBuffer=NULL;
char *hashResult;
int ret;
if (!PyArg_ParseTuple(args, "s#", &inputBuffer, &stringSize)){
return NULL;
}
// Allocate return buffers for hash
hashResult = (char *)malloc(FUZZY_MAX_RESULT);
if (hashResult == NULL){
PyErr_SetString(pydeepError, "Error allocating malloc buffer");
return NULL;
}
ret = fuzzy_hash_buf((unsigned char*)inputBuffer, (uint32_t)stringSize, hashResult);
if (ret !=0 ){
free(hashResult);
PyErr_SetString(pydeepError, "Error in fuzzy_hash!");
return NULL;
}
ssdeepHash = PyBytes_FromString(hashResult);
free(hashResult);
return ssdeepHash;
}
static PyObject * pydeep_compare(PyObject *self, PyObject *args){
char *ssdeepHash1= NULL;
char *ssdeepHash2= NULL;
int ret;
#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTuple(args, "yy", &ssdeepHash1, &ssdeepHash2)){
#else
if (!PyArg_ParseTuple(args, "ss", &ssdeepHash1, &ssdeepHash2)){
#endif
return NULL;
}
ret = fuzzy_compare(ssdeepHash1, ssdeepHash2);
if (ret < 0){
PyErr_SetString(pydeepError, "Error in fuzzy compare");
return NULL;
}
return Py_BuildValue("i", ret);
}
// Method definitions
static PyMethodDef pydeepMethods[] = {
{"hash_file", pydeep_hash_file, METH_VARARGS, "compute the ssdeep fuzzy hash for a file"},
{"hash_buf", pydeep_hash_buf, METH_VARARGS, "compute the ssdeep fuzzy hash for a buffer"},
{"hash_bytes", pydeep_hash_buf, METH_VARARGS, "compute the ssdeep fuzzy hash for a buffer"},
{"compare", pydeep_compare, METH_VARARGS, "compute similarity between two ssdeep hashes"},
{NULL, NULL}
};
#if PY_MAJOR_VERSION >=3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"pydeep", // m_name
NULL, // m_doc
-1, // m_size
pydeepMethods, // m_methods
NULL, // m_slots
NULL, // m_traverse
NULL, // m_clear
NULL // m_free
};
#endif
#if PY_MAJOR_VERSION >= 3
PyObject *PyInit_pydeep(void)
#else
void initpydeep(void)
#endif
{
PyObject *pydeep;
#if PY_MAJOR_VERSION >= 3
// Python 3
pydeep = PyModule_Create(&moduledef);
if (pydeep == NULL)
return NULL;
#else
// Python 2
pydeep = Py_InitModule3("pydeep", pydeepMethods, "C/Python bindings for ssdeep [ssdeep.sourceforge.net]");
#endif
pydeepError = PyErr_NewException("pydeep.Error", NULL, NULL);
Py_INCREF(pydeepError);
PyModule_AddObject(pydeep, "error", pydeepError);
PyModule_AddStringConstant(pydeep, "__version__", PYDEEP_VERSION);
#if PY_MAJOR_VERSION >= 3
return pydeep;
#endif
}