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