Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
list.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "list.h"
18
19#include <stdlib.h>
20#include <string.h>
21
23
24void list_create(List *list, int item_size, FreeItemFn free_item_fn) {
25
26 list->length = 0;
27 list->item_size = item_size;
28 list->head = NULL;
29 list->tail = NULL;
30 list->free_item_fn = free_item_fn;
31}
32
33void list_destroy(List *list) {
34
35 ListItem *current;
36
37 while (list->head != NULL) {
38 current = list->head;
39 list->head = current->next;
40
41 if (list->free_item_fn) {
42 list->free_item_fn(current->data);
43 }
44
45 insecure_memzero(current->data, list->item_size);
46 free(current->data);
47 free(current);
48 }
49}
50
51bool list_prepend(List *list, void *item) {
52
53 ListItem *node = calloc(1, sizeof(ListItem));
54 if (node == NULL) {
55 return false;
56 }
57
58 node->data = calloc(1, list->item_size);
59
60 if (node->data == NULL) {
61 free(node);
62 return false;
63 }
64
65 memcpy(node->data, item, list->item_size);
66
67 node->next = list->head;
68 list->head = node;
69
70 if (list->tail == NULL) {
71 list->tail = list->head;
72 }
73
74 list->length++;
75 return true;
76}
77
78bool list_append(List *list, void *item) {
79
80 ListItem *node = calloc(1, sizeof(ListItem));
81 if (node == NULL) {
82 return false;
83 }
84
85 node->data = calloc(1, list->item_size);
86 if (node->data == NULL) {
87 free(node);
88 return false;
89 }
90
91 memcpy(node->data, item, list->item_size);
92
93 if (list->length == 0) {
94 list->head = node;
95 list->tail = node;
96 } else {
97 list->tail->next = node;
98 list->tail = node;
99 }
100
101 list->length++;
102 return true;
103}
104
105ListItem *list_get(List *list, void *data, CompareItemFn compare_item_fn) {
106
107 for (ListItem *item = list->head; item != NULL; item = item->next) {
108 if (compare_item_fn(data, item->data) == true) {
109
110 return item;
111 }
112 }
113
114 return NULL;
115}
116
117void list_delete(List *list, ListItem *item) {
118
119 if (item == NULL) {
120
121 return;
122 }
123
124 if (item == list->head) {
125 if (list->head == list->tail) {
126 list->head = NULL;
127 list->tail = NULL;
128 } else {
129 list->head = list->head->next;
130 }
131
132 if (list->free_item_fn) {
133 list->free_item_fn(item->data);
134 }
135 insecure_memzero(item->data, list->item_size);
136 free(item->data);
137 free(item);
138 } else if (item == list->tail) {
139 for (ListItem *i = list->head; i != NULL; i = i->next) {
140 if (i->next == list->tail) {
141 list->tail = i;
142 i->next = NULL;
143
144 if (list->free_item_fn) {
145 list->free_item_fn(item->data);
146 }
147 insecure_memzero(item->data, list->item_size);
148 free(item->data);
149 free(item);
150 }
151 }
152 } else {
153 if (list->free_item_fn) {
154 list->free_item_fn(item->data);
155 }
156
157 ListItem *tmp = item->next;
158
159 insecure_memzero(item->data, list->item_size);
160 free(item->data);
161
162 item->data = item->next->data;
163 item->next = item->next->next;
164
165 if (tmp == list->tail) {
166 list->tail = item;
167 }
168
169 free(tmp);
170 }
171
172 list->length--;
173}
174
175void list_iterate(List *list, IteratorFn iterator_fn) {
176
177 for (ListItem *item = list->head; item != NULL; item = item->next) {
178 iterator_fn(item->data);
179 }
180}
#define insecure_memzero(buf, len)
bool list_append(List *list, void *item)
Definition list.c:78
bool list_prepend(List *list, void *item)
Definition list.c:51
void list_delete(List *list, ListItem *item)
Definition list.c:117
void list_iterate(List *list, IteratorFn iterator_fn)
Definition list.c:175
ListItem * list_get(List *list, void *data, CompareItemFn compare_item_fn)
Definition list.c:105
void list_create(List *list, int item_size, FreeItemFn free_item_fn)
Definition list.c:24
void list_destroy(List *list)
Definition list.c:33
bool(* CompareItemFn)(void *, void *)
Definition list.h:24
void(* IteratorFn)(void *)
Definition list.h:26
void(* FreeItemFn)(void *)
Definition list.h:22
Definition list.h:35
void * data
Definition list.h:31
ListItem * next
Definition list.h:32
memcpy((char *) pInfo->slotDescription, s, l)