2 * mem.c - Routines to cleanup memory after use.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
8 * $Id: mem.c,v 1.6 2003/06/07 13:45:35 noodles Exp $
16 #include "keystructs.h"
21 * packet_dup - duplicate an OpenPGP packet.
22 * @packet: The packet to duplicate.
24 * This function takes an OpenPGP packet structure and duplicates it,
25 * including the data part. It returns NULL if there is a problem
26 * allocating memory for the duplicate.
28 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
30 struct openpgp_packet *newpacket = NULL;
32 assert(packet != NULL);
34 newpacket = malloc(sizeof (struct openpgp_packet));
35 if (newpacket != NULL) {
36 newpacket->tag = packet->tag;
37 newpacket->newformat = packet->newformat;
38 newpacket->length = packet->length;
39 newpacket->data = malloc(newpacket->length);
40 if (newpacket->data != NULL) {
41 memcpy(newpacket->data, packet->data,
50 * packet_list_add - Adds an OpenPGP packet list to another.
51 * @list: The packet list to add to.
52 * @list_end: The end of the packet list to add to.
53 * @packet_list: The packet list to add.
55 * This function takes an OpenPGP packet list and adds it to another list,
56 * duplicating it in the process. The list to add to need not exists to
57 * begin with, in which case the function simply duplicates the supplied
60 void packet_list_add(struct openpgp_packet_list **list,
61 struct openpgp_packet_list **list_end,
62 struct openpgp_packet_list *packet_list)
65 assert(list_end != NULL);
67 for (; packet_list != NULL; packet_list = packet_list->next) {
68 ADD_PACKET_TO_LIST((*list_end),
69 packet_dup(packet_list->packet));
79 * free_packet - free the memory used by an OpenPGP packet.
80 * @packet: The packet to free.
82 * Takes an OpenPGP packet structure and frees the memory used by it,
83 * including the data part.
85 void free_packet(struct openpgp_packet *packet) {
86 assert(packet != NULL);
88 if (packet->data != NULL) {
96 * free_packet_list - free the memory used by an OpenPGP packet list.
97 * @packet_list: The packet list to free.
99 * Takes an OpenPGP packet list structure and frees the memory used by the
100 * packets in it and the linked list overhead.
102 void free_packet_list(struct openpgp_packet_list *packet_list) {
103 struct openpgp_packet_list *nextpacket = NULL;
105 assert(packet_list != NULL);
107 while (packet_list != NULL) {
108 nextpacket = packet_list->next;
109 if (packet_list->packet != NULL) {
110 free_packet(packet_list->packet);
113 packet_list = nextpacket;
118 * free_signedpacket_list - free an OpenPGP signed packet list.
119 * @signedpacket_list: The packet list to free.
121 * Takes an OpenPGP signed packet list structure and frees the memory used
122 * by the packets and signatures it and the linked list overhead.
124 void free_signedpacket_list(
125 struct openpgp_signedpacket_list *signedpacket_list) {
126 struct openpgp_signedpacket_list *nextpacket = NULL;
128 assert(signedpacket_list != 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;
155 while (key != NULL) {
157 if (key->publickey != NULL) {
158 free_packet(key->publickey);
159 key->publickey = NULL;
161 if (key->revocations != NULL) {
162 free_packet_list(key->revocations);
163 key->revocations = NULL;
165 if (key->uids != NULL) {
166 free_signedpacket_list(key->uids);
169 if (key->subkeys != NULL) {
170 free_signedpacket_list(key->subkeys);
179 * free_statskey - free an stats key structure.
180 * @key: The key to free.
182 * Takes a stats key and frees the memory used by it and the linked list
183 * of sigs under it. Doesn't recurse into the list as it's assumed all the
184 * objects referenced also exist in the hash.
186 void free_statskey(struct stats_key *key)
189 if (key->sigs != NULL) {
190 llfree(key->sigs, NULL);
193 if (key->signs != NULL) {
194 llfree(key->signs, NULL);