forked from kbandla/pydeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydeep.c
117 lines (103 loc) · 3.48 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
#include <Python.h>
#include <fuzzy.h>
#include <unistd.h>
#define PYDEEP_VERSION "0.2"
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 = PyString_FromString(hashResult);
free(hashResult);
fclose(inputFile);
return ssdeepHash;
}
static PyObject * pydeep_hash_buf(PyObject *self, PyObject *args){
PyObject *inputStringBuffer = NULL;
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;
}
inputStringBuffer = PyString_FromStringAndSize(inputBuffer, stringSize);
ret = fuzzy_hash_buf((unsigned char*)inputBuffer, (uint32_t)stringSize, hashResult);
Py_XDECREF(inputStringBuffer);
if (ret !=0 ){
free(hashResult);
PyErr_SetString(pydeepError, "Error in fuzzy_hash!");
return NULL;
}
ssdeepHash = PyString_FromString( hashResult );
free(hashResult);
return ssdeepHash;
}
static PyObject * pydeep_compare(PyObject *self, PyObject *args){
char *ssdeepHash1= NULL;
char *ssdeepHash2= NULL;
int ret;
if (!PyArg_ParseTuple(args, "ss", &ssdeepHash1, &ssdeepHash2)){
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}
};
// Initialization
void initpydeep(void)
{
PyObject *pydeep;
pydeep = Py_InitModule3("pydeep", pydeepMethods, "C/Python bindings for ssdeep [ssdeep.sourceforge.net]");
pydeepError = PyErr_NewException("pydeep.Error", NULL, NULL);
Py_INCREF(pydeepError);
PyModule_AddObject(pydeep, "error", pydeepError);
PyModule_AddStringConstant(pydeep, "__version__", PYDEEP_VERSION);
}