summaryrefslogtreecommitdiff
path: root/src/list.c
blob: d1a00533f7b8a8a24032a0c9df57f3ecf580e7b3 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
 * XML Security Library (http://www.aleksey.com/xmlsec).
 *
 * List of pointers.
 *
 * This is free software; see Copyright file in the source
 * distribution for preciese wording.
 *
 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
 */
#include "globals.h"

#include <stdlib.h>
#include <string.h>

#include <libxml/tree.h>

#include <xmlsec/xmlsec.h>
#include <xmlsec/list.h>
#include <xmlsec/errors.h>


static int              xmlSecPtrListEnsureSize                 (xmlSecPtrListPtr list,
                                                                 xmlSecSize size);

static xmlSecAllocMode gAllocMode = xmlSecAllocModeDouble;
static xmlSecSize gInitialSize = 64;

/**
 * xmlSecPtrListSetDefaultAllocMode:
 * @defAllocMode:       the new default memory allocation mode.
 * @defInitialSize:     the new default minimal initial size.
 *
 * Sets new default allocation mode and minimal initial list size.
 */
void
xmlSecPtrListSetDefaultAllocMode(xmlSecAllocMode defAllocMode, xmlSecSize defInitialSize) {
    xmlSecAssert(defInitialSize > 0);

    gAllocMode = defAllocMode;
    gInitialSize = defInitialSize;
}

/**
 * xmlSecPtrListCreate:
 * @id:                 the list klass.
 *
 * Creates new list object. Caller is responsible for freeing returned list
 * by calling #xmlSecPtrListDestroy function.
 *
 * Returns: pointer to newly allocated list or NULL if an error occurs.
 */
xmlSecPtrListPtr
xmlSecPtrListCreate(xmlSecPtrListId id) {
    xmlSecPtrListPtr list;
    int ret;

    xmlSecAssert2(id != xmlSecPtrListIdUnknown, NULL);

    /* Allocate a new xmlSecPtrList and fill the fields. */
    list = (xmlSecPtrListPtr)xmlMalloc(sizeof(xmlSecPtrList));
    if(list == NULL) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListKlassGetName(id)),
                    NULL,
                    XMLSEC_ERRORS_R_MALLOC_FAILED,
                    "sizeof(xmlSecPtrList)=%d",
                    sizeof(xmlSecPtrList));
        return(NULL);
    }

    ret = xmlSecPtrListInitialize(list, id);
    if(ret < 0) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListKlassGetName(id)),
                    "xmlSecPtrListInitialize",
                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
                    XMLSEC_ERRORS_NO_MESSAGE);
        xmlFree(list);
        return(NULL);
    }

    return(list);
}

/**
 * xmlSecPtrListDestroy:
 * @list:               the pointer to list.
 *
 * Destroys @list created with #xmlSecPtrListCreate function.
 */
void
xmlSecPtrListDestroy(xmlSecPtrListPtr list) {
    xmlSecAssert(xmlSecPtrListIsValid(list));
    xmlSecPtrListFinalize(list);
    xmlFree(list);
}

/**
 * xmlSecPtrListInitialize:
 * @list:               the pointer to list.
 * @id:                 the list klass.
 *
 * Initializes the list of given klass. Caller is responsible
 * for cleaning up by calling #xmlSecPtrListFinalize function.
 *
 * Returns: 0 on success or a negative value if an error occurs.
 */
int
xmlSecPtrListInitialize(xmlSecPtrListPtr list, xmlSecPtrListId id) {
    xmlSecAssert2(id != xmlSecPtrListIdUnknown, -1);
    xmlSecAssert2(list != NULL, -1);

    memset(list, 0, sizeof(xmlSecPtrList));
    list->id = id;
    list->allocMode = gAllocMode;

    return(0);
}

/**
 * xmlSecPtrListFinalize:
 * @list:               the pointer to list.
 *
 * Cleans up the list initialized with #xmlSecPtrListInitialize
 * function.
 */
void
xmlSecPtrListFinalize(xmlSecPtrListPtr list) {
    xmlSecAssert(xmlSecPtrListIsValid(list));

    xmlSecPtrListEmpty(list);
    memset(list, 0, sizeof(xmlSecPtrList));
}

