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 <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-04-06 18:57:05 +02:00
parent c581ed717a
commit f577d5c1a7
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
1 changed files with 45 additions and 57 deletions

View File

@ -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)
{