[svn] ksba - r289 - in trunk: . src

svn author wk cvs at cvs.gnupg.org
Mon Feb 25 11:00:11 CET 2008


Author: wk
Date: 2008-02-25 11:00:09 +0100 (Mon, 25 Feb 2008)
New Revision: 289

Modified:
   trunk/THANKS
   trunk/src/ChangeLog
   trunk/src/asn1-func.h
   trunk/src/asn1-func2.c
   trunk/src/asn1-gentables.c
   trunk/src/ber-decoder.c
   trunk/src/cms.c
   trunk/src/dn.c
   trunk/src/keyinfo.c
Log:
Avoid dynamic relocations.


[The diff below has been truncated]

Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/src/ChangeLog	2008-02-25 10:00:09 UTC (rev 289)
@@ -1,3 +1,16 @@
+2008-02-25  Werner Koch  <wk at g10code.com>
+
+	* keyinfo.c (pk_algo_table, sig_algo_table, enc_algo_table): Make
+	const.
+	(cryptval_to_sexp): Adjust for it.
+	* dn.c (oid_name_tbl): Make const and static.
+	* cms.c: Make oidstr_ const.
+
+	* asn1-gentables.c (create_static_structure): Revamp to use a
+	string table to reduce the relocation table.
+	* asn1-func.h: Replace char* by offsets.
+	* asn1-func2.c (ksba_asn_create_tree): Adjust for that change.
+
 2008-02-22  Werner Koch  <wk at g10code.com>
 
 	* keyinfo.c (pkalgo_t): New.

Modified: trunk/THANKS
===================================================================
--- trunk/THANKS	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/THANKS	2008-02-25 10:00:09 UTC (rev 289)
@@ -4,13 +4,14 @@
 Bernhard Herzog          bh at intevation.de
 Brad Hards               bradh at frogmouth.net
 Daiki Ueno               ueno at unixuser.org
+Diego Pettenò            flameeyes at gmail.com
 Peter O'Gorman           gnupg-devel at mlists.thewrittenword.com
 Robert Spillner          Robert.Spillner at post.rwth-aachen.de
 Stéphane Corthésy        stephane at sente.ch
 Thomas Koester           tkoester at intevation.de
 
 
- Copyright 2002, 2003, 2006 g10 Code GmbH
+ Copyright 2002, 2003, 2006, 2008 g10 Code GmbH
 
  This file is free software; as a special exception the author gives
  unlimited permission to copy and/or distribute it, with or without

Modified: trunk/src/asn1-func.h
===================================================================
--- trunk/src/asn1-func.h	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/src/asn1-func.h	2008-02-25 10:00:09 UTC (rev 289)
@@ -160,10 +160,10 @@
 
 
 typedef struct static_struct_asn {
-  char *name;                    /* Node name */
+  unsigned int name_off;        /* Node name */
   node_type_t type;             /* Node type */
   struct node_flag_s flags;
-  char *stringvalue;      
+  unsigned int stringvalue_off;
 } static_asn;
 
 
@@ -212,7 +212,8 @@
 
 
 /*-- asn1-tables.c (generated) --*/
-const static_asn *_ksba_asn_lookup_table (const char *name);
+const static_asn *_ksba_asn_lookup_table (const char *name,
+                                          const char **stringtbl);
 
 
 

