]> granicus.if.org Git - p11-kit/commitdiff
common: Add support for multiple field names (ie: nicks) per constant
authorStef Walter <stefw@redhat.com>
Thu, 4 Sep 2014 13:56:02 +0000 (15:56 +0200)
committerStef Walter <stef@thewalter.net>
Wed, 10 Sep 2014 06:03:09 +0000 (08:03 +0200)
This allows us to have old/new names for a given constant.

https://bugs.freedesktop.org/show_bug.cgi?id=83495

common/constants.c
common/constants.h
common/test-constants.c

index ca956d33dafb9c8e4a0ba3bcf7e311c2a5133b77..a2427c91f7178a5b1b3db6a5967cd498c615e5d3 100644 (file)
@@ -50,7 +50,8 @@
  * test to verify everything is in order.
  */
 
-#define CT(x, n) { x, #x, n },
+#define CT(x, n) { x, #x, { n } },
+#define CT2(x, n, n2) { x, #x, { n, n2 } },
 
 const p11_constant p11_constant_types[] = {
        CT (CKA_CLASS, "class")
@@ -254,10 +255,10 @@ const p11_constant p11_constant_asserts[] = {
 };
 
 const p11_constant p11_constant_categories[] = {
-       { 0, "unspecified", "unspecified" },
-       { 1, "token-user", "token-user" },
-       { 2, "authority", "authority" },
-       { 3, "other-entry", "other-entry" },
+       { 0, "unspecified", { "unspecified" } },
+       { 1, "token-user",  { "token-user" } },
+       { 2, "authority",  { "authority" } },
+       { 3, "other-entry",  { "other-entry" } },
        { CKA_INVALID },
 };
 
@@ -629,7 +630,7 @@ static const p11_constant *
 lookup_info (const p11_constant *table,
              CK_ATTRIBUTE_TYPE type)
 {
-       p11_constant match = { type, NULL, NULL };
+       p11_constant match = { type, NULL, { NULL } };
        int length = -1;
        int i;
 
@@ -657,7 +658,7 @@ p11_constant_nick (const p11_constant *constants,
                    CK_ULONG type)
 {
        const p11_constant *constant = lookup_info (constants, type);
-       return constant ? constant->nick : NULL;
+       return constant ? constant->nicks[0] : NULL;
 }
 
 p11_dict *
@@ -665,9 +666,8 @@ p11_constant_reverse (bool nick)
 {
        const p11_constant *table;
        p11_dict *lookups;
-       void *string;
        int length = -1;
-       int i, j;
+       int i, j, k;
 
        lookups = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL);
        return_val_if_fail (lookups != NULL, NULL);
@@ -678,14 +678,15 @@ p11_constant_reverse (bool nick)
 
                for (j = 0; j < length; j++) {
                        if (nick) {
-                               if (!table[j].nick)
-                                       continue;
-                               string = (void *)table[j].nick;
+                               for (k = 0; table[j].nicks[k] != NULL; k++) {
+                                       if (!p11_dict_set (lookups, (void *)table[j].nicks[k],
+                                                          (void *)&table[j].value))
+                                               return_val_if_reached (NULL);
+                               }
                        } else {
-                               string = (void *)table[j].name;
+                               if (!p11_dict_set (lookups, (void *)table[j].name, (void *)&table[j].value))
+                                       return_val_if_reached (NULL);
                        }
-                       if (!p11_dict_set (lookups, string, (void *)&table[j].value))
-                               return_val_if_reached (NULL);
                }
        }
 
index 5b0f3a54c82740b940d0d02f413f4b09f8d346bd..15263734882d537ca45401a9eb8378d609bbfe7b 100644 (file)
@@ -43,7 +43,7 @@
 typedef struct {
        CK_ULONG value;
        const char *name;
-       const char *nick;
+       const char *nicks[4];
 } p11_constant;
 
 const char *        p11_constant_name      (const p11_constant *constants,
index 9adc81a825d06289f900f802e445f5b1058084eb..577d61101c90951d5c5fbbd7c49191b72473efbd 100644 (file)
@@ -49,7 +49,7 @@ test_constants (void *arg)
        const p11_constant *constant = arg;
        p11_dict *nicks, *names;
        CK_ULONG check;
-       int i;
+       int i, j;
 
        nicks = p11_constant_reverse (true);
        names = p11_constant_reverse (false);
@@ -61,16 +61,16 @@ test_constants (void *arg)
        for (i = 0; constant[i].value != CKA_INVALID; i++) {
                assert_ptr_not_null (constant[i].name);
 
-               if (constant[i].nick) {
-                       assert_str_eq (constant[i].nick,
+               if (constant[i].nicks[0]) {
+                       assert_str_eq (constant[i].nicks[0],
                                       p11_constant_nick (constant, constant[i].value));
                }
 
                assert_str_eq (constant[i].name,
                               p11_constant_name (constant, constant[i].value));
 
-               if (constant[i].nick) {
-                       check = p11_constant_resolve (nicks, constant[i].nick);
+               for (j = 0; constant[i].nicks[j] != NULL; j++) {
+                       check = p11_constant_resolve (nicks, constant[i].nicks[j]);
                        assert_num_eq (constant[i].value, check);
                }