]> git.ipfire.org Git - pakfire.git/commitdiff
digest: Add function to return a digest name
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 11 Oct 2022 10:48:20 +0000 (10:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 11 Oct 2022 11:19:34 +0000 (11:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/digest.c
src/libpakfire/libpakfire.sym

index 458e9b08e760e54660f0dd2672c2f78f0f2e105c..5f406203315c2fd6009471796db8d4ead2bbd07f 100644 (file)
 #include <pakfire/private.h>
 #include <pakfire/util.h>
 
-PAKFIRE_EXPORT int pakfire_digest_get_by_name(const char* name) {
-       static const struct _pakfire_digest_name {
-               const char* name;
-               int type;
-       } names[] = {
-               // SHA-2
-               { "sha2-512", PAKFIRE_DIGEST_SHA2_512, },
-               { "sha2-256", PAKFIRE_DIGEST_SHA2_256, },
-
-               // BLAKE2
-               { "blake2b512", PAKFIRE_DIGEST_BLAKE2B512, },
-               { "blake2s256", PAKFIRE_DIGEST_BLAKE2S256, },
-
-               // SHA-3
-               { "sha3-512", PAKFIRE_DIGEST_SHA3_512, },
-               { "sha3-256", PAKFIRE_DIGEST_SHA3_256, },
+static const struct _pakfire_digest_name {
+       const char* name;
+       const enum pakfire_digest_types type;
+} PAKFIRE_DIGEST_NAMES[] = {
+       // SHA-2
+       { "sha2-512", PAKFIRE_DIGEST_SHA2_512, },
+       { "sha2-256", PAKFIRE_DIGEST_SHA2_256, },
+
+       // BLAKE2
+       { "blake2b512", PAKFIRE_DIGEST_BLAKE2B512, },
+       { "blake2s256", PAKFIRE_DIGEST_BLAKE2S256, },
+
+       // SHA-3
+       { "sha3-512", PAKFIRE_DIGEST_SHA3_512, },
+       { "sha3-256", PAKFIRE_DIGEST_SHA3_256, },
+
+       { NULL, PAKFIRE_DIGEST_UNDEFINED, },
+};
+
+PAKFIRE_EXPORT const char* pakfire_digest_name(const enum pakfire_digest_types type) {
+       for (const struct _pakfire_digest_name* n = PAKFIRE_DIGEST_NAMES; n->name; n++) {
+               if (n->type == type)
+                       return n->name;
+       }
 
-               { NULL, 0, },
-       };
+       return NULL;
+}
 
+PAKFIRE_EXPORT int pakfire_digest_get_by_name(const char* name) {
        // Check that name is not NULL
        if (!name) {
                errno = EINVAL;
-               return 0;
+               return PAKFIRE_DIGEST_UNDEFINED;
        }
 
-       for (const struct _pakfire_digest_name* n = names; n->name; n++) {
+       for (const struct _pakfire_digest_name* n = PAKFIRE_DIGEST_NAMES; n->name; n++) {
                if (strcmp(n->name, name) == 0)
                        return n->type;
        }
 
-       return 0;
+       return PAKFIRE_DIGEST_UNDEFINED;
 }
 
 size_t pakfire_digest_length(const enum pakfire_digest_types digest) {
index ddfa50f015d69ff013f902af1c26ed30eb8e039b..3597c989613fc09173606c7a0f7bb4d4ec642661 100644 (file)
@@ -75,6 +75,7 @@ global:
 
        # digest
        pakfire_digest_get_by_name;
+       pakfire_digest_name;
 
        # dist
        pakfire_dist;