Update Debian Vcs-* fields to point to git repository
[onak.git] / mem.c
1 /*
2  * mem.c - Routines to cleanup memory after use.
3  *
4  * Copyright 2002-2004,2007 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "keystructs.h"
25 #include "ll.h"
26 #include "mem.h"
27 #include "stats.h"
28
29 /**
30  *      packet_dup - duplicate an OpenPGP packet.
31  *      @packet: The packet to duplicate.
32  *
33  *      This function takes an OpenPGP packet structure and duplicates it,
34  *      including the data part. It returns NULL if there is a problem
35  *      allocating memory for the duplicate.
36  */
37 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
38 {
39         struct openpgp_packet *newpacket = NULL;
40
41         if (packet == NULL)
42                 return NULL;
43
44         newpacket = malloc(sizeof (struct openpgp_packet));
45         if (newpacket != NULL) {
46                 newpacket->tag = packet->tag;
47                 newpacket->newformat = packet->newformat;
48                 newpacket->length = packet->length;
49                 newpacket->data = malloc(newpacket->length);
50                 if (newpacket->data != NULL) {
51                         memcpy(newpacket->data, packet->data,
52                                         newpacket->length);
53                 }
54         }
55
56         return newpacket;
57 }
58
59 /**
60  *      packet_list_add - Adds an OpenPGP packet list to another.
61  *      @list: The packet list to add to.
62  *      @list_end: The end of the packet list to add to.
63  *      @packet_list: The packet list to add.
64  *
65  *      This function takes an OpenPGP packet list and adds it to another list,
66  *      duplicating it in the process. The list to add to need not exists to
67  *      begin with, in which case the function simply duplicates the supplied
68  *      list.
69  */
70 void packet_list_add(struct openpgp_packet_list **list,
71                 struct openpgp_packet_list **list_end,
72                 struct openpgp_packet_list *packet_list)
73 {
74         for (; packet_list != NULL; packet_list = packet_list->next) {
75                 ADD_PACKET_TO_LIST((*list_end),
76                                 packet_dup(packet_list->packet));
77                 if (*list == NULL) {
78                         *list = *list_end;
79                 }
80         }
81
82         return;
83 }
84
85 /**
86  *      free_packet - free the memory used by an OpenPGP packet.
87  *      @packet: The packet to free.
88  *
89  *      Takes an OpenPGP packet structure and frees the memory used by it,
90  *      including the data part.
91  */
92 void free_packet(struct openpgp_packet *packet) {
93         if (packet->data != NULL) {
94                 free(packet->data);
95                 packet->data = NULL;
96         }
97         free(packet);
98 }
99
100 /**
101  *      free_packet_list - free the memory used by an OpenPGP packet list.
102  *      @packet_list: The packet list to free.
103  *
104  *      Takes an OpenPGP packet list structure and frees the memory used by the
105  *      packets in it and the linked list overhead.
106  */
107 void free_packet_list(struct openpgp_packet_list *packet_list) {
108         struct openpgp_packet_list *nextpacket = NULL;
109
110         while (packet_list != NULL) {
111                 nextpacket = packet_list->next;
112                 if (packet_list->packet != NULL) {
113                         free_packet(packet_list->packet);
114                 }
115                 free(packet_list);
116                 packet_list = nextpacket;
117         }
118 }
119
120 /**
121  *      free_signedpacket_list - free an OpenPGP signed packet list.
122  *      @signedpacket_list: The packet list to free.
123  *
124  *      Takes an OpenPGP signed packet list structure and frees the memory used
125  *      by the packets and signatures it and the linked list overhead.
126  */
127 void free_signedpacket_list(
128                 struct openpgp_signedpacket_list *signedpacket_list) {
129         struct openpgp_signedpacket_list *nextpacket = NULL;
130
131         while (signedpacket_list != NULL) {
132                 nextpacket = signedpacket_list->next;
133                 if (signedpacket_list->packet != NULL) {
134                         free_packet(signedpacket_list->packet);
135                 }
136                 if (signedpacket_list->sigs != NULL) {
137                         free_packet_list(signedpacket_list->sigs);
138                 }
139                 free(signedpacket_list);
140                 signedpacket_list = nextpacket;
141         }
142 }
143
144 /**
145  *      free_publickey - free an OpenPGP public key structure.
146  *      @key: The key to free.
147  *
148  *      Takes an OpenPGP key and frees the memory used by all the structures it
149  *      contains.
150  */
151 void free_publickey(struct openpgp_publickey *key) {
152         struct openpgp_publickey *nextkey = NULL;
153
154         while (key != NULL) {
155                 nextkey = key->next;
156                 if (key->publickey != NULL) {
157                         free_packet(key->publickey);
158                         key->publickey = NULL;
159                 }
160                 if (key->sigs != NULL) {
161                         free_packet_list(key->sigs);
162                         key->sigs = NULL;
163                 }
164                 if (key->uids != NULL) {
165                         free_signedpacket_list(key->uids);
166                         key->uids = NULL;
167                 }
168                 if (key->subkeys != NULL) {
169                         free_signedpacket_list(key->subkeys);
170                         key->subkeys = NULL;
171                 }
172                 free(key);
173                 key = nextkey;
174         }
175 }
176
177 /**
178  *      free_statskey - free an stats key structure.
179  *      @key: The key to free.
180  *
181  *      Takes a stats key and frees the memory used by it and the linked list
182  *      of sigs under it. Doesn't recurse into the list as it's assumed all the
183  *      objects referenced also exist in the hash.
184  */
185 void free_statskey(struct stats_key *key)
186 {
187         if (key != NULL) {
188                 if (key->sigs != NULL) {
189                         llfree(key->sigs, NULL);
190                         key->sigs = NULL;
191                 }
192                 if (key->signs != NULL) {
193                         llfree(key->signs, NULL);
194                         key->signs = NULL;
195                 }
196                 free(key);
197         }
198 }