Modified: trunk/src/asn1-func2.c
===================================================================
--- trunk/src/asn1-func2.c	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/src/asn1-func2.c	2008-02-25 10:00:09 UTC (rev 289)
@@ -1,6 +1,6 @@
 /* asn1-func2.c - More ASN.1 definitions
  *      Copyright (C) 2000, 2001 Fabio Fiorina
- *      Copyright (C) 2001 Free Software Foundation, Inc.
+ *      Copyright (C) 2001, 2008 Free Software Foundation, Inc.
  *
  * This file is part of GNUTLS.
  *
@@ -103,6 +103,7 @@
 {
   enum { DOWN, UP, RIGHT } move;
   const static_asn *root;
+  const char *strgtbl;
   AsnNode pointer;
   AsnNode p = NULL;
   AsnNode p_last = NULL;
@@ -116,7 +117,7 @@
 
   if (!mod_name)
     return gpg_error (GPG_ERR_INV_VALUE);
-  root = _ksba_asn_lookup_table (mod_name);
+  root = _ksba_asn_lookup_table (mod_name, &strgtbl);
   if (!root)
     return gpg_error (GPG_ERR_MODULE_NOT_FOUND);
 
@@ -124,7 +125,7 @@
   move = UP;
 
   k = 0;
-  while (root[k].stringvalue || root[k].type || root[k].name)
+  while (root[k].stringvalue_off || root[k].type || root[k].name_off)
     {
       p = _ksba_asn_new_node (root[k].type);
       p->flags = root[k].flags;
@@ -132,18 +133,19 @@
       p->link_next = link_next;
       link_next = p;
 
-      if (root[k].name)
-	_ksba_asn_set_name (p, root[k].name);
-      if (root[k].stringvalue)
+      if (root[k].name_off)
+	_ksba_asn_set_name (p, strgtbl + root[k].name_off);
+      if (root[k].stringvalue_off)
         {
           if (root[k].type == TYPE_TAG)
             {
               unsigned long val;
-              val = strtoul (root[k].stringvalue, NULL, 10);
+              val = strtoul (strgtbl+root[k].stringvalue_off, NULL, 10);
               _ksba_asn_set_value (p, VALTYPE_ULONG, &val, sizeof(val));
             }
           else
-            _ksba_asn_set_value (p, VALTYPE_CSTR, root[k].stringvalue, 0);
+            _ksba_asn_set_value (p, VALTYPE_CSTR, 
+                                 strgtbl+root[k].stringvalue_off, 0);
         }
 
       if (!pointer)

Modified: trunk/src/asn1-gentables.c
===================================================================
--- trunk/src/asn1-gentables.c	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/src/asn1-gentables.c	2008-02-25 10:00:09 UTC (rev 289)
@@ -1,5 +1,5 @@
 /* asn1-gentables.c - Tool to create required ASN tables
- *      Copyright (C) 2001 g10 Code GmbH
+ *      Copyright (C) 2001, 2008 g10 Code GmbH
  *
  * This file is part of KSBA.
  *
@@ -50,6 +50,8 @@
   char name[1];
 };
 
+static struct name_list_s *string_table, **string_table_tail;
+static size_t string_table_offset;
 
 static void print_error (const char *fmt, ... )  ATTR_PRINTF(1,2);
 
@@ -67,12 +69,111 @@
   
 }
 
+static size_t
+insert_string (const char *name)
+{
+  struct name_list_s *item;
+  size_t off, n;
+
+  if (!string_table_tail)
+    {
+      string_table_tail = &string_table;
+      insert_string ("");
+    }
+  
+  if (string_table_offset && !*name)
+    return 0;
+
+  for (item = string_table,off = 0; item; item = item->next)
+    {
+      for (n=0; item->name[n]; n++)
+        if (!strcmp (item->name+n, name))
+          return off + n;
+      off += strlen (item->name) + 1;
+    }
+  
+  item = xmalloc ( sizeof *item + strlen (name));
+  strcpy (item->name, name);
+  item->next = NULL;
+  *string_table_tail = item;
+  string_table_tail = &item->next;
+  off = string_table_offset;
+  string_table_offset += strlen (name) + 1;
+  return off;
+}
+
+static int
+cmp_string (const void *aptr, const void *bptr)
+{
+  const struct name_list_s **a = (const struct name_list_s **)aptr;
+  const struct name_list_s **b = (const struct name_list_s **)bptr;
+
+  return strlen ((*a)->name) < strlen ((*b)->name);
+}
+
+static void
+sort_string_table (void)
+{
+  struct name_list_s *item;
+  struct name_list_s **array;
+  size_t i, arraylen;
+  
+  if (!string_table || !string_table->next)
+    return; /* Nothing to sort.  */
+
+  for (item = string_table,arraylen = 0; item; item = item->next)
+    arraylen++;
+  array = xcalloc (arraylen, sizeof *array);
+  for (item = string_table,arraylen = 0; item; item = item->next)
+    array[arraylen++] = item;
+  qsort (array, arraylen, sizeof *array, cmp_string);
+  /* Replace table by sorted one.  */
+  string_table_tail = NULL;
+  string_table = NULL;
+  string_table_offset = 0;
+  for (i=0; i < arraylen; i++)
+    insert_string (array[i]->name);
+  xfree (array);
+  for (item = string_table,arraylen = 0; item; item = item->next)
+    fprintf (stderr, "  `%s'\n", item->name);
+}
+
+
+static void
+write_string_table (FILE *fp)
+{
+  struct name_list_s *item;
+  const char *s;
+  int count = 0;
+  int pos;
+
+  if (!string_table)
+    insert_string ("");
+
+  fputs ("static const char string_table[] = {\n  ", fp);
+  for (item = string_table; item; item = item->next)
+    {
+      for (s=item->name, pos=0; *s; s++)
+        {
+          if (!(pos++ % 16)) 
+            fprintf (fp, "%s  ", pos>1? "\n":"");
+          fprintf (fp, "'%c',", *s);
+        }
+      fputs ("'\\0',\n", fp);
+      count++;
+    }
+  /* (we use an extra \0 to get rid of the last comma) */
+  fprintf (fp, "  '\\0' };\n/* (%d strings) */\n", count);
+}
+
+
 static struct name_list_s *
