]> git.ipfire.org Git - pakfire.git/commitdiff
hasher: Add a convenience function to has some data in memory
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 17:46:13 +0000 (17:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 17:46:13 +0000 (17:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/hasher.c
src/pakfire/hasher.h

index ff2a2b9856ecdab8c9eaf0bf6e7ab3d80c198490..d588f082faa77ab13cbe1baf189bc8ed0d8a2d53 100644 (file)
@@ -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
 */
index 70c47409214728b894272a9ce86dcc38efd5c137..4324a82fc02ccc9fc1cc278fcdb0ddc49ea2548b 100644 (file)
@@ -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,