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>
30 /************************************************************************
32 * Some declaration better left private ATM *
34 ************************************************************************/
37 XMLC14N_BEFORE_DOCUMENT_ELEMENT
= 0,
38 XMLC14N_INSIDE_DOCUMENT_ELEMENT
= 1,
39 XMLC14N_AFTER_DOCUMENT_ELEMENT
= 2
42 typedef struct _xmlC14NVisibleNsStack
{
43 int nsCurEnd
; /* number of nodes in the set */
44 int nsPrevStart
; /* the begginning of the stack for previous visible node */
45 int nsPrevEnd
; /* the end of the stack for previous visible node */
46 int nsMax
; /* size of the array as allocated */
47 xmlNsPtr
*nsTab
; /* array of ns in no particular order */
48 xmlNodePtr
*nodeTab
; /* array of nodes in no particular order */
49 } xmlC14NVisibleNsStack
, *xmlC14NVisibleNsStackPtr
;
51 typedef struct _xmlC14NCtx
{
52 /* input parameters */
54 xmlC14NIsVisibleCallback is_visible_callback
;
57 xmlOutputBufferPtr buf
;
59 /* position in the XML document */
62 xmlC14NVisibleNsStackPtr ns_rendered
;
67 /* exclusive canonicalization */
68 xmlChar
**inclusive_ns_prefixes
;
72 } xmlC14NCtx
, *xmlC14NCtxPtr
;
74 static xmlC14NVisibleNsStackPtr
xmlC14NVisibleNsStackCreate (void);
75 static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur
);
76 static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cur
,
79 static void xmlC14NVisibleNsStackSave (xmlC14NVisibleNsStackPtr cur
,
80 xmlC14NVisibleNsStackPtr state
);
81 static void xmlC14NVisibleNsStackRestore (xmlC14NVisibleNsStackPtr cur
,
82 xmlC14NVisibleNsStackPtr state
);
83 static void xmlC14NVisibleNsStackShift (xmlC14NVisibleNsStackPtr cur
);
84 static int xmlC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur
,
86 static int xmlExcC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur
,
90 static int xmlC14NIsNodeInNodeset (xmlNodeSetPtr nodes
,
96 static int xmlC14NProcessNode(xmlC14NCtxPtr ctx
, xmlNodePtr cur
);
97 static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx
, xmlNodePtr cur
);
99 XMLC14N_NORMALIZE_ATTR
= 0,
100 XMLC14N_NORMALIZE_COMMENT
= 1,
101 XMLC14N_NORMALIZE_PI
= 2,
102 XMLC14N_NORMALIZE_TEXT
= 3
103 } xmlC14NNormalizationMode
;
105 static xmlChar
*xmlC11NNormalizeString(const xmlChar
* input
,
106 xmlC14NNormalizationMode mode
);
108 #define xmlC11NNormalizeAttr( a ) \
109 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR)
110 #define xmlC11NNormalizeComment( a ) \
111 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT)
112 #define xmlC11NNormalizePI( a ) \
113 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI)
114 #define xmlC11NNormalizeText( a ) \
115 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT)
117 #define xmlC14NIsVisible( ctx, node, parent ) \
118 (((ctx)->is_visible_callback != NULL) ? \
119 (ctx)->is_visible_callback((ctx)->user_data, \
120 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1)
122 #define xmlC14NIsExclusive( ctx ) \
123 ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 )
125 /************************************************************************
127 * Some factorized error routines *
129 ************************************************************************/
133 * @extra: extra informations
135 * Handle a redefinition of memory error
138 xmlC14NErrMemory(const char *extra
)
140 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
141 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0, extra
,
143 "Memory allocation failed : %s\n", extra
);
148 * @extra: extra informations
150 * Handle a redefinition of param error
153 xmlC14NErrParam(const char *extra
)
155 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
156 XML_ERR_INTERNAL_ERROR
, XML_ERR_ERROR
, NULL
, 0, extra
,
158 "Invalid parameter : %s\n", extra
);
162 * xmlC14NErrInternal:
163 * @extra: extra informations
165 * Handle a redefinition of internal error
168 xmlC14NErrInternal(const char *extra
)
170 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
171 XML_ERR_INTERNAL_ERROR
, XML_ERR_ERROR
, NULL
, 0, extra
,
173 "Internal error : %s\n", extra
);
177 * xmlC14NErrInvalidNode:
178 * @extra: extra informations
180 * Handle a redefinition of invalid node error
183 xmlC14NErrInvalidNode(const char *node_type
, const char *extra
)
185 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
186 XML_C14N_INVALID_NODE
, XML_ERR_ERROR
, NULL
, 0, extra
,
188 "Node %s is invalid here : %s\n", node_type
, extra
);
192 * xmlC14NErrUnknownNode:
193 * @extra: extra informations
195 * Handle a redefinition of unknown node error
198 xmlC14NErrUnknownNode(int node_type
, const char *extra
)
200 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
201 XML_C14N_UNKNOW_NODE
, XML_ERR_ERROR
, NULL
, 0, extra
,
203 "Unknown node type %d found : %s\n", node_type
, extra
);
207 * xmlC14NErrRelativeNamespace:
208 * @extra: extra informations
210 * Handle a redefinition of relative namespace error
213 xmlC14NErrRelativeNamespace(const char *ns_uri
)
215 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_C14N
,
216 XML_C14N_RELATIVE_NAMESPACE
, XML_ERR_ERROR
, NULL
, 0, NULL
,
218 "Relative namespace UR is invalid here : %s\n", ns_uri
);
225 * @ctxt: a C14N evaluation context
226 * @node: the context node
227 * @error: the erorr code
229 * @extra: extra informations
231 * Handle a redefinition of attribute error
234 xmlC14NErr(xmlC14NCtxPtr ctxt
, xmlNodePtr node
, int error
,
239 __xmlRaiseError(NULL
, NULL
, NULL
,
240 ctxt
, node
, XML_FROM_C14N
, error
,
241 XML_ERR_ERROR
, NULL
, 0,
242 NULL
, NULL
, NULL
, 0, 0, "%s", msg
);
245 /************************************************************************
247 * The implementation internals *
249 ************************************************************************/
250 #define XML_NAMESPACES_DEFAULT 16
253 xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes
, xmlNodePtr node
, xmlNodePtr parent
) {
254 if((nodes
!= NULL
) && (node
!= NULL
)) {
255 if(node
->type
!= XML_NAMESPACE_DECL
) {
256 return(xmlXPathNodeSetContains(nodes
, node
));
260 memcpy(&ns
, node
, sizeof(ns
));
262 /* this is a libxml hack! check xpath.c for details */
263 if((parent
!= NULL
) && (parent
->type
== XML_ATTRIBUTE_NODE
)) {
264 ns
.next
= (xmlNsPtr
)parent
->parent
;
266 ns
.next
= (xmlNsPtr
)parent
;
270 * If the input is an XPath node-set, then the node-set must explicitly
271 * contain every node to be rendered to the canonical form.
273 return(xmlXPathNodeSetContains(nodes
, (xmlNodePtr
)&ns
));
279 static xmlC14NVisibleNsStackPtr
280 xmlC14NVisibleNsStackCreate(void) {
281 xmlC14NVisibleNsStackPtr ret
;
283 ret
= (xmlC14NVisibleNsStackPtr
) xmlMalloc(sizeof(xmlC14NVisibleNsStack
));
285 xmlC14NErrMemory("creating namespaces stack");
288 memset(ret
, 0 , (size_t) sizeof(xmlC14NVisibleNsStack
));
293 xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr cur
) {
295 xmlC14NErrParam("destroying namespaces stack");
298 if(cur
->nsTab
!= NULL
) {
299 memset(cur
->nsTab
, 0, cur
->nsMax
* sizeof(xmlNsPtr
));
302 if(cur
->nodeTab
!= NULL
) {
303 memset(cur
->nodeTab
, 0, cur
->nsMax
* sizeof(xmlNodePtr
));
304 xmlFree(cur
->nodeTab
);
306 memset(cur
, 0, sizeof(xmlC14NVisibleNsStack
));
312 xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur
, xmlNsPtr ns
, xmlNodePtr node
) {
314 ((cur
->nsTab
== NULL
) && (cur
->nodeTab
!= NULL
)) ||
315 ((cur
->nsTab
!= NULL
) && (cur
->nodeTab
== NULL
))) {
316 xmlC14NErrParam("adding namespace to stack");
320 if ((cur
->nsTab
== NULL
) && (cur
->nodeTab
== NULL
)) {
321 cur
->nsTab
= (xmlNsPtr
*) xmlMalloc(XML_NAMESPACES_DEFAULT
* sizeof(xmlNsPtr
));
322 cur
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NAMESPACES_DEFAULT
* sizeof(xmlNodePtr
));
323 if ((cur
->nsTab
== NULL
) || (cur
->nodeTab
== NULL
)) {
324 xmlC14NErrMemory("adding node to stack");
327 memset(cur
->nsTab
, 0 , XML_NAMESPACES_DEFAULT
* sizeof(xmlNsPtr
));
328 memset(cur
->nodeTab
, 0 , XML_NAMESPACES_DEFAULT
* sizeof(xmlNodePtr
));
329 cur
->nsMax
= XML_NAMESPACES_DEFAULT
;
330 } else if(cur
->nsMax
== cur
->nsCurEnd
) {
334 tmpSize
= 2 * cur
->nsMax
;
335 tmp
= xmlRealloc(cur
->nsTab
, tmpSize
* sizeof(xmlNsPtr
));
337 xmlC14NErrMemory("adding node to stack");
340 cur
->nsTab
= (xmlNsPtr
*)tmp
;
342 tmp
= xmlRealloc(cur
->nodeTab
, tmpSize
* sizeof(xmlNodePtr
));
344 xmlC14NErrMemory("adding node to stack");
347 cur
->nodeTab
= (xmlNodePtr
*)tmp
;
349 cur
->nsMax
= tmpSize
;
351 cur
->nsTab
[cur
->nsCurEnd
] = ns
;
352 cur
->nodeTab
[cur
->nsCurEnd
] = node
;
358 xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur
, xmlC14NVisibleNsStackPtr state
) {
359 if((cur
== NULL
) || (state
== NULL
)) {
360 xmlC14NErrParam("saving namespaces stack");
364 state
->nsCurEnd
= cur
->nsCurEnd
;
365 state
->nsPrevStart
= cur
->nsPrevStart
;
366 state
->nsPrevEnd
= cur
->nsPrevEnd
;
370 xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr cur
, xmlC14NVisibleNsStackPtr state
) {
371 if((cur
== NULL
) || (state
== NULL
)) {
372 xmlC14NErrParam("restoring namespaces stack");
375 cur
->nsCurEnd
= state
->nsCurEnd
;
376 cur
->nsPrevStart
= state
->nsPrevStart
;
377 cur
->nsPrevEnd
= state
->nsPrevEnd
;
381 xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur
) {
383 xmlC14NErrParam("shifting namespaces stack");
386 cur
->nsPrevStart
= cur
->nsPrevEnd
;
387 cur
->nsPrevEnd
= cur
->nsCurEnd
;
391 xmlC14NStrEqual(const xmlChar
*str1
, const xmlChar
*str2
) {
392 if (str1
== str2
) return(1);
393 if (str1
== NULL
) return((*str2
) == '\0');
394 if (str2
== NULL
) return((*str1
) == '\0');
396 if (*str1
++ != *str2
) return(0);
402 * xmlC14NVisibleNsStackFind:
403 * @ctx: the C14N context
404 * @ns: the namespace to check
406 * Checks whether the given namespace was already rendered or not
408 * Returns 1 if we already wrote this namespace or 0 otherwise
411 xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
, xmlNsPtr ns
)
414 const xmlChar
*prefix
;
419 xmlC14NErrParam("searching namespaces stack (c14n)");
424 * if the default namespace xmlns="" is not defined yet then
425 * we do not want to print it out
427 prefix
= ((ns
== NULL
) || (ns
->prefix
== NULL
)) ? BAD_CAST
"" : ns
->prefix
;
428 href
= ((ns
== NULL
) || (ns
->href
== NULL
)) ? BAD_CAST
"" : ns
->href
;
429 has_empty_ns
= (xmlC14NStrEqual(prefix
, NULL
) && xmlC14NStrEqual(href
, NULL
));
431 if (cur
->nsTab
!= NULL
) {
432 int start
= (has_empty_ns
) ? 0 : cur
->nsPrevStart
;
433 for (i
= cur
->nsCurEnd
- 1; i
>= start
; --i
) {
434 xmlNsPtr ns1
= cur
->nsTab
[i
];
436 if(xmlC14NStrEqual(prefix
, (ns1
!= NULL
) ? ns1
->prefix
: NULL
)) {
437 return(xmlC14NStrEqual(href
, (ns1
!= NULL
) ? ns1
->href
: NULL
));
441 return(has_empty_ns
);
445 xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
, xmlNsPtr ns
, xmlC14NCtxPtr ctx
) {
447 const xmlChar
*prefix
;
452 xmlC14NErrParam("searching namespaces stack (exc c14n)");
457 * if the default namespace xmlns="" is not defined yet then
458 * we do not want to print it out
460 prefix
= ((ns
== NULL
) || (ns
->prefix
== NULL
)) ? BAD_CAST
"" : ns
->prefix
;
461 href
= ((ns
== NULL
) || (ns
->href
== NULL
)) ? BAD_CAST
"" : ns
->href
;
462 has_empty_ns
= (xmlC14NStrEqual(prefix
, NULL
) && xmlC14NStrEqual(href
, NULL
));
464 if (cur
->nsTab
!= NULL
) {
466 for (i
= cur
->nsCurEnd
- 1; i
>= start
; --i
) {
467 xmlNsPtr ns1
= cur
->nsTab
[i
];
469 if(xmlC14NStrEqual(prefix
, (ns1
!= NULL
) ? ns1
->prefix
: NULL
)) {
470 if(xmlC14NStrEqual(href
, (ns1
!= NULL
) ? ns1
->href
: NULL
)) {
471 return(xmlC14NIsVisible(ctx
, ns1
, cur
->nodeTab
[i
]));
478 return(has_empty_ns
);
486 * @ns: the namespace to check
488 * Checks whether the given namespace is a default "xml:" namespace
489 * with href="http://www.w3.org/XML/1998/namespace"
491 * Returns 1 if the node is default or 0 otherwise
494 /* todo: make it a define? */
496 xmlC14NIsXmlNs(xmlNsPtr ns
)
498 return ((ns
!= NULL
) &&
499 (xmlStrEqual(ns
->prefix
, BAD_CAST
"xml")) &&
500 (xmlStrEqual(ns
->href
, XML_XML_NAMESPACE
)));
506 * @ns1: the pointer to first namespace
507 * @ns2: the pointer to second namespace
509 * Compares the namespaces by names (prefixes).
511 * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2.
514 xmlC14NNsCompare(xmlNsPtr ns1
, xmlNsPtr ns2
)
523 return (xmlStrcmp(ns1
->prefix
, ns2
->prefix
));
528 * xmlC14NPrintNamespaces:
529 * @ns: the pointer to namespace
530 * @ctx: the C14N context
532 * Prints the given namespace to the output buffer from C14N context.
534 * Returns 1 on success or 0 on fail.
537 xmlC14NPrintNamespaces(const xmlNsPtr ns
, xmlC14NCtxPtr ctx
)
540 if ((ns
== NULL
) || (ctx
== NULL
)) {
541 xmlC14NErrParam("writing namespaces");
545 if (ns
->prefix
!= NULL
) {
546 xmlOutputBufferWriteString(ctx
->buf
, " xmlns:");
547 xmlOutputBufferWriteString(ctx
->buf
, (const char *) ns
->prefix
);
548 xmlOutputBufferWriteString(ctx
->buf
, "=\"");
550 xmlOutputBufferWriteString(ctx
->buf
, " xmlns=\"");
552 if(ns
->href
!= NULL
) {
553 xmlOutputBufferWriteString(ctx
->buf
, (const char *) ns
->href
);
555 xmlOutputBufferWriteString(ctx
->buf
, "\"");
560 * xmlC14NProcessNamespacesAxis:
561 * @ctx: the C14N context
562 * @node: the current node
564 * Prints out canonical namespace axis of the current node to the
565 * buffer from C14N context as follows
567 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
570 * Consider a list L containing only namespace nodes in the
571 * axis and in the node-set in lexicographic order (ascending). To begin
572 * processing L, if the first node is not the default namespace node (a node
573 * with no namespace URI and no local name), then generate a space followed
574 * by xmlns="" if and only if the following conditions are met:
575 * - the element E that owns the axis is in the node-set
576 * - The nearest ancestor element of E in the node-set has a default
577 * namespace node in the node-set (default namespace nodes always
578 * have non-empty values in XPath)
579 * The latter condition eliminates unnecessary occurrences of xmlns="" in
580 * the canonical form since an element only receives an xmlns="" if its
581 * default namespace is empty and if it has an immediate parent in the
582 * canonical form that has a non-empty default namespace. To finish
583 * processing L, simply process every namespace node in L, except omit
584 * namespace node with local name xml, which defines the xml prefix,
585 * if its string value is http://www.w3.org/XML/1998/namespace.
587 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
588 * Canonical XML applied to a document subset requires the search of the
589 * ancestor nodes of each orphan element node for attributes in the xml
590 * namespace, such as xml:lang and xml:space. These are copied into the
591 * element node except if a declaration of the same attribute is already
592 * in the attribute axis of the element (whether or not it is included in
593 * the document subset). This search and copying are omitted from the
594 * Exclusive XML Canonicalization method.
596 * Returns 0 on success or -1 on fail.
599 xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int visible
)
604 int already_rendered
;
605 int has_empty_ns
= 0;
607 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
608 xmlC14NErrParam("processing namespaces axis (c14n)");
613 * Create a sorted list to store element namespaces
615 list
= xmlListCreate(NULL
, (xmlListDataCompare
) xmlC14NNsCompare
);
617 xmlC14NErrInternal("creating namespaces list (c14n)");
621 /* check all namespaces */
622 for(n
= cur
; n
!= NULL
; n
= n
->parent
) {
623 for(ns
= n
->nsDef
; ns
!= NULL
; ns
= ns
->next
) {
624 tmp
= xmlSearchNs(cur
->doc
, cur
, ns
->prefix
);
626 if((tmp
== ns
) && !xmlC14NIsXmlNs(ns
) && xmlC14NIsVisible(ctx
, ns
, cur
)) {
627 already_rendered
= xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, ns
);
629 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, ns
, cur
);
631 if(!already_rendered
) {
632 xmlListInsert(list
, ns
);
634 if(xmlStrlen(ns
->prefix
) == 0) {
642 * if the first node is not the default namespace node (a node with no
643 * namespace URI and no local name), then generate a space followed by
644 * xmlns="" if and only if the following conditions are met:
645 * - the element E that owns the axis is in the node-set
646 * - the nearest ancestor element of E in the node-set has a default
647 * namespace node in the node-set (default namespace nodes always
648 * have non-empty values in XPath)
650 if(visible
&& !has_empty_ns
) {
651 static xmlNs ns_default
;
653 memset(&ns_default
, 0, sizeof(ns_default
));
654 if(!xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, &ns_default
)) {
655 xmlC14NPrintNamespaces(&ns_default
, ctx
);
661 * print out all elements from list
663 xmlListWalk(list
, (xmlListWalker
) xmlC14NPrintNamespaces
, (const void *) ctx
);
674 * xmlExcC14NProcessNamespacesAxis:
675 * @ctx: the C14N context
676 * @node: the current node
678 * Prints out exclusive canonical namespace axis of the current node to the
679 * buffer from C14N context as follows
681 * Exclusive XML Canonicalization
682 * http://www.w3.org/TR/xml-exc-c14n
684 * If the element node is in the XPath subset then output the node in
685 * accordance with Canonical XML except for namespace nodes which are
686 * rendered as follows:
688 * 1. Render each namespace node iff:
689 * * it is visibly utilized by the immediate parent element or one of
690 * its attributes, or is present in InclusiveNamespaces PrefixList, and
691 * * its prefix and value do not appear in ns_rendered. ns_rendered is
692 * obtained by popping the state stack in order to obtain a list of
693 * prefixes and their values which have already been rendered by
694 * an output ancestor of the namespace node's parent element.
695 * 2. Append the rendered namespace node to the list ns_rendered of namespace
696 * nodes rendered by output ancestors. Push ns_rendered on state stack and
698 * 3. After the recursion returns, pop thestate stack.
701 * Returns 0 on success or -1 on fail.
704 xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int visible
)
709 int already_rendered
;
710 int has_empty_ns
= 0;
711 int has_visibly_utilized_empty_ns
= 0;
712 int has_empty_ns_in_inclusive_list
= 0;
714 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
715 xmlC14NErrParam("processing namespaces axis (exc c14n)");
719 if(!xmlC14NIsExclusive(ctx
)) {
720 xmlC14NErrParam("processing namespaces axis (exc c14n)");
726 * Create a sorted list to store element namespaces
728 list
= xmlListCreate(NULL
, (xmlListDataCompare
) xmlC14NNsCompare
);
730 xmlC14NErrInternal("creating namespaces list (exc c14n)");
735 * process inclusive namespaces:
736 * All namespace nodes appearing on inclusive ns list are
737 * handled as provided in Canonical XML
739 if(ctx
->inclusive_ns_prefixes
!= NULL
) {
743 for (i
= 0; ctx
->inclusive_ns_prefixes
[i
] != NULL
; ++i
) {
744 prefix
= ctx
->inclusive_ns_prefixes
[i
];
746 * Special values for namespace with empty prefix
748 if (xmlStrEqual(prefix
, BAD_CAST
"#default")
749 || xmlStrEqual(prefix
, BAD_CAST
"")) {
751 has_empty_ns_in_inclusive_list
= 1;
754 ns
= xmlSearchNs(cur
->doc
, cur
, prefix
);
755 if((ns
!= NULL
) && !xmlC14NIsXmlNs(ns
) && xmlC14NIsVisible(ctx
, ns
, cur
)) {
756 already_rendered
= xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, ns
);
758 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, ns
, cur
);
760 if(!already_rendered
) {
761 xmlListInsert(list
, ns
);
763 if(xmlStrlen(ns
->prefix
) == 0) {
770 /* add node namespace */
771 if(cur
->ns
!= NULL
) {
774 ns
= xmlSearchNs(cur
->doc
, cur
, NULL
);
775 has_visibly_utilized_empty_ns
= 1;
777 if((ns
!= NULL
) && !xmlC14NIsXmlNs(ns
)) {
778 if(visible
&& xmlC14NIsVisible(ctx
, ns
, cur
)) {
779 if(!xmlExcC14NVisibleNsStackFind(ctx
->ns_rendered
, ns
, ctx
)) {
780 xmlListInsert(list
, ns
);
784 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, ns
, cur
);
786 if(xmlStrlen(ns
->prefix
) == 0) {
793 for(attr
= cur
->properties
; attr
!= NULL
; attr
= attr
->next
) {
795 * we need to check that attribute is visible and has non
796 * default namespace (XML Namespaces: "default namespaces
797 * do not apply directly to attributes")
799 if((attr
->ns
!= NULL
) && !xmlC14NIsXmlNs(attr
->ns
) && xmlC14NIsVisible(ctx
, attr
, cur
)) {
800 already_rendered
= xmlExcC14NVisibleNsStackFind(ctx
->ns_rendered
, attr
->ns
, ctx
);
801 xmlC14NVisibleNsStackAdd(ctx
->ns_rendered
, attr
->ns
, cur
);
802 if(!already_rendered
&& visible
) {
803 xmlListInsert(list
, attr
->ns
);
805 if(xmlStrlen(attr
->ns
->prefix
) == 0) {
808 } else if((attr
->ns
!= NULL
) && (xmlStrlen(attr
->ns
->prefix
) == 0) && (xmlStrlen(attr
->ns
->href
) == 0)) {
809 has_visibly_utilized_empty_ns
= 1;
816 if(visible
&& has_visibly_utilized_empty_ns
&&
817 !has_empty_ns
&& !has_empty_ns_in_inclusive_list
) {
818 static xmlNs ns_default
;
820 memset(&ns_default
, 0, sizeof(ns_default
));
822 already_rendered
= xmlExcC14NVisibleNsStackFind(ctx
->ns_rendered
, &ns_default
, ctx
);
823 if(!already_rendered
) {
824 xmlC14NPrintNamespaces(&ns_default
, ctx
);
826 } else if(visible
&& !has_empty_ns
&& has_empty_ns_in_inclusive_list
) {
827 static xmlNs ns_default
;
829 memset(&ns_default
, 0, sizeof(ns_default
));
830 if(!xmlC14NVisibleNsStackFind(ctx
->ns_rendered
, &ns_default
)) {
831 xmlC14NPrintNamespaces(&ns_default
, ctx
);
838 * print out all elements from list
840 xmlListWalk(list
, (xmlListWalker
) xmlC14NPrintNamespaces
, (const void *) ctx
);
852 * @attr: the attr to check
854 * Checks whether the given attribute is a default "xml:" namespace
855 * with href="http://www.w3.org/XML/1998/namespace"
857 * Returns 1 if the node is default or 0 otherwise
860 /* todo: make it a define? */
862 xmlC14NIsXmlAttr(xmlAttrPtr attr
)
864 return ((attr
->ns
!= NULL
) &&
865 (xmlC14NIsXmlNs(attr
->ns
) != 0));
870 * xmlC14NAttrsCompare:
871 * @attr1: the pointer tls o first attr
872 * @attr2: the pointer to second attr
874 * Prints the given attribute to the output buffer from C14N context.
876 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2.
879 xmlC14NAttrsCompare(xmlAttrPtr attr1
, xmlAttrPtr attr2
)
892 if (attr1
->ns
== attr2
->ns
) {
893 return (xmlStrcmp(attr1
->name
, attr2
->name
));
897 * Attributes in the default namespace are first
898 * because the default namespace is not applied to
899 * unqualified attributes
901 if (attr1
->ns
== NULL
)
903 if (attr2
->ns
== NULL
)
905 if (attr1
->ns
->prefix
== NULL
)
907 if (attr2
->ns
->prefix
== NULL
)
910 ret
= xmlStrcmp(attr1
->ns
->href
, attr2
->ns
->href
);
912 ret
= xmlStrcmp(attr1
->name
, attr2
->name
);
920 * @attr: the pointer to attr
921 * @ctx: the C14N context
923 * Prints out canonical attribute urrent node to the
924 * buffer from C14N context as follows
926 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
928 * Returns 1 on success or 0 on fail.
931 xmlC14NPrintAttrs(const xmlAttrPtr attr
, xmlC14NCtxPtr ctx
)
936 if ((attr
== NULL
) || (ctx
== NULL
)) {
937 xmlC14NErrParam("writing attributes");
941 xmlOutputBufferWriteString(ctx
->buf
, " ");
942 if (attr
->ns
!= NULL
&& xmlStrlen(attr
->ns
->prefix
) > 0) {
943 xmlOutputBufferWriteString(ctx
->buf
,
944 (const char *) attr
->ns
->prefix
);
945 xmlOutputBufferWriteString(ctx
->buf
, ":");
947 xmlOutputBufferWriteString(ctx
->buf
, (const char *) attr
->name
);
948 xmlOutputBufferWriteString(ctx
->buf
, "=\"");
950 value
= xmlNodeListGetString(ctx
->doc
, attr
->children
, 1);
951 /* todo: should we log an error if value==NULL ? */
953 buffer
= xmlC11NNormalizeAttr(value
);
955 if (buffer
!= NULL
) {
956 xmlOutputBufferWriteString(ctx
->buf
, (const char *) buffer
);
959 xmlC14NErrInternal("normalizing attributes axis");
963 xmlOutputBufferWriteString(ctx
->buf
, "\"");
968 * xmlC14NFindHiddenParentAttr:
970 * Finds an attribute in a hidden parent node.
972 * Returns a pointer to the attribute node (if found) or NULL otherwise.
975 xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, const xmlChar
* name
, const xmlChar
* ns
)
978 while((cur
!= NULL
) && (!xmlC14NIsVisible(ctx
, cur
, cur
->parent
))) {
979 res
= xmlHasNsProp(cur
, name
, ns
);
991 * xmlC14NFixupBaseAttr:
993 * Fixes up the xml:base attribute
995 * Returns the newly created attribute or NULL
998 xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx
, xmlAttrPtr xml_base_attr
)
1000 xmlChar
* res
= NULL
;
1007 if ((ctx
== NULL
) || (xml_base_attr
== NULL
) || (xml_base_attr
->parent
== NULL
)) {
1008 xmlC14NErrParam("processing xml:base attribute");
1012 /* start from current value */
1013 res
= xmlNodeListGetString(ctx
->doc
, xml_base_attr
->children
, 1);
1015 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
1019 /* go up the stack until we find a node that we rendered already */
1020 cur
= xml_base_attr
->parent
->parent
;
1021 while((cur
!= NULL
) && (!xmlC14NIsVisible(ctx
, cur
, cur
->parent
))) {
1022 attr
= xmlHasNsProp(cur
, BAD_CAST
"base", XML_XML_NAMESPACE
);
1024 /* get attr value */
1025 tmp_str
= xmlNodeListGetString(ctx
->doc
, attr
->children
, 1);
1026 if(tmp_str
== NULL
) {
1029 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
1033 /* we need to add '/' if our current base uri ends with '..' or '.'
1034 to ensure that we are forced to go "up" all the time */
1035 tmp_str_len
= xmlStrlen(tmp_str
);
1036 if(tmp_str_len
> 1 && tmp_str
[tmp_str_len
- 2] == '.') {
1037 tmp_str2
= xmlStrcat(tmp_str
, BAD_CAST
"/");
1038 if(tmp_str2
== NULL
) {
1042 xmlC14NErrInternal("processing xml:base attribute - can't modify uri");
1050 tmp_str2
= xmlBuildURI(res
, tmp_str
);
1051 if(tmp_str2
== NULL
) {
1055 xmlC14NErrInternal("processing xml:base attribute - can't construct uri");
1059 /* cleanup and set the new res */
1069 /* check if result uri is empty or not */
1070 if((res
== NULL
) || xmlStrEqual(res
, BAD_CAST
"")) {
1075 /* create and return the new attribute node */
1076 attr
= xmlNewNsProp(NULL
, xml_base_attr
->ns
, BAD_CAST
"base", res
);
1080 xmlC14NErrInternal("processing xml:base attribute - can't construct attribute");
1090 * xmlC14NProcessAttrsAxis:
1091 * @ctx: the C14N context
1092 * @cur: the current node
1093 * @parent_visible: the visibility of parent node
1094 * @all_parents_visible: the visibility of all parent nodes
1096 * Prints out canonical attribute axis of the current node to the
1097 * buffer from C14N context as follows
1099 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1102 * In lexicographic order (ascending), process each node that
1103 * is in the element's attribute axis and in the node-set.
1105 * The processing of an element node E MUST be modified slightly
1106 * when an XPath node-set is given as input and the element's
1107 * parent is omitted from the node-set.
1110 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
1112 * Canonical XML applied to a document subset requires the search of the
1113 * ancestor nodes of each orphan element node for attributes in the xml
1114 * namespace, such as xml:lang and xml:space. These are copied into the
1115 * element node except if a declaration of the same attribute is already
1116 * in the attribute axis of the element (whether or not it is included in
1117 * the document subset). This search and copying are omitted from the
1118 * Exclusive XML Canonicalization method.
1120 * Returns 0 on success or -1 on fail.
1123 xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int parent_visible
)
1127 xmlAttrPtr attrs_to_delete
= NULL
;
1129 /* special processing for 1.1 spec */
1130 xmlAttrPtr xml_base_attr
= NULL
;
1131 xmlAttrPtr xml_lang_attr
= NULL
;
1132 xmlAttrPtr xml_space_attr
= NULL
;
1134 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
1135 xmlC14NErrParam("processing attributes axis");
1140 * Create a sorted list to store element attributes
1142 list
= xmlListCreate(NULL
, (xmlListDataCompare
) xmlC14NAttrsCompare
);
1144 xmlC14NErrInternal("creating attributes list");
1150 /* The processing of an element node E MUST be modified slightly when an XPath node-set is
1151 * given as input and the element's parent is omitted from the node-set. The method for processing
1152 * the attribute axis of an element E in the node-set is enhanced. All element nodes along E's
1153 * ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such
1154 * as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,
1155 * remove any that are in E's attribute axis (whether or not they are in the node-set). Then,
1156 * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
1157 * the node-set. The result of visiting the attribute axis is computed by processing the attribute
1158 * nodes in this merged attribute list.
1162 * Add all visible attributes from current node.
1164 attr
= cur
->properties
;
1165 while (attr
!= NULL
) {
1166 /* check that attribute is visible */
1167 if (xmlC14NIsVisible(ctx
, attr
, cur
)) {
1168 xmlListInsert(list
, attr
);
1174 * Handle xml attributes
1176 if (parent_visible
&& (cur
->parent
!= NULL
) &&
1177 (!xmlC14NIsVisible(ctx
, cur
->parent
, cur
->parent
->parent
)))
1182 * If XPath node-set is not specified then the parent is always
1186 while (tmp
!= NULL
) {
1187 attr
= tmp
->properties
;
1188 while (attr
!= NULL
) {
1189 if (xmlC14NIsXmlAttr(attr
) != 0) {
1190 if (xmlListSearch(list
, attr
) == NULL
) {
1191 xmlListInsert(list
, attr
);
1202 case XML_C14N_EXCLUSIVE_1_0
:
1203 /* attributes in the XML namespace, such as xml:lang and xml:space
1204 * are not imported into orphan nodes of the document subset
1208 * Add all visible attributes from current node.
1210 attr
= cur
->properties
;
1211 while (attr
!= NULL
) {
1212 /* check that attribute is visible */
1213 if (xmlC14NIsVisible(ctx
, attr
, cur
)) {
1214 xmlListInsert(list
, attr
);
1219 /* do nothing special for xml attributes */
1222 /* The processing of an element node E MUST be modified slightly when an XPath node-set is
1223 * given as input and some of the element's ancestors are omitted from the node-set.
1225 * Simple inheritable attributes are attributes that have a value that requires at most a simple
1226 * redeclaration. This redeclaration is done by supplying a new value in the child axis. The
1227 * redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done
1228 * by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes
1229 * are xml:lang and xml:space.
1231 * The method for processing the attribute axis of an element E in the node-set is hence enhanced.
1232 * All element nodes along E's ancestor axis are examined for the nearest occurrences of simple
1233 * inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they
1234 * are in the node-set). From this list of attributes, any simple inheritable attributes that are
1235 * already in E's attribute axis (whether or not they are in the node-set) are removed. Then,
1236 * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
1237 * the node-set. The result of visiting the attribute axis is computed by processing the attribute
1238 * nodes in this merged attribute list.
1240 * The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is
1243 * The xml:base attribute is not a simple inheritable attribute and requires special processing beyond
1244 * a simple redeclaration.
1246 * Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed
1247 * as ordinary attributes.
1251 * Add all visible attributes from current node.
1253 attr
= cur
->properties
;
1254 while (attr
!= NULL
) {
1255 /* special processing for XML attribute kiks in only when we have invisible parents */
1256 if ((!parent_visible
) || (xmlC14NIsXmlAttr(attr
) == 0)) {
1257 /* check that attribute is visible */
1258 if (xmlC14NIsVisible(ctx
, attr
, cur
)) {
1259 xmlListInsert(list
, attr
);
1264 /* check for simple inheritance attributes */
1265 if((!matched
) && (xml_lang_attr
== NULL
) && xmlStrEqual(attr
->name
, BAD_CAST
"lang")) {
1266 xml_lang_attr
= attr
;
1269 if((!matched
) && (xml_space_attr
== NULL
) && xmlStrEqual(attr
->name
, BAD_CAST
"space")) {
1270 xml_space_attr
= attr
;
1274 /* check for base attr */
1275 if((!matched
) && (xml_base_attr
== NULL
) && xmlStrEqual(attr
->name
, BAD_CAST
"base")) {
1276 xml_base_attr
= attr
;
1280 /* otherwise, it is a normal attribute, so just check if it is visible */
1281 if((!matched
) && xmlC14NIsVisible(ctx
, attr
, cur
)) {
1282 xmlListInsert(list
, attr
);
1286 /* move to the next one */
1290 /* special processing for XML attribute kiks in only when we have invisible parents */
1291 if ((parent_visible
)) {
1293 /* simple inheritance attributes - copy */
1294 if(xml_lang_attr
== NULL
) {
1295 xml_lang_attr
= xmlC14NFindHiddenParentAttr(ctx
, cur
->parent
, BAD_CAST
"lang", XML_XML_NAMESPACE
);
1297 if(xml_lang_attr
!= NULL
) {
1298 xmlListInsert(list
, xml_lang_attr
);
1300 if(xml_space_attr
== NULL
) {
1301 xml_space_attr
= xmlC14NFindHiddenParentAttr(ctx
, cur
->parent
, BAD_CAST
"space", XML_XML_NAMESPACE
);
1303 if(xml_space_attr
!= NULL
) {
1304 xmlListInsert(list
, xml_space_attr
);
1307 /* base uri attribute - fix up */
1308 if(xml_base_attr
== NULL
) {
1309 /* if we don't have base uri attribute, check if we have a "hidden" one above */
1310 xml_base_attr
= xmlC14NFindHiddenParentAttr(ctx
, cur
->parent
, BAD_CAST
"base", XML_XML_NAMESPACE
);
1312 if(xml_base_attr
!= NULL
) {
1313 xml_base_attr
= xmlC14NFixupBaseAttr(ctx
, xml_base_attr
);
1314 if(xml_base_attr
!= NULL
) {
1315 xmlListInsert(list
, xml_base_attr
);
1317 /* note that we MUST delete returned attr node ourselves! */
1318 xml_base_attr
->next
= attrs_to_delete
;
1319 attrs_to_delete
= xml_base_attr
;
1329 * print out all elements from list
1331 xmlListWalk(list
, (xmlListWalker
) xmlC14NPrintAttrs
, (const void *) ctx
);
1336 xmlFreePropList(attrs_to_delete
);
1337 xmlListDelete(list
);
1342 * xmlC14NCheckForRelativeNamespaces:
1343 * @ctx: the C14N context
1344 * @cur: the current element node
1346 * Checks that current element node has no relative namespaces defined
1348 * Returns 0 if the node has no relative namespaces or -1 otherwise.
1351 xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx
, xmlNodePtr cur
)
1355 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
1356 xmlC14NErrParam("checking for relative namespaces");
1361 while (ns
!= NULL
) {
1362 if (xmlStrlen(ns
->href
) > 0) {
1365 uri
= xmlParseURI((const char *) ns
->href
);
1367 xmlC14NErrInternal("parsing namespace uri");
1370 if (xmlStrlen((const xmlChar
*) uri
->scheme
) == 0) {
1371 xmlC14NErrRelativeNamespace(uri
->scheme
);
1375 if ((xmlStrcasecmp((const xmlChar
*) uri
->scheme
, BAD_CAST
"urn") != 0)
1376 && (xmlStrcasecmp((const xmlChar
*) uri
->scheme
, BAD_CAST
"dav") !=0)
1377 && (xmlStrlen((const xmlChar
*) uri
->server
) == 0)) {
1378 xmlC14NErrRelativeNamespace(uri
->scheme
);
1390 * xmlC14NProcessElementNode:
1391 * @ctx: the pointer to C14N context object
1392 * @cur: the node to process
1393 * @visible: this node is visible
1394 * @all_parents_visible: whether all the parents of this node are visible
1396 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1399 * If the element is not in the node-set, then the result is obtained
1400 * by processing the namespace axis, then the attribute axis, then
1401 * processing the child nodes of the element that are in the node-set
1402 * (in document order). If the element is in the node-set, then the result
1403 * is an open angle bracket (<), the element QName, the result of
1404 * processing the namespace axis, the result of processing the attribute
1405 * axis, a close angle bracket (>), the result of processing the child
1406 * nodes of the element that are in the node-set (in document order), an
1407 * open angle bracket, a forward slash (/), the element QName, and a close
1410 * Returns non-negative value on success or negative value on fail
1413 xmlC14NProcessElementNode(xmlC14NCtxPtr ctx
, xmlNodePtr cur
, int visible
)
1416 xmlC14NVisibleNsStack state
;
1417 int parent_is_doc
= 0;
1419 if ((ctx
== NULL
) || (cur
== NULL
) || (cur
->type
!= XML_ELEMENT_NODE
)) {
1420 xmlC14NErrParam("processing element node");
1425 * Check relative relative namespaces:
1426 * implementations of XML canonicalization MUST report an operation
1427 * failure on documents containing relative namespace URIs.
1429 if (xmlC14NCheckForRelativeNamespaces(ctx
, cur
) < 0) {
1430 xmlC14NErrInternal("checking for relative namespaces");
1436 * Save ns_rendered stack position
1438 memset(&state
, 0, sizeof(state
));
1439 xmlC14NVisibleNsStackSave(ctx
->ns_rendered
, &state
);
1442 if (ctx
->parent_is_doc
) {
1443 /* save this flag into the stack */
1444 parent_is_doc
= ctx
->parent_is_doc
;
1445 ctx
->parent_is_doc
= 0;
1446 ctx
->pos
= XMLC14N_INSIDE_DOCUMENT_ELEMENT
;
1448 xmlOutputBufferWriteString(ctx
->buf
, "<");
1450 if ((cur
->ns
!= NULL
) && (xmlStrlen(cur
->ns
->prefix
) > 0)) {
1451 xmlOutputBufferWriteString(ctx
->buf
,
1452 (const char *) cur
->ns
->prefix
);
1453 xmlOutputBufferWriteString(ctx
->buf
, ":");
1455 xmlOutputBufferWriteString(ctx
->buf
, (const char *) cur
->name
);
1458 if (!xmlC14NIsExclusive(ctx
)) {
1459 ret
= xmlC14NProcessNamespacesAxis(ctx
, cur
, visible
);
1461 ret
= xmlExcC14NProcessNamespacesAxis(ctx
, cur
, visible
);
1464 xmlC14NErrInternal("processing namespaces axis");
1467 /* todo: shouldn't this go to "visible only"? */
1469 xmlC14NVisibleNsStackShift(ctx
->ns_rendered
);
1472 ret
= xmlC14NProcessAttrsAxis(ctx
, cur
, visible
);
1474 xmlC14NErrInternal("processing attributes axis");
1479 xmlOutputBufferWriteString(ctx
->buf
, ">");
1481 if (cur
->children
!= NULL
) {
1482 ret
= xmlC14NProcessNodeList(ctx
, cur
->children
);
1484 xmlC14NErrInternal("processing childrens list");
1489 xmlOutputBufferWriteString(ctx
->buf
, "</");
1490 if ((cur
->ns
!= NULL
) && (xmlStrlen(cur
->ns
->prefix
) > 0)) {
1491 xmlOutputBufferWriteString(ctx
->buf
,
1492 (const char *) cur
->ns
->prefix
);
1493 xmlOutputBufferWriteString(ctx
->buf
, ":");
1495 xmlOutputBufferWriteString(ctx
->buf
, (const char *) cur
->name
);
1496 xmlOutputBufferWriteString(ctx
->buf
, ">");
1497 if (parent_is_doc
) {
1498 /* restore this flag from the stack for next node */
1499 ctx
->parent_is_doc
= parent_is_doc
;
1500 ctx
->pos
= XMLC14N_AFTER_DOCUMENT_ELEMENT
;
1505 * Restore ns_rendered stack position
1507 xmlC14NVisibleNsStackRestore(ctx
->ns_rendered
, &state
);
1512 * xmlC14NProcessNode:
1513 * @ctx: the pointer to C14N context object
1514 * @cur: the node to process
1516 * Processes the given node
1518 * Returns non-negative value on success or negative value on fail
1521 xmlC14NProcessNode(xmlC14NCtxPtr ctx
, xmlNodePtr cur
)
1526 if ((ctx
== NULL
) || (cur
== NULL
)) {
1527 xmlC14NErrParam("processing node");
1531 visible
= xmlC14NIsVisible(ctx
, cur
, cur
->parent
);
1532 switch (cur
->type
) {
1533 case XML_ELEMENT_NODE
:
1534 ret
= xmlC14NProcessElementNode(ctx
, cur
, visible
);
1536 case XML_CDATA_SECTION_NODE
:
1540 * the string value, except all ampersands are replaced
1541 * by &, all open angle brackets (<) are replaced by <, all closing
1542 * angle brackets (>) are replaced by >, and all #xD characters are
1543 * replaced by 
.
1545 /* cdata sections are processed as text nodes */
1546 /* todo: verify that cdata sections are included in XPath nodes set */
1547 if ((visible
) && (cur
->content
!= NULL
)) {
1550 buffer
= xmlC11NNormalizeText(cur
->content
);
1551 if (buffer
!= NULL
) {
1552 xmlOutputBufferWriteString(ctx
->buf
,
1553 (const char *) buffer
);
1556 xmlC14NErrInternal("normalizing text node");
1563 * Processing Instruction (PI) Nodes-
1564 * The opening PI symbol (<?), the PI target name of the node,
1565 * a leading space and the string value if it is not empty, and
1566 * the closing PI symbol (?>). If the string value is empty,
1567 * then the leading space is not added. Also, a trailing #xA is
1568 * rendered after the closing PI symbol for PI children of the
1569 * root node with a lesser document order than the document
1570 * element, and a leading #xA is rendered before the opening PI
1571 * symbol of PI children of the root node with a greater document
1572 * order than the document element.
1575 if (ctx
->pos
== XMLC14N_AFTER_DOCUMENT_ELEMENT
) {
1576 xmlOutputBufferWriteString(ctx
->buf
, "\x0A<?");
1578 xmlOutputBufferWriteString(ctx
->buf
, "<?");
1581 xmlOutputBufferWriteString(ctx
->buf
,
1582 (const char *) cur
->name
);
1583 if ((cur
->content
!= NULL
) && (*(cur
->content
) != '\0')) {
1586 xmlOutputBufferWriteString(ctx
->buf
, " ");
1588 /* todo: do we need to normalize pi? */
1589 buffer
= xmlC11NNormalizePI(cur
->content
);
1590 if (buffer
!= NULL
) {
1591 xmlOutputBufferWriteString(ctx
->buf
,
1592 (const char *) buffer
);
1595 xmlC14NErrInternal("normalizing pi node");
1600 if (ctx
->pos
== XMLC14N_BEFORE_DOCUMENT_ELEMENT
) {
1601 xmlOutputBufferWriteString(ctx
->buf
, "?>\x0A");
1603 xmlOutputBufferWriteString(ctx
->buf
, "?>");
1607 case XML_COMMENT_NODE
:
1610 * Nothing if generating canonical XML without comments. For
1611 * canonical XML with comments, generate the opening comment
1612 * symbol (<!--), the string value of the node, and the
1613 * closing comment symbol (-->). Also, a trailing #xA is rendered
1614 * after the closing comment symbol for comment children of the
1615 * root node with a lesser document order than the document
1616 * element, and a leading #xA is rendered before the opening
1617 * comment symbol of comment children of the root node with a
1618 * greater document order than the document element. (Comment
1619 * children of the root node represent comments outside of the
1620 * top-level document element and outside of the document type
1623 if (visible
&& ctx
->with_comments
) {
1624 if (ctx
->pos
== XMLC14N_AFTER_DOCUMENT_ELEMENT
) {
1625 xmlOutputBufferWriteString(ctx
->buf
, "\x0A<!--");
1627 xmlOutputBufferWriteString(ctx
->buf
, "<!--");
1630 if (cur
->content
!= NULL
) {
1633 /* todo: do we need to normalize comment? */
1634 buffer
= xmlC11NNormalizeComment(cur
->content
);
1635 if (buffer
!= NULL
) {
1636 xmlOutputBufferWriteString(ctx
->buf
,
1637 (const char *) buffer
);
1640 xmlC14NErrInternal("normalizing comment node");
1645 if (ctx
->pos
== XMLC14N_BEFORE_DOCUMENT_ELEMENT
) {
1646 xmlOutputBufferWriteString(ctx
->buf
, "-->\x0A");
1648 xmlOutputBufferWriteString(ctx
->buf
, "-->");
1652 case XML_DOCUMENT_NODE
:
1653 case XML_DOCUMENT_FRAG_NODE
: /* should be processed as document? */
1654 #ifdef LIBXML_DOCB_ENABLED
1655 case XML_DOCB_DOCUMENT_NODE
: /* should be processed as document? */
1657 #ifdef LIBXML_HTML_ENABLED
1658 case XML_HTML_DOCUMENT_NODE
: /* should be processed as document? */
1660 if (cur
->children
!= NULL
) {
1661 ctx
->pos
= XMLC14N_BEFORE_DOCUMENT_ELEMENT
;
1662 ctx
->parent_is_doc
= 1;
1663 ret
= xmlC14NProcessNodeList(ctx
, cur
->children
);
1667 case XML_ATTRIBUTE_NODE
:
1668 xmlC14NErrInvalidNode("XML_ATTRIBUTE_NODE", "processing node");
1670 case XML_NAMESPACE_DECL
:
1671 xmlC14NErrInvalidNode("XML_NAMESPACE_DECL", "processing node");
1673 case XML_ENTITY_REF_NODE
:
1674 xmlC14NErrInvalidNode("XML_ENTITY_REF_NODE", "processing node");
1676 case XML_ENTITY_NODE
:
1677 xmlC14NErrInvalidNode("XML_ENTITY_NODE", "processing node");
1680 case XML_DOCUMENT_TYPE_NODE
:
1681 case XML_NOTATION_NODE
:
1683 case XML_ELEMENT_DECL
:
1684 case XML_ATTRIBUTE_DECL
:
1685 case XML_ENTITY_DECL
:
1686 #ifdef LIBXML_XINCLUDE_ENABLED
1687 case XML_XINCLUDE_START
:
1688 case XML_XINCLUDE_END
:
1691 * should be ignored according to "W3C Canonical XML"
1695 xmlC14NErrUnknownNode(cur
->type
, "processing node");
1703 * xmlC14NProcessNodeList:
1704 * @ctx: the pointer to C14N context object
1705 * @cur: the node to start from
1707 * Processes all nodes in the row starting from cur.
1709 * Returns non-negative value on success or negative value on fail
1712 xmlC14NProcessNodeList(xmlC14NCtxPtr ctx
, xmlNodePtr cur
)
1717 xmlC14NErrParam("processing node list");
1721 for (ret
= 0; cur
!= NULL
&& ret
>= 0; cur
= cur
->next
) {
1722 ret
= xmlC14NProcessNode(ctx
, cur
);
1730 * @ctx: the pointer to C14N context object
1732 * Cleanups the C14N context object.
1736 xmlC14NFreeCtx(xmlC14NCtxPtr ctx
)
1739 xmlC14NErrParam("freeing context");
1743 if (ctx
->ns_rendered
!= NULL
) {
1744 xmlC14NVisibleNsStackDestroy(ctx
->ns_rendered
);
1751 * @doc: the XML document for canonization
1752 * @is_visible_callback:the function to use to determine is node visible
1754 * @user_data: the first parameter for @is_visible_callback function
1755 * (in most cases, it is nodes set)
1756 * @mode: the c14n mode (see @xmlC14NMode)
1757 * @inclusive_ns_prefixe the list of inclusive namespace prefixes
1758 * ended with a NULL or NULL if there is no
1759 * inclusive namespaces (only for `
1761 * @with_comments: include comments in the result (!=0) or not (==0)
1762 * @buf: the output buffer to store canonical XML; this
1763 * buffer MUST have encoder==NULL because C14N requires
1766 * Creates new C14N context object to store C14N parameters.
1768 * Returns pointer to newly created object (success) or NULL (fail)
1770 static xmlC14NCtxPtr
1771 xmlC14NNewCtx(xmlDocPtr doc
,
1772 xmlC14NIsVisibleCallback is_visible_callback
, void* user_data
,
1773 xmlC14NMode mode
, xmlChar
** inclusive_ns_prefixes
,
1774 int with_comments
, xmlOutputBufferPtr buf
)
1776 xmlC14NCtxPtr ctx
= NULL
;
1778 if ((doc
== NULL
) || (buf
== NULL
)) {
1779 xmlC14NErrParam("creating new context");
1784 * Validate the encoding output buffer encoding
1786 if (buf
->encoder
!= NULL
) {
1787 xmlC14NErr(ctx
, (xmlNodePtr
) doc
, XML_C14N_REQUIRES_UTF8
,
1788 "xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n");
1793 * Validate the XML document encoding value, if provided.
1795 if (doc
->charset
!= XML_CHAR_ENCODING_UTF8
) {
1796 xmlC14NErr(ctx
, (xmlNodePtr
) doc
, XML_C14N_REQUIRES_UTF8
,
1797 "xmlC14NNewCtx: source document not in UTF8\n");
1802 * Allocate a new xmlC14NCtxPtr and fill the fields.
1804 ctx
= (xmlC14NCtxPtr
) xmlMalloc(sizeof(xmlC14NCtx
));
1806 xmlC14NErrMemory("creating context");
1809 memset(ctx
, 0, sizeof(xmlC14NCtx
));
1812 * initialize C14N context
1815 ctx
->with_comments
= with_comments
;
1816 ctx
->is_visible_callback
= is_visible_callback
;
1817 ctx
->user_data
= user_data
;
1819 ctx
->parent_is_doc
= 1;
1820 ctx
->pos
= XMLC14N_BEFORE_DOCUMENT_ELEMENT
;
1821 ctx
->ns_rendered
= xmlC14NVisibleNsStackCreate();
1823 if(ctx
->ns_rendered
== NULL
) {
1824 xmlC14NErr(ctx
, (xmlNodePtr
) doc
, XML_C14N_CREATE_STACK
,
1825 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n");
1826 xmlC14NFreeCtx(ctx
);
1831 * Set "mode" flag and remember list of incluseve prefixes
1832 * for exclusive c14n
1835 if(xmlC14NIsExclusive(ctx
)) {
1836 ctx
->inclusive_ns_prefixes
= inclusive_ns_prefixes
;
1843 * @doc: the XML document for canonization
1844 * @is_visible_callback:the function to use to determine is node visible
1846 * @user_data: the first parameter for @is_visible_callback function
1847 * (in most cases, it is nodes set)
1848 * @mode: the c14n mode (see @xmlC14NMode)
1849 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1850 * ended with a NULL or NULL if there is no
1851 * inclusive namespaces (only for exclusive
1852 * canonicalization, ignored otherwise)
1853 * @with_comments: include comments in the result (!=0) or not (==0)
1854 * @buf: the output buffer to store canonical XML; this
1855 * buffer MUST have encoder==NULL because C14N requires
1858 * Dumps the canonized image of given XML document into the provided buffer.
1859 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1860 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1862 * Returns non-negative value on success or a negative value on fail
1865 xmlC14NExecute(xmlDocPtr doc
, xmlC14NIsVisibleCallback is_visible_callback
,
1866 void* user_data
, int mode
, xmlChar
**inclusive_ns_prefixes
,
1867 int with_comments
, xmlOutputBufferPtr buf
) {
1870 xmlC14NMode c14n_mode
= XML_C14N_1_0
;
1873 if ((buf
== NULL
) || (doc
== NULL
)) {
1874 xmlC14NErrParam("executing c14n");
1878 /* for backward compatibility, we have to have "mode" as "int"
1879 and here we check that user gives valid value */
1882 case XML_C14N_EXCLUSIVE_1_0
:
1884 c14n_mode
= (xmlC14NMode
)mode
;
1887 xmlC14NErrParam("invalid mode for executing c14n");
1892 * Validate the encoding output buffer encoding
1894 if (buf
->encoder
!= NULL
) {
1895 xmlC14NErr(NULL
, (xmlNodePtr
) doc
, XML_C14N_REQUIRES_UTF8
,
1896 "xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
1900 ctx
= xmlC14NNewCtx(doc
, is_visible_callback
, user_data
,
1901 c14n_mode
, inclusive_ns_prefixes
,
1902 with_comments
, buf
);
1904 xmlC14NErr(NULL
, (xmlNodePtr
) doc
, XML_C14N_CREATE_CTXT
,
1905 "xmlC14NExecute: unable to create C14N context\n");
1913 * The root node is the parent of the top-level document element. The
1914 * result of processing each of its child nodes that is in the node-set
1915 * in document order. The root node does not generate a byte order mark,
1916 * XML declaration, nor anything from within the document type
1919 if (doc
->children
!= NULL
) {
1920 ret
= xmlC14NProcessNodeList(ctx
, doc
->children
);
1922 xmlC14NErrInternal("processing docs children list");
1923 xmlC14NFreeCtx(ctx
);
1929 * Flush buffer to get number of bytes written
1931 ret
= xmlOutputBufferFlush(buf
);
1933 xmlC14NErrInternal("flushing output buffer");
1934 xmlC14NFreeCtx(ctx
);
1941 xmlC14NFreeCtx(ctx
);
1947 * @doc: the XML document for canonization
1948 * @nodes: the nodes set to be included in the canonized image
1949 * or NULL if all document nodes should be included
1950 * @mode: the c14n mode (see @xmlC14NMode)
1951 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1952 * ended with a NULL or NULL if there is no
1953 * inclusive namespaces (only for exclusive
1954 * canonicalization, ignored otherwise)
1955 * @with_comments: include comments in the result (!=0) or not (==0)
1956 * @buf: the output buffer to store canonical XML; this
1957 * buffer MUST have encoder==NULL because C14N requires
1960 * Dumps the canonized image of given XML document into the provided buffer.
1961 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1962 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1964 * Returns non-negative value on success or a negative value on fail
1967 xmlC14NDocSaveTo(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
1968 int mode
, xmlChar
** inclusive_ns_prefixes
,
1969 int with_comments
, xmlOutputBufferPtr buf
) {
1970 return(xmlC14NExecute(doc
,
1971 (xmlC14NIsVisibleCallback
)xmlC14NIsNodeInNodeset
,
1974 inclusive_ns_prefixes
,
1981 * xmlC14NDocDumpMemory:
1982 * @doc: the XML document for canonization
1983 * @nodes: the nodes set to be included in the canonized image
1984 * or NULL if all document nodes should be included
1985 * @mode: the c14n mode (see @xmlC14NMode)
1986 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1987 * ended with a NULL or NULL if there is no
1988 * inclusive namespaces (only for exclusive
1989 * canonicalization, ignored otherwise)
1990 * @with_comments: include comments in the result (!=0) or not (==0)
1991 * @doc_txt_ptr: the memory pointer for allocated canonical XML text;
1992 * the caller of this functions is responsible for calling
1993 * xmlFree() to free allocated memory
1995 * Dumps the canonized image of given XML document into memory.
1996 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1997 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1999 * Returns the number of bytes written on success or a negative value on fail
2002 xmlC14NDocDumpMemory(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
2003 int mode
, xmlChar
** inclusive_ns_prefixes
,
2004 int with_comments
, xmlChar
** doc_txt_ptr
)
2007 xmlOutputBufferPtr buf
;
2009 if (doc_txt_ptr
== NULL
) {
2010 xmlC14NErrParam("dumping doc to memory");
2014 *doc_txt_ptr
= NULL
;
2017 * create memory buffer with UTF8 (default) encoding
2019 buf
= xmlAllocOutputBuffer(NULL
);
2021 xmlC14NErrMemory("creating output buffer");
2026 * canonize document and write to buffer
2028 ret
= xmlC14NDocSaveTo(doc
, nodes
, mode
, inclusive_ns_prefixes
,
2029 with_comments
, buf
);
2031 xmlC14NErrInternal("saving doc to output buffer");
2032 (void) xmlOutputBufferClose(buf
);
2036 ret
= buf
->buffer
->use
;
2038 *doc_txt_ptr
= xmlStrndup(buf
->buffer
->content
, ret
);
2040 (void) xmlOutputBufferClose(buf
);
2042 if ((*doc_txt_ptr
== NULL
) && (ret
> 0)) {
2043 xmlC14NErrMemory("coping canonicanized document");
2051 * @doc: the XML document for canonization
2052 * @nodes: the nodes set to be included in the canonized image
2053 * or NULL if all document nodes should be included
2054 * @mode: the c14n mode (see @xmlC14NMode)
2055 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
2056 * ended with a NULL or NULL if there is no
2057 * inclusive namespaces (only for exclusive
2058 * canonicalization, ignored otherwise)
2059 * @with_comments: include comments in the result (!=0) or not (==0)
2060 * @filename: the filename to store canonical XML image
2061 * @compression: the compression level (zlib requred):
2062 * -1 - libxml default,
2064 * >0 - compression level
2066 * Dumps the canonized image of given XML document into the file.
2067 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
2068 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
2070 * Returns the number of bytes written success or a negative value on fail
2073 xmlC14NDocSave(xmlDocPtr doc
, xmlNodeSetPtr nodes
,
2074 int mode
, xmlChar
** inclusive_ns_prefixes
,
2075 int with_comments
, const char *filename
, int compression
)
2077 xmlOutputBufferPtr buf
;
2080 if (filename
== NULL
) {
2081 xmlC14NErrParam("saving doc");
2085 if (compression
< 0)
2086 compression
= xmlGetCompressMode();
2090 * save the content to a temp buffer, use default UTF8 encoding.
2092 buf
= xmlOutputBufferCreateFilename(filename
, NULL
, compression
);
2094 xmlC14NErrInternal("creating temporary filename");
2099 * canonize document and write to buffer
2101 ret
= xmlC14NDocSaveTo(doc
, nodes
, mode
, inclusive_ns_prefixes
,
2102 with_comments
, buf
);
2104 xmlC14NErrInternal("cannicanize document to buffer");
2105 (void) xmlOutputBufferClose(buf
);
2110 * get the numbers of bytes written
2112 ret
= xmlOutputBufferClose(buf
);
2119 * Macro used to grow the current buffer.
2121 #define growBufferReentrant() { \
2123 buffer = (xmlChar *) \
2124 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
2125 if (buffer == NULL) { \
2126 xmlC14NErrMemory("growing buffer"); \
2132 * xmlC11NNormalizeString:
2133 * @input: the input string
2134 * @mode: the normalization mode (attribute, comment, PI or text)
2136 * Converts a string to a canonical (normalized) format. The code is stolen
2137 * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A
2138 * and the @mode parameter
2140 * Returns a normalized string (caller is responsible for calling xmlFree())
2141 * or NULL if an error occurs
2144 xmlC11NNormalizeString(const xmlChar
* input
,
2145 xmlC14NNormalizationMode mode
)
2147 const xmlChar
*cur
= input
;
2148 xmlChar
*buffer
= NULL
;
2149 xmlChar
*out
= NULL
;
2150 int buffer_size
= 0;
2156 * allocate an translation buffer.
2159 buffer
= (xmlChar
*) xmlMallocAtomic(buffer_size
* sizeof(xmlChar
));
2160 if (buffer
== NULL
) {
2161 xmlC14NErrMemory("allocating buffer");
2166 while (*cur
!= '\0') {
2167 if ((out
- buffer
) > (buffer_size
- 10)) {
2168 int indx
= out
- buffer
;
2170 growBufferReentrant();
2171 out
= &buffer
[indx
];
2174 if ((*cur
== '<') && ((mode
== XMLC14N_NORMALIZE_ATTR
) ||
2175 (mode
== XMLC14N_NORMALIZE_TEXT
))) {
2180 } else if ((*cur
== '>') && (mode
== XMLC14N_NORMALIZE_TEXT
)) {
2185 } else if ((*cur
== '&') && ((mode
== XMLC14N_NORMALIZE_ATTR
) ||
2186 (mode
== XMLC14N_NORMALIZE_TEXT
))) {
2192 } else if ((*cur
== '"') && (mode
== XMLC14N_NORMALIZE_ATTR
)) {
2199 } else if ((*cur
== '\x09') && (mode
== XMLC14N_NORMALIZE_ATTR
)) {
2205 } else if ((*cur
== '\x0A') && (mode
== XMLC14N_NORMALIZE_ATTR
)) {
2211 } else if ((*cur
== '\x0D') && ((mode
== XMLC14N_NORMALIZE_ATTR
) ||
2212 (mode
== XMLC14N_NORMALIZE_TEXT
) ||
2213 (mode
== XMLC14N_NORMALIZE_COMMENT
) ||
2214 (mode
== XMLC14N_NORMALIZE_PI
))) {
2222 * Works because on UTF-8, all extended sequences cannot
2223 * result in bytes in the ASCII range.
2232 #endif /* LIBXML_OUTPUT_ENABLED */
2234 #include "elfgcchack.h"
2235 #endif /* LIBXML_C14N_ENABLED */