2004-02-25 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / kbnode.c
blob58daad8715c27043f254c0e1d3e0f551bc37be75
1 /* kbnode.c - keyblock node utility functions
2 * Copyright (C) 1998, 1999, 2000, 2001 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
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "gpg.h"
28 #include "util.h"
29 #include "memory.h"
30 #include "packet.h"
31 #include "keydb.h"
33 #define USE_UNUSED_NODES 1
35 static KBNODE unused_nodes;
37 static KBNODE
38 alloc_node(void)
40 KBNODE n;
42 n = unused_nodes;
43 if( n )
44 unused_nodes = n->next;
45 else
46 n = xmalloc ( sizeof *n );
47 n->next = NULL;
48 n->pkt = NULL;
49 n->flag = 0;
50 n->private_flag=0;
51 n->recno = 0;
52 return n;
55 static void
56 free_node( KBNODE n )
58 if( n ) {
59 #if USE_UNUSED_NODES
60 n->next = unused_nodes;
61 unused_nodes = n;
62 #else
63 xfree ( n );
64 #endif
70 KBNODE
71 new_kbnode( PACKET *pkt )
73 KBNODE n = alloc_node();
74 n->pkt = pkt;
75 return n;
79 KBNODE
80 clone_kbnode( KBNODE node )
82 KBNODE n = alloc_node();
84 n->pkt = node->pkt;
85 n->private_flag = node->private_flag | 2; /* mark cloned */
86 return n;
90 void
91 release_kbnode( KBNODE n )
93 KBNODE n2;
95 while( n ) {
96 n2 = n->next;
97 if( !is_cloned_kbnode(n) ) {
98 free_packet( n->pkt );
99 xfree ( n->pkt );
101 free_node( n );
102 n = n2;
107 /****************
108 * Delete NODE.
109 * Note: This only works with walk_kbnode!!
111 void
112 delete_kbnode( KBNODE node )
114 node->private_flag |= 1;
119 /****************
120 * Append NODE to ROOT. ROOT must exist!
122 void
123 add_kbnode( KBNODE root, KBNODE node )
125 KBNODE n1;
127 for(n1=root; n1->next; n1 = n1->next)
129 n1->next = node;
132 /****************
133 * Insert NODE into the list after root but before a packet which is not of
134 * type PKTTYPE
135 * (only if PKTTYPE != 0)
137 void
138 insert_kbnode( KBNODE root, KBNODE node, int pkttype )
140 if( !pkttype ) {
141 node->next = root->next;
142 root->next = node;
144 else {
145 KBNODE n1;
147 for(n1=root; n1->next; n1 = n1->next)
148 if( pkttype != n1->next->pkt->pkttype ) {
149 node->next = n1->next;
150 n1->next = node;
151 return;
153 /* no such packet, append */
154 node->next = NULL;
155 n1->next = node;
160 /****************
161 * Find the previous node (if PKTTYPE = 0) or the previous node
162 * with pkttype PKTTYPE in the list starting with ROOT of NODE.
164 KBNODE
165 find_prev_kbnode( KBNODE root, KBNODE node, int pkttype )
167 KBNODE n1;
169 for (n1=NULL; root && root != node; root = root->next ) {
170 if (!pkttype ||root->pkt->pkttype == pkttype)
171 n1 = root;
173 return n1;
176 /****************
177 * Ditto, but find the next packet. The behaviour is trivial if
178 * PKTTYPE is 0 but if it is specified, the next node with a packet
179 * of this type is returned. The function has some knowledge about
180 * the valid ordering of packets: e.g. if the next signature packet
181 * is requested, the function will not return one if it encounters
182 * a user-id.
184 KBNODE
185 find_next_kbnode( KBNODE node, int pkttype )
187 for( node=node->next ; node; node = node->next ) {
188 if( !pkttype )
189 return node;
190 else if( pkttype == PKT_USER_ID
191 && ( node->pkt->pkttype == PKT_PUBLIC_KEY
192 || node->pkt->pkttype == PKT_SECRET_KEY ) )
193 return NULL;
194 else if( pkttype == PKT_SIGNATURE
195 && ( node->pkt->pkttype == PKT_USER_ID
196 || node->pkt->pkttype == PKT_PUBLIC_KEY
197 || node->pkt->pkttype == PKT_SECRET_KEY ) )
198 return NULL;
199 else if( node->pkt->pkttype == pkttype )
200 return node;
202 return NULL;
206 KBNODE
207 find_kbnode( KBNODE node, int pkttype )
209 for( ; node; node = node->next ) {
210 if( node->pkt->pkttype == pkttype )
211 return node;
213 return NULL;
218 /****************
219 * Walk through a list of kbnodes. This function returns
220 * the next kbnode for each call; before using the function the first
221 * time, the caller must set CONTEXT to NULL (This has simply the effect
222 * to start with ROOT).
224 KBNODE
225 walk_kbnode( KBNODE root, KBNODE *context, int all )
227 KBNODE n;
229 do {
230 if( !*context ) {
231 *context = root;
232 n = root;
234 else {
235 n = (*context)->next;
236 *context = n;
238 } while( !all && n && is_deleted_kbnode(n) );
240 return n;
243 void
244 clear_kbnode_flags( KBNODE n )
246 for( ; n; n = n->next ) {
247 n->flag = 0;
252 /****************
253 * Commit changes made to the kblist at ROOT. Note that ROOT my change,
254 * and it is therefore passed by reference.
255 * The function has the effect of removing all nodes marked as deleted.
256 * returns true if any node has been changed
259 commit_kbnode( KBNODE *root )
261 KBNODE n, nl;
262 int changed = 0;
264 for( n = *root, nl=NULL; n; n = nl->next ) {
265 if( is_deleted_kbnode(n) ) {
266 if( n == *root )
267 *root = nl = n->next;
268 else
269 nl->next = n->next;
270 if( !is_cloned_kbnode(n) ) {
271 free_packet( n->pkt );
272 xfree ( n->pkt );
274 free_node( n );
275 changed = 1;
277 else
278 nl = n;
280 return changed;
283 void
284 remove_kbnode( KBNODE *root, KBNODE node )
286 KBNODE n, nl;
288 for( n = *root, nl=NULL; n; n = nl->next ) {
289 if( n == node ) {
290 if( n == *root )
291 *root = nl = n->next;
292 else
293 nl->next = n->next;
294 if( !is_cloned_kbnode(n) ) {
295 free_packet( n->pkt );
296 xfree ( n->pkt );
298 free_node( n );
300 else
301 nl = n;
306 /****************
307 * Move NODE behind right after WHERE or to the beginning if WHERE is NULL.
309 void
310 move_kbnode( KBNODE *root, KBNODE node, KBNODE where )
312 KBNODE tmp, prev;
314 if( !root || !*root || !node )
315 return; /* sanity check */
316 for( prev = *root; prev && prev->next != node; prev = prev->next )
318 if( !prev )
319 return; /* node is not in the list */
321 if( !where ) { /* move node before root */
322 if( node == *root ) /* move to itself */
323 return;
324 prev->next = node->next;
325 node->next = *root;
326 *root = node;
327 return;
329 /* move it after where */
330 if( node == where )
331 return;
332 tmp = node->next;
333 node->next = where->next;
334 where->next = node;
335 prev->next = tmp;
341 void
342 dump_kbnode( KBNODE node )
344 for(; node; node = node->next ) {
345 const char *s;
346 switch( node->pkt->pkttype ) {
347 case 0: s="empty"; break;
348 case PKT_PUBLIC_KEY: s="public-key"; break;
349 case PKT_SECRET_KEY: s="secret-key"; break;
350 case PKT_SECRET_SUBKEY: s= "secret-subkey"; break;
351 case PKT_PUBKEY_ENC: s="public-enc"; break;
352 case PKT_SIGNATURE: s="signature"; break;
353 case PKT_ONEPASS_SIG: s="onepass-sig"; break;
354 case PKT_USER_ID: s="user-id"; break;
355 case PKT_PUBLIC_SUBKEY: s="public-subkey"; break;
356 case PKT_COMMENT: s="comment"; break;
357 case PKT_RING_TRUST: s="trust"; break;
358 case PKT_PLAINTEXT: s="plaintext"; break;
359 case PKT_COMPRESSED: s="compressed"; break;
360 case PKT_ENCRYPTED: s="encrypted"; break;
361 case PKT_GPG_CONTROL: s="gpg-control"; break;
362 default: s="unknown"; break;
364 fprintf(stderr, "node %p %02x/%02x type=%s",
365 node, node->flag, node->private_flag, s);
366 if( node->pkt->pkttype == PKT_USER_ID ) {
367 PKT_user_id *uid = node->pkt->pkt.user_id;
368 fputs(" \"", stderr);
369 print_string( stderr, uid->name, uid->len, 0 );
370 fprintf (stderr, "\" %c%c%c%c\n",
371 uid->is_expired? 'e':'.',
372 uid->is_revoked? 'r':'.',
373 uid->created? 'v':'.',
374 uid->is_primary? 'p':'.' );
376 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
377 fprintf(stderr, " class=%02x keyid=%08lX ts=%lu\n",
378 node->pkt->pkt.signature->sig_class,
379 (ulong)node->pkt->pkt.signature->keyid[1],
380 (ulong)node->pkt->pkt.signature->timestamp);
382 else if( node->pkt->pkttype == PKT_GPG_CONTROL ) {
383 fprintf(stderr, " ctrl=%d len=%u\n",
384 node->pkt->pkt.gpg_control->control,
385 (unsigned int)node->pkt->pkt.gpg_control->datalen);
387 else if( node->pkt->pkttype == PKT_PUBLIC_KEY
388 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
389 PKT_public_key *pk = node->pkt->pkt.public_key;
390 fprintf(stderr, " keyid=%08lX a=%d u=%d %c%c%c%c\n",
391 (ulong)keyid_from_pk( pk, NULL ),
392 pk->pubkey_algo, pk->pubkey_usage,
393 pk->has_expired? 'e':'.',
394 pk->is_revoked? 'r':'.',
395 pk->is_valid? 'v':'.',
396 pk->mdc_feature? 'm':'.');
398 else
399 fputs("\n", stderr);