gpg: Replace two sprintf calls.
[gnupg.git] / g10 / kbnode.c
index 5df6d8d..e814fa8 100644 (file)
@@ -1,11 +1,12 @@
 /* kbnode.c -  keyblock node utility functions
 /* kbnode.c -  keyblock node utility functions
- * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+ * Copyright (C) 1998, 1999, 2000, 2001, 2002,
+ *               2005, 2010 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * GnuPG is distributed in the hope that it will be useful,
  * (at your option) any later version.
  *
  * GnuPG is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
+
+#include "gpg.h"
 #include "util.h"
 #include "util.h"
-#include "memory.h"
+#include "../common/init.h"
 #include "packet.h"
 #include "keydb.h"
 
 #define USE_UNUSED_NODES 1
 
 #include "packet.h"
 #include "keydb.h"
 
 #define USE_UNUSED_NODES 1
 
+static int cleanup_registered;
 static KBNODE unused_nodes;
 
 static KBNODE unused_nodes;
 
-static KBNODE
-alloc_node(void)
+static void
+release_unused_nodes (void)
 {
 {
-    KBNODE n;
+#if USE_UNUSED_NODES
+  while (unused_nodes)
+    {
+      kbnode_t next = unused_nodes->next;
+      xfree (unused_nodes);
+      unused_nodes = next;
+    }
+#endif /*USE_UNUSED_NODES*/
+}
 
 
-    n = unused_nodes;
-    if( n )
-       unused_nodes = n->next;
-    else
-       n = m_alloc( sizeof *n );
-    n->next = NULL;
-    n->pkt = NULL;
-    n->flag = 0;
-    n->private_flag=0;
-    n->recno = 0;
-    return n;
+
+static kbnode_t
+alloc_node (void)
+{
+  kbnode_t n;
+
+  n = unused_nodes;
+  if (n)
+    unused_nodes = n->next;
+  else
+    {
+      if (!cleanup_registered)
+        {
+          cleanup_registered = 1;
+          register_mem_cleanup_func (release_unused_nodes);
+        }
+      n = xmalloc (sizeof *n);
+    }
+  n->next = NULL;
+  n->pkt = NULL;
+  n->flag = 0;
+  n->private_flag=0;
+  n->recno = 0;
+  return n;
 }
 
 static void
 free_node( KBNODE n )
 {
 }
 
 static void
 free_node( KBNODE n )
 {
-    if( n ) {
+  if (n)
+    {
 #if USE_UNUSED_NODES
 #if USE_UNUSED_NODES
-       n->next = unused_nodes;
-       unused_nodes = n;
+      n->next = unused_nodes;
+      unused_nodes = n;
 #else
 #else
-       m_free( n );
+      xfree (n);
 #endif
     }
 }
 #endif
     }
 }
@@ -94,7 +118,7 @@ release_kbnode( KBNODE n )
        n2 = n->next;
        if( !is_cloned_kbnode(n) ) {
            free_packet( n->pkt );
        n2 = n->next;
        if( !is_cloned_kbnode(n) ) {
            free_packet( n->pkt );
-           m_free( n->pkt );
+           xfree( n->pkt );
        }
        free_node( n );
        n = n2;
        }
        free_node( n );
        n = n2;
@@ -112,8 +136,6 @@ delete_kbnode( KBNODE node )
     node->private_flag |= 1;
 }
 
     node->private_flag |= 1;
 }
 
-
-
 /****************
  * Append NODE to ROOT.  ROOT must exist!
  */
 /****************
  * Append NODE to ROOT.  ROOT must exist!
  */
