2 * mem.c - Routines to cleanup memory after use.
4 * Copyright 2002-2004,2007 Jonathan McDowell <noodles@earth.li>
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.
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
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.
24 #include "keystructs.h"
29 * packet_dup - duplicate an OpenPGP packet.
30 * @packet: The packet to duplicate.
32 * This function takes an OpenPGP packet structure and duplicates it,
33 * including the data part. It returns NULL if there is a problem
34 * allocating memory for the duplicate.
36 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
38 struct openpgp_packet *newpacket = NULL;
43 newpacket = malloc(sizeof (struct openpgp_packet));
44 if (newpacket != NULL) {
45 newpacket->tag = packet->tag;
46 newpacket->newformat = packet->newformat;
47 newpacket->length = packet->length;
48 newpacket->data = malloc(newpacket->length);
49 if (newpacket->data != NULL) {
50 memcpy(newpacket->data, packet->data,
59 * packet_list_add - Adds an OpenPGP packet list to another.
60 * @list: The packet list to add to.
61 * @list_end: The end of the packet list to add to.
62 * @packet_list: The packet list to add.
64 * This function takes an OpenPGP packet list and adds it to another list,
65 * duplicating it in the process. The list to add to need not exists to
66 * begin with, in which case the function simply duplicates the supplied
69 void packet_list_add(struct openpgp_packet_list **list,
70 struct openpgp_packet_list **list_end,
71 struct openpgp_packet_list *packet_list)
73 for (; packet_list != NULL; packet_list = packet_list->next) {
74 ADD_PACKET_TO_LIST((*list_end),
75 packet_dup(packet_list->packet));
85 * free_packet - free the memory used by an OpenPGP packet.
86 * @packet: The packet to free.
88 * Takes an OpenPGP packet structure and frees the memory used by it,
89 * including the data part.
91 void free_packet(struct openpgp_packet *packet) {
92 if (packet->data != NULL) {
100 * free_packet_list - free the memory used by an OpenPGP packet list.
101 * @packet_list: The packet list to free.
103 * Takes an OpenPGP packet list structure and frees the memory used by the
104 * packets in it and the linked list overhead.
106 void free_packet_list(struct openpgp_packet_list *packet_list) {
107 struct openpgp_packet_list *nextpacket = NULL;
109 while (packet_list != NULL) {
110 nextpacket = packet_list->next;
111 if (packet_list->packet != NULL) {
112 free_packet(packet_list->packet);
115 packet_list = nextpacket;
120 * free_signedpacket_list - free an OpenPGP signed packet list.
121 * @signedpacket_list: The packet list to free.
123 * Takes an OpenPGP signed packet list structure and frees the memory used
124 * by the packets and signatures it and the linked list overhead.
126 void free_signedpacket_list(
127 struct openpgp_signedpacket_list *signedpacket_list) {
128 struct openpgp_signedpacket_list *nextpacket = NULL;
130 while (signedpacket_list != NULL) {
131 nextpacket = signedpacket_list->next;
132 if (signedpacket_list->packet != NULL) {
133 free_packet(signedpacket_list->packet);
135 if (signedpacket_list->sigs != NULL) {
136 free_packet_list(signedpacket_list->sigs);
138 free(signedpacket_list);
139 signedpacket_list = nextpacket;
144 * free_publickey - free an OpenPGP public key structure.
145 * @key: The key to free.
147 * Takes an OpenPGP key and frees the memory used by all the structures it
150 void free_publickey(struct openpgp_publickey *key) {
151 struct openpgp_publickey *nextkey = NULL;
153 while (key != NULL) {
155 if (key->publickey != NULL) {
156 free_packet(key->publickey);
157 key->publickey = NULL;
159 if (key->sigs != NULL) {
160 free_packet_list(key->sigs);
163 if (key->uids != NULL) {
164 free_signedpacket_list(key->uids);
167 if (key->subkeys != NULL) {
168 free_signedpacket_list(key->subkeys);
177 * free_statskey - free an stats key structure.
178 * @key: The key to free.
180 * Takes a stats key and frees the memory used by it and the linked list
181 * of sigs under it. Doesn't recurse into the list as it's assumed all the
182 * objects referenced also exist in the hash.
184 void free_statskey(struct stats_key *key)
187 if (key->sigs != NULL) {
188 llfree(key->sigs, NULL);
191 if (key->signs != NULL) {
192 llfree(key->signs, NULL);