cscvs to tla changeset 77
[onak.git] / mem.c
1 /*
2  * mem.c - Routines to cleanup memory after use.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  *
8  * $Id: mem.c,v 1.5 2003/06/04 20:57:10 noodles Exp $
9  */
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include "keystructs.h"
16 #include "ll.h"
17 #include "mem.h"
18
19 /**
20  *      packet_dup - duplicate an OpenPGP packet.
21  *      @packet: The packet to duplicate.
22  *
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.
26  */
27 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
28 {
29         struct openpgp_packet *newpacket = NULL;
30
31         assert(packet != NULL);
32
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,
41                                         newpacket->length);
42                 }
43         }
44
45         return newpacket;
46 }
47
48 /**
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.
53  *
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
57  *      list.
58  */
59 void packet_list_add(struct openpgp_packet_list **list,
60                 struct openpgp_packet_list **list_end,
61                 struct openpgp_packet_list *packet_list)
62 {
63         assert(list != NULL);
64         assert(list_end != NULL);
65
66         for (; packet_list != NULL; packet_list = packet_list->next) {
67                 ADD_PACKET_TO_LIST((*list_end),
68                                 packet_dup(packet_list->packet));
69                 if (*list == NULL) {
70                         *list = *list_end;
71                 }
72         }
73
74         return;
75 }
76
77 /**
78  *      free_packet - free the memory used by an OpenPGP packet.
79  *      @packet: The packet to free.
80  *
81  *      Takes an OpenPGP packet structure and frees the memory used by it,
82  *      including the data part.
83  */
84 void free_packet(struct openpgp_packet *packet) {
85         assert(packet != NULL);
86
87         if (packet->data != NULL) {
88                 free(packet->data);
89                 packet->data = NULL;
90         }
91         free(packet);
92 }
93
94 /**
95  *      free_packet_list - free the memory used by an OpenPGP packet list.
96  *      @packet_list: The packet list to free.
97  *
98  *      Takes an OpenPGP packet list structure and frees the memory used by the
99  *      packets in it and the linked list overhead.
100  */
101 void free_packet_list(struct openpgp_packet_list *packet_list) {
102         struct openpgp_packet_list *nextpacket = NULL;
103
104         assert(packet_list != NULL);
105
106         while (packet_list != NULL) {
107                 nextpacket = packet_list->next;
108                 if (packet_list->packet != NULL) {
109                         free_packet(packet_list->packet);
110                 }
111                 free(packet_list);
112                 packet_list = nextpacket;
113         }
114 }
115
116 /**
117  *      free_signedpacket_list - free an OpenPGP signed packet list.
118  *      @signedpacket_list: The packet list to free.
119  *
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.
122  */
123 void free_signedpacket_list(
124                 struct openpgp_signedpacket_list *signedpacket_list) {
125         struct openpgp_signedpacket_list *nextpacket = NULL;
126
127         assert(signedpacket_list != NULL);
128
129         while (signedpacket_list != NULL) {
130                 nextpacket = signedpacket_list->next;
131                 if (signedpacket_list->packet != NULL) {
132                         free_packet(signedpacket_list->packet);
133                 }
134                 if (signedpacket_list->sigs != NULL) {
135                         free_packet_list(signedpacket_list->sigs);
136                 }
137                 free(signedpacket_list);
138                 signedpacket_list = nextpacket;
139         }
140 }
141
142 /**
143  *      free_publickey - free an OpenPGP public key structure.
144  *      @key: The key to free.
145  *
146  *      Takes an OpenPGP key and frees the memory used by all the structures it
147  *      contains.
148  */
149 void free_publickey(struct openpgp_publickey *key) {
150         struct openpgp_publickey *nextkey = NULL;
151
152         assert(key != NULL);
153
154         while (key != NULL) {
155                 nextkey = key->next;
156                 if (key->publickey != NULL) {
157                         free_packet(key->publickey);
158                         key->publickey = NULL;
159                 }
160                 if (key->revocations != NULL) {
161                         free_packet_list(key->revocations);
162                         key->revocations = NULL;
163                 }
164                 if (key->uids != NULL) {
165                         free_signedpacket_list(key->uids);
166                         key->uids = NULL;
167                 }
168                 if (key->subkeys != NULL) {
169                         free_signedpacket_list(key->subkeys);
170                         key->subkeys = NULL;
171                 }
172                 free(key);
173                 key = nextkey;
174         }
175 }
176
177 /**
178  *      free_statskey - free an stats key structure.
179  *      @key: The key to free.
180  *
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.
184  */
185 void free_statskey(struct stats_key *key)
186 {
187         if (key != NULL) {
188                 if (key->sigs != NULL) {
189                         llfree(key->sigs, NULL);
190                         key->sigs = NULL;
191                 }
192                 if (key->signs != NULL) {
193                         llfree(key->signs, NULL);
194                         key->signs = NULL;
195                 }
196                 free(key);
197         }
198 }