Only seed database for Debian install if we're using default config
[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
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "keystructs.h"
14 #include "ll.h"
15 #include "log.h"
16 #include "mem.h"
17
18 /**
19  *      packet_dup - duplicate an OpenPGP packet.
20  *      @packet: The packet to duplicate.
21  *
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.
25  */
26 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
27 {
28         struct openpgp_packet *newpacket = NULL;
29
30         log_assert(packet != NULL);
31
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,
40                                         newpacket->length);
41                 }
42         }
43
44         return newpacket;
45 }
46
47 /**
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.
52  *
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
56  *      list.
57  */
58 void packet_list_add(struct openpgp_packet_list **list,
59                 struct openpgp_packet_list **list_end,
60                 struct openpgp_packet_list *packet_list)
61 {
62         log_assert(list != NULL);
63         log_assert(list_end != NULL);
64
65         for (; packet_list != NULL; packet_list = packet_list->next) {
66                 ADD_PACKET_TO_LIST((*list_end),
67                                 packet_dup(packet_list->packet));
68                 if (*list == NULL) {
69                         *list = *list_end;
70                 }
71         }
72
73         return;
74 }
75
76 /**
77  *      free_packet - free the memory used by an OpenPGP packet.
78  *      @packet: The packet to free.
79  *
80  *      Takes an OpenPGP packet structure and frees the memory used by it,
81  *      including the data part.
82  */
83 void free_packet(struct openpgp_packet *packet) {
84         log_assert(packet != NULL);
85
86         if (packet->data != NULL) {
87                 free(packet->data);
88                 packet->data = NULL;
89         }
90         free(packet);
91 }
92
93 /**
94  *      free_packet_list - free the memory used by an OpenPGP packet list.
95  *      @packet_list: The packet list to free.
96  *
97  *      Takes an OpenPGP packet list structure and frees the memory used by the
98  *      packets in it and the linked list overhead.
99  */
100 void free_packet_list(struct openpgp_packet_list *packet_list) {
101         struct openpgp_packet_list *nextpacket = NULL;
102
103         while (packet_list != NULL) {
104                 nextpacket = packet_list->next;
105                 if (packet_list->packet != NULL) {
106                         free_packet(packet_list->packet);
107                 }
108                 free(packet_list);
109                 packet_list = nextpacket;
110         }
111 }
112
113 /**
114  *      free_signedpacket_list - free an OpenPGP signed packet list.
115  *      @signedpacket_list: The packet list to free.
116  *
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.
119  */
120 void free_signedpacket_list(
121                 struct openpgp_signedpacket_list *signedpacket_list) {
122         struct openpgp_signedpacket_list *nextpacket = NULL;
123
124         while (signedpacket_list != NULL) {
125                 nextpacket = signedpacket_list->next;
126                 if (signedpacket_list->packet != NULL) {
127                         free_packet(signedpacket_list->packet);
128                 }
129                 if (signedpacket_list->sigs != NULL) {
130                         free_packet_list(signedpacket_list->sigs);
131                 }
132                 free(signedpacket_list);
133                 signedpacket_list = nextpacket;
134         }
135 }
136
137 /**
138  *      free_publickey - free an OpenPGP public key structure.
139  *      @key: The key to free.
140  *
141  *      Takes an OpenPGP key and frees the memory used by all the structures it
142  *      contains.
143  */
144 void free_publickey(struct openpgp_publickey *key) {
145         struct openpgp_publickey *nextkey = NULL;
146
147         while (key != NULL) {
148                 nextkey = key->next;
149                 if (key->publickey != NULL) {
150                         free_packet(key->publickey);
151                         key->publickey = NULL;
152                 }
153                 if (key->sigs != NULL) {
154                         free_packet_list(key->sigs);
155                         key->sigs = NULL;
156                 }
157                 if (key->uids != NULL) {
158                         free_signedpacket_list(key->uids);
159                         key->uids = NULL;
160                 }
161                 if (key->subkeys != NULL) {
162                         free_signedpacket_list(key->subkeys);
163                         key->subkeys = NULL;
164                 }
165                 free(key);
166                 key = nextkey;
167         }
168 }
169
170 /**
171  *      free_statskey - free an stats key structure.
172  *      @key: The key to free.
173  *
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.
177  */
178 void free_statskey(struct stats_key *key)
179 {
180         if (key != NULL) {
181                 if (key->sigs != NULL) {
182                         llfree(key->sigs, NULL);
183                         key->sigs = NULL;
184                 }
185                 if (key->signs != NULL) {
186                         llfree(key->signs, NULL);
187                         key->signs = NULL;
188                 }
189                 free(key);
190         }
191 }