[Pkg-gnupg-commit] [gnupg2] 136/241: gpg: Add function format_keyid.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Dec 9 20:32:07 UTC 2015


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gnupg2.

commit a052c30d31c0f6b532fea081f4a9bee083f5440f
Author: Neal H. Walfield <neal at g10code.com>
Date:   Tue Nov 17 11:47:43 2015 +0100

    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 <neal at g10code.com>
---
 g10/keydb.h   |  1 +
 g10/keyid.c   | 76 ++++++++++++++++++++++++++++++++++++-----------------------
 g10/options.h |  2 +-
 3 files changed, 48 insertions(+), 31 deletions(-)

diff --git a/g10/keydb.h b/g10/keydb.h
index 882af35..b1dfa08 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 d716985..cb237ef 100644
--- a/g10/keyid.c
+++ b/g10/keyid.c
@@ -274,65 +274,81 @@ v3_keyid (gcry_mpi_t a, u32 *ki)
 }
 
 
-size_t
-keystrlen(void)
+const char *
+format_keyid (u32 *keyid, int format, char *buffer, int len)
 {
-  switch(opt.keyid_format)
+  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:
-      return 8;
+      snprintf (buffer, len, "%08lX", (ulong)keyid[1]);
+      break;
 
     case KF_LONG:
-      return 16;
+      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:
-      return 10;
+      snprintf (buffer, len, "0x%08lX", (ulong)keyid[1]);
+      break;
 
     case KF_0xLONG:
-      return 18;
+      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;
+}
 
-const char *
-keystr (u32 *keyid)
+size_t
+keystrlen(void)
 {
-  static char keyid_str[KEYID_STR_SIZE];
-
-  switch (opt.keyid_format)
+  switch(opt.keyid_format)
     {
     case KF_SHORT:
-      snprintf (keyid_str, sizeof keyid_str, "%08lX", (ulong)keyid[1]);
-      break;
+      return 8;
 
     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;
+      return 16;
 
     case KF_0xSHORT:
-      snprintf (keyid_str, sizeof keyid_str, "0x%08lX", (ulong)keyid[1]);
-      break;
+      return 10;
 
     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;
+      return 18;
 
     default:
       BUG();
     }
+}
+
 
-  return keyid_str;
+const char *
+keystr (u32 *keyid)
+{
+  static char keyid_str[KEYID_STR_SIZE];
+  return format_keyid (keyid, opt.keyid_format, keyid_str, sizeof (keyid_str));
 }
 
 
diff --git a/g10/options.h b/g10/options.h
index 6fca714..0bb2aae 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;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list