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.5 2003/06/04 20:57:10 noodles Exp $
15 #include "keystructs.h"
20 * packet_dup - duplicate an OpenPGP packet.
21 * @packet: The packet to duplicate.
23 * This function takes an OpenPGP packet structure and duplicates it,
24 * including the data part. It returns NULL if there is a problem
25 * allocating memory for the duplicate.
27 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
29 struct openpgp_packet *newpacket = NULL;
31 assert(packet != NULL);
33 newpacket = malloc(sizeof (struct openpgp_packet));
34 if (newpacket != NULL) {
35 newpacket->tag = packet->tag;
36 newpacket->newformat = packet->newformat;
37 newpacket->length = packet->length;
38 newpacket->data = malloc(newpacket->length);
39 if (newpacket->data != NULL) {
40 memcpy(newpacket->data, packet->data,
49 * packet_list_add - Adds an OpenPGP packet list to another.
50 * @list: The packet list to add to.
51 * @list_end: The end of the packet list to add to.
52 * @packet_list: The packet list to add.
54 * This function takes an OpenPGP packet list and adds it to another list,
55 * duplicating it in the process. The list to add to need not exists to
56 * begin with, in which case the function simply duplicates the supplied
59 void packet_list_add(struct openpgp_packet_list **list,
60 struct openpgp_packet_list **list_end,
61 struct openpgp_packet_list *packet_list)
64 assert(list_end != NULL);
66 for (; packet_list != NULL; packet_list = packet_list->next) {
67 ADD_PACKET_TO_LIST((*list_end),
68 packet_dup(packet_list->packet));
78 * free_packet - free the memory used by an OpenPGP packet.
79 * @packet: The packet to free.
81 * Takes an OpenPGP packet structure and frees the memory used by it,
82 * including the data part.
84 void free_packet(struct openpgp_packet *packet) {
85 assert(packet != NULL);
87 if (packet->data != NULL) {
95 * free_packet_list - free the memory used by an OpenPGP packet list.
96 * @packet_list: The packet list to free.
98 * Takes an OpenPGP packet list structure and frees the memory used by the
99 * packets in it and the linked list overhead.
101 void free_packet_list(struct openpgp_packet_list *packet_list) {
102 struct openpgp_packet_list *nextpacket = NULL;
104 assert(packet_list != NULL);
106 while (packet_list != NULL) {
107 nextpacket = packet_list->next;
108 if (packet_list->packet != NULL) {
109 free_packet(packet_list->packet);
112 packet_list = nextpacket;
117 * free_signedpacket_list - free an OpenPGP signed packet list.
118 * @signedpacket_list: The packet list to free.
120 * Takes an OpenPGP signed packet list structure and frees the memory used
121 * by the packets and signatures it and the linked list overhead.
123 void free_signedpacket_list(
124 struct openpgp_signedpacket_list *signedpacket_list) {
125 struct openpgp_signedpacket_list *nextpacket = NULL;
127 assert(signedpacket_list != NULL);
129 while (signedpacket_list != NULL) {
130 nextpacket = signedpacket_list->next;
131 if (signedpacket_list->packet != NULL) {
132 free_packet(signedpacket_list->packet);
134 if (signedpacket_list->sigs != NULL) {
135 free_packet_list(signedpacket_list->sigs);
137 free(signedpacket_list);
138 signedpacket_list = nextpacket;
143 * free_publickey - free an OpenPGP public key structure.
144 * @key: The key to free.
146 * Takes an OpenPGP key and frees the memory used by all the structures it
149 void free_publickey(struct openpgp_publickey *key) {
150 struct openpgp_publickey *nextkey = NULL;
154 while (key != NULL) {
156 if (key->publickey != NULL) {
157 free_packet(key->publickey);
158 key->publickey = NULL;
160 if (key->revocations != NULL) {
161 free_packet_list(key->revocations);
162 key->revocations = NULL;
164 if (key->uids != NULL) {
165 free_signedpacket_list(key->uids);
168 if (key->subkeys != NULL) {
169 free_signedpacket_list(key->subkeys);
178 * free_statskey - free an stats key structure.
179 * @key: The key to free.
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.
185 void free_statskey(struct stats_key *key)
188 if (key->sigs != NULL) {
189 llfree(key->sigs, NULL);
192 if (key->signs != NULL) {
193 llfree(key->signs, NULL);