#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) {