Release 0.2.1
[gpgme.git] / gpgme / signers.c
1 /* signers.c - maintain signer sets
2  *      Copyright (C) 2001 Werner Koch (dd9jn)
3  *      Copyright (C) 2001 g10 Code GmbH
4  *
5  * This file is part of GPGME.
6  *
7  * GPGME is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * GPGME is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26
27 #include "util.h"
28 #include "context.h"
29 #include "rungpg.h"
30
31 /* The signers are directly stored in the context.
32  * So this is quite different to a recipient set.
33  */
34
35
36 void
37 gpgme_signers_clear (GpgmeCtx c)
38 {
39     int i;
40
41     return_if_fail (c);
42
43     if (!c->signers)
44         return;
45     for (i=0; i < c->signers_size; i++ ) {
46         if (!c->signers[i])
47             break;
48         gpgme_key_unref (c->signers[i]);
49         c->signers[i] = NULL;
50     }
51 }
52
53
54 GpgmeError
55 gpgme_signers_add (GpgmeCtx c, const GpgmeKey key)
56 {
57     int i = 0;
58
59     if (!c || !key)
60         return mk_error (Invalid_Value);
61
62     if (!c->signers)
63         c->signers_size = 0;
64
65     for (i=0; i < c->signers_size && c->signers[i]; i++ )
66         ;
67     if ( !(i < c->signers_size) ) {
68         GpgmeKey *newarr;
69         int j;
70         int n = c->signers_size + 5;
71
72         newarr = xtrycalloc ( n, sizeof *newarr );
73         if ( !newarr )
74             return mk_error (Out_Of_Core);
75         for (j=0; j < c->signers_size; j++ )
76             newarr[j] = c->signers[j];
77         c->signers_size = n;
78         xfree (c->signers);
79         c->signers = newarr;
80     }
81     gpgme_key_ref (key);
82     c->signers[i] = key;
83     return 0;
84 }
85
86
87 GpgmeKey
88 gpgme_signers_enum (const GpgmeCtx c, int seq )
89 {
90     int i;
91
92     return_null_if_fail (c);
93     return_null_if_fail (seq>=0);
94
95     if (!c->signers)
96         c->signers_size = 0;
97     for (i=0; i < c->signers_size && c->signers[i]; i++ ) {
98         if (i==seq) {
99             gpgme_key_ref (c->signers[i]);
100             return c->signers[i];
101         }
102     }
103     return NULL;
104 }
105
106
107
108