diff --git a/src/file_t.c b/src/file_t.c index 25266d9..3260344 100644 --- a/src/file_t.c +++ b/src/file_t.c @@ -1,7 +1,6 @@ #include #include #include -#include /* md5sum */ #include #include #include @@ -46,9 +45,8 @@ file_init(const char *filename, filepath_t FILEPATH, exception_t *e) } file_t *f; - f = malloc(sizeof(file_t*)); + f = malloc(sizeof(file_t)); f->name = (char*) path; - f->hash = NULL; f->hash_str = NULL; return f; } @@ -76,11 +74,14 @@ file_md5_gen(file_t *f, exception_t *e) file_buffer = (unsigned char*) mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0); /* Computes the MD5 checksum to result */ - MD5(file_buffer, file_size, f->hash); - + if (MD5(file_buffer, file_size, f->hash) == NULL) { + e->type = MD5SUM_GEN_FAIL; + e->msg = "No hash!"; + } + /* Removes fime_buffer and file_size */ munmap(file_buffer, file_size); - + return f->hash; } @@ -88,14 +89,14 @@ char* file_md5_str(file_t *f, exception_t *e) { static const char digits[] = "0123456789abcdef"; - f->hash_str = malloc (MD5_DIGEST_LENGTH+1); - - for (size_t i = 0; i < MD5_DIGEST_LENGTH; i+=2) + f->hash_str = malloc (2*MD5_DIGEST_LENGTH+1); + size_t si = 0; + for (size_t i = si; i < MD5_DIGEST_LENGTH; i++) { - f->hash_str[i] += digits[f->hash[i] / MD5_DIGEST_LENGTH]; - f->hash_str[i+1] += digits[f->hash[i+1] % MD5_DIGEST_LENGTH]; + f->hash_str[si++] = digits[f->hash[i] / MD5_DIGEST_LENGTH]; + f->hash_str[si++] = digits[f->hash[i] % MD5_DIGEST_LENGTH]; } - f->hash_str[MD5_DIGEST_LENGTH] = '\0'; + f->hash_str[2*MD5_DIGEST_LENGTH] = '\0'; return f->hash_str; } diff --git a/src/file_t.h b/src/file_t.h index 709ffae..e618e27 100644 --- a/src/file_t.h +++ b/src/file_t.h @@ -1,6 +1,8 @@ #ifndef FILE_T_H #define FILE_T_H +#include /* md5sum */ + #include "exception_t.h" typedef enum filepath_t { @@ -13,7 +15,7 @@ typedef enum filepath_t { */ typedef struct file_t { char *name; /**< Absolute path to file */ - unsigned char *hash; /**< MD5sum hash */ + unsigned char hash[MD5_DIGEST_LENGTH]; /**< MD5sum hash */ char *hash_str; /**< MD5sum hash as char* */ } file_t;