Fix the size of the hash array when checking sig hashes
[onak.git] / mem.c
1 /*
2  * mem.c - Routines to cleanup memory after use.
3  *
4  * Copyright 2002-2004,2007 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "keystructs.h"
25 #include "ll.h"
26 #include "log.h"
27 #include "mem.h"
28
29 /**
30  *      packet_dup - duplicate an OpenPGP packet.
31  *      @packet: The packet to duplicate.
32  *
33  *      This function takes an OpenPGP packet structure and duplicates it,
34  *      including the data part. It returns NULL if there is a problem
35  *      allocating memory for the duplicate.
36  */
37 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
38 {
39         struct openpgp_packet *newpacket = NULL;
40
41         log_assert(packet != NULL);
42
43         newpacket = malloc(sizeof (struct openpgp_packet));
44         if (newpacket != NULL) {
45                 newpacket->tag = packet->tag;
46                 newpacket->newformat = packet->newformat;
47                 newpacket->length = packet->length;
48                 newpacket->data = malloc(newpacket->length);
49                 if (newpacket->data != NULL) {
50                         memcpy(newpacket->data, packet->data,
51                                         newpacket->length);
52                 }
53         }
54
55         return newpacket;
56 }
57
58 /**
59  *      packet_list_add - Adds an OpenPGP packet list to another.
60  *      @list: The packet list to add to.
61  *      @list_end: The end of the packet list to add to.
62  *      @packet_list: The packet list to add.
63  *
64  *      This function takes an OpenPGP packet list and adds it to another list,
65  *      duplicating it in the process. The list to add to need not exists to
66  *      begin with, in which case the function simply duplicates the supplied
67  *      list.
68  */
69 void packet_list_add(struct openpgp_packet_list **list,
70                 struct openpgp_packet_list **list_end,
71                 struct openpgp_packet_list *packet_list)
72 {
73         log_assert(list != NULL);
74         log_assert(list_end != NULL);
75
76         for (; packet_list != NULL; packet_list = packet_list->next) {
77                 ADD_PACKET_TO_LIST((*list_end),
78                                 packet_dup(packet_list->packet));
79                 if (*list == NULL) {
80                         *list = *list_end;
81                 }
82         }
83
84         return;
85 }
86
87 /**
88  *      free_packet - free the memory used by an OpenPGP packet.
89  *      @packet: The packet to free.
90  *
91  *      Takes an OpenPGP packet structure and frees the memory used by it,
92  *      including the data part.
93  */
94 void free_packet(struct openpgp_packet *packet) {
95         log_assert(packet != NULL);
96
97         if (packet->data != NULL) {
98                 free(packet->data);
99                 packet->data = NULL;
100         }
101         free(packet);
102 }
103
104 /**
105  *      free_packet_list - free the memory used by an OpenPGP packet list.
106  *      @packet_list: The packet list to free.
107  *
108  *      Takes an OpenPGP packet list structure and frees the memory used by the
109  *      packets in it and the linked list overhead.
110  */
111 void free_packet_list(struct openpgp_packet_list *packet_list) {
112         struct openpgp_packet_list *nextpacket = NULL;
113
114         while (packet_list != NULL) {
115                 nextpacket = packet_list->next;
116                 if (packet_list->packet != NULL) {
117                         free_packet(packet_list->packet);
118                 }
119                 free(packet_list);
120                 packet_list = nextpacket;
121         }
122 }
123
124 /**
125  *      free_signedpacket_list - free an OpenPGP signed packet list.
126  *      @signedpacket_list: The packet list to free.
127  *
128  *      Takes an OpenPGP signed packet list structure and frees the memory used
129  *      by the packets and signatures it and the linked list overhead.
130  */
131 void free_signedpacket_list(
132                 struct openpgp_signedpacket_list *signedpacket_list) {
133         struct openpgp_signedpacket_list *nextpacket = NULL;
134
135         while (signedpacket_list != NULL) {
136                 nextpacket = signedpacket_list->next;
137                 if (signedpacket_list->packet != NULL) {
138                         free_packet(signedpacket_list->packet);
139                 }
140                 if (signedpacket_list->sigs != NULL) {
141                         free_packet_list(signedpacket_list->sigs);
142                 }
143                 free(signedpacket_list);
144                 signedpacket_list = nextpacket;
145         }
146 }
147
148 /**
149  *      free_publickey - free an OpenPGP public key structure.
150  *      @key: The key to free.
151  *
152  *      Takes an OpenPGP key and frees the memory used by all the structures it
153  *      contains.
154  */
155 void free_publickey(struct openpgp_publickey *key) {
156         struct openpgp_publickey *nextkey = NULL;
157
158         while (key != NULL) {
159                 nextkey = key->next;
160                 if (key->publickey != NULL) {
161                         free_packet(key->publickey);
162                         key->publickey = NULL;
163                 }
164                 if (key->sigs != NULL) {
165                         free_packet_list(key->sigs);
166                         key->sigs = NULL;
167                 }
168                 if (key->uids != NULL) {
169                         free_signedpacket_list(key->uids);
170                         key->uids = NULL;
171                 }
172                 if (key->subkeys != NULL) {
173                         free_signedpacket_list(key->subkeys);
174                         key->subkeys = NULL;
175                 }
176                 free(key);
177                 key = nextkey;
178         }
179 }
180
181 /**
182  *      free_statskey - free an stats key structure.
183  *      @key: The key to free.
184  *
185  *      Takes a stats key and frees the memory used by it and the linked list
186  *      of sigs under it. Doesn't recurse into the list as it's assumed all the
187  *      objects referenced also exist in the hash.
188  */
189 void free_statskey(struct stats_key *key)
190 {
191         if (key != NULL) {
192                 if (key->sigs != NULL) {
193                         llfree(key->sigs, NULL);
194                         key->sigs = NULL;
195                 }
196                 if (key->signs != NULL) {
197                         llfree(key->signs, NULL);
198                         key->signs = NULL;
199                 }
200                 free(key);
201         }
202 }