1、调整SFH的函数名和源文件名,原有使用sfh的用户会受到影响;2、digest_gen由目录遍历,改为单文件并在屏幕输出结果,便于通过命令行调用。

This commit is contained in:
zhengchao
2017-07-08 19:23:17 +08:00
parent 5ba84a69f1
commit 6ffc3e3ded
7 changed files with 78 additions and 115 deletions

View File

@@ -1,12 +1,12 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>
#include<time.h>
#include<math.h>
#include "mesa_fuzzy.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <math.h>
#include "stream_fuzzy_hash.h"
void* entropy_start(void)
{
@@ -40,96 +40,60 @@ double entropy_stop(void* handle)
free(handle);
return (-sum);
}
void dir_digest(int argc, char * argv[])
void hash_file(const char* path)
{
if(argc != 2)
{
printf("uasge: ./digest_gen [Dir]\n");
exit(-1);
}
DIR * dir;
struct dirent * file;
char * dir_path = argv[1];
char read_buff[1024*4];
unsigned long long read_size=0,feed_offset=0;
dir = opendir(dir_path);
chdir(dir_path);
int ret =0;
unsigned int file_id = 1;
unsigned long hash_length=0,file_effective_length=0;
FILE * result_fp = NULL,*fp=NULL;
struct stat digest_fstat;
char * digest_result_buff=NULL;
const char* result_file="./digest_result.txt";
result_fp = fopen(result_file,"a");
char read_buff[1024*4];
void * entropy_handle=NULL;
double file_entropy=0.0;
if(NULL == result_fp)
int hash_length;
char * digest_result_buff=NULL;
struct stat digest_fstat;
FILE* fp;
stat(path,&digest_fstat);
fp = fopen(path, "r");
if(NULL == fp)
{
printf("open file failed!");
exit(-1);
printf("Open %s failed\n", path);
return;
}
while((file = readdir(dir)) != NULL)
read_size=0;
feed_offset=0;
sfh_instance_t * fhandle = SFH_instance(0);
entropy_handle=entropy_start();
while(0==feof(fp))
{
if(!strcmp(file->d_name, ".") ||!strcmp(file->d_name, ".."))
{
continue;
}
ret=stat(file->d_name,&digest_fstat);
if(ret!=0)
{
printf("fstat %s error.\n",file->d_name);
continue;
}
off_t file_size = digest_fstat.st_size;
fp = fopen(file->d_name, "r");
if(NULL == fp)
{
printf("Can't open file %s\n", file->d_name);
continue;
}
read_size=0;
feed_offset=0;
fuzzy_handle_t * fhandle = fuzzy_create_handle((unsigned long long)file_size);
entropy_handle=entropy_start();
while(0==feof(fp))
{
read_size=fread(read_buff,1,sizeof(read_buff),fp);
fuzzy_feed(fhandle,read_buff,read_size,feed_offset);
feed_offset+=read_size;
entropy_feed(entropy_handle,(const unsigned char*) read_buff, read_size);
}
file_entropy=entropy_stop(entropy_handle);
hash_length = fuzzy_status(fhandle, HASH_LENGTH);
file_effective_length = fuzzy_status(fhandle, EFFECTIVE_LENGTH);
digest_result_buff= (char *)malloc(sizeof(char) * (hash_length));
if(fuzzy_digest(fhandle, digest_result_buff, hash_length) < 0)
{
printf("error\n");
continue;
}
fprintf(result_fp, "%u\t%s\t%llu\t%lu\t%lf\n", file_id, file->d_name,file_size, hash_length,file_entropy);
fprintf(result_fp, "%s\n", digest_result_buff);
printf("%u %s\n", file_id,file->d_name);
file_id++;
fuzzy_destroy_handle(fhandle);
fclose(fp);
free(digest_result_buff);
read_size=fread(read_buff,1,sizeof(read_buff),fp);
SFH_feed(fhandle,read_buff,read_size,feed_offset);
feed_offset+=read_size;
entropy_feed(entropy_handle,(const unsigned char*) read_buff, read_size);
}
fclose(result_fp);
closedir(dir);
printf("write result to %s\n", result_file);
file_entropy=entropy_stop(entropy_handle);
hash_length = SFH_status(fhandle, HASH_LENGTH);
digest_result_buff= (char *)malloc(sizeof(char) * (hash_length));
SFH_digest(fhandle, digest_result_buff, hash_length);
printf("%s %u %lf %s\n",path,digest_fstat.st_size,file_entropy,digest_result_buff);
SFH_release(fhandle);
free(digest_result_buff);
fclose(fp);
}
int main(int argc, char * argv[])
{
dir_digest(argc, argv);
//overlap_test(argc, argv);
char path[256];
if(argc == 2)
{
hash_file(argv[1]);
}
else if(NULL!=fgets(path,sizeof(path),stdin))
{
hash_file(path);
}
else
{
printf("SFH uasge: ./digest_gen [Dir]\n");
exit(-1);
}
return 0;
}