diff options
author | Werner Koch <[email protected]> | 2015-04-06 16:57:05 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2015-04-06 17:09:50 +0000 |
commit | f577d5c1a747d673fa1d5c012ce3e3b78b699c6a (patch) | |
tree | 5012661b57de15917983c4125991b265244dc179 | |
parent | gpg: Add new option --debug-iolbf. (diff) | |
download | gnupg-f577d5c1a747d673fa1d5c012ce3e3b78b699c6a.tar.gz gnupg-f577d5c1a747d673fa1d5c012ce3e3b78b699c6a.zip |
gpg: Merge duplicated code for get_user_id et al.
* g10/getkey.c (get_user_id_string): Add args mode and r_LEN.
(get_user_id_string_native): Add new args.
(get_long_user_id_string, get_user_id): Rewrite using
get_user_id_string.
Signed-off-by: Werner Koch <[email protected]>
-rw-r--r-- | g10/getkey.c | 102 |
1 files changed, 45 insertions, 57 deletions
diff --git a/g10/getkey.c b/g10/getkey.c index 998f111ac..20b37d80e 100644 --- a/g10/getkey.c +++ b/g10/getkey.c @@ -2754,34 +2754,69 @@ enum_secret_keys (void **context, PKT_public_key *sk) /* Return a string with a printable representation of the user_id. * this string must be freed by xfree. */ static char * -get_user_id_string (u32 * keyid) +get_user_id_string (u32 * keyid, int mode, size_t *r_len) { user_id_db_t r; + keyid_list_t a; int pass = 0; + char *p; + /* Try it two times; second pass reads from key resources. */ do { for (r = user_id_db; r; r = r->next) { - keyid_list_t a; for (a = r->keyids; a; a = a->next) { if (a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1]) { - return xasprintf ("%s %.*s", keystr (keyid), r->len, r->name); + if (mode == 2) + { + /* An empty string as user id is possible. Make + sure that the malloc allocates one byte and + does not bail out. */ + p = xmalloc (r->len? r->len : 1); + memcpy (p, r->name, r->len); + if (r_len) + *r_len = r->len; + } + else + { + if (mode) + p = xasprintf ("%08lX%08lX %.*s", + (ulong) keyid[0], (ulong) keyid[1], + r->len, r->name); + else + p = xasprintf ("%s %.*s", keystr (keyid), + r->len, r->name); + if (r_len) + *r_len = strlen (p); + } + + return p; } } } } while (++pass < 2 && !get_pubkey (NULL, keyid)); - return xasprintf ("%s [?]", keystr (keyid)); + + if (mode == 2) + p = xstrdup (user_id_not_found_utf8 ()); + else if (mode) + p = xasprintf ("%08lX%08lX [?]", (ulong) keyid[0], (ulong) keyid[1]); + else + p = xasprintf ("%s [?]", keystr (keyid)); + + if (r_len) + *r_len = strlen (p); + return p; } char * get_user_id_string_native (u32 * keyid) { - char *p = get_user_id_string (keyid); + char *p = get_user_id_string (keyid, 0, NULL); char *p2 = utf8_to_native (p, strlen (p), 0); xfree (p); return p2; @@ -2791,65 +2826,18 @@ get_user_id_string_native (u32 * keyid) char * get_long_user_id_string (u32 * keyid) { - user_id_db_t r; - keyid_list_t a; - int pass = 0; - /* Try it two times; second pass reads from key resources. */ - do - { - for (r = user_id_db; r; r = r->next) - { - for (a = r->keyids; a; a = a->next) - { - if (a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1]) - { - return xasprintf ("%08lX%08lX %.*s", - (ulong) keyid[0], (ulong) keyid[1], - r->len, r->name); - } - } - } - } - while (++pass < 2 && !get_pubkey (NULL, keyid)); - return xasprintf ("%08lX%08lX [?]", (ulong) keyid[0], (ulong) keyid[1]); + return get_user_id_string (keyid, 1, NULL); } -/* Please try to use get_user_id_native instead of this one. */ +/* Please try to use get_user_byfpr instead of this one. */ char * get_user_id (u32 * keyid, size_t * rn) { - user_id_db_t r; - char *p; - int pass = 0; - - /* Try it two times; second pass reads from key resources. */ - do - { - for (r = user_id_db; r; r = r->next) - { - keyid_list_t a; - for (a = r->keyids; a; a = a->next) - { - if (a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1]) - { - /* An empty string as user id is possible. Make - sure that the malloc allocates one byte and does - not bail out. */ - p = xmalloc (r->len? r->len : 1); - memcpy (p, r->name, r->len); - *rn = r->len; - return p; - } - } - } - } - while (++pass < 2 && !get_pubkey (NULL, keyid)); - p = xstrdup (user_id_not_found_utf8 ()); - *rn = strlen (p); - return p; + return get_user_id_string (keyid, 2, rn); } + /* Please try to use get_user_id_byfpr_native instead of this one. */ char * get_user_id_native (u32 * keyid) @@ -2863,7 +2851,7 @@ get_user_id_native (u32 * keyid) /* Return a user id from the caching by looking it up using the FPR - which mustbe of size MAX_FINGERPRINT_LEN. */ + which must be of size MAX_FINGERPRINT_LEN. */ char * get_user_id_byfpr (const byte *fpr, size_t *rn) { |