]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-list.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * asn_list.c - borrowed from Murray Goldberg
22 * the following routines implement the list data structure
24 * Copyright (C) 1992 the University of British Columbia
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
30 * If you modify this file, you must clearly indicate your changes.
32 * This source code is distributed in the hope that it will be
33 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
34 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/src/asn-list.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
37 * $Log: asn-list.c,v $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
44 * Revision 1.1.1.1 1999/03/16 18:06:30 aram
45 * Originals from SMIME Free Library.
47 * Revision 1.2 1995/07/27 08:59:36 rj
48 * merged GetAsnListElmt(), a function used only by the type table code.
50 * changed `_' to `-' in file names.
52 * Revision 1.1 1994/08/28 09:45:55 rj
53 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
57 #include "asn-config.h"
60 /* remove the entire list and all its nodes (not the actual list data elmts) */
61 /* this is set up for the snace compiler */
63 AsnListFree
PARAMS ((list
),
66 AsnListNode
*node
, *next
;
81 * this routine removes the current node from the list. After removal the
82 * current pointer will point to the next node in line, or NULL if the
83 * removed item was at the tail of the list.
86 AsnListRemove
PARAMS ((list
),
94 list
->curr
->next
->prev
= list
->curr
->prev
;
96 list
->last
= list
->curr
->prev
;
99 list
->curr
->prev
->next
= list
->curr
->next
;
101 list
->first
= list
->curr
->next
;
105 list
->curr
= list
->curr
->next
;
113 * this creates a new node after the current node and returns the
114 * address of the memory allocated for data. The current pointer is changed
115 * to point to the newly added node in the list. If the current pointer is
116 * initially off the list then this operation fails.
119 AsnListAdd
PARAMS ((list
),
122 AsnListNode
*newNode
;
127 newNode
= (AsnListNode
*) Asn1Alloc (sizeof (AsnListNode
) + list
->dataSize
);
128 dataAddr
= (void *) &(newNode
->data
);
130 newNode
->next
= list
->curr
->next
;
131 newNode
->prev
= list
->curr
;
132 if (list
->curr
->next
)
133 list
->curr
->next
->prev
= newNode
;
135 list
->last
= newNode
;
136 list
->curr
->next
= newNode
;
138 list
->curr
= newNode
;
149 * this creates a new node before the current node and returns the
150 * address of the memory allocated for data. The current pointer is changed
151 * to point to the newly added node in the list. If the current pointer is
152 * initially off the list then this operation fails.
155 AsnListInsert
PARAMS ((list
),
158 AsnListNode
*newNode
;
163 newNode
= (AsnListNode
*) Asn1Alloc (sizeof (AsnListNode
) + list
->dataSize
);
164 dataAddr
= (void *) &(newNode
->data
);
166 newNode
->next
= list
->curr
;
167 newNode
->prev
= list
->curr
->prev
;
168 if (list
->curr
->prev
)
169 list
->curr
->prev
->next
= newNode
;
171 list
->first
= newNode
;
172 list
->curr
->prev
= newNode
;
174 list
->curr
= newNode
;
186 AsnListInit
PARAMS ((list
, dataSize
),
190 list
->first
= list
->last
= list
->curr
= NULL
;
192 list
->dataSize
= dataSize
;
198 AsnListNew
PARAMS ((dataSize
),
203 list
= (AsnList
*) Asn1Alloc (sizeof (AsnList
));
204 list
->first
= list
->last
= list
->curr
= NULL
;
206 list
->dataSize
= dataSize
;
212 * backs up the current pointer by one and returns the data address of the new
213 * current node. If the current pointer is off the list, the new current node
214 * will be the last node of the list (unless the list is empty).
217 AsnListPrev
PARAMS ((list
),
222 if (list
->curr
== NULL
)
223 list
->curr
= list
->last
;
225 list
->curr
= list
->curr
->prev
;
227 if (list
->curr
== NULL
)
230 retVal
= (void *) &(list
->curr
->data
);
236 * advances the current pointer by one and returns the data address of the new
237 * current node. If the current pointer is off the list, the new current node
238 * will be the first node of the list (unless the list is empty).
241 AsnListNext
PARAMS ((list
),
246 if (list
->curr
== NULL
)
247 list
->curr
= list
->first
;
249 list
->curr
= list
->curr
->next
;
251 if (list
->curr
== NULL
)
254 retVal
= (void *) &(list
->curr
->data
);
260 * returns the data address of the last node (if there is one) and sets the
261 * current pointer to this node.
264 AsnListLast
PARAMS ((list
),
269 list
->curr
= list
->last
;
271 if (list
->curr
== NULL
)
274 retVal
= (void *) &(list
->curr
->data
);
280 * returns the data address of the first node (if there is one) and sets the
281 * current pointer to this node.
284 AsnListFirst
PARAMS ((list
),
289 list
->curr
= list
->first
;
291 if (list
->curr
== NULL
)
294 retVal
= (void *) &(list
->curr
->data
);
300 * this creates a new node at the beginning of the list and returns the
301 * address of the memory allocated for data. The current pointer is changed
302 * to point to the newly added node in the list.
305 AsnListPrepend
PARAMS ((list
),
308 AsnListNode
*newNode
;
311 newNode
= (AsnListNode
*) Asn1Alloc (sizeof (AsnListNode
) + list
->dataSize
);
312 dataAddr
= (void *) &(newNode
->data
);
314 newNode
->prev
= NULL
;
316 if (list
->first
== NULL
)
318 newNode
->next
= NULL
;
319 list
->first
= list
->last
= newNode
;
323 newNode
->next
= list
->first
;
324 list
->first
->prev
= newNode
;
325 list
->first
= newNode
;
328 list
->curr
= newNode
;
335 * this creates a new node at the end of the list and returns the
336 * address of the memory allocated for data. The current pointer is changed
337 * to point to the newly added node in the list.
340 AsnListAppend
PARAMS ((list
),
343 AsnListNode
*newNode
;
346 newNode
= (AsnListNode
*) Asn1Alloc (sizeof (AsnListNode
) + list
->dataSize
);
347 dataAddr
= (void *) &(newNode
->data
);
349 newNode
->next
= NULL
;
351 if (list
->last
== NULL
)
353 newNode
->prev
= NULL
;
354 list
->first
= list
->last
= newNode
;
358 newNode
->prev
= list
->last
;
359 list
->last
->next
= newNode
;
360 list
->last
= newNode
;
363 list
->curr
= newNode
;
370 AsnListCurr
PARAMS ((list
),
376 retVal
= (void *) &(list
->curr
->data
);
384 AsnListCount
PARAMS ((list
),
392 AsnListConcat
PARAMS ((l1
,l2
),
401 l1
->count
= l2
->count
;
403 l1
->first
= l2
->first
;
404 l1
->curr
= l1
->first
;
408 l1
->count
+= l2
->count
;
409 l1
->last
->next
= l2
->first
;
410 l2
->first
->prev
= l1
->last
;
419 * Returns the index (starting a 0 for the first elmt)
420 * of the given elmt in the given list
421 * returns -1 if the elmt is not in the list
422 * Assumes that the list node contains a single pointer
425 GetAsnListElmtIndex
PARAMS ((elmt
, list
),
434 tmp
= (void*) CURR_LIST_NODE (list
);
435 FOR_EACH_LIST_ELMT (tmpElmt
, list
)
439 SET_CURR_LIST_NODE (list
, tmp
);
446 SET_CURR_LIST_NODE (list
, tmp
);
449 } /* GetAsnListElmtIndex */
454 * Returns the element with the given index.
455 * indexes start a 0 for the first elmt.
456 * returns NULL if the index is too large.
457 * Assumes that the list node contains a single pointer.
460 GetAsnListElmt
PARAMS ((list
, index
),
468 if (index
> LIST_COUNT (list
))
472 tmp
= (void*) CURR_LIST_NODE (list
);
473 FOR_EACH_LIST_ELMT (tmpElmt
, list
)
475 if (currIndex
== index
)
477 SET_CURR_LIST_NODE (list
, tmp
);
482 SET_CURR_LIST_NODE (list
, tmp
);
485 } /* GetAsnListElmt */