summaryrefslogtreecommitdiffhomepage
path: root/src/vector.c
blob: e9ca586727bd02fbdd583ee0069c80a89ff1b4cd (plain)
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
/* tinyproxy - A fast light-weight HTTP proxy
 * Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* A vector implementation.  The vector can be of an arbitrary length, and
 * the data for each entry is an lump of data (the size is stored in the
 * vector.)
 */

#include "tinyproxy.h"

#include "heap.h"
#include "vector.h"

/*
 * These structures are the storage for the "vector".  Entries are
 * stored in struct vectorentry_s (the data and the length), and the
 * "vector" structure is implemented as a linked-list.  The struct
 * vector_s stores a pointer to the first vector (vector[0]) and a
 * count of the number of entries (or how long the vector is.)
 */
struct vectorentry_s {
        void *data;
        size_t len;

        struct vectorentry_s *next;
};

struct vector_s {
        size_t num_entries;
        struct vectorentry_s *head;
        struct vectorentry_s *tail;
};

/*
 * Create an vector.  The vector initially has no elements and no
 * storage has been allocated for the entries.
 *
 * A NULL is returned if memory could not be allocated for the
 * vector.
 */
vector_t
vector_create(void)
{
        vector_t vector;

        vector = safemalloc(sizeof(struct vector_s));
        if (!vector)
                return NULL;

        vector->num_entries = 0;
        vector->head = vector->tail = NULL;

        return vector;
}

/*
 * Deletes an vector.  All the entries when this function is run.
 *
 * Returns: 0 on success
 *          negative if a NULL vector is supplied
 */
int
vector_delete(vector_t vector)
{
        struct vectorentry_s *ptr, *next;

        if (!vector)
                return -EINVAL;

        ptr = vector->head;
        while (ptr) {
                next = ptr->next;
                safefree(ptr->data);
                safefree(ptr);

                ptr = next;
        }

        safefree(vector);

        return 0;
}

/*
 * Appends an entry into the vector.  The entry is an arbitrary
 * collection of bytes of _len_ octets.  The data is copied into the
 * vector, so the original data must be freed to avoid a memory leak.
 * The "data" must be non-NULL and the "len" must be greater than zero.
 * "pos" is either 0 to prepend the data, or 1 to append the data.
 *
 * Returns: 0 on success
 *          negative number if there are errors
 */
#define INSERT_PREPEND 0
#define INSERT_APPEND 1

static int
vector_insert(vector_t vector, void *data, ssize_t len, int pos)
{
        struct vectorentry_s *entry;

        if (!vector || !data || len <= 0 ||
            (pos != INSERT_PREPEND && pos != INSERT_APPEND))
                return -EINVAL;

        entry = safemalloc(sizeof(struct vectorentry_s));
        if (!entry)
                return -ENOMEM;

        entry->data = safemalloc(len);
        if (!entry->data) {
                safefree(entry);
                return -ENOMEM;
        }

        memcpy(entry->data, data, len);
        entry->len = len;
        entry->next = NULL;

        /* If there is no head or tail, create them */
        if (!vector->head && !vector->tail)
                vector->head = vector->tail = entry;
        else if (pos == 0) {
                /* prepend the entry */
                entry->next = vector->head;
                vector->head = entry;
        } else {
                /* append the entry */
                vector->tail->next = entry;
                vector->tail = entry;
        }

        vector->num_entries++;

        return 0;
}

/*
 * The following two function are used to make the API clearer.  As you
 * can see they simply call the vector_insert() function with appropriate
 * arguments.
 */
int
vector_append(vector_t vector, void *data, ssize_t len)
{
        return vector_insert(vector, data, len, INSERT_APPEND);
}

int
vector_prepend(vector_t vector, void *data, ssize_t len)
{
        return vector_insert(vector, data, len, INSERT_PREPEND);
}

/*
 * A pointer to the data at position "pos" (zero based) is returned.
 * If the vector is out of bound, data is set to NULL.
 *
 * Returns: negative upon an error
 *          length of data if position is valid
 */
void *
vector_getentry(vector_t vector, size_t pos, size_t * size)
{
        struct vectorentry_s *ptr;
        size_t loc;

        if (!vector || pos >= vector->num_entries)
                return NULL;

        loc = 0;
        ptr = vector->head;

        while (loc != pos) {
                ptr = ptr->next;
                loc++;
        }

        if (size)
                *size = ptr->len;

        return ptr->data;
}

/*
 * Returns the number of entries (or the length) of the vector.
 *
 * Returns: negative if vector is not valid
 *          positive length of vector otherwise
 */
ssize_t
vector_length(vector_t vector)
{
        if (!vector)
                return -EINVAL;

        return vector->num_entries;
}