cscvs to tla changeset 83
[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.6 2003/06/07 13:45:35 noodles Exp $
9  */
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "keystructs.h"
17 #include "ll.h"
18 #include "mem.h"
19
20 /**
21  *      packet_dup - duplicate an OpenPGP packet.
22  *      @packet: The packet to duplicate.
23  *
24  *      This function takes an OpenPGP packet structure and duplicates it,
25  *      including the data part. It returns NULL if there is a problem
26  *      allocating memory for the duplicate.
27  */
28 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
29 {
30         struct openpgp_packet *newpacket = NULL;
31
32         assert(packet != NULL);
33
34         newpacket = malloc(sizeof (struct openpgp_packet));
35         if (newpacket != NULL) {
36                 newpacket->tag = packet->tag;
37                 newpacket->newformat = packet->newformat;
38                 newpacket->length = packet->length;
39                 newpacket->data = malloc(newpacket->length);
40                 if (newpacket->data != NULL) {
41                         memcpy(newpacket->data, packet->data,
42                                         newpacket->length);
43                 }
44         }
45
46         return newpacket;
47 }
48
49 /**
50  *      packet_list_add - Adds an OpenPGP packet list to another.
51  *      @list: The packet list to add to.
52  *      @list_end: The end of the packet list to add to.
53  *      @packet_list: The packet list to add.
54  *
55  *      This function takes an OpenPGP packet list and adds it to another list,
56  *      duplicating it in the process. The list to add to need not exists to
57  *      begin with, in which case the function simply duplicates the supplied
58  *      list.
59  */
60 void packet_list_add(struct openpgp_packet_list **list,
61                 struct openpgp_packet_list **list_end,
62                 struct openpgp_packet_list *packet_list)
63 {
64         assert(list != NULL);
65         assert(list_end != NULL);
66
67         for (; packet_list != NULL; packet_list = packet_list->next) {
68                 ADD_PACKET_TO_LIST((*list_end),
69                                 packet_dup(packet_list->packet));
70                 if (*list == NULL) {
71                         *list = *list_end;
72                 }
73         }
74
75         return;
76 }
77
78 /**
79  *      free_packet - free the memory used by an OpenPGP packet.
80  *      @packet: The packet to free.
81  *
82  *      Takes an OpenPGP packet structure and frees the memory used by it,
83  *      including the data part.
84  */
85 void free_packet(struct openpgp_packet *packet) {
86         assert(packet != NULL);
87
88         if (packet->data != NULL) {
89                 free(packet->data);
90                 packet->data = NULL;
91         }
92         free(packet);
93 }
94
95 /**
96  *      free_packet_list - free the memory used by an OpenPGP packet list.
97  *      @packet_list: The packet list to free.
98  *
99  *      Takes an OpenPGP packet list structure and frees the memory used by the
100  *      packets in it and the linked list overhead.
101  */
102 void free_packet_list(struct openpgp_packet_list *packet_list) {
103         struct openpgp_packet_list *nextpacket = NULL;
104
105         assert(packet_list != NULL);
106
107         while (packet_list != NULL) {
108                 nextpacket = packet_list->next;
109                 if (packet_list->packet != NULL) {
110                         free_packet(packet_list->packet);
111                 }
112                 free(packet_list);
113                 packet_list = nextpacket;
114         }
115 }
116
117 /**
118  *      free_signedpacket_list - free an OpenPGP signed packet list.
119  *      @signedpacket_list: The packet list to free.
120  *
121  *      Takes an OpenPGP signed packet list structure and frees the memory used
122  *      by the packets and signatures it and the linked list overhead.
123  */
124 void free_signedpacket_list(
125                 struct openpgp_signedpacket_list *signedpacket_list) {
126         struct openpgp_signedpacket_list *nextpacket = NULL;
127
128         assert(signedpacket_list != NULL);
129
130         while (signedpacket_list != NULL) {
131                 nextpacket = signedpacket_list->next;
132                 if (signedpacket_list->packet != NULL) {
133                         free_packet(signedpacket_list->packet);
134                 }
135                 if (signedpacket_list->sigs != NULL) {
136                         free_packet_list(signedpacket_list->sigs);
137                 }
138                 free(signedpacket_list);
139                 signedpacket_list = nextpacket;
140         }
141 }
142
143 /**
144  *      free_publickey - free an OpenPGP public key structure.
145  *      @key: The key to free.
146  *
147  *      Takes an OpenPGP key and frees the memory used by all the structures it
148  *      contains.
149  */
150 void free_publickey(struct openpgp_publickey *key) {
151         struct openpgp_publickey *nextkey = NULL;
152
153         assert(key != NULL);
154
155         while (key != NULL) {
156                 nextkey = key->next;
157                 if (key->publickey != NULL) {
158                         free_packet(key->publickey);
159                         key->publickey = NULL;
160                 }
161                 if (key->revocations != NULL) {
162                         free_packet_list(key->revocations);
163                         key->revocations = NULL;
164                 }
165                 if (key->uids != NULL) {
166                         free_signedpacket_list(key->uids);
167                         key->uids = NULL;
168                 }
169                 if (key->subkeys != NULL) {
170                         free_signedpacket_list(key->subkeys);
171                         key->subkeys = NULL;
172                 }
173                 free(key);
174                 key = nextkey;
175         }
176 }
177
178 /**
179  *      free_statskey - free an stats key structure.
180  *      @key: The key to free.
181  *
182  *      Takes a stats key and frees the memory used by it and the linked list
183  *      of sigs under it. Doesn't recurse into the list as it's assumed all the
184  *      objects referenced also exist in the hash.
185  */
186 void free_statskey(struct stats_key *key)
187 {
188         if (key != NULL) {
189                 if (key->sigs != NULL) {
190                         llfree(key->sigs, NULL);
191                         key->sigs = NULL;
192                 }
193                 if (key->signs != NULL) {
194                         llfree(key->signs, NULL);
195                         key->signs = NULL;
196                 }
197                 free(key);
198         }
199 }