1 /*
2  * XML Security Library (http://www.aleksey.com/xmlsec).
3  *
4  * List of pointers.
5  *
6  * This is free software; see Copyright file in the source
7  * distribution for preciese wording.
8  *
9  * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
10  */
11 #ifndef __XMLSEC_LIST_H__
12 #define __XMLSEC_LIST_H__
13 
14 #include <xmlsec/xmlsec.h>
15 #include <xmlsec/buffer.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif /* __cplusplus */
20 
21 typedef const struct _xmlSecPtrListKlass                        xmlSecPtrListKlass,
22                                                                 *xmlSecPtrListId;
23 typedef struct _xmlSecPtrList                                   xmlSecPtrList,
24                                                                 *xmlSecPtrListPtr;
25 
26 /**
27  * xmlSecPtrList:
28  * @id:                         the list items description.
29  * @data:                       the list data.
30  * @use:                        the current list size.
31  * @max:                        the max (allocated) list size.
32  * @allocMode:                  the memory allocation mode.
33  *
34  * The pointers list.
35  */
36 struct _xmlSecPtrList {
37     xmlSecPtrListId             id;
38 
39     xmlSecPtr*                  data;
40     xmlSecSize                  use;
41     xmlSecSize                  max;
42     xmlSecAllocMode             allocMode;
43 };
44 
45 XMLSEC_EXPORT void              xmlSecPtrListSetDefaultAllocMode(xmlSecAllocMode defAllocMode,
46                                                                  xmlSecSize defInitialSize);
47 
48 
49 XMLSEC_EXPORT int               xmlSecPtrListInitialize         (xmlSecPtrListPtr list,
50                                                                  xmlSecPtrListId id);
51 XMLSEC_EXPORT void              xmlSecPtrListFinalize           (xmlSecPtrListPtr list);
52 XMLSEC_EXPORT xmlSecPtrListPtr  xmlSecPtrListCreate             (xmlSecPtrListId id);
53 XMLSEC_EXPORT void              xmlSecPtrListDestroy            (xmlSecPtrListPtr list);
54 XMLSEC_EXPORT void              xmlSecPtrListEmpty              (xmlSecPtrListPtr list);
55 
56 XMLSEC_EXPORT int               xmlSecPtrListCopy               (xmlSecPtrListPtr dst,
57                                                                  xmlSecPtrListPtr src);
58 XMLSEC_EXPORT xmlSecPtrListPtr  xmlSecPtrListDuplicate          (xmlSecPtrListPtr list);
59 
60 XMLSEC_EXPORT xmlSecSize        xmlSecPtrListGetSize            (xmlSecPtrListPtr list);
61 XMLSEC_EXPORT xmlSecPtr         xmlSecPtrListGetItem            (xmlSecPtrListPtr list,
62                                                                  xmlSecSize pos);
63 XMLSEC_EXPORT int               xmlSecPtrListAdd                (xmlSecPtrListPtr list,
64                                                                  xmlSecPtr item);
65 XMLSEC_EXPORT int               xmlSecPtrListSet                (xmlSecPtrListPtr list,
66                                                                  xmlSecPtr item,
67                                                                  xmlSecSize pos);
68 XMLSEC_EXPORT int               xmlSecPtrListRemove             (xmlSecPtrListPtr list,
69                                                                  xmlSecSize pos);
70 XMLSEC_EXPORT xmlSecPtr         xmlSecPtrListRemoveAndReturn    (xmlSecPtrListPtr list,
71                                                                  xmlSecSize pos);
72 XMLSEC_EXPORT void              xmlSecPtrListDebugDump          (xmlSecPtrListPtr list,
73                                                                  FILE* output);
74 XMLSEC_EXPORT void              xmlSecPtrListDebugXmlDump       (xmlSecPtrListPtr list,
75                                                                  FILE* output);
76 
77 /**
78  * xmlSecPtrListGetName:
79  * @list:               the ponter to list.
80  *
81  * Macro. Returns lists's name.
82  */
83 #define xmlSecPtrListGetName(list) \
84         (((list) != NULL) ? xmlSecPtrListKlassGetName((list)->id) : NULL)
85 
86 /**
87  * xmlSecPtrListIsValid:
88  * @list:               the pointer to list.
89  *
90  * Macro. Returns 1 if @list is not NULL and @list->id is not NULL
91  * or 0 otherwise.
92  */
93 #define xmlSecPtrListIsValid(list) \
94         ((( list ) != NULL) && ((( list )->id) != NULL))
95 /**
96  * xmlSecPtrListCheckId:
97  * @list:               the pointer to list.
98  * @dataId:             the list Id.
99  *
100  * Macro. Returns 1 if @list is valid and @list's id is equal to @dataId.
101  */
102 #define xmlSecPtrListCheckId(list, dataId) \
103         (xmlSecPtrListIsValid(( list )) && \
104         ((( list )->id) == ( dataId )))
105 
106 
107 /**************************************************************************
108  *
109  * List klass
110  *
111  *************************************************************************/
112 /**
113  * xmlSecPtrListIdUnknown:
114  *
115  * The "unknown" id.
116  */
117 #define xmlSecPtrListIdUnknown                  NULL
118 
119 /**
120  * xmlSecPtrDuplicateItemMethod:
121  * @ptr:                the poinetr to list item.
122  *
123  * Duplicates item @ptr.
124  *
125  * Returns: pointer to new item copy or NULL if an error occurs.
126  */
127 typedef xmlSecPtr               (*xmlSecPtrDuplicateItemMethod) (xmlSecPtr ptr);
128 
129 /**
130  * xmlSecPtrDestroyItemMethod:
131  * @ptr:                the poinetr to list item.
132  *
133  * Destroys list item @ptr.
134  */
135 typedef void                    (*xmlSecPtrDestroyItemMethod)   (xmlSecPtr ptr);
136 
137 /**
138  * xmlSecPtrDebugDumpItemMethod:
139  * @ptr:                the poinetr to list item.
140  * @output:             the output FILE.
141  *
142  * Prints debug information about @item to @output.
143  */
144 typedef void                    (*xmlSecPtrDebugDumpItemMethod) (xmlSecPtr ptr,
145                                                                  FILE* output);
146 
147 /**
148  * xmlSecPtrListKlass:
149  * @name:               the list klass name.
150  * @duplicateItem:      the duplicate item method.
151  * @destroyItem:        the destroy item method.
152  * @debugDumpItem:      the debug dump item method.
153  * @debugXmlDumpItem:   the debug dump item in xml format method.
154  *
155  * List klass.
156  */
157 struct _xmlSecPtrListKlass {
158     const xmlChar*                      name;
159     xmlSecPtrDuplicateItemMethod        duplicateItem;
160     xmlSecPtrDestroyItemMethod          destroyItem;
161     xmlSecPtrDebugDumpItemMethod        debugDumpItem;
162     xmlSecPtrDebugDumpItemMethod        debugXmlDumpItem;
163 };
164 
165 /**
166  * xmlSecPtrListKlassGetName:
167  * @klass:              the list klass.
168  *2
169 
170  * Macro. Returns the list klass name.
171  */
172 #define xmlSecPtrListKlassGetName(klass) \
173         (((klass) != NULL) ? ((klass)->name) : NULL)
174 
175 /**************************************************************************
176  *
177  * xmlSecStringListKlass
178  *
179  *************************************************************************/
180 /**
181  * xmlSecStringListId:
182  *
183  * Strings list klass.
184  */
185 #define xmlSecStringListId \
186         xmlSecStringListGetKlass()
187 XMLSEC_EXPORT xmlSecPtrListId   xmlSecStringListGetKlass        (void);
188 
189 #ifdef __cplusplus
190 }
191 #endif /* __cplusplus */
192 
193 #endif /* __XMLSEC_LIST_H__ */
194 
195