From be53633c4f20474549b3beaf494bcc3b6bebfca0 Mon Sep 17 00:00:00 2001 From: womax Date: Tue, 28 May 2024 20:14:08 +0200 Subject: [PATCH] Optimisation in hash_object --- src/fs.c | 2 +- src/fs.h | 2 ++ src/objects.c | 21 ++++++--------------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/fs.c b/src/fs.c index e525ecf..8f55f81 100644 --- a/src/fs.c +++ b/src/fs.c @@ -9,8 +9,8 @@ #include #include -#include "includes.h" #include "fs.h" +#include "includes.h" #include "objects.h" int local_repo_exist() diff --git a/src/fs.h b/src/fs.h index 5bd136f..249e271 100644 --- a/src/fs.h +++ b/src/fs.h @@ -1,6 +1,8 @@ #ifndef FS_H #define FS_H 1 +#include "objects.h" + #define LOCAL_REPO ".cgit" #define DEFAULT_DIR_MODE 0755 #define DEFAULT_FILE_MODE 0444 diff --git a/src/objects.c b/src/objects.c index 42f8e1a..3467525 100644 --- a/src/objects.c +++ b/src/objects.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -54,27 +54,18 @@ int full_object(struct object *obj, char *buffer, size_t buffer_size) void hash_object(struct object *obj, char* result) { unsigned char md_buffer[DIGEST_LENGTH] = {0}; - EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + size_t data_size = object_size(obj); + char* data = malloc(data_size); + full_object(obj, data, data_size); - char* hr_size = malloc(decimal_len(obj->size) + 1); - sprintf(hr_size, "%li", obj->size); - - EVP_DigestInit(ctx, EVP_sha1()); - EVP_DigestUpdate(ctx, obj->object_type, strlen(obj->object_type)); - EVP_DigestUpdate(ctx, " ", 1); - EVP_DigestUpdate(ctx, hr_size, decimal_len(obj->size)); - EVP_DigestUpdate(ctx, "\0", 1); - EVP_DigestUpdate(ctx, obj->content, obj->size); - - EVP_DigestFinal(ctx, md_buffer, NULL); - free(hr_size); + SHA1(data, data_size, md_buffer); for (int i = 0; i < DIGEST_LENGTH; i++) { sprintf((result + 2 * i), "%.2x", md_buffer[i]); } - EVP_MD_CTX_free(ctx); + free(data); } int uncompress_object(struct object *obj, char* compressed, uLongf comp_size)