]> git.sommitrealweird.co.uk Git - onak.git/blob - mem.c
cscvs to tla changeset 19
[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 <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "keystructs.h"
14 #include "ll.h"
15 #include "mem.h"
16
17 /**
18  *      packet_dup - duplicate an OpenPGP packet.
19  *      @packet: The packet to duplicate.
20  *
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.
24  */
25 struct openpgp_packet *packet_dup(struct openpgp_packet *packet)
26 {
27         struct openpgp_packet *newpacket = NULL;
28
29         assert(packet != NULL);
30
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,
39                                         newpacket->length);
40                 }
41         }
42
43         return newpacket;
44 }
45
46 /**
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.
51  *
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
55  *      list.
56  */
57 void packet_list_add(struct openpgp_packet_list **list,
58                 struct openpgp_packet_list **list_end,
59                 struct openpgp_packet_list *packet_list)
60 {
61         assert(list != NULL);
62         assert(list_end != NULL);
63
64         for (; packet_list != NULL; packet_list = packet_list->next) {
65                 ADD_PACKET_TO_LIST((*list_end),
66                                 packet_dup(packet_list->packet));
67                 if (*list == NULL) {
68                         *list = *list_end;
69                 }
70         }
71
72         return;
73 }
74
75 /**
76  *      free_packet - free the memory used by an OpenPGP packet.
77  *      @packet: The packet to free.
78  *
79  *      Takes an OpenPGP packet structure and frees the memory used by it,
80  *      including the data part.
81  */
82 void free_packet(struct openpgp_packet *packet) {
83         assert(packet != NULL);
84
85         if (packet->data != NULL) {
86                 free(packet->data);
87                 packet->data = NULL;
88         }
89         free(packet);
90 }
91
92 /**
93  *      free_packet_list - free the memory used by an OpenPGP packet list.
94  *      @packet_list: The packet list to free.
95  *
96  *      Takes an OpenPGP packet list structure and frees the memory used by the
97  *      packets in it and the linked list overhead.
98  */
99 void free_packet_list(struct openpgp_packet_list *packet_list) {
100         struct openpgp_packet_list *nextpacket = NULL;
101
102         assert(packet_list != NULL);
103
104         while (packet_list != NULL) {
105                 nextpacket = packet_list->next;
106                 if (packet_list->packet != NULL) {
107                         free_packet(packet_list->packet);
108                 }
109                 free(packet_list);
110                 packet_list = nextpacket;
111         }
112 }
113
114 /**
115  *      free_signedpacket_list - free an OpenPGP signed packet list.
116  *      @signedpacket_list: The packet list to free.
117  *
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.
120  */
121 void free_signedpacket_list(
122                 struct openpgp_signedpacket_list *signedpacket_list) {
123         struct openpgp_signedpacket_list *nextpacket = NULL;
124
125         assert(signedpacket_list != NULL);
126
127         while (signedpacket_list != NULL) {
128                 nextpacket = signedpacket_list->next;
129                 if (signedpacket_list->packet != NULL) {
130                         free_packet(signedpacket_list->packet);
131                 }
132                 if (signedpacket_list->sigs != NULL) {
133                         free_packet_list(signedpacket_list->sigs);
134                 }
135                 free(signedpacket_list);
136                 signedpacket_list = nextpacket;
137         }
138 }
139
140 /**
141  *      free_publickey - free an OpenPGP public key structure.
142  *      @key: The key to free.
143  *
144  *      Takes an OpenPGP key and frees the memory used by all the structures it
145  *      contains.
146  */
147 void free_publickey(struct openpgp_publickey *key) {
148         struct openpgp_publickey *nextkey = NULL;
149
150         assert(key != NULL);
151
152         while (key != NULL) {
153                 nextkey = key->next;
154                 if (key->publickey != NULL) {
155                         free_packet(key->publickey);
156                         key->publickey = NULL;
157                 }
158                 if (key->revocations != NULL) {
159                         free_packet_list(key->revocations);
160                         key->revocations = NULL;
161                 }
162                 if (key->uids != NULL) {
163                         free_signedpacket_list(key->uids);
164                         key->uids = NULL;
165                 }
166                 if (key->subkeys != NULL) {
167                         free_signedpacket_list(key->subkeys);
168                         key->subkeys = NULL;
169                 }
170                 free(key);
171                 key = nextkey;
172         }
173 }