2 * mem.c - Routines to cleanup memory after use.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
13 #include "keystructs.h"
19 * packet_dup - duplicate an OpenPGP packet.
20 * @packet: The packet to duplicate.
22 * This function takes an OpenPGP packet structure and duplicates it,
23 * including the data part. It returns NULL if there is a problem
24 * allocating memory for the duplicate.
26 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
28 struct openpgp_packet *newpacket = NULL;
30 assert(packet != NULL);
32 newpacket = malloc(sizeof (struct openpgp_packet));
33 if (newpacket != NULL) {
34 newpacket->tag = packet->tag;
35 newpacket->newformat = packet->newformat;
36 newpacket->length = packet->length;
37 newpacket->data = malloc(newpacket->length);
38 if (newpacket->data != NULL) {
39 memcpy(newpacket->data, packet->data,
48 * packet_list_add - Adds an OpenPGP packet list to another.
49 * @list: The packet list to add to.
50 * @list_end: The end of the packet list to add to.
51 * @packet_list: The packet list to add.
53 * This function takes an OpenPGP packet list and adds it to another list,
54 * duplicating it in the process. The list to add to need not exists to
55 * begin with, in which case the function simply duplicates the supplied
58 void packet_list_add(struct openpgp_packet_list **list,
59 struct openpgp_packet_list **list_end,
60 struct openpgp_packet_list *packet_list)
63 assert(list_end != NULL);
65 for (; packet_list != NULL; packet_list = packet_list->next) {
66 ADD_PACKET_TO_LIST((*list_end),
67 packet_dup(packet_list->packet));
77 * free_packet - free the memory used by an OpenPGP packet.
78 * @packet: The packet to free.
80 * Takes an OpenPGP packet structure and frees the memory used by it,
81 * including the data part.
83 void free_packet(struct openpgp_packet *packet) {
84 assert(packet != NULL);
86 if (packet->data != NULL) {
94 * free_packet_list - free the memory used by an OpenPGP packet list.
95 * @packet_list: The packet list to free.
97 * Takes an OpenPGP packet list structure and frees the memory used by the
98 * packets in it and the linked list overhead.
100 void free_packet_list(struct openpgp_packet_list *packet_list) {
101 struct openpgp_packet_list *nextpacket = NULL;
103 assert(packet_list != NULL);
105 while (packet_list != NULL) {
106 nextpacket = packet_list->next;
107 if (packet_list->packet != NULL) {
108 free_packet(packet_list->packet);
111 packet_list = nextpacket;
116 * free_signedpacket_list - free an OpenPGP signed packet list.
117 * @signedpacket_list: The packet list to free.
119 * Takes an OpenPGP signed packet list structure and frees the memory used
120 * by the packets and signatures it and the linked list overhead.
122 void free_signedpacket_list(
123 struct openpgp_signedpacket_list *signedpacket_list) {
124 struct openpgp_signedpacket_list *nextpacket = NULL;
126 assert(signedpacket_list != NULL);
128 while (signedpacket_list != NULL) {
129 nextpacket = signedpacket_list->next;
130 if (signedpacket_list->packet != NULL) {
131 free_packet(signedpacket_list->packet);
133 if (signedpacket_list->sigs != NULL) {
134 free_packet_list(signedpacket_list->sigs);
136 free(signedpacket_list);
137 signedpacket_list = nextpacket;
142 * free_publickey - free an OpenPGP public key structure.
143 * @key: The key to free.
145 * Takes an OpenPGP key and frees the memory used by all the structures it
148 void free_publickey(struct openpgp_publickey *key) {
149 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->revocations != NULL) {
160 free_packet_list(key->revocations);
161 key->revocations = NULL;
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);