@@ -185,7 +207,7 @@ find_next_kbnode( KBNODE node, int pkttype )
     for( node=node->next ; node; node = node->next ) {
        if( !pkttype )
            return node;
     for( node=node->next ; node; node = node->next ) {
        if( !pkttype )
            return node;
-       else if( pkttype == PKT_USER_ID 
+       else if( pkttype == PKT_USER_ID
                 && (   node->pkt->pkttype == PKT_PUBLIC_KEY
                     || node->pkt->pkttype == PKT_SECRET_KEY ) )
            return NULL;
                 && (   node->pkt->pkttype == PKT_PUBLIC_KEY
                     || node->pkt->pkttype == PKT_SECRET_KEY ) )
            return NULL;
@@ -267,7 +289,7 @@ commit_kbnode( KBNODE *root )
                nl->next = n->next;
            if( !is_cloned_kbnode(n) ) {
                free_packet( n->pkt );
                nl->next = n->next;
            if( !is_cloned_kbnode(n) ) {
                free_packet( n->pkt );
-               m_free( n->pkt );
+               xfree( n->pkt );
            }
            free_node( n );
            changed = 1;
            }
            free_node( n );
            changed = 1;
@@ -291,7 +313,7 @@ remove_kbnode( KBNODE *root, KBNODE node )
                nl->next = n->next;
            if( !is_cloned_kbnode(n) ) {
                free_packet( n->pkt );
                nl->next = n->next;
            if( !is_cloned_kbnode(n) ) {
                free_packet( n->pkt );
-               m_free( n->pkt );
+               xfree( n->pkt );
            }
            free_node( n );
        }
            }
            free_node( n );
        }
@@ -337,63 +359,73 @@ move_kbnode( KBNODE *root, KBNODE node, KBNODE where )
 
 
 void
 
 
 void