/**
 * xmlSecPtrListEmpty:
 * @list:               the pointer to list.
 *
 * Remove all items from @list (if any).
 */
void
xmlSecPtrListEmpty(xmlSecPtrListPtr list) {
    xmlSecAssert(xmlSecPtrListIsValid(list));

    if(list->id->destroyItem != NULL) {
        xmlSecSize pos;

        for(pos = 0; pos < list->use; ++pos) {
            xmlSecAssert(list->data != NULL);
            if(list->data[pos] != NULL) {
                list->id->destroyItem(list->data[pos]);
            }
        }
    }
    if(list->max > 0) {
        xmlSecAssert(list->data != NULL);

        memset(list->data, 0, sizeof(xmlSecPtr) * list->use);
        xmlFree(list->data);
    }
    list->max = list->use = 0;
    list->data = NULL;
}

/**
 * xmlSecPtrListCopy:
 * @dst:                the pointer to destination list.
 * @src:                the pointer to source list.
 *
 * Copies @src list items to @dst list using #duplicateItem method
 * of the list klass. If #duplicateItem method is NULL then
 * we jsut copy pointers to items.
 *
 * Returns: 0 on success or a negative value if an error occurs.
 */
int
xmlSecPtrListCopy(xmlSecPtrListPtr dst, xmlSecPtrListPtr src) {
    xmlSecSize i;
    int ret;

    xmlSecAssert2(xmlSecPtrListIsValid(dst), -1);
    xmlSecAssert2(xmlSecPtrListIsValid(src), -1);
    xmlSecAssert2(dst->id == src->id, -1);

    /* allocate memory */
    ret = xmlSecPtrListEnsureSize(dst, dst->use + src->use);
    if(ret < 0) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListGetName(src)),
                    "xmlSecPtrListEnsureSize",
                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
                    "size=%d", src->use);
        return(-1);
    }

    /* copy one item after another */
    for(i = 0; i < src->use; ++i, ++dst->use) {
        xmlSecAssert2(src->data != NULL, -1);
        xmlSecAssert2(dst->data != NULL, -1);

        if((dst->id->duplicateItem != NULL) && (src->data[i] != NULL)) {
            dst->data[dst->use] = dst->id->duplicateItem(src->data[i]);
            if(dst->data[dst->use] == NULL) {
                xmlSecError(XMLSEC_ERRORS_HERE,
                            xmlSecErrorsSafeString(xmlSecPtrListGetName(src)),
                            "duplicateItem",
                            XMLSEC_ERRORS_R_XMLSEC_FAILED,
                            XMLSEC_ERRORS_NO_MESSAGE);
                return(-1);
            }
        } else {
            dst->data[dst->use] = src->data[i];
        }
    }

    return(0);
}

/**
 * xmlSecPtrListDuplicate:
 * @list:               the pointer to list.
 *
 * Creates a new copy of @list and all its items.
 *
 * Returns: pointer to newly allocated list or NULL if an error occurs.
 */
xmlSecPtrListPtr
xmlSecPtrListDuplicate(xmlSecPtrListPtr list) {
    xmlSecPtrListPtr newList;
    int ret;

    xmlSecAssert2(xmlSecPtrListIsValid(list), NULL);

    newList = xmlSecPtrListCreate(list->id);
    if(newList == NULL) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListGetName(list)),
                    "xmlSecPtrListCreate",
                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
                    XMLSEC_ERRORS_NO_MESSAGE);
        return(NULL);
    }

    ret = xmlSecPtrListCopy(newList, list);
    if(ret < 0) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListGetName(list)),
                    "xmlSecPtrListCopy",
                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
                    XMLSEC_ERRORS_NO_MESSAGE);
        xmlSecPtrListDestroy(newList);
        return(NULL);
    }
    return(newList);
}

/**
 * xmlSecPtrListGetSize:
 * @list:               the pointer to list.
 *
 * Gets list size.
 *
 * Returns: the number of itmes in @list.
 */