-create_static_structure (AsnNode pointer, const char *file_name)
+create_static_structure (AsnNode pointer, const char *file_name, FILE *fp)
 {
   AsnNode p;
   struct name_list_s *structure_name;
   const char *char_p, *slash_p, *dot_p;
+  char numbuf[50];
 
   char_p = file_name;
   slash_p = file_name;
@@ -96,8 +197,8 @@
   memcpy (structure_name->name, slash_p, dot_p - slash_p);
   structure_name->name[dot_p - slash_p] = 0;
 
-  printf ("static const static_asn %s_asn1_tab[] = {\n",
-          structure_name->name);
+  fprintf (fp, "static const static_asn %s_asn1_tab[] = {\n",
+           structure_name->name);
 
   for (p = pointer; p; p = _ksba_asn_walk_tree (pointer, p))
     {
@@ -106,56 +207,63 @@
       p->flags.help_right = !!p->right;
 
       /* write a structure line */
-      fputs ("  {", stdout);
+      fputs ("  {", fp);
       if (p->name)
-	fprintf (stdout, "\"%s\"", p->name);
+	fprintf (fp, "%u", (unsigned int)insert_string (p->name));
       else
-	fprintf (stdout, "NULL");
-      fprintf (stdout, ",%u", p->type);
+	fprintf (fp, "0");
+      fprintf (fp, ",%u", p->type);
 
-      fputs (", {", stdout);
-      fprintf (stdout, "%u", p->flags.class);
-      fputs (p->flags.explicit       ? ",1":",0", stdout);
-      fputs (p->flags.implicit       ? ",1":",0", stdout);
-      fputs (p->flags.has_imports    ? ",1":",0", stdout);
-      fputs (p->flags.assignment     ? ",1":",0", stdout);
-      fputs (p->flags.one_param      ? ",1":",0", stdout);
-      fputs (p->flags.has_tag        ? ",1":",0", stdout);
-      fputs (p->flags.has_size       ? ",1":",0", stdout);
-      fputs (p->flags.has_list       ? ",1":",0", stdout);
-      fputs (p->flags.has_min_max    ? ",1":",0", stdout);
-      fputs (p->flags.has_defined_by ? ",1":",0", stdout);
-      fputs (p->flags.is_false       ? ",1":",0", stdout);
-      fputs (p->flags.is_true        ? ",1":",0", stdout);
-      fputs (p->flags.has_default     ? ",1":",0", stdout);
-      fputs (p->flags.is_optional    ? ",1":",0", stdout);
-      fputs (p->flags.is_implicit    ? ",1":",0", stdout);
-      fputs (p->flags.in_set         ? ",1":",0", stdout);
-      fputs (p->flags.in_choice      ? ",1":",0", stdout);
-      fputs (p->flags.in_array       ? ",1":",0", stdout);
-      fputs (p->flags.is_any         ? ",1":",0", stdout);
-      fputs (p->flags.not_used       ? ",1":",0", stdout);
-      fputs (p->flags.help_down      ? ",1":",0", stdout);
-      fputs (p->flags.help_right     ? ",1":",0", stdout);
-      fputs ("}", stdout);
+      fputs (", {", fp);
+      fprintf (fp, "%u", p->flags.class);
+      fputs (p->flags.explicit       ? ",1":",0", fp);
+      fputs (p->flags.implicit       ? ",1":",0", fp);
+      fputs (p->flags.has_imports    ? ",1":",0", fp);
+      fputs (p->flags.assignment     ? ",1":",0", fp);
+      fputs (p->flags.one_param      ? ",1":",0", fp);
+      fputs (p->flags.has_tag        ? ",1":",0", fp);
+      fputs (p->flags.has_size       ? ",1":",0", fp);
+      fputs (p->flags.has_list       ? ",1":",0", fp);
+      fputs (p->flags.has_min_max    ? ",1":",0", fp);
+      fputs (p->flags.has_defined_by ? ",1":",0", fp);
+      fputs (p->flags.is_false       ? ",1":",0", fp);
+      fputs (p->flags.is_true        ? ",1":",0", fp);
+      fputs (p->flags.has_default     ? ",1":",0", fp);
+      fputs (p->flags.is_optional    ? ",1":",0", fp);
+      fputs (p->flags.is_implicit    ? ",1":",0", fp);
+      fputs (p->flags.in_set         ? ",1":",0", fp);
+      fputs (p->flags.in_choice      ? ",1":",0", fp);
+      fputs (p->flags.in_array       ? ",1":",0", fp);
+      fputs (p->flags.is_any         ? ",1":",0", fp);
+      fputs (p->flags.not_used       ? ",1":",0", fp);
+      fputs (p->flags.help_down      ? ",1":",0", fp);
+      fputs (p->flags.help_right     ? ",1":",0", fp);
+      fputs ("}", fp);
 
       if (p->valuetype == VALTYPE_CSTR)
-	fprintf (stdout, ",\"%s\"", p->value.v_cstr);
+	fprintf (fp, ",%u", 
+                 (unsigned int)insert_string (p->value.v_cstr));
       else if (p->valuetype == VALTYPE_LONG
                && p->type == TYPE_INTEGER && p->flags.assignment)
-        fprintf (stdout, ",\"%ld\"", p->value.v_long);
+        {
+          snprintf (numbuf, sizeof numbuf, "%ld", p->value.v_long);
+          fprintf (fp, ",%u", (unsigned int)insert_string (numbuf));
+        }
       else if (p->valuetype == VALTYPE_ULONG)
-        fprintf (stdout, ",\"%lu\"", p->value.v_ulong);
+        {
+          snprintf (numbuf, sizeof numbuf, "%lu", p->value.v_ulong);
+          fprintf (fp, ",%u", (unsigned int)insert_string (numbuf));
+        }
       else
         {
           if (p->valuetype)
             print_error ("can't store a value of type %d\n", p->valuetype);
-          fprintf (stdout, ",0");
+          fprintf (fp, ",0");
         }
-      fputs ("},\n", stdout);
+      fputs ("},\n", fp);
     }
 
-  fprintf (stdout, "  {0,0}\n};\n");
+  fprintf (fp, "  {0,0}\n};\n");
 
   return structure_name;
 }
