vb@4256
|
1 |
// This file is under GNU General Public License 3.0
|
vb@4256
|
2 |
// see LICENSE.txt
|
vb@4256
|
3 |
|
vb@4256
|
4 |
#include "pEp_internal.h"
|
vb@4256
|
5 |
|
vb@4256
|
6 |
#include <stdlib.h>
|
vb@4256
|
7 |
#include <assert.h>
|
vb@4256
|
8 |
#include <string.h>
|
vb@4256
|
9 |
|
vb@4256
|
10 |
#include "keyreset_command.h"
|
vb@4256
|
11 |
|
vb@4290
|
12 |
DYNAMIC_API keyreset_command * new_keyreset_command(const pEp_identity * ident, const char * new_key)
|
vb@4256
|
13 |
{
|
vb@4290
|
14 |
keyreset_command * command = NULL;
|
vb@4256
|
15 |
|
vb@4256
|
16 |
// key and command should not be NULL, that's bad style (while legal)
|
vb@4256
|
17 |
|
vb@4256
|
18 |
assert(ident);
|
vb@4256
|
19 |
assert(new_key);
|
vb@4256
|
20 |
|
vb@4256
|
21 |
command = calloc(1, sizeof(keyreset_command));
|
vb@4256
|
22 |
assert(command);
|
vb@4256
|
23 |
if (command == NULL)
|
vb@4256
|
24 |
goto enomem;
|
vb@4256
|
25 |
|
vb@4256
|
26 |
command->ident = ident ? identity_dup(ident) : new_identity(NULL, NULL, NULL, NULL);
|
vb@4256
|
27 |
if (command->ident == NULL)
|
vb@4256
|
28 |
goto enomem;
|
vb@4290
|
29 |
|
vb@4277
|
30 |
if (command->ident && command->ident->fpr) {
|
vb@4290
|
31 |
// make content uppercase
|
vb@4277
|
32 |
for (size_t i=0; i<strlen(command->ident->fpr); i++)
|
vb@4277
|
33 |
command->ident->fpr[i] = toupper(command->ident->fpr[i]);
|
vb@4277
|
34 |
}
|
vb@4290
|
35 |
|
vb@4256
|
36 |
command->new_key = new_key ? strdup(new_key) : strdup("");
|
vb@4256
|
37 |
assert(command->new_key);
|
vb@4256
|
38 |
if (command->new_key == NULL)
|
vb@4256
|
39 |
goto enomem;
|
vb@4256
|
40 |
|
vb@4290
|
41 |
// make content uppercase
|
vb@4277
|
42 |
for (size_t i=0; i<strlen(command->new_key); i++)
|
vb@4277
|
43 |
command->new_key[i] = toupper(command->new_key[i]);
|
vb@4290
|
44 |
|
vb@4256
|
45 |
return command;
|
vb@4256
|
46 |
|
vb@4256
|
47 |
enomem:
|
vb@4256
|
48 |
free_keyreset_command(command);
|
vb@4256
|
49 |
return NULL;
|
vb@4256
|
50 |
}
|
vb@4256
|
51 |
|
vb@4256
|
52 |
DYNAMIC_API void free_keyreset_command(keyreset_command * command)
|
vb@4256
|
53 |
{
|
vb@4256
|
54 |
if (command) {
|
vb@4256
|
55 |
free_identity(command->ident);
|
vb@4256
|
56 |
free(command->new_key);
|
vb@4256
|
57 |
free(command);
|
vb@4256
|
58 |
}
|
vb@4256
|
59 |
}
|
vb@4256
|
60 |
|
vb@4290
|
61 |
DYNAMIC_API keyreset_command * keyreset_command_dup(const keyreset_command * src)
|
vb@4256
|
62 |
{
|
vb@4256
|
63 |
assert(src);
|
vb@4256
|
64 |
if (src == NULL)
|
vb@4256
|
65 |
return NULL;
|
vb@4256
|
66 |
|
vb@4256
|
67 |
return new_keyreset_command(src->ident, src->new_key);
|
vb@4256
|
68 |
}
|
vb@4256
|
69 |
|
vb@4290
|
70 |
DYNAMIC_API keyreset_command_list * new_keyreset_command_list(keyreset_command * command)
|
vb@4256
|
71 |
{
|
vb@4290
|
72 |
keyreset_command_list * result = calloc(1, sizeof(keyreset_command_list));
|
vb@4256
|
73 |
assert(result);
|
vb@4256
|
74 |
|
vb@4256
|
75 |
if (result && command)
|
vb@4256
|
76 |
result->command = command;
|
vb@4256
|
77 |
|
vb@4256
|
78 |
return result;
|
vb@4256
|
79 |
}
|
vb@4256
|
80 |
|
vb@4290
|
81 |
DYNAMIC_API keyreset_command_list * keyreset_command_list_dup(
|
vb@4290
|
82 |
const keyreset_command_list * src
|
vb@4256
|
83 |
)
|
vb@4256
|
84 |
{
|
vb@4256
|
85 |
assert(src);
|
vb@4256
|
86 |
if (src == NULL)
|
vb@4256
|
87 |
return NULL;
|
vb@4256
|
88 |
|
vb@4290
|
89 |
keyreset_command * cpy = keyreset_command_dup(src->command);
|
vb@4256
|
90 |
|
vb@4290
|
91 |
keyreset_command_list * dst = new_keyreset_command_list(cpy);
|
vb@4256
|
92 |
if (dst == NULL)
|
vb@4256
|
93 |
return NULL;
|
vb@4256
|
94 |
|
vb@4290
|
95 |
keyreset_command_list * src_curr = src->next;
|
vb@4290
|
96 |
keyreset_command_list ** dst_curr_ptr = &dst->next;
|
vb@4256
|
97 |
|
vb@4256
|
98 |
while (src_curr) {
|
vb@4290
|
99 |
cpy = keyreset_command_dup(src_curr->command);
|
vb@4290
|
100 |
if (cpy == NULL) {
|
vb@4256
|
101 |
free_keyreset_command_list(dst);
|
vb@4256
|
102 |
return NULL;
|
vb@4256
|
103 |
}
|
vb@4290
|
104 |
*dst_curr_ptr = new_keyreset_command_list(cpy);
|
vb@4256
|
105 |
if (*dst_curr_ptr == NULL) {
|
vb@4290
|
106 |
free_keyreset_command(cpy);
|
vb@4256
|
107 |
free_keyreset_command_list(dst);
|
vb@4256
|
108 |
return NULL;
|
vb@4256
|
109 |
}
|
vb@4256
|
110 |
src_curr = src_curr->next;
|
vb@4256
|
111 |
dst_curr_ptr = &((*dst_curr_ptr)->next);
|
vb@4256
|
112 |
}
|
vb@4256
|
113 |
|
vb@4256
|
114 |
return dst;
|
vb@4256
|
115 |
}
|
vb@4256
|
116 |
|
vb@4290
|
117 |
DYNAMIC_API keyreset_command_list * keyreset_command_list_add(
|
vb@4290
|
118 |
keyreset_command_list * command_list,
|
vb@4290
|
119 |
keyreset_command * command
|
vb@4256
|
120 |
)
|
vb@4256
|
121 |
{
|
vb@4256
|
122 |
assert(command);
|
vb@4256
|
123 |
|
vb@4256
|
124 |
// empty list (no nodes)
|
vb@4256
|
125 |
if (command_list == NULL)
|
vb@4256
|
126 |
return new_keyreset_command_list(command);
|
vb@4256
|
127 |
|
vb@4256
|
128 |
// empty list (one node, no command)
|
vb@4256
|
129 |
if (command_list->command == NULL) {
|
vb@4256
|
130 |
if (command_list->next)
|
vb@4256
|
131 |
return NULL; // invalid list
|
vb@4256
|
132 |
|
vb@4256
|
133 |
command_list->command = command;
|
vb@4256
|
134 |
assert(command_list->command);
|
vb@4256
|
135 |
|
vb@4256
|
136 |
if (command_list->command == NULL)
|
vb@4256
|
137 |
return NULL;
|
vb@4256
|
138 |
|
vb@4256
|
139 |
return command_list;
|
vb@4256
|
140 |
}
|
vb@4256
|
141 |
|
vb@4290
|
142 |
keyreset_command_list * list_curr = command_list;
|
vb@4256
|
143 |
|
vb@4256
|
144 |
while (list_curr->next)
|
vb@4256
|
145 |
list_curr = list_curr->next;
|
vb@4256
|
146 |
|
vb@4256
|
147 |
list_curr->next = new_keyreset_command_list(command);
|
vb@4256
|
148 |
|
vb@4256
|
149 |
assert(list_curr->next);
|
vb@4256
|
150 |
if (list_curr->next == NULL)
|
vb@4256
|
151 |
return NULL;
|
vb@4256
|
152 |
|
vb@4256
|
153 |
return list_curr->next;
|
vb@4256
|
154 |
}
|
vb@4256
|
155 |
|
vb@4290
|
156 |
DYNAMIC_API keyreset_command_list * keyreset_command_list_append(
|
vb@4290
|
157 |
keyreset_command_list * command_list,
|
vb@4290
|
158 |
keyreset_command_list * second
|
vb@4256
|
159 |
)
|
vb@4256
|
160 |
{
|
vb@4256
|
161 |
assert(command_list);
|
vb@4256
|
162 |
if (command_list == NULL)
|
vb@4256
|
163 |
return NULL;
|
vb@4256
|
164 |
|
vb@4256
|
165 |
// second list is empty
|
vb@4256
|
166 |
if (second == NULL || second->command == NULL)
|
vb@4256
|
167 |
return command_list;
|
vb@4256
|
168 |
|
vb@4290
|
169 |
keyreset_command_list * _s = command_list;
|
vb@4290
|
170 |
for (keyreset_command_list * _s2 = second; _s2 != NULL; _s2 = _s2->next) {
|
vb@4290
|
171 |
keyreset_command * _sp = keyreset_command_dup(_s2->command);
|
vb@4256
|
172 |
if (_sp == NULL)
|
vb@4256
|
173 |
return NULL;
|
vb@4256
|
174 |
_s = keyreset_command_list_add(_s, _sp);
|
vb@4256
|
175 |
if (_s == NULL){
|
vb@4256
|
176 |
free_keyreset_command(_sp);
|
vb@4256
|
177 |
return NULL;
|
vb@4256
|
178 |
}
|
vb@4256
|
179 |
}
|
vb@4256
|
180 |
return _s;
|
vb@4256
|
181 |
}
|
vb@4256
|
182 |
|
vb@4256
|
183 |
DYNAMIC_API int keyreset_command_list_length(
|
vb@4290
|
184 |
const keyreset_command_list * command_list
|
vb@4256
|
185 |
)
|
vb@4256
|
186 |
{
|
vb@4256
|
187 |
int len = 0;
|
vb@4256
|
188 |
|
vb@4290
|
189 |
for (const keyreset_command_list * _sl = command_list; _sl && _sl->command; _sl = _sl->next)
|
vb@4256
|
190 |
len++;
|
vb@4256
|
191 |
|
vb@4256
|
192 |
return len;
|
vb@4256
|
193 |
}
|
vb@4256
|
194 |
|
vb@4290
|
195 |
DYNAMIC_API void free_keyreset_command_list(keyreset_command_list * command_list)
|
vb@4256
|
196 |
{
|
vb@4256
|
197 |
if (command_list) {
|
vb@4256
|
198 |
free_keyreset_command_list(command_list->next);
|
vb@4256
|
199 |
free_keyreset_command(command_list->command);
|
vb@4256
|
200 |
free(command_list);
|
vb@4256
|
201 |
}
|
vb@4256
|
202 |
}
|
vb@4256
|
203 |
|