[PATCH 2/3] Change some tables with string pointers to use inline strings.

Diego Elio Pettenò flameeyes at gmail.com
Fri Oct 1 16:48:00 CEST 2010


By using this method, while (some) structures increase in final size, they
are defined directly in the output ELF file and mapped from there (from
.rodata) rather than requiring relocations and thus Copy-on-Write memory
for each process.

Out of those changed, the only one which really increases a lot is
oidtranstbl, while others even decrease in size on 64-bit systems (where a
pointer is 8-bytes).

Since it's no longer possible to check for the pointer to be non-NULL,
rather than terminating the table with an invalid entry, check against the
computed size of the table and its members, which should also make it
theoretically possible for the compiler to unroll the loops if needed.
---
 g10/armor.c       |   15 ++++++---------
 scd/app-nks.c     |    9 ++++-----
 scd/app-openpgp.c |   22 ++++++++++------------
 scd/app-p15.c     |   34 +++++++++++++++-------------------
 sm/certreqgen.c   |   11 +++++------
 sm/keylist.c      |   12 +++++-------
 6 files changed, 45 insertions(+), 58 deletions(-)

diff --git a/g10/armor.c b/g10/armor.c
index 30213a2..105c4a8 100644
--- a/g10/armor.c
+++ b/g10/armor.c
@@ -90,17 +90,16 @@ typedef enum {
  * into a mode which fakes packets and wait for the next armor */
 #define BEGIN_SIGNATURE 2
 #define BEGIN_SIGNED_MSG_IDX 3
-static char *head_strings[] = {
+static const char head_strings[][32] = {
     "BEGIN PGP MESSAGE",
     "BEGIN PGP PUBLIC KEY BLOCK",
     "BEGIN PGP SIGNATURE",
     "BEGIN PGP SIGNED MESSAGE",
     "BEGIN PGP ARMORED FILE",       /* gnupg extension */
     "BEGIN PGP PRIVATE KEY BLOCK",
-    "BEGIN PGP SECRET KEY BLOCK",   /* only used by pgp2 */
-    NULL
+    "BEGIN PGP SECRET KEY BLOCK"   /* only used by pgp2 */
 };
-static char *tail_strings[] = {
+static const char tail_strings[][32] = {
     "END PGP MESSAGE",
     "END PGP PUBLIC KEY BLOCK",
     "END PGP SIGNATURE",
@@ -108,7 +107,6 @@ static char *tail_strings[] = {
     "END PGP ARMORED FILE",
     "END PGP PRIVATE KEY BLOCK",
     "END PGP SECRET KEY BLOCK",
-    NULL
 };
 
 
@@ -330,7 +328,6 @@ is_armor_tag(const char *line)
 static int
 is_armor_header( byte *line, unsigned len )
 {
-    const char *s;
     byte *save_p, *p;
     int save_c;
     int i;
@@ -368,11 +365,11 @@ is_armor_header( byte *line, unsigned len )
 	return -1; /* garbage after dashes */
     save_c = *save_p; *save_p = 0;
     p = line+5;
-    for(i=0; (s=head_strings[i]); i++ )
-	if( !strcmp(s, p) )
+    for(i=0; i < DIM(head_strings); i++ )
+	if( !strcmp(head_strings[i], p) )
 	    break;
     *save_p = save_c;
-    if( !s )
+    if( i >= DIM(head_strings) )
 	return -1; /* unknown armor line */
 
     if( opt.verbose > 1 )
diff --git a/scd/app-nks.c b/scd/app-nks.c
index 068ccef..e27ffd4 100644
--- a/scd/app-nks.c
+++ b/scd/app-nks.c
@@ -302,14 +302,13 @@ get_chv_status (app_t app, int sigg, int pwid)
 static gpg_error_t 
 do_getattr (app_t app, ctrl_t ctrl, const char *name)
 {
-  static struct {
-    const char *name;
+  static const struct {
+    char name[16];
     int special;
   } table[] = {
     { "$AUTHKEYID",   1 },
     { "NKS-VERSION",  2 },
     { "CHV-STATUS",   3 },
-    { NULL, 0 }
   };
   gpg_error_t err = 0;
   int idx;
@@ -319,9 +318,9 @@ do_getattr (app_t app, ctrl_t ctrl, const char *name)
   if (err)
     return err;
 
-  for (idx=0; table[idx].name && strcmp (table[idx].name, name); idx++)
+  for (idx=0; idx < DIM(table) && strcmp (table[idx].name, name); idx++)
     ;
-  if (!table[idx].name)
+  if (idx >= DIM(table))
     return gpg_error (GPG_ERR_INV_NAME); 
 
   switch (table[idx].special)
diff --git a/scd/app-openpgp.c b/scd/app-openpgp.c
index eed6600..44b76bd 100644
--- a/scd/app-openpgp.c
+++ b/scd/app-openpgp.c
@@ -795,8 +795,8 @@ send_key_attr (ctrl_t ctrl, app_t app, const char *keyword, int number)
 static gpg_error_t 
 do_getattr (app_t app, ctrl_t ctrl, const char *name)
 {
-  static struct {
-    const char *name;
+  static const struct {
+    char name[16];
     int tag;
     int special;
   } table[] = {
@@ -819,17 +819,16 @@ do_getattr (app_t app, ctrl_t ctrl, const char *name)
     { "PRIVATE-DO-3", 0x0103 },
     { "PRIVATE-DO-4", 0x0104 },
     { "$AUTHKEYID",   0x0000, -3 },
-    { "$DISPSERIALNO",0x0000, -4 },
-    { NULL, 0 }
+    { "$DISPSERIALNO",0x0000, -4 }
   };
   int idx, i, rc;
   void *relptr;
   unsigned char *value;
   size_t valuelen;
 
-  for (idx=0; table[idx].name && strcmp (table[idx].name, name); idx++)
+  for (idx=0; idx < DIM(table) && strcmp (table[idx].name, name); idx++)
     ;
-  if (!table[idx].name)
+  if (idx >= DIM(table))
     return gpg_error (GPG_ERR_INV_NAME); 
   
   if (table[idx].special == -1)
@@ -1773,8 +1772,8 @@ do_setattr (app_t app, const char *name,
 {
   gpg_error_t rc;
   int idx;
-  static struct {
-    const char *name;
+  static const struct {
+    char name[16];
     int tag;
     int need_chv;
     int special;
@@ -1796,14 +1795,13 @@ do_setattr (app_t app, const char *name,
     { "CERT-3",       0x7F21, 3, 0, 1 },
     { "SM-KEY-ENC",   0x00D1, 3, 0, 1 },
     { "SM-KEY-MAC",   0x00D2, 3, 0, 1 },
-    { "KEY-ATTR",     0,      0, 3, 1 },
-    { NULL, 0 }
+    { "KEY-ATTR",     0,      0, 3, 1 }
   };
   int exmode;
 
-  for (idx=0; table[idx].name && strcmp (table[idx].name, name); idx++)
+  for (idx=0; idx < DIM(table) && strcmp (table[idx].name, name); idx++)
     ;
-  if (!table[idx].name)
+  if (idx >= DIM(table))
     return gpg_error (GPG_ERR_INV_NAME); 
   if (table[idx].need_v2 && !app->app_local->extcap.is_v2)
     return gpg_error (GPG_ERR_NOT_SUPPORTED); /* Not yet supported.  */
diff --git a/scd/app-p15.c b/scd/app-p15.c
index 512476b..e7737c7 100644
--- a/scd/app-p15.c
+++ b/scd/app-p15.c
@@ -54,35 +54,31 @@ typedef enum
 card_type_t;
 
 /* A list card types with ATRs noticed with these cards. */
-#define X(a) ((unsigned char const *)(a))
-static struct 
+static const struct
 {
   size_t atrlen;
-  unsigned char const *atr;
+  unsigned char const atr[32];
   card_type_t type;
 } card_atr_list[] = {
-  { 19, X("\x3B\xBA\x13\x00\x81\x31\x86\x5D\x00\x64\x05\x0A\x02\x01\x31\x80"
-          "\x90\x00\x8B"), 
+  { 19, "\x3B\xBA\x13\x00\x81\x31\x86\x5D\x00\x64\x05\x0A\x02\x01\x31\x80"
+        "\x90\x00\x8B",
     CARD_TYPE_TCOS },  /* SLE44 */
-  { 19, X("\x3B\xBA\x14\x00\x81\x31\x86\x5D\x00\x64\x05\x14\x02\x02\x31\x80"
-          "\x90\x00\x91"), 
+  { 19, "\x3B\xBA\x14\x00\x81\x31\x86\x5D\x00\x64\x05\x14\x02\x02\x31\x80"
+        "\x90\x00\x91",
     CARD_TYPE_TCOS }, /* SLE66S */
-  { 19, X("\x3B\xBA\x96\x00\x81\x31\x86\x5D\x00\x64\x05\x60\x02\x03\x31\x80"
-          "\x90\x00\x66"),
+  { 19, "\x3B\xBA\x96\x00\x81\x31\x86\x5D\x00\x64\x05\x60\x02\x03\x31\x80"
+        "\x90\x00\x66",
     CARD_TYPE_TCOS }, /* SLE66P */
-  { 27, X("\x3B\xFF\x94\x00\xFF\x80\xB1\xFE\x45\x1F\x03\x00\x68\xD2\x76\x00"
-          "\x00\x28\xFF\x05\x1E\x31\x80\x00\x90\x00\x23"),
+  { 27, "\x3B\xFF\x94\x00\xFF\x80\xB1\xFE\x45\x1F\x03\x00\x68\xD2\x76\x00"
+        "\x00\x28\xFF\x05\x1E\x31\x80\x00\x90\x00\x23",
     CARD_TYPE_MICARDO }, /* German BMI card */
-  { 19, X("\x3B\x6F\x00\xFF\x00\x68\xD2\x76\x00\x00\x28\xFF\x05\x1E\x31\x80"
-          "\x00\x90\x00"),
+  { 19, "\x3B\x6F\x00\xFF\x00\x68\xD2\x76\x00\x00\x28\xFF\x05\x1E\x31\x80"
+        "\x00\x90\x00",
     CARD_TYPE_MICARDO }, /* German BMI card (ATR due to reader problem) */
-  { 26, X("\x3B\xFE\x94\x00\xFF\x80\xB1\xFA\x45\x1F\x03\x45\x73\x74\x45\x49"
-          "\x44\x20\x76\x65\x72\x20\x31\x2E\x30\x43"),
+  { 26, "\x3B\xFE\x94\x00\xFF\x80\xB1\xFA\x45\x1F\x03\x45\x73\x74\x45\x49"
+        "\x44\x20\x76\x65\x72\x20\x31\x2E\x30\x43",
     CARD_TYPE_MICARDO }, /* EstEID (Estonian Big Brother card) */
-
-  { 0 }
 };
-#undef X
 
 
 /* The AID of PKCS15. */
@@ -3334,7 +3330,7 @@ app_select_p15 (app_t app)
             rc = gpg_error (GPG_ERR_INV_CARD);
           else
             {
-              for (i=0; card_atr_list[i].atrlen; i++)
+              for (i=0; i < DIM(card_atr_list); i++)
                 if (card_atr_list[i].atrlen == atrlen
                     && !memcmp (card_atr_list[i].atr, atr, atrlen))
                   {
diff --git a/sm/certreqgen.c b/sm/certreqgen.c
index 57f9515..efc7eec 100644
--- a/sm/certreqgen.c
+++ b/sm/certreqgen.c
@@ -249,8 +249,8 @@ get_parameter_uint (struct para_data_s *para, enum para_name key)
 static int
 read_parameters (ctrl_t ctrl, estream_t fp, ksba_writer_t writer)
 {
-  static struct {
-    const char *name;
+  static const struct {
+    char name[16];
     enum para_name key;
     int allow_dups;
   } keywords[] = {
@@ -261,8 +261,7 @@ read_parameters (ctrl_t ctrl, estream_t fp, ksba_writer_t writer)
     { "Name-DN",        pNAMEDN },
     { "Name-Email",     pNAMEEMAIL, 1 },
     { "Name-DNS",       pNAMEDNS, 1 },
-    { "Name-URI",       pNAMEURI, 1 },
-    { NULL, 0 }
+    { "Name-URI",       pNAMEURI, 1 }
   };
   char line[1024], *p;
   const char *err = NULL;
@@ -339,10 +338,10 @@ read_parameters (ctrl_t ctrl, estream_t fp, ksba_writer_t writer)
       value = p;
       trim_trailing_spaces (value);
 
-      for (i=0; (keywords[i].name
+      for (i=0; (i < DIM(keywords)
                  && ascii_strcasecmp (keywords[i].name, keyword)); i++)
         ;
-      if (!keywords[i].name)
+      if (i >= DIM(keywords))
         {
           err = "unknown keyword";
           break;
diff --git a/sm/keylist.c b/sm/keylist.c
index 9b8538c..fe2fb9f 100644
--- a/sm/keylist.c
+++ b/sm/keylist.c
@@ -85,10 +85,10 @@ struct
 #define OID_FLAG_UTF8 2 
 
 /* A table mapping OIDs to a descriptive string. */
-static struct 
+static const struct
 {
-  char *oid;
-  char *name;
+  char oid[32];
+  char name[44];
   unsigned int flag; /* A flag as described above.  */
 } oidtranstbl[] = {
 
@@ -188,9 +188,7 @@ static struct
   { "1.3.6.1.4.1.11591.2.1.1", "pkaAddress" },
 
   /* Extensions used by the Bundesnetzagentur.  */
-  { "1.3.6.1.4.1.8301.3.5", "validityModel" },
-
-  { NULL }
+  { "1.3.6.1.4.1.8301.3.5", "validityModel" }
 };
 
 
@@ -202,7 +200,7 @@ get_oid_desc (const char *oid, unsigned int *flag)
   int i;
 
   if (oid)
-    for (i=0; oidtranstbl[i].oid; i++)
+    for (i=0; i<DIM(oidtranstbl); i++)
       if (!strcmp (oidtranstbl[i].oid, oid))
         {
           if (flag)
-- 
1.7.3.1




More information about the Gnupg-devel mailing list