]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
digests: Add function to return the type from string
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Oct 2022 13:07:15 +0000 (13:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Oct 2022 13:07:15 +0000 (13:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/digest.c
src/libpakfire/include/pakfire/digest.h
tests/libpakfire/digest.c

index b7364600758135306e600c5ce4fdfdd1a669d641..3ea348491c3b1259327310a33dba5a0f8593d49f 100644 (file)
 #include <pakfire/private.h>
 #include <pakfire/util.h>
 
+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, },
+
+               { NULL, 0, },
+       };
+
+       // Check that name is not NULL
+       if (!name) {
+               errno = EINVAL;
+               return 0;
+       }
+
+       for (const struct _pakfire_digest_name* n = names; n->name; n++) {
+               if (strcmp(n->name, name) == 0)
+                       return n->type;
+       }
+
+       return 0;
+}
+
 size_t pakfire_digest_length(const enum pakfire_digest_types digest) {
        switch (digest) {
                case PAKFIRE_DIGEST_SHA3_512:
index c0ac04e54fb536df0cd1a2973188b7f2570232fd..db34f96e220191772ea32c12c5558d37472e5fae 100644 (file)
@@ -85,6 +85,8 @@ struct pakfire_digests {
                .sha2_256 = 0, \
        }
 
+int pakfire_digest_get_by_name(const char* name);
+
 size_t pakfire_digest_length(const enum pakfire_digest_types digest);
 
 #define pakfire_digest_set(digest) __pakfire_digest_set(digest, sizeof(digest))
index 12b7c17592d9bbb8ffba752f717466b103d32b7c..eab91c9ecaa4020fa8caf6a6ed004a18139e7c5f 100644 (file)
@@ -85,9 +85,29 @@ FAIL:
        return r;
 }
 
+static int test_get_by_name(const struct test* t) {
+       // Positive cases
+       ASSERT(pakfire_digest_get_by_name("sha2-512") == PAKFIRE_DIGEST_SHA2_512);
+       ASSERT(pakfire_digest_get_by_name("sha2-256") == PAKFIRE_DIGEST_SHA2_256);
+       ASSERT(pakfire_digest_get_by_name("blake2b512") == PAKFIRE_DIGEST_BLAKE2B512);
+       ASSERT(pakfire_digest_get_by_name("blake2s256") == PAKFIRE_DIGEST_BLAKE2S256);
+       ASSERT(pakfire_digest_get_by_name("sha3-512") == PAKFIRE_DIGEST_SHA3_512);
+       ASSERT(pakfire_digest_get_by_name("sha3-256") == PAKFIRE_DIGEST_SHA3_256);
+
+       // Negative cases
+       ASSERT(pakfire_digest_get_by_name("XXX") == 0);
+       ASSERT(pakfire_digest_get_by_name(NULL) == 0);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_init);
        testsuite_add_test(test_random);
+       testsuite_add_test(test_get_by_name);
 
        return testsuite_run(argc, argv);
 }