2 * "Canonical XML" implementation
3 * http://www.w3.org/TR/xml-c14n
5 * "Exclusive XML Canonicalization" implementation
6 * http://www.w3.org/TR/xml-exc-c14n
8 * See Copyright for the status of this software.
10 * Author: Aleksey Sanin <aleksey@aleksey.com>
14 #ifdef LIBXML_C14N_ENABLED
15 #ifdef LIBXML_OUTPUT_ENABLED
22 #include <libxml/tree.h>
23 #include <libxml/parser.h>
24 #include <libxml/uri.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/globals.h>
27 #include <libxml/xpathInternals.h>
28 #include <libxml/c14n.h>
32 /************************************************************************
34 * Some declaration better left private ATM *
36 ************************************************************************/
39 XMLC14N_BEFORE_DOCUMENT_ELEMENT
= 0,
40 XMLC14N_INSIDE_DOCUMENT_ELEMENT
= 1,
41 XMLC14N_AFTER_DOCUMENT_ELEMENT
= 2
44 typedef struct _xmlC14NVisibleNsStack
{
45 int nsCurEnd
; /* number of nodes in the set */
46 int nsPrevStart
; /* the begginning of the stack for previous visible node */
47 int nsPrevEnd
; /* the end of the stack for previous visible node */
48 int nsMax
; /* size of the array as allocated */
49 xmlNsPtr
*nsTab
; /* array of ns in no particular order */
50 xmlNodePtr
*nodeTab
; /* array of nodes in no particular order */
51 } xmlC14NVisibleNsStack
, *xmlC14NVisibleNsStackPtr
;
53 typedef struct _xmlC14NCtx
{
54 /* input parameters */
56 xmlC14NIsVisibleCallback is_visible_callback
;
59 xmlOutputBufferPtr buf
;
61 /* position in the XML document */
64 xmlC14NVisibleNsStackPtr ns_rendered
;
69 /* exclusive canonicalization */
70 xmlChar
**inclusive_ns_prefixes
;
74 } xmlC14NCtx
, *xmlC14NCtxPtr
;
76 static xmlC14NVisibleNsStackPtr
xmlC14NVisibleNsStackCreate (void);
77 static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur
);
78 static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cur
,
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
,
88 static int xmlExcC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur
,
92 static int xmlC14NIsNodeInNodeset (void *user_data
,
98 static int xmlC14NProcessNode(xmlC14NCtxPtr ctx
, xmlNodePtr cur
);
99 static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx
, xmlNodePtr cur
);
101 XMLC14N_NORMALIZE_ATTR
= 0,
102 XMLC14N_NORMALIZE_COMMENT
= 1,
103 XMLC14N_NORMALIZE_PI
= 2,
104 XMLC14N_NORMALIZE_TEXT
= 3
105 } xmlC14NNormalizationMode
;
107 static xmlChar
*xmlC11NNormalizeString(const xmlChar
* input
,
108 xmlC14NNormalizationMode mode
);
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)
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)
124 #define xmlC14NIsExclusive( ctx ) \
125 ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 )
127 /************************************************************************
129 * Some factorized error routines *
131 ************************************************************************/
135 * @extra: extra informations
137 * Handle a redefinition of memory error
140 xmlC14NErrMemory(const char *extra
)
142 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
143 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0, extra
,
145 "Memory allocation failed : %s\n", extra
);
150 * @extra: extra informations
152 * Handle a redefinition of param error
155 xmlC14NErrParam(const char *extra
)
157 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
158 XML_ERR_INTERNAL_ERROR
, XML_ERR_ERROR
, NULL
, 0, extra
,
160 "Invalid parameter : %s\n", extra
);
164 * xmlC14NErrInternal:
165 * @extra: extra informations
167 * Handle a redefinition of internal error
170 xmlC14NErrInternal(const char *extra
)
172 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
173 XML_ERR_INTERNAL_ERROR
, XML_ERR_ERROR
, NULL
, 0, extra
,
175 "Internal error : %s\n", extra
);
179 * xmlC14NErrInvalidNode:
180 * @extra: extra informations
182 * Handle a redefinition of invalid node error
185 xmlC14NErrInvalidNode(const char *node_type
, const char *extra
)
187 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
188 XML_C14N_INVALID_NODE
, XML_ERR_ERROR
, NULL
, 0, extra
,
190 "Node %s is invalid here : %s\n", node_type
, extra
);
194 * xmlC14NErrUnknownNode:
195 * @extra: extra informations
197 * Handle a redefinition of unknown node error
200 xmlC14NErrUnknownNode(int node_type
, const char *extra
)
202 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
203 XML_C14N_UNKNOW_NODE
, XML_ERR_ERROR
, NULL
, 0, extra
,
205 "Unknown node type %d found : %s\n", node_type
, extra
);
209 * xmlC14NErrRelativeNamespace:
210 * @extra: extra informations
212 * Handle a redefinition of relative namespace error
215 xmlC14NErrRelativeNamespace(const char *ns_uri
)
217 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
218 XML_C14N_RELATIVE_NAMESPACE
, XML_ERR_ERROR
, NULL
, 0, NULL
,
220 "Relative namespace UR is invalid here : %s\n", ns_uri
);
227 * @ctxt: a C14N evaluation context
228 * @node: the context node
229 * @error: the erorr code
231 * @extra: extra informations
233 * Handle a redefinition of attribute error
236 xmlC14NErr(xmlC14NCtxPtr ctxt
, xmlNodePtr node
, int error
,
241 __xmlRaiseError(NULL
, NULL
, NULL
,
242 ctxt
, node
, XML_FROM_C14N
, error
,
243 XML_ERR_ERROR
, NULL
, 0,
244 NULL
, NULL
, NULL
, 0, 0, "%s", msg
);
247 /************************************************************************
249 * The implementation internals *
251 ************************************************************************/
252 #define XML_NAMESPACES_DEFAULT 16
255 xmlC14NIsNodeInNodeset(void *user_data
, xmlNodePtr node
, xmlNodePtr parent
) {
256 xmlNodeSetPtr nodes
= (xmlNodeSetPtr
) user_data
;
257 if((nodes
!= NULL
) && (node
!= NULL
)) {
258 if(node
->type
!= XML_NAMESPACE_DECL
) {
259 return(xmlXPathNodeSetContains(nodes
, node
));
263 memcpy(&ns
, node
, sizeof(ns
));
265 /* this is a libxml hack! check xpath.c for details */
266 if((parent
!= NULL
) && (parent
->type
== XML_ATTRIBUTE_NODE
)) {
267 ns
.next
= (xmlNsPtr
)parent
->parent
;
269 ns
.next
= (xmlNsPtr
)parent
;
273 * If the input is an XPath node-set, then the node-set must explicitly
274 * contain every node to be rendered to the canonical form.
276 return(xmlXPathNodeSetContains(nodes
, (xmlNodePtr
)&ns
));
282 static xmlC14NVisibleNsStackPtr
283 xmlC14NVisibleNsStackCreate(void) {
284 xmlC14NVisibleNsStackPtr ret
;
286 ret
= (xmlC14NVisibleNsStackPtr
) xmlMalloc(sizeof(xmlC14NVisibleNsStack
));
288 xmlC14NErrMemory("creating namespaces stack");
291 memset(ret
, 0 , (size_t) sizeof(xmlC14NVisibleNsStack
));
296 xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr cur
) {
298 xmlC14NErrParam("destroying namespaces stack");
301 if(cur
->nsTab
!= NULL
) {
302 memset(cur
->nsTab
, 0, cur
->nsMax
* sizeof(xmlNsPtr
));
305 if(cur
->nodeTab
!= NULL
) {
306 memset(cur
->nodeTab
, 0, cur
->nsMax
* sizeof(xmlNodePtr
));
307 xmlFree(cur
->nodeTab
);
309 memset(cur
, 0, sizeof(xmlC14NVisibleNsStack
));
315 xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur
, xmlNsPtr ns
, xmlNodePtr node
) {
317 ((cur
->nsTab
== NULL
) && (cur
->nodeTab
!= NULL
)) ||
318 ((cur
->nsTab
!= NULL
) && (cur
->nodeTab
== NULL
))) {
319 xmlC14NErrParam("adding namespace to stack");
323 if ((cur
->nsTab
== NULL
) && (cur
->nodeTab
== NULL
)) {
324 cur
->nsTab
= (xmlNsPtr
*) xmlMalloc(XML_NAMESPACES_DEFAULT
* sizeof(xmlNsPtr
));
325 cur
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NAMESPACES_DEFAULT
* sizeof(xmlNodePtr
));
326 if ((cur
->nsTab
== NULL
) || (cur
->nodeTab
== NULL
)) {
327 xmlC14NErrMemory("adding node to stack");
330 memset(cur
->nsTab
, 0 , XML_NAMESPACES_DEFAULT
* sizeof(xmlNsPtr
));
331 memset(cur
->nodeTab
, 0 , XML_NAMESPACES_DEFAULT
* sizeof(xmlNodePtr
));
332 cur
->nsMax
= XML_NAMESPACES_DEFAULT
;
333 } else if(cur
->nsMax
== cur
->nsCurEnd
) {
337 tmpSize
= 2 * cur
->nsMax
;
338 tmp
= xmlRealloc(cur
->nsTab
, tmpSize
* sizeof(xmlNsPtr
));
340 xmlC14NErrMemory("adding node to stack");
343 cur
->nsTab
= (xmlNsPtr
*)tmp
;
345 tmp
= xmlRealloc(cur
->nodeTab
, tmpSize
* sizeof(xmlNodePtr
));
347 xmlC14NErrMemory("adding node to stack");
350 cur
->nodeTab
= (xmlNodePtr
*)tmp
;
352 cur
->nsMax
= tmpSize
;
354 cur
->nsTab
[cur
->nsCurEnd
] = ns
;
355 cur
->nodeTab
[cur
->nsCurEnd
] = node
;
361 xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur
, xmlC14NVisibleNsStackPtr state
) {
362 if((cur
== NULL
) || (state
== NULL
)) {
363 xmlC14NErrParam("saving namespaces stack");
367 state
->nsCurEnd
= cur
->nsCurEnd
;
368 state
->nsPrevStart
= cur
->nsPrevStart
;
369 state
->nsPrevEnd
= cur
->nsPrevEnd
;
373 xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr cur
, xmlC14NVisibleNsStackPtr state
) {
374 if((cur
== NULL
) || (state
== NULL
)) {
375 xmlC14NErrParam("restoring namespaces stack");
378 cur
->nsCurEnd
= state
->nsCurEnd
;
379 cur
->nsPrevStart
= state
->nsPrevStart
;
380 cur
->nsPrevEnd
= state
->nsPrevEnd
;
384 xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur
) {
386 xmlC14NErrParam("shifting namespaces stack");
389 cur
->nsPrevStart
= cur
->nsPrevEnd
;
390 cur
->nsPrevEnd
= cur
->nsCurEnd
;
394 xmlC14NStrEqual(const xmlChar
*str1
, const xmlChar
*str2
) {
395 if (str1
== str2
) return(1);
396 if (str1
== NULL
) return((*str2
) == '\0');
397 if (str2
== NULL
) return((*str1
) == '\0');
399 if (*str1
++ != *str2
) return(0);
405 * xmlC14NVisibleNsStackFind:
406 * @ctx: the C14N context
407 * @ns: the namespace to check
409 * Checks whether the given namespace was already rendered or not
411 * Returns 1 if we already wrote this namespace or 0 otherwise
414 xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
, xmlNsPtr ns
)
417 const xmlChar
*prefix
;
422 xmlC14NErrParam("searching namespaces stack (c14n)");
427 * if the default namespace xmlns="" is not defined yet then
428 * we do not want to print it out
430 prefix
= ((ns
== NULL
) || (ns
->prefix
== NULL
)) ? BAD_CAST
"" : ns
->prefix
;
431 href
= ((ns
== NULL
) || (ns
->href
== NULL
)) ? BAD_CAST
"" : ns
->href
;
432 has_empty_ns
= (xmlC14NStrEqual(prefix
, NULL
) && xmlC14NStrEqual(href
, NULL
));
434 if (cur
->nsTab
!= NULL
) {
435 int start
= (has_empty_ns
) ? 0 : cur
->nsPrevStart
;
436 for (i
= cur
->nsCurEnd
- 1; i
>= start
; --i
) {
437 xmlNsPtr ns1
= cur
->nsTab
[i
];
439 if(xmlC14NStrEqual(prefix
, (ns1
!= NULL
) ? ns1
->prefix
: NULL
)) {
440 return(xmlC14NStrEqual(href
, (ns1
!= NULL
) ? ns1
->href
: NULL
));
444 return(has_empty_ns
);
448 xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
, xmlNsPtr ns
, xmlC14NCtxPtr ctx
) {
450 const xmlChar
*prefix
;
455 xmlC14NErrParam("searching namespaces stack (exc c14n)");
460 * if the default namespace xmlns="" is not defined yet then
461 * we do not want to print it out
463 prefix
= ((ns
== NULL
) || (ns
->prefix
== NULL
)) ? BAD_CAST
"" : ns
->prefix
;
464 href
= ((ns
== NULL
) || (ns
->href
== NULL
)) ? BAD_CAST
"" : ns
->href
;
465 has_empty_ns
= (xmlC14NStrEqual(prefix
, NULL
) && xmlC14NStrEqual(href
, NULL
));
467 if (cur
->nsTab
!= NULL
) {
469 for (i
= cur
->nsCurEnd
- 1; i
>= start
; --i
) {
470 xmlNsPtr ns1
= cur
->nsTab
[i
];
472 if(xmlC14NStrEqual(prefix
, (ns1
!= NULL
) ? ns1
->prefix
: NULL
)) {
473 if(xmlC14NStrEqual(href
, (ns1
!= NULL
) ? ns1
->href
: NULL
)) {
474 return(xmlC14NIsVisible(ctx
, ns1
, cur
->nodeTab
[i
]));
481 return(has_empty_ns
);
489 * @ns: the namespace to check
491 * Checks whether the given namespace is a default "xml:" namespace
492 * with href="http://www.w3.org/XML/1998/namespace"
494 * Returns 1 if the node is default or 0 otherwise
497 /* todo: make it a define? */
499 xmlC14NIsXmlNs(xmlNsPtr ns
)
501 return ((ns
!= NULL
) &&
502 (xmlStrEqual(ns
->prefix
, BAD_CAST
"xml")) &&
503 (xmlStrEqual(ns
->href
, XML_XML_NAMESPACE
)));
509 * @ns1: the pointer to first namespace
510 * @ns2: the pointer to second namespace
512 * Compares the namespaces by names (prefixes).
514 * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2.
517 xmlC14NNsCompare(const void *data1
, const void *data2
)
519 const xmlNsPtr ns1
= (const xmlNsPtr
) data1
;
520 const xmlNsPtr ns2
= (const xmlNsPtr
) data2
;
528 return (xmlStrcmp(ns1
->prefix
, ns2
->prefix
));
533 * xmlC14NPrintNamespaces:
534 * @ns: the pointer to namespace
535 * @ctx: the C14N context
537 * Prints the given namespace to the output buffer from C14N context.
539 * Returns 1 on success or 0 on fail.
542 xmlC14NPrintNamespaces(const xmlNsPtr ns
, xmlC14NCtxPtr ctx
)
545 if ((ns
== NULL
) || (ctx
== NULL
)) {
546 xmlC14NErrParam("writing namespaces");
550 if (ns
->prefix
!= NULL
) {
551 xmlOutputBufferWriteString(ctx
->buf
, " xmlns:");
552 xmlOutputBufferWriteString(ctx
->buf
, (const char *) ns
->prefix
);
553 xmlOutputBufferWriteString(ctx
->buf
, "=");
555 xmlOutputBufferWriteString(ctx
->buf
, " xmlns=");
557 if(ns
->href
!= NULL
) {
558 xmlBufWriteQuotedString(ctx
->buf
->buffer
, ns
->href
);
560 xmlOutputBufferWriteString(ctx
->buf
, "\"\"");
566 xmlC14NPrintNamespacesWalker(const void *ns
, void *ctx
) {
567 return xmlC14NPrintNamespaces((const xmlNsPtr
) ns
, (xmlC14NCtxPtr
) ctx
);
571 * xmlC14NProcessNamespacesAxis:
572 * @ctx: the C14N context
573 * @node: the current node
575 * Prints out canonical namespace axis of the current node to the
576 * buffer from C14N context as follows
578 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
581 * Consider a list L containing only namespace nodes in the
582 * axis and in the node-set in lexicographic order (ascending). To begin
583 * processing L, if the first node is not the default namespace node (a node
584 * with no namespace URI and no local name), then generate a space followed
585 * by xmlns="" if and only if the following conditions are met:
586 * - the element E that owns the axis is in the node-set
587 * - The nearest ancestor element of E in the node-set has a default
588 * namespace node in the node-set (default namespace nodes always
589 * have non-empty values in XPath)
590 * The latter condition eliminates unnecessary occurrences of xmlns="" in
591 * the canonical form since an element only receives an xmlns="" if its
592 * default namespace is empty and if it has an immediate parent in the
593 * canonical form that has a non-empty default namespace. To finish
594 * processing L, simply process every namespace node in L, except omit
595 * namespace node with local name xml, which defines the xml prefix,
596 * if its string value is http://www.w3.org/XML/1998/namespace.
598 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
599 * Canonical XML applied to a document subset requires the search of the
600 * ancestor nodes of each orphan element node for attributes in the xml
601 * namespace, such as xml:lang and xml:space. These are copied into the
602 * element node except if a declaration of the same attribute is already
603 * in the attribute axis of the element (whether or not it is included in
604 * the document subset). This search and copying are omitted from the
605 * Exclusive XML Canonicalization method.
607 * Returns 0 on success or -1 on fail.
610 xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int visible
)
615 int already_rendered
;
616 int has_empty_ns
= 0;
618 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
619 xmlC14NErrParam("processing namespaces axis (c14n)");
624 * Create a sorted list to store element namespaces
626 list
= xmlListCreate(NULL
, xmlC14NNsCompare
);
628 xmlC14NErrInternal("creating namespaces list (c14n)");
632 /* check all namespaces */
633 for(n
= cur
; n
!= NULL
; n
= n
->parent
) {
634 for(ns
= n
->nsDef
; ns
!= NULL
; ns
= ns
->next
) {
635 tmp
= xmlSearchNs(cur
->doc
, cur
, ns
->prefix
);
637 if((tmp
== ns
) && !xmlC14NIsXmlNs(ns
) && xmlC14NIsVisible(ctx
, ns
, cur
)) {
638 already_rendered
= xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, ns
);
640 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, ns
, cur
);
642 if(!already_rendered
) {
643 xmlListInsert(list
, ns
);
645 if(xmlStrlen(ns
->prefix
) == 0) {
653 * if the first node is not the default namespace node (a node with no
654 * namespace URI and no local name), then generate a space followed by
655 * xmlns="" if and only if the following conditions are met:
656 * - the element E that owns the axis is in the node-set
657 * - the nearest ancestor element of E in the node-set has a default
658 * namespace node in the node-set (default namespace nodes always
659 * have non-empty values in XPath)
661 if(visible
&& !has_empty_ns
) {
662 static xmlNs ns_default
;
664 memset(&ns_default
, 0, sizeof(ns_default
));
665 if(!xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, &ns_default
)) {
666 xmlC14NPrintNamespaces(&ns_default
, ctx
);
672 * print out all elements from list
674 xmlListWalk(list
, xmlC14NPrintNamespacesWalker
, (void *) ctx
);
685 * xmlExcC14NProcessNamespacesAxis:
686 * @ctx: the C14N context
687 * @node: the current node
689 * Prints out exclusive canonical namespace axis of the current node to the
690 * buffer from C14N context as follows
692 * Exclusive XML Canonicalization
693 * http://www.w3.org/TR/xml-exc-c14n
695 * If the element node is in the XPath subset then output the node in
696 * accordance with Canonical XML except for namespace nodes which are
697 * rendered as follows:
699 * 1. Render each namespace node iff:
700 * * it is visibly utilized by the immediate parent element or one of
701 * its attributes, or is present in InclusiveNamespaces PrefixList, and
702 * * its prefix and value do not appear in ns_rendered. ns_rendered is
703 * obtained by popping the state stack in order to obtain a list of
704 * prefixes and their values which have already been rendered by
705 * an output ancestor of the namespace node's parent element.
706 * 2. Append the rendered namespace node to the list ns_rendered of namespace
707 * nodes rendered by output ancestors. Push ns_rendered on state stack and
709 * 3. After the recursion returns, pop thestate stack.
712 * Returns 0 on success or -1 on fail.
715 xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int visible
)
720 int already_rendered
;
721 int has_empty_ns
= 0;
722 int has_visibly_utilized_empty_ns
= 0;
723 int has_empty_ns_in_inclusive_list
= 0;
725 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
726 xmlC14NErrParam("processing namespaces axis (exc c14n)");
730 if(!xmlC14NIsExclusive(ctx
)) {
731 xmlC14NErrParam("processing namespaces axis (exc c14n)");
737 * Create a sorted list to store element namespaces
739 list
= xmlListCreate(NULL
, xmlC14NNsCompare
);
741 xmlC14NErrInternal("creating namespaces list (exc c14n)");
746 * process inclusive namespaces:
747 * All namespace nodes appearing on inclusive ns list are
748 * handled as provided in Canonical XML
750 if(ctx
->inclusive_ns_prefixes
!= NULL
) {
754 for (i
= 0; ctx
->inclusive_ns_prefixes
[i
] != NULL
; ++i
) {
755 prefix
= ctx
->inclusive_ns_prefixes
[i
];
757 * Special values for namespace with empty prefix
759 if (xmlStrEqual(prefix
, BAD_CAST
"#default")
760 || xmlStrEqual(prefix
, BAD_CAST
"")) {
762 has_empty_ns_in_inclusive_list
= 1;
765 ns
= xmlSearchNs(cur
->doc
, cur
, prefix
);
766 if((ns
!= NULL
) && !xmlC14NIsXmlNs(ns
) && xmlC14NIsVisible(ctx
, ns
, cur
)) {
767 already_rendered
= xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, ns
);
769 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, ns
, cur
);
771 if(!already_rendered
) {
772 xmlListInsert(list
, ns
);
774 if(xmlStrlen(ns
->prefix
) == 0) {
781 /* add node namespace */
782 if(cur
->ns
!= NULL
) {
785 ns
= xmlSearchNs(cur
->doc
, cur
, NULL
);
786 has_visibly_utilized_empty_ns
= 1;
788 if((ns
!= NULL
) && !xmlC14NIsXmlNs(ns
)) {
789 if(visible
&& xmlC14NIsVisible(ctx
, ns
, cur
)) {
790 if(!xmlExcC14NVisibleNsStackFind(ctx
->ns_rendered
, ns
, ctx
)) {
791 xmlListInsert(list
, ns
);
795 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, ns
, cur
);
797 if(xmlStrlen(ns
->prefix
) == 0) {
804 for(attr
= cur
->properties
; attr
!= NULL
; attr
= attr
->next
) {
806 * we need to check that attribute is visible and has non
807 * default namespace (XML Namespaces: "default namespaces
808 * do not apply directly to attributes")
810 if((attr
->ns
!= NULL
) && !xmlC14NIsXmlNs(attr
->ns
) && xmlC14NIsVisible(ctx
, attr
, cur
)) {
811 already_rendered
= xmlExcC14NVisibleNsStackFind(ctx
->ns_rendered
, attr
->ns
, ctx
);
812 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, attr
->ns
, cur
);
813 if(!already_rendered
&& visible
) {
814 xmlListInsert(list
, attr
->ns
);
816 if(xmlStrlen(attr
->ns
->prefix
) == 0) {
819 } else if((attr
->ns
!= NULL
) && (xmlStrlen(attr
->ns
->prefix
) == 0) && (xmlStrlen(attr
->ns
->href
) == 0)) {
820 has_visibly_utilized_empty_ns
= 1;
827 if(visible
&& has_visibly_utilized_empty_ns
&&
828 !has_empty_ns
&& !has_empty_ns_in_inclusive_list
) {
829 static xmlNs ns_default
;
831 memset(&ns_default
, 0, sizeof(ns_default
));
833 already_rendered
= xmlExcC14NVisibleNsStackFind(ctx
->ns_rendered
, &ns_default
, ctx
);
834 if(!already_rendered
) {
835 xmlC14NPrintNamespaces(&ns_default
, ctx
);
837 } else if(visible
&& !has_empty_ns
&& has_empty_ns_in_inclusive_list
) {
838 static xmlNs ns_default
;
840 memset(&ns_default
, 0, sizeof(ns_default
));
841 if(!xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, &ns_default
)) {
842 xmlC14NPrintNamespaces(&ns_default
, ctx
);
849 * print out all elements from list
851 xmlListWalk(list
, xmlC14NPrintNamespacesWalker
, (void *) ctx
);
863 * @attr: the attr to check
865 * Checks whether the given attribute is a default "xml:" namespace
866 * with href="http://www.w3.org/XML/1998/namespace"
868 * Returns 1 if the node is default or 0 otherwise
871 /* todo: make it a define? */
873 xmlC14NIsXmlAttr(xmlAttrPtr attr
)
875 return ((attr
->ns
!= NULL
) &&
876 (xmlC14NIsXmlNs(attr
->ns
) != 0));
881 * xmlC14NAttrsCompare:
882 * @attr1: the pointer tls o first attr
883 * @attr2: the pointer to second attr
885 * Prints the given attribute to the output buffer from C14N context.
887 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2.
890 xmlC14NAttrsCompare(const void *data1
, const void *data2
)
892 const xmlAttrPtr attr1
= (const xmlAttrPtr
) data1
;
893 const xmlAttrPtr attr2
= (const xmlAttrPtr
) data2
;
905 if (attr1
->ns
== attr2
->ns
) {
906 return (xmlStrcmp(attr1
->name
, attr2
->name
));
910 * Attributes in the default namespace are first
911 * because the default namespace is not applied to
912 * unqualified attributes
914 if (attr1
->ns
== NULL
)
916 if (attr2
->ns
== NULL
)
918 if (attr1
->ns
->prefix
== NULL
)
920 if (attr2
->ns
->prefix
== NULL
)
923 ret
= xmlStrcmp(attr1
->ns
->href
, attr2
->ns
->href
);
925 ret
= xmlStrcmp(attr1
->name
, attr2
->name
);
933 * @attr: the pointer to attr
934 * @ctx: the C14N context
936 * Prints out canonical attribute urrent node to the
937 * buffer from C14N context as follows
939 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
941 * Returns 1 on success or 0 on fail.
944 xmlC14NPrintAttrs(const void *data
, void *user
)
946 const xmlAttrPtr attr
= (const xmlAttrPtr
) data
;
947 xmlC14NCtxPtr ctx
= (xmlC14NCtxPtr
) user
;
951 if ((attr
== NULL
) || (ctx
== NULL
)) {
952 xmlC14NErrParam("writing attributes");
956 xmlOutputBufferWriteString(ctx
->buf
, " ");
957 if (attr
->ns
!= NULL
&& xmlStrlen(attr
->ns
->prefix
) > 0) {
958 xmlOutputBufferWriteString(ctx
->buf
,
959 (const char *) attr
->ns
->prefix
);
960 xmlOutputBufferWriteString(ctx
->buf
, ":");
962 xmlOutputBufferWriteString(ctx
->buf
, (const char *) attr
->name
);
963 xmlOutputBufferWriteString(ctx
->buf
, "=\"");
965 value
= xmlNodeListGetString(ctx
->doc
, attr
->children
, 1);
966 /* todo: should we log an error if value==NULL ? */
968 buffer
= xmlC11NNormalizeAttr(value
);
970 if (buffer
!= NULL
) {
971 xmlOutputBufferWriteString(ctx
->buf
, (const char *) buffer
);
974 xmlC14NErrInternal("normalizing attributes axis");
978 xmlOutputBufferWriteString(ctx
->buf
, "\"");
983 * xmlC14NFindHiddenParentAttr:
985 * Finds an attribute in a hidden parent node.
987 * Returns a pointer to the attribute node (if found) or NULL otherwise.
990 xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, const xmlChar
* name
, const xmlChar
* ns
)
993 while((cur
!= NULL
) && (!xmlC14NIsVisible(ctx
, cur
, cur
->parent
))) {
994 res
= xmlHasNsProp(cur
, name
, ns
);
1006 * xmlC14NFixupBaseAttr:
1008 * Fixes up the xml:base attribute
1010 * Returns the newly created attribute or NULL
1013 xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx
, xmlAttrPtr xml_base_attr
)
1015 xmlChar
* res
= NULL
;
1022 if ((ctx
== NULL
) || (xml_base_attr
== NULL
) || (xml_base_attr
->parent
== NULL
)) {
1023 xmlC14NErrParam("processing xml:base attribute");
1027 /* start from current value */
1028 res
= xmlNodeListGetString(ctx
->doc
, xml_base_attr
->children
, 1);
1030 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
1034 /* go up the stack until we find a node that we rendered already */
1035 cur
= xml_base_attr
->parent
->parent
;
1036 while((cur
!= NULL
) && (!xmlC14NIsVisible(ctx
, cur
, cur
->parent
))) {
1037 attr
= xmlHasNsProp(cur
, BAD_CAST
"base", XML_XML_NAMESPACE
);
1039 /* get attr value */
1040 tmp_str
= xmlNodeListGetString(ctx
->doc
, attr
->children
, 1);
1041 if(tmp_str
== NULL
) {
1044 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
1048 /* we need to add '/' if our current base uri ends with '..' or '.'
1049 to ensure that we are forced to go "up" all the time */
1050 tmp_str_len
= xmlStrlen(tmp_str
);
1051 if(tmp_str_len
> 1 && tmp_str
[tmp_str_len
- 2] == '.') {
1052 tmp_str2
= xmlStrcat(tmp_str
, BAD_CAST
"/");
1053 if(tmp_str2
== NULL
) {
1057 xmlC14NErrInternal("processing xml:base attribute - can't modify uri");
1065 tmp_str2
= xmlBuildURI(res
, tmp_str
);
1066 if(tmp_str2
== NULL
) {
1070 xmlC14NErrInternal("processing xml:base attribute - can't construct uri");
1074 /* cleanup and set the new res */
1084 /* check if result uri is empty or not */
1085 if((res
== NULL
) || xmlStrEqual(res
, BAD_CAST
"")) {
1090 /* create and return the new attribute node */
1091 attr
= xmlNewNsProp(NULL
, xml_base_attr
->ns
, BAD_CAST
"base", res
);
1095 xmlC14NErrInternal("processing xml:base attribute - can't construct attribute");
1105 * xmlC14NProcessAttrsAxis:
1106 * @ctx: the C14N context
1107 * @cur: the current node
1108 * @parent_visible: the visibility of parent node
1109 * @all_parents_visible: the visibility of all parent nodes
1111 * Prints out canonical attribute axis of the current node to the
1112 * buffer from C14N context as follows
1114 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1117 * In lexicographic order (ascending), process each node that
1118 * is in the element's attribute axis and in the node-set.
1120 * The processing of an element node E MUST be modified slightly
1121 * when an XPath node-set is given as input and the element's
1122 * parent is omitted from the node-set.
1125 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
1127 * Canonical XML applied to a document subset requires the search of the
1128 * ancestor nodes of each orphan element node for attributes in the xml
1129 * namespace, such as xml:lang and xml:space. These are copied into the
1130 * element node except if a declaration of the same attribute is already
1131 * in the attribute axis of the element (whether or not it is included in
1132 * the document subset). This search and copying are omitted from the
1133 * Exclusive XML Canonicalization method.
1135 * Returns 0 on success or -1 on fail.
1138 xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int parent_visible
)
1142 xmlAttrPtr attrs_to_delete
= NULL
;
1144 /* special processing for 1.1 spec */
1145 xmlAttrPtr xml_base_attr
= NULL
;
1146 xmlAttrPtr xml_lang_attr
= NULL
;
1147 xmlAttrPtr xml_space_attr
= NULL
;
1149 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
1150 xmlC14NErrParam("processing attributes axis");
1155 * Create a sorted list to store element attributes
1157 list
= xmlListCreate(NULL
, xmlC14NAttrsCompare
);
1159 xmlC14NErrInternal("creating attributes list");
1165 /* The processing of an element node E MUST be modified slightly when an XPath node-set is
1166 * given as input and the element's parent is omitted from the node-set. The method for processing
1167 * the attribute axis of an element E in the node-set is enhanced. All element nodes along E's
1168 * ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such
1169 * as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,
1170 * remove any that are in E's attribute axis (whether or not they are in the node-set). Then,
1171 * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
1172 * the node-set. The result of visiting the attribute axis is computed by processing the attribute
1173 * nodes in this merged attribute list.
1177 * Add all visible attributes from current node.
1179 attr
= cur
->properties
;
1180 while (attr
!= NULL
) {
1181 /* check that attribute is visible */
1182 if (xmlC14NIsVisible(ctx
, attr
, cur
)) {
1183 xmlListInsert(list
, attr
);
1189 * Handle xml attributes
1191 if (parent_visible
&& (cur
->parent
!= NULL
) &&
1192 (!xmlC14NIsVisible(ctx
, cur
->parent
, cur
->parent
->parent
)))
1197 * If XPath node-set is not specified then the parent is always
1201 while (tmp
!= NULL
) {
1202 attr
= tmp
->properties
;
1203 while (attr
!= NULL
) {
1204 if (xmlC14NIsXmlAttr(attr
) != 0) {
1205 if (xmlListSearch(list
, attr
) == NULL
) {
1206 xmlListInsert(list
, attr
);
1217 case XML_C14N_EXCLUSIVE_1_0
:
1218 /* attributes in the XML namespace, such as xml:lang and xml:space
1219 * are not imported into orphan nodes of the document subset
1223 * Add all visible attributes from current node.
1225 attr
= cur
->properties
;
1226 while (attr
!= NULL
) {
1227 /* check that attribute is visible */
1228 if (xmlC14NIsVisible(ctx
, attr
, cur
)) {
1229 xmlListInsert(list
, attr
);
1234 /* do nothing special for xml attributes */
1237 /* The processing of an element node E MUST be modified slightly when an XPath node-set is
1238 * given as input and some of the element's ancestors are omitted from the node-set.
1240 * Simple inheritable attributes are attributes that have a value that requires at most a simple
1241 * redeclaration. This redeclaration is done by supplying a new value in the child axis. The
1242 * redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done
1243 * by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes
1244 * are xml:lang and xml:space.
1246 * The method for processing the attribute axis of an element E in the node-set is hence enhanced.
1247 * All element nodes along E's ancestor axis are examined for the nearest occurrences of simple
1248 * inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they
1249 * are in the node-set). From this list of attributes, any simple inheritable attributes that are
1250 * already in E's attribute axis (whether or not they are in the node-set) are removed. Then,
1251 * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
1252 * the node-set. The result of visiting the attribute axis is computed by processing the attribute
1253 * nodes in this merged attribute list.
1255 * The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is
1258 * The xml:base attribute is not a simple inheritable attribute and requires special processing beyond
1259 * a simple redeclaration.
1261 * Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed
1262 * as ordinary attributes.
1266 * Add all visible attributes from current node.
1268 attr
= cur
->properties
;
1269 while (attr
!= NULL
) {
1270 /* special processing for XML attribute kiks in only when we have invisible parents */
1271 if ((!parent_visible
) || (xmlC14NIsXmlAttr(attr
) == 0)) {
1272 /* check that attribute is visible */
1273 if (xmlC14NIsVisible(ctx
, attr
, cur
)) {
1274 xmlListInsert(list
, attr
);
1279 /* check for simple inheritance attributes */
1280 if((!matched
) && (xml_lang_attr
== NULL
) && xmlStrEqual(attr
->name
, BAD_CAST
"lang")) {
1281 xml_lang_attr
= attr
;
1284 if((!matched
) && (xml_space_attr
== NULL
) && xmlStrEqual(attr
->name
, BAD_CAST
"space")) {
1285 xml_space_attr
= attr
;
1289 /* check for base attr */
1290 if((!matched
) && (xml_base_attr
== NULL
) && xmlStrEqual(attr
->name
, BAD_CAST
"base")) {
1291 xml_base_attr
= attr
;
1295 /* otherwise, it is a normal attribute, so just check if it is visible */
1296 if((!matched
) && xmlC14NIsVisible(ctx
, attr
, cur
)) {
1297 xmlListInsert(list
, attr
);
1301 /* move to the next one */
1305 /* special processing for XML attribute kiks in only when we have invisible parents */
1306 if ((parent_visible
)) {
1308 /* simple inheritance attributes - copy */
1309 if(xml_lang_attr
== NULL
) {
1310 xml_lang_attr
= xmlC14NFindHiddenParentAttr(ctx
, cur
->parent
, BAD_CAST
"lang", XML_XML_NAMESPACE
);
1312 if(xml_lang_attr
!= NULL
) {
1313 xmlListInsert(list
, xml_lang_attr
);
1315 if(xml_space_attr
== NULL
) {
1316 xml_space_attr
= xmlC14NFindHiddenParentAttr(ctx
, cur
->parent
, BAD_CAST
"space", XML_XML_NAMESPACE
);
1318 if(xml_space_attr
!= NULL
) {
1319 xmlListInsert(list
, xml_space_attr
);
1322 /* base uri attribute - fix up */
1323 if(xml_base_attr
== NULL
) {
1324 /* if we don't have base uri attribute, check if we have a "hidden" one above */
1325 xml_base_attr
= xmlC14NFindHiddenParentAttr(ctx
, cur
->parent
, BAD_CAST
"base", XML_XML_NAMESPACE
);
1327 if(xml_base_attr
!= NULL
) {
1328 xml_base_attr
= xmlC14NFixupBaseAttr(ctx
, xml_base_attr
);
1329 if(xml_base_attr
!= NULL
) {
1330 xmlListInsert(list
, xml_base_attr
);
1332 /* note that we MUST delete returned attr node ourselves! */
1333 xml_base_attr
->next
= attrs_to_delete
;
1334 attrs_to_delete
= xml_base_attr
;
1344 * print out all elements from list
1346 xmlListWalk(list
, xmlC14NPrintAttrs
, (void *) ctx
);
1351 xmlFreePropList(attrs_to_delete
);
1352 xmlListDelete(list
);
1357 * xmlC14NCheckForRelativeNamespaces:
1358 * @ctx: the C14N context
1359 * @cur: the current element node
1361 * Checks that current element node has no relative namespaces defined
1363 * Returns 0 if the node has no relative namespaces or -1 otherwise.
1366 xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx
, xmlNodePtr cur
)
1370 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
1371 xmlC14NErrParam("checking for relative namespaces");
1376 while (ns
!= NULL
) {
1377 if (xmlStrlen(ns
->href
) > 0) {
1380 uri
= xmlParseURI((const char *) ns
->href
);
1382 xmlC14NErrInternal("parsing namespace uri");
1385 if (xmlStrlen((const xmlChar
*) uri
->scheme
) == 0) {
1386 xmlC14NErrRelativeNamespace(uri
->scheme
);
1398 * xmlC14NProcessElementNode:
1399 * @ctx: the pointer to C14N context object
1400 * @cur: the node to process
1401 * @visible: this node is visible
1402 * @all_parents_visible: whether all the parents of this node are visible
1404 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1407 * If the element is not in the node-set, then the result is obtained
1408 * by processing the namespace axis, then the attribute axis, then
1409 * processing the child nodes of the element that are in the node-set
1410 * (in document order). If the element is in the node-set, then the result
1411 * is an open angle bracket (<), the element QName, the result of
1412 * processing the namespace axis, the result of processing the attribute
1413 * axis, a close angle bracket (>), the result of processing the child
1414 * nodes of the element that are in the node-set (in document order), an
1415 * open angle bracket, a forward slash (/), the element QName, and a close
1418 * Returns non-negative value on success or negative value on fail
1421 xmlC14NProcessElementNode(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int visible
)
1424 xmlC14NVisibleNsStack state
;
1425 int parent_is_doc
= 0;
1427 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
1428 xmlC14NErrParam("processing element node");
1433 * Check relative relative namespaces:
1434 * implementations of XML canonicalization MUST report an operation
1435 * failure on documents containing relative namespace URIs.
1437 if (xmlC14NCheckForRelativeNamespaces(ctx
, cur
) < 0) {
1438 xmlC14NErrInternal("checking for relative namespaces");
1444 * Save ns_rendered stack position
1446 memset(&state
, 0, sizeof(state
));
1447 xmlC14NVisibleNsStackSave(ctx
->ns_rendered
, &state
);
1450 if (ctx
->parent_is_doc
) {
1451 /* save this flag into the stack */
1452 parent_is_doc
= ctx
->parent_is_doc
;
1453 ctx
->parent_is_doc
= 0;
1454 ctx
->pos
= XMLC14N_INSIDE_DOCUMENT_ELEMENT
;
1456 xmlOutputBufferWriteString(ctx
->buf
, "<");
1458 if ((cur
->ns
!= NULL
) && (xmlStrlen(cur
->ns
->prefix
) > 0)) {
1459 xmlOutputBufferWriteString(ctx
->buf
,
1460 (const char *) cur
->ns
->prefix
);
1461 xmlOutputBufferWriteString(ctx
->buf
, ":");
1463 xmlOutputBufferWriteString(ctx
->buf
, (const char *) cur
->name
);
1466 if (!xmlC14NIsExclusive(ctx
)) {
1467 ret
= xmlC14NProcessNamespacesAxis(ctx
, cur
, visible
);
1469 ret
= xmlExcC14NProcessNamespacesAxis(ctx
, cur
, visible
);
1472 xmlC14NErrInternal("processing namespaces axis");
1475 /* todo: shouldn't this go to "visible only"? */
1477 xmlC14NVisibleNsStackShift(ctx
->ns_rendered
);
1480 ret
= xmlC14NProcessAttrsAxis(ctx
, cur
, visible
);
1482 xmlC14NErrInternal("processing attributes axis");
1487 xmlOutputBufferWriteString(ctx
->buf
, ">");
1489 if (cur
->children
!= NULL
) {
1490 ret
= xmlC14NProcessNodeList(ctx
, cur
->children
);
1492 xmlC14NErrInternal("processing childrens list");
1497 xmlOutputBufferWriteString(ctx
->buf
, "</");
1498 if ((cur
->ns
!= NULL
) && (xmlStrlen(cur
->ns
->prefix
) > 0)) {
1499 xmlOutputBufferWriteString(ctx
->buf
,
1500 (const char *) cur
->ns
->prefix
);
1501 xmlOutputBufferWriteString(ctx
->buf
, ":");
1503 xmlOutputBufferWriteString(ctx
->buf
, (const char *) cur
->name
);
1504 xmlOutputBufferWriteString(ctx
->buf
, ">");
1505 if (parent_is_doc
) {
1506 /* restore this flag from the stack for next node */
1507 ctx
->parent_is_doc
= parent_is_doc
;
1508 ctx
->pos
= XMLC14N_AFTER_DOCUMENT_ELEMENT
;
1513 * Restore ns_rendered stack position
1515 xmlC14NVisibleNsStackRestore(ctx
->ns_rendered
, &state
);
1520 * xmlC14NProcessNode:
1521 * @ctx: the pointer to C14N context object
1522 * @cur: the node to process
1524 * Processes the given node
1526 * Returns non-negative value on success or negative value on fail
1529 xmlC14NProcessNode(xmlC14NCtxPtr ctx
, xmlNodePtr cur
)
1534 if ((ctx
== NULL
) || (cur
== NULL
)) {
1535 xmlC14NErrParam("processing node");
1539 visible
= xmlC14NIsVisible(ctx
, cur
, cur
->parent
);
1540 switch (cur
->type
) {
1541 case XML_ELEMENT_NODE
:
1542 ret
= xmlC14NProcessElementNode(ctx
, cur
, visible
);
1544 case XML_CDATA_SECTION_NODE
:
1548 * the string value, except all ampersands are replaced
1549 * by &, all open angle brackets (<) are replaced by <, all closing
1550 * angle brackets (>) are replaced by >, and all #xD characters are
1551 * replaced by 
.
1553 /* cdata sections are processed as text nodes */
1554 /* todo: verify that cdata sections are included in XPath nodes set */
1555 if ((visible
) && (cur
->content
!= NULL
)) {
1558 buffer
= xmlC11NNormalizeText(cur
->content
);
1559 if (buffer
!= NULL
) {
1560 xmlOutputBufferWriteString(ctx
->buf
,
1561 (const char *) buffer
);
1564 xmlC14NErrInternal("normalizing text node");
1571 * Processing Instruction (PI) Nodes-
1572 * The opening PI symbol (<?), the PI target name of the node,
1573 * a leading space and the string value if it is not empty, and
1574 * the closing PI symbol (?>). If the string value is empty,
1575 * then the leading space is not added. Also, a trailing #xA is
1576 * rendered after the closing PI symbol for PI children of the
1577 * root node with a lesser document order than the document
1578 * element, and a leading #xA is rendered before the opening PI
1579 * symbol of PI children of the root node with a greater document
1580 * order than the document element.
1583 if (ctx
->pos
== XMLC14N_AFTER_DOCUMENT_ELEMENT
) {
1584 xmlOutputBufferWriteString(ctx
->buf
, "\x0A<?");
1586 xmlOutputBufferWriteString(ctx
->buf
, "<?");
1589 xmlOutputBufferWriteString(ctx
->buf
,
1590 (const char *) cur
->name
);
1591 if ((cur
->content
!= NULL
) && (*(cur
->content
) != '\0')) {
1594 xmlOutputBufferWriteString(ctx
->buf
, " ");
1596 /* todo: do we need to normalize pi? */
1597 buffer
= xmlC11NNormalizePI(cur
->content
);
1598 if (buffer
!= NULL
) {
1599 xmlOutputBufferWriteString(ctx
->buf
,
1600 (const char *) buffer
);
1603 xmlC14NErrInternal("normalizing pi node");
1608 if (ctx
->pos
== XMLC14N_BEFORE_DOCUMENT_ELEMENT
) {
1609 xmlOutputBufferWriteString(ctx
->buf
, "?>\x0A");
1611 xmlOutputBufferWriteString(ctx
->buf
, "?>");
1615 case XML_COMMENT_NODE
:
1618 * Nothing if generating canonical XML without comments. For
1619 * canonical XML with comments, generate the opening comment
1620 * symbol (<!--), the string value of the node, and the
1621 * closing comment symbol (-->). Also, a trailing #xA is rendered
1622 * after the closing comment symbol for comment children of the
1623 * root node with a lesser document order than the document
1624 * element, and a leading #xA is rendered before the opening
1625 * comment symbol of comment children of the root node with a
1626 * greater document order than the document element. (Comment
1627 * children of the root node represent comments outside of the
1628 * top-level document element and outside of the document type
1631 if (visible
&& ctx
->with_comments
) {
1632 if (ctx
->pos
== XMLC14N_AFTER_DOCUMENT_ELEMENT
) {
1633 xmlOutputBufferWriteString(ctx
->buf
, "\x0A<!--");
1635 xmlOutputBufferWriteString(ctx
->buf
, "<!--");
1638 if (cur
->content
!= NULL
) {
1641 /* todo: do we need to normalize comment? */
1642 buffer
= xmlC11NNormalizeComment(cur
->content
);
1643 if (buffer
!= NULL
) {
1644 xmlOutputBufferWriteString(ctx
->buf
,
1645 (const char *) buffer
);
1648 xmlC14NErrInternal("normalizing comment node");
1653 if (ctx
->pos
== XMLC14N_BEFORE_DOCUMENT_ELEMENT
) {
1654 xmlOutputBufferWriteString(ctx
->buf
, "-->\x0A");
1656 xmlOutputBufferWriteString(ctx
->buf
, "-->");
1660 case XML_DOCUMENT_NODE
:
1661 case XML_DOCUMENT_FRAG_NODE
: /* should be processed as document? */
1662 #ifdef LIBXML_DOCB_ENABLED
1663 case XML_DOCB_DOCUMENT_NODE
: /* should be processed as document? */
1665 #ifdef LIBXML_HTML_ENABLED
1666 case XML_HTML_DOCUMENT_NODE
: /* should be processed as document? */
1668 if (cur
->children
!= NULL
) {
1669 ctx
->pos
= XMLC14N_BEFORE_DOCUMENT_ELEMENT
;
1670 ctx
->parent_is_doc
= 1;
1671 ret
= xmlC14NProcessNodeList(ctx
, cur
->children
);
1675 case XML_ATTRIBUTE_NODE
:
1676 xmlC14NErrInvalidNode("XML_ATTRIBUTE_NODE", "processing node");
1678 case XML_NAMESPACE_DECL
:
1679 xmlC14NErrInvalidNode("XML_NAMESPACE_DECL", "processing node");
1681 case XML_ENTITY_REF_NODE
:
1682 xmlC14NErrInvalidNode("XML_ENTITY_REF_NODE", "processing node");
1684 case XML_ENTITY_NODE
:
1685 xmlC14NErrInvalidNode("XML_ENTITY_NODE", "processing node");
1688 case XML_DOCUMENT_TYPE_NODE
:
1689 case XML_NOTATION_NODE
:
1691 case XML_ELEMENT_DECL
:
1692 case XML_ATTRIBUTE_DECL
:
1693 case XML_ENTITY_DECL
:
1694 #ifdef LIBXML_XINCLUDE_ENABLED
1695 case XML_XINCLUDE_START
:
1696 case XML_XINCLUDE_END
:
1699 * should be ignored according to "W3C Canonical XML"
1703 xmlC14NErrUnknownNode(cur
->type
, "processing node");
1711 * xmlC14NProcessNodeList:
1712 * @ctx: the pointer to C14N context object
1713 * @cur: the node to start from
1715 * Processes all nodes in the row starting from cur.
1717 * Returns non-negative value on success or negative value on fail
1720 xmlC14NProcessNodeList(xmlC14NCtxPtr ctx
, xmlNodePtr cur
)
1725 xmlC14NErrParam("processing node list");
1729 for (ret
= 0; cur
!= NULL
&& ret
>= 0; cur
= cur
->next
) {
1730 ret
= xmlC14NProcessNode(ctx
, cur
);
1738 * @ctx: the pointer to C14N context object
1740 * Cleanups the C14N context object.
1744 xmlC14NFreeCtx(xmlC14NCtxPtr ctx
)
1747 xmlC14NErrParam("freeing context");
1751 if (ctx
->ns_rendered
!= NULL
) {
1752 xmlC14NVisibleNsStackDestroy(ctx
->ns_rendered
);
1759 * @doc: the XML document for canonization
1760 * @is_visible_callback:the function to use to determine is node visible
1762 * @user_data: the first parameter for @is_visible_callback function
1763 * (in most cases, it is nodes set)
1764 * @mode: the c14n mode (see @xmlC14NMode)
1765 * @inclusive_ns_prefixe the list of inclusive namespace prefixes
1766 * ended with a NULL or NULL if there is no
1767 * inclusive namespaces (only for `
1769 * @with_comments: include comments in the result (!=0) or not (==0)
1770 * @buf: the output buffer to store canonical XML; this
1771 * buffer MUST have encoder==NULL because C14N requires
1774 * Creates new C14N context object to store C14N parameters.
1776 * Returns pointer to newly created object (success) or NULL (fail)
1778 static xmlC14NCtxPtr
1779 xmlC14NNewCtx(xmlDocPtr doc
,
1780 xmlC14NIsVisibleCallback is_visible_callback
, void* user_data
,
1781 xmlC14NMode mode
, xmlChar
** inclusive_ns_prefixes
,
1782 int with_comments
, xmlOutputBufferPtr buf
)
1784 xmlC14NCtxPtr ctx
= NULL
;
1786 if ((doc
== NULL
) || (buf
== NULL
)) {
1787 xmlC14NErrParam("creating new context");
1792 * Validate the encoding output buffer encoding
1794 if (buf
->encoder
!= NULL
) {
1795 xmlC14NErr(ctx
, (xmlNodePtr
) doc
, XML_C14N_REQUIRES_UTF8
,
1796 "xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n");
1801 * Allocate a new xmlC14NCtxPtr and fill the fields.
1803 ctx
= (xmlC14NCtxPtr
) xmlMalloc(sizeof(xmlC14NCtx
));
1805 xmlC14NErrMemory("creating context");
1808 memset(ctx
, 0, sizeof(xmlC14NCtx
));
1811 * initialize C14N context
1814 ctx
->with_comments
= with_comments
;
1815 ctx
->is_visible_callback
= is_visible_callback
;
1816 ctx
->user_data
= user_data
;
1818 ctx
->parent_is_doc
= 1;
1819 ctx
->pos
= XMLC14N_BEFORE_DOCUMENT_ELEMENT
;
1820 ctx
->ns_rendered
= xmlC14NVisibleNsStackCreate();
1822 if(ctx
->ns_rendered
== NULL
) {
1823 xmlC14NErr(ctx
, (xmlNodePtr
) doc
, XML_C14N_CREATE_STACK
,
1824 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n");
1825 xmlC14NFreeCtx(ctx
);
1830 * Set "mode" flag and remember list of incluseve prefixes
1831 * for exclusive c14n
1834 if(xmlC14NIsExclusive(ctx
)) {
1835 ctx
->inclusive_ns_prefixes
= inclusive_ns_prefixes
;
1842 * @doc: the XML document for canonization
1843 * @is_visible_callback:the function to use to determine is node visible
1845 * @user_data: the first parameter for @is_visible_callback function
1846 * (in most cases, it is nodes set)
1847 * @mode: the c14n mode (see @xmlC14NMode)
1848 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1849 * ended with a NULL or NULL if there is no
1850 * inclusive namespaces (only for exclusive
1851 * canonicalization, ignored otherwise)
1852 * @with_comments: include comments in the result (!=0) or not (==0)
1853 * @buf: the output buffer to store canonical XML; this
1854 * buffer MUST have encoder==NULL because C14N requires
1857 * Dumps the canonized image of given XML document into the provided buffer.
1858 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1859 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1861 * Returns non-negative value on success or a negative value on fail
1864 xmlC14NExecute(xmlDocPtr doc
, xmlC14NIsVisibleCallback is_visible_callback
,
1865 void* user_data
, int mode
, xmlChar
**inclusive_ns_prefixes
,
1866 int with_comments
, xmlOutputBufferPtr buf
) {
1869 xmlC14NMode c14n_mode
= XML_C14N_1_0
;
1872 if ((buf
== NULL
) || (doc
== NULL
)) {
1873 xmlC14NErrParam("executing c14n");
1877 /* for backward compatibility, we have to have "mode" as "int"
1878 and here we check that user gives valid value */
1881 case XML_C14N_EXCLUSIVE_1_0
:
1883 c14n_mode
= (xmlC14NMode
)mode
;
1886 xmlC14NErrParam("invalid mode for executing c14n");
1891 * Validate the encoding output buffer encoding
1893 if (buf
->encoder
!= NULL
) {
1894 xmlC14NErr(NULL
, (xmlNodePtr
) doc
, XML_C14N_REQUIRES_UTF8
,
1895 "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
1899 ctx
= xmlC14NNewCtx(doc
, is_visible_callback
, user_data
,
1900 c14n_mode
, inclusive_ns_prefixes
,
1901 with_comments
, buf
);
1903 xmlC14NErr(NULL
, (xmlNodePtr
) doc
, XML_C14N_CREATE_CTXT
,
1904 "xmlC14NExecute: unable to create C14N context\n");
1912 * The root node is the parent of the top-level document element. The
1913 * result of processing each of its child nodes that is in the node-set
1914 * in document order. The root node does not generate a byte order mark,
1915 * XML declaration, nor anything from within the document type
1918 if (doc
->children
!= NULL
) {
1919 ret
= xmlC14NProcessNodeList(ctx
, doc
->children
);
1921 xmlC14NErrInternal("processing docs children list");
1922 xmlC14NFreeCtx(ctx
);
1928 * Flush buffer to get number of bytes written
1930 ret
= xmlOutputBufferFlush(buf
);
1932 xmlC14NErrInternal("flushing output buffer");
1933 xmlC14NFreeCtx(ctx
);
1940 xmlC14NFreeCtx(ctx
);
1946 * @doc: the XML document for canonization
1947 * @nodes: the nodes set to be included in the canonized image
1948 * or NULL if all document nodes should be included
1949 * @mode: the c14n mode (see @xmlC14NMode)
1950 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1951 * ended with a NULL or NULL if there is no
1952 * inclusive namespaces (only for exclusive
1953 * canonicalization, ignored otherwise)
1954 * @with_comments: include comments in the result (!=0) or not (==0)
1955 * @buf: the output buffer to store canonical XML; this
1956 * buffer MUST have encoder==NULL because C14N requires
1959 * Dumps the canonized image of given XML document into the provided buffer.
1960 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1961 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1963 * Returns non-negative value on success or a negative value on fail
1966 xmlC14NDocSaveTo(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1967 int mode
, xmlChar
** inclusive_ns_prefixes
,
1968 int with_comments
, xmlOutputBufferPtr buf
) {
1969 return(xmlC14NExecute(doc
,
1970 xmlC14NIsNodeInNodeset
,
1973 inclusive_ns_prefixes
,
1980 * xmlC14NDocDumpMemory:
1981 * @doc: the XML document for canonization
1982 * @nodes: the nodes set to be included in the canonized image
1983 * or NULL if all document nodes should be included
1984 * @mode: the c14n mode (see @xmlC14NMode)
1985 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1986 * ended with a NULL or NULL if there is no
1987 * inclusive namespaces (only for exclusive
1988 * canonicalization, ignored otherwise)
1989 * @with_comments: include comments in the result (!=0) or not (==0)
1990 * @doc_txt_ptr: the memory pointer for allocated canonical XML text;
1991 * the caller of this functions is responsible for calling
1992 * xmlFree() to free allocated memory
1994 * Dumps the canonized image of given XML document into memory.
1995 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1996 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1998 * Returns the number of bytes written on success or a negative value on fail
2001 xmlC14NDocDumpMemory(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
2002 int mode
, xmlChar
** inclusive_ns_prefixes
,
2003 int with_comments
, xmlChar
** doc_txt_ptr
)
2006 xmlOutputBufferPtr buf
;
2008 if (doc_txt_ptr
== NULL
) {
2009 xmlC14NErrParam("dumping doc to memory");
2013 *doc_txt_ptr
= NULL
;
2016 * create memory buffer with UTF8 (default) encoding
2018 buf
= xmlAllocOutputBuffer(NULL
);
2020 xmlC14NErrMemory("creating output buffer");
2025 * canonize document and write to buffer
2027 ret
= xmlC14NDocSaveTo(doc
, nodes
, mode
, inclusive_ns_prefixes
,
2028 with_comments
, buf
);
2030 xmlC14NErrInternal("saving doc to output buffer");
2031 (void) xmlOutputBufferClose(buf
);
2035 ret
= xmlBufUse(buf
->buffer
);
2037 *doc_txt_ptr
= xmlStrndup(xmlBufContent(buf
->buffer
), ret
);
2039 (void) xmlOutputBufferClose(buf
);
2041 if ((*doc_txt_ptr
== NULL
) && (ret
> 0)) {
2042 xmlC14NErrMemory("coping canonicanized document");
2050 * @doc: the XML document for canonization
2051 * @nodes: the nodes set to be included in the canonized image
2052 * or NULL if all document nodes should be included
2053 * @mode: the c14n mode (see @xmlC14NMode)
2054 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
2055 * ended with a NULL or NULL if there is no
2056 * inclusive namespaces (only for exclusive
2057 * canonicalization, ignored otherwise)
2058 * @with_comments: include comments in the result (!=0) or not (==0)
2059 * @filename: the filename to store canonical XML image
2060 * @compression: the compression level (zlib requred):
2061 * -1 - libxml default,
2063 * >0 - compression level
2065 * Dumps the canonized image of given XML document into the file.
2066 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
2067 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
2069 * Returns the number of bytes written success or a negative value on fail
2072 xmlC14NDocSave(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
2073 int mode
, xmlChar
** inclusive_ns_prefixes
,
2074 int with_comments
, const char *filename
, int compression
)
2076 xmlOutputBufferPtr buf
;
2079 if (filename
== NULL
) {
2080 xmlC14NErrParam("saving doc");
2083 #ifdef LIBXML_ZLIB_ENABLED
2084 if (compression
< 0)
2085 compression
= xmlGetCompressMode();
2089 * save the content to a temp buffer, use default UTF8 encoding.
2091 buf
= xmlOutputBufferCreateFilename(filename
, NULL
, compression
);
2093 xmlC14NErrInternal("creating temporary filename");
2098 * canonize document and write to buffer
2100 ret
= xmlC14NDocSaveTo(doc
, nodes
, mode
, inclusive_ns_prefixes
,
2101 with_comments
, buf
);
2103 xmlC14NErrInternal("cannicanize document to buffer");
2104 (void) xmlOutputBufferClose(buf
);
2109 * get the numbers of bytes written
2111 ret
= xmlOutputBufferClose(buf
);
2118 * Macro used to grow the current buffer.
2120 #define growBufferReentrant() { \
2122 buffer = (xmlChar *) \
2123 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
2124 if (buffer == NULL) { \
2125 xmlC14NErrMemory("growing buffer"); \
2131 * xmlC11NNormalizeString:
2132 * @input: the input string
2133 * @mode: the normalization mode (attribute, comment, PI or text)
2135 * Converts a string to a canonical (normalized) format. The code is stolen
2136 * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A
2137 * and the @mode parameter
2139 * Returns a normalized string (caller is responsible for calling xmlFree())
2140 * or NULL if an error occurs
2143 xmlC11NNormalizeString(const xmlChar
* input
,
2144 xmlC14NNormalizationMode mode
)
2146 const xmlChar
*cur
= input
;
2147 xmlChar
*buffer
= NULL
;
2148 xmlChar
*out
= NULL
;
2149 int buffer_size
= 0;
2155 * allocate an translation buffer.
2158 buffer
= (xmlChar
*) xmlMallocAtomic(buffer_size
* sizeof(xmlChar
));
2159 if (buffer
== NULL
) {
2160 xmlC14NErrMemory("allocating buffer");
2165 while (*cur
!= '\0') {
2166 if ((out
- buffer
) > (buffer_size
- 10)) {
2167 int indx
= out
- buffer
;
2169 growBufferReentrant();
2170 out
= &buffer
[indx
];
2173 if ((*cur
== '<') && ((mode
== XMLC14N_NORMALIZE_ATTR
) ||
2174 (mode
== XMLC14N_NORMALIZE_TEXT
))) {
2179 } else if ((*cur
== '>') && (mode
== XMLC14N_NORMALIZE_TEXT
)) {
2184 } else if ((*cur
== '&') && ((mode
== XMLC14N_NORMALIZE_ATTR
) ||
2185 (mode
== XMLC14N_NORMALIZE_TEXT
))) {
2191 } else if ((*cur
== '"') && (mode
== XMLC14N_NORMALIZE_ATTR
)) {
2198 } else if ((*cur
== '\x09') && (mode
== XMLC14N_NORMALIZE_ATTR
)) {
2204 } else if ((*cur
== '\x0A') && (mode
== XMLC14N_NORMALIZE_ATTR
)) {
2210 } else if ((*cur
== '\x0D') && ((mode
== XMLC14N_NORMALIZE_ATTR
) ||
2211 (mode
== XMLC14N_NORMALIZE_TEXT
) ||
2212 (mode
== XMLC14N_NORMALIZE_COMMENT
) ||
2213 (mode
== XMLC14N_NORMALIZE_PI
))) {
2221 * Works because on UTF-8, all extended sequences cannot
2222 * result in bytes in the ASCII range.
2231 #endif /* LIBXML_OUTPUT_ENABLED */
2233 #include "elfgcchack.h"
2234 #endif /* LIBXML_C14N_ENABLED */