[PATCH 3/3] Simplify lookup of key purpose names.
Diego Elio Pettenò
flameeyes at gmail.com
Fri Oct 1 16:48:01 CEST 2010
Again, use a completely static, read-only table (non-relocated) for the
translation of oid to name.
At the same time, instead of replicating the same loop in three places,
simplify it with a static looping function, and use that in two of the
three places.
The third place only drops the loop because its result weren't used at all;
it might have to be changed to fix that issue as well.
---
sm/keylist.c | 37 +++++++++++++++++++------------------
1 files changed, 19 insertions(+), 18 deletions(-)
diff --git a/sm/keylist.c b/sm/keylist.c
index fe2fb9f..cb54a92 100644
--- a/sm/keylist.c
+++ b/sm/keylist.c
@@ -50,10 +50,10 @@ struct list_external_parm_s
/* This table is to map Extended Key Usage OIDs to human readable
names. */
-struct
+static const struct
{
- const char *oid;
- const char *name;
+ char oid[32];
+ char name[32];
} key_purpose_map[] = {
{ "1.3.6.1.5.5.7.3.1", "serverAuth" },
{ "1.3.6.1.5.5.7.3.2", "clientAuth" },
@@ -72,11 +72,21 @@ struct
{ "2.16.840.1.113730.4.1", "serverGatedCrypto.ns" }, /* Netscape. */
{ "1.3.6.1.4.1.311.10.3.3", "serverGatedCrypto.ms"}, /* Microsoft. */
- { "1.3.6.1.5.5.7.48.1.5", "ocspNoCheck" },
-
- { NULL, NULL }
+ { "1.3.6.1.5.5.7.48.1.5", "ocspNoCheck" }
};
+static const char *
+find_key_name(const char *oid)
+{
+ int i;
+
+ for (i=0; i<DIM(key_purpose_map); i++)
+ if ( !strcmp (key_purpose_map[i].oid, oid) )
+ return key_purpose_map[i].name;
+
+ return oid;
+}
+
/* Do not print this extension in the list of extensions. This is set
for oids which are already available via ksba fucntions. */
@@ -832,10 +842,7 @@ list_cert_raw (ctrl_t ctrl, KEYDB_HANDLE hd,
while (p && (pend=strchr (p, ':')))
{
*pend++ = 0;
- for (i=0; key_purpose_map[i].oid; i++)
- if ( !strcmp (key_purpose_map[i].oid, p) )
- break;
- es_fputs (key_purpose_map[i].oid?key_purpose_map[i].name:p, fp);
+ es_fputs (find_key_name(p), fp);
p = pend;
if (*p != 'C')
es_fputs (" (suggested)", fp);
@@ -865,9 +872,6 @@ list_cert_raw (ctrl_t ctrl, KEYDB_HANDLE hd,
while (p && (pend=strchr (p, ':')))
{
*pend++ = 0;
- for (i=0; key_purpose_map[i].oid; i++)
- if ( !strcmp (key_purpose_map[i].oid, p) )
- break;
es_fputs (p, fp);
p = pend;
if (*p == 'C')
@@ -1031,7 +1035,7 @@ list_cert_std (ctrl_t ctrl, ksba_cert_t cert, estream_t fp, int have_secret,
ksba_sexp_t sexp;
char *dn;
ksba_isotime_t t;
- int idx, i;
+ int idx;
int is_ca, chainlen;
unsigned int kusage;
char *string, *p, *pend;
@@ -1136,10 +1140,7 @@ list_cert_std (ctrl_t ctrl, ksba_cert_t cert, estream_t fp, int have_secret,
while (p && (pend=strchr (p, ':')))
{
*pend++ = 0;
- for (i=0; key_purpose_map[i].oid; i++)
- if ( !strcmp (key_purpose_map[i].oid, p) )
- break;
- es_fputs (key_purpose_map[i].oid?key_purpose_map[i].name:p, fp);
+ es_fputs (find_key_name(p), fp);
p = pend;
if (*p != 'C')
es_fputs (" (suggested)", fp);
--
1.7.3.1
More information about the Gnupg-devel
mailing list