aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ccapi/common/generic_lists.c
blob: 62f8d8de40f1c6a3eb9a51ccddbcfdf012e4f38c (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* $Copyright:
*
* Copyright 2004-2006 by the Massachusetts Institute of Technology.
* 
* All rights reserved.
* 
* Export of this software from the United States of America may require a
* specific license from the United States Government.  It is the
* responsibility of any person or organization contemplating export to
* obtain such a license before exporting.
* 
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and distribute
* this software and its documentation for any purpose and without fee is
* hereby granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of M.I.T. not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.  Furthermore if you
* modify this software you must label your software as modified software
* and not distribute it in such a fashion that it might be confused with
* the original MIT software. M.I.T. makes no representations about the
* suitability of this software for any purpose.  It is provided "as is"
* without express or implied warranty.
* 
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 
* Individual source code files are copyright MIT, Cygnus Support,
* OpenVision, Oracle, Sun Soft, FundsXpress, and others.
* 
* Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira,
* and Zephyr are trademarks of the Massachusetts Institute of Technology
* (MIT).  No commercial use of these trademarks may be made without prior
* written permission of MIT.
* 
* "Commercial use" means use of a name in a product or other for-profit
* manner.  It does NOT prevent a commercial firm from referring to the MIT
* trademarks in order to convey information (although in doing so,
* recognition of their trademark status should be given).
* $
*/


/*
 * Lists implementation.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#include "CredentialsCache.h"
#include "generic_lists.h"

/**
 * cci_generic_iterate_has_next()
 *
 * Purpose: Determine if an iterator has a next element
 *
 * Return:  1 if another element exists
 *          0 if no additional elements exist
 *
 * Errors:  None
 *
 */
cc_int32 
cci_generic_iterate_has_next(cc_generic_iterate_t *iterate) 
{
    return ((iterate == NULL || iterate->next == NULL) ? 0 : 1);
}

/**
 * cci_generic_iterate_next()
 *
 * Purpose: Retrieve the next element from an iterator and advance
 *          the iterator
 *
 * Return:  non-NULL, the next element in the iterator
 *          NULL, the iterator list is empty or iterator is invalid
 *
 * Errors:  ccErrBadParam
 *
 */
cc_int32
cci_generic_iterate_next(cc_generic_iterate_t *iterator, cc_generic_list_node_t** nodepp) 
{
    cc_generic_list_node_t* ret;
    
    if (iterator == NULL || nodepp == NULL)
        return ccErrBadParam;

    ret = iterator->next;
    if (iterator->next != NULL)
        iterator->next = iterator->next->next;

    *nodepp = ret;
    return ccNoError;
}

/**
 * cci_generic_list_new()
 *
 * Purpose: Allocate new generic list
 *
 * Return:  non-NULL, an empty list
 *          NULL, failure
 *
 * Errors:  ccErrNoMem
 *
 */
cc_int32
cci_generic_list_new(cc_generic_list_head_t ** listpp) 
{
    cc_generic_list_head_t* ret = (cc_generic_list_head_t *)malloc(sizeof(cc_generic_list_head_t));
    if (ret == NULL)
        return ccErrNoMem;
	
    ret->type = cc_generic_list;
    ret->head = ret->tail = NULL;
    *listpp = ret;

    return ccNoError;
}

/**
 * cci_generic_list_append()
 *
 * Purpose: Appends a new node containing a copy of 'len' bytes of 'data' 
 *
 * Return:  non-NULL, a pointer to the newly allocated node
 *          NULL, failure
 *
 * Errors:  ccErrNoMem,ccErrBadParam
 *
 */
cc_int32
cci_generic_list_append(cc_generic_list_head_t *head, void *data, cc_uint32 len, cc_generic_list_node_t** nodepp) 
{
    cc_generic_list_node_t* new_node;

    if ( data == NULL || len == 0 )
        return ccErrBadParam;

    new_node = (cc_generic_list_node_t *)malloc(sizeof(cc_generic_list_node_t));
    if (new_node == NULL)
        return ccErrNoMem;

    new_node->data = malloc(len);
    if ( new_node->data == NULL ) {
        free(new_node);
        return ccErrNoMem;         
    }
    
    memcpy(new_node->data,data,len);
    new_node->len = len;

    if (head->head == NULL) { /*empty list*/
        head->head = new_node;
        head->tail = new_node;
	    new_node->next = new_node->prev = NULL;
    } else {
        new_node->prev = head->tail;
        head->tail->next = new_node;
        head->tail = new_node;
		new_node->next = NULL;
    }
	if (nodepp != NULL)
	    *nodepp = new_node;
    return ccNoError;
}

/**
 * cci_generic_list_prepend()
 *
 * Purpose: Prepends a new node containing a copy of 'len' bytes of 'data'
 *
 * Return:  non-NULL, a pointer to the newly allocated node
 *          NULL, failure
 *
 * Errors:  ccErrNoMem, ccErrBadParam
 *
 */
cc_int32 
cci_generic_list_prepend(cc_generic_list_head_t *head, void *data, cc_uint32 len, cc_generic_list_node_t** nodepp) 
{
    cc_generic_list_node_t* new_node;

    if ( data == NULL || len == 0 )
        return ccErrBadParam;

    new_node = (cc_generic_list_node_t *)malloc(sizeof(cc_generic_list_node_t));
    if (new_node == NULL)
        return ccErrNoMem;

    new_node->data = malloc(len);
    if ( new_node->data == NULL ) {
        free(new_node);
        return ccErrNoMem;
    }
    
    memcpy(new_node->data,data,len);
    new_node->len = len;
	
    if (head->head == NULL) { /*empty list*/
        head->head = new_node;
        head->tail = new_node;
        new_node->prev = new_node->next = NULL;
    } else {
        new_node->next = head->head;
        head->head->prev = new_node;
        new_node->prev = NULL;
        head->head = new_node;
    }

	if (nodepp != NULL)
		*nodepp = new_node;

    return ccNoError;
}

/**
 * cci_generic_list_remove_element()
 *
 * Purpose: Remove a node from the list
 *
 * Return:  0, success
 *         -1, failure
 *
 * Errors:  ccErrBadParam
 *
 */
cc_int32 
cci_generic_list_remove_element(cc_generic_list_head_t* head, cc_generic_list_node_t* rem) 
{
    if (head->head == NULL || rem == NULL)
        return ccErrBadParam;

    if (head->head == rem && head->tail == rem) { /*removing only element of list*/
        head->head = head->tail = NULL;
    } else if (head->head == rem) { /*removing head*/
        head->head = head->head->next;
    } else if (head->tail == rem) { /*removing tail*/
        head->tail = head->tail->prev;
        head->tail->next = NULL;
    } else {
        rem->prev->next = rem->next;
        rem->next->prev = rem->prev;
    }
    free(rem);
    return ccNoError;
}

/**
 * cci_generic_free_element()
 *
 * Purpose: Free the memory associated with a node
 *
 * Return:  0, success
 *         -1, failure
 *
 * Errors:  ccErrBadParam
 *
 */
cc_int32
cci_generic_free_element(cc_generic_list_node_t* node)
{
    if ( node == NULL )
        return ccErrBadParam;

    if ( node->data ) {
        free(node->data);
        node->data = NULL;
    }
    node->len = 0;
    node->next = node->prev = NULL;
    free(node);
    return ccNoError;
}


/**
 * cci_generic_list_destroy()
 *
 * Purpose: Deallocate a list and all of its contents
 *
 * Return:  0, success
 *         -1, failure
 *
 * Errors:  ccErrBadParam
 */
cc_int32
cci_generic_list_destroy(cc_generic_list_head_t* head) 
{
    cc_generic_list_node_t *cur, *next;
    cc_int32 ret = ccNoError;

    if ( head == NULL )
        return ccErrBadParam;
	
    for (cur = head->head; ret == ccNoError && cur != NULL; cur = next) {
        next = cur->next;
        ret = cci_generic_free_element(cur);
    }       
    free(head);
    return(ret);
}

/**
 * cci_generic_list_copy()
 *
 * Purpose: Copy a list
 *
 * Return:  non-NULL, a new list
 *          NULL, failure
 *
 * Errors:  ccErrBadParam, ccErrNoMem
 *
 */
cc_int32
cci_generic_list_copy(cc_generic_list_head_t* head, cc_generic_list_head_t** headpp) 
{
    cc_generic_list_head_t* copy;
    cc_generic_list_node_t *src_node, *dst_node;
    cc_int32 code;

    if (head == NULL || headpp == NULL)
        return ccErrBadParam;

    code = cci_generic_list_new(&copy);
    if (code != ccNoError)
        return code;

    for (src_node = head->head; src_node != NULL; src_node = src_node->next) {
        code = cci_generic_list_append(copy, src_node->data, src_node->len, &dst_node);
        if (code != ccNoError) {
            cci_generic_list_destroy(copy);
            return code;
        }
    }
    *headpp = copy;
    return ccNoError;
}

/**
 * cci_generic_list_iterator()
 *
 * Purpose: Allocate an iterator for the specified list
 *
 * Return:  non-NULL, an iterator
 *          NULL, failure
 *
 * Errors:  ccErrNoMem
 *
 */
cc_int32
cci_generic_list_iterator(cc_generic_list_head_t *head, cc_generic_iterate_t** headpp) 
{
    cc_generic_iterate_t* iterator;

    if ( head == NULL || headpp == NULL )
        return ccErrBadParam;

    iterator = (cc_generic_iterate_t*)malloc(sizeof(cc_generic_iterate_t));
    if (iterator == NULL)
        return ccErrNoMem;
    
    iterator->next = head->head;
    *headpp = iterator;
    return ccNoError;
}

/**
 * cci_generic_free_iterator()
 *
 * Purpose: Deallocate memory associated with an iterator
 *
 * Return:  0, success
 *         -1, failure
 *
 * Errors:  ccErrBadParam
 *
 */
cc_int32
cci_generic_free_iterator(cc_generic_iterate_t* iterator)
{
    if ( iterator == NULL )
        return ccErrBadParam;

    iterator->next = NULL;
    free(iterator);
    return ccNoError;
}