-dump_kbnode( KBNODE node )
+dump_kbnode (KBNODE node)
 {
 {
-    for(; node; node = node->next ) {
-       const char *s;
-       switch( node->pkt->pkttype ) {
-         case 0:               s="empty"; break;
-         case PKT_PUBLIC_KEY:  s="public-key"; break;
-         case PKT_SECRET_KEY:  s="secret-key"; break;
-         case PKT_SECRET_SUBKEY: s= "secret-subkey"; break;
-         case PKT_PUBKEY_ENC:  s="public-enc"; break;
-         case PKT_SIGNATURE:   s="signature"; break;
-         case PKT_ONEPASS_SIG: s="onepass-sig"; break;
-         case PKT_USER_ID:     s="user-id"; break;
-         case PKT_PUBLIC_SUBKEY: s="public-subkey"; break;
-         case PKT_COMMENT:     s="comment"; break;
-         case PKT_RING_TRUST:  s="trust"; break;
-         case PKT_PLAINTEXT:   s="plaintext"; break;
-         case PKT_COMPRESSED:  s="compressed"; break;
-         case PKT_ENCRYPTED:   s="encrypted"; break;
-          case PKT_GPG_CONTROL: s="gpg-control"; break;
-         default:              s="unknown"; break;
-       }
-       fprintf(stderr, "node %p %02x/%02x type=%s",
-               node, node->flag, node->private_flag, s);
-       if( node->pkt->pkttype == PKT_USER_ID ) {
-            PKT_user_id *uid = node->pkt->pkt.user_id;
-           fputs("  \"", stderr);
-           print_string( stderr, uid->name, uid->len, 0 );
-           fprintf (stderr, "\" %c%c%c%c\n",
-                     uid->is_expired? 'e':'.',
-                     uid->is_revoked? 'r':'.',
-                     uid->created?    'v':'.',
-                     uid->is_primary? 'p':'.' );
+  for (; node; node = node->next )
+    {
+      const char *s;
+      switch (node->pkt->pkttype)
+        {
+        case 0:                s="empty"; break;
+        case PKT_PUBLIC_KEY:   s="public-key"; break;
+        case PKT_SECRET_KEY:   s="secret-key"; break;
+        case PKT_SECRET_SUBKEY: s= "secret-subkey"; break;
+        case PKT_PUBKEY_ENC:   s="public-enc"; break;
+        case PKT_SIGNATURE:    s="signature"; break;
+        case PKT_ONEPASS_SIG: s="onepass-sig"; break;
+        case PKT_USER_ID:      s="user-id"; break;
+        case PKT_PUBLIC_SUBKEY: s="public-subkey"; break;
+        case PKT_COMMENT:      s="comment"; break;
+        case PKT_RING_TRUST:   s="trust"; break;
+        case PKT_PLAINTEXT:    s="plaintext"; break;
+        case PKT_COMPRESSED:   s="compressed"; break;
+        case PKT_ENCRYPTED:    s="encrypted"; break;
+        case PKT_GPG_CONTROL: s="gpg-control"; break;
+        default:               s="unknown"; break;
        }
        }
-       else if( node->pkt->pkttype == PKT_SIGNATURE ) {
-           fprintf(stderr, "  class=%02x keyid=%08lX ts=%lu\n",
-                  node->pkt->pkt.signature->sig_class,
-                  (ulong)node->pkt->pkt.signature->keyid[1],
-                   (ulong)node->pkt->pkt.signature->timestamp);
-       }
-       else if( node->pkt->pkttype == PKT_GPG_CONTROL ) {
-           fprintf(stderr, " ctrl=%d len=%u\n",
-                    node->pkt->pkt.gpg_control->control,
-                    (unsigned int)node->pkt->pkt.gpg_control->datalen);
-       }
-       else if( node->pkt->pkttype == PKT_PUBLIC_KEY
-                || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
-            PKT_public_key *pk = node->pkt->pkt.public_key;
-           fprintf(stderr, "  keyid=%08lX a=%d u=%d %c%c%c%c\n",
-                    (ulong)keyid_from_pk( pk, NULL ),
-                    pk->pubkey_algo, pk->pubkey_usage,
-                    pk->has_expired? 'e':'.',  
-                    pk->is_revoked?  'r':'.',  
-                    pk->is_valid?    'v':'.',
-                    pk->mdc_feature? 'm':'.');
-       }
-       else
-           fputs("\n", stderr);
+      log_debug ("node %p %02x/%02x type=%s",
+                 node, node->flag, node->private_flag, s);
+      if (node->pkt->pkttype == PKT_USER_ID)
+        {
+          PKT_user_id *uid = node->pkt->pkt.user_id;
+          log_printf ("  \"");
+          es_write_sanitized (log_get_stream (), uid->name, uid->len,
+                              NULL, NULL);
+          log_printf ("\" %c%c%c%c\n",
+                      uid->is_expired? 'e':'.',
+                      uid->is_revoked? 'r':'.',
+                      uid->created?    'v':'.',
+                      uid->is_primary? 'p':'.' );
+        }
+      else if (node->pkt->pkttype == PKT_SIGNATURE)
+        {
+          log_printf ("  class=%02x keyid=%08lX ts=%lu\n",
+                      node->pkt->pkt.signature->sig_class,
+                      (ulong)node->pkt->pkt.signature->keyid[1],
+                      (ulong)node->pkt->pkt.signature->timestamp);
+        }
+      else if (node->pkt->pkttype == PKT_GPG_CONTROL)
+        {
+          log_printf (" ctrl=%d len=%u\n",
+                      node->pkt->pkt.gpg_control->control,
+                      (unsigned int)node->pkt->pkt.gpg_control->datalen);
+        }
+      else if (node->pkt->pkttype == PKT_PUBLIC_KEY
+               || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
+        {
+          PKT_public_key *pk = node->pkt->pkt.public_key;
+
+          log_printf ("  keyid=%08lX a=%d u=%d %c%c%c%c\n",
+                      (ulong)keyid_from_pk( pk, NULL ),
+                      pk->pubkey_algo, pk->pubkey_usage,
+                      pk->has_expired? 'e':'.',
+                      pk->flags.revoked? 'r':'.',
+                      pk->flags.valid?    'v':'.',
+                      pk->flags.mdc?   'm':'.');
+        }
+      else
+        log_printf ("\n");
+
+      log_flush ();
     }
 }
     }
 }