diff --git a/common/name-value.c b/common/name-value.c index 89fd060c8..989a5b111 100644 --- a/common/name-value.c +++ b/common/name-value.c @@ -514,6 +514,21 @@ nvc_delete (nvc_t pk, nve_t entry) nve_release (entry, pk->private_key_mode); } + +/* Delete the entries with NAME from PK. */ +void +nvc_delete_named (nvc_t pk, const char *name) +{ + nve_t e; + + if (!valid_name (name)) + return; + + while ((e = nvc_lookup (pk, name))) + nvc_delete (pk, e); +} + + /* Lookup and iteration. */ @@ -563,6 +578,25 @@ nve_next_value (nve_t entry, const char *name) return NULL; } + +/* Return the string for the first entry in NVC with NAME. If an + * entry with NAME is missing in NVC or its value is the empty string + * NULL is returned. Note that the The returned string is a pointer + * into NVC. */ +const char * +nvc_get_string (nvc_t nvc, const char *name) +{ + nve_t item; + + if (!nvc) + return NULL; + item = nvc_lookup (nvc, name); + if (!item) + return NULL; + return nve_value (item); +} + + /* Private key handling. */ diff --git a/common/name-value.h b/common/name-value.h index 5c24b8db1..a6283a649 100644 --- a/common/name-value.h +++ b/common/name-value.h @@ -72,6 +72,9 @@ nve_t nve_next (nve_t entry); /* Get the next entry with the given name. */ nve_t nve_next_value (nve_t entry, const char *name); +/* Return the string for the first entry in NVC with NAME or NULL. */ +const char *nvc_get_string (nvc_t nvc, const char *name); + /* Adding and modifying values. */ @@ -88,6 +91,9 @@ gpg_error_t nvc_set (nvc_t pk, const char *name, const char *value); /* Delete the given entry from PK. */ void nvc_delete (nvc_t pk, nve_t pke); +/* Delete the entries with NAME from PK. */ +void nvc_delete_named (nvc_t pk, const char *name); + /* Private key handling. */ diff --git a/common/t-name-value.c b/common/t-name-value.c index 57f685ffb..13a383ddb 100644 --- a/common/t-name-value.c +++ b/common/t-name-value.c @@ -292,6 +292,7 @@ run_modification_tests (void) { gpg_error_t err; nvc_t pk; + nve_t e; gcry_sexp_t key; char *buf; @@ -344,6 +345,30 @@ run_modification_tests (void) assert (strcmp (buf, "") == 0); xfree (buf); + /* Test whether we can delete an entry by name. */ + err = nvc_add (pk, "Key:", "(3:foo)"); + assert (!err); + e = nvc_lookup (pk, "Key:"); + assert (e); + nvc_delete_named (pk, "Kez:"); /* Delete an inexistant name. */ + e = nvc_lookup (pk, "Key:"); + assert (e); + nvc_delete_named (pk, "Key:"); + e = nvc_lookup (pk, "Key:"); + assert (!e); + + /* Ditto but now whether it deletes all entries with that name. We + * don't use "Key" because that name is special in private key mode. */ + err = nvc_add (pk, "AKey:", "A-value"); + assert (!err); + err = nvc_add (pk, "AKey:", "B-value"); + assert (!err); + e = nvc_lookup (pk, "AKey:"); + assert (e); + nvc_delete_named (pk, "AKey:"); + e = nvc_lookup (pk, "AKey:"); + assert (!e); + nvc_set (pk, "Foo:", "A really long value spanning across multiple lines" " that has to be wrapped at a convenient space."); buf = nvc_to_string (pk);