1 /* kbnode.c - keyblock node utility functions
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31 #define USE_UNUSED_NODES 1
33 static KBNODE unused_nodes;
36 new_kbnode( PACKET *pkt )
42 unused_nodes = n->next;
44 n = m_alloc( sizeof *n );
55 clone_kbnode( KBNODE node )
61 unused_nodes = n->next;
63 n = m_alloc( sizeof *n );
67 n->private_flag = node->private_flag | 2; /* mark cloned */
73 release_kbnode( KBNODE n )
79 if( !(n->private_flag & 2) ) {
80 free_packet( n->pkt );
84 n->next = unused_nodes;
95 * Delete NODE from ROOT. ROOT must exist!
96 * Note: This only works with walk_kbnode!!
99 delete_kbnode( KBNODE node )
101 node->private_flag |= 1;
106 * Append NODE to ROOT. ROOT must exist!
109 add_kbnode( KBNODE root, KBNODE node )
113 for(n1=root; n1->next; n1 = n1->next)
119 * Insert NODE into the list after root but before a packet which is not of
121 * (only if PKTTYPE != 0)
124 insert_kbnode( KBNODE root, KBNODE node, int pkttype )
127 node->next = root->next;
133 for(n1=root; n1->next; n1 = n1->next)
134 if( pkttype != n1->next->pkt->pkttype ) {
135 node->next = n1->next;
139 /* no such packet, append */
147 * Find the previous node (if PKTTYPE = 0) or the previous node
148 * with pkttype PKTTYPE in the list starting with ROOT of NODE.
151 find_prev_kbnode( KBNODE root, KBNODE node, int pkttype )
155 for(n1=NULL ; root && root != node; root = root->next )
156 if( !pkttype || root->pkt->pkttype == pkttype )
162 * Ditto, but find the next packet. The behaviour is trivial if
163 * PKTTYPE is 0 but if it is specified, the next node with a packet
164 * of this type is returned. The function has some knowledge about
165 * the valid ordering of packets: e.g. if the next signature packet
166 * is requested, the function will not return one if it encounters
170 find_next_kbnode( KBNODE node, int pkttype )
172 for( node=node->next ; node; node = node->next ) {
175 else if( pkttype == PKT_USER_ID
176 && ( node->pkt->pkttype == PKT_PUBLIC_KEY
177 || node->pkt->pkttype == PKT_SECRET_KEY ) )
179 else if( pkttype == PKT_SIGNATURE
180 && ( node->pkt->pkttype == PKT_USER_ID
181 || node->pkt->pkttype == PKT_PUBLIC_KEY
182 || node->pkt->pkttype == PKT_SECRET_KEY ) )
184 else if( node->pkt->pkttype == pkttype )
192 find_kbnode( KBNODE node, int pkttype )
194 for( ; node; node = node->next ) {
195 if( node->pkt->pkttype == pkttype )
204 * Walk through a list of kbnodes. This function returns
205 * the next kbnode for each call; before using the function the first
206 * time, the caller must set CONTEXT to NULL (This has simply the effect
207 * to start with ROOT).
210 walk_kbnode( KBNODE root, KBNODE *context, int all )
220 n = (*context)->next;
223 } while( !all && n && (n->private_flag & 1) );
229 clear_kbnode_flags( KBNODE n )
231 for( ; n; n = n->next ) {
238 * Commit changes made to the kblist at ROOT. Note that ROOT my change,
239 * and it is therefore passed by reference.
240 * The function has the effect of removing all nodes marked as deleted.
241 * returns true if any node has been changed
244 commit_kbnode( KBNODE *root )
249 for( n = *root, nl=NULL; n; n = nl->next ) {
250 if( (n->private_flag & 1) ) {
252 *root = nl = n->next;
255 if( !(n->private_flag & 2) ) {
256 free_packet( n->pkt );
260 n->next = unused_nodes;
275 dump_kbnode( KBNODE node )
277 for(; node; node = node->next ) {
279 switch( node->pkt->pkttype ) {
280 case 0: s="empty"; break;
281 case PKT_PUBLIC_KEY: s="public-key"; break;
282 case PKT_SECRET_KEY: s="secret-key"; break;
283 case PKT_SECRET_SUBKEY: s= "secret-subkey"; break;
284 case PKT_PUBKEY_ENC: s="public-enc"; break;
285 case PKT_SIGNATURE: s="signature"; break;
286 case PKT_ONEPASS_SIG: s="onepass-sig"; break;
287 case PKT_USER_ID: s="user-id"; break;
288 case PKT_PUBLIC_SUBKEY: s="public-subkey"; break;
289 case PKT_COMMENT: s="comment"; break;
290 case PKT_RING_TRUST: s="trust"; break;
291 case PKT_PLAINTEXT: s="plaintext"; break;
292 case PKT_COMPRESSED: s="compressed"; break;
293 case PKT_ENCRYPTED: s="encrypted"; break;
294 default: s="unknown"; break;
296 fprintf(stderr, "node %p %02x/%02x type=%s",
297 node, node->flag, node->private_flag, s);
298 if( node->pkt->pkttype == PKT_USER_ID ) {
299 fputs(" \"", stderr);
300 print_string( stderr, node->pkt->pkt.user_id->name,
301 node->pkt->pkt.user_id->len, 0 );
302 fputs("\"\n", stderr);
304 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
305 fprintf(stderr, " keyid=%08lX\n",
306 (ulong)node->pkt->pkt.signature->keyid[1] );
308 else if( node->pkt->pkttype == PKT_PUBLIC_KEY
309 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
310 fprintf(stderr, " keyid=%08lX\n", (ulong)
311 keyid_from_pk( node->pkt->pkt.public_key, NULL ));