From: Michael Tremer Date: Tue, 11 Oct 2022 10:48:20 +0000 (+0000) Subject: digest: Add function to return a digest name X-Git-Tag: 0.9.28~271 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3f42f127ea51e1480e827255c2f81710cd0ab4e9;p=pakfire.git digest: Add function to return a digest name Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/digest.c b/src/libpakfire/digest.c index 458e9b08e..5f4062033 100644 --- a/src/libpakfire/digest.c +++ b/src/libpakfire/digest.c @@ -31,38 +31,47 @@ #include #include -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) { diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index ddfa50f01..3597c9896 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -75,6 +75,7 @@ global: # digest pakfire_digest_get_by_name; + pakfire_digest_name; # dist pakfire_dist;