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; } inputFile = fopen(filename, "rb"); // 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 * _cffi_f_fuzzy_hash_file(PyObject *self, PyObject *args) { FILE * x0; char * x1; Py_ssize_t datasize; int result; PyObject *arg0; PyObject *arg1; if (!PyArg_ParseTuple(args, "OO:fuzzy_hash_file", &arg0, &arg1)) return NULL; datasize = _cffi_prepare_pointer_call_argument( _cffi_type(6), arg0, (char **)&x0); if (datasize != 0) { if (datasize < 0) return NULL; x0 = alloca(datasize); memset((void *)x0, 0, datasize); if (_cffi_convert_array_from_object((char *)x0, _cffi_type(6), arg0) < 0) return NULL; } datasize = _cffi_prepare_pointer_call_argument( _cffi_type(2), arg1, (char **)&x1); if (datasize != 0) { if (datasize < 0) return NULL; x1 = alloca(datasize); memset((void *)x1, 0, datasize); if (_cffi_convert_array_from_object((char *)x1, _cffi_type(2), arg1) < 0) return NULL; } Py_BEGIN_ALLOW_THREADS _cffi_restore_errno(); { result = fuzzy_hash_file(x0, x1); } _cffi_save_errno(); Py_END_ALLOW_THREADS return _cffi_from_c_int(result, int); }
int main(int argc, char **argv) { unsigned char * buf; char * result, * result2; FILE *handle; srand(1); buf = (unsigned char *)malloc(SIZE); result = (char *)malloc(FUZZY_MAX_RESULT); result2 = (char *)malloc(FUZZY_MAX_RESULT); if (NULL == result || NULL == buf || NULL == result2) { fprintf (stderr,"%s: Out of memory\n", argv[0]); return EXIT_FAILURE; } generate_random(buf,SIZE); if (write_data(buf,SIZE,FILENAME)) return EXIT_FAILURE; printf ("Hashing buffer\n"); int status = fuzzy_hash_buf(buf,SIZE,result); if (status) printf ("Error during buf hash\n"); else printf ("%s\n", result); handle = fopen(FILENAME,"rb"); if (NULL == handle) { perror(FILENAME); return EXIT_FAILURE; } printf ("Hashing file\n"); status = fuzzy_hash_file(handle,result); if (status) printf ("Error during file hash\n"); else printf ("%s\n", result); fclose(handle); printf ("Modifying buffer and comparing to file\n"); int i; for (i = 0x100 ; i < 0x110 ; ++i) buf[i] = 37; status = fuzzy_hash_buf(buf,SIZE,result2); if (status) printf ("Error during buffer hash\n"); else printf ("%s\n", result2); i = fuzzy_compare(result,result2); if (-1 == i) printf ("An error occured during matching\n"); else { if (i != 0) printf ("MATCH: score = %d\n", i); else printf ("did not match\n"); } return EXIT_SUCCESS; }
int hash_file(state *s, TCHAR *fn) { size_t fn_length; char *sum; TCHAR *my_filename, *msg; FILE *handle; handle = _tfopen(fn,_TEXT("rb")); if (NULL == handle) { print_error_unicode(s,fn,"%s", strerror(errno)); return TRUE; } if ((sum = (char *)malloc(sizeof(char) * FUZZY_MAX_RESULT)) == NULL) { fclose(handle); print_error_unicode(s,fn,"%s", strerror(errno)); return TRUE; } if ((msg = (TCHAR *)malloc(sizeof(TCHAR) * (MAX_STATUS_MSG + 2))) == NULL) { free(sum); fclose(handle); print_error_unicode(s,fn,"%s", strerror(errno)); return TRUE; } if (MODE(mode_verbose)) { fn_length = _tcslen(fn); if (fn_length > MAX_STATUS_MSG) { // We have to make a duplicate of the string to call basename on it // We need the original name for the output later on my_filename = _tcsdup(fn); my_basename(my_filename); } else my_filename = fn; _sntprintf(msg, MAX_STATUS_MSG-1, _TEXT("Hashing: %s%s"), my_filename, _TEXT(BLANK_LINE)); _ftprintf(stderr,_TEXT("%s\r"), msg); if (fn_length > MAX_STATUS_MSG) free(my_filename); } fuzzy_hash_file(handle,sum); prepare_filename(s,fn); display_result(s,fn,sum); if (find_file_size(handle) > SSDEEP_MIN_FILE_SIZE) s->found_meaningful_file = true; s->processed_file = true; fclose(handle); free(sum); free(msg); return FALSE; }