diff options
author | Werner Koch <[email protected]> | 2022-03-18 12:47:10 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2022-03-18 13:14:39 +0000 |
commit | 449d2fbcde630974628285d3405feb6ab2a812f9 (patch) | |
tree | e2eef0f8d1ff5e68d3bacb5a8193829cb31333b1 /common/mapstrings.c | |
parent | gpg: Allow decryption of symencr even for non-compliant cipher. (diff) | |
download | gnupg-449d2fbcde630974628285d3405feb6ab2a812f9.tar.gz gnupg-449d2fbcde630974628285d3405feb6ab2a812f9.zip |
common: New function map_static_strings
* common/mapstrings.c (struct intmapping_s): New.
(map_static_strings): New.
* common/stringhelp.c (do_strconcat): Rename to ...
(vstrconcat): this and make global.
* common/t-mapstrings.c (test_map_static_strings): New test.
Diffstat (limited to 'common/mapstrings.c')
-rw-r--r-- | common/mapstrings.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/common/mapstrings.c b/common/mapstrings.c index 318ca5bd1..172e198ea 100644 --- a/common/mapstrings.c +++ b/common/mapstrings.c @@ -69,6 +69,18 @@ struct mapping_s static struct mapping_s *mappings; +/* Similar to above but using two integers and a domain as key. */ +struct intmapping_s +{ + struct intmapping_s *next; + int key1; + int key2; + const char *string; + char domain[1]; +}; +static struct intmapping_s *intmappings; + + /* If STRING has already been mapped, return the mapped string. If not return NULL. */ static const char * @@ -166,3 +178,40 @@ map_static_macro_string (const char *string) return store_mapping (string, p); } + + +/* If a list of strings has already been mapped to a the tuple + * (DOMAIN,KEY1,KEY2) return that string. If not, create a mapping + * made up of the concatenation of the given strings. */ +const char * +map_static_strings (const char *domain, int key1, int key2, + const char *string1, ...) +{ + va_list arg_ptr; + struct intmapping_s *m; + + if (!string1 || !domain) + return ""; + + for (m = intmappings; m; m = m->next) + if (m->key1 == key1 && m->key2 == key2 && !strcmp (domain, m->domain)) + return m->string; + + m = xmalloc (sizeof *m + strlen (domain)); + strcpy (m->domain, domain); + m->key1 = key1; + m->key2 = key2; + + va_start (arg_ptr, string1); + m->string = vstrconcat (string1, arg_ptr); + va_end (arg_ptr); + if (!m->string) + log_fatal ("map_static_strings failed: %s\n", strerror (errno)); + + gpgrt_annotate_leaked_object (m->string); + gpgrt_annotate_leaked_object (m); + + m->next = intmappings; + intmappings = m; + return m->string; +} |