1 |
/* kbnode.c - keyblock node utility functions |
/* kbnode.c - keyblock node utility functions |
2 |
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
3 |
* |
* |
4 |
* This file is part of GnuPG. |
* This file is part of GnuPG. |
5 |
* |
* |
6 |
* GnuPG is free software; you can redistribute it and/or modify |
* 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 |
* 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 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
* (at your option) any later version. |
10 |
* |
* |
11 |
* GnuPG is distributed in the hope that it will be useful, |
* GnuPG is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
15 |
* |
* |
16 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include <stdio.h> |
#ifdef HAVE_CONFIG_H |
22 |
#include <string.h> |
#include <config.h> |
23 |
#include <assert.h> |
#endif |
24 |
#include <malloc.h> |
|
25 |
#include <sys/types.h> |
#include <stdio.h> |
26 |
|
#include <stdio.h> |
27 |
#include "openpgp.h" |
#include <string.h> |
28 |
|
#include <assert.h> |
29 |
#define is_deleted_kbnode(a) ((a)->private_flag & 1) |
#include <malloc.h> |
30 |
#define is_cloned_kbnode(a) ((a)->private_flag & 2) |
#include <sys/types.h> |
31 |
|
|
32 |
|
#include "openpgp.h" |
33 |
static gpg_kbnode_t |
|
34 |
alloc_node( void ) |
#define is_deleted_kbnode(a) ((a)->private_flag & 1) |
35 |
{ |
#define is_cloned_kbnode(a) ((a)->private_flag & 2) |
36 |
gpg_kbnode_t n; |
|
37 |
|
|
38 |
n = malloc( sizeof *n ); |
static gpg_kbnode_t |
39 |
n->next = NULL; |
alloc_node( void ) |
40 |
n->pkt = NULL; |
{ |
41 |
n->flag = 0; |
gpg_kbnode_t n; |
42 |
n->private_flag=0; |
|
43 |
n->recno = 0; |
n = malloc( sizeof *n ); |
44 |
return n; |
n->next = NULL; |
45 |
} |
n->pkt = NULL; |
46 |
|
n->flag = 0; |
47 |
static void |
n->private_flag=0; |
48 |
free_node( gpg_kbnode_t n ) |
n->recno = 0; |
49 |
{ |
return n; |
50 |
if( n ) { |
} |
51 |
free( n ); |
|
52 |
} |
static void |
53 |
} |
free_node( gpg_kbnode_t n ) |
54 |
|
{ |
55 |
|
if( n ) { |
56 |
|
free( n ); |
57 |
gpg_kbnode_t |
} |
58 |
gpg_new_kbnode( PACKET *pkt ) |
} |
59 |
{ |
|
60 |
gpg_kbnode_t n = alloc_node(); |
|
61 |
n->pkt = pkt; |
|
62 |
return n; |
gpg_kbnode_t |
63 |
} |
gpg_new_kbnode( PACKET *pkt ) |
64 |
|
{ |
65 |
|
gpg_kbnode_t n = alloc_node(); |
66 |
gpg_kbnode_t |
n->pkt = pkt; |
67 |
gpg_clone_kbnode( gpg_kbnode_t node ) |
return n; |
68 |
{ |
} |
69 |
gpg_kbnode_t n = alloc_node(); |
|
70 |
|
|
71 |
n->pkt = node->pkt; |
gpg_kbnode_t |
72 |
n->private_flag = node->private_flag | 2; /* mark cloned */ |
gpg_clone_kbnode( gpg_kbnode_t node ) |
73 |
return n; |
{ |
74 |
} |
gpg_kbnode_t n = alloc_node(); |
75 |
|
|
76 |
|
n->pkt = node->pkt; |
77 |
void |
n->private_flag = node->private_flag | 2; /* mark cloned */ |
78 |
gpg_release_kbnode( gpg_kbnode_t n ) |
return n; |
79 |
{ |
} |
80 |
gpg_kbnode_t n2; |
|
81 |
|
|
82 |
while( n ) { |
void |
83 |
n2 = n->next; |
gpg_release_kbnode( gpg_kbnode_t n ) |
84 |
if( !is_cloned_kbnode(n) ) { |
{ |
85 |
gpg_free_packet( n->pkt ); |
gpg_kbnode_t n2; |
86 |
free( n->pkt ); |
|
87 |
} |
while( n ) { |
88 |
free_node( n ); |
n2 = n->next; |
89 |
n = n2; |
if( !is_cloned_kbnode(n) ) { |
90 |
} |
gpg_free_packet( n->pkt ); |
91 |
} |
free( n->pkt ); |
92 |
|
} |
93 |
|
free_node( n ); |
94 |
/**************** |
n = n2; |
95 |
* Delete NODE. |
} |
96 |
* Note: This only works with walk_kbnode!! |
} |
97 |
*/ |
|
98 |
void |
|
99 |
gpg_delete_kbnode( gpg_kbnode_t node ) |
/**************** |
100 |
{ |
* Delete NODE. |
101 |
node->private_flag |= 1; |
* Note: This only works with walk_kbnode!! |
102 |
} |
*/ |
103 |
|
void |
104 |
|
gpg_delete_kbnode( gpg_kbnode_t node ) |
105 |
|
{ |
106 |
/**************** |
node->private_flag |= 1; |
107 |
* Append NODE to ROOT. ROOT must exist! |
} |
108 |
*/ |
|
109 |
void |
|
110 |
gpg_add_kbnode( gpg_kbnode_t root, gpg_kbnode_t node ) |
|
111 |
{ |
/**************** |
112 |
gpg_kbnode_t n1; |
* Append NODE to ROOT. ROOT must exist! |
113 |
|
*/ |
114 |
for(n1=root; n1->next; n1 = n1->next) |
void |
115 |
; |
gpg_add_kbnode( gpg_kbnode_t root, gpg_kbnode_t node ) |
116 |
n1->next = node; |
{ |
117 |
} |
gpg_kbnode_t n1; |
118 |
|
|
119 |
|
for(n1=root; n1->next; n1 = n1->next) |
120 |
/**************** |
; |
121 |
* Find the previous node (if PKTTYPE = 0) or the previous node |
n1->next = node; |
122 |
* with pkttype PKTTYPE in the list starting with ROOT of NODE. |
} |
123 |
*/ |
|
124 |
gpg_kbnode_t |
|
125 |
gpg_find_prev_kbnode( gpg_kbnode_t root, gpg_kbnode_t node, int pkttype ) |
/**************** |
126 |
{ |
* Find the previous node (if PKTTYPE = 0) or the previous node |
127 |
gpg_kbnode_t n1; |
* with pkttype PKTTYPE in the list starting with ROOT of NODE. |
128 |
|
*/ |
129 |
for (n1=NULL; root && root != node; root = root->next ) { |
gpg_kbnode_t |
130 |
if (!pkttype ||root->pkt->pkttype == pkttype) |
gpg_find_prev_kbnode( gpg_kbnode_t root, gpg_kbnode_t node, int pkttype ) |
131 |
n1 = root; |
{ |
132 |
} |
gpg_kbnode_t n1; |
133 |
return n1; |
|
134 |
} |
for (n1=NULL; root && root != node; root = root->next ) { |
135 |
|
if (!pkttype ||root->pkt->pkttype == pkttype) |
136 |
/**************** |
n1 = root; |
137 |
* Ditto, but find the next packet. The behaviour is trivial if |
} |
138 |
* PKTTYPE is 0 but if it is specified, the next node with a packet |
return n1; |
139 |
* of this type is returned. The function has some knowledge about |
} |
140 |
* the valid ordering of packets: e.g. if the next signature packet |
|
141 |
* is requested, the function will not return one if it encounters |
/**************** |
142 |
* a user-id. |
* Ditto, but find the next packet. The behaviour is trivial if |
143 |
*/ |
* PKTTYPE is 0 but if it is specified, the next node with a packet |
144 |
gpg_kbnode_t |
* of this type is returned. The function has some knowledge about |
145 |
gpg_find_next_kbnode( gpg_kbnode_t node, int pkttype ) |
* the valid ordering of packets: e.g. if the next signature packet |
146 |
{ |
* is requested, the function will not return one if it encounters |
147 |
for( node=node->next ; node; node = node->next ) { |
* a user-id. |
148 |
if( !pkttype ) |
*/ |
149 |
return node; |
gpg_kbnode_t |
150 |
else if( pkttype == PKT_USER_ID |
gpg_find_next_kbnode( gpg_kbnode_t node, int pkttype ) |
151 |
&& ( node->pkt->pkttype == PKT_PUBLIC_KEY |
{ |
152 |
|| node->pkt->pkttype == PKT_SECRET_KEY ) ) |
for( node=node->next ; node; node = node->next ) { |
153 |
return NULL; |
if( !pkttype ) |
154 |
else if( pkttype == PKT_SIGNATURE |
return node; |
155 |
&& ( node->pkt->pkttype == PKT_USER_ID |
else if( pkttype == PKT_USER_ID |
156 |
|| node->pkt->pkttype == PKT_PUBLIC_KEY |
&& ( node->pkt->pkttype == PKT_PUBLIC_KEY |
157 |
|| node->pkt->pkttype == PKT_SECRET_KEY ) ) |
|| node->pkt->pkttype == PKT_SECRET_KEY ) ) |
158 |
return NULL; |
return NULL; |
159 |
else if( node->pkt->pkttype == pkttype ) |
else if( pkttype == PKT_SIGNATURE |
160 |
return node; |
&& ( node->pkt->pkttype == PKT_USER_ID |
161 |
} |
|| node->pkt->pkttype == PKT_PUBLIC_KEY |
162 |
return NULL; |
|| node->pkt->pkttype == PKT_SECRET_KEY ) ) |
163 |
} |
return NULL; |
164 |
|
else if( node->pkt->pkttype == pkttype ) |
165 |
|
return node; |
166 |
gpg_kbnode_t |
} |
167 |
gpg_find_kbnode( gpg_kbnode_t node, int pkttype ) |
return NULL; |
168 |
{ |
} |
169 |
for( ; node; node = node->next ) { |
|
170 |
if( node->pkt->pkttype == pkttype ) |
|
171 |
return node; |
gpg_kbnode_t |
172 |
} |
gpg_find_kbnode( gpg_kbnode_t node, int pkttype ) |
173 |
return NULL; |
{ |
174 |
} |
for( ; node; node = node->next ) { |
175 |
|
if( node->pkt->pkttype == pkttype ) |
176 |
|
return node; |
177 |
|
} |
178 |
/**************** |
return NULL; |
179 |
* Walk through a list of kbnodes. This function returns |
} |
180 |
* the next kbnode for each call; before using the function the first |
|
181 |
* time, the caller must set CONTEXT to NULL (This has simply the effect |
|
182 |
* to start with ROOT). |
|
183 |
*/ |
/**************** |
184 |
gpg_kbnode_t |
* Walk through a list of kbnodes. This function returns |
185 |
gpg_walk_kbnode( gpg_kbnode_t root, gpg_kbnode_t *context, int all ) |
* the next kbnode for each call; before using the function the first |
186 |
{ |
* time, the caller must set CONTEXT to NULL (This has simply the effect |
187 |
gpg_kbnode_t n; |
* to start with ROOT). |
188 |
|
*/ |
189 |
do { |
gpg_kbnode_t |
190 |
if( !*context ) { |
gpg_walk_kbnode( gpg_kbnode_t root, gpg_kbnode_t *context, int all ) |
191 |
*context = root; |
{ |
192 |
n = root; |
gpg_kbnode_t n; |
193 |
} |
|
194 |
else { |
do { |
195 |
n = (*context)->next; |
if( !*context ) { |
196 |
*context = n; |
*context = root; |
197 |
} |
n = root; |
198 |
} while( !all && n && is_deleted_kbnode(n) ); |
} |
199 |
|
else { |
200 |
return n; |
n = (*context)->next; |
201 |
} |
*context = n; |
202 |
|
} |
203 |
void |
} while( !all && n && is_deleted_kbnode(n) ); |
204 |
gpg_clear_kbnode_flags( gpg_kbnode_t n ) |
|
205 |
{ |
return n; |
206 |
for( ; n; n = n->next ) { |
} |
207 |
n->flag = 0; |
|
208 |
} |
void |
209 |
} |
gpg_clear_kbnode_flags( gpg_kbnode_t n ) |
210 |
|
{ |
211 |
|
for( ; n; n = n->next ) { |
212 |
|
n->flag = 0; |
213 |
|
} |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|