Fix hash and hash_str
* file_t.hash is now a buffer and not a pointer * add exception checking to file_t_md5_gen
This commit is contained in:
parent
a6227b8481
commit
ea37ce1732
25
src/file_t.c
25
src/file_t.c
@ -1,7 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <openssl/md5.h> /* md5sum */
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <errno.h>
|
||||
@ -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;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef FILE_T_H
|
||||
#define FILE_T_H
|
||||
|
||||
#include <openssl/md5.h> /* 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;
|
||||
|
||||
|
Reference in New Issue
Block a user