[BB#63] conf: Allow multiple Listen statements in the config.
[obnox/tinyproxy.git] / src / vector.c
1 /* tinyproxy - A fast light-weight HTTP proxy
2  * Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /* A vector implementation.  The vector can be of an arbitrary length, and
20  * the data for each entry is an lump of data (the size is stored in the
21  * vector.)
22  */
23
24 #include "main.h"
25
26 #include "heap.h"
27 #include "vector.h"
28
29 /*
30  * These structures are the storage for the "vector".  Entries are
31  * stored in struct vectorentry_s (the data and the length), and the
32  * "vector" structure is implemented as a linked-list.  The struct
33  * vector_s stores a pointer to the first vector (vector[0]) and a
34  * count of the number of entries (or how long the vector is.)
35  */
36 struct vectorentry_s {
37         void *data;
38         size_t len;
39
40         struct vectorentry_s *next;
41 };
42
43 struct vector_s {
44         size_t num_entries;
45         struct vectorentry_s *head;
46         struct vectorentry_s *tail;
47 };
48
49 /*
50  * Create an vector.  The vector initially has no elements and no
51  * storage has been allocated for the entries.
52  *
53  * A NULL is returned if memory could not be allocated for the
54  * vector.
55  */
56 vector_t vector_create (void)
57 {
58         vector_t vector;
59
60         vector = (vector_t) safemalloc (sizeof (struct vector_s));
61         if (!vector)
62                 return NULL;
63
64         vector->num_entries = 0;
65         vector->head = vector->tail = NULL;
66
67         return vector;
68 }
69
70 /*
71  * Deletes an vector.  All the entries when this function is run.
72  *
73  * Returns: 0 on success
74  *          negative if a NULL vector is supplied
75  */
76 int vector_delete (vector_t vector)
77 {
78         struct vectorentry_s *ptr, *next;
79
80         if (!vector)
81                 return -EINVAL;
82
83         ptr = vector->head;
84         while (ptr) {
85                 next = ptr->next;
86                 safefree (ptr->data);
87                 safefree (ptr);
88
89                 ptr = next;
90         }
91
92         safefree (vector);
93
94         return 0;
95 }
96
97 /*
98  * Appends an entry into the vector.  The entry is an arbitrary
99  * collection of bytes of _len_ octets.  The data is copied into the
100  * vector, so the original data must be freed to avoid a memory leak.
101  * The "data" must be non-NULL and the "len" must be greater than zero.
102  * "pos" is either 0 to prepend the data, or 1 to append the data.
103  *
104  * Returns: 0 on success
105  *          negative number if there are errors
106  */
107
108 typedef enum {
109         INSERT_PREPEND,
110         INSERT_APPEND
111 } vector_pos_t;
112
113 static int
114 vector_insert (vector_t      vector,
115                void         *data,
116                size_t        len,
117                vector_pos_t  pos)
118 {
119         struct vectorentry_s *entry;
120
121         if (!vector || !data || len <= 0 ||
122             (pos != INSERT_PREPEND && pos != INSERT_APPEND))
123                 return -EINVAL;
124
125         entry =
126             (struct vectorentry_s *) safemalloc (sizeof (struct vectorentry_s));
127         if (!entry)
128                 return -ENOMEM;
129
130         entry->data = safemalloc (len);
131         if (!entry->data) {
132                 safefree (entry);
133                 return -ENOMEM;
134         }
135
136         memcpy (entry->data, data, len);
137         entry->len = len;
138         entry->next = NULL;
139
140         /* If there is no head or tail, create them */
141         if (!vector->head && !vector->tail)
142                 vector->head = vector->tail = entry;
143         else if (pos == INSERT_PREPEND) {
144                 /* prepend the entry */
145                 entry->next = vector->head;
146                 vector->head = entry;
147         } else {
148                 /* append the entry */
149                 vector->tail->next = entry;
150                 vector->tail = entry;
151         }
152
153         vector->num_entries++;
154
155         return 0;
156 }
157
158 /*
159  * The following two function are used to make the API clearer.  As you
160  * can see they simply call the vector_insert() function with appropriate
161  * arguments.
162  */
163 int vector_append (vector_t vector, void *data, size_t len)
164 {
165         return vector_insert (vector, data, len, INSERT_APPEND);
166 }
167
168 int vector_prepend (vector_t vector, void *data, size_t len)
169 {
170         return vector_insert (vector, data, len, INSERT_PREPEND);
171 }
172
173 /*
174  * A pointer to the data at position "pos" (zero based) is returned.
175  * If the vector is out of bound, data is set to NULL.
176  *
177  * Returns: negative upon an error
178  *          length of data if position is valid
179  */
180 void *vector_getentry (vector_t vector, size_t pos, size_t * size)
181 {
182         struct vectorentry_s *ptr;
183         size_t loc;
184
185         if (!vector || pos >= vector->num_entries)
186                 return NULL;
187
188         loc = 0;
189         ptr = vector->head;
190
191         while (loc != pos) {
192                 ptr = ptr->next;
193                 loc++;
194         }
195
196         if (size)
197                 *size = ptr->len;
198
199         return ptr->data;
200 }
201
202 /*
203  * Returns the number of entries (or the length) of the vector.
204  *
205  * Returns: negative if vector is not valid
206  *          positive length of vector otherwise
207  */
208 ssize_t vector_length (vector_t vector)
209 {
210         if (!vector)
211                 return -EINVAL;
212
213         return vector->num_entries;
214 }