|
1 /* |
|
2 * "Canonical XML" implementation |
|
3 * http://www.w3.org/TR/xml-c14n |
|
4 * |
|
5 * "Exclusive XML Canonicalization" implementation |
|
6 * http://www.w3.org/TR/xml-exc-c14n |
|
7 * |
|
8 * See Copyright for the status of this software. |
|
9 * |
|
10 * Author: Aleksey Sanin <aleksey@aleksey.com> |
|
11 */ |
|
12 #define IN_LIBXML |
|
13 #include "XmlEnglibxml.h" |
|
14 #ifdef LIBXML_C14N_ENABLED |
|
15 #ifdef LIBXML_OUTPUT_ENABLED |
|
16 |
|
17 #ifdef HAVE_STDLIB_H |
|
18 #include <stdlib.h> |
|
19 #endif |
|
20 #include <string.h> |
|
21 |
|
22 #include "Libxml2_tree.h" |
|
23 #include "Libxml2_parser.h" |
|
24 #include "Libxml2_uri.h" |
|
25 #include "Libxml2_xmlerror.h" |
|
26 #include "Libxml2_globals.h" |
|
27 #include "Libxml2_xpathInternals.h" |
|
28 #include "Libxml2_c14n.h" |
|
29 |
|
30 /************************************************************************ |
|
31 * * |
|
32 * Some declaration better left private ATM * |
|
33 * * |
|
34 ************************************************************************/ |
|
35 |
|
36 typedef enum { |
|
37 XMLC14N_BEFORE_DOCUMENT_ELEMENT = 0, |
|
38 XMLC14N_INSIDE_DOCUMENT_ELEMENT = 1, |
|
39 XMLC14N_AFTER_DOCUMENT_ELEMENT = 2 |
|
40 } xmlC14NPosition; |
|
41 |
|
42 typedef struct _xmlC14NVisibleNsStack { |
|
43 int nsCurEnd; /* number of nodes in the set */ |
|
44 int nsPrevStart; /* the begginning of the stack for previous visible node */ |
|
45 int nsPrevEnd; /* the end of the stack for previous visible node */ |
|
46 int nsMax; /* size of the array as allocated */ |
|
47 xmlNsPtr *nsTab; /* array of ns in no particular order */ |
|
48 xmlNodePtr *nodeTab;/* array of nodes in no particular order */ |
|
49 } xmlC14NVisibleNsStack, *xmlC14NVisibleNsStackPtr; |
|
50 |
|
51 typedef struct _xmlC14NCtx { |
|
52 /* input parameters */ |
|
53 xmlDocPtr doc; |
|
54 xmlC14NIsVisibleCallback is_visible_callback; |
|
55 void* user_data; |
|
56 int with_comments; |
|
57 xmlOutputBufferPtr buf; |
|
58 |
|
59 /* position in the XML document */ |
|
60 xmlC14NPosition pos; |
|
61 int parent_is_doc; |
|
62 xmlC14NVisibleNsStackPtr ns_rendered; |
|
63 |
|
64 /* exclusive canonicalization */ |
|
65 int exclusive; |
|
66 xmlChar **inclusive_ns_prefixes; |
|
67 |
|
68 /* error number */ |
|
69 int error; |
|
70 } xmlC14NCtx, *xmlC14NCtxPtr; |
|
71 |
|
72 static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate (void); |
|
73 static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur); |
|
74 static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cur, |
|
75 xmlNsPtr ns, |
|
76 xmlNodePtr node); |
|
77 static void xmlC14NVisibleNsStackSave (xmlC14NVisibleNsStackPtr cur, |
|
78 xmlC14NVisibleNsStackPtr state); |
|
79 static void xmlC14NVisibleNsStackRestore (xmlC14NVisibleNsStackPtr cur, |
|
80 xmlC14NVisibleNsStackPtr state); |
|
81 static void xmlC14NVisibleNsStackShift (xmlC14NVisibleNsStackPtr cur); |
|
82 static int xmlC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur, |
|
83 xmlNsPtr ns); |
|
84 static int xmlExcC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur, |
|
85 xmlNsPtr ns, |
|
86 xmlC14NCtxPtr ctx); |
|
87 |
|
88 static int xmlC14NIsNodeInNodeset (xmlNodeSetPtr nodes, |
|
89 xmlNodePtr node, |
|
90 xmlNodePtr parent); |
|
91 |
|
92 |
|
93 |
|
94 static int xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur); |
|
95 static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur); |
|
96 typedef enum { |
|
97 XMLC14N_NORMALIZE_ATTR = 0, |
|
98 XMLC14N_NORMALIZE_COMMENT = 1, |
|
99 XMLC14N_NORMALIZE_PI = 2, |
|
100 XMLC14N_NORMALIZE_TEXT = 3 |
|
101 } xmlC14NNormalizationMode; |
|
102 |
|
103 static xmlChar *xmlC11NNormalizeString(const xmlChar * input, |
|
104 xmlC14NNormalizationMode mode); |
|
105 |
|
106 #define xmlC11NNormalizeAttr( a ) \ |
|
107 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR) |
|
108 #define xmlC11NNormalizeComment( a ) \ |
|
109 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT) |
|
110 #define xmlC11NNormalizePI( a ) \ |
|
111 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI) |
|
112 #define xmlC11NNormalizeText( a ) \ |
|
113 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT) |
|
114 |
|
115 #define xmlC14NIsVisible( ctx, node, parent ) \ |
|
116 (((ctx)->is_visible_callback != NULL) ? \ |
|
117 (ctx)->is_visible_callback((ctx)->user_data, \ |
|
118 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1) |
|
119 |
|
120 /************************************************************************ |
|
121 * * |
|
122 * Some factorized error routines * |
|
123 * * |
|
124 ************************************************************************/ |
|
125 |
|
126 /** |
|
127 * xmlC14NErrMemory: |
|
128 * @extra: extra informations |
|
129 * |
|
130 * Handle a redefinition of attribute error |
|
131 */ |
|
132 static void |
|
133 xmlC14NErrMemory(const char *extra) |
|
134 { |
|
135 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N, |
|
136 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra, |
|
137 NULL, NULL, 0, 0, |
|
138 "Memory allocation failed : %s\n", extra); |
|
139 } |
|
140 |
|
141 /** |
|
142 * xmlC14NErr: |
|
143 * @ctxt: a C14N evaluation context |
|
144 * @node: the context node |
|
145 * @error: the erorr code |
|
146 * @msg: the message |
|
147 * @extra: extra informations |
|
148 * |
|
149 * Handle a redefinition of attribute error |
|
150 */ |
|
151 static void |
|
152 xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int error, |
|
153 const char * msg) |
|
154 { |
|
155 if (ctxt != NULL) |
|
156 ctxt->error = error; |
|
157 __xmlRaiseError(NULL, NULL, NULL, |
|
158 ctxt, node, XML_FROM_C14N, error, |
|
159 XML_ERR_ERROR, NULL, 0, |
|
160 NULL, NULL, NULL, 0, 0, msg); |
|
161 } |
|
162 |
|
163 /************************************************************************ |
|
164 * * |
|
165 * The implementation internals * |
|
166 * * |
|
167 ************************************************************************/ |
|
168 #define XML_NAMESPACES_DEFAULT 16 |
|
169 |
|
170 static int |
|
171 xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes, xmlNodePtr node, xmlNodePtr parent) { |
|
172 if((nodes != NULL) && (node != NULL)) { |
|
173 if(node->type != XML_NAMESPACE_DECL) { |
|
174 return(xmlXPathNodeSetContains(nodes, node)); |
|
175 } else { |
|
176 xmlNs ns; |
|
177 |
|
178 memcpy(&ns, node, sizeof(ns)); |
|
179 |
|
180 /* this is a libxml hack! check xpath.c for details */ |
|
181 if((parent != NULL) && (parent->type == XML_ATTRIBUTE_NODE)) { |
|
182 ns.next = (xmlNsPtr)parent->parent; |
|
183 } else { |
|
184 ns.next = (xmlNsPtr)parent; |
|
185 } |
|
186 |
|
187 /* |
|
188 * If the input is an XPath node-set, then the node-set must explicitly |
|
189 * contain every node to be rendered to the canonical form. |
|
190 */ |
|
191 return(xmlXPathNodeSetContains(nodes, (xmlNodePtr)&ns)); |
|
192 } |
|
193 } |
|
194 return(1); |
|
195 } |
|
196 |
|
197 static xmlC14NVisibleNsStackPtr |
|
198 xmlC14NVisibleNsStackCreate(void) { |
|
199 xmlC14NVisibleNsStackPtr ret; |
|
200 |
|
201 ret = (xmlC14NVisibleNsStackPtr) xmlMalloc(sizeof(xmlC14NVisibleNsStack)); |
|
202 if (ret == NULL) { |
|
203 xmlC14NErrMemory("creating stack"); |
|
204 return(NULL); |
|
205 } |
|
206 memset(ret, 0 , (size_t) sizeof(xmlC14NVisibleNsStack)); |
|
207 return(ret); |
|
208 } |
|
209 |
|
210 static void |
|
211 xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr cur) { |
|
212 if(cur == NULL) { |
|
213 #ifdef DEBUG_C14N |
|
214 xmlGenericError(xmlGenericErrorContext, |
|
215 "xmlC14NVisibleNsStackDestroy: cur is null.\n"); |
|
216 #endif |
|
217 return; |
|
218 } |
|
219 if(cur->nsTab != NULL) { |
|
220 memset(cur->nsTab, 0, cur->nsMax * sizeof(xmlNsPtr)); |
|
221 xmlFree(cur->nsTab); |
|
222 } |
|
223 if(cur->nodeTab != NULL) { |
|
224 memset(cur->nodeTab, 0, cur->nsMax * sizeof(xmlNodePtr)); |
|
225 xmlFree(cur->nodeTab); |
|
226 } |
|
227 memset(cur, 0, sizeof(xmlC14NVisibleNsStack)); |
|
228 xmlFree(cur); |
|
229 |
|
230 } |
|
231 |
|
232 static void |
|
233 xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlNodePtr node) { |
|
234 if((cur == NULL) || |
|
235 ((cur->nsTab == NULL) && (cur->nodeTab != NULL)) || |
|
236 ((cur->nsTab != NULL) && (cur->nodeTab == NULL))) { |
|
237 #ifdef DEBUG_C14N |
|
238 xmlGenericError(xmlGenericErrorContext, |
|
239 "xmlC14NVisibleNsStackAdd: cur is null.\n"); |
|
240 #endif |
|
241 return; |
|
242 } |
|
243 |
|
244 if ((cur->nsTab == NULL) && (cur->nodeTab == NULL)) { |
|
245 cur->nsTab = (xmlNsPtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr)); |
|
246 cur->nodeTab = (xmlNodePtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr)); |
|
247 if ((cur->nsTab == NULL) || (cur->nodeTab == NULL)) { |
|
248 xmlC14NErrMemory("adding node to stack"); |
|
249 return; |
|
250 } |
|
251 memset(cur->nsTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr)); |
|
252 memset(cur->nodeTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr)); |
|
253 cur->nsMax = XML_NAMESPACES_DEFAULT; |
|
254 } else if(cur->nsMax == cur->nsCurEnd) { |
|
255 void *tmp; |
|
256 int tmpSize; |
|
257 |
|
258 tmpSize = 2 * cur->nsMax; |
|
259 tmp = xmlRealloc(cur->nsTab, tmpSize * sizeof(xmlNsPtr)); |
|
260 if (tmp == NULL) { |
|
261 xmlC14NErrMemory("adding node to stack"); |
|
262 return; |
|
263 } |
|
264 cur->nsTab = (xmlNsPtr*)tmp; |
|
265 |
|
266 tmp = xmlRealloc(cur->nodeTab, tmpSize * sizeof(xmlNodePtr)); |
|
267 if (tmp == NULL) { |
|
268 xmlC14NErrMemory("adding node to stack"); |
|
269 return; |
|
270 } |
|
271 cur->nodeTab = (xmlNodePtr*)tmp; |
|
272 |
|
273 cur->nsMax = tmpSize; |
|
274 } |
|
275 cur->nsTab[cur->nsCurEnd] = ns; |
|
276 cur->nodeTab[cur->nsCurEnd] = node; |
|
277 |
|
278 ++cur->nsCurEnd; |
|
279 } |
|
280 |
|
281 static void |
|
282 xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) { |
|
283 if((cur == NULL) || (state == NULL)) { |
|
284 #ifdef DEBUG_C14N |
|
285 xmlGenericError(xmlGenericErrorContext, |
|
286 "xmlC14NVisibleNsStackSave: cur or state is null.\n"); |
|
287 #endif |
|
288 return; |
|
289 } |
|
290 |
|
291 state->nsCurEnd = cur->nsCurEnd; |
|
292 state->nsPrevStart = cur->nsPrevStart; |
|
293 state->nsPrevEnd = cur->nsPrevEnd; |
|
294 } |
|
295 |
|
296 static void |
|
297 xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) { |
|
298 if((cur == NULL) || (state == NULL)) { |
|
299 #ifdef DEBUG_C14N |
|
300 xmlGenericError(xmlGenericErrorContext, |
|
301 "xmlC14NVisibleNsStackRestore: cur or state is null.\n"); |
|
302 #endif |
|
303 return; |
|
304 } |
|
305 cur->nsCurEnd = state->nsCurEnd; |
|
306 cur->nsPrevStart = state->nsPrevStart; |
|
307 cur->nsPrevEnd = state->nsPrevEnd; |
|
308 } |
|
309 |
|
310 static void |
|
311 xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur) { |
|
312 if(cur == NULL) { |
|
313 #ifdef DEBUG_C14N |
|
314 xmlGenericError(xmlGenericErrorContext, |
|
315 "xmlC14NVisibleNsStackRestore: cur is null.\n"); |
|
316 #endif |
|
317 return; |
|
318 } |
|
319 cur->nsPrevStart = cur->nsPrevEnd; |
|
320 cur->nsPrevEnd = cur->nsCurEnd; |
|
321 } |
|
322 |
|
323 static int |
|
324 xmlC14NStrEqual(const xmlChar *str1, const xmlChar *str2) { |
|
325 if (str1 == str2) return(1); |
|
326 if (str1 == NULL) return((*str2) == '\0'); |
|
327 if (str2 == NULL) return((*str1) == '\0'); |
|
328 do { |
|
329 if (*str1++ != *str2) return(0); |
|
330 } while (*str2++); |
|
331 return(1); |
|
332 } |
|
333 |
|
334 /** |
|
335 * xmlC14NVisibleNsStackFind: |
|
336 * @ctx: the C14N context |
|
337 * @ns: the namespace to check |
|
338 * |
|
339 * Checks whether the given namespace was already rendered or not |
|
340 * |
|
341 * Returns 1 if we already wrote this namespace or 0 otherwise |
|
342 */ |
|
343 static int |
|
344 xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns) |
|
345 { |
|
346 int i; |
|
347 const xmlChar *prefix; |
|
348 const xmlChar *href; |
|
349 int has_empty_ns; |
|
350 |
|
351 if(cur == NULL) { |
|
352 #ifdef DEBUG_C14N |
|
353 xmlGenericError(xmlGenericErrorContext, |
|
354 "xmlC14NVisibleNsStackFind: cur is null.\n"); |
|
355 #endif |
|
356 return (0); |
|
357 } |
|
358 |
|
359 /* |
|
360 * if the default namespace xmlns="" is not defined yet then |
|
361 * we do not want to print it out |
|
362 */ |
|
363 prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix; |
|
364 href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href; |
|
365 has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL)); |
|
366 |
|
367 if (cur->nsTab != NULL) { |
|
368 int start = (has_empty_ns) ? 0 : cur->nsPrevStart; |
|
369 for (i = cur->nsCurEnd - 1; i >= start; --i) { |
|
370 xmlNsPtr ns1 = cur->nsTab[i]; |
|
371 |
|
372 if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) { |
|
373 return(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)); |
|
374 } |
|
375 } |
|
376 } |
|
377 return(has_empty_ns); |
|
378 } |
|
379 |
|
380 static int |
|
381 xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlC14NCtxPtr ctx) { |
|
382 int i; |
|
383 const xmlChar *prefix; |
|
384 const xmlChar *href; |
|
385 int has_empty_ns; |
|
386 |
|
387 if(cur == NULL) { |
|
388 #ifdef DEBUG_C14N |
|
389 xmlGenericError(xmlGenericErrorContext, |
|
390 "xmlExcC14NVisibleNsStackFind: cur is null.\n"); |
|
391 #endif |
|
392 return (0); |
|
393 } |
|
394 |
|
395 /* |
|
396 * if the default namespace xmlns="" is not defined yet then |
|
397 * we do not want to print it out |
|
398 */ |
|
399 prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix; |
|
400 href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href; |
|
401 has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL)); |
|
402 |
|
403 if (cur->nsTab != NULL) { |
|
404 int start = 0; |
|
405 for (i = cur->nsCurEnd - 1; i >= start; --i) { |
|
406 xmlNsPtr ns1 = cur->nsTab[i]; |
|
407 |
|
408 if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) { |
|
409 if(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)) { |
|
410 return(xmlC14NIsVisible(ctx, ns1, cur->nodeTab[i])); |
|
411 } else { |
|
412 return(0); |
|
413 } |
|
414 } |
|
415 } |
|
416 } |
|
417 return(has_empty_ns); |
|
418 } |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 /** |
|
424 * xmlC14NIsXmlNs: |
|
425 * @ns: the namespace to check |
|
426 * |
|
427 * Checks whether the given namespace is a default "xml:" namespace |
|
428 * with href="http://www.w3.org/XML/1998/namespace" |
|
429 * |
|
430 * Returns 1 if the node is default or 0 otherwise |
|
431 */ |
|
432 |
|
433 /* todo: make it a define? */ |
|
434 static int |
|
435 xmlC14NIsXmlNs(xmlNsPtr ns) |
|
436 { |
|
437 return ((ns != NULL) && |
|
438 (xmlStrEqual(ns->prefix, BAD_CAST "xml")) && |
|
439 (xmlStrEqual(ns->href, |
|
440 BAD_CAST |
|
441 "http://www.w3.org/XML/1998/namespace"))); |
|
442 } |
|
443 |
|
444 |
|
445 /** |
|
446 * xmlC14NNsCompare: |
|
447 * @ns1: the pointer to first namespace |
|
448 * @ns2: the pointer to second namespace |
|
449 * |
|
450 * Compares the namespaces by names (prefixes). |
|
451 * |
|
452 * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2. |
|
453 */ |
|
454 static int |
|
455 xmlC14NNsCompare(xmlNsPtr ns1, xmlNsPtr ns2) |
|
456 { |
|
457 if (ns1 == ns2) |
|
458 return (0); |
|
459 if (ns1 == NULL) |
|
460 return (-1); |
|
461 if (ns2 == NULL) |
|
462 return (1); |
|
463 |
|
464 return (xmlStrcmp(ns1->prefix, ns2->prefix)); |
|
465 } |
|
466 |
|
467 |
|
468 /** |
|
469 * xmlC14NPrintNamespaces: |
|
470 * @ns: the pointer to namespace |
|
471 * @ctx: the C14N context |
|
472 * |
|
473 * Prints the given namespace to the output buffer from C14N context. |
|
474 * |
|
475 * Returns 1 on success or 0 on fail. |
|
476 */ |
|
477 static int |
|
478 xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPtr ctx) |
|
479 { |
|
480 |
|
481 if ((ns == NULL) || (ctx == NULL)) { |
|
482 #ifdef DEBUG_C14N |
|
483 xmlGenericError(xmlGenericErrorContext, |
|
484 "xmlC14NPrintNamespace: namespace or context pointer is null\n"); |
|
485 #endif |
|
486 return 0; |
|
487 } |
|
488 |
|
489 if (ns->prefix != NULL) { |
|
490 xmlOutputBufferWriteString(ctx->buf, " xmlns:"); |
|
491 xmlOutputBufferWriteString(ctx->buf, (const char *) ns->prefix); |
|
492 xmlOutputBufferWriteString(ctx->buf, "=\""); |
|
493 } else { |
|
494 xmlOutputBufferWriteString(ctx->buf, " xmlns=\""); |
|
495 } |
|
496 if(ns->href != NULL) { |
|
497 xmlOutputBufferWriteString(ctx->buf, (const char *) ns->href); |
|
498 } |
|
499 xmlOutputBufferWriteString(ctx->buf, "\""); |
|
500 return (1); |
|
501 } |
|
502 |
|
503 /** |
|
504 * xmlC14NProcessNamespacesAxis: |
|
505 * @ctx: the C14N context |
|
506 * @node: the current node |
|
507 * |
|
508 * Prints out canonical namespace axis of the current node to the |
|
509 * buffer from C14N context as follows |
|
510 * |
|
511 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
|
512 * |
|
513 * Namespace Axis |
|
514 * Consider a list L containing only namespace nodes in the |
|
515 * axis and in the node-set in lexicographic order (ascending). To begin |
|
516 * processing L, if the first node is not the default namespace node (a node |
|
517 * with no namespace URI and no local name), then generate a space followed |
|
518 * by xmlns="" if and only if the following conditions are met: |
|
519 * - the element E that owns the axis is in the node-set |
|
520 * - The nearest ancestor element of E in the node-set has a default |
|
521 * namespace node in the node-set (default namespace nodes always |
|
522 * have non-empty values in XPath) |
|
523 * The latter condition eliminates unnecessary occurrences of xmlns="" in |
|
524 * the canonical form since an element only receives an xmlns="" if its |
|
525 * default namespace is empty and if it has an immediate parent in the |
|
526 * canonical form that has a non-empty default namespace. To finish |
|
527 * processing L, simply process every namespace node in L, except omit |
|
528 * namespace node with local name xml, which defines the xml prefix, |
|
529 * if its string value is http://www.w3.org/XML/1998/namespace. |
|
530 * |
|
531 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n) |
|
532 * Canonical XML applied to a document subset requires the search of the |
|
533 * ancestor nodes of each orphan element node for attributes in the xml |
|
534 * namespace, such as xml:lang and xml:space. These are copied into the |
|
535 * element node except if a declaration of the same attribute is already |
|
536 * in the attribute axis of the element (whether or not it is included in |
|
537 * the document subset). This search and copying are omitted from the |
|
538 * Exclusive XML Canonicalization method. |
|
539 * |
|
540 * Returns 0 on success or -1 on fail. |
|
541 */ |
|
542 static int |
|
543 xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible) |
|
544 { |
|
545 xmlNodePtr n; |
|
546 xmlNsPtr ns, tmp; |
|
547 xmlListPtr list; |
|
548 int already_rendered; |
|
549 int has_empty_ns = 0; |
|
550 |
|
551 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
|
552 #ifdef DEBUG_C14N |
|
553 xmlGenericError(xmlGenericErrorContext, |
|
554 "xmlC14NProcessNamespacesAxis: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
|
555 #endif |
|
556 return (-1); |
|
557 } |
|
558 |
|
559 /* |
|
560 * Create a sorted list to store element namespaces |
|
561 */ |
|
562 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare); |
|
563 if (list == NULL) { |
|
564 #ifdef DEBUG_C14N |
|
565 xmlGenericError(xmlGenericErrorContext, |
|
566 "xmlC14NProcessNamespacesAxis: list creation failed\n"); |
|
567 #endif |
|
568 return (-1); |
|
569 } |
|
570 |
|
571 /* check all namespaces */ |
|
572 for(n = cur; n != NULL; n = n->parent) { |
|
573 for(ns = n->nsDef; ns != NULL; ns = ns->next) { |
|
574 tmp = xmlSearchNs(cur->doc, cur, ns->prefix); |
|
575 |
|
576 if((tmp == ns) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) { |
|
577 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns); |
|
578 if(visible) { |
|
579 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur); |
|
580 } |
|
581 if(!already_rendered) { |
|
582 xmlListInsert(list, ns); |
|
583 } |
|
584 if(xmlStrlen(ns->prefix) == 0) { |
|
585 has_empty_ns = 1; |
|
586 } |
|
587 } |
|
588 } |
|
589 } |
|
590 |
|
591 /** |
|
592 * if the first node is not the default namespace node (a node with no |
|
593 * namespace URI and no local name), then generate a space followed by |
|
594 * xmlns="" if and only if the following conditions are met: |
|
595 * - the element E that owns the axis is in the node-set |
|
596 * - the nearest ancestor element of E in the node-set has a default |
|
597 * namespace node in the node-set (default namespace nodes always |
|
598 * have non-empty values in XPath) |
|
599 */ |
|
600 if(visible && !has_empty_ns) { |
|
601 //FIXIT |
|
602 //static |
|
603 xmlNs ns_default; |
|
604 |
|
605 memset(&ns_default, 0, sizeof(ns_default)); |
|
606 if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) { |
|
607 xmlC14NPrintNamespaces(&ns_default, ctx); |
|
608 } |
|
609 } |
|
610 |
|
611 |
|
612 /* |
|
613 * print out all elements from list |
|
614 */ |
|
615 xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx); |
|
616 |
|
617 /* |
|
618 * Cleanup |
|
619 */ |
|
620 xmlListDelete(list); |
|
621 return (0); |
|
622 } |
|
623 |
|
624 |
|
625 /** |
|
626 * xmlExcC14NProcessNamespacesAxis: |
|
627 * @ctx: the C14N context |
|
628 * @node: the current node |
|
629 * |
|
630 * Prints out exclusive canonical namespace axis of the current node to the |
|
631 * buffer from C14N context as follows |
|
632 * |
|
633 * Exclusive XML Canonicalization |
|
634 * http://www.w3.org/TR/xml-exc-c14n |
|
635 * |
|
636 * If the element node is in the XPath subset then output the node in |
|
637 * accordance with Canonical XML except for namespace nodes which are |
|
638 * rendered as follows: |
|
639 * |
|
640 * 1. Render each namespace node iff: |
|
641 * * it is visibly utilized by the immediate parent element or one of |
|
642 * its attributes, or is present in InclusiveNamespaces PrefixList, and |
|
643 * * its prefix and value do not appear in ns_rendered. ns_rendered is |
|
644 * obtained by popping the state stack in order to obtain a list of |
|
645 * prefixes and their values which have already been rendered by |
|
646 * an output ancestor of the namespace node's parent element. |
|
647 * 2. Append the rendered namespace node to the list ns_rendered of namespace |
|
648 * nodes rendered by output ancestors. Push ns_rendered on state stack and |
|
649 * recurse. |
|
650 * 3. After the recursion returns, pop thestate stack. |
|
651 * |
|
652 * |
|
653 * Returns 0 on success or -1 on fail. |
|
654 */ |
|
655 static int |
|
656 xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible) |
|
657 { |
|
658 xmlNsPtr ns; |
|
659 xmlListPtr list; |
|
660 xmlAttrPtr attr; |
|
661 int already_rendered; |
|
662 int has_empty_ns = 0; |
|
663 int has_visibly_utilized_empty_ns = 0; |
|
664 int has_empty_ns_in_inclusive_list = 0; |
|
665 |
|
666 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
|
667 #ifdef DEBUG_C14N |
|
668 xmlGenericError(xmlGenericErrorContext, |
|
669 "xmlExcC14NProcessNamespacesAxis: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
|
670 #endif |
|
671 return (-1); |
|
672 } |
|
673 |
|
674 if(!ctx->exclusive) { |
|
675 #ifdef DEBUG_C14N |
|
676 xmlGenericError(xmlGenericErrorContext, |
|
677 "xmlExcC14NProcessNamespacesAxis: called for non-exclusive canonization or rendered stack is NULL.\n"); |
|
678 #endif |
|
679 return (-1); |
|
680 |
|
681 } |
|
682 |
|
683 /* |
|
684 * Create a sorted list to store element namespaces |
|
685 */ |
|
686 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare); |
|
687 if (list == NULL) { |
|
688 #ifdef DEBUG_C14N |
|
689 xmlGenericError(xmlGenericErrorContext, |
|
690 "xmlExcC14NProcessNamespacesAxis: list creation failed\n"); |
|
691 #endif |
|
692 return (-1); |
|
693 } |
|
694 |
|
695 /* |
|
696 * process inclusive namespaces: |
|
697 * All namespace nodes appearing on inclusive ns list are |
|
698 * handled as provided in Canonical XML |
|
699 */ |
|
700 if(ctx->inclusive_ns_prefixes != NULL) { |
|
701 xmlChar *prefix; |
|
702 int i; |
|
703 |
|
704 for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) { |
|
705 prefix = ctx->inclusive_ns_prefixes[i]; |
|
706 /* |
|
707 * Special values for namespace with empty prefix |
|
708 */ |
|
709 if (xmlStrEqual(prefix, BAD_CAST "#default") |
|
710 || xmlStrEqual(prefix, BAD_CAST "")) { |
|
711 prefix = NULL; |
|
712 has_empty_ns_in_inclusive_list = 1; |
|
713 } |
|
714 |
|
715 ns = xmlSearchNs(cur->doc, cur, prefix); |
|
716 if((ns != NULL) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) { |
|
717 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns); |
|
718 if(visible) { |
|
719 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur); |
|
720 } |
|
721 if(!already_rendered) { |
|
722 xmlListInsert(list, ns); |
|
723 } |
|
724 if(xmlStrlen(ns->prefix) == 0) { |
|
725 has_empty_ns = 1; |
|
726 } |
|
727 } |
|
728 } |
|
729 } |
|
730 |
|
731 /* add node namespace */ |
|
732 if(cur->ns != NULL) { |
|
733 ns = cur->ns; |
|
734 } else { |
|
735 ns = xmlSearchNs(cur->doc, cur, NULL); |
|
736 has_visibly_utilized_empty_ns = 1; |
|
737 } |
|
738 if((ns != NULL) && !xmlC14NIsXmlNs(ns)) { |
|
739 if(visible && xmlC14NIsVisible(ctx, ns, cur)) { |
|
740 if(!xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, ns, ctx)) { |
|
741 xmlListInsert(list, ns); |
|
742 } |
|
743 } |
|
744 if(visible) { |
|
745 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur); |
|
746 } |
|
747 if(xmlStrlen(ns->prefix) == 0) { |
|
748 has_empty_ns = 1; |
|
749 } |
|
750 } |
|
751 |
|
752 |
|
753 /* add attributes */ |
|
754 for(attr = cur->properties; attr != NULL; attr = attr->next) { |
|
755 /* |
|
756 * we need to check that attribute is visible and has non |
|
757 * default namespace (XML Namespaces: "default namespaces |
|
758 * do not apply directly to attributes") |
|
759 */ |
|
760 if((attr->ns != NULL) && xmlC14NIsVisible(ctx, attr, cur)) { |
|
761 already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, attr->ns, ctx); |
|
762 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, attr->ns, (xmlNodePtr)attr); |
|
763 if(!already_rendered && visible) { |
|
764 xmlListInsert(list, attr->ns); |
|
765 } |
|
766 if(xmlStrlen(attr->ns->prefix) == 0) { |
|
767 has_empty_ns = 1; |
|
768 } |
|
769 } else if(attr->ns == NULL) { |
|
770 has_visibly_utilized_empty_ns = 1; |
|
771 } |
|
772 } |
|
773 |
|
774 /* |
|
775 * Process xmlns="" |
|
776 */ |
|
777 if(visible && has_visibly_utilized_empty_ns && |
|
778 !has_empty_ns && !has_empty_ns_in_inclusive_list) { |
|
779 //static //FIXIT |
|
780 xmlNs ns_default; |
|
781 |
|
782 memset(&ns_default, 0, sizeof(ns_default)); |
|
783 |
|
784 already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default, ctx); |
|
785 if(!already_rendered) { |
|
786 xmlC14NPrintNamespaces(&ns_default, ctx); |
|
787 } |
|
788 } else if(visible && !has_empty_ns && has_empty_ns_in_inclusive_list) { |
|
789 //static FIXIT |
|
790 xmlNs ns_default; |
|
791 |
|
792 memset(&ns_default, 0, sizeof(ns_default)); |
|
793 if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) { |
|
794 xmlC14NPrintNamespaces(&ns_default, ctx); |
|
795 } |
|
796 } |
|
797 |
|
798 |
|
799 |
|
800 /* |
|
801 * print out all elements from list |
|
802 */ |
|
803 xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx); |
|
804 |
|
805 /* |
|
806 * Cleanup |
|
807 */ |
|
808 xmlListDelete(list); |
|
809 return (0); |
|
810 } |
|
811 |
|
812 |
|
813 /** |
|
814 * xmlC14NAttrsCompare: |
|
815 * @attr1: the pointer tls o first attr |
|
816 * @attr2: the pointer to second attr |
|
817 * |
|
818 * Prints the given attribute to the output buffer from C14N context. |
|
819 * |
|
820 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2. |
|
821 */ |
|
822 static int |
|
823 xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2) |
|
824 { |
|
825 int ret = 0; |
|
826 |
|
827 /* |
|
828 * Simple cases |
|
829 */ |
|
830 if (attr1 == attr2) |
|
831 return (0); |
|
832 if (attr1 == NULL) |
|
833 return (-1); |
|
834 if (attr2 == NULL) |
|
835 return (1); |
|
836 if (attr1->ns == attr2->ns) { |
|
837 return (xmlStrcmp(attr1->name, attr2->name)); |
|
838 } |
|
839 |
|
840 /* |
|
841 * Attributes in the default namespace are first |
|
842 * because the default namespace is not applied to |
|
843 * unqualified attributes |
|
844 */ |
|
845 if (attr1->ns == NULL) |
|
846 return (-1); |
|
847 if (attr2->ns == NULL) |
|
848 return (1); |
|
849 if (attr1->ns->prefix == NULL) |
|
850 return (-1); |
|
851 if (attr2->ns->prefix == NULL) |
|
852 return (1); |
|
853 |
|
854 ret = xmlStrcmp(attr1->ns->href, attr2->ns->href); |
|
855 if (ret == 0) { |
|
856 ret = xmlStrcmp(attr1->name, attr2->name); |
|
857 } |
|
858 return (ret); |
|
859 } |
|
860 |
|
861 |
|
862 /** |
|
863 * xmlC14NPrintAttrs: |
|
864 * @attr: the pointer to attr |
|
865 * @ctx: the C14N context |
|
866 * |
|
867 * Prints out canonical attribute urrent node to the |
|
868 * buffer from C14N context as follows |
|
869 * |
|
870 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
|
871 * |
|
872 * Returns 1 on success or 0 on fail. |
|
873 */ |
|
874 static int |
|
875 xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr ctx) |
|
876 { |
|
877 xmlChar *value; |
|
878 xmlChar *buffer; |
|
879 |
|
880 if ((attr == NULL) || (ctx == NULL)) { |
|
881 #ifdef DEBUG_C14N |
|
882 xmlGenericError(xmlGenericErrorContext, |
|
883 "xmlC14NPrintAttrs: attr == NULL or ctx == NULL\n"); |
|
884 #endif |
|
885 return (0); |
|
886 } |
|
887 |
|
888 xmlOutputBufferWriteString(ctx->buf, " "); |
|
889 if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) { |
|
890 xmlOutputBufferWriteString(ctx->buf, |
|
891 (const char *) attr->ns->prefix); |
|
892 xmlOutputBufferWriteString(ctx->buf, ":"); |
|
893 } |
|
894 xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name); |
|
895 xmlOutputBufferWriteString(ctx->buf, "=\""); |
|
896 |
|
897 value = xmlNodeListGetString(attr->doc, attr->children, 1); |
|
898 /* todo: should we log an error if value==NULL ? */ |
|
899 if (value != NULL) { |
|
900 buffer = xmlC11NNormalizeAttr(value); |
|
901 xmlFree(value); |
|
902 if (buffer != NULL) { |
|
903 xmlOutputBufferWriteString(ctx->buf, (const char *) buffer); |
|
904 xmlFree(buffer); |
|
905 } else { |
|
906 #ifdef DEBUG_C14N |
|
907 xmlGenericError(xmlGenericErrorContext, |
|
908 "xmlC14NPrintAttrs: xmlC11NNormalizeAttr failed\n"); |
|
909 #endif |
|
910 return (0); |
|
911 } |
|
912 } |
|
913 xmlOutputBufferWriteString(ctx->buf, "\""); |
|
914 return (1); |
|
915 } |
|
916 |
|
917 /** |
|
918 * xmlC14NProcessAttrsAxis: |
|
919 * @ctx: the C14N context |
|
920 * @cur: the current node |
|
921 * |
|
922 * Prints out canonical attribute axis of the current node to the |
|
923 * buffer from C14N context as follows |
|
924 * |
|
925 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
|
926 * |
|
927 * Attribute Axis |
|
928 * In lexicographic order (ascending), process each node that |
|
929 * is in the element's attribute axis and in the node-set. |
|
930 * |
|
931 * The processing of an element node E MUST be modified slightly |
|
932 * when an XPath node-set is given as input and the element's |
|
933 * parent is omitted from the node-set. |
|
934 * |
|
935 * |
|
936 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n) |
|
937 * |
|
938 * Canonical XML applied to a document subset requires the search of the |
|
939 * ancestor nodes of each orphan element node for attributes in the xml |
|
940 * namespace, such as xml:lang and xml:space. These are copied into the |
|
941 * element node except if a declaration of the same attribute is already |
|
942 * in the attribute axis of the element (whether or not it is included in |
|
943 * the document subset). This search and copying are omitted from the |
|
944 * Exclusive XML Canonicalization method. |
|
945 * |
|
946 * Returns 0 on success or -1 on fail. |
|
947 */ |
|
948 static int |
|
949 xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
|
950 { |
|
951 xmlAttrPtr attr; |
|
952 xmlListPtr list; |
|
953 |
|
954 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
|
955 #ifdef DEBUG_C14N |
|
956 xmlGenericError(xmlGenericErrorContext, |
|
957 "xmlC14NProcessAttrsAxis: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
|
958 #endif |
|
959 return (-1); |
|
960 } |
|
961 |
|
962 /* |
|
963 * Create a sorted list to store element attributes |
|
964 */ |
|
965 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare); |
|
966 if (list == NULL) { |
|
967 #ifdef DEBUG_C14N |
|
968 xmlGenericError(xmlGenericErrorContext, |
|
969 "xmlC14NProcessAttrsAxis: list creation failed\n"); |
|
970 #endif |
|
971 return (-1); |
|
972 } |
|
973 |
|
974 /* |
|
975 * Add all visible attributes from current node. |
|
976 */ |
|
977 attr = cur->properties; |
|
978 while (attr != NULL) { |
|
979 /* check that attribute is visible */ |
|
980 if (xmlC14NIsVisible(ctx, attr, cur)) { |
|
981 xmlListInsert(list, attr); |
|
982 } |
|
983 attr = attr->next; |
|
984 } |
|
985 |
|
986 /* |
|
987 * include attributes in "xml" namespace defined in ancestors |
|
988 * (only for non-exclusive XML Canonicalization) |
|
989 */ |
|
990 if ((!ctx->exclusive) && (cur->parent != NULL) |
|
991 && (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent))) { |
|
992 /* |
|
993 * If XPath node-set is not specified then the parent is always |
|
994 * visible! |
|
995 */ |
|
996 cur = cur->parent; |
|
997 while (cur != NULL) { |
|
998 attr = cur->properties; |
|
999 while (attr != NULL) { |
|
1000 if ((attr->ns != NULL) |
|
1001 && (xmlStrEqual(attr->ns->prefix, BAD_CAST "xml"))) { |
|
1002 if (xmlListSearch(list, attr) == NULL) { |
|
1003 xmlListInsert(list, attr); |
|
1004 } |
|
1005 } |
|
1006 attr = attr->next; |
|
1007 } |
|
1008 cur = cur->parent; |
|
1009 } |
|
1010 } |
|
1011 |
|
1012 /* |
|
1013 * print out all elements from list |
|
1014 */ |
|
1015 xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx); |
|
1016 |
|
1017 /* |
|
1018 * Cleanup |
|
1019 */ |
|
1020 xmlListDelete(list); |
|
1021 return (0); |
|
1022 } |
|
1023 |
|
1024 /** |
|
1025 * xmlC14NCheckForRelativeNamespaces: |
|
1026 * @ctx: the C14N context |
|
1027 * @cur: the current element node |
|
1028 * |
|
1029 * Checks that current element node has no relative namespaces defined |
|
1030 * |
|
1031 * Returns 0 if the node has no relative namespaces or -1 otherwise. |
|
1032 */ |
|
1033 static int |
|
1034 xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
|
1035 { |
|
1036 xmlNsPtr ns; |
|
1037 |
|
1038 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
|
1039 #ifdef DEBUG_C14N |
|
1040 xmlGenericError(xmlGenericErrorContext, |
|
1041 "xmlC14NCheckForRelativeNamespaces: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
|
1042 #endif |
|
1043 return (-1); |
|
1044 } |
|
1045 |
|
1046 ns = cur->nsDef; |
|
1047 while (ns != NULL) { |
|
1048 if (xmlStrlen(ns->href) > 0) { |
|
1049 xmlURIPtr uri; |
|
1050 |
|
1051 uri = xmlParseURI((const char *) ns->href); |
|
1052 if (uri == NULL) { |
|
1053 #ifdef DEBUG_C14N |
|
1054 xmlGenericError(xmlGenericErrorContext, |
|
1055 "xmlC14NCheckForRelativeNamespaces: unable to parse uri=\"%s\".\n", |
|
1056 ns->href); |
|
1057 #endif |
|
1058 return (-1); |
|
1059 } |
|
1060 if (xmlStrlen((const xmlChar *) uri->scheme) == 0) { |
|
1061 xmlFreeURI(uri); |
|
1062 return (-1); |
|
1063 } |
|
1064 if ((!xmlStrEqual |
|
1065 ((const xmlChar *) uri->scheme, BAD_CAST "urn")) |
|
1066 && (xmlStrlen((const xmlChar *) uri->server) == 0)) { |
|
1067 xmlFreeURI(uri); |
|
1068 return (-1); |
|
1069 } |
|
1070 xmlFreeURI(uri); |
|
1071 } |
|
1072 ns = ns->next; |
|
1073 } |
|
1074 return (0); |
|
1075 } |
|
1076 |
|
1077 /** |
|
1078 * xmlC14NProcessElementNode: |
|
1079 * @ctx: the pointer to C14N context object |
|
1080 * @cur: the node to process |
|
1081 * |
|
1082 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
|
1083 * |
|
1084 * Element Nodes |
|
1085 * If the element is not in the node-set, then the result is obtained |
|
1086 * by processing the namespace axis, then the attribute axis, then |
|
1087 * processing the child nodes of the element that are in the node-set |
|
1088 * (in document order). If the element is in the node-set, then the result |
|
1089 * is an open angle bracket (<), the element QName, the result of |
|
1090 * processing the namespace axis, the result of processing the attribute |
|
1091 * axis, a close angle bracket (>), the result of processing the child |
|
1092 * nodes of the element that are in the node-set (in document order), an |
|
1093 * open angle bracket, a forward slash (/), the element QName, and a close |
|
1094 * angle bracket. |
|
1095 * |
|
1096 * Returns non-negative value on success or negative value on fail |
|
1097 */ |
|
1098 static int |
|
1099 xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible) |
|
1100 { |
|
1101 int ret; |
|
1102 xmlC14NVisibleNsStack state; |
|
1103 int parent_is_doc = 0; |
|
1104 |
|
1105 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
|
1106 #ifdef DEBUG_C14N |
|
1107 xmlGenericError(xmlGenericErrorContext, |
|
1108 "xmlC14NProcessElementNode: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
|
1109 #endif |
|
1110 return (-1); |
|
1111 } |
|
1112 |
|
1113 /* |
|
1114 * Check relative relative namespaces: |
|
1115 * implementations of XML canonicalization MUST report an operation |
|
1116 * failure on documents containing relative namespace URIs. |
|
1117 */ |
|
1118 if (xmlC14NCheckForRelativeNamespaces(ctx, cur) < 0) { |
|
1119 #ifdef DEBUG_C14N |
|
1120 xmlGenericError(xmlGenericErrorContext, |
|
1121 "xmlC14NProcessElementNode: xmlC14NCheckForRelativeNamespaces failed.\n"); |
|
1122 #endif |
|
1123 return (-1); |
|
1124 } |
|
1125 |
|
1126 |
|
1127 /* |
|
1128 * Save ns_rendered stack position |
|
1129 */ |
|
1130 xmlC14NVisibleNsStackSave(ctx->ns_rendered, &state); |
|
1131 |
|
1132 if (visible) { |
|
1133 if (ctx->parent_is_doc) { |
|
1134 /* save this flag into the stack */ |
|
1135 parent_is_doc = ctx->parent_is_doc; |
|
1136 ctx->parent_is_doc = 0; |
|
1137 ctx->pos = XMLC14N_INSIDE_DOCUMENT_ELEMENT; |
|
1138 } |
|
1139 xmlOutputBufferWriteString(ctx->buf, "<"); |
|
1140 |
|
1141 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) { |
|
1142 xmlOutputBufferWriteString(ctx->buf, |
|
1143 (const char *) cur->ns->prefix); |
|
1144 xmlOutputBufferWriteString(ctx->buf, ":"); |
|
1145 } |
|
1146 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name); |
|
1147 } |
|
1148 |
|
1149 if (!ctx->exclusive) { |
|
1150 ret = xmlC14NProcessNamespacesAxis(ctx, cur, visible); |
|
1151 } else { |
|
1152 ret = xmlExcC14NProcessNamespacesAxis(ctx, cur, visible); |
|
1153 } |
|
1154 if (ret < 0) { |
|
1155 #ifdef DEBUG_C14N |
|
1156 xmlGenericError(xmlGenericErrorContext, |
|
1157 "xmlC14NProcessElementNode: xmlC14NProcessNamespacesAxis failed.\n"); |
|
1158 #endif |
|
1159 return (-1); |
|
1160 } |
|
1161 /* todo: shouldn't this go to "visible only"? */ |
|
1162 if(visible) { |
|
1163 xmlC14NVisibleNsStackShift(ctx->ns_rendered); |
|
1164 } |
|
1165 |
|
1166 if(visible) { |
|
1167 ret = xmlC14NProcessAttrsAxis(ctx, cur); |
|
1168 if (ret < 0) { |
|
1169 #ifdef DEBUG_C14N |
|
1170 xmlGenericError(xmlGenericErrorContext, |
|
1171 "xmlC14NProcessElementNode: xmlC14NProcessAttrsAxis failed.\n"); |
|
1172 #endif |
|
1173 return (-1); |
|
1174 } |
|
1175 } |
|
1176 |
|
1177 if (visible) { |
|
1178 xmlOutputBufferWriteString(ctx->buf, ">"); |
|
1179 } |
|
1180 if (cur->children != NULL) { |
|
1181 ret = xmlC14NProcessNodeList(ctx, cur->children); |
|
1182 if (ret < 0) { |
|
1183 #ifdef DEBUG_C14N |
|
1184 xmlGenericError(xmlGenericErrorContext, |
|
1185 "xmlC14NProcessElementNode: xmlC14NProcessNodeList failed.\n"); |
|
1186 #endif |
|
1187 return (-1); |
|
1188 } |
|
1189 } |
|
1190 if (visible) { |
|
1191 xmlOutputBufferWriteString(ctx->buf, "</"); |
|
1192 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) { |
|
1193 xmlOutputBufferWriteString(ctx->buf, |
|
1194 (const char *) cur->ns->prefix); |
|
1195 xmlOutputBufferWriteString(ctx->buf, ":"); |
|
1196 } |
|
1197 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name); |
|
1198 xmlOutputBufferWriteString(ctx->buf, ">"); |
|
1199 if (parent_is_doc) { |
|
1200 /* restore this flag from the stack for next node */ |
|
1201 ctx->parent_is_doc = parent_is_doc; |
|
1202 ctx->pos = XMLC14N_AFTER_DOCUMENT_ELEMENT; |
|
1203 } |
|
1204 } |
|
1205 |
|
1206 /* |
|
1207 * Restore ns_rendered stack position |
|
1208 */ |
|
1209 xmlC14NVisibleNsStackRestore(ctx->ns_rendered, &state); |
|
1210 return (0); |
|
1211 } |
|
1212 |
|
1213 /** |
|
1214 * xmlC14NProcessNode: |
|
1215 * @ctx: the pointer to C14N context object |
|
1216 * @cur: the node to process |
|
1217 * |
|
1218 * Processes the given node |
|
1219 * |
|
1220 * Returns non-negative value on success or negative value on fail |
|
1221 */ |
|
1222 static int |
|
1223 xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
|
1224 { |
|
1225 int ret = 0; |
|
1226 int visible; |
|
1227 |
|
1228 if ((ctx == NULL) || (cur == NULL)) { |
|
1229 #ifdef DEBUG_C14N |
|
1230 xmlGenericError(xmlGenericErrorContext, |
|
1231 "xmlC14NProcessNode: Null context or node pointer.\n"); |
|
1232 #endif |
|
1233 return (-1); |
|
1234 } |
|
1235 |
|
1236 visible = xmlC14NIsVisible(ctx, cur, cur->parent); |
|
1237 switch (cur->type) { |
|
1238 case XML_ELEMENT_NODE: |
|
1239 ret = xmlC14NProcessElementNode(ctx, cur, visible); |
|
1240 break; |
|
1241 case XML_CDATA_SECTION_NODE: |
|
1242 case XML_TEXT_NODE: |
|
1243 /* |
|
1244 * Text Nodes |
|
1245 * the string value, except all ampersands are replaced |
|
1246 * by &, all open angle brackets (<) are replaced by <, all closing |
|
1247 * angle brackets (>) are replaced by >, and all #xD characters are |
|
1248 * replaced by 
. |
|
1249 */ |
|
1250 /* cdata sections are processed as text nodes */ |
|
1251 /* todo: verify that cdata sections are included in XPath nodes set */ |
|
1252 if ((visible) && (cur->content != NULL)) { |
|
1253 xmlChar *buffer; |
|
1254 |
|
1255 buffer = xmlC11NNormalizeText(cur->content); |
|
1256 if (buffer != NULL) { |
|
1257 xmlOutputBufferWriteString(ctx->buf, |
|
1258 (const char *) buffer); |
|
1259 xmlFree(buffer); |
|
1260 } else { |
|
1261 #ifdef DEBUG_C14N |
|
1262 xmlGenericError(xmlGenericErrorContext, |
|
1263 "xmlC14NProcessNode: xmlC11NNormalizeText() failed\n"); |
|
1264 #endif |
|
1265 return (-1); |
|
1266 } |
|
1267 } |
|
1268 break; |
|
1269 case XML_PI_NODE: |
|
1270 /* |
|
1271 * Processing Instruction (PI) Nodes- |
|
1272 * The opening PI symbol (<?), the PI target name of the node, |
|
1273 * a leading space and the string value if it is not empty, and |
|
1274 * the closing PI symbol (?>). If the string value is empty, |
|
1275 * then the leading space is not added. Also, a trailing #xA is |
|
1276 * rendered after the closing PI symbol for PI children of the |
|
1277 * root node with a lesser document order than the document |
|
1278 * element, and a leading #xA is rendered before the opening PI |
|
1279 * symbol of PI children of the root node with a greater document |
|
1280 * order than the document element. |
|
1281 */ |
|
1282 if (visible) { |
|
1283 if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) { |
|
1284 xmlOutputBufferWriteString(ctx->buf, "\x0A<?"); |
|
1285 } else { |
|
1286 xmlOutputBufferWriteString(ctx->buf, "<?"); |
|
1287 } |
|
1288 |
|
1289 xmlOutputBufferWriteString(ctx->buf, |
|
1290 (const char *) cur->name); |
|
1291 if ((cur->content != NULL) && (*(cur->content) != '\0')) { |
|
1292 xmlChar *buffer; |
|
1293 |
|
1294 xmlOutputBufferWriteString(ctx->buf, " "); |
|
1295 |
|
1296 /* todo: do we need to normalize pi? */ |
|
1297 buffer = xmlC11NNormalizePI(cur->content); |
|
1298 if (buffer != NULL) { |
|
1299 xmlOutputBufferWriteString(ctx->buf, |
|
1300 (const char *) buffer); |
|
1301 xmlFree(buffer); |
|
1302 } else { |
|
1303 #ifdef DEBUG_C14N |
|
1304 xmlGenericError(xmlGenericErrorContext, |
|
1305 "xmlC14NProcessNode: xmlC11NNormalizePI() failed\n"); |
|
1306 #endif |
|
1307 return (-1); |
|
1308 } |
|
1309 } |
|
1310 |
|
1311 if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) { |
|
1312 xmlOutputBufferWriteString(ctx->buf, "?>\x0A"); |
|
1313 } else { |
|
1314 xmlOutputBufferWriteString(ctx->buf, "?>"); |
|
1315 } |
|
1316 } |
|
1317 break; |
|
1318 case XML_COMMENT_NODE: |
|
1319 /* |
|
1320 * Comment Nodes |
|
1321 * Nothing if generating canonical XML without comments. For |
|
1322 * canonical XML with comments, generate the opening comment |
|
1323 * symbol (<!--), the string value of the node, and the |
|
1324 * closing comment symbol (-->). Also, a trailing #xA is rendered |
|
1325 * after the closing comment symbol for comment children of the |
|
1326 * root node with a lesser document order than the document |
|
1327 * element, and a leading #xA is rendered before the opening |
|
1328 * comment symbol of comment children of the root node with a |
|
1329 * greater document order than the document element. (Comment |
|
1330 * children of the root node represent comments outside of the |
|
1331 * top-level document element and outside of the document type |
|
1332 * declaration). |
|
1333 */ |
|
1334 if (visible && ctx->with_comments) { |
|
1335 if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) { |
|
1336 xmlOutputBufferWriteString(ctx->buf, "\x0A<!--"); |
|
1337 } else { |
|
1338 xmlOutputBufferWriteString(ctx->buf, "<!--"); |
|
1339 } |
|
1340 |
|
1341 if (cur->content != NULL) { |
|
1342 xmlChar *buffer; |
|
1343 |
|
1344 /* todo: do we need to normalize comment? */ |
|
1345 buffer = xmlC11NNormalizeComment(cur->content); |
|
1346 if (buffer != NULL) { |
|
1347 xmlOutputBufferWriteString(ctx->buf, |
|
1348 (const char *) buffer); |
|
1349 xmlFree(buffer); |
|
1350 } else { |
|
1351 #ifdef DEBUG_C14N |
|
1352 xmlGenericError(xmlGenericErrorContext, |
|
1353 "xmlC14NProcessNode: xmlC11NNormalizeComment() failed\n"); |
|
1354 #endif |
|
1355 return (-1); |
|
1356 } |
|
1357 } |
|
1358 |
|
1359 if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) { |
|
1360 xmlOutputBufferWriteString(ctx->buf, "-->\x0A"); |
|
1361 } else { |
|
1362 xmlOutputBufferWriteString(ctx->buf, "-->"); |
|
1363 } |
|
1364 } |
|
1365 break; |
|
1366 case XML_DOCUMENT_NODE: |
|
1367 case XML_DOCUMENT_FRAG_NODE: /* should be processed as document? */ |
|
1368 #ifdef LIBXML_DOCB_ENABLED |
|
1369 case XML_DOCB_DOCUMENT_NODE: /* should be processed as document? */ |
|
1370 #endif |
|
1371 #ifdef LIBXML_HTML_ENABLED |
|
1372 case XML_HTML_DOCUMENT_NODE: /* should be processed as document? */ |
|
1373 #endif |
|
1374 if (cur->children != NULL) { |
|
1375 ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT; |
|
1376 ctx->parent_is_doc = 1; |
|
1377 ret = xmlC14NProcessNodeList(ctx, cur->children); |
|
1378 } |
|
1379 break; |
|
1380 |
|
1381 case XML_ATTRIBUTE_NODE: |
|
1382 xmlC14NErr(ctx, cur, XML_C14N_INVALID_NODE, |
|
1383 "xmlC14NProcessNode: XML_ATTRIBUTE_NODE is illegal here\n"); |
|
1384 return (-1); |
|
1385 case XML_NAMESPACE_DECL: |
|
1386 xmlC14NErr(ctx, cur, XML_C14N_INVALID_NODE, |
|
1387 "xmlC14NProcessNode: XML_NAMESPACE_DECL is illegal here\n"); |
|
1388 return (-1); |
|
1389 case XML_ENTITY_REF_NODE: |
|
1390 xmlC14NErr(ctx, cur, XML_C14N_INVALID_NODE, |
|
1391 "xmlC14NProcessNode: XML_ENTITY_REF_NODE is illegal here\n"); |
|
1392 return (-1); |
|
1393 case XML_ENTITY_NODE: |
|
1394 xmlC14NErr(ctx, cur, XML_C14N_INVALID_NODE, |
|
1395 "xmlC14NProcessNode: XML_ENTITY_NODE is illegal here\n"); |
|
1396 return (-1); |
|
1397 |
|
1398 case XML_DOCUMENT_TYPE_NODE: |
|
1399 case XML_NOTATION_NODE: |
|
1400 case XML_DTD_NODE: |
|
1401 case XML_ELEMENT_DECL: |
|
1402 case XML_ATTRIBUTE_DECL: |
|
1403 case XML_ENTITY_DECL: |
|
1404 #ifdef LIBXML_XINCLUDE_ENABLED |
|
1405 case XML_XINCLUDE_START: |
|
1406 case XML_XINCLUDE_END: |
|
1407 #endif |
|
1408 /* |
|
1409 * should be ignored according to "W3C Canonical XML" |
|
1410 */ |
|
1411 break; |
|
1412 default: |
|
1413 #ifdef DEBUG_C14N |
|
1414 xmlGenericError(xmlGenericErrorContext, |
|
1415 "xmlC14NProcessNode: unknown node type = %d\n", |
|
1416 cur->type); |
|
1417 #endif |
|
1418 return (-1); |
|
1419 } |
|
1420 |
|
1421 return (ret); |
|
1422 } |
|
1423 |
|
1424 /** |
|
1425 * xmlC14NProcessNodeList: |
|
1426 * @ctx: the pointer to C14N context object |
|
1427 * @cur: the node to start from |
|
1428 * |
|
1429 * Processes all nodes in the row starting from cur. |
|
1430 * |
|
1431 * Returns non-negative value on success or negative value on fail |
|
1432 */ |
|
1433 static int |
|
1434 xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
|
1435 { |
|
1436 int ret; |
|
1437 |
|
1438 if (ctx == NULL) { |
|
1439 #ifdef DEBUG_C14N |
|
1440 xmlGenericError(xmlGenericErrorContext, |
|
1441 "xmlC14NProcessNodeList: Null context pointer.\n"); |
|
1442 #endif |
|
1443 return (-1); |
|
1444 } |
|
1445 |
|
1446 for (ret = 0; cur != NULL && ret >= 0; cur = cur->next) { |
|
1447 ret = xmlC14NProcessNode(ctx, cur); |
|
1448 } |
|
1449 return (ret); |
|
1450 } |
|
1451 |
|
1452 |
|
1453 /** |
|
1454 * xmlC14NFreeCtx: |
|
1455 * @ctx: the pointer to C14N context object |
|
1456 * |
|
1457 * Cleanups the C14N context object. |
|
1458 */ |
|
1459 |
|
1460 static void |
|
1461 xmlC14NFreeCtx(xmlC14NCtxPtr ctx) |
|
1462 { |
|
1463 if (ctx == NULL) { |
|
1464 #ifdef DEBUG_C14N |
|
1465 xmlGenericError(xmlGenericErrorContext, |
|
1466 "xmlC14NFreeCtx: ctx == NULL\n"); |
|
1467 #endif |
|
1468 return; |
|
1469 } |
|
1470 |
|
1471 if (ctx->ns_rendered != NULL) { |
|
1472 xmlC14NVisibleNsStackDestroy(ctx->ns_rendered); |
|
1473 } |
|
1474 xmlFree(ctx); |
|
1475 } |
|
1476 |
|
1477 /** |
|
1478 * xmlC14NNewCtx: |
|
1479 * @doc: the XML document for canonization |
|
1480 * @is_visible_callback:the function to use to determine is node visible |
|
1481 * or not |
|
1482 * @user_data: the first parameter for @is_visible_callback function |
|
1483 * (in most cases, it is nodes set) |
|
1484 * @inclusive_ns_prefixe the list of inclusive namespace prefixes |
|
1485 * ended with a NULL or NULL if there is no |
|
1486 * inclusive namespaces (only for exclusive |
|
1487 * canonicalization) |
|
1488 * @with_comments: include comments in the result (!=0) or not (==0) |
|
1489 * @buf: the output buffer to store canonical XML; this |
|
1490 * buffer MUST have encoder==NULL because C14N requires |
|
1491 * UTF-8 output |
|
1492 * |
|
1493 * Creates new C14N context object to store C14N parameters. |
|
1494 * |
|
1495 * Returns pointer to newly created object (success) or NULL (fail) |
|
1496 */ |
|
1497 static xmlC14NCtxPtr |
|
1498 xmlC14NNewCtx(xmlDocPtr doc, |
|
1499 xmlC14NIsVisibleCallback is_visible_callback, void* user_data, |
|
1500 int exclusive, xmlChar ** inclusive_ns_prefixes, |
|
1501 int with_comments, xmlOutputBufferPtr buf) |
|
1502 { |
|
1503 xmlC14NCtxPtr ctx = NULL; |
|
1504 |
|
1505 if ((doc == NULL) || (buf == NULL)) { |
|
1506 #ifdef DEBUG_C14N |
|
1507 xmlGenericError(xmlGenericErrorContext, |
|
1508 "xmlC14NNewCtx: pointer to document or output buffer is NULL\n"); |
|
1509 #endif |
|
1510 return (NULL); |
|
1511 } |
|
1512 |
|
1513 /* |
|
1514 * Validate the encoding output buffer encoding |
|
1515 */ |
|
1516 if (buf->encoder != NULL) { |
|
1517 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8, |
|
1518 "xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n"); |
|
1519 return (NULL); |
|
1520 } |
|
1521 |
|
1522 /* |
|
1523 * Validate the XML document encoding value, if provided. |
|
1524 */ |
|
1525 if (doc->charset != XML_CHAR_ENCODING_UTF8) { |
|
1526 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8, |
|
1527 "xmlC14NNewCtx: source document not in UTF8\n"); |
|
1528 return (NULL); |
|
1529 } |
|
1530 |
|
1531 /* |
|
1532 * Allocate a new xmlC14NCtxPtr and fill the fields. |
|
1533 */ |
|
1534 ctx = (xmlC14NCtxPtr) xmlMalloc(sizeof(xmlC14NCtx)); |
|
1535 if (ctx == NULL) { |
|
1536 xmlC14NErrMemory("creating context"); |
|
1537 return (NULL); |
|
1538 } |
|
1539 memset(ctx, 0, sizeof(xmlC14NCtx)); |
|
1540 |
|
1541 /* |
|
1542 * initialize C14N context |
|
1543 */ |
|
1544 ctx->doc = doc; |
|
1545 ctx->with_comments = with_comments; |
|
1546 ctx->is_visible_callback = is_visible_callback; |
|
1547 ctx->user_data = user_data; |
|
1548 ctx->buf = buf; |
|
1549 ctx->parent_is_doc = 1; |
|
1550 ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT; |
|
1551 ctx->ns_rendered = xmlC14NVisibleNsStackCreate(); |
|
1552 |
|
1553 if(ctx->ns_rendered == NULL) { |
|
1554 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_CREATE_STACK, |
|
1555 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n"); |
|
1556 xmlC14NFreeCtx(ctx); |
|
1557 return (NULL); |
|
1558 } |
|
1559 |
|
1560 /* |
|
1561 * Set "exclusive" flag, create a nodes set for namespaces |
|
1562 * stack and remember list of incluseve prefixes |
|
1563 */ |
|
1564 if (exclusive) { |
|
1565 ctx->exclusive = 1; |
|
1566 ctx->inclusive_ns_prefixes = inclusive_ns_prefixes; |
|
1567 } |
|
1568 return (ctx); |
|
1569 } |
|
1570 |
|
1571 /** |
|
1572 * xmlC14NExecute: |
|
1573 * @doc: the XML document for canonization |
|
1574 * @is_visible_callback:the function to use to determine is node visible |
|
1575 * or not |
|
1576 * @user_data: the first parameter for @is_visible_callback function |
|
1577 * (in most cases, it is nodes set) |
|
1578 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
|
1579 * otherwise - exclusive canonicalization) |
|
1580 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
|
1581 * ended with a NULL or NULL if there is no |
|
1582 * inclusive namespaces (only for exclusive |
|
1583 * canonicalization, ignored otherwise) |
|
1584 * @with_comments: include comments in the result (!=0) or not (==0) |
|
1585 * @buf: the output buffer to store canonical XML; this |
|
1586 * buffer MUST have encoder==NULL because C14N requires |
|
1587 * UTF-8 output |
|
1588 * |
|
1589 * Dumps the canonized image of given XML document into the provided buffer. |
|
1590 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
|
1591 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
|
1592 * |
|
1593 * Returns non-negative value on success or a negative value on fail |
|
1594 */ |
|
1595 int |
|
1596 xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback, |
|
1597 void* user_data, int exclusive, xmlChar **inclusive_ns_prefixes, |
|
1598 int with_comments, xmlOutputBufferPtr buf) { |
|
1599 |
|
1600 xmlC14NCtxPtr ctx; |
|
1601 int ret; |
|
1602 |
|
1603 if ((buf == NULL) || (doc == NULL)) { |
|
1604 #ifdef DEBUG_C14N |
|
1605 xmlGenericError(xmlGenericErrorContext, |
|
1606 "xmlC14NExecute: null return buffer or doc pointer\n"); |
|
1607 #endif |
|
1608 return (-1); |
|
1609 } |
|
1610 |
|
1611 /* |
|
1612 * Validate the encoding output buffer encoding |
|
1613 */ |
|
1614 if (buf->encoder != NULL) { |
|
1615 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8, |
|
1616 "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n"); |
|
1617 return (-1); |
|
1618 } |
|
1619 |
|
1620 ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data, |
|
1621 exclusive, inclusive_ns_prefixes, |
|
1622 with_comments, buf); |
|
1623 if (ctx == NULL) { |
|
1624 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_CREATE_CTXT, |
|
1625 "xmlC14NExecute: unable to create C14N context\n"); |
|
1626 return (-1); |
|
1627 } |
|
1628 |
|
1629 |
|
1630 |
|
1631 /* |
|
1632 * Root Node |
|
1633 * The root node is the parent of the top-level document element. The |
|
1634 * result of processing each of its child nodes that is in the node-set |
|
1635 * in document order. The root node does not generate a byte order mark, |
|
1636 * XML declaration, nor anything from within the document type |
|
1637 * declaration. |
|
1638 */ |
|
1639 if (doc->children != NULL) { |
|
1640 ret = xmlC14NProcessNodeList(ctx, doc->children); |
|
1641 if (ret < 0) { |
|
1642 #ifdef DEBUG_C14N |
|
1643 xmlGenericError(xmlGenericErrorContext, |
|
1644 "xmlC14NExecute: process childrens' list failed.\n"); |
|
1645 #endif |
|
1646 xmlC14NFreeCtx(ctx); |
|
1647 return (-1); |
|
1648 } |
|
1649 } |
|
1650 |
|
1651 /* |
|
1652 * Flush buffer to get number of bytes written |
|
1653 */ |
|
1654 ret = xmlOutputBufferFlush(buf); |
|
1655 if (ret < 0) { |
|
1656 #ifdef DEBUG_C14N |
|
1657 xmlGenericError(xmlGenericErrorContext, |
|
1658 "xmlC14NExecute: buffer flush failed.\n"); |
|
1659 #endif |
|
1660 xmlC14NFreeCtx(ctx); |
|
1661 return (-1); |
|
1662 } |
|
1663 |
|
1664 /* |
|
1665 * Cleanup |
|
1666 */ |
|
1667 xmlC14NFreeCtx(ctx); |
|
1668 return (ret); |
|
1669 } |
|
1670 |
|
1671 /** |
|
1672 * xmlC14NDocSaveTo: |
|
1673 * @doc: the XML document for canonization |
|
1674 * @nodes: the nodes set to be included in the canonized image |
|
1675 * or NULL if all document nodes should be included |
|
1676 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
|
1677 * otherwise - exclusive canonicalization) |
|
1678 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
|
1679 * ended with a NULL or NULL if there is no |
|
1680 * inclusive namespaces (only for exclusive |
|
1681 * canonicalization, ignored otherwise) |
|
1682 * @with_comments: include comments in the result (!=0) or not (==0) |
|
1683 * @buf: the output buffer to store canonical XML; this |
|
1684 * buffer MUST have encoder==NULL because C14N requires |
|
1685 * UTF-8 output |
|
1686 * |
|
1687 * Dumps the canonized image of given XML document into the provided buffer. |
|
1688 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
|
1689 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
|
1690 * |
|
1691 * Returns non-negative value on success or a negative value on fail |
|
1692 */ |
|
1693 int |
|
1694 xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes, |
|
1695 int exclusive, xmlChar ** inclusive_ns_prefixes, |
|
1696 int with_comments, xmlOutputBufferPtr buf) { |
|
1697 return(xmlC14NExecute(doc, |
|
1698 (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset, |
|
1699 nodes, |
|
1700 exclusive, |
|
1701 inclusive_ns_prefixes, |
|
1702 with_comments, |
|
1703 buf)); |
|
1704 } |
|
1705 |
|
1706 |
|
1707 /** |
|
1708 * xmlC14NDocDumpMemory: |
|
1709 * @doc: the XML document for canonization |
|
1710 * @nodes: the nodes set to be included in the canonized image |
|
1711 * or NULL if all document nodes should be included |
|
1712 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
|
1713 * otherwise - exclusive canonicalization) |
|
1714 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
|
1715 * ended with a NULL or NULL if there is no |
|
1716 * inclusive namespaces (only for exclusive |
|
1717 * canonicalization, ignored otherwise) |
|
1718 * @with_comments: include comments in the result (!=0) or not (==0) |
|
1719 * @doc_txt_ptr: the memory pointer for allocated canonical XML text; |
|
1720 * the caller of this functions is responsible for calling |
|
1721 * xmlFree() to free allocated memory |
|
1722 * |
|
1723 * Dumps the canonized image of given XML document into memory. |
|
1724 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
|
1725 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
|
1726 * |
|
1727 * Returns the number of bytes written on success or a negative value on fail |
|
1728 */ |
|
1729 int |
|
1730 xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes, |
|
1731 int exclusive, xmlChar ** inclusive_ns_prefixes, |
|
1732 int with_comments, xmlChar ** doc_txt_ptr) |
|
1733 { |
|
1734 int ret; |
|
1735 xmlOutputBufferPtr buf; |
|
1736 |
|
1737 if (doc_txt_ptr == NULL) { |
|
1738 #ifdef DEBUG_C14N |
|
1739 xmlGenericError(xmlGenericErrorContext, |
|
1740 "xmlC14NDocDumpMemory: null return buffer pointer\n"); |
|
1741 #endif |
|
1742 return (-1); |
|
1743 } |
|
1744 |
|
1745 *doc_txt_ptr = NULL; |
|
1746 |
|
1747 /* |
|
1748 * create memory buffer with UTF8 (default) encoding |
|
1749 */ |
|
1750 buf = xmlAllocOutputBuffer(NULL); |
|
1751 if (buf == NULL) { |
|
1752 #ifdef DEBUG_C14N |
|
1753 xmlGenericError(xmlGenericErrorContext, |
|
1754 "xmlC14NDocDumpMemory: failed to allocate output buffer.\n"); |
|
1755 #endif |
|
1756 return (-1); |
|
1757 } |
|
1758 |
|
1759 /* |
|
1760 * canonize document and write to buffer |
|
1761 */ |
|
1762 ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes, |
|
1763 with_comments, buf); |
|
1764 if (ret < 0) { |
|
1765 #ifdef DEBUG_C14N |
|
1766 xmlGenericError(xmlGenericErrorContext, |
|
1767 "xmlC14NDocDumpMemory: xmlC14NDocSaveTo failed.\n"); |
|
1768 #endif |
|
1769 (void) xmlOutputBufferClose(buf); |
|
1770 return (-1); |
|
1771 } |
|
1772 |
|
1773 ret = buf->buffer->use; |
|
1774 if (ret > 0) { |
|
1775 *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret); |
|
1776 } |
|
1777 (void) xmlOutputBufferClose(buf); |
|
1778 |
|
1779 if ((*doc_txt_ptr == NULL) && (ret > 0)) { |
|
1780 #ifdef DEBUG_C14N |
|
1781 xmlGenericError(xmlGenericErrorContext, |
|
1782 "xmlC14NDocDumpMemory: failed to allocate memory for document text representation\n"); |
|
1783 #endif |
|
1784 return (-1); |
|
1785 } |
|
1786 return (ret); |
|
1787 } |
|
1788 |
|
1789 /** |
|
1790 * xmlC14NDocSave: |
|
1791 * @doc: the XML document for canonization |
|
1792 * @nodes: the nodes set to be included in the canonized image |
|
1793 * or NULL if all document nodes should be included |
|
1794 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
|
1795 * otherwise - exclusive canonicalization) |
|
1796 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
|
1797 * ended with a NULL or NULL if there is no |
|
1798 * inclusive namespaces (only for exclusive |
|
1799 * canonicalization, ignored otherwise) |
|
1800 * @with_comments: include comments in the result (!=0) or not (==0) |
|
1801 * @filename: the filename to store canonical XML image |
|
1802 * @compression: the compression level (zlib requred): |
|
1803 * -1 - libxml default, |
|
1804 * 0 - uncompressed, |
|
1805 * >0 - compression level |
|
1806 * |
|
1807 * Dumps the canonized image of given XML document into the file. |
|
1808 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
|
1809 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
|
1810 * |
|
1811 * Returns the number of bytes written success or a negative value on fail |
|
1812 */ |
|
1813 int |
|
1814 xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, |
|
1815 int exclusive, xmlChar ** inclusive_ns_prefixes, |
|
1816 int with_comments, const char *filename, int compression) |
|
1817 { |
|
1818 xmlOutputBufferPtr buf; |
|
1819 int ret; |
|
1820 |
|
1821 if (filename == NULL) { |
|
1822 #ifdef DEBUG_C14N |
|
1823 xmlGenericError(xmlGenericErrorContext, |
|
1824 "xmlC14NDocSave: filename is NULL\n"); |
|
1825 #endif |
|
1826 return (-1); |
|
1827 } |
|
1828 #ifdef HAVE_ZLIB_H |
|
1829 if (compression < 0) |
|
1830 compression = xmlGetCompressMode(); |
|
1831 #endif |
|
1832 |
|
1833 /* |
|
1834 * save the content to a temp buffer, use default UTF8 encoding. |
|
1835 */ |
|
1836 buf = xmlOutputBufferCreateFilename(filename, NULL, compression); |
|
1837 if (buf == NULL) { |
|
1838 #ifdef DEBUG_C14N |
|
1839 xmlGenericError(xmlGenericErrorContext, |
|
1840 "xmlC14NDocSave: unable to create buffer for file=\"%s\" with compressin=%d\n", |
|
1841 filename, compression); |
|
1842 #endif |
|
1843 return (-1); |
|
1844 } |
|
1845 |
|
1846 /* |
|
1847 * canonize document and write to buffer |
|
1848 */ |
|
1849 ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes, |
|
1850 with_comments, buf); |
|
1851 if (ret < 0) { |
|
1852 #ifdef DEBUG_C14N |
|
1853 xmlGenericError(xmlGenericErrorContext, |
|
1854 "xmlC14NDocSave: xmlC14NDocSaveTo failed.\n"); |
|
1855 #endif |
|
1856 (void) xmlOutputBufferClose(buf); |
|
1857 return (-1); |
|
1858 } |
|
1859 |
|
1860 /* |
|
1861 * get the numbers of bytes written |
|
1862 */ |
|
1863 ret = xmlOutputBufferClose(buf); |
|
1864 return (ret); |
|
1865 } |
|
1866 |
|
1867 |
|
1868 |
|
1869 /* |
|
1870 * Macro used to grow the current buffer. |
|
1871 */ // DONE: Fix xmlRealloc // TODO: Make it parametrized |
|
1872 #define growBufferReentrant() { \ |
|
1873 void* tmp; \ |
|
1874 buffer_size *= 2; \ |
|
1875 tmp = xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ |
|
1876 if (!tmp) { \ |
|
1877 /* Note: must free the buffer */ \ |
|
1878 xmlFree(buffer); \ |
|
1879 xmlC14NErrMemory("growing buffer"); \ |
|
1880 return(NULL); \ |
|
1881 } \ |
|
1882 buffer = (xmlChar*) tmp; \ |
|
1883 } |
|
1884 |
|
1885 /** |
|
1886 * xmlC11NNormalizeString: |
|
1887 * @input: the input string |
|
1888 * @mode: the normalization mode (attribute, comment, PI or text) |
|
1889 * |
|
1890 * Converts a string to a canonical (normalized) format. The code is stolen |
|
1891 * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A |
|
1892 * and the @mode parameter |
|
1893 * |
|
1894 * Returns a normalized string (caller is responsible for calling xmlFree()) |
|
1895 * or NULL if an error occurs |
|
1896 */ |
|
1897 static xmlChar * |
|
1898 xmlC11NNormalizeString(const xmlChar * input, |
|
1899 xmlC14NNormalizationMode mode) |
|
1900 { |
|
1901 const xmlChar *cur = input; |
|
1902 xmlChar *buffer = NULL; |
|
1903 xmlChar *out = NULL; |
|
1904 int buffer_size = 0; |
|
1905 |
|
1906 if (input == NULL) |
|
1907 return (NULL); |
|
1908 |
|
1909 /* |
|
1910 * allocate an translation buffer. |
|
1911 */ |
|
1912 buffer_size = 1000; |
|
1913 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
|
1914 if (buffer == NULL) { |
|
1915 xmlC14NErrMemory("allocating buffer"); |
|
1916 return (NULL); |
|
1917 } |
|
1918 out = buffer; |
|
1919 |
|
1920 while (*cur != '\0') { |
|
1921 if ((out - buffer) > (buffer_size - 10)) { |
|
1922 int indx = out - buffer; |
|
1923 |
|
1924 growBufferReentrant(); |
|
1925 out = &buffer[indx]; |
|
1926 } |
|
1927 |
|
1928 if ((*cur == '<') && ((mode == XMLC14N_NORMALIZE_ATTR) || |
|
1929 (mode == XMLC14N_NORMALIZE_TEXT))) { |
|
1930 *out++ = '&'; |
|
1931 *out++ = 'l'; |
|
1932 *out++ = 't'; |
|
1933 *out++ = ';'; |
|
1934 } else if ((*cur == '>') && (mode == XMLC14N_NORMALIZE_TEXT)) { |
|
1935 *out++ = '&'; |
|
1936 *out++ = 'g'; |
|
1937 *out++ = 't'; |
|
1938 *out++ = ';'; |
|
1939 } else if ((*cur == '&') && ((mode == XMLC14N_NORMALIZE_ATTR) || |
|
1940 (mode == XMLC14N_NORMALIZE_TEXT))) { |
|
1941 *out++ = '&'; |
|
1942 *out++ = 'a'; |
|
1943 *out++ = 'm'; |
|
1944 *out++ = 'p'; |
|
1945 *out++ = ';'; |
|
1946 } else if ((*cur == '"') && (mode == XMLC14N_NORMALIZE_ATTR)) { |
|
1947 *out++ = '&'; |
|
1948 *out++ = 'q'; |
|
1949 *out++ = 'u'; |
|
1950 *out++ = 'o'; |
|
1951 *out++ = 't'; |
|
1952 *out++ = ';'; |
|
1953 } else if ((*cur == '\x09') && (mode == XMLC14N_NORMALIZE_ATTR)) { |
|
1954 *out++ = '&'; |
|
1955 *out++ = '#'; |
|
1956 *out++ = 'x'; |
|
1957 *out++ = '9'; |
|
1958 *out++ = ';'; |
|
1959 } else if ((*cur == '\x0A') && (mode == XMLC14N_NORMALIZE_ATTR)) { |
|
1960 *out++ = '&'; |
|
1961 *out++ = '#'; |
|
1962 *out++ = 'x'; |
|
1963 *out++ = 'A'; |
|
1964 *out++ = ';'; |
|
1965 } else if ((*cur == '\x0D') && ((mode == XMLC14N_NORMALIZE_ATTR) || |
|
1966 (mode == XMLC14N_NORMALIZE_TEXT) || |
|
1967 (mode == XMLC14N_NORMALIZE_COMMENT) || |
|
1968 (mode == XMLC14N_NORMALIZE_PI))) { |
|
1969 *out++ = '&'; |
|
1970 *out++ = '#'; |
|
1971 *out++ = 'x'; |
|
1972 *out++ = 'D'; |
|
1973 *out++ = ';'; |
|
1974 } else { |
|
1975 /* |
|
1976 * Works because on UTF-8, all extended sequences cannot |
|
1977 * result in bytes in the ASCII range. |
|
1978 */ |
|
1979 *out++ = *cur; |
|
1980 } |
|
1981 cur++; |
|
1982 } |
|
1983 *out++ = 0; |
|
1984 return (buffer); |
|
1985 } |
|
1986 #endif /* LIBXML_OUTPUT_ENABLED */ |
|
1987 #endif /* LIBXML_C14N_ENABLED */ |