@@ -163,30 +271,30 @@
 
 
 static struct name_list_s *
-one_file (FILE *fp, const char *fname, int *count)
+one_file (const char *fname, int *count, FILE *fp)
 {
   ksba_asn_tree_t tree;
   int rc;
     
   rc = ksba_asn_parse_file (fname, &tree, check_only);
   if (rc)
-      print_error ("error parsing `%s': %s\n", fname, gpg_strerror (rc) );
+    print_error ("error parsing `%s': %s\n", fname, gpg_strerror (rc) );
   else if (!check_only)
     {
       if (dump_only)
-        ksba_asn_tree_dump (tree, dump_only==2? "<":NULL, stdout);
+        ksba_asn_tree_dump (tree, dump_only==2? "<":NULL, fp);
       else
         {
           if (!*count)
-            printf ("\n"
-                    "#include <config.h>\n"
-                    "#include <stdio.h>\n"
-                    "#include <string.h>\n"
-                    "#include \"ksba.h\"\n"
-                    "#include \"asn1-func.h\"\n"
-                    "\n");
+            fprintf (fp,"\n"
+                     "#include <config.h>\n"
+                     "#include <stdio.h>\n"
+                     "#include <string.h>\n"
+                     "#include \"ksba.h\"\n"
+                     "#include \"asn1-func.h\"\n"
+                     "\n");
           ++*count;
-          return create_static_structure (tree->parse_tree, fname);
+          return create_static_structure (tree->parse_tree, fname, fp);
         }
     }
   return 0;
@@ -198,6 +306,7 @@
 {
   int count = 0;
   struct name_list_s *all_names = NULL, *nl;
+  int i;
 
   if (!argc || (argc > 1 &&
                 (!strcmp (argv[1],"--help") || !strcmp (argv[1],"-h"))) )
@@ -226,34 +335,48 @@
 
 
   if (!argc)
-    all_names = one_file (stdin, "-", &count);
+    all_names = one_file ("-", &count, stdout);
   else
     {
+      FILE *nullfp;
+
+      /* We first parse it to /dev/null to build up the string table.  */
+      nullfp = fopen ("/dev/null", "w");
+      if (!nullfp)
+        {
+          print_error ("can't open `/dev/null': %s\n", strerror (errno));
+          exit (2);
+        }
+      for (i=0; i < argc; i++) 
+        one_file (argv[i], &count, nullfp);
+      fclose (nullfp);
+
+      sort_string_table ();
+
+      count = 0;
       for (; argc; argc--, argv++) 
         {
-          FILE *fp;
-          
-          fp = fopen (*argv, "r");
-          if (!fp)
-              print_error ("can't open `%s': %s\n", *argv, strerror (errno));
-          else
+          nl = one_file (*argv, &count, stdout);
+          if (nl)
             {
-              nl = one_file (fp, *argv, &count);
-              fclose (fp);
-              if (nl)
-                {
-                  nl->next = all_names;
-                  all_names = nl;
-                }
+              nl->next = all_names;
+              all_names = nl;
             }
         }
     }
 
   if (all_names && !error_counter)
-    { /* Write the lookup function */
+    { 
+      /* Write the string table. */
+      putchar ('\n');
+      write_string_table (stdout);
+      /* Write the lookup function */
       printf ("\n\nconst static_asn *\n"
-              "_ksba_asn_lookup_table (const char *name)\n"
-              "{\n");
+              "_ksba_asn_lookup_table (const char *name,"
+              " const char **stringtbl)\n"
+              "{\n"
+              "  *stringtbl = string_table;\n"
+              );
       for (nl=all_names; nl; nl = nl->next)
         printf ("  if (!strcmp (name, \"%s\"))\n"
                 "    return %s_asn1_tab;\n", nl->name, nl->name);

Modified: trunk/src/ber-decoder.c
===================================================================
--- trunk/src/ber-decoder.c	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/src/ber-decoder.c	2008-02-25 10:00:09 UTC (rev 289)
@@ -198,7 +198,7 @@
 static const char *
 universal_tag_name (unsigned long no)
 {
-  static const char *names[31] = {
+  static const char * const names[31] = {
     "[End Tag]",
     "BOOLEAN",
     "INTEGER",

Modified: trunk/src/cms.c
===================================================================
--- trunk/src/cms.c	2008-02-22 15:46:34 UTC (rev 288)
+++ trunk/src/cms.c	2008-02-25 10:00:09 UTC (rev 289)
@@ -65,16 +65,16 @@
   { NULL }
 };
 
-static char oidstr_contentType[] = "1.2.840.113549.1.9.3";
+static const char oidstr_contentType[] = "1.2.840.113549.1.9.3";
 /*static char oid_contentType[9] = "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x03";*/
 
-static char oidstr_messageDigest[] = "1.2.840.113549.1.9.4";




More information about the Gnupg-commits mailing list