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 log_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)
62 log_assert(list != NULL);
63 log_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 log_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 while (packet_list != NULL) {
104 nextpacket = packet_list->next;
105 if (packet_list->packet != NULL) {
106 free_packet(packet_list->packet);
109 packet_list = nextpacket;
114 * free_signedpacket_list - free an OpenPGP signed packet list.
115 * @signedpacket_list: The packet list to free.
117 * Takes an OpenPGP signed packet list structure and frees the memory used
118 * by the packets and signatures it and the linked list overhead.
120 void free_signedpacket_list(
121 struct openpgp_signedpacket_list *signedpacket_list) {
122 struct openpgp_signedpacket_list *nextpacket = NULL;
124 while (signedpacket_list != NULL) {
125 nextpacket = signedpacket_list->next;
126 if (signedpacket_list->packet != NULL) {
127 free_packet(signedpacket_list->packet);
129 if (signedpacket_list->sigs != NULL) {
130 free_packet_list(signedpacket_list->sigs);
132 free(signedpacket_list);
133 signedpacket_list = nextpacket;
138 * free_publickey - free an OpenPGP public key structure.
139 * @key: The key to free.
141 * Takes an OpenPGP key and frees the memory used by all the structures it
144 void free_publickey(struct openpgp_publickey *key) {
145 struct openpgp_publickey *nextkey = NULL;
147 while (key != NULL) {
149 if (key->publickey != NULL) {
150 free_packet(key->publickey);
151 key->publickey = NULL;
153 if (key->sigs != NULL) {
154 free_packet_list(key->sigs);
157 if (key->uids != NULL) {
158 free_signedpacket_list(key->uids);
161 if (key->subkeys != NULL) {
162 free_signedpacket_list(key->subkeys);
171 * free_statskey - free an stats key structure.
172 * @key: The key to free.
174 * Takes a stats key and frees the memory used by it and the linked list
175 * of sigs under it. Doesn't recurse into the list as it's assumed all the
176 * objects referenced also exist in the hash.
178 void free_statskey(struct stats_key *key)
181 if (key->sigs != NULL) {
182 llfree(key->sigs, NULL);
185 if (key->signs != NULL) {
186 llfree(key->signs, NULL);