forked from samyk/pwnat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
250 lines (210 loc) · 5.86 KB
/
list.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Project: udptunnel
* File: list.c
*
* Copyright (C) 2009 Daniel Meekins
* Contact: dmeekins - gmail
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "list.h"
/*
* Allocates and initializes a new list type to hold objects of obj_sz bytes,
* and uses the cmp, copy, and free functions. If any of the function pointers
* are NULL, they will be set to memcmp, memcpy, or free.
*/
list_t *list_create(int obj_sz,
int (*obj_cmp)(const void *, const void *, size_t),
void* (*obj_copy)(void *, const void *, size_t),
void (*obj_free)(void *))
{
list_t *new;
new = malloc(sizeof(*new));
if(!new)
return NULL;
new->obj_arr = malloc(LIST_INIT_SIZE * sizeof(void *));
if(!new->obj_arr)
{
free(new);
return NULL;
}
new->obj_sz = obj_sz;
new->num_objs = 0;
new->length = LIST_INIT_SIZE;
new->obj_cmp = obj_cmp ? obj_cmp : &memcmp;
new->obj_copy = obj_copy ? obj_copy : &memcpy;
new->obj_free = obj_free ? obj_free : &free;
return new;
}
/*
* Inserts a new object into the list in sorted order. It makes a deep copy
* of the object and returns a pointer to that new object if obj wasn't
* already in the list, or a pointer to the object already in the list.
*/
void *list_add(list_t *list, void *obj)
{
void *o;
void **new_arr;
void *temp;
int i;
/* Check if obj is already in the list, and return it if so */
o = list_get(list, obj);
if(o)
return o;
o = malloc(list->obj_sz);
if(!o)
return NULL;
list->obj_copy(o, obj, list->obj_sz);
/* Resize the object array if needed, doubling the size */
if(list->num_objs == list->length)
{
new_arr = realloc(list->obj_arr, list->length * 2 * sizeof(void *));
if(!new_arr)
{
list->obj_free(o);
return NULL;
}
list->obj_arr = new_arr;
list->length *= 2;
}
/* Insert new object at end of array */
list->obj_arr[list->num_objs++] = o;
/* Move object up in array until list is sorted again */
for(i = list->num_objs-1; i > 0; i--)
{
if(list->obj_cmp(list->obj_arr[i-1], list->obj_arr[i],
list->obj_sz) > 0)
{
temp = list->obj_arr[i-1];
list->obj_arr[i-1] = list->obj_arr[i];
list->obj_arr[i] = temp;
}
else
break; /* since list was sorted before, just need to go this far */
}
return o;
}
/*
* Returns a pointer to the object that matches the one passed, or NULL if
* one wasn't found. Does a simple linear search for now.
*/
void *list_get(list_t *list, void *obj)
{
int i;
i = list_get_index(list, obj);
if(i == -1)
return NULL;
return list->obj_arr[i];
}
/*
* Returns a pointer to the object at position i in the list or NULL if i is
* out of bounds.
*/
void *list_get_at(list_t *list, int i)
{
if(i >= list->num_objs || i < 0)
return NULL;
return list->obj_arr[i];
}
/*
* Gets the index of object that's equal to the passed object.
*/
int list_get_index(list_t *list, void *obj)
{
int i;
for(i = 0; i < list->num_objs; i++)
{
if(list->obj_cmp(list->obj_arr[i], obj, list->obj_sz) == 0)
return i;
}
return -1;
}
/*
* Does a deep copy of src into a newly created list, and returns a pointer to
* the new list.
*/
list_t *list_copy(list_t *src)
{
list_t *dst;
int i;
dst = malloc(sizeof(*dst));
if(!dst)
return NULL;
memcpy(dst, src, sizeof(*src));
/* Create the pointer array */
dst->obj_arr = malloc(sizeof(void *) * src->length);
if(!dst->obj_arr)
{
free(dst);
return NULL;
}
/* Make copies of all the objects in the src array */
for(i = 0; i < src->num_objs; i++)
{
dst->obj_arr[i] = malloc(dst->obj_sz);
if(!dst->obj_arr[i])
{
dst->num_objs = i; /* so only will free objs up to this point */
list_free(dst);
return NULL;
}
dst->obj_copy(dst->obj_arr[i], src->obj_arr[i], dst->obj_sz);
}
return dst;
}
/*
* Calls a function 'action', passing each object in the list to it, one at
* a time.
*/
void list_action(list_t *list, void (*action)(void *))
{
int i;
for(i = 0; i < list->num_objs; i++)
action(list->obj_arr[i]);
}
/*
* Removes the object from the list that compares equally to obj.
*/
void list_delete(list_t *list, void *obj)
{
list_delete_at(list, list_get_index(list, obj));
}
/*
* Removes and frees an object from the list at the specified index
*/
void list_delete_at(list_t *list, int i)
{
if(i >= list->num_objs || i < 0)
return;
list->obj_free(list->obj_arr[i]);
/* Shift the rest of the object pointers one to the left */
for(; i < list->num_objs - 1; i++)
list->obj_arr[i] = list->obj_arr[i+1];
list->obj_arr[i] = NULL;
list->num_objs--;
}
/*
* Frees each element in the list and then the list and then the list struct
* itself.
*/
void list_free(list_t *list)
{
int i;
for(i = 0; i < list->num_objs; i++)
list->obj_free(list->obj_arr[i]);
free(list->obj_arr);
free(list);
}