Line data Source code
1 : /* trust-item.c - Trust item objects.
2 : Copyright (C) 2000 Werner Koch (dd9jn)
3 : Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
4 :
5 : This file is part of GPGME.
6 :
7 : GPGME is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU Lesser General Public License as
9 : published by the Free Software Foundation; either version 2.1 of
10 : the License, or (at your option) any later version.
11 :
12 : GPGME is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this program; if not, write to the Free Software
19 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 : 02111-1307, USA. */
21 :
22 : #if HAVE_CONFIG_H
23 : #include <config.h>
24 : #endif
25 : #include <stdlib.h>
26 : #include <string.h>
27 : #include <assert.h>
28 : #include <errno.h>
29 :
30 : #include "util.h"
31 : #include "ops.h"
32 : #include "sema.h"
33 : #include "debug.h"
34 :
35 :
36 : /* Protects all reference counters in trust items. All other accesses
37 : to a trust item are either read only or happen before the trust
38 : item is available to the user. */
39 : DEFINE_STATIC_LOCK (trust_item_ref_lock);
40 :
41 :
42 : /* Create a new trust item. */
43 : gpgme_error_t
44 0 : _gpgme_trust_item_new (gpgme_trust_item_t *r_item)
45 : {
46 : gpgme_trust_item_t item;
47 :
48 0 : item = calloc (1, sizeof *item);
49 0 : if (!item)
50 0 : return gpg_error_from_syserror ();
51 0 : item->_refs = 1;
52 0 : item->keyid = item->_keyid;
53 0 : item->_keyid[16] = '\0';
54 0 : item->owner_trust = item->_owner_trust;
55 0 : item->_owner_trust[1] = '\0';
56 0 : item->validity = item->_validity;
57 0 : item->_validity[1] = '\0';
58 0 : *r_item = item;
59 0 : return 0;
60 : }
61 :
62 :
63 : /* Acquire a reference to ITEM. */
64 : void
65 0 : gpgme_trust_item_ref (gpgme_trust_item_t item)
66 : {
67 0 : LOCK (trust_item_ref_lock);
68 0 : item->_refs++;
69 0 : UNLOCK (trust_item_ref_lock);
70 0 : }
71 :
72 :
73 : /* gpgme_trust_item_unref releases the trust item object. Note that
74 : this function may not do an actual release if there are other
75 : shallow copies of the object. You have to call this function for
76 : every newly created trust item object as well as for every
77 : gpgme_trust_item_ref() done on the trust item object. */
78 : void
79 0 : gpgme_trust_item_unref (gpgme_trust_item_t item)
80 : {
81 0 : LOCK (trust_item_ref_lock);
82 0 : assert (item->_refs > 0);
83 0 : if (--item->_refs)
84 : {
85 0 : UNLOCK (trust_item_ref_lock);
86 0 : return;
87 : }
88 0 : UNLOCK (trust_item_ref_lock);
89 :
90 0 : if (item->name)
91 0 : free (item->name);
92 0 : free (item);
93 : }
94 :
95 :
96 : /* Compatibility interfaces. */
97 : void
98 0 : gpgme_trust_item_release (gpgme_trust_item_t item)
99 : {
100 0 : gpgme_trust_item_unref (item);
101 0 : }
102 :
103 : /* Return the value of the attribute WHAT of ITEM, which has to be
104 : representable by a string. */
105 0 : const char *gpgme_trust_item_get_string_attr (gpgme_trust_item_t item,
106 : _gpgme_attr_t what,
107 : const void *reserved, int idx)
108 : {
109 0 : const char *val = NULL;
110 :
111 0 : if (!item)
112 0 : return NULL;
113 0 : if (reserved)
114 0 : return NULL;
115 0 : if (idx)
116 0 : return NULL;
117 :
118 0 : switch (what)
119 : {
120 : case GPGME_ATTR_KEYID:
121 0 : val = item->keyid;
122 0 : break;
123 :
124 : case GPGME_ATTR_OTRUST:
125 0 : val = item->owner_trust;
126 0 : break;
127 :
128 : case GPGME_ATTR_VALIDITY:
129 0 : val = item->validity;
130 0 : break;
131 :
132 : case GPGME_ATTR_USERID:
133 0 : val = item->name;
134 0 : break;
135 :
136 : default:
137 0 : break;
138 : }
139 0 : return val;
140 : }
141 :
142 :
143 : /* Return the value of the attribute WHAT of KEY, which has to be
144 : representable by an integer. IDX specifies a running index if the
145 : attribute appears more than once in the key. */
146 0 : int gpgme_trust_item_get_int_attr (gpgme_trust_item_t item, _gpgme_attr_t what,
147 : const void *reserved, int idx)
148 : {
149 0 : int val = 0;
150 :
151 0 : if (!item)
152 0 : return 0;
153 0 : if (reserved)
154 0 : return 0;
155 0 : if (idx)
156 0 : return 0;
157 :
158 0 : switch (what)
159 : {
160 : case GPGME_ATTR_LEVEL:
161 0 : val = item->level;
162 0 : break;
163 :
164 : case GPGME_ATTR_TYPE:
165 0 : val = item->type;
166 0 : break;
167 :
168 : default:
169 0 : break;
170 : }
171 0 : return val;
172 : }
|