-
Notifications
You must be signed in to change notification settings - Fork 5
/
keystore.c
59 lines (52 loc) · 1.59 KB
/
keystore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <string.h>
#include "keystore.h"
#include "utils.h"
keystore_t *new_keystore(char *psk_buf)
{
keystore_t *head = (keystore_t *)malloc(sizeof(keystore_t));
if (NULL==head) {
ERR("failed to allocate keystore");
return NULL;
}
memset(head, 0, sizeof(keystore_t));
keystore_t *psk = head;
char *ptr = (char*)psk_buf;
char *psk_str = strtok_r((char*)psk_buf, ",", &ptr);
while (psk_str) {
char *sep = strchr(psk_str, ':');
if (sep) {
//DBG("psk_str=%s", psk_str);
//sep = '\0';
psk->id = (uint8_t*)psk_str;
psk->id_length = sep-psk_str;
psk->key = (uint8_t*)sep+1;
psk->key_length = strlen(sep+1);
psk->next = (keystore_t *)malloc(sizeof(keystore_t));
if (NULL==psk->next) {
ERR("failed to allocate keystore");
return NULL;
}
psk = psk->next;
memset(psk, 0, sizeof(keystore_t));
}
psk_str = strtok_r(NULL, ",", &ptr);
}
for (psk=head; psk && psk->id_length; psk=psk->next) {
//psk->id[psk->id_length] = '\0';
char *sep = strchr((char *)psk->id, ':');
if (sep) {
*sep = '\0';
}
//DBG("%s id=\"%s\", key=\"%s\"", __func__, psk->id, psk->key);
}
return head;
}
void free_keystore(keystore_t *keystore)
{
while (keystore) {
keystore_t *tmp = keystore;
keystore = keystore->next;
free(tmp);
}
}