1 /*
2  * XML Security Library (http://www.aleksey.com/xmlsec).
3  *
4  * Common XML utility functions
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_TREE_H__
12 #define __XMLSEC_TREE_H__
13 
14 #include <stdio.h>
15 
16 #include <libxml/tree.h>
17 #include <libxml/xpath.h>
18 #include <xmlsec/xmlsec.h>
19 
20 #ifdef WIN32
21 #include <windows.h>
22 #endif /* WIN32 */
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif /* __cplusplus */
27 
28 /**
29  * xmlSecNodeGetName:
30  * @node:               the pointer to node.
31  *
32  * Macro. Returns node's name.
33  */
34 #define xmlSecNodeGetName(node) \
35     (((node)) ? ((const char*)((node)->name)) : NULL)
36 
37 XMLSEC_EXPORT const xmlChar*	xmlSecGetDefaultLineFeed(void);
38 XMLSEC_EXPORT void		xmlSecSetDefaultLineFeed(const xmlChar *linefeed);
39 
40 XMLSEC_EXPORT const xmlChar*    xmlSecGetNodeNsHref     (const xmlNodePtr cur);
41 XMLSEC_EXPORT int               xmlSecCheckNodeName     (const xmlNodePtr cur,
42                                                          const xmlChar *name,
43                                                          const xmlChar *ns);
44 XMLSEC_EXPORT xmlNodePtr        xmlSecGetNextElementNode(xmlNodePtr cur);
45 XMLSEC_EXPORT xmlNodePtr        xmlSecFindSibling       (const xmlNodePtr cur,
46                                                          const xmlChar *name,
47                                                          const xmlChar *ns);
48 XMLSEC_EXPORT xmlNodePtr        xmlSecFindChild         (const xmlNodePtr parent,
49                                                          const xmlChar *name,
50                                                          const xmlChar *ns);
51 XMLSEC_EXPORT xmlNodePtr        xmlSecFindParent        (const xmlNodePtr cur,
52                                                          const xmlChar *name,
53                                                          const xmlChar *ns);
54 XMLSEC_EXPORT xmlNodePtr        xmlSecFindNode          (const xmlNodePtr parent,
55                                                          const xmlChar *name,
56                                                          const xmlChar *ns);
57 XMLSEC_EXPORT xmlNodePtr        xmlSecAddChild          (xmlNodePtr parent,
58                                                          const xmlChar *name,
59                                                          const xmlChar *ns);
60 XMLSEC_EXPORT xmlNodePtr        xmlSecEnsureEmptyChild  (xmlNodePtr parent,
61                                                          const xmlChar *name,
62                                                          const xmlChar *ns);
63 XMLSEC_EXPORT xmlNodePtr        xmlSecAddChildNode      (xmlNodePtr parent,
64                                                          xmlNodePtr child);
65 XMLSEC_EXPORT xmlNodePtr        xmlSecAddNextSibling    (xmlNodePtr node,
66                                                          const xmlChar *name,
67                                                          const xmlChar *ns);
68 XMLSEC_EXPORT xmlNodePtr        xmlSecAddPrevSibling    (xmlNodePtr node,
69                                                          const xmlChar *name,
70                                                          const xmlChar *ns);
71 
72 XMLSEC_EXPORT int               xmlSecReplaceNode       (xmlNodePtr node,
73                                                          xmlNodePtr newNode);
74 XMLSEC_EXPORT int               xmlSecReplaceNodeAndReturn
75                                                         (xmlNodePtr node,
76                                                          xmlNodePtr newNode,
77                                                          xmlNodePtr* replaced);
78 XMLSEC_EXPORT int               xmlSecReplaceContent    (xmlNodePtr node,
79                                                          xmlNodePtr newNode);
80 XMLSEC_EXPORT int               xmlSecReplaceContentAndReturn
81                                                         (xmlNodePtr node,
82                                                          xmlNodePtr newNode,
83                                                          xmlNodePtr* replaced);
84 XMLSEC_EXPORT int               xmlSecReplaceNodeBuffer (xmlNodePtr node,
85                                                          const xmlSecByte *buffer,
86                                                          xmlSecSize size);
87 XMLSEC_EXPORT int               xmlSecReplaceNodeBufferAndReturn
88                                                         (xmlNodePtr node,
89                                                          const xmlSecByte *buffer,
90                                                          xmlSecSize size,
91                                                          xmlNodePtr* replaced);
92 XMLSEC_EXPORT int               xmlSecNodeEncodeAndSetContent
93                                                         (xmlNodePtr node,
94                                                          const xmlChar *buffer);
95 XMLSEC_EXPORT void              xmlSecAddIDs            (xmlDocPtr doc,
96                                                          xmlNodePtr cur,
97                                                          const xmlChar** ids);
98 XMLSEC_EXPORT xmlDocPtr         xmlSecCreateTree        (const xmlChar* rootNodeName,
99                                                          const xmlChar* rootNodeNs);
100 XMLSEC_EXPORT int               xmlSecIsEmptyNode       (xmlNodePtr node);
101 XMLSEC_EXPORT int               xmlSecIsEmptyString     (const xmlChar* str);
102 XMLSEC_EXPORT xmlChar*          xmlSecGetQName          (xmlNodePtr node,
103                                                          const xmlChar* href,
104                                                          const xmlChar* local);
105 
106 
107 XMLSEC_EXPORT int               xmlSecPrintXmlString    (FILE * fd,
108                                                          const xmlChar * str);
109 
110 /**
111  * xmlSecIsHex:
112  * @c:                  the character.
113  *
114  * Macro. Returns 1 if @c is a hex digit or 0 other wise.
115  */
116 #define xmlSecIsHex(c) \
117     (( (('0' <= (c)) && ((c) <= '9')) || \
118        (('a' <= (c)) && ((c) <= 'f')) || \
119        (('A' <= (c)) && ((c) <= 'F')) ) ? 1 : 0)
120 
121 /**
122  * xmlSecGetHex:
123  * @c:                  the character,
124  *
125  * Macro. Returns the hex value of the @c.
126  */
127 #define xmlSecGetHex(c) \
128     ( (('0' <= (c)) && ((c) <= '9')) ? (c) - '0' : \
129     ( (('a' <= (c)) && ((c) <= 'f')) ? (c) - 'a' + 10 :  \
130     ( (('A' <= (c)) && ((c) <= 'F')) ? (c) - 'A' + 10 : 0 )))
131 
132 /*************************************************************************
133  *
134  * QName <-> Integer mapping
135  *
136  ************************************************************************/
137 
138 /**
139  * xmlSecQName2IntegerInfo:
140  * @qnameHref:          the QName href
141  * @qnameLocalPart:     the QName local
142  * @intValue:           the integer value
143  *
144  * QName <-> Integer conversion definition.
145  */
146 typedef struct _xmlSecQName2IntegerInfo         xmlSecQName2IntegerInfo, *xmlSecQName2IntegerInfoPtr;
147 struct _xmlSecQName2IntegerInfo {
148     const xmlChar*      qnameHref;
149     const xmlChar*      qnameLocalPart;
150     int                 intValue;
151 };
152 
153 /**
154  * xmlSecQName2IntegerInfoConstPtr:
155  *
156  * Pointer to constant QName <-> Integer conversion definition.
157  */
158 typedef const xmlSecQName2IntegerInfo *         xmlSecQName2IntegerInfoConstPtr;
159 
160 XMLSEC_EXPORT xmlSecQName2IntegerInfoConstPtr xmlSecQName2IntegerGetInfo
161                                                                 (xmlSecQName2IntegerInfoConstPtr info,
162                                                                  int intValue);
163 XMLSEC_EXPORT int               xmlSecQName2IntegerGetInteger   (xmlSecQName2IntegerInfoConstPtr info,
164                                                                  const xmlChar* qnameHref,
165                                                                  const xmlChar* qnameLocalPart,
166                                                                  int* intValue);
167 XMLSEC_EXPORT int               xmlSecQName2IntegerGetIntegerFromString
168                                                                 (xmlSecQName2IntegerInfoConstPtr info,
169                                                                  xmlNodePtr node,
170                                                                  const xmlChar* qname,
171                                                                  int* intValue);
172 XMLSEC_EXPORT xmlChar*          xmlSecQName2IntegerGetStringFromInteger
173                                                                 (xmlSecQName2IntegerInfoConstPtr info,
174                                                                  xmlNodePtr node,
175                                                                  int intValue);
176 XMLSEC_EXPORT int               xmlSecQName2IntegerNodeRead     (xmlSecQName2IntegerInfoConstPtr info,
177                                                                  xmlNodePtr node,
178                                                                  int* intValue);
179 XMLSEC_EXPORT int               xmlSecQName2IntegerNodeWrite    (xmlSecQName2IntegerInfoConstPtr info,
180                                                                  xmlNodePtr node,
181                                                                  const xmlChar* nodeName,
182                                                                  const xmlChar* nodeNs,
183                                                                  int intValue);
184 XMLSEC_EXPORT int               xmlSecQName2IntegerAttributeRead(xmlSecQName2IntegerInfoConstPtr info,
185                                                                  xmlNodePtr node,
186                                                                  const xmlChar* attrName,
187                                                                  int* intValue);
188 XMLSEC_EXPORT int               xmlSecQName2IntegerAttributeWrite(xmlSecQName2IntegerInfoConstPtr info,
189                                                                  xmlNodePtr node,
190                                                                  const xmlChar* attrName,
191                                                                  int intValue);
192 XMLSEC_EXPORT void              xmlSecQName2IntegerDebugDump    (xmlSecQName2IntegerInfoConstPtr info,
193                                                                  int intValue,
194                                                                  const xmlChar* name,
195                                                                  FILE* output);
196 XMLSEC_EXPORT void              xmlSecQName2IntegerDebugXmlDump(xmlSecQName2IntegerInfoConstPtr info,
197                                                                  int intValue,
198                                                                  const xmlChar* name,
199                                                                  FILE* output);
200 
201 /*************************************************************************
202  *
203  * QName <-> Bitmask mapping
204  *
205  ************************************************************************/
206 
207 /**
208  * xmlSecBitMask:
209  *
210  * Bitmask datatype.
211  */
212 typedef unsigned int                                    xmlSecBitMask;
213 
214 /**
215  * xmlSecQName2BitMaskInfo:
216  * @qnameHref:          the QName href
217  * @qnameLocalPart:     the QName local
218  * @mask:               the bitmask value
219  *
220  * QName <-> Bitmask conversion definition.
221  */
222 typedef struct _xmlSecQName2BitMaskInfo         xmlSecQName2BitMaskInfo, *xmlSecQName2BitMaskInfoPtr;
223 
224 struct _xmlSecQName2BitMaskInfo {
225     const xmlChar*      qnameHref;
226     const xmlChar*      qnameLocalPart;
227     xmlSecBitMask       mask;
228 };
229 
230 /**
231  * xmlSecQName2BitMaskInfoConstPtr:
232  *
233  * Pointer to constant QName <-> Bitmask conversion definition.
234  */
235 typedef const xmlSecQName2BitMaskInfo*          xmlSecQName2BitMaskInfoConstPtr;
236 
237 XMLSEC_EXPORT xmlSecQName2BitMaskInfoConstPtr xmlSecQName2BitMaskGetInfo
238                                                                 (xmlSecQName2BitMaskInfoConstPtr info,
239                                                                  xmlSecBitMask mask);
240 XMLSEC_EXPORT int               xmlSecQName2BitMaskGetBitMask   (xmlSecQName2BitMaskInfoConstPtr info,
241                                                                  const xmlChar* qnameLocalPart,
242                                                                  const xmlChar* qnameHref,
243                                                                  xmlSecBitMask* mask);
244 XMLSEC_EXPORT int               xmlSecQName2BitMaskNodesRead    (xmlSecQName2BitMaskInfoConstPtr info,
245                                                                  xmlNodePtr* node,
246                                                                  const xmlChar* nodeName,
247                                                                  const xmlChar* nodeNs,
248                                                                  int stopOnUnknown,
249                                                                  xmlSecBitMask* mask);
250 XMLSEC_EXPORT int               xmlSecQName2BitMaskGetBitMaskFromString
251                                                                 (xmlSecQName2BitMaskInfoConstPtr info,
252                                                                  xmlNodePtr node,
253                                                                  const xmlChar* qname,
254                                                                  xmlSecBitMask* mask);
255 XMLSEC_EXPORT xmlChar*          xmlSecQName2BitMaskGetStringFromBitMask
256                                                                 (xmlSecQName2BitMaskInfoConstPtr info,
257                                                                  xmlNodePtr node,
258                                                                  xmlSecBitMask mask);
259 XMLSEC_EXPORT int               xmlSecQName2BitMaskNodesWrite   (xmlSecQName2BitMaskInfoConstPtr info,
260                                                                  xmlNodePtr node,
261                                                                  const xmlChar* nodeName,
262                                                                  const xmlChar* nodeNs,
263                                                                  xmlSecBitMask mask);
264 XMLSEC_EXPORT void              xmlSecQName2BitMaskDebugDump    (xmlSecQName2BitMaskInfoConstPtr info,
265                                                                  xmlSecBitMask mask,
266                                                                  const xmlChar* name,
267                                                                  FILE* output);
268 XMLSEC_EXPORT void              xmlSecQName2BitMaskDebugXmlDump(xmlSecQName2BitMaskInfoConstPtr info,
269                                                                  xmlSecBitMask mask,
270                                                                  const xmlChar* name,
271                                                                  FILE* output);
272 
273 
274 /*************************************************************************
275  *
276  * Windows string conversions
277  *
278  ************************************************************************/
279 #ifdef WIN32
280 XMLSEC_EXPORT LPWSTR             xmlSecWin32ConvertLocaleToUnicode(const char* str);
281 
282 XMLSEC_EXPORT LPWSTR             xmlSecWin32ConvertUtf8ToUnicode  (const xmlChar* str);
283 XMLSEC_EXPORT xmlChar*           xmlSecWin32ConvertUnicodeToUtf8  (LPCWSTR str);
284 
285 XMLSEC_EXPORT xmlChar*           xmlSecWin32ConvertLocaleToUtf8   (const char* str);
286 XMLSEC_EXPORT char*              xmlSecWin32ConvertUtf8ToLocale   (const xmlChar* str);
287 
288 XMLSEC_EXPORT xmlChar*           xmlSecWin32ConvertTstrToUtf8     (LPCTSTR str);
289 XMLSEC_EXPORT LPTSTR             xmlSecWin32ConvertUtf8ToTstr     (const xmlChar*  str);
290 
291 
292 #endif /* WIN32 */
293 
294 
295 #ifdef __cplusplus
296 }
297 #endif /* __cplusplus */
298 
299 #endif /* __XMLSEC_TREE_H__ */
300 
301