xmlSecSize
xmlSecPtrListGetSize(xmlSecPtrListPtr list) {
    xmlSecAssert2(xmlSecPtrListIsValid(list), 0);

    return(list->use);
}

/**
 * xmlSecPtrListGetItem:
 * @list:               the pointer to list.
 * @pos:                the item position.
 *
 * Gets item from the list.
 *
 * Returns: the list item at position @pos or NULL if @pos is greater
 * than the number of items in the list or an error occurs.
 */
xmlSecPtr
xmlSecPtrListGetItem(xmlSecPtrListPtr list, xmlSecSize pos) {
    xmlSecAssert2(xmlSecPtrListIsValid(list), NULL);
    xmlSecAssert2(list->data != NULL, NULL);
    xmlSecAssert2(pos < list->use, NULL);

    return(list->data[pos]);
}

/**
 * xmlSecPtrListAdd:
 * @list:               the pointer to list.
 * @item:               the item.
 *
 * Adds @item to the end of the @list.
 *
 * Returns: 0 on success or a negative value if an error occurs.
 */
int
xmlSecPtrListAdd(xmlSecPtrListPtr list, xmlSecPtr item) {
    int ret;

    xmlSecAssert2(xmlSecPtrListIsValid(list), -1);

    ret = xmlSecPtrListEnsureSize(list, list->use + 1);
    if(ret < 0) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListGetName(list)),
                    "xmlSecPtrListAdd",
                    XMLSEC_ERRORS_R_XMLSEC_FAILED,
                    "size=%d", list->use + 1);
        return(-1);
    }

    list->data[list->use++] = item;
    return(0);
}

/**
 * xmlSecPtrListSet:
 * @list:               the pointer to list.
 * @item:               the item.
 * @pos:                the pos.
 *
 * Sets the value of list item at position @pos. The old value
 * is destroyed.
 *
 * Returns: 0 on success or a negative value if an error occurs.
 */
int
xmlSecPtrListSet(xmlSecPtrListPtr list, xmlSecPtr item, xmlSecSize pos) {
    xmlSecAssert2(xmlSecPtrListIsValid(list), -1);
    xmlSecAssert2(list->data != NULL, -1);
    xmlSecAssert2(pos < list->use, -1);

    if((list->id->destroyItem != NULL) && (list->data[pos] != NULL)) {
        list->id->destroyItem(list->data[pos]);
    }
    list->data[pos] = item;
    return(0);
}

/**
 * xmlSecPtrListRemove:
 * @list:               the pointer to list.
 * @pos:                the position.
 *
 * Destroys list item at the position @pos and sets it value to NULL.
 *
 * Returns: 0 on success or a negative value if an error occurs.
 */
int
xmlSecPtrListRemove(xmlSecPtrListPtr list, xmlSecSize pos) {
    xmlSecAssert2(xmlSecPtrListIsValid(list), -1);
    xmlSecAssert2(list->data != NULL, -1);
    xmlSecAssert2(pos < list->use, -1);

    if((list->id->destroyItem != NULL) && (list->data[pos] != NULL)) {
        list->id->destroyItem(list->data[pos]);
    }
    list->data[pos] = NULL;
    if(pos == list->use - 1) {
        --list->use;
    }
    return(0);
}

/**
 * xmlSecPtrListRemoveAndReturn:
 * @list:               the pointer to list.
 * @pos:                the position.
 *
 * Remove the list item at the position @pos and return it back.
 *
 * Returns: the pointer to the list item.
 */
xmlSecPtr
xmlSecPtrListRemoveAndReturn(xmlSecPtrListPtr list, xmlSecSize pos) {
    xmlSecPtr res;

    xmlSecAssert2(xmlSecPtrListIsValid(list), NULL);
    xmlSecAssert2(list->data != NULL, NULL);
    xmlSecAssert2(pos < list->use, NULL);

    res = list->data[pos];
    list->data[pos] = NULL;
    if(pos == list->use - 1) {
        --list->use;
    }
    return(res);
}


/**
 * xmlSecPtrListDebugDump:
 * @list:               the pointer to list.
 * @output:             the pointer to output FILE.
 *
 * Prints debug information about @list to the @output.
 */
