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 
r_list_create(listp)102 int r_list_create(listp)
103   r_list **listp;
104   {
105     r_list *list=0;
106     int _status;
107 
108     if(!(list=(r_list *)RCALLOC(sizeof(r_list))))
109       ABORT(R_NO_MEMORY);
110 
111     list->first=0;
112     list->last=0;
113     *listp=list;
114 
115     _status=0;
116   abort:
117     return(_status);
118   }
119 
r_list_destroy(listp)120 int r_list_destroy(listp)
121   r_list **listp;
122   {
123     r_list *list;
124     r_list_el *el;
125 
126     if(!listp || !*listp)
127       return(0);
128     list=*listp;
129 
130     el=list->first;
131 
132     while(el){
133       r_list_el *el_t;
134 
135       if(el->destroy && el->data)
136         el->destroy(&el->data);
137       el_t=el;
138       el=el->next;
139       RFREE(el_t);
140     }
141 
142     RFREE(list);
143     *listp=0;
144 
145     return(0);
146   }
147 
r_list_copy(outp,in)148 int r_list_copy(outp,in)
149   r_list**outp;
150   r_list *in;
151   {
152     r_list *out=0;
153     r_list_el *el,*el2,*last=0;
154     int r, _status;
155 
156     if(!in){
157       *outp=0;
158       return(0);
159     }
160 
161     if(r=r_list_create(&out))
162       ABORT(r);
163 
164     for(el=in->first;el;el=el->next){
165       if(!(el2=(r_list_el *)RCALLOC(sizeof(r_list_el))))
166         ABORT(R_NO_MEMORY);
167 
168       if(el->copy && el->data){
169         if(r=el->copy(&el2->data,el->data))
170           ABORT(r);
171       }
172 
173       el2->copy=el->copy;
174       el2->destroy=el->destroy;
175 
176       if(!(out->first))
177         out->first=el2;
178 
179       el2->prev=last;
180       if(last) last->next=el2;
181       last=el2;
182     }
183 
184     out->last=last;
185 
186     *outp=out;
187 
188     _status=0;
189   abort:
190     if(_status)
191       r_list_destroy(&out);
192     return(_status);
193   }
194 
r_list_insert(list,value,copy,destroy)195 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     r_list_el *el=0;
202     int _status;
203 
204     if(!(el=(r_list_el *)RCALLOC(sizeof(r_list_el))))
205       ABORT(R_NO_MEMORY);
206     el->data=value;
207     el->copy=copy;
208     el->destroy=destroy;
209 
210     el->prev=0;
211     el->next=list->first;
212     if(list->first){
213       list->first->prev=el;
214     }
215     list->first=el;
216 
217     _status=0;
218   abort:
219     return(_status);
220   }
221 
r_list_append(list,value,copy,destroy)222 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     r_list_el *el=0;
229     int _status;
230 
231     if(!(el=(r_list_el *)RCALLOC(sizeof(r_list_el))))
232       ABORT(R_NO_MEMORY);
233     el->data=value;
234     el->copy=copy;
235     el->destroy=destroy;
236 
237     el->prev=list->last;
238     el->next=0;
239 
240     if(list->last) list->last->next=el;
241     else list->first=el;
242 
243     list->last=el;
244 
245     _status=0;
246   abort:
247     return(_status);
248   }
249 
r_list_init_iter(list,iter)250 int r_list_init_iter(list,iter)
251   r_list *list;
252   r_list_iterator *iter;
253   {
254     iter->list=list;
255     iter->ptr=list->first;
256 
257     return(0);
258   }
259 
r_list_iter(iter,val)260 int r_list_iter(iter,val)
261   r_list_iterator *iter;
262   void **val;
263   {
264     if(!iter->ptr)
265       return(R_EOD);
266 
267     *val=iter->ptr->data;
268     iter->ptr=iter->ptr->next;
269 
270     return(0);
271   }
272 
273 
274 
275 
276 
277