2 * mem.c - Routines to cleanup memory after use.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
13 #include "keystructs.h"
18 * packet_dup - duplicate an OpenPGP packet.
19 * @packet: The packet to duplicate.
21 * This function takes an OpenPGP packet structure and duplicates it,
22 * including the data part. It returns NULL if there is a problem
23 * allocating memory for the duplicate.
25 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
27 struct openpgp_packet *newpacket = NULL;
29 assert(packet != NULL);
31 newpacket = malloc(sizeof (struct openpgp_packet));
32 if (newpacket != NULL) {
33 newpacket->tag = packet->tag;
34 newpacket->newformat = packet->newformat;
35 newpacket->length = packet->length;
36 newpacket->data = malloc(newpacket->length);
37 if (newpacket->data != NULL) {
38 memcpy(newpacket->data, packet->data,
47 * packet_list_add - Adds an OpenPGP packet list to another.
48 * @list: The packet list to add to.
49 * @list_end: The end of the packet list to add to.
50 * @packet_list: The packet list to add.
52 * This function takes an OpenPGP packet list and adds it to another list,
53 * duplicating it in the process. The list to add to need not exists to
54 * begin with, in which case the function simply duplicates the supplied
57 void packet_list_add(struct openpgp_packet_list **list,
58 struct openpgp_packet_list **list_end,
59 struct openpgp_packet_list *packet_list)
62 assert(list_end != NULL);
64 for (; packet_list != NULL; packet_list = packet_list->next) {
65 ADD_PACKET_TO_LIST((*list_end),
66 packet_dup(packet_list->packet));
76 * free_packet - free the memory used by an OpenPGP packet.
77 * @packet: The packet to free.
79 * Takes an OpenPGP packet structure and frees the memory used by it,
80 * including the data part.
82 void free_packet(struct openpgp_packet *packet) {
83 assert(packet != NULL);
85 if (packet->data != NULL) {
93 * free_packet_list - free the memory used by an OpenPGP packet list.
94 * @packet_list: The packet list to free.
96 * Takes an OpenPGP packet list structure and frees the memory used by the
97 * packets in it and the linked list overhead.
99 void free_packet_list(struct openpgp_packet_list *packet_list) {
100 struct openpgp_packet_list *nextpacket = NULL;
102 assert(packet_list != NULL);
104 while (packet_list != NULL) {
105 nextpacket = packet_list->next;
106 if (packet_list->packet != NULL) {
107 free_packet(packet_list->packet);
110 packet_list = nextpacket;
115 * free_signedpacket_list - free an OpenPGP signed packet list.
116 * @signedpacket_list: The packet list to free.
118 * Takes an OpenPGP signed packet list structure and frees the memory used
119 * by the packets and signatures it and the linked list overhead.
121 void free_signedpacket_list(
122 struct openpgp_signedpacket_list *signedpacket_list) {
123 struct openpgp_signedpacket_list *nextpacket = NULL;
125 assert(signedpacket_list != NULL);
127 while (signedpacket_list != NULL) {
128 nextpacket = signedpacket_list->next;
129 if (signedpacket_list->packet != NULL) {
130 free_packet(signedpacket_list->packet);
132 if (signedpacket_list->sigs != NULL) {
133 free_packet_list(signedpacket_list->sigs);
135 free(signedpacket_list);
136 signedpacket_list = nextpacket;
141 * free_publickey - free an OpenPGP public key structure.
142 * @key: The key to free.
144 * Takes an OpenPGP key and frees the memory used by all the structures it
147 void free_publickey(struct openpgp_publickey *key) {
148 struct openpgp_publickey *nextkey = NULL;
152 while (key != NULL) {
154 if (key->publickey != NULL) {
155 free_packet(key->publickey);
156 key->publickey = NULL;
158 if (key->revocations != NULL) {
159 free_packet_list(key->revocations);
160 key->revocations = NULL;
162 if (key->uids != NULL) {
163 free_signedpacket_list(key->uids);
166 if (key->subkeys != NULL) {
167 free_signedpacket_list(key->subkeys);