void
xmlSecPtrListDebugDump(xmlSecPtrListPtr list, FILE* output) {
    xmlSecAssert(xmlSecPtrListIsValid(list));
    xmlSecAssert(output != NULL);

    fprintf(output, "=== list size: %d\n", list->use);
    if(list->id->debugDumpItem != NULL) {
        xmlSecSize pos;

        for(pos = 0; pos < list->use; ++pos) {
            xmlSecAssert(list->data != NULL);
            if(list->data[pos] != NULL) {
                list->id->debugDumpItem(list->data[pos], output);
            }
        }
    }
}

/**
 * xmlSecPtrListDebugXmlDump:
 * @list:               the pointer to list.
 * @output:             the pointer to output FILE.
 *
 * Prints debug information about @list to the @output in XML format.
 */
void
xmlSecPtrListDebugXmlDump(xmlSecPtrListPtr list, FILE* output) {
    xmlSecAssert(xmlSecPtrListIsValid(list));
    xmlSecAssert(output != NULL);

    fprintf(output, "<List size=\"%d\">\n", list->use);
    if(list->id->debugXmlDumpItem != NULL) {
        xmlSecSize pos;

        for(pos = 0; pos < list->use; ++pos) {
            xmlSecAssert(list->data != NULL);
            if(list->data[pos] != NULL) {
                list->id->debugXmlDumpItem(list->data[pos], output);
            }
        }
    }
    fprintf(output, "</List>\n");
}

static int
xmlSecPtrListEnsureSize(xmlSecPtrListPtr list, xmlSecSize size) {
    xmlSecPtr* newData;
    xmlSecSize newSize = 0;

    xmlSecAssert2(xmlSecPtrListIsValid(list), -1);

    if(size < list->max) {
        return(0);
    }

    switch(list->allocMode) {
        case xmlSecAllocModeExact:
            newSize = size + 8;
            break;
        case xmlSecAllocModeDouble:
            newSize = 2 * size + 32;
            break;
    }

    if(newSize < gInitialSize) {
        newSize = gInitialSize;
    }

    if(list->data != NULL) {
        newData = (xmlSecPtr*)xmlRealloc(list->data, sizeof(xmlSecPtr) * newSize);
    } else {
        newData = (xmlSecPtr*)xmlMalloc(sizeof(xmlSecPtr) * newSize);
    }
    if(newData == NULL) {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    xmlSecErrorsSafeString(xmlSecPtrListGetName(list)),
                    NULL,
                    XMLSEC_ERRORS_R_MALLOC_FAILED,
                    "sizeof(xmlSecPtr)*%d=%d",
                    newSize, sizeof(xmlSecPtr) * newSize);
        return(-1);
    }

    list->data = newData;
    list->max = newSize;

    return(0);
}

/***********************************************************************
 *
 * strings list
 *
 **********************************************************************/
static xmlSecPtr        xmlSecStringListDuplicateItem           (xmlSecPtr ptr);
static void             xmlSecStringListDestroyItem             (xmlSecPtr ptr);

static xmlSecPtrListKlass xmlSecStringListKlass = {
    BAD_CAST "strings-list",
    xmlSecStringListDuplicateItem,              /* xmlSecPtrDuplicateItemMethod duplicateItem; */
    xmlSecStringListDestroyItem,                /* xmlSecPtrDestroyItemMethod destroyItem; */
    NULL,                                       /* xmlSecPtrDebugDumpItemMethod debugDumpItem; */
    NULL,                                       /* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */
};

/**
 * xmlSecStringListGetKlass:
 *
 * The strins list class.
 *
 * Returns: strings list klass.
 */
xmlSecPtrListId
xmlSecStringListGetKlass(void) {
    return(&xmlSecStringListKlass);
}

static xmlSecPtr
xmlSecStringListDuplicateItem(xmlSecPtr ptr) {
    xmlSecAssert2(ptr != NULL, NULL);

    return(xmlStrdup((xmlChar*)ptr));
}

static void
xmlSecStringListDestroyItem(xmlSecPtr ptr) {
    xmlSecAssert(ptr != NULL);

    xmlFree(ptr);
}