From 7e83064eceb014f983ca95cb29d5ec76c439e6a2 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 8 Feb 2025 17:46:13 +0000 Subject: [PATCH] hasher: Add a convenience function to has some data in memory Signed-off-by: Michael Tremer --- src/pakfire/hasher.c | 33 +++++++++++++++++++++++++++++++++ src/pakfire/hasher.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/src/pakfire/hasher.c b/src/pakfire/hasher.c index ff2a2b98..d588f082 100644 --- a/src/pakfire/hasher.c +++ b/src/pakfire/hasher.c @@ -315,6 +315,39 @@ int pakfire_hasher_finalize(struct pakfire_hasher* self, struct pakfire_hashes* return 0; } +/* + * Convenience function to hash a buffer + */ +int pakfire_hash_buffer(struct pakfire_ctx* ctx, const char* buffer, const size_t length, + const enum pakfire_hash_type types, struct pakfire_hashes* hashes) { + struct pakfire_hasher* hasher = NULL; + int r; + + // Create a new hasher + r = pakfire_hasher_create(&hasher, ctx, types); + if (r < 0) + goto ERROR; + + // To make the static analyzer happy + assert(hasher); + + // Feed the buffer into the hash functions + r = pakfire_hasher_update(hasher, buffer, length); + if (r < 0) + goto ERROR; + + // Finalize the hash functions + r = pakfire_hasher_finalize(hasher, hashes); + if (r < 0) + goto ERROR; + +ERROR: + if (hasher) + pakfire_hasher_unref(hasher); + + return r; +} + /* Convenience function to hash a file */ diff --git a/src/pakfire/hasher.h b/src/pakfire/hasher.h index 70c47409..4324a82f 100644 --- a/src/pakfire/hasher.h +++ b/src/pakfire/hasher.h @@ -37,6 +37,8 @@ struct pakfire_hasher* pakfire_hasher_unref(struct pakfire_hasher* self); int pakfire_hasher_update(struct pakfire_hasher* self, const char* buffer, const size_t length); int pakfire_hasher_finalize(struct pakfire_hasher* self, struct pakfire_hashes* computed_hashes); +int pakfire_hash_buffer(struct pakfire_ctx* ctx, const char* buffer, const size_t length, + const enum pakfire_hash_type types, struct pakfire_hashes* hashes); int pakfire_hash_file(struct pakfire_ctx* ctx, FILE* f, enum pakfire_hash_type types, struct pakfire_hashes* hashes); int pakfire_hash_path(struct pakfire_ctx* ctx, -- 2.39.5