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