blob: afd95b352326d770aa5aead0fa6f9a3223db1fb1 [file] [log] [blame]
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001/*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002 * "Canonical XML" implementation
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003 * http://www.w3.org/TR/xml-c14n
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005 * "Exclusive XML Canonicalization" implementation
6 * http://www.w3.org/TR/xml-exc-c14n
7 *
8 * See Copyright for the status of this software.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08009 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +000010 * Author: Aleksey Sanin <aleksey@aleksey.com>
11 */
Daniel Veillard34ce8be2002-03-18 19:37:11 +000012#define IN_LIBXML
Daniel Veillard044fc6b2002-03-04 17:09:44 +000013#include "libxml.h"
14#ifdef LIBXML_C14N_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000015#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard044fc6b2002-03-04 17:09:44 +000016
17#ifdef HAVE_STDLIB_H
18#include <stdlib.h>
19#endif
20#include <string.h>
21
22#include <libxml/tree.h>
23#include <libxml/parser.h>
Daniel Veillard9ff88172002-03-11 09:15:32 +000024#include <libxml/uri.h>
Daniel Veillard044fc6b2002-03-04 17:09:44 +000025#include <libxml/xmlerror.h>
26#include <libxml/globals.h>
27#include <libxml/xpathInternals.h>
28#include <libxml/c14n.h>
29
Daniel Veillard53aa2932012-07-16 14:37:00 +080030#include "buf.h"
31
Daniel Veillard044fc6b2002-03-04 17:09:44 +000032/************************************************************************
33 * *
34 * Some declaration better left private ATM *
35 * *
36 ************************************************************************/
37
Daniel Veillard9ff88172002-03-11 09:15:32 +000038typedef enum {
39 XMLC14N_BEFORE_DOCUMENT_ELEMENT = 0,
40 XMLC14N_INSIDE_DOCUMENT_ELEMENT = 1,
41 XMLC14N_AFTER_DOCUMENT_ELEMENT = 2
Daniel Veillard044fc6b2002-03-04 17:09:44 +000042} xmlC14NPosition;
43
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +000044typedef struct _xmlC14NVisibleNsStack {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +000045 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 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080049 xmlNsPtr *nsTab; /* array of ns in no particular order */
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +000050 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +000051} xmlC14NVisibleNsStack, *xmlC14NVisibleNsStackPtr;
Aleksey Sanindffd5c82002-05-31 04:24:13 +000052
Daniel Veillard044fc6b2002-03-04 17:09:44 +000053typedef struct _xmlC14NCtx {
54 /* input parameters */
Daniel Veillard9ff88172002-03-11 09:15:32 +000055 xmlDocPtr doc;
Aleksey Sanin2c135a12002-08-01 06:31:50 +000056 xmlC14NIsVisibleCallback is_visible_callback;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080057 void* user_data;
Daniel Veillard9ff88172002-03-11 09:15:32 +000058 int with_comments;
59 xmlOutputBufferPtr buf;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000060
61 /* position in the XML document */
Daniel Veillard9ff88172002-03-11 09:15:32 +000062 xmlC14NPosition pos;
63 int parent_is_doc;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +000064 xmlC14NVisibleNsStackPtr ns_rendered;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080065
Aleksey Sanin83868242009-07-09 10:26:22 +020066 /* C14N mode */
67 xmlC14NMode mode;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000068
69 /* exclusive canonicalization */
Daniel Veillard9ff88172002-03-11 09:15:32 +000070 xmlChar **inclusive_ns_prefixes;
Daniel Veillardd96cce12003-10-10 12:30:37 +000071
72 /* error number */
73 int error;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000074} xmlC14NCtx, *xmlC14NCtxPtr;
75
Aleksey Sanin2c135a12002-08-01 06:31:50 +000076static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate (void);
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +000077static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur);
Daniel Veillardf8e3db02012-09-11 13:26:36 +080078static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cur,
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +000079 xmlNsPtr ns,
80 xmlNodePtr node);
Daniel Veillardf8e3db02012-09-11 13:26:36 +080081static void xmlC14NVisibleNsStackSave (xmlC14NVisibleNsStackPtr cur,
Aleksey Sanin2c135a12002-08-01 06:31:50 +000082 xmlC14NVisibleNsStackPtr state);
Daniel Veillardf8e3db02012-09-11 13:26:36 +080083static void xmlC14NVisibleNsStackRestore (xmlC14NVisibleNsStackPtr cur,
Aleksey Sanin2c135a12002-08-01 06:31:50 +000084 xmlC14NVisibleNsStackPtr state);
Daniel Veillardf8e3db02012-09-11 13:26:36 +080085static void xmlC14NVisibleNsStackShift (xmlC14NVisibleNsStackPtr cur);
86static int xmlC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur,
Aleksey Sanin2c135a12002-08-01 06:31:50 +000087 xmlNsPtr ns);
Daniel Veillardf8e3db02012-09-11 13:26:36 +080088static int xmlExcC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur,
Aleksey Sanin2c135a12002-08-01 06:31:50 +000089 xmlNsPtr ns,
90 xmlC14NCtxPtr ctx);
91
92static int xmlC14NIsNodeInNodeset (xmlNodeSetPtr nodes,
93 xmlNodePtr node,
94 xmlNodePtr parent);
95
96
Daniel Veillard044fc6b2002-03-04 17:09:44 +000097
Daniel Veillard9ff88172002-03-11 09:15:32 +000098static int xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur);
99static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000100typedef enum {
Daniel Veillard9ff88172002-03-11 09:15:32 +0000101 XMLC14N_NORMALIZE_ATTR = 0,
102 XMLC14N_NORMALIZE_COMMENT = 1,
103 XMLC14N_NORMALIZE_PI = 2,
104 XMLC14N_NORMALIZE_TEXT = 3
105} xmlC14NNormalizationMode;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000106
Daniel Veillard9ff88172002-03-11 09:15:32 +0000107static xmlChar *xmlC11NNormalizeString(const xmlChar * input,
108 xmlC14NNormalizationMode mode);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000109
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800110#define xmlC11NNormalizeAttr( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000111 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800112#define xmlC11NNormalizeComment( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000113 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800114#define xmlC11NNormalizePI( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000115 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800116#define xmlC11NNormalizeText( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000117 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT)
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000118
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800119#define xmlC14NIsVisible( ctx, node, parent ) \
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000120 (((ctx)->is_visible_callback != NULL) ? \
121 (ctx)->is_visible_callback((ctx)->user_data, \
122 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1)
Daniel Veillardd96cce12003-10-10 12:30:37 +0000123
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800124#define xmlC14NIsExclusive( ctx ) \
Aleksey Sanin83868242009-07-09 10:26:22 +0200125 ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 )
126
Daniel Veillardd96cce12003-10-10 12:30:37 +0000127/************************************************************************
128 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800129 * Some factorized error routines *
Daniel Veillardd96cce12003-10-10 12:30:37 +0000130 * *
131 ************************************************************************/
132
133/**
134 * xmlC14NErrMemory:
135 * @extra: extra informations
136 *
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000137 * Handle a redefinition of memory error
Daniel Veillardd96cce12003-10-10 12:30:37 +0000138 */
139static void
140xmlC14NErrMemory(const char *extra)
141{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000142 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
Daniel Veillardd96cce12003-10-10 12:30:37 +0000143 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
144 NULL, NULL, 0, 0,
145 "Memory allocation failed : %s\n", extra);
146}
147
148/**
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000149 * xmlC14NErrParam:
150 * @extra: extra informations
151 *
152 * Handle a redefinition of param error
153 */
154static void
155xmlC14NErrParam(const char *extra)
156{
157 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
158 XML_ERR_INTERNAL_ERROR, XML_ERR_ERROR, NULL, 0, extra,
159 NULL, NULL, 0, 0,
160 "Invalid parameter : %s\n", extra);
161}
162
163/**
164 * xmlC14NErrInternal:
165 * @extra: extra informations
166 *
167 * Handle a redefinition of internal error
168 */
169static void
170xmlC14NErrInternal(const char *extra)
171{
172 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
173 XML_ERR_INTERNAL_ERROR, XML_ERR_ERROR, NULL, 0, extra,
174 NULL, NULL, 0, 0,
175 "Internal error : %s\n", extra);
176}
177
178/**
179 * xmlC14NErrInvalidNode:
180 * @extra: extra informations
181 *
182 * Handle a redefinition of invalid node error
183 */
184static void
185xmlC14NErrInvalidNode(const char *node_type, const char *extra)
186{
187 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
188 XML_C14N_INVALID_NODE, XML_ERR_ERROR, NULL, 0, extra,
189 NULL, NULL, 0, 0,
190 "Node %s is invalid here : %s\n", node_type, extra);
191}
192
193/**
194 * xmlC14NErrUnknownNode:
195 * @extra: extra informations
196 *
197 * Handle a redefinition of unknown node error
198 */
199static void
200xmlC14NErrUnknownNode(int node_type, const char *extra)
201{
202 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
203 XML_C14N_UNKNOW_NODE, XML_ERR_ERROR, NULL, 0, extra,
204 NULL, NULL, 0, 0,
205 "Unknown node type %d found : %s\n", node_type, extra);
206}
207
208/**
209 * xmlC14NErrRelativeNamespace:
210 * @extra: extra informations
211 *
212 * Handle a redefinition of relative namespace error
213 */
214static void
215xmlC14NErrRelativeNamespace(const char *ns_uri)
216{
217 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
218 XML_C14N_RELATIVE_NAMESPACE, XML_ERR_ERROR, NULL, 0, NULL,
219 NULL, NULL, 0, 0,
220 "Relative namespace UR is invalid here : %s\n", ns_uri);
221}
222
223
224
225/**
Daniel Veillardd96cce12003-10-10 12:30:37 +0000226 * xmlC14NErr:
227 * @ctxt: a C14N evaluation context
228 * @node: the context node
229 * @error: the erorr code
230 * @msg: the message
231 * @extra: extra informations
232 *
233 * Handle a redefinition of attribute error
234 */
235static void
236xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int error,
237 const char * msg)
238{
239 if (ctxt != NULL)
240 ctxt->error = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000241 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96cce12003-10-10 12:30:37 +0000242 ctxt, node, XML_FROM_C14N, error,
243 XML_ERR_ERROR, NULL, 0,
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200244 NULL, NULL, NULL, 0, 0, "%s", msg);
Daniel Veillardd96cce12003-10-10 12:30:37 +0000245}
246
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000247/************************************************************************
248 * *
249 * The implementation internals *
250 * *
251 ************************************************************************/
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000252#define XML_NAMESPACES_DEFAULT 16
253
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800254static int
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000255xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes, xmlNodePtr node, xmlNodePtr parent) {
256 if((nodes != NULL) && (node != NULL)) {
257 if(node->type != XML_NAMESPACE_DECL) {
258 return(xmlXPathNodeSetContains(nodes, node));
259 } else {
260 xmlNs ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800261
262 memcpy(&ns, node, sizeof(ns));
263
Aleksey Sanin6de6f972004-04-20 02:05:30 +0000264 /* this is a libxml hack! check xpath.c for details */
265 if((parent != NULL) && (parent->type == XML_ATTRIBUTE_NODE)) {
266 ns.next = (xmlNsPtr)parent->parent;
267 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800268 ns.next = (xmlNsPtr)parent;
Aleksey Sanin6de6f972004-04-20 02:05:30 +0000269 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000270
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800271 /*
272 * If the input is an XPath node-set, then the node-set must explicitly
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000273 * contain every node to be rendered to the canonical form.
274 */
275 return(xmlXPathNodeSetContains(nodes, (xmlNodePtr)&ns));
276 }
277 }
278 return(1);
279}
280
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000281static xmlC14NVisibleNsStackPtr
282xmlC14NVisibleNsStackCreate(void) {
283 xmlC14NVisibleNsStackPtr ret;
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000284
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000285 ret = (xmlC14NVisibleNsStackPtr) xmlMalloc(sizeof(xmlC14NVisibleNsStack));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000286 if (ret == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000287 xmlC14NErrMemory("creating namespaces stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000288 return(NULL);
289 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000290 memset(ret, 0 , (size_t) sizeof(xmlC14NVisibleNsStack));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000291 return(ret);
292}
293
294static void
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000295xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr cur) {
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000296 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000297 xmlC14NErrParam("destroying namespaces stack");
298 return;
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000299 }
300 if(cur->nsTab != NULL) {
301 memset(cur->nsTab, 0, cur->nsMax * sizeof(xmlNsPtr));
302 xmlFree(cur->nsTab);
303 }
Aleksey Saninea4272a2002-08-02 23:50:03 +0000304 if(cur->nodeTab != NULL) {
305 memset(cur->nodeTab, 0, cur->nsMax * sizeof(xmlNodePtr));
306 xmlFree(cur->nodeTab);
307 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000308 memset(cur, 0, sizeof(xmlC14NVisibleNsStack));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000309 xmlFree(cur);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800310
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000311}
312
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800313static void
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000314xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlNodePtr node) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800315 if((cur == NULL) ||
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000316 ((cur->nsTab == NULL) && (cur->nodeTab != NULL)) ||
317 ((cur->nsTab != NULL) && (cur->nodeTab == NULL))) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000318 xmlC14NErrParam("adding namespace to stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000319 return;
320 }
321
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000322 if ((cur->nsTab == NULL) && (cur->nodeTab == NULL)) {
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000323 cur->nsTab = (xmlNsPtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr));
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000324 cur->nodeTab = (xmlNodePtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
325 if ((cur->nsTab == NULL) || (cur->nodeTab == NULL)) {
Daniel Veillardd96cce12003-10-10 12:30:37 +0000326 xmlC14NErrMemory("adding node to stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000327 return;
328 }
329 memset(cur->nsTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr));
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000330 memset(cur->nodeTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000331 cur->nsMax = XML_NAMESPACES_DEFAULT;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000332 } else if(cur->nsMax == cur->nsCurEnd) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800333 void *tmp;
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000334 int tmpSize;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800335
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000336 tmpSize = 2 * cur->nsMax;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000337 tmp = xmlRealloc(cur->nsTab, tmpSize * sizeof(xmlNsPtr));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000338 if (tmp == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +0000339 xmlC14NErrMemory("adding node to stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000340 return;
341 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000342 cur->nsTab = (xmlNsPtr*)tmp;
343
344 tmp = xmlRealloc(cur->nodeTab, tmpSize * sizeof(xmlNodePtr));
345 if (tmp == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +0000346 xmlC14NErrMemory("adding node to stack");
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000347 return;
348 }
349 cur->nodeTab = (xmlNodePtr*)tmp;
350
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000351 cur->nsMax = tmpSize;
352 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000353 cur->nsTab[cur->nsCurEnd] = ns;
354 cur->nodeTab[cur->nsCurEnd] = node;
355
356 ++cur->nsCurEnd;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000357}
358
359static void
360xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) {
361 if((cur == NULL) || (state == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000362 xmlC14NErrParam("saving namespaces stack");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000363 return;
364 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800365
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000366 state->nsCurEnd = cur->nsCurEnd;
367 state->nsPrevStart = cur->nsPrevStart;
368 state->nsPrevEnd = cur->nsPrevEnd;
369}
370
371static void
372xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) {
373 if((cur == NULL) || (state == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000374 xmlC14NErrParam("restoring namespaces stack");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000375 return;
376 }
377 cur->nsCurEnd = state->nsCurEnd;
378 cur->nsPrevStart = state->nsPrevStart;
379 cur->nsPrevEnd = state->nsPrevEnd;
380}
381
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800382static void
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000383xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur) {
384 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000385 xmlC14NErrParam("shifting namespaces stack");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000386 return;
387 }
388 cur->nsPrevStart = cur->nsPrevEnd;
389 cur->nsPrevEnd = cur->nsCurEnd;
390}
391
392static int
393xmlC14NStrEqual(const xmlChar *str1, const xmlChar *str2) {
394 if (str1 == str2) return(1);
395 if (str1 == NULL) return((*str2) == '\0');
396 if (str2 == NULL) return((*str1) == '\0');
397 do {
398 if (*str1++ != *str2) return(0);
399 } while (*str2++);
400 return(1);
401}
402
403/**
404 * xmlC14NVisibleNsStackFind:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800405 * @ctx: the C14N context
Daniel Veillard01c13b52002-12-10 15:19:08 +0000406 * @ns: the namespace to check
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000407 *
408 * Checks whether the given namespace was already rendered or not
409 *
410 * Returns 1 if we already wrote this namespace or 0 otherwise
411 */
412static int
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000413xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns)
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000414{
415 int i;
416 const xmlChar *prefix;
417 const xmlChar *href;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000418 int has_empty_ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800419
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000420 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000421 xmlC14NErrParam("searching namespaces stack (c14n)");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000422 return (0);
423 }
424
425 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800426 * if the default namespace xmlns="" is not defined yet then
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000427 * we do not want to print it out
428 */
429 prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
430 href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000431 has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL));
432
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000433 if (cur->nsTab != NULL) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000434 int start = (has_empty_ns) ? 0 : cur->nsPrevStart;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000435 for (i = cur->nsCurEnd - 1; i >= start; --i) {
436 xmlNsPtr ns1 = cur->nsTab[i];
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800437
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000438 if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
439 return(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL));
440 }
441 }
442 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000443 return(has_empty_ns);
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000444}
445
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800446static int
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000447xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlC14NCtxPtr ctx) {
448 int i;
449 const xmlChar *prefix;
450 const xmlChar *href;
451 int has_empty_ns;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800452
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000453 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000454 xmlC14NErrParam("searching namespaces stack (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000455 return (0);
456 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000457
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000458 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800459 * if the default namespace xmlns="" is not defined yet then
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000460 * we do not want to print it out
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000461 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000462 prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
463 href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href;
464 has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL));
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000465
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000466 if (cur->nsTab != NULL) {
467 int start = 0;
468 for (i = cur->nsCurEnd - 1; i >= start; --i) {
469 xmlNsPtr ns1 = cur->nsTab[i];
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800470
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000471 if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
472 if(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800473 return(xmlC14NIsVisible(ctx, ns1, cur->nodeTab[i]));
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000474 } else {
475 return(0);
476 }
477 }
478 }
479 }
480 return(has_empty_ns);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000481}
482
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000483
484
485
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000486/**
487 * xmlC14NIsXmlNs:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800488 * @ns: the namespace to check
489 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000490 * Checks whether the given namespace is a default "xml:" namespace
491 * with href="http://www.w3.org/XML/1998/namespace"
492 *
493 * Returns 1 if the node is default or 0 otherwise
494 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000495
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000496/* todo: make it a define? */
497static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000498xmlC14NIsXmlNs(xmlNsPtr ns)
499{
500 return ((ns != NULL) &&
501 (xmlStrEqual(ns->prefix, BAD_CAST "xml")) &&
Aleksey Sanin83868242009-07-09 10:26:22 +0200502 (xmlStrEqual(ns->href, XML_XML_NAMESPACE)));
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000503}
504
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000505
506/**
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000507 * xmlC14NNsCompare:
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000508 * @ns1: the pointer to first namespace
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800509 * @ns2: the pointer to second namespace
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000510 *
511 * Compares the namespaces by names (prefixes).
512 *
513 * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2.
514 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000515static int
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000516xmlC14NNsCompare(xmlNsPtr ns1, xmlNsPtr ns2)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000517{
518 if (ns1 == ns2)
519 return (0);
520 if (ns1 == NULL)
521 return (-1);
522 if (ns2 == NULL)
523 return (1);
524
525 return (xmlStrcmp(ns1->prefix, ns2->prefix));
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000526}
527
528
529/**
530 * xmlC14NPrintNamespaces:
531 * @ns: the pointer to namespace
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800532 * @ctx: the C14N context
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000533 *
534 * Prints the given namespace to the output buffer from C14N context.
535 *
536 * Returns 1 on success or 0 on fail.
537 */
538static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000539xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPtr ctx)
540{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000541
Daniel Veillard9ff88172002-03-11 09:15:32 +0000542 if ((ns == NULL) || (ctx == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000543 xmlC14NErrParam("writing namespaces");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000544 return 0;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000545 }
546
Daniel Veillard9ff88172002-03-11 09:15:32 +0000547 if (ns->prefix != NULL) {
548 xmlOutputBufferWriteString(ctx->buf, " xmlns:");
549 xmlOutputBufferWriteString(ctx->buf, (const char *) ns->prefix);
550 xmlOutputBufferWriteString(ctx->buf, "=\"");
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000551 } else {
Daniel Veillard9ff88172002-03-11 09:15:32 +0000552 xmlOutputBufferWriteString(ctx->buf, " xmlns=\"");
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000553 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000554 if(ns->href != NULL) {
555 xmlOutputBufferWriteString(ctx->buf, (const char *) ns->href);
556 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000557 xmlOutputBufferWriteString(ctx->buf, "\"");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000558 return (1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000559}
560
561/**
562 * xmlC14NProcessNamespacesAxis:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800563 * @ctx: the C14N context
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000564 * @node: the current node
565 *
566 * Prints out canonical namespace axis of the current node to the
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800567 * buffer from C14N context as follows
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000568 *
569 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
570 *
571 * Namespace Axis
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800572 * Consider a list L containing only namespace nodes in the
573 * axis and in the node-set in lexicographic order (ascending). To begin
574 * processing L, if the first node is not the default namespace node (a node
575 * with no namespace URI and no local name), then generate a space followed
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000576 * by xmlns="" if and only if the following conditions are met:
577 * - the element E that owns the axis is in the node-set
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800578 * - The nearest ancestor element of E in the node-set has a default
579 * namespace node in the node-set (default namespace nodes always
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000580 * have non-empty values in XPath)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800581 * The latter condition eliminates unnecessary occurrences of xmlns="" in
582 * the canonical form since an element only receives an xmlns="" if its
583 * default namespace is empty and if it has an immediate parent in the
584 * canonical form that has a non-empty default namespace. To finish
585 * processing L, simply process every namespace node in L, except omit
586 * namespace node with local name xml, which defines the xml prefix,
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000587 * if its string value is http://www.w3.org/XML/1998/namespace.
588 *
589 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800590 * Canonical XML applied to a document subset requires the search of the
591 * ancestor nodes of each orphan element node for attributes in the xml
592 * namespace, such as xml:lang and xml:space. These are copied into the
593 * element node except if a declaration of the same attribute is already
594 * in the attribute axis of the element (whether or not it is included in
595 * the document subset). This search and copying are omitted from the
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000596 * Exclusive XML Canonicalization method.
597 *
598 * Returns 0 on success or -1 on fail.
599 */
600static int
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000601xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000602{
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000603 xmlNodePtr n;
604 xmlNsPtr ns, tmp;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000605 xmlListPtr list;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000606 int already_rendered;
607 int has_empty_ns = 0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800608
Daniel Veillard9ff88172002-03-11 09:15:32 +0000609 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000610 xmlC14NErrParam("processing namespaces axis (c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000611 return (-1);
612 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000613
614 /*
615 * Create a sorted list to store element namespaces
616 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000617 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000618 if (list == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000619 xmlC14NErrInternal("creating namespaces list (c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000620 return (-1);
621 }
622
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000623 /* check all namespaces */
624 for(n = cur; n != NULL; n = n->parent) {
625 for(ns = n->nsDef; ns != NULL; ns = ns->next) {
626 tmp = xmlSearchNs(cur->doc, cur, ns->prefix);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800627
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000628 if((tmp == ns) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
629 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
630 if(visible) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800631 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000632 }
633 if(!already_rendered) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800634 xmlListInsert(list, ns);
Aleksey Saninc57f9c12002-05-31 19:14:57 +0000635 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800636 if(xmlStrlen(ns->prefix) == 0) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000637 has_empty_ns = 1;
638 }
Aleksey Saninc57f9c12002-05-31 19:14:57 +0000639 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000640 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000641 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800642
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000643 /**
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800644 * if the first node is not the default namespace node (a node with no
645 * namespace URI and no local name), then generate a space followed by
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000646 * xmlns="" if and only if the following conditions are met:
647 * - the element E that owns the axis is in the node-set
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800648 * - the nearest ancestor element of E in the node-set has a default
649 * namespace node in the node-set (default namespace nodes always
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000650 * have non-empty values in XPath)
651 */
652 if(visible && !has_empty_ns) {
653 static xmlNs ns_default;
654
655 memset(&ns_default, 0, sizeof(ns_default));
656 if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800657 xmlC14NPrintNamespaces(&ns_default, ctx);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000658 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000659 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800660
661
662 /*
663 * print out all elements from list
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000664 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000665 xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000666
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800667 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000668 * Cleanup
669 */
670 xmlListDelete(list);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000671 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000672}
673
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000674
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000675/**
676 * xmlExcC14NProcessNamespacesAxis:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800677 * @ctx: the C14N context
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000678 * @node: the current node
679 *
680 * Prints out exclusive canonical namespace axis of the current node to the
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800681 * buffer from C14N context as follows
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000682 *
683 * Exclusive XML Canonicalization
684 * http://www.w3.org/TR/xml-exc-c14n
685 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800686 * If the element node is in the XPath subset then output the node in
687 * accordance with Canonical XML except for namespace nodes which are
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000688 * rendered as follows:
689 *
690 * 1. Render each namespace node iff:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800691 * * it is visibly utilized by the immediate parent element or one of
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000692 * its attributes, or is present in InclusiveNamespaces PrefixList, and
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800693 * * its prefix and value do not appear in ns_rendered. ns_rendered is
694 * obtained by popping the state stack in order to obtain a list of
695 * prefixes and their values which have already been rendered by
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000696 * an output ancestor of the namespace node's parent element.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800697 * 2. Append the rendered namespace node to the list ns_rendered of namespace
698 * nodes rendered by output ancestors. Push ns_rendered on state stack and
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000699 * recurse.
700 * 3. After the recursion returns, pop thestate stack.
701 *
702 *
703 * Returns 0 on success or -1 on fail.
704 */
705static int
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000706xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000707{
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000708 xmlNsPtr ns;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000709 xmlListPtr list;
710 xmlAttrPtr attr;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000711 int already_rendered;
712 int has_empty_ns = 0;
713 int has_visibly_utilized_empty_ns = 0;
714 int has_empty_ns_in_inclusive_list = 0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800715
Daniel Veillard9ff88172002-03-11 09:15:32 +0000716 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000717 xmlC14NErrParam("processing namespaces axis (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000718 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000719 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000720
Aleksey Sanin83868242009-07-09 10:26:22 +0200721 if(!xmlC14NIsExclusive(ctx)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000722 xmlC14NErrParam("processing namespaces axis (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000723 return (-1);
724
725 }
726
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000727 /*
728 * Create a sorted list to store element namespaces
729 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000730 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000731 if (list == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000732 xmlC14NErrInternal("creating namespaces list (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000733 return (-1);
734 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000735
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800736 /*
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000737 * process inclusive namespaces:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800738 * All namespace nodes appearing on inclusive ns list are
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000739 * handled as provided in Canonical XML
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000740 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000741 if(ctx->inclusive_ns_prefixes != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800742 xmlChar *prefix;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000743 int i;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800744
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000745 for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) {
746 prefix = ctx->inclusive_ns_prefixes[i];
747 /*
748 * Special values for namespace with empty prefix
749 */
750 if (xmlStrEqual(prefix, BAD_CAST "#default")
751 || xmlStrEqual(prefix, BAD_CAST "")) {
752 prefix = NULL;
753 has_empty_ns_in_inclusive_list = 1;
754 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800755
756 ns = xmlSearchNs(cur->doc, cur, prefix);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000757 if((ns != NULL) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
758 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
759 if(visible) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800760 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000761 }
762 if(!already_rendered) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800763 xmlListInsert(list, ns);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000764 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800765 if(xmlStrlen(ns->prefix) == 0) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000766 has_empty_ns = 1;
767 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000768 }
Aleksey Saninc57f9c12002-05-31 19:14:57 +0000769 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000770 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800771
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000772 /* add node namespace */
773 if(cur->ns != NULL) {
774 ns = cur->ns;
775 } else {
776 ns = xmlSearchNs(cur->doc, cur, NULL);
777 has_visibly_utilized_empty_ns = 1;
778 }
779 if((ns != NULL) && !xmlC14NIsXmlNs(ns)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800780 if(visible && xmlC14NIsVisible(ctx, ns, cur)) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000781 if(!xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, ns, ctx)) {
782 xmlListInsert(list, ns);
783 }
784 }
785 if(visible) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800786 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000787 }
788 if(xmlStrlen(ns->prefix) == 0) {
789 has_empty_ns = 1;
790 }
791 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800792
793
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000794 /* add attributes */
795 for(attr = cur->properties; attr != NULL; attr = attr->next) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800796 /*
Daniel Veillard2d347fa2002-03-17 10:34:11 +0000797 * we need to check that attribute is visible and has non
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800798 * default namespace (XML Namespaces: "default namespaces
799 * do not apply directly to attributes")
Daniel Veillard9ff88172002-03-11 09:15:32 +0000800 */
Aleksey Sanin2650df12005-06-06 17:16:50 +0000801 if((attr->ns != NULL) && !xmlC14NIsXmlNs(attr->ns) && xmlC14NIsVisible(ctx, attr, cur)) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000802 already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, attr->ns, ctx);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800803 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, attr->ns, cur);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000804 if(!already_rendered && visible) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800805 xmlListInsert(list, attr->ns);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000806 }
807 if(xmlStrlen(attr->ns->prefix) == 0) {
808 has_empty_ns = 1;
809 }
Aleksey Saninb2eabc02005-10-28 03:15:18 +0000810 } else if((attr->ns != NULL) && (xmlStrlen(attr->ns->prefix) == 0) && (xmlStrlen(attr->ns->href) == 0)) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000811 has_visibly_utilized_empty_ns = 1;
812 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000813 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000814
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000815 /*
816 * Process xmlns=""
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000817 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800818 if(visible && has_visibly_utilized_empty_ns &&
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000819 !has_empty_ns && !has_empty_ns_in_inclusive_list) {
820 static xmlNs ns_default;
Daniel Veillard9ff88172002-03-11 09:15:32 +0000821
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000822 memset(&ns_default, 0, sizeof(ns_default));
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800823
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000824 already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default, ctx);
825 if(!already_rendered) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800826 xmlC14NPrintNamespaces(&ns_default, ctx);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000827 }
828 } else if(visible && !has_empty_ns && has_empty_ns_in_inclusive_list) {
829 static xmlNs ns_default;
830
831 memset(&ns_default, 0, sizeof(ns_default));
832 if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800833 xmlC14NPrintNamespaces(&ns_default, ctx);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000834 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000835 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000836
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000837
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800838
839 /*
840 * print out all elements from list
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000841 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000842 xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000843
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800844 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000845 * Cleanup
846 */
847 xmlListDelete(list);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000848 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000849}
850
851
852/**
Aleksey Sanin83868242009-07-09 10:26:22 +0200853 * xmlC14NIsXmlAttr:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800854 * @attr: the attr to check
855 *
Aleksey Sanin83868242009-07-09 10:26:22 +0200856 * Checks whether the given attribute is a default "xml:" namespace
857 * with href="http://www.w3.org/XML/1998/namespace"
858 *
859 * Returns 1 if the node is default or 0 otherwise
860 */
861
862/* todo: make it a define? */
863static int
864xmlC14NIsXmlAttr(xmlAttrPtr attr)
865{
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800866 return ((attr->ns != NULL) &&
Aleksey Sanin83868242009-07-09 10:26:22 +0200867 (xmlC14NIsXmlNs(attr->ns) != 0));
868}
869
870
871/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000872 * xmlC14NAttrsCompare:
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000873 * @attr1: the pointer tls o first attr
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800874 * @attr2: the pointer to second attr
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000875 *
876 * Prints the given attribute to the output buffer from C14N context.
877 *
878 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2.
879 */
880static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000881xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2)
882{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000883 int ret = 0;
884
885 /*
886 * Simple cases
887 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000888 if (attr1 == attr2)
889 return (0);
890 if (attr1 == NULL)
891 return (-1);
892 if (attr2 == NULL)
893 return (1);
894 if (attr1->ns == attr2->ns) {
895 return (xmlStrcmp(attr1->name, attr2->name));
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000896 }
897
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800898 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000899 * Attributes in the default namespace are first
900 * because the default namespace is not applied to
901 * unqualified attributes
902 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000903 if (attr1->ns == NULL)
904 return (-1);
905 if (attr2->ns == NULL)
906 return (1);
907 if (attr1->ns->prefix == NULL)
908 return (-1);
909 if (attr2->ns->prefix == NULL)
910 return (1);
911
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000912 ret = xmlStrcmp(attr1->ns->href, attr2->ns->href);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000913 if (ret == 0) {
914 ret = xmlStrcmp(attr1->name, attr2->name);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000915 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000916 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000917}
918
919
920/**
921 * xmlC14NPrintAttrs:
922 * @attr: the pointer to attr
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800923 * @ctx: the C14N context
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000924 *
925 * Prints out canonical attribute urrent node to the
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800926 * buffer from C14N context as follows
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000927 *
928 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
929 *
930 * Returns 1 on success or 0 on fail.
931 */
932static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000933xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr ctx)
934{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000935 xmlChar *value;
936 xmlChar *buffer;
937
Daniel Veillard9ff88172002-03-11 09:15:32 +0000938 if ((attr == NULL) || (ctx == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000939 xmlC14NErrParam("writing attributes");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000940 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000941 }
942
943 xmlOutputBufferWriteString(ctx->buf, " ");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000944 if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) {
945 xmlOutputBufferWriteString(ctx->buf,
946 (const char *) attr->ns->prefix);
947 xmlOutputBufferWriteString(ctx->buf, ":");
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000948 }
949 xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name);
950 xmlOutputBufferWriteString(ctx->buf, "=\"");
951
Aleksey Sanin83868242009-07-09 10:26:22 +0200952 value = xmlNodeListGetString(ctx->doc, attr->children, 1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000953 /* todo: should we log an error if value==NULL ? */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000954 if (value != NULL) {
955 buffer = xmlC11NNormalizeAttr(value);
956 xmlFree(value);
957 if (buffer != NULL) {
958 xmlOutputBufferWriteString(ctx->buf, (const char *) buffer);
959 xmlFree(buffer);
960 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000961 xmlC14NErrInternal("normalizing attributes axis");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000962 return (0);
963 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000964 }
965 xmlOutputBufferWriteString(ctx->buf, "\"");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000966 return (1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000967}
968
969/**
Aleksey Sanin83868242009-07-09 10:26:22 +0200970 * xmlC14NFindHiddenParentAttr:
971 *
972 * Finds an attribute in a hidden parent node.
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800973 *
Aleksey Sanin83868242009-07-09 10:26:22 +0200974 * Returns a pointer to the attribute node (if found) or NULL otherwise.
975 */
976static xmlAttrPtr
977xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx, xmlNodePtr cur, const xmlChar * name, const xmlChar * ns)
978{
979 xmlAttrPtr res;
980 while((cur != NULL) && (!xmlC14NIsVisible(ctx, cur, cur->parent))) {
981 res = xmlHasNsProp(cur, name, ns);
982 if(res != NULL) {
983 return res;
984 }
985
986 cur = cur->parent;
987 }
988
989 return NULL;
990}
991
992/**
993 * xmlC14NFixupBaseAttr:
994 *
995 * Fixes up the xml:base attribute
996 *
997 * Returns the newly created attribute or NULL
998 */
999static xmlAttrPtr
1000xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml_base_attr)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001001{
Aleksey Sanin83868242009-07-09 10:26:22 +02001002 xmlChar * res = NULL;
1003 xmlNodePtr cur;
1004 xmlAttrPtr attr;
1005 xmlChar * tmp_str;
1006 xmlChar * tmp_str2;
1007 int tmp_str_len;
1008
1009 if ((ctx == NULL) || (xml_base_attr == NULL) || (xml_base_attr->parent == NULL)) {
1010 xmlC14NErrParam("processing xml:base attribute");
1011 return (NULL);
1012 }
1013
1014 /* start from current value */
1015 res = xmlNodeListGetString(ctx->doc, xml_base_attr->children, 1);
1016 if(res == NULL) {
1017 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
1018 return (NULL);
1019 }
1020
1021 /* go up the stack until we find a node that we rendered already */
1022 cur = xml_base_attr->parent->parent;
1023 while((cur != NULL) && (!xmlC14NIsVisible(ctx, cur, cur->parent))) {
1024 attr = xmlHasNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
1025 if(attr != NULL) {
1026 /* get attr value */
1027 tmp_str = xmlNodeListGetString(ctx->doc, attr->children, 1);
1028 if(tmp_str == NULL) {
1029 xmlFree(res);
1030
1031 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
1032 return (NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001033 }
Aleksey Sanin83868242009-07-09 10:26:22 +02001034
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001035 /* we need to add '/' if our current base uri ends with '..' or '.'
Aleksey Sanin83868242009-07-09 10:26:22 +02001036 to ensure that we are forced to go "up" all the time */
1037 tmp_str_len = xmlStrlen(tmp_str);
1038 if(tmp_str_len > 1 && tmp_str[tmp_str_len - 2] == '.') {
1039 tmp_str2 = xmlStrcat(tmp_str, BAD_CAST "/");
1040 if(tmp_str2 == NULL) {
1041 xmlFree(tmp_str);
1042 xmlFree(res);
1043
1044 xmlC14NErrInternal("processing xml:base attribute - can't modify uri");
1045 return (NULL);
1046 }
1047
1048 tmp_str = tmp_str2;
1049 }
1050
1051 /* build uri */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001052 tmp_str2 = xmlBuildURI(res, tmp_str);
Aleksey Sanin83868242009-07-09 10:26:22 +02001053 if(tmp_str2 == NULL) {
1054 xmlFree(tmp_str);
1055 xmlFree(res);
1056
1057 xmlC14NErrInternal("processing xml:base attribute - can't construct uri");
1058 return (NULL);
1059 }
1060
1061 /* cleanup and set the new res */
1062 xmlFree(tmp_str);
1063 xmlFree(res);
1064 res = tmp_str2;
1065 }
1066
1067 /* next */
1068 cur = cur->parent;
1069 }
1070
1071 /* check if result uri is empty or not */
1072 if((res == NULL) || xmlStrEqual(res, BAD_CAST "")) {
1073 xmlFree(res);
1074 return (NULL);
1075 }
1076
1077 /* create and return the new attribute node */
1078 attr = xmlNewNsProp(NULL, xml_base_attr->ns, BAD_CAST "base", res);
1079 if(attr == NULL) {
1080 xmlFree(res);
1081
1082 xmlC14NErrInternal("processing xml:base attribute - can't construct attribute");
1083 return (NULL);
1084 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001085
Aleksey Sanin83868242009-07-09 10:26:22 +02001086 /* done */
1087 xmlFree(res);
1088 return (attr);
1089}
1090
1091/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001092 * xmlC14NProcessAttrsAxis:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001093 * @ctx: the C14N context
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001094 * @cur: the current node
Aleksey Sanin3ea201c2005-06-07 16:53:57 +00001095 * @parent_visible: the visibility of parent node
Aleksey Sanin83868242009-07-09 10:26:22 +02001096 * @all_parents_visible: the visibility of all parent nodes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001097 *
1098 * Prints out canonical attribute axis of the current node to the
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001099 * buffer from C14N context as follows
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001100 *
1101 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1102 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001103 * Attribute Axis
1104 * In lexicographic order (ascending), process each node that
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001105 * is in the element's attribute axis and in the node-set.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001106 *
1107 * The processing of an element node E MUST be modified slightly
1108 * when an XPath node-set is given as input and the element's
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001109 * parent is omitted from the node-set.
1110 *
1111 *
1112 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
1113 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001114 * Canonical XML applied to a document subset requires the search of the
1115 * ancestor nodes of each orphan element node for attributes in the xml
1116 * namespace, such as xml:lang and xml:space. These are copied into the
1117 * element node except if a declaration of the same attribute is already
1118 * in the attribute axis of the element (whether or not it is included in
1119 * the document subset). This search and copying are omitted from the
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001120 * Exclusive XML Canonicalization method.
1121 *
1122 * Returns 0 on success or -1 on fail.
1123 */
1124static int
Aleksey Sanin3ea201c2005-06-07 16:53:57 +00001125xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible)
Daniel Veillard9ff88172002-03-11 09:15:32 +00001126{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001127 xmlAttrPtr attr;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001128 xmlListPtr list;
Aleksey Sanin83868242009-07-09 10:26:22 +02001129 xmlAttrPtr attrs_to_delete = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001130
Aleksey Sanin83868242009-07-09 10:26:22 +02001131 /* special processing for 1.1 spec */
1132 xmlAttrPtr xml_base_attr = NULL;
1133 xmlAttrPtr xml_lang_attr = NULL;
1134 xmlAttrPtr xml_space_attr = NULL;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001135
1136 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001137 xmlC14NErrParam("processing attributes axis");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001138 return (-1);
1139 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001140
1141 /*
1142 * Create a sorted list to store element attributes
1143 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001144 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare);
1145 if (list == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001146 xmlC14NErrInternal("creating attributes list");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001147 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001148 }
1149
Aleksey Sanin83868242009-07-09 10:26:22 +02001150 switch(ctx->mode) {
1151 case XML_C14N_1_0:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001152 /* The processing of an element node E MUST be modified slightly when an XPath node-set is
1153 * given as input and the element's parent is omitted from the node-set. The method for processing
1154 * the attribute axis of an element E in the node-set is enhanced. All element nodes along E's
1155 * ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such
1156 * as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,
1157 * remove any that are in E's attribute axis (whether or not they are in the node-set). Then,
1158 * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
1159 * the node-set. The result of visiting the attribute axis is computed by processing the attribute
1160 * nodes in this merged attribute list.
Daniel Veillard9ff88172002-03-11 09:15:32 +00001161 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001162
1163 /*
1164 * Add all visible attributes from current node.
Aleksey Sanin83868242009-07-09 10:26:22 +02001165 */
1166 attr = cur->properties;
1167 while (attr != NULL) {
1168 /* check that attribute is visible */
1169 if (xmlC14NIsVisible(ctx, attr, cur)) {
1170 xmlListInsert(list, attr);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001171 }
Aleksey Sanin83868242009-07-09 10:26:22 +02001172 attr = attr->next;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001173 }
Aleksey Sanin83868242009-07-09 10:26:22 +02001174
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001175 /*
Aleksey Sanin83868242009-07-09 10:26:22 +02001176 * Handle xml attributes
1177 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001178 if (parent_visible && (cur->parent != NULL) &&
1179 (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent)))
Aleksey Sanin83868242009-07-09 10:26:22 +02001180 {
1181 xmlNodePtr tmp;
1182
1183 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001184 * If XPath node-set is not specified then the parent is always
Aleksey Sanin83868242009-07-09 10:26:22 +02001185 * visible!
1186 */
1187 tmp = cur->parent;
1188 while (tmp != NULL) {
1189 attr = tmp->properties;
1190 while (attr != NULL) {
1191 if (xmlC14NIsXmlAttr(attr) != 0) {
1192 if (xmlListSearch(list, attr) == NULL) {
1193 xmlListInsert(list, attr);
1194 }
1195 }
1196 attr = attr->next;
1197 }
1198 tmp = tmp->parent;
1199 }
1200 }
1201
1202 /* done */
1203 break;
1204 case XML_C14N_EXCLUSIVE_1_0:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001205 /* attributes in the XML namespace, such as xml:lang and xml:space
1206 * are not imported into orphan nodes of the document subset
Aleksey Sanin83868242009-07-09 10:26:22 +02001207 */
1208
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001209 /*
1210 * Add all visible attributes from current node.
Aleksey Sanin83868242009-07-09 10:26:22 +02001211 */
1212 attr = cur->properties;
1213 while (attr != NULL) {
1214 /* check that attribute is visible */
1215 if (xmlC14NIsVisible(ctx, attr, cur)) {
1216 xmlListInsert(list, attr);
1217 }
1218 attr = attr->next;
1219 }
1220
1221 /* do nothing special for xml attributes */
1222 break;
1223 case XML_C14N_1_1:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001224 /* The processing of an element node E MUST be modified slightly when an XPath node-set is
1225 * given as input and some of the element's ancestors are omitted from the node-set.
Aleksey Sanin83868242009-07-09 10:26:22 +02001226 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001227 * Simple inheritable attributes are attributes that have a value that requires at most a simple
1228 * redeclaration. This redeclaration is done by supplying a new value in the child axis. The
1229 * redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done
1230 * by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes
Aleksey Sanin83868242009-07-09 10:26:22 +02001231 * are xml:lang and xml:space.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001232 *
1233 * The method for processing the attribute axis of an element E in the node-set is hence enhanced.
1234 * All element nodes along E's ancestor axis are examined for the nearest occurrences of simple
1235 * inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they
1236 * are in the node-set). From this list of attributes, any simple inheritable attributes that are
1237 * already in E's attribute axis (whether or not they are in the node-set) are removed. Then,
1238 * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
1239 * the node-set. The result of visiting the attribute axis is computed by processing the attribute
Aleksey Sanin83868242009-07-09 10:26:22 +02001240 * nodes in this merged attribute list.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001241 *
1242 * The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is
Aleksey Sanin83868242009-07-09 10:26:22 +02001243 * performed.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001244 *
1245 * The xml:base attribute is not a simple inheritable attribute and requires special processing beyond
Aleksey Sanin83868242009-07-09 10:26:22 +02001246 * a simple redeclaration.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001247 *
1248 * Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed
Aleksey Sanin83868242009-07-09 10:26:22 +02001249 * as ordinary attributes.
1250 */
1251
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001252 /*
1253 * Add all visible attributes from current node.
Aleksey Sanin83868242009-07-09 10:26:22 +02001254 */
1255 attr = cur->properties;
1256 while (attr != NULL) {
1257 /* special processing for XML attribute kiks in only when we have invisible parents */
1258 if ((!parent_visible) || (xmlC14NIsXmlAttr(attr) == 0)) {
1259 /* check that attribute is visible */
1260 if (xmlC14NIsVisible(ctx, attr, cur)) {
1261 xmlListInsert(list, attr);
1262 }
1263 } else {
1264 int matched = 0;
1265
1266 /* check for simple inheritance attributes */
1267 if((!matched) && (xml_lang_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "lang")) {
1268 xml_lang_attr = attr;
1269 matched = 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001270 }
Aleksey Sanin83868242009-07-09 10:26:22 +02001271 if((!matched) && (xml_space_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "space")) {
1272 xml_space_attr = attr;
1273 matched = 1;
1274 }
1275
1276 /* check for base attr */
1277 if((!matched) && (xml_base_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "base")) {
1278 xml_base_attr = attr;
1279 matched = 1;
1280 }
1281
1282 /* otherwise, it is a normal attribute, so just check if it is visible */
1283 if((!matched) && xmlC14NIsVisible(ctx, attr, cur)) {
1284 xmlListInsert(list, attr);
1285 }
1286 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001287
Aleksey Sanin83868242009-07-09 10:26:22 +02001288 /* move to the next one */
1289 attr = attr->next;
1290 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001291
Aleksey Sanin83868242009-07-09 10:26:22 +02001292 /* special processing for XML attribute kiks in only when we have invisible parents */
1293 if ((parent_visible)) {
1294
1295 /* simple inheritance attributes - copy */
1296 if(xml_lang_attr == NULL) {
1297 xml_lang_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BAD_CAST "lang", XML_XML_NAMESPACE);
1298 }
1299 if(xml_lang_attr != NULL) {
1300 xmlListInsert(list, xml_lang_attr);
1301 }
1302 if(xml_space_attr == NULL) {
1303 xml_space_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BAD_CAST "space", XML_XML_NAMESPACE);
1304 }
1305 if(xml_space_attr != NULL) {
1306 xmlListInsert(list, xml_space_attr);
1307 }
1308
1309 /* base uri attribute - fix up */
1310 if(xml_base_attr == NULL) {
1311 /* if we don't have base uri attribute, check if we have a "hidden" one above */
1312 xml_base_attr = xmlC14NFindHiddenParentAttr(ctx, cur->parent, BAD_CAST "base", XML_XML_NAMESPACE);
1313 }
1314 if(xml_base_attr != NULL) {
1315 xml_base_attr = xmlC14NFixupBaseAttr(ctx, xml_base_attr);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001316 if(xml_base_attr != NULL) {
Aleksey Sanin83868242009-07-09 10:26:22 +02001317 xmlListInsert(list, xml_base_attr);
1318
1319 /* note that we MUST delete returned attr node ourselves! */
1320 xml_base_attr->next = attrs_to_delete;
1321 attrs_to_delete = xml_base_attr;
1322 }
1323 }
1324 }
1325
1326 /* done */
1327 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001328 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001329
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001330 /*
1331 * print out all elements from list
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001332 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001333 xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001334
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001335 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001336 * Cleanup
1337 */
Aleksey Sanin83868242009-07-09 10:26:22 +02001338 xmlFreePropList(attrs_to_delete);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001339 xmlListDelete(list);
1340 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001341}
1342
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001343/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001344 * xmlC14NCheckForRelativeNamespaces:
1345 * @ctx: the C14N context
1346 * @cur: the current element node
1347 *
1348 * Checks that current element node has no relative namespaces defined
1349 *
1350 * Returns 0 if the node has no relative namespaces or -1 otherwise.
1351 */
1352static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001353xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, xmlNodePtr cur)
1354{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001355 xmlNsPtr ns;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001356
Daniel Veillard9ff88172002-03-11 09:15:32 +00001357 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001358 xmlC14NErrParam("checking for relative namespaces");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001359 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001360 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001361
1362 ns = cur->nsDef;
1363 while (ns != NULL) {
1364 if (xmlStrlen(ns->href) > 0) {
1365 xmlURIPtr uri;
1366
1367 uri = xmlParseURI((const char *) ns->href);
1368 if (uri == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001369 xmlC14NErrInternal("parsing namespace uri");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001370 return (-1);
1371 }
1372 if (xmlStrlen((const xmlChar *) uri->scheme) == 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001373 xmlC14NErrRelativeNamespace(uri->scheme);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001374 xmlFreeURI(uri);
1375 return (-1);
1376 }
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001377 if ((xmlStrcasecmp((const xmlChar *) uri->scheme, BAD_CAST "urn") != 0)
1378 && (xmlStrcasecmp((const xmlChar *) uri->scheme, BAD_CAST "dav") !=0)
Daniel Veillard9ff88172002-03-11 09:15:32 +00001379 && (xmlStrlen((const xmlChar *) uri->server) == 0)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001380 xmlC14NErrRelativeNamespace(uri->scheme);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001381 xmlFreeURI(uri);
1382 return (-1);
1383 }
1384 xmlFreeURI(uri);
1385 }
1386 ns = ns->next;
1387 }
1388 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001389}
1390
1391/**
1392 * xmlC14NProcessElementNode:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001393 * @ctx: the pointer to C14N context object
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001394 * @cur: the node to process
Aleksey Sanin83868242009-07-09 10:26:22 +02001395 * @visible: this node is visible
1396 * @all_parents_visible: whether all the parents of this node are visible
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001397 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001398 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1399 *
1400 * Element Nodes
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001401 * If the element is not in the node-set, then the result is obtained
1402 * by processing the namespace axis, then the attribute axis, then
1403 * processing the child nodes of the element that are in the node-set
1404 * (in document order). If the element is in the node-set, then the result
1405 * is an open angle bracket (<), the element QName, the result of
1406 * processing the namespace axis, the result of processing the attribute
1407 * axis, a close angle bracket (>), the result of processing the child
1408 * nodes of the element that are in the node-set (in document order), an
1409 * open angle bracket, a forward slash (/), the element QName, and a close
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001410 * angle bracket.
1411 *
1412 * Returns non-negative value on success or negative value on fail
1413 */
1414static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001415xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
1416{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001417 int ret;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001418 xmlC14NVisibleNsStack state;
Daniel Veillard6f293b12002-03-15 09:42:33 +00001419 int parent_is_doc = 0;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001420
Daniel Veillard9ff88172002-03-11 09:15:32 +00001421 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001422 xmlC14NErrParam("processing element node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001423 return (-1);
1424 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001425
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001426 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001427 * Check relative relative namespaces:
1428 * implementations of XML canonicalization MUST report an operation
1429 * failure on documents containing relative namespace URIs.
1430 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001431 if (xmlC14NCheckForRelativeNamespaces(ctx, cur) < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001432 xmlC14NErrInternal("checking for relative namespaces");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001433 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001434 }
1435
1436
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001437 /*
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001438 * Save ns_rendered stack position
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001439 */
Daniel Veillardaac7c682006-03-10 13:40:16 +00001440 memset(&state, 0, sizeof(state));
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001441 xmlC14NVisibleNsStackSave(ctx->ns_rendered, &state);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001442
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001443 if (visible) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001444 if (ctx->parent_is_doc) {
Daniel Veillard6f293b12002-03-15 09:42:33 +00001445 /* save this flag into the stack */
1446 parent_is_doc = ctx->parent_is_doc;
1447 ctx->parent_is_doc = 0;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001448 ctx->pos = XMLC14N_INSIDE_DOCUMENT_ELEMENT;
1449 }
1450 xmlOutputBufferWriteString(ctx->buf, "<");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001451
Daniel Veillard9ff88172002-03-11 09:15:32 +00001452 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) {
1453 xmlOutputBufferWriteString(ctx->buf,
1454 (const char *) cur->ns->prefix);
1455 xmlOutputBufferWriteString(ctx->buf, ":");
1456 }
1457 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001458 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001459
Aleksey Sanin83868242009-07-09 10:26:22 +02001460 if (!xmlC14NIsExclusive(ctx)) {
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001461 ret = xmlC14NProcessNamespacesAxis(ctx, cur, visible);
1462 } else {
1463 ret = xmlExcC14NProcessNamespacesAxis(ctx, cur, visible);
1464 }
1465 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001466 xmlC14NErrInternal("processing namespaces axis");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001467 return (-1);
1468 }
1469 /* todo: shouldn't this go to "visible only"? */
1470 if(visible) {
1471 xmlC14NVisibleNsStackShift(ctx->ns_rendered);
1472 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001473
Aleksey Sanin3ea201c2005-06-07 16:53:57 +00001474 ret = xmlC14NProcessAttrsAxis(ctx, cur, visible);
1475 if (ret < 0) {
1476 xmlC14NErrInternal("processing attributes axis");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001477 return (-1);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001478 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001479
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001480 if (visible) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001481 xmlOutputBufferWriteString(ctx->buf, ">");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001482 }
1483 if (cur->children != NULL) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001484 ret = xmlC14NProcessNodeList(ctx, cur->children);
1485 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001486 xmlC14NErrInternal("processing childrens list");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001487 return (-1);
1488 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001489 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001490 if (visible) {
1491 xmlOutputBufferWriteString(ctx->buf, "</");
1492 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) {
1493 xmlOutputBufferWriteString(ctx->buf,
1494 (const char *) cur->ns->prefix);
1495 xmlOutputBufferWriteString(ctx->buf, ":");
1496 }
1497 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name);
1498 xmlOutputBufferWriteString(ctx->buf, ">");
Daniel Veillard6f293b12002-03-15 09:42:33 +00001499 if (parent_is_doc) {
1500 /* restore this flag from the stack for next node */
1501 ctx->parent_is_doc = parent_is_doc;
1502 ctx->pos = XMLC14N_AFTER_DOCUMENT_ELEMENT;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001503 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001504 }
1505
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001506 /*
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001507 * Restore ns_rendered stack position
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001508 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001509 xmlC14NVisibleNsStackRestore(ctx->ns_rendered, &state);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001510 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001511}
1512
1513/**
1514 * xmlC14NProcessNode:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001515 * @ctx: the pointer to C14N context object
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001516 * @cur: the node to process
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001517 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001518 * Processes the given node
1519 *
1520 * Returns non-negative value on success or negative value on fail
1521 */
1522static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001523xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
1524{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001525 int ret = 0;
1526 int visible;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001527
1528 if ((ctx == NULL) || (cur == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001529 xmlC14NErrParam("processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001530 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001531 }
1532
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001533 visible = xmlC14NIsVisible(ctx, cur, cur->parent);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001534 switch (cur->type) {
1535 case XML_ELEMENT_NODE:
1536 ret = xmlC14NProcessElementNode(ctx, cur, visible);
1537 break;
1538 case XML_CDATA_SECTION_NODE:
1539 case XML_TEXT_NODE:
1540 /*
1541 * Text Nodes
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001542 * the string value, except all ampersands are replaced
1543 * by &amp;, all open angle brackets (<) are replaced by &lt;, all closing
1544 * angle brackets (>) are replaced by &gt;, and all #xD characters are
Daniel Veillard9ff88172002-03-11 09:15:32 +00001545 * replaced by &#xD;.
1546 */
1547 /* cdata sections are processed as text nodes */
1548 /* todo: verify that cdata sections are included in XPath nodes set */
1549 if ((visible) && (cur->content != NULL)) {
1550 xmlChar *buffer;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001551
Daniel Veillard9ff88172002-03-11 09:15:32 +00001552 buffer = xmlC11NNormalizeText(cur->content);
1553 if (buffer != NULL) {
1554 xmlOutputBufferWriteString(ctx->buf,
1555 (const char *) buffer);
1556 xmlFree(buffer);
1557 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001558 xmlC14NErrInternal("normalizing text node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001559 return (-1);
1560 }
1561 }
1562 break;
1563 case XML_PI_NODE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001564 /*
1565 * Processing Instruction (PI) Nodes-
1566 * The opening PI symbol (<?), the PI target name of the node,
1567 * a leading space and the string value if it is not empty, and
1568 * the closing PI symbol (?>). If the string value is empty,
1569 * then the leading space is not added. Also, a trailing #xA is
1570 * rendered after the closing PI symbol for PI children of the
1571 * root node with a lesser document order than the document
1572 * element, and a leading #xA is rendered before the opening PI
1573 * symbol of PI children of the root node with a greater document
Daniel Veillard9ff88172002-03-11 09:15:32 +00001574 * order than the document element.
1575 */
1576 if (visible) {
1577 if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) {
1578 xmlOutputBufferWriteString(ctx->buf, "\x0A<?");
1579 } else {
1580 xmlOutputBufferWriteString(ctx->buf, "<?");
1581 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001582
Daniel Veillard9ff88172002-03-11 09:15:32 +00001583 xmlOutputBufferWriteString(ctx->buf,
1584 (const char *) cur->name);
1585 if ((cur->content != NULL) && (*(cur->content) != '\0')) {
1586 xmlChar *buffer;
1587
1588 xmlOutputBufferWriteString(ctx->buf, " ");
1589
1590 /* todo: do we need to normalize pi? */
1591 buffer = xmlC11NNormalizePI(cur->content);
1592 if (buffer != NULL) {
1593 xmlOutputBufferWriteString(ctx->buf,
1594 (const char *) buffer);
1595 xmlFree(buffer);
1596 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001597 xmlC14NErrInternal("normalizing pi node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001598 return (-1);
1599 }
1600 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001601
Daniel Veillard9ff88172002-03-11 09:15:32 +00001602 if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) {
1603 xmlOutputBufferWriteString(ctx->buf, "?>\x0A");
1604 } else {
1605 xmlOutputBufferWriteString(ctx->buf, "?>");
1606 }
1607 }
1608 break;
1609 case XML_COMMENT_NODE:
1610 /*
1611 * Comment Nodes
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001612 * Nothing if generating canonical XML without comments. For
1613 * canonical XML with comments, generate the opening comment
1614 * symbol (<!--), the string value of the node, and the
1615 * closing comment symbol (-->). Also, a trailing #xA is rendered
1616 * after the closing comment symbol for comment children of the
1617 * root node with a lesser document order than the document
1618 * element, and a leading #xA is rendered before the opening
1619 * comment symbol of comment children of the root node with a
1620 * greater document order than the document element. (Comment
1621 * children of the root node represent comments outside of the
1622 * top-level document element and outside of the document type
Daniel Veillard9ff88172002-03-11 09:15:32 +00001623 * declaration).
1624 */
1625 if (visible && ctx->with_comments) {
1626 if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) {
1627 xmlOutputBufferWriteString(ctx->buf, "\x0A<!--");
1628 } else {
1629 xmlOutputBufferWriteString(ctx->buf, "<!--");
1630 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001631
Daniel Veillard9ff88172002-03-11 09:15:32 +00001632 if (cur->content != NULL) {
1633 xmlChar *buffer;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001634
Daniel Veillard9ff88172002-03-11 09:15:32 +00001635 /* todo: do we need to normalize comment? */
1636 buffer = xmlC11NNormalizeComment(cur->content);
1637 if (buffer != NULL) {
1638 xmlOutputBufferWriteString(ctx->buf,
1639 (const char *) buffer);
1640 xmlFree(buffer);
1641 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001642 xmlC14NErrInternal("normalizing comment node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001643 return (-1);
1644 }
1645 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001646
Daniel Veillard9ff88172002-03-11 09:15:32 +00001647 if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) {
1648 xmlOutputBufferWriteString(ctx->buf, "-->\x0A");
1649 } else {
1650 xmlOutputBufferWriteString(ctx->buf, "-->");
1651 }
1652 }
1653 break;
1654 case XML_DOCUMENT_NODE:
1655 case XML_DOCUMENT_FRAG_NODE: /* should be processed as document? */
Daniel Veillard1840ef02002-03-21 08:05:23 +00001656#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard9ff88172002-03-11 09:15:32 +00001657 case XML_DOCB_DOCUMENT_NODE: /* should be processed as document? */
Daniel Veillard1840ef02002-03-21 08:05:23 +00001658#endif
1659#ifdef LIBXML_HTML_ENABLED
Daniel Veillard9ff88172002-03-11 09:15:32 +00001660 case XML_HTML_DOCUMENT_NODE: /* should be processed as document? */
Daniel Veillard1840ef02002-03-21 08:05:23 +00001661#endif
Daniel Veillard9ff88172002-03-11 09:15:32 +00001662 if (cur->children != NULL) {
1663 ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT;
1664 ctx->parent_is_doc = 1;
1665 ret = xmlC14NProcessNodeList(ctx, cur->children);
1666 }
1667 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001668
Daniel Veillard9ff88172002-03-11 09:15:32 +00001669 case XML_ATTRIBUTE_NODE:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001670 xmlC14NErrInvalidNode("XML_ATTRIBUTE_NODE", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001671 return (-1);
1672 case XML_NAMESPACE_DECL:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001673 xmlC14NErrInvalidNode("XML_NAMESPACE_DECL", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001674 return (-1);
1675 case XML_ENTITY_REF_NODE:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001676 xmlC14NErrInvalidNode("XML_ENTITY_REF_NODE", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001677 return (-1);
1678 case XML_ENTITY_NODE:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001679 xmlC14NErrInvalidNode("XML_ENTITY_NODE", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001680 return (-1);
1681
1682 case XML_DOCUMENT_TYPE_NODE:
1683 case XML_NOTATION_NODE:
1684 case XML_DTD_NODE:
1685 case XML_ELEMENT_DECL:
1686 case XML_ATTRIBUTE_DECL:
1687 case XML_ENTITY_DECL:
Daniel Veillard1840ef02002-03-21 08:05:23 +00001688#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard9ff88172002-03-11 09:15:32 +00001689 case XML_XINCLUDE_START:
1690 case XML_XINCLUDE_END:
Daniel Veillard1840ef02002-03-21 08:05:23 +00001691#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001692 /*
1693 * should be ignored according to "W3C Canonical XML"
Daniel Veillard9ff88172002-03-11 09:15:32 +00001694 */
1695 break;
1696 default:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001697 xmlC14NErrUnknownNode(cur->type, "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001698 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001699 }
1700
Daniel Veillard9ff88172002-03-11 09:15:32 +00001701 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001702}
1703
1704/**
1705 * xmlC14NProcessNodeList:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001706 * @ctx: the pointer to C14N context object
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001707 * @cur: the node to start from
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001708 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001709 * Processes all nodes in the row starting from cur.
1710 *
1711 * Returns non-negative value on success or negative value on fail
1712 */
1713static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001714xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur)
1715{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001716 int ret;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001717
1718 if (ctx == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001719 xmlC14NErrParam("processing node list");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001720 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001721 }
1722
Daniel Veillard9ff88172002-03-11 09:15:32 +00001723 for (ret = 0; cur != NULL && ret >= 0; cur = cur->next) {
1724 ret = xmlC14NProcessNode(ctx, cur);
1725 }
1726 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001727}
1728
1729
1730/**
1731 * xmlC14NFreeCtx:
1732 * @ctx: the pointer to C14N context object
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001733 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001734 * Cleanups the C14N context object.
1735 */
1736
1737static void
Daniel Veillard9ff88172002-03-11 09:15:32 +00001738xmlC14NFreeCtx(xmlC14NCtxPtr ctx)
1739{
1740 if (ctx == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001741 xmlC14NErrParam("freeing context");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001742 return;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001743 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001744
1745 if (ctx->ns_rendered != NULL) {
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001746 xmlC14NVisibleNsStackDestroy(ctx->ns_rendered);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001747 }
1748 xmlFree(ctx);
1749}
1750
1751/**
1752 * xmlC14NNewCtx:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001753 * @doc: the XML document for canonization
1754 * @is_visible_callback:the function to use to determine is node visible
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001755 * or not
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001756 * @user_data: the first parameter for @is_visible_callback function
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001757 * (in most cases, it is nodes set)
Aleksey Sanin83868242009-07-09 10:26:22 +02001758 * @mode: the c14n mode (see @xmlC14NMode)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001759 * @inclusive_ns_prefixe the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001760 * ended with a NULL or NULL if there is no
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001761 * inclusive namespaces (only for `
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001762 * canonicalization)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001763 * @with_comments: include comments in the result (!=0) or not (==0)
1764 * @buf: the output buffer to store canonical XML; this
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001765 * buffer MUST have encoder==NULL because C14N requires
1766 * UTF-8 output
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001767 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001768 * Creates new C14N context object to store C14N parameters.
1769 *
1770 * Returns pointer to newly created object (success) or NULL (fail)
1771 */
1772static xmlC14NCtxPtr
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001773xmlC14NNewCtx(xmlDocPtr doc,
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001774 xmlC14NIsVisibleCallback is_visible_callback, void* user_data,
Aleksey Sanin83868242009-07-09 10:26:22 +02001775 xmlC14NMode mode, xmlChar ** inclusive_ns_prefixes,
Daniel Veillard9ff88172002-03-11 09:15:32 +00001776 int with_comments, xmlOutputBufferPtr buf)
1777{
Daniel Veillard659e71e2003-10-10 14:10:40 +00001778 xmlC14NCtxPtr ctx = NULL;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001779
Daniel Veillard9ff88172002-03-11 09:15:32 +00001780 if ((doc == NULL) || (buf == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001781 xmlC14NErrParam("creating new context");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001782 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001783 }
1784
1785 /*
1786 * Validate the encoding output buffer encoding
1787 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001788 if (buf->encoder != NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001789 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
1790"xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001791 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001792 }
1793
1794 /*
1795 * Validate the XML document encoding value, if provided.
1796 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001797 if (doc->charset != XML_CHAR_ENCODING_UTF8) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001798 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
1799 "xmlC14NNewCtx: source document not in UTF8\n");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001800 return (NULL);
1801 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001802
1803 /*
1804 * Allocate a new xmlC14NCtxPtr and fill the fields.
1805 */
1806 ctx = (xmlC14NCtxPtr) xmlMalloc(sizeof(xmlC14NCtx));
1807 if (ctx == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001808 xmlC14NErrMemory("creating context");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001809 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001810 }
1811 memset(ctx, 0, sizeof(xmlC14NCtx));
1812
1813 /*
1814 * initialize C14N context
Daniel Veillard9ff88172002-03-11 09:15:32 +00001815 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001816 ctx->doc = doc;
1817 ctx->with_comments = with_comments;
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001818 ctx->is_visible_callback = is_visible_callback;
1819 ctx->user_data = user_data;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001820 ctx->buf = buf;
1821 ctx->parent_is_doc = 1;
1822 ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001823 ctx->ns_rendered = xmlC14NVisibleNsStackCreate();
1824
1825 if(ctx->ns_rendered == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001826 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_CREATE_STACK,
1827 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001828 xmlC14NFreeCtx(ctx);
1829 return (NULL);
1830 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001831
1832 /*
Aleksey Sanin83868242009-07-09 10:26:22 +02001833 * Set "mode" flag and remember list of incluseve prefixes
1834 * for exclusive c14n
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001835 */
Aleksey Sanin83868242009-07-09 10:26:22 +02001836 ctx->mode = mode;
1837 if(xmlC14NIsExclusive(ctx)) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001838 ctx->inclusive_ns_prefixes = inclusive_ns_prefixes;
1839 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001840 return (ctx);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001841}
1842
1843/**
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001844 * xmlC14NExecute:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001845 * @doc: the XML document for canonization
1846 * @is_visible_callback:the function to use to determine is node visible
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001847 * or not
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001848 * @user_data: the first parameter for @is_visible_callback function
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001849 * (in most cases, it is nodes set)
Aleksey Sanin83868242009-07-09 10:26:22 +02001850 * @mode: the c14n mode (see @xmlC14NMode)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001851 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001852 * ended with a NULL or NULL if there is no
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001853 * inclusive namespaces (only for exclusive
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001854 * canonicalization, ignored otherwise)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001855 * @with_comments: include comments in the result (!=0) or not (==0)
1856 * @buf: the output buffer to store canonical XML; this
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001857 * buffer MUST have encoder==NULL because C14N requires
1858 * UTF-8 output
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001859 *
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001860 * Dumps the canonized image of given XML document into the provided buffer.
1861 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1862 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1863 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001864 * Returns non-negative value on success or a negative value on fail
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001865 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001866int
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001867xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback,
Aleksey Sanin175beba2009-07-09 22:54:00 +02001868 void* user_data, int mode, xmlChar **inclusive_ns_prefixes,
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001869 int with_comments, xmlOutputBufferPtr buf) {
1870
1871 xmlC14NCtxPtr ctx;
Aleksey Sanin175beba2009-07-09 22:54:00 +02001872 xmlC14NMode c14n_mode = XML_C14N_1_0;
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001873 int ret;
1874
1875 if ((buf == NULL) || (doc == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001876 xmlC14NErrParam("executing c14n");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001877 return (-1);
1878 }
1879
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001880 /* for backward compatibility, we have to have "mode" as "int"
Aleksey Sanin175beba2009-07-09 22:54:00 +02001881 and here we check that user gives valid value */
1882 switch(mode) {
1883 case XML_C14N_1_0:
1884 case XML_C14N_EXCLUSIVE_1_0:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001885 case XML_C14N_1_1:
Aleksey Sanin175beba2009-07-09 22:54:00 +02001886 c14n_mode = (xmlC14NMode)mode;
1887 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001888 default:
Aleksey Sanin175beba2009-07-09 22:54:00 +02001889 xmlC14NErrParam("invalid mode for executing c14n");
1890 return (-1);
1891 }
1892
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001893 /*
1894 * Validate the encoding output buffer encoding
1895 */
1896 if (buf->encoder != NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001897 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
1898"xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001899 return (-1);
1900 }
1901
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001902 ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data,
Aleksey Sanin175beba2009-07-09 22:54:00 +02001903 c14n_mode, inclusive_ns_prefixes,
1904 with_comments, buf);
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001905 if (ctx == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001906 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_CREATE_CTXT,
1907 "xmlC14NExecute: unable to create C14N context\n");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001908 return (-1);
1909 }
1910
1911
1912
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001913 /*
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001914 * Root Node
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001915 * The root node is the parent of the top-level document element. The
1916 * result of processing each of its child nodes that is in the node-set
1917 * in document order. The root node does not generate a byte order mark,
1918 * XML declaration, nor anything from within the document type
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001919 * declaration.
1920 */
1921 if (doc->children != NULL) {
1922 ret = xmlC14NProcessNodeList(ctx, doc->children);
1923 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001924 xmlC14NErrInternal("processing docs children list");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001925 xmlC14NFreeCtx(ctx);
1926 return (-1);
1927 }
1928 }
1929
1930 /*
1931 * Flush buffer to get number of bytes written
1932 */
1933 ret = xmlOutputBufferFlush(buf);
1934 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001935 xmlC14NErrInternal("flushing output buffer");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001936 xmlC14NFreeCtx(ctx);
1937 return (-1);
1938 }
1939
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001940 /*
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001941 * Cleanup
1942 */
1943 xmlC14NFreeCtx(ctx);
1944 return (ret);
1945}
1946
1947/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001948 * xmlC14NDocSaveTo:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001949 * @doc: the XML document for canonization
1950 * @nodes: the nodes set to be included in the canonized image
1951 * or NULL if all document nodes should be included
Aleksey Sanin83868242009-07-09 10:26:22 +02001952 * @mode: the c14n mode (see @xmlC14NMode)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001953 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001954 * ended with a NULL or NULL if there is no
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001955 * inclusive namespaces (only for exclusive
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001956 * canonicalization, ignored otherwise)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001957 * @with_comments: include comments in the result (!=0) or not (==0)
1958 * @buf: the output buffer to store canonical XML; this
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001959 * buffer MUST have encoder==NULL because C14N requires
1960 * UTF-8 output
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001961 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001962 * Dumps the canonized image of given XML document into the provided buffer.
1963 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1964 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1965 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001966 * Returns non-negative value on success or a negative value on fail
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001967 */
1968int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001969xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes,
Aleksey Sanin83868242009-07-09 10:26:22 +02001970 int mode, xmlChar ** inclusive_ns_prefixes,
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001971 int with_comments, xmlOutputBufferPtr buf) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001972 return(xmlC14NExecute(doc,
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001973 (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset,
1974 nodes,
Aleksey Sanin83868242009-07-09 10:26:22 +02001975 mode,
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001976 inclusive_ns_prefixes,
1977 with_comments,
1978 buf));
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001979}
1980
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001981
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001982/**
1983 * xmlC14NDocDumpMemory:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001984 * @doc: the XML document for canonization
1985 * @nodes: the nodes set to be included in the canonized image
1986 * or NULL if all document nodes should be included
Aleksey Sanin83868242009-07-09 10:26:22 +02001987 * @mode: the c14n mode (see @xmlC14NMode)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001988 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001989 * ended with a NULL or NULL if there is no
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001990 * inclusive namespaces (only for exclusive
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001991 * canonicalization, ignored otherwise)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001992 * @with_comments: include comments in the result (!=0) or not (==0)
1993 * @doc_txt_ptr: the memory pointer for allocated canonical XML text;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001994 * the caller of this functions is responsible for calling
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001995 * xmlFree() to free allocated memory
1996 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001997 * Dumps the canonized image of given XML document into memory.
1998 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1999 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
2000 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002001 * Returns the number of bytes written on success or a negative value on fail
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002002 */
2003int
Daniel Veillard9ff88172002-03-11 09:15:32 +00002004xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes,
Aleksey Sanin175beba2009-07-09 22:54:00 +02002005 int mode, xmlChar ** inclusive_ns_prefixes,
Daniel Veillard9ff88172002-03-11 09:15:32 +00002006 int with_comments, xmlChar ** doc_txt_ptr)
2007{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002008 int ret;
2009 xmlOutputBufferPtr buf;
2010
2011 if (doc_txt_ptr == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002012 xmlC14NErrParam("dumping doc to memory");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002013 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002014 }
2015
2016 *doc_txt_ptr = NULL;
Daniel Veillard9ff88172002-03-11 09:15:32 +00002017
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002018 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002019 * create memory buffer with UTF8 (default) encoding
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002020 */
2021 buf = xmlAllocOutputBuffer(NULL);
Daniel Veillard9ff88172002-03-11 09:15:32 +00002022 if (buf == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002023 xmlC14NErrMemory("creating output buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002024 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002025 }
2026
2027 /*
2028 * canonize document and write to buffer
2029 */
Aleksey Sanin83868242009-07-09 10:26:22 +02002030 ret = xmlC14NDocSaveTo(doc, nodes, mode, inclusive_ns_prefixes,
Daniel Veillard9ff88172002-03-11 09:15:32 +00002031 with_comments, buf);
2032 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002033 xmlC14NErrInternal("saving doc to output buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002034 (void) xmlOutputBufferClose(buf);
2035 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002036 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002037
Daniel Veillard53aa2932012-07-16 14:37:00 +08002038 ret = xmlBufUse(buf->buffer);
Daniel Veillard9ff88172002-03-11 09:15:32 +00002039 if (ret > 0) {
Daniel Veillard53aa2932012-07-16 14:37:00 +08002040 *doc_txt_ptr = xmlStrndup(xmlBufContent(buf->buffer), ret);
Daniel Veillard9ff88172002-03-11 09:15:32 +00002041 }
2042 (void) xmlOutputBufferClose(buf);
2043
2044 if ((*doc_txt_ptr == NULL) && (ret > 0)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002045 xmlC14NErrMemory("coping canonicanized document");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002046 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002047 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00002048 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002049}
2050
2051/**
2052 * xmlC14NDocSave:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002053 * @doc: the XML document for canonization
2054 * @nodes: the nodes set to be included in the canonized image
2055 * or NULL if all document nodes should be included
Aleksey Sanin83868242009-07-09 10:26:22 +02002056 * @mode: the c14n mode (see @xmlC14NMode)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002057 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002058 * ended with a NULL or NULL if there is no
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002059 * inclusive namespaces (only for exclusive
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002060 * canonicalization, ignored otherwise)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002061 * @with_comments: include comments in the result (!=0) or not (==0)
2062 * @filename: the filename to store canonical XML image
2063 * @compression: the compression level (zlib requred):
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002064 * -1 - libxml default,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002065 * 0 - uncompressed,
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002066 * >0 - compression level
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002067 *
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002068 * Dumps the canonized image of given XML document into the file.
2069 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
2070 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
2071 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002072 * Returns the number of bytes written success or a negative value on fail
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002073 */
2074int
Daniel Veillard9ff88172002-03-11 09:15:32 +00002075xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
Aleksey Sanin175beba2009-07-09 22:54:00 +02002076 int mode, xmlChar ** inclusive_ns_prefixes,
Daniel Veillard9ff88172002-03-11 09:15:32 +00002077 int with_comments, const char *filename, int compression)
2078{
2079 xmlOutputBufferPtr buf;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002080 int ret;
2081
Daniel Veillard9ff88172002-03-11 09:15:32 +00002082 if (filename == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002083 xmlC14NErrParam("saving doc");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002084 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002085 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002086#ifdef HAVE_ZLIB_H
Daniel Veillard9ff88172002-03-11 09:15:32 +00002087 if (compression < 0)
2088 compression = xmlGetCompressMode();
2089#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002090
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002091 /*
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002092 * save the content to a temp buffer, use default UTF8 encoding.
2093 */
2094 buf = xmlOutputBufferCreateFilename(filename, NULL, compression);
2095 if (buf == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002096 xmlC14NErrInternal("creating temporary filename");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002097 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002098 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00002099
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002100 /*
2101 * canonize document and write to buffer
2102 */
Aleksey Sanin83868242009-07-09 10:26:22 +02002103 ret = xmlC14NDocSaveTo(doc, nodes, mode, inclusive_ns_prefixes,
Daniel Veillard9ff88172002-03-11 09:15:32 +00002104 with_comments, buf);
2105 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00002106 xmlC14NErrInternal("cannicanize document to buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002107 (void) xmlOutputBufferClose(buf);
2108 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002109 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00002110
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002111 /*
2112 * get the numbers of bytes written
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002113 */
2114 ret = xmlOutputBufferClose(buf);
Daniel Veillard9ff88172002-03-11 09:15:32 +00002115 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002116}
2117
2118
2119
2120/*
2121 * Macro used to grow the current buffer.
2122 */
2123#define growBufferReentrant() { \
2124 buffer_size *= 2; \
2125 buffer = (xmlChar *) \
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002126 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002127 if (buffer == NULL) { \
Daniel Veillardd96cce12003-10-10 12:30:37 +00002128 xmlC14NErrMemory("growing buffer"); \
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002129 return(NULL); \
2130 } \
2131}
2132
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002133/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002134 * xmlC11NNormalizeString:
2135 * @input: the input string
2136 * @mode: the normalization mode (attribute, comment, PI or text)
2137 *
2138 * Converts a string to a canonical (normalized) format. The code is stolen
2139 * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A
2140 * and the @mode parameter
2141 *
2142 * Returns a normalized string (caller is responsible for calling xmlFree())
2143 * or NULL if an error occurs
2144 */
2145static xmlChar *
Daniel Veillard9ff88172002-03-11 09:15:32 +00002146xmlC11NNormalizeString(const xmlChar * input,
2147 xmlC14NNormalizationMode mode)
2148{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002149 const xmlChar *cur = input;
2150 xmlChar *buffer = NULL;
2151 xmlChar *out = NULL;
2152 int buffer_size = 0;
2153
Daniel Veillard9ff88172002-03-11 09:15:32 +00002154 if (input == NULL)
2155 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002156
2157 /*
2158 * allocate an translation buffer.
2159 */
2160 buffer_size = 1000;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002161 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002162 if (buffer == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00002163 xmlC14NErrMemory("allocating buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00002164 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002165 }
2166 out = buffer;
2167
2168 while (*cur != '\0') {
Daniel Veillard9ff88172002-03-11 09:15:32 +00002169 if ((out - buffer) > (buffer_size - 10)) {
2170 int indx = out - buffer;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002171
Daniel Veillard9ff88172002-03-11 09:15:32 +00002172 growBufferReentrant();
2173 out = &buffer[indx];
2174 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002175
Daniel Veillard9ff88172002-03-11 09:15:32 +00002176 if ((*cur == '<') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
2177 (mode == XMLC14N_NORMALIZE_TEXT))) {
2178 *out++ = '&';
2179 *out++ = 'l';
2180 *out++ = 't';
2181 *out++ = ';';
2182 } else if ((*cur == '>') && (mode == XMLC14N_NORMALIZE_TEXT)) {
2183 *out++ = '&';
2184 *out++ = 'g';
2185 *out++ = 't';
2186 *out++ = ';';
2187 } else if ((*cur == '&') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
2188 (mode == XMLC14N_NORMALIZE_TEXT))) {
2189 *out++ = '&';
2190 *out++ = 'a';
2191 *out++ = 'm';
2192 *out++ = 'p';
2193 *out++ = ';';
2194 } else if ((*cur == '"') && (mode == XMLC14N_NORMALIZE_ATTR)) {
2195 *out++ = '&';
2196 *out++ = 'q';
2197 *out++ = 'u';
2198 *out++ = 'o';
2199 *out++ = 't';
2200 *out++ = ';';
2201 } else if ((*cur == '\x09') && (mode == XMLC14N_NORMALIZE_ATTR)) {
2202 *out++ = '&';
2203 *out++ = '#';
2204 *out++ = 'x';
2205 *out++ = '9';
2206 *out++ = ';';
2207 } else if ((*cur == '\x0A') && (mode == XMLC14N_NORMALIZE_ATTR)) {
2208 *out++ = '&';
2209 *out++ = '#';
2210 *out++ = 'x';
2211 *out++ = 'A';
2212 *out++ = ';';
2213 } else if ((*cur == '\x0D') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
2214 (mode == XMLC14N_NORMALIZE_TEXT) ||
2215 (mode == XMLC14N_NORMALIZE_COMMENT) ||
2216 (mode == XMLC14N_NORMALIZE_PI))) {
2217 *out++ = '&';
2218 *out++ = '#';
2219 *out++ = 'x';
2220 *out++ = 'D';
2221 *out++ = ';';
2222 } else {
2223 /*
2224 * Works because on UTF-8, all extended sequences cannot
2225 * result in bytes in the ASCII range.
2226 */
2227 *out++ = *cur;
2228 }
2229 cur++;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002230 }
Daniel Veillard13cee4e2009-09-05 14:52:55 +02002231 *out = 0;
Daniel Veillard9ff88172002-03-11 09:15:32 +00002232 return (buffer);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002233}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002234#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002235#define bottom_c14n
2236#include "elfgcchack.h"
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002237#endif /* LIBXML_C14N_ENABLED */