From a052c30d31c0f6b532fea081f4a9bee083f5440f Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Tue, 17 Nov 2015 11:47:43 +0100 Subject: [PATCH] gpg: Add function format_keyid. * g10/options.h (opt.keyid_format): Add new value KF_DEFAULT. * g10/keyid.c (format_keyid): New function. (keystr): Use it. -- Signed-off-by: Neal H. Walfield --- g10/keydb.h | 1 + g10/keyid.c | 80 ++++++++++++++++++++++++++++++--------------------- g10/options.h | 2 +- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/g10/keydb.h b/g10/keydb.h index 882af35cf..b1dfa08c7 100644 --- a/g10/keydb.h +++ b/g10/keydb.h @@ -786,6 +786,7 @@ char *pubkey_string (PKT_public_key *pk, char *buffer, size_t bufsize); #define PUBKEY_STRING_SIZE 32 u32 v3_keyid (gcry_mpi_t a, u32 *ki); void hash_public_key( gcry_md_hd_t md, PKT_public_key *pk ); +const char *format_keyid (u32 *keyid, int format, char *buffer, int len); size_t keystrlen(void); const char *keystr(u32 *keyid); const char *keystr_with_sub (u32 *main_kid, u32 *sub_kid); diff --git a/g10/keyid.c b/g10/keyid.c index d71698594..cb237ef54 100644 --- a/g10/keyid.c +++ b/g10/keyid.c @@ -274,6 +274,53 @@ v3_keyid (gcry_mpi_t a, u32 *ki) } +const char * +format_keyid (u32 *keyid, int format, char *buffer, int len) +{ + char tmp[KEYID_STR_SIZE]; + if (! buffer) + buffer = tmp; + + if (format == KF_DEFAULT) + format = opt.keyid_format; + if (format == KF_DEFAULT) + format = KF_0xLONG; + + switch (format) + { + case KF_SHORT: + snprintf (buffer, len, "%08lX", (ulong)keyid[1]); + break; + + case KF_LONG: + if (keyid[0]) + snprintf (buffer, len, "%08lX%08lX", + (ulong)keyid[0], (ulong)keyid[1]); + else + snprintf (buffer, len, "%08lX", (ulong)keyid[1]); + break; + + case KF_0xSHORT: + snprintf (buffer, len, "0x%08lX", (ulong)keyid[1]); + break; + + case KF_0xLONG: + if(keyid[0]) + snprintf (buffer, len, "0x%08lX%08lX", + (ulong)keyid[0],(ulong)keyid[1]); + else + snprintf (buffer, len, "0x%08lX", (ulong)keyid[1]); + break; + + default: + BUG(); + } + + if (buffer == tmp) + return xstrdup (buffer); + return buffer; +} + size_t keystrlen(void) { @@ -301,38 +348,7 @@ const char * keystr (u32 *keyid) { static char keyid_str[KEYID_STR_SIZE]; - - switch (opt.keyid_format) - { - case KF_SHORT: - snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]); - break; - - case KF_LONG: - if (keyid[0]) - snprintf (keyid_str, sizeof keyid_str, "%08lX%08lX", - (ulong)keyid[0], (ulong)keyid[1]); - else - snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]); - break; - - case KF_0xSHORT: - snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]); - break; - - case KF_0xLONG: - if(keyid[0]) - snprintf (keyid_str, sizeof keyid_str, "0x%08lX%08lX", - (ulong)keyid[0],(ulong)keyid[1]); - else - snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]); - break; - - default: - BUG(); - } - - return keyid_str; + return format_keyid (keyid, opt.keyid_format, keyid_str, sizeof (keyid_str)); } diff --git a/g10/options.h b/g10/options.h index 6fca714dd..0bb2aaee6 100644 --- a/g10/options.h +++ b/g10/options.h @@ -136,7 +136,7 @@ struct } compliance; enum { - KF_SHORT, KF_LONG, KF_0xSHORT, KF_0xLONG + KF_DEFAULT, KF_SHORT, KF_LONG, KF_0xSHORT, KF_0xLONG } keyid_format; int shm_coprocess; const char *set_filename;