Line data Source code
1 : /**
2 : r_list.c
3 :
4 :
5 : Copyright (C) 2002-2003, Network Resonance, Inc.
6 : Copyright (C) 2006, Network Resonance, Inc.
7 : All Rights Reserved
8 :
9 : Redistribution and use in source and binary forms, with or without
10 : modification, are permitted provided that the following conditions
11 : are met:
12 :
13 : 1. Redistributions of source code must retain the above copyright
14 : notice, this list of conditions and the following disclaimer.
15 : 2. Redistributions in binary form must reproduce the above copyright
16 : notice, this list of conditions and the following disclaimer in the
17 : documentation and/or other materials provided with the distribution.
18 : 3. Neither the name of Network Resonance, Inc. nor the name of any
19 : contributors to this software may be used to endorse or promote
20 : products derived from this software without specific prior written
21 : permission.
22 :
23 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
24 : AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 : IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 : ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 : LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 : CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 : SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 : INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 : CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 : ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 : POSSIBILITY OF SUCH DAMAGE.
34 :
35 :
36 : */
37 :
38 : /**
39 : r_list.c
40 :
41 :
42 : Copyright (C) 1999-2000 RTFM, Inc.
43 : All Rights Reserved
44 :
45 : This package is a SSLv3/TLS protocol analyzer written by Eric Rescorla
46 : <ekr@rtfm.com> and licensed by RTFM, Inc.
47 :
48 : Redistribution and use in source and binary forms, with or without
49 : modification, are permitted provided that the following conditions
50 : are met:
51 : 1. Redistributions of source code must retain the above copyright
52 : notice, this list of conditions and the following disclaimer.
53 : 2. Redistributions in binary form must reproduce the above copyright
54 : notice, this list of conditions and the following disclaimer in the
55 : documentation and/or other materials provided with the distribution.
56 : 3. All advertising materials mentioning features or use of this software
57 : must display the following acknowledgement:
58 :
59 : This product includes software developed by Eric Rescorla for
60 : RTFM, Inc.
61 :
62 : 4. Neither the name of RTFM, Inc. nor the name of Eric Rescorla may be
63 : used to endorse or promote products derived from this
64 : software without specific prior written permission.
65 :
66 : THIS SOFTWARE IS PROVIDED BY ERIC RESCORLA AND RTFM, INC. ``AS IS'' AND
67 : ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 : IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 : ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 : FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 : DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 : OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 : HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 : LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 : OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY SUCH DAMAGE.
76 :
77 : $Id: r_list.c,v 1.2 2006/08/16 19:39:17 adamcain Exp $
78 :
79 :
80 : ekr@rtfm.com Tue Jan 19 08:36:39 1999
81 : */
82 :
83 :
84 : static char *RCSSTRING __UNUSED__ ="$Id: r_list.c,v 1.2 2006/08/16 19:39:17 adamcain Exp $";
85 :
86 : #include <r_common.h>
87 : #include "r_list.h"
88 :
89 : typedef struct r_list_el_ {
90 : void *data;
91 : struct r_list_el_ *next;
92 : struct r_list_el_ *prev;
93 : int (*copy)(void **new,void *old);
94 : int (*destroy)(void **ptr);
95 : } r_list_el;
96 :
97 : struct r_list_ {
98 : struct r_list_el_ *first;
99 : struct r_list_el_ *last;
100 : };
101 :
102 0 : int r_list_create(listp)
103 : r_list **listp;
104 : {
105 0 : r_list *list=0;
106 : int _status;
107 :
108 0 : if(!(list=(r_list *)RCALLOC(sizeof(r_list))))
109 0 : ABORT(R_NO_MEMORY);
110 :
111 0 : list->first=0;
112 0 : list->last=0;
113 0 : *listp=list;
114 :
115 0 : _status=0;
116 : abort:
117 0 : return(_status);
118 : }
119 :
120 0 : int r_list_destroy(listp)
121 : r_list **listp;
122 : {
123 : r_list *list;
124 : r_list_el *el;
125 :
126 0 : if(!listp || !*listp)
127 0 : return(0);
128 0 : list=*listp;
129 :
130 0 : el=list->first;
131 :
132 0 : while(el){
133 : r_list_el *el_t;
134 :
135 0 : if(el->destroy && el->data)
136 0 : el->destroy(&el->data);
137 0 : el_t=el;
138 0 : el=el->next;
139 0 : RFREE(el_t);
140 : }
141 :
142 0 : RFREE(list);
143 0 : *listp=0;
144 :
145 0 : return(0);
146 : }
147 :
148 0 : int r_list_copy(outp,in)
149 : r_list**outp;
150 : r_list *in;
151 : {
152 0 : r_list *out=0;
153 0 : r_list_el *el,*el2,*last=0;
154 : int r, _status;
155 :
156 0 : if(!in){
157 0 : *outp=0;
158 0 : return(0);
159 : }
160 :
161 0 : if(r=r_list_create(&out))
162 0 : ABORT(r);
163 :
164 0 : for(el=in->first;el;el=el->next){
165 0 : if(!(el2=(r_list_el *)RCALLOC(sizeof(r_list_el))))
166 0 : ABORT(R_NO_MEMORY);
167 :
168 0 : if(el->copy && el->data){
169 0 : if(r=el->copy(&el2->data,el->data))
170 0 : ABORT(r);
171 : }
172 :
173 0 : el2->copy=el->copy;
174 0 : el2->destroy=el->destroy;
175 :
176 0 : if(!(out->first))
177 0 : out->first=el2;
178 :
179 0 : el2->prev=last;
180 0 : if(last) last->next=el2;
181 0 : last=el2;
182 : }
183 :
184 0 : out->last=last;
185 :
186 0 : *outp=out;
187 :
188 0 : _status=0;
189 : abort:
190 0 : if(_status)
191 0 : r_list_destroy(&out);
192 0 : return(_status);
193 : }
194 :
195 0 : int r_list_insert(list,value,copy,destroy)
196 : r_list *list;
197 : void *value;
198 : int (*copy)(void **out, void *in);
199 : int (*destroy)(void **val);
200 : {
201 0 : r_list_el *el=0;
202 : int _status;
203 :
204 0 : if(!(el=(r_list_el *)RCALLOC(sizeof(r_list_el))))
205 0 : ABORT(R_NO_MEMORY);
206 0 : el->data=value;
207 0 : el->copy=copy;
208 0 : el->destroy=destroy;
209 :
210 0 : el->prev=0;
211 0 : el->next=list->first;
212 0 : if(list->first){
213 0 : list->first->prev=el;
214 : }
215 0 : list->first=el;
216 :
217 0 : _status=0;
218 : abort:
219 0 : return(_status);
220 : }
221 :
222 0 : int r_list_append(list,value,copy,destroy)
223 : r_list *list;
224 : void *value;
225 : int (*copy)(void **out, void *in);
226 : int (*destroy)(void **val);
227 : {
228 0 : r_list_el *el=0;
229 : int _status;
230 :
231 0 : if(!(el=(r_list_el *)RCALLOC(sizeof(r_list_el))))
232 0 : ABORT(R_NO_MEMORY);
233 0 : el->data=value;
234 0 : el->copy=copy;
235 0 : el->destroy=destroy;
236 :
237 0 : el->prev=list->last;
238 0 : el->next=0;
239 :
240 0 : if(list->last) list->last->next=el;
241 0 : else list->first=el;
242 :
243 0 : list->last=el;
244 :
245 0 : _status=0;
246 : abort:
247 0 : return(_status);
248 : }
249 :
250 0 : int r_list_init_iter(list,iter)
251 : r_list *list;
252 : r_list_iterator *iter;
253 : {
254 0 : iter->list=list;
255 0 : iter->ptr=list->first;
256 :
257 0 : return(0);
258 : }
259 :
260 0 : int r_list_iter(iter,val)
261 : r_list_iterator *iter;
262 : void **val;
263 : {
264 0 : if(!iter->ptr)
265 0 : return(R_EOD);
266 :
267 0 : *val=iter->ptr->data;
268 0 : iter->ptr=iter->ptr->next;
269 :
270 0 : return(0);
271 : }
272 :
273 :
274 :
275 :
276 :
|