blob: 33a5803ff7a17d4550424f311e77ada1e3eab514 [file] [log] [blame]
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001/*
2 * "Canonical XML" implementation
3 * http://www.w3.org/TR/xml-c14n
4 *
5 * "Exclusive XML Canonicalization" implementation
6 * http://www.w3.org/TR/xml-exc-c14n
7 *
8 * See Copyright for the status of this software.
9 *
10 * Author: Aleksey Sanin <aleksey@aleksey.com>
11 */
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
30/************************************************************************
31 * *
32 * Some declaration better left private ATM *
33 * *
34 ************************************************************************/
35
Daniel Veillard9ff88172002-03-11 09:15:32 +000036typedef enum {
37 XMLC14N_BEFORE_DOCUMENT_ELEMENT = 0,
38 XMLC14N_INSIDE_DOCUMENT_ELEMENT = 1,
39 XMLC14N_AFTER_DOCUMENT_ELEMENT = 2
Daniel Veillard044fc6b2002-03-04 17:09:44 +000040} xmlC14NPosition;
41
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +000042typedef struct _xmlC14NVisibleNsStack {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +000043 int nsCurEnd; /* number of nodes in the set */
44 int nsPrevStart; /* the begginning of the stack for previous visible node */
45 int nsPrevEnd; /* the end of the stack for previous visible node */
46 int nsMax; /* size of the array as allocated */
47 xmlNsPtr *nsTab; /* array of ns in no particular order */
48 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +000049} xmlC14NVisibleNsStack, *xmlC14NVisibleNsStackPtr;
Aleksey Sanindffd5c82002-05-31 04:24:13 +000050
Daniel Veillard044fc6b2002-03-04 17:09:44 +000051typedef struct _xmlC14NCtx {
52 /* input parameters */
Daniel Veillard9ff88172002-03-11 09:15:32 +000053 xmlDocPtr doc;
Aleksey Sanin2c135a12002-08-01 06:31:50 +000054 xmlC14NIsVisibleCallback is_visible_callback;
55 void* user_data;
Daniel Veillard9ff88172002-03-11 09:15:32 +000056 int with_comments;
57 xmlOutputBufferPtr buf;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000058
59 /* position in the XML document */
Daniel Veillard9ff88172002-03-11 09:15:32 +000060 xmlC14NPosition pos;
61 int parent_is_doc;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +000062 xmlC14NVisibleNsStackPtr ns_rendered;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000063
64 /* exclusive canonicalization */
Daniel Veillard9ff88172002-03-11 09:15:32 +000065 int exclusive;
Daniel Veillard9ff88172002-03-11 09:15:32 +000066 xmlChar **inclusive_ns_prefixes;
Daniel Veillardd96cce12003-10-10 12:30:37 +000067
68 /* error number */
69 int error;
Daniel Veillard044fc6b2002-03-04 17:09:44 +000070} xmlC14NCtx, *xmlC14NCtxPtr;
71
Aleksey Sanin2c135a12002-08-01 06:31:50 +000072static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate (void);
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +000073static void xmlC14NVisibleNsStackDestroy (xmlC14NVisibleNsStackPtr cur);
74static void xmlC14NVisibleNsStackAdd (xmlC14NVisibleNsStackPtr cur,
75 xmlNsPtr ns,
76 xmlNodePtr node);
Aleksey Sanin2c135a12002-08-01 06:31:50 +000077static void xmlC14NVisibleNsStackSave (xmlC14NVisibleNsStackPtr cur,
78 xmlC14NVisibleNsStackPtr state);
79static void xmlC14NVisibleNsStackRestore (xmlC14NVisibleNsStackPtr cur,
80 xmlC14NVisibleNsStackPtr state);
81static void xmlC14NVisibleNsStackShift (xmlC14NVisibleNsStackPtr cur);
82static int xmlC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur,
83 xmlNsPtr ns);
84static int xmlExcC14NVisibleNsStackFind (xmlC14NVisibleNsStackPtr cur,
85 xmlNsPtr ns,
86 xmlC14NCtxPtr ctx);
87
88static int xmlC14NIsNodeInNodeset (xmlNodeSetPtr nodes,
89 xmlNodePtr node,
90 xmlNodePtr parent);
91
92
Daniel Veillard044fc6b2002-03-04 17:09:44 +000093
Daniel Veillard9ff88172002-03-11 09:15:32 +000094static int xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur);
95static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur);
Daniel Veillard044fc6b2002-03-04 17:09:44 +000096typedef enum {
Daniel Veillard9ff88172002-03-11 09:15:32 +000097 XMLC14N_NORMALIZE_ATTR = 0,
98 XMLC14N_NORMALIZE_COMMENT = 1,
99 XMLC14N_NORMALIZE_PI = 2,
100 XMLC14N_NORMALIZE_TEXT = 3
101} xmlC14NNormalizationMode;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000102
Daniel Veillard9ff88172002-03-11 09:15:32 +0000103static xmlChar *xmlC11NNormalizeString(const xmlChar * input,
104 xmlC14NNormalizationMode mode);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000105
106#define xmlC11NNormalizeAttr( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000107 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR)
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000108#define xmlC11NNormalizeComment( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000109 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT)
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000110#define xmlC11NNormalizePI( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000111 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI)
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000112#define xmlC11NNormalizeText( a ) \
Daniel Veillard9ff88172002-03-11 09:15:32 +0000113 xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT)
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000114
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000115#define xmlC14NIsVisible( ctx, node, parent ) \
116 (((ctx)->is_visible_callback != NULL) ? \
117 (ctx)->is_visible_callback((ctx)->user_data, \
118 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1)
Daniel Veillardd96cce12003-10-10 12:30:37 +0000119
120/************************************************************************
121 * *
122 * Some factorized error routines *
123 * *
124 ************************************************************************/
125
126/**
127 * xmlC14NErrMemory:
128 * @extra: extra informations
129 *
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000130 * Handle a redefinition of memory error
Daniel Veillardd96cce12003-10-10 12:30:37 +0000131 */
132static void
133xmlC14NErrMemory(const char *extra)
134{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000135 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
Daniel Veillardd96cce12003-10-10 12:30:37 +0000136 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
137 NULL, NULL, 0, 0,
138 "Memory allocation failed : %s\n", extra);
139}
140
141/**
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000142 * xmlC14NErrParam:
143 * @extra: extra informations
144 *
145 * Handle a redefinition of param error
146 */
147static void
148xmlC14NErrParam(const char *extra)
149{
150 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
151 XML_ERR_INTERNAL_ERROR, XML_ERR_ERROR, NULL, 0, extra,
152 NULL, NULL, 0, 0,
153 "Invalid parameter : %s\n", extra);
154}
155
156/**
157 * xmlC14NErrInternal:
158 * @extra: extra informations
159 *
160 * Handle a redefinition of internal error
161 */
162static void
163xmlC14NErrInternal(const char *extra)
164{
165 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
166 XML_ERR_INTERNAL_ERROR, XML_ERR_ERROR, NULL, 0, extra,
167 NULL, NULL, 0, 0,
168 "Internal error : %s\n", extra);
169}
170
171/**
172 * xmlC14NErrInvalidNode:
173 * @extra: extra informations
174 *
175 * Handle a redefinition of invalid node error
176 */
177static void
178xmlC14NErrInvalidNode(const char *node_type, const char *extra)
179{
180 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
181 XML_C14N_INVALID_NODE, XML_ERR_ERROR, NULL, 0, extra,
182 NULL, NULL, 0, 0,
183 "Node %s is invalid here : %s\n", node_type, extra);
184}
185
186/**
187 * xmlC14NErrUnknownNode:
188 * @extra: extra informations
189 *
190 * Handle a redefinition of unknown node error
191 */
192static void
193xmlC14NErrUnknownNode(int node_type, const char *extra)
194{
195 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
196 XML_C14N_UNKNOW_NODE, XML_ERR_ERROR, NULL, 0, extra,
197 NULL, NULL, 0, 0,
198 "Unknown node type %d found : %s\n", node_type, extra);
199}
200
201/**
202 * xmlC14NErrRelativeNamespace:
203 * @extra: extra informations
204 *
205 * Handle a redefinition of relative namespace error
206 */
207static void
208xmlC14NErrRelativeNamespace(const char *ns_uri)
209{
210 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_C14N,
211 XML_C14N_RELATIVE_NAMESPACE, XML_ERR_ERROR, NULL, 0, NULL,
212 NULL, NULL, 0, 0,
213 "Relative namespace UR is invalid here : %s\n", ns_uri);
214}
215
216
217
218/**
Daniel Veillardd96cce12003-10-10 12:30:37 +0000219 * xmlC14NErr:
220 * @ctxt: a C14N evaluation context
221 * @node: the context node
222 * @error: the erorr code
223 * @msg: the message
224 * @extra: extra informations
225 *
226 * Handle a redefinition of attribute error
227 */
228static void
229xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int error,
230 const char * msg)
231{
232 if (ctxt != NULL)
233 ctxt->error = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000234 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96cce12003-10-10 12:30:37 +0000235 ctxt, node, XML_FROM_C14N, error,
236 XML_ERR_ERROR, NULL, 0,
237 NULL, NULL, NULL, 0, 0, msg);
238}
239
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000240/************************************************************************
241 * *
242 * The implementation internals *
243 * *
244 ************************************************************************/
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000245#define XML_NAMESPACES_DEFAULT 16
246
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000247static int
248xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes, xmlNodePtr node, xmlNodePtr parent) {
249 if((nodes != NULL) && (node != NULL)) {
250 if(node->type != XML_NAMESPACE_DECL) {
251 return(xmlXPathNodeSetContains(nodes, node));
252 } else {
253 xmlNs ns;
254
255 memcpy(&ns, node, sizeof(ns));
Aleksey Sanin6de6f972004-04-20 02:05:30 +0000256
257 /* this is a libxml hack! check xpath.c for details */
258 if((parent != NULL) && (parent->type == XML_ATTRIBUTE_NODE)) {
259 ns.next = (xmlNsPtr)parent->parent;
260 } else {
261 ns.next = (xmlNsPtr)parent;
262 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000263
264 /*
265 * If the input is an XPath node-set, then the node-set must explicitly
266 * contain every node to be rendered to the canonical form.
267 */
268 return(xmlXPathNodeSetContains(nodes, (xmlNodePtr)&ns));
269 }
270 }
271 return(1);
272}
273
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000274static xmlC14NVisibleNsStackPtr
275xmlC14NVisibleNsStackCreate(void) {
276 xmlC14NVisibleNsStackPtr ret;
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000277
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000278 ret = (xmlC14NVisibleNsStackPtr) xmlMalloc(sizeof(xmlC14NVisibleNsStack));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000279 if (ret == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000280 xmlC14NErrMemory("creating namespaces stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000281 return(NULL);
282 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000283 memset(ret, 0 , (size_t) sizeof(xmlC14NVisibleNsStack));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000284 return(ret);
285}
286
287static void
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000288xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr cur) {
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000289 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000290 xmlC14NErrParam("destroying namespaces stack");
291 return;
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000292 }
293 if(cur->nsTab != NULL) {
294 memset(cur->nsTab, 0, cur->nsMax * sizeof(xmlNsPtr));
295 xmlFree(cur->nsTab);
296 }
Aleksey Saninea4272a2002-08-02 23:50:03 +0000297 if(cur->nodeTab != NULL) {
298 memset(cur->nodeTab, 0, cur->nsMax * sizeof(xmlNodePtr));
299 xmlFree(cur->nodeTab);
300 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000301 memset(cur, 0, sizeof(xmlC14NVisibleNsStack));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000302 xmlFree(cur);
303
304}
305
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000306static void
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000307xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlNodePtr node) {
308 if((cur == NULL) ||
309 ((cur->nsTab == NULL) && (cur->nodeTab != NULL)) ||
310 ((cur->nsTab != NULL) && (cur->nodeTab == NULL))) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000311 xmlC14NErrParam("adding namespace to stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000312 return;
313 }
314
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000315 if ((cur->nsTab == NULL) && (cur->nodeTab == NULL)) {
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000316 cur->nsTab = (xmlNsPtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr));
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000317 cur->nodeTab = (xmlNodePtr*) xmlMalloc(XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
318 if ((cur->nsTab == NULL) || (cur->nodeTab == NULL)) {
Daniel Veillardd96cce12003-10-10 12:30:37 +0000319 xmlC14NErrMemory("adding node to stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000320 return;
321 }
322 memset(cur->nsTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNsPtr));
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000323 memset(cur->nodeTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000324 cur->nsMax = XML_NAMESPACES_DEFAULT;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000325 } else if(cur->nsMax == cur->nsCurEnd) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000326 void *tmp;
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000327 int tmpSize;
328
329 tmpSize = 2 * cur->nsMax;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000330 tmp = xmlRealloc(cur->nsTab, tmpSize * sizeof(xmlNsPtr));
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000331 if (tmp == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +0000332 xmlC14NErrMemory("adding node to stack");
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000333 return;
334 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000335 cur->nsTab = (xmlNsPtr*)tmp;
336
337 tmp = xmlRealloc(cur->nodeTab, tmpSize * sizeof(xmlNodePtr));
338 if (tmp == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +0000339 xmlC14NErrMemory("adding node to stack");
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000340 return;
341 }
342 cur->nodeTab = (xmlNodePtr*)tmp;
343
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000344 cur->nsMax = tmpSize;
345 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000346 cur->nsTab[cur->nsCurEnd] = ns;
347 cur->nodeTab[cur->nsCurEnd] = node;
348
349 ++cur->nsCurEnd;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000350}
351
352static void
353xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) {
354 if((cur == NULL) || (state == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000355 xmlC14NErrParam("saving namespaces stack");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000356 return;
357 }
358
359 state->nsCurEnd = cur->nsCurEnd;
360 state->nsPrevStart = cur->nsPrevStart;
361 state->nsPrevEnd = cur->nsPrevEnd;
362}
363
364static void
365xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr cur, xmlC14NVisibleNsStackPtr state) {
366 if((cur == NULL) || (state == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000367 xmlC14NErrParam("restoring namespaces stack");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000368 return;
369 }
370 cur->nsCurEnd = state->nsCurEnd;
371 cur->nsPrevStart = state->nsPrevStart;
372 cur->nsPrevEnd = state->nsPrevEnd;
373}
374
375static void
376xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur) {
377 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000378 xmlC14NErrParam("shifting namespaces stack");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000379 return;
380 }
381 cur->nsPrevStart = cur->nsPrevEnd;
382 cur->nsPrevEnd = cur->nsCurEnd;
383}
384
385static int
386xmlC14NStrEqual(const xmlChar *str1, const xmlChar *str2) {
387 if (str1 == str2) return(1);
388 if (str1 == NULL) return((*str2) == '\0');
389 if (str2 == NULL) return((*str1) == '\0');
390 do {
391 if (*str1++ != *str2) return(0);
392 } while (*str2++);
393 return(1);
394}
395
396/**
397 * xmlC14NVisibleNsStackFind:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000398 * @ctx: the C14N context
399 * @ns: the namespace to check
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000400 *
401 * Checks whether the given namespace was already rendered or not
402 *
403 * Returns 1 if we already wrote this namespace or 0 otherwise
404 */
405static int
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000406xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns)
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000407{
408 int i;
409 const xmlChar *prefix;
410 const xmlChar *href;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000411 int has_empty_ns;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000412
413 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000414 xmlC14NErrParam("searching namespaces stack (c14n)");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000415 return (0);
416 }
417
418 /*
419 * if the default namespace xmlns="" is not defined yet then
420 * we do not want to print it out
421 */
422 prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
423 href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000424 has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL));
425
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000426 if (cur->nsTab != NULL) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000427 int start = (has_empty_ns) ? 0 : cur->nsPrevStart;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000428 for (i = cur->nsCurEnd - 1; i >= start; --i) {
429 xmlNsPtr ns1 = cur->nsTab[i];
430
431 if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
432 return(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL));
433 }
434 }
435 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000436 return(has_empty_ns);
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000437}
438
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000439static int
440xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlC14NCtxPtr ctx) {
441 int i;
442 const xmlChar *prefix;
443 const xmlChar *href;
444 int has_empty_ns;
445
446 if(cur == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000447 xmlC14NErrParam("searching namespaces stack (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000448 return (0);
449 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000450
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000451 /*
452 * if the default namespace xmlns="" is not defined yet then
453 * we do not want to print it out
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000454 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000455 prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
456 href = ((ns == NULL) || (ns->href == NULL)) ? BAD_CAST "" : ns->href;
457 has_empty_ns = (xmlC14NStrEqual(prefix, NULL) && xmlC14NStrEqual(href, NULL));
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000458
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000459 if (cur->nsTab != NULL) {
460 int start = 0;
461 for (i = cur->nsCurEnd - 1; i >= start; --i) {
462 xmlNsPtr ns1 = cur->nsTab[i];
463
464 if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
465 if(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)) {
466 return(xmlC14NIsVisible(ctx, ns1, cur->nodeTab[i]));
467 } else {
468 return(0);
469 }
470 }
471 }
472 }
473 return(has_empty_ns);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000474}
475
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000476
477
478
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000479/**
480 * xmlC14NIsXmlNs:
481 * @ns: the namespace to check
482 *
483 * Checks whether the given namespace is a default "xml:" namespace
484 * with href="http://www.w3.org/XML/1998/namespace"
485 *
486 * Returns 1 if the node is default or 0 otherwise
487 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000488
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000489/* todo: make it a define? */
490static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000491xmlC14NIsXmlNs(xmlNsPtr ns)
492{
493 return ((ns != NULL) &&
494 (xmlStrEqual(ns->prefix, BAD_CAST "xml")) &&
495 (xmlStrEqual(ns->href,
496 BAD_CAST
497 "http://www.w3.org/XML/1998/namespace")));
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000498}
499
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000500
501/**
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000502 * xmlC14NNsCompare:
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000503 * @ns1: the pointer to first namespace
504 * @ns2: the pointer to second namespace
505 *
506 * Compares the namespaces by names (prefixes).
507 *
508 * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2.
509 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000510static int
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000511xmlC14NNsCompare(xmlNsPtr ns1, xmlNsPtr ns2)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000512{
513 if (ns1 == ns2)
514 return (0);
515 if (ns1 == NULL)
516 return (-1);
517 if (ns2 == NULL)
518 return (1);
519
520 return (xmlStrcmp(ns1->prefix, ns2->prefix));
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000521}
522
523
524/**
525 * xmlC14NPrintNamespaces:
526 * @ns: the pointer to namespace
527 * @ctx: the C14N context
528 *
529 * Prints the given namespace to the output buffer from C14N context.
530 *
531 * Returns 1 on success or 0 on fail.
532 */
533static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000534xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPtr ctx)
535{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000536
Daniel Veillard9ff88172002-03-11 09:15:32 +0000537 if ((ns == NULL) || (ctx == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000538 xmlC14NErrParam("writing namespaces");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000539 return 0;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000540 }
541
Daniel Veillard9ff88172002-03-11 09:15:32 +0000542 if (ns->prefix != NULL) {
543 xmlOutputBufferWriteString(ctx->buf, " xmlns:");
544 xmlOutputBufferWriteString(ctx->buf, (const char *) ns->prefix);
545 xmlOutputBufferWriteString(ctx->buf, "=\"");
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000546 } else {
Daniel Veillard9ff88172002-03-11 09:15:32 +0000547 xmlOutputBufferWriteString(ctx->buf, " xmlns=\"");
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000548 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000549 if(ns->href != NULL) {
550 xmlOutputBufferWriteString(ctx->buf, (const char *) ns->href);
551 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000552 xmlOutputBufferWriteString(ctx->buf, "\"");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000553 return (1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000554}
555
556/**
557 * xmlC14NProcessNamespacesAxis:
558 * @ctx: the C14N context
559 * @node: the current node
560 *
561 * Prints out canonical namespace axis of the current node to the
562 * buffer from C14N context as follows
563 *
564 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
565 *
566 * Namespace Axis
567 * Consider a list L containing only namespace nodes in the
568 * axis and in the node-set in lexicographic order (ascending). To begin
569 * processing L, if the first node is not the default namespace node (a node
570 * with no namespace URI and no local name), then generate a space followed
571 * by xmlns="" if and only if the following conditions are met:
572 * - the element E that owns the axis is in the node-set
573 * - The nearest ancestor element of E in the node-set has a default
574 * namespace node in the node-set (default namespace nodes always
575 * have non-empty values in XPath)
576 * The latter condition eliminates unnecessary occurrences of xmlns="" in
577 * the canonical form since an element only receives an xmlns="" if its
578 * default namespace is empty and if it has an immediate parent in the
579 * canonical form that has a non-empty default namespace. To finish
580 * processing L, simply process every namespace node in L, except omit
581 * namespace node with local name xml, which defines the xml prefix,
582 * if its string value is http://www.w3.org/XML/1998/namespace.
583 *
584 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
585 * Canonical XML applied to a document subset requires the search of the
586 * ancestor nodes of each orphan element node for attributes in the xml
587 * namespace, such as xml:lang and xml:space. These are copied into the
588 * element node except if a declaration of the same attribute is already
589 * in the attribute axis of the element (whether or not it is included in
590 * the document subset). This search and copying are omitted from the
591 * Exclusive XML Canonicalization method.
592 *
593 * Returns 0 on success or -1 on fail.
594 */
595static int
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000596xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000597{
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000598 xmlNodePtr n;
599 xmlNsPtr ns, tmp;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000600 xmlListPtr list;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000601 int already_rendered;
602 int has_empty_ns = 0;
Daniel Veillard5c396542002-03-15 07:57:50 +0000603
Daniel Veillard9ff88172002-03-11 09:15:32 +0000604 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000605 xmlC14NErrParam("processing namespaces axis (c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000606 return (-1);
607 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000608
609 /*
610 * Create a sorted list to store element namespaces
611 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000612 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000613 if (list == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000614 xmlC14NErrInternal("creating namespaces list (c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000615 return (-1);
616 }
617
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000618 /* check all namespaces */
619 for(n = cur; n != NULL; n = n->parent) {
620 for(ns = n->nsDef; ns != NULL; ns = ns->next) {
621 tmp = xmlSearchNs(cur->doc, cur, ns->prefix);
622
623 if((tmp == ns) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
624 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
625 if(visible) {
626 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
627 }
628 if(!already_rendered) {
Aleksey Saninc57f9c12002-05-31 19:14:57 +0000629 xmlListInsert(list, ns);
630 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000631 if(xmlStrlen(ns->prefix) == 0) {
632 has_empty_ns = 1;
633 }
Aleksey Saninc57f9c12002-05-31 19:14:57 +0000634 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000635 }
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000636 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000637
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000638 /**
639 * if the first node is not the default namespace node (a node with no
640 * namespace URI and no local name), then generate a space followed by
641 * xmlns="" if and only if the following conditions are met:
642 * - the element E that owns the axis is in the node-set
643 * - the nearest ancestor element of E in the node-set has a default
644 * namespace node in the node-set (default namespace nodes always
645 * have non-empty values in XPath)
646 */
647 if(visible && !has_empty_ns) {
648 static xmlNs ns_default;
649
650 memset(&ns_default, 0, sizeof(ns_default));
651 if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
652 xmlC14NPrintNamespaces(&ns_default, ctx);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000653 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000654 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000655
656
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000657 /*
658 * print out all elements from list
659 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000660 xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000661
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000662 /*
663 * Cleanup
664 */
665 xmlListDelete(list);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000666 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000667}
668
Aleksey Sanindffd5c82002-05-31 04:24:13 +0000669
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000670/**
671 * xmlExcC14NProcessNamespacesAxis:
672 * @ctx: the C14N context
673 * @node: the current node
674 *
675 * Prints out exclusive canonical namespace axis of the current node to the
676 * buffer from C14N context as follows
677 *
678 * Exclusive XML Canonicalization
679 * http://www.w3.org/TR/xml-exc-c14n
680 *
681 * If the element node is in the XPath subset then output the node in
682 * accordance with Canonical XML except for namespace nodes which are
683 * rendered as follows:
684 *
685 * 1. Render each namespace node iff:
686 * * it is visibly utilized by the immediate parent element or one of
687 * its attributes, or is present in InclusiveNamespaces PrefixList, and
688 * * its prefix and value do not appear in ns_rendered. ns_rendered is
689 * obtained by popping the state stack in order to obtain a list of
690 * prefixes and their values which have already been rendered by
691 * an output ancestor of the namespace node's parent element.
692 * 2. Append the rendered namespace node to the list ns_rendered of namespace
693 * nodes rendered by output ancestors. Push ns_rendered on state stack and
694 * recurse.
695 * 3. After the recursion returns, pop thestate stack.
696 *
697 *
698 * Returns 0 on success or -1 on fail.
699 */
700static int
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000701xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000702{
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000703 xmlNsPtr ns;
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000704 xmlListPtr list;
705 xmlAttrPtr attr;
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000706 int already_rendered;
707 int has_empty_ns = 0;
708 int has_visibly_utilized_empty_ns = 0;
709 int has_empty_ns_in_inclusive_list = 0;
710
Daniel Veillard9ff88172002-03-11 09:15:32 +0000711 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000712 xmlC14NErrParam("processing namespaces axis (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000713 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000714 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000715
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000716 if(!ctx->exclusive) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000717 xmlC14NErrParam("processing namespaces axis (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000718 return (-1);
719
720 }
721
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000722 /*
723 * Create a sorted list to store element namespaces
724 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000725 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNsCompare);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000726 if (list == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000727 xmlC14NErrInternal("creating namespaces list (exc c14n)");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000728 return (-1);
729 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000730
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000731 /*
732 * process inclusive namespaces:
733 * All namespace nodes appearing on inclusive ns list are
734 * handled as provided in Canonical XML
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000735 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000736 if(ctx->inclusive_ns_prefixes != NULL) {
737 xmlChar *prefix;
738 int i;
739
740 for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) {
741 prefix = ctx->inclusive_ns_prefixes[i];
742 /*
743 * Special values for namespace with empty prefix
744 */
745 if (xmlStrEqual(prefix, BAD_CAST "#default")
746 || xmlStrEqual(prefix, BAD_CAST "")) {
747 prefix = NULL;
748 has_empty_ns_in_inclusive_list = 1;
749 }
750
751 ns = xmlSearchNs(cur->doc, cur, prefix);
752 if((ns != NULL) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
753 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
754 if(visible) {
755 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
756 }
757 if(!already_rendered) {
758 xmlListInsert(list, ns);
759 }
760 if(xmlStrlen(ns->prefix) == 0) {
761 has_empty_ns = 1;
762 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000763 }
Aleksey Saninc57f9c12002-05-31 19:14:57 +0000764 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000765 }
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000766
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000767 /* add node namespace */
768 if(cur->ns != NULL) {
769 ns = cur->ns;
770 } else {
771 ns = xmlSearchNs(cur->doc, cur, NULL);
772 has_visibly_utilized_empty_ns = 1;
773 }
774 if((ns != NULL) && !xmlC14NIsXmlNs(ns)) {
775 if(visible && xmlC14NIsVisible(ctx, ns, cur)) {
776 if(!xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, ns, ctx)) {
777 xmlListInsert(list, ns);
778 }
779 }
780 if(visible) {
781 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
782 }
783 if(xmlStrlen(ns->prefix) == 0) {
784 has_empty_ns = 1;
785 }
786 }
787
788
789 /* add attributes */
790 for(attr = cur->properties; attr != NULL; attr = attr->next) {
Daniel Veillard9ff88172002-03-11 09:15:32 +0000791 /*
Daniel Veillard2d347fa2002-03-17 10:34:11 +0000792 * we need to check that attribute is visible and has non
793 * default namespace (XML Namespaces: "default namespaces
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000794 * do not apply directly to attributes")
Daniel Veillard9ff88172002-03-11 09:15:32 +0000795 */
Aleksey Sanin2650df12005-06-06 17:16:50 +0000796 if((attr->ns != NULL) && !xmlC14NIsXmlNs(attr->ns) && xmlC14NIsVisible(ctx, attr, cur)) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000797 already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, attr->ns, ctx);
Aleksey Sanin64453bc2004-05-25 17:39:48 +0000798 xmlC14NVisibleNsStackAdd(ctx->ns_rendered, attr->ns, cur);
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000799 if(!already_rendered && visible) {
800 xmlListInsert(list, attr->ns);
801 }
802 if(xmlStrlen(attr->ns->prefix) == 0) {
803 has_empty_ns = 1;
804 }
Aleksey Saninb2eabc02005-10-28 03:15:18 +0000805 } else if((attr->ns != NULL) && (xmlStrlen(attr->ns->prefix) == 0) && (xmlStrlen(attr->ns->href) == 0)) {
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000806 has_visibly_utilized_empty_ns = 1;
807 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000808 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000809
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000810 /*
811 * Process xmlns=""
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000812 */
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000813 if(visible && has_visibly_utilized_empty_ns &&
814 !has_empty_ns && !has_empty_ns_in_inclusive_list) {
815 static xmlNs ns_default;
Daniel Veillard9ff88172002-03-11 09:15:32 +0000816
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000817 memset(&ns_default, 0, sizeof(ns_default));
818
819 already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default, ctx);
820 if(!already_rendered) {
821 xmlC14NPrintNamespaces(&ns_default, ctx);
822 }
823 } else if(visible && !has_empty_ns && has_empty_ns_in_inclusive_list) {
824 static xmlNs ns_default;
825
826 memset(&ns_default, 0, sizeof(ns_default));
827 if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
828 xmlC14NPrintNamespaces(&ns_default, ctx);
829 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000830 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000831
Aleksey Sanin2c135a12002-08-01 06:31:50 +0000832
833
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000834 /*
835 * print out all elements from list
836 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000837 xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000838
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000839 /*
840 * Cleanup
841 */
842 xmlListDelete(list);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000843 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000844}
845
846
847/**
848 * xmlC14NAttrsCompare:
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +0000849 * @attr1: the pointer tls o first attr
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000850 * @attr2: the pointer to second attr
851 *
852 * Prints the given attribute to the output buffer from C14N context.
853 *
854 * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2.
855 */
856static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000857xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2)
858{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000859 int ret = 0;
860
861 /*
862 * Simple cases
863 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000864 if (attr1 == attr2)
865 return (0);
866 if (attr1 == NULL)
867 return (-1);
868 if (attr2 == NULL)
869 return (1);
870 if (attr1->ns == attr2->ns) {
871 return (xmlStrcmp(attr1->name, attr2->name));
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000872 }
873
874 /*
875 * Attributes in the default namespace are first
876 * because the default namespace is not applied to
877 * unqualified attributes
878 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000879 if (attr1->ns == NULL)
880 return (-1);
881 if (attr2->ns == NULL)
882 return (1);
883 if (attr1->ns->prefix == NULL)
884 return (-1);
885 if (attr2->ns->prefix == NULL)
886 return (1);
887
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000888 ret = xmlStrcmp(attr1->ns->href, attr2->ns->href);
Daniel Veillard9ff88172002-03-11 09:15:32 +0000889 if (ret == 0) {
890 ret = xmlStrcmp(attr1->name, attr2->name);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000891 }
Daniel Veillard9ff88172002-03-11 09:15:32 +0000892 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000893}
894
895
896/**
897 * xmlC14NPrintAttrs:
898 * @attr: the pointer to attr
899 * @ctx: the C14N context
900 *
901 * Prints out canonical attribute urrent node to the
902 * buffer from C14N context as follows
903 *
904 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
905 *
906 * Returns 1 on success or 0 on fail.
907 */
908static int
Daniel Veillard9ff88172002-03-11 09:15:32 +0000909xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr ctx)
910{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000911 xmlChar *value;
912 xmlChar *buffer;
913
Daniel Veillard9ff88172002-03-11 09:15:32 +0000914 if ((attr == NULL) || (ctx == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000915 xmlC14NErrParam("writing attributes");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000916 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000917 }
918
919 xmlOutputBufferWriteString(ctx->buf, " ");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000920 if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) {
921 xmlOutputBufferWriteString(ctx->buf,
922 (const char *) attr->ns->prefix);
923 xmlOutputBufferWriteString(ctx->buf, ":");
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000924 }
925 xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name);
926 xmlOutputBufferWriteString(ctx->buf, "=\"");
927
928 value = xmlNodeListGetString(attr->doc, attr->children, 1);
929 /* todo: should we log an error if value==NULL ? */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000930 if (value != NULL) {
931 buffer = xmlC11NNormalizeAttr(value);
932 xmlFree(value);
933 if (buffer != NULL) {
934 xmlOutputBufferWriteString(ctx->buf, (const char *) buffer);
935 xmlFree(buffer);
936 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000937 xmlC14NErrInternal("normalizing attributes axis");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000938 return (0);
939 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000940 }
941 xmlOutputBufferWriteString(ctx->buf, "\"");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000942 return (1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000943}
944
945/**
946 * xmlC14NProcessAttrsAxis:
947 * @ctx: the C14N context
948 * @cur: the current node
Aleksey Sanin3ea201c2005-06-07 16:53:57 +0000949 * @parent_visible: the visibility of parent node
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000950 *
951 * Prints out canonical attribute axis of the current node to the
952 * buffer from C14N context as follows
953 *
954 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
955 *
956 * Attribute Axis
957 * In lexicographic order (ascending), process each node that
958 * is in the element's attribute axis and in the node-set.
959 *
960 * The processing of an element node E MUST be modified slightly
961 * when an XPath node-set is given as input and the element's
962 * parent is omitted from the node-set.
963 *
964 *
965 * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
966 *
967 * Canonical XML applied to a document subset requires the search of the
968 * ancestor nodes of each orphan element node for attributes in the xml
969 * namespace, such as xml:lang and xml:space. These are copied into the
970 * element node except if a declaration of the same attribute is already
971 * in the attribute axis of the element (whether or not it is included in
972 * the document subset). This search and copying are omitted from the
973 * Exclusive XML Canonicalization method.
974 *
975 * Returns 0 on success or -1 on fail.
976 */
977static int
Aleksey Sanin3ea201c2005-06-07 16:53:57 +0000978xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible)
Daniel Veillard9ff88172002-03-11 09:15:32 +0000979{
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000980 xmlAttrPtr attr;
981 xmlListPtr list;
Daniel Veillard9ff88172002-03-11 09:15:32 +0000982
983 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000984 xmlC14NErrParam("processing attributes axis");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000985 return (-1);
986 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000987
988 /*
989 * Create a sorted list to store element attributes
990 */
Daniel Veillard9ff88172002-03-11 09:15:32 +0000991 list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare);
992 if (list == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +0000993 xmlC14NErrInternal("creating attributes list");
Daniel Veillard9ff88172002-03-11 09:15:32 +0000994 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +0000995 }
996
997 /*
998 * Add all visible attributes from current node.
999 */
1000 attr = cur->properties;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001001 while (attr != NULL) {
1002 /* check that attribute is visible */
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001003 if (xmlC14NIsVisible(ctx, attr, cur)) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001004 xmlListInsert(list, attr);
1005 }
1006 attr = attr->next;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001007 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001008
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001009 /*
1010 * include attributes in "xml" namespace defined in ancestors
1011 * (only for non-exclusive XML Canonicalization)
1012 */
Aleksey Sanin3ea201c2005-06-07 16:53:57 +00001013 if (parent_visible && (!ctx->exclusive) && (cur->parent != NULL)
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001014 && (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent))) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001015 /*
Daniel Veillard9ff88172002-03-11 09:15:32 +00001016 * If XPath node-set is not specified then the parent is always
1017 * visible!
1018 */
1019 cur = cur->parent;
1020 while (cur != NULL) {
1021 attr = cur->properties;
1022 while (attr != NULL) {
1023 if ((attr->ns != NULL)
1024 && (xmlStrEqual(attr->ns->prefix, BAD_CAST "xml"))) {
1025 if (xmlListSearch(list, attr) == NULL) {
1026 xmlListInsert(list, attr);
1027 }
1028 }
1029 attr = attr->next;
1030 }
1031 cur = cur->parent;
1032 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001033 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001034
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001035 /*
1036 * print out all elements from list
1037 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001038 xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001039
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001040 /*
1041 * Cleanup
1042 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001043 xmlListDelete(list);
1044 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001045}
1046
1047/**
1048 * xmlC14NCheckForRelativeNamespaces:
1049 * @ctx: the C14N context
1050 * @cur: the current element node
1051 *
1052 * Checks that current element node has no relative namespaces defined
1053 *
1054 * Returns 0 if the node has no relative namespaces or -1 otherwise.
1055 */
1056static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001057xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, xmlNodePtr cur)
1058{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001059 xmlNsPtr ns;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001060
Daniel Veillard9ff88172002-03-11 09:15:32 +00001061 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001062 xmlC14NErrParam("checking for relative namespaces");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001063 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001064 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001065
1066 ns = cur->nsDef;
1067 while (ns != NULL) {
1068 if (xmlStrlen(ns->href) > 0) {
1069 xmlURIPtr uri;
1070
1071 uri = xmlParseURI((const char *) ns->href);
1072 if (uri == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001073 xmlC14NErrInternal("parsing namespace uri");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001074 return (-1);
1075 }
1076 if (xmlStrlen((const xmlChar *) uri->scheme) == 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001077 xmlC14NErrRelativeNamespace(uri->scheme);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001078 xmlFreeURI(uri);
1079 return (-1);
1080 }
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001081 if ((xmlStrcasecmp((const xmlChar *) uri->scheme, BAD_CAST "urn") != 0)
1082 && (xmlStrcasecmp((const xmlChar *) uri->scheme, BAD_CAST "dav") !=0)
Daniel Veillard9ff88172002-03-11 09:15:32 +00001083 && (xmlStrlen((const xmlChar *) uri->server) == 0)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001084 xmlC14NErrRelativeNamespace(uri->scheme);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001085 xmlFreeURI(uri);
1086 return (-1);
1087 }
1088 xmlFreeURI(uri);
1089 }
1090 ns = ns->next;
1091 }
1092 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001093}
1094
1095/**
1096 * xmlC14NProcessElementNode:
1097 * @ctx: the pointer to C14N context object
1098 * @cur: the node to process
1099 *
1100 * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
1101 *
1102 * Element Nodes
1103 * If the element is not in the node-set, then the result is obtained
1104 * by processing the namespace axis, then the attribute axis, then
1105 * processing the child nodes of the element that are in the node-set
1106 * (in document order). If the element is in the node-set, then the result
1107 * is an open angle bracket (<), the element QName, the result of
1108 * processing the namespace axis, the result of processing the attribute
1109 * axis, a close angle bracket (>), the result of processing the child
1110 * nodes of the element that are in the node-set (in document order), an
1111 * open angle bracket, a forward slash (/), the element QName, and a close
1112 * angle bracket.
1113 *
1114 * Returns non-negative value on success or negative value on fail
1115 */
1116static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001117xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible)
1118{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001119 int ret;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001120 xmlC14NVisibleNsStack state;
Daniel Veillard6f293b12002-03-15 09:42:33 +00001121 int parent_is_doc = 0;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001122
Daniel Veillard9ff88172002-03-11 09:15:32 +00001123 if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001124 xmlC14NErrParam("processing element node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001125 return (-1);
1126 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001127
1128 /*
1129 * Check relative relative namespaces:
1130 * implementations of XML canonicalization MUST report an operation
1131 * failure on documents containing relative namespace URIs.
1132 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001133 if (xmlC14NCheckForRelativeNamespaces(ctx, cur) < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001134 xmlC14NErrInternal("checking for relative namespaces");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001135 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001136 }
1137
1138
1139 /*
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001140 * Save ns_rendered stack position
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001141 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001142 xmlC14NVisibleNsStackSave(ctx->ns_rendered, &state);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001143
Daniel Veillard6f293b12002-03-15 09:42:33 +00001144 if (visible) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001145 if (ctx->parent_is_doc) {
Daniel Veillard6f293b12002-03-15 09:42:33 +00001146 /* save this flag into the stack */
1147 parent_is_doc = ctx->parent_is_doc;
1148 ctx->parent_is_doc = 0;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001149 ctx->pos = XMLC14N_INSIDE_DOCUMENT_ELEMENT;
1150 }
1151 xmlOutputBufferWriteString(ctx->buf, "<");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001152
Daniel Veillard9ff88172002-03-11 09:15:32 +00001153 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) {
1154 xmlOutputBufferWriteString(ctx->buf,
1155 (const char *) cur->ns->prefix);
1156 xmlOutputBufferWriteString(ctx->buf, ":");
1157 }
1158 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001159 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001160
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001161 if (!ctx->exclusive) {
1162 ret = xmlC14NProcessNamespacesAxis(ctx, cur, visible);
1163 } else {
1164 ret = xmlExcC14NProcessNamespacesAxis(ctx, cur, visible);
1165 }
1166 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001167 xmlC14NErrInternal("processing namespaces axis");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001168 return (-1);
1169 }
1170 /* todo: shouldn't this go to "visible only"? */
1171 if(visible) {
1172 xmlC14NVisibleNsStackShift(ctx->ns_rendered);
1173 }
1174
Aleksey Sanin3ea201c2005-06-07 16:53:57 +00001175 ret = xmlC14NProcessAttrsAxis(ctx, cur, visible);
1176 if (ret < 0) {
1177 xmlC14NErrInternal("processing attributes axis");
1178 return (-1);
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001179 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001180
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001181 if (visible) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001182 xmlOutputBufferWriteString(ctx->buf, ">");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001183 }
1184 if (cur->children != NULL) {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001185 ret = xmlC14NProcessNodeList(ctx, cur->children);
1186 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001187 xmlC14NErrInternal("processing childrens list");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001188 return (-1);
1189 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001190 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001191 if (visible) {
1192 xmlOutputBufferWriteString(ctx->buf, "</");
1193 if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) {
1194 xmlOutputBufferWriteString(ctx->buf,
1195 (const char *) cur->ns->prefix);
1196 xmlOutputBufferWriteString(ctx->buf, ":");
1197 }
1198 xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name);
1199 xmlOutputBufferWriteString(ctx->buf, ">");
Daniel Veillard6f293b12002-03-15 09:42:33 +00001200 if (parent_is_doc) {
1201 /* restore this flag from the stack for next node */
1202 ctx->parent_is_doc = parent_is_doc;
1203 ctx->pos = XMLC14N_AFTER_DOCUMENT_ELEMENT;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001204 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001205 }
1206
1207 /*
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001208 * Restore ns_rendered stack position
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001209 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001210 xmlC14NVisibleNsStackRestore(ctx->ns_rendered, &state);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001211 return (0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001212}
1213
1214/**
1215 * xmlC14NProcessNode:
1216 * @ctx: the pointer to C14N context object
1217 * @cur: the node to process
1218 *
1219 * Processes the given node
1220 *
1221 * Returns non-negative value on success or negative value on fail
1222 */
1223static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001224xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
1225{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001226 int ret = 0;
1227 int visible;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001228
1229 if ((ctx == NULL) || (cur == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001230 xmlC14NErrParam("processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001231 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001232 }
1233
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001234 visible = xmlC14NIsVisible(ctx, cur, cur->parent);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001235 switch (cur->type) {
1236 case XML_ELEMENT_NODE:
1237 ret = xmlC14NProcessElementNode(ctx, cur, visible);
1238 break;
1239 case XML_CDATA_SECTION_NODE:
1240 case XML_TEXT_NODE:
1241 /*
1242 * Text Nodes
1243 * the string value, except all ampersands are replaced
1244 * by &amp;, all open angle brackets (<) are replaced by &lt;, all closing
1245 * angle brackets (>) are replaced by &gt;, and all #xD characters are
1246 * replaced by &#xD;.
1247 */
1248 /* cdata sections are processed as text nodes */
1249 /* todo: verify that cdata sections are included in XPath nodes set */
1250 if ((visible) && (cur->content != NULL)) {
1251 xmlChar *buffer;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001252
Daniel Veillard9ff88172002-03-11 09:15:32 +00001253 buffer = xmlC11NNormalizeText(cur->content);
1254 if (buffer != NULL) {
1255 xmlOutputBufferWriteString(ctx->buf,
1256 (const char *) buffer);
1257 xmlFree(buffer);
1258 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001259 xmlC14NErrInternal("normalizing text node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001260 return (-1);
1261 }
1262 }
1263 break;
1264 case XML_PI_NODE:
1265 /*
1266 * Processing Instruction (PI) Nodes-
1267 * The opening PI symbol (<?), the PI target name of the node,
1268 * a leading space and the string value if it is not empty, and
1269 * the closing PI symbol (?>). If the string value is empty,
1270 * then the leading space is not added. Also, a trailing #xA is
1271 * rendered after the closing PI symbol for PI children of the
1272 * root node with a lesser document order than the document
1273 * element, and a leading #xA is rendered before the opening PI
1274 * symbol of PI children of the root node with a greater document
1275 * order than the document element.
1276 */
1277 if (visible) {
1278 if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) {
1279 xmlOutputBufferWriteString(ctx->buf, "\x0A<?");
1280 } else {
1281 xmlOutputBufferWriteString(ctx->buf, "<?");
1282 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001283
Daniel Veillard9ff88172002-03-11 09:15:32 +00001284 xmlOutputBufferWriteString(ctx->buf,
1285 (const char *) cur->name);
1286 if ((cur->content != NULL) && (*(cur->content) != '\0')) {
1287 xmlChar *buffer;
1288
1289 xmlOutputBufferWriteString(ctx->buf, " ");
1290
1291 /* todo: do we need to normalize pi? */
1292 buffer = xmlC11NNormalizePI(cur->content);
1293 if (buffer != NULL) {
1294 xmlOutputBufferWriteString(ctx->buf,
1295 (const char *) buffer);
1296 xmlFree(buffer);
1297 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001298 xmlC14NErrInternal("normalizing pi node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001299 return (-1);
1300 }
1301 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001302
Daniel Veillard9ff88172002-03-11 09:15:32 +00001303 if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) {
1304 xmlOutputBufferWriteString(ctx->buf, "?>\x0A");
1305 } else {
1306 xmlOutputBufferWriteString(ctx->buf, "?>");
1307 }
1308 }
1309 break;
1310 case XML_COMMENT_NODE:
1311 /*
1312 * Comment Nodes
1313 * Nothing if generating canonical XML without comments. For
1314 * canonical XML with comments, generate the opening comment
1315 * symbol (<!--), the string value of the node, and the
1316 * closing comment symbol (-->). Also, a trailing #xA is rendered
1317 * after the closing comment symbol for comment children of the
1318 * root node with a lesser document order than the document
1319 * element, and a leading #xA is rendered before the opening
1320 * comment symbol of comment children of the root node with a
1321 * greater document order than the document element. (Comment
1322 * children of the root node represent comments outside of the
1323 * top-level document element and outside of the document type
1324 * declaration).
1325 */
1326 if (visible && ctx->with_comments) {
1327 if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) {
1328 xmlOutputBufferWriteString(ctx->buf, "\x0A<!--");
1329 } else {
1330 xmlOutputBufferWriteString(ctx->buf, "<!--");
1331 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001332
Daniel Veillard9ff88172002-03-11 09:15:32 +00001333 if (cur->content != NULL) {
1334 xmlChar *buffer;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001335
Daniel Veillard9ff88172002-03-11 09:15:32 +00001336 /* todo: do we need to normalize comment? */
1337 buffer = xmlC11NNormalizeComment(cur->content);
1338 if (buffer != NULL) {
1339 xmlOutputBufferWriteString(ctx->buf,
1340 (const char *) buffer);
1341 xmlFree(buffer);
1342 } else {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001343 xmlC14NErrInternal("normalizing comment node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001344 return (-1);
1345 }
1346 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001347
Daniel Veillard9ff88172002-03-11 09:15:32 +00001348 if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) {
1349 xmlOutputBufferWriteString(ctx->buf, "-->\x0A");
1350 } else {
1351 xmlOutputBufferWriteString(ctx->buf, "-->");
1352 }
1353 }
1354 break;
1355 case XML_DOCUMENT_NODE:
1356 case XML_DOCUMENT_FRAG_NODE: /* should be processed as document? */
Daniel Veillard1840ef02002-03-21 08:05:23 +00001357#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard9ff88172002-03-11 09:15:32 +00001358 case XML_DOCB_DOCUMENT_NODE: /* should be processed as document? */
Daniel Veillard1840ef02002-03-21 08:05:23 +00001359#endif
1360#ifdef LIBXML_HTML_ENABLED
Daniel Veillard9ff88172002-03-11 09:15:32 +00001361 case XML_HTML_DOCUMENT_NODE: /* should be processed as document? */
Daniel Veillard1840ef02002-03-21 08:05:23 +00001362#endif
Daniel Veillard9ff88172002-03-11 09:15:32 +00001363 if (cur->children != NULL) {
1364 ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT;
1365 ctx->parent_is_doc = 1;
1366 ret = xmlC14NProcessNodeList(ctx, cur->children);
1367 }
1368 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001369
Daniel Veillard9ff88172002-03-11 09:15:32 +00001370 case XML_ATTRIBUTE_NODE:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001371 xmlC14NErrInvalidNode("XML_ATTRIBUTE_NODE", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001372 return (-1);
1373 case XML_NAMESPACE_DECL:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001374 xmlC14NErrInvalidNode("XML_NAMESPACE_DECL", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001375 return (-1);
1376 case XML_ENTITY_REF_NODE:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001377 xmlC14NErrInvalidNode("XML_ENTITY_REF_NODE", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001378 return (-1);
1379 case XML_ENTITY_NODE:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001380 xmlC14NErrInvalidNode("XML_ENTITY_NODE", "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001381 return (-1);
1382
1383 case XML_DOCUMENT_TYPE_NODE:
1384 case XML_NOTATION_NODE:
1385 case XML_DTD_NODE:
1386 case XML_ELEMENT_DECL:
1387 case XML_ATTRIBUTE_DECL:
1388 case XML_ENTITY_DECL:
Daniel Veillard1840ef02002-03-21 08:05:23 +00001389#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard9ff88172002-03-11 09:15:32 +00001390 case XML_XINCLUDE_START:
1391 case XML_XINCLUDE_END:
Daniel Veillard1840ef02002-03-21 08:05:23 +00001392#endif
Daniel Veillard9ff88172002-03-11 09:15:32 +00001393 /*
1394 * should be ignored according to "W3C Canonical XML"
1395 */
1396 break;
1397 default:
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001398 xmlC14NErrUnknownNode(cur->type, "processing node");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001399 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001400 }
1401
Daniel Veillard9ff88172002-03-11 09:15:32 +00001402 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001403}
1404
1405/**
1406 * xmlC14NProcessNodeList:
1407 * @ctx: the pointer to C14N context object
1408 * @cur: the node to start from
1409 *
1410 * Processes all nodes in the row starting from cur.
1411 *
1412 * Returns non-negative value on success or negative value on fail
1413 */
1414static int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001415xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur)
1416{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001417 int ret;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001418
1419 if (ctx == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001420 xmlC14NErrParam("processing node list");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001421 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001422 }
1423
Daniel Veillard9ff88172002-03-11 09:15:32 +00001424 for (ret = 0; cur != NULL && ret >= 0; cur = cur->next) {
1425 ret = xmlC14NProcessNode(ctx, cur);
1426 }
1427 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001428}
1429
1430
1431/**
1432 * xmlC14NFreeCtx:
1433 * @ctx: the pointer to C14N context object
1434 *
1435 * Cleanups the C14N context object.
1436 */
1437
1438static void
Daniel Veillard9ff88172002-03-11 09:15:32 +00001439xmlC14NFreeCtx(xmlC14NCtxPtr ctx)
1440{
1441 if (ctx == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001442 xmlC14NErrParam("freeing context");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001443 return;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001444 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001445
1446 if (ctx->ns_rendered != NULL) {
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001447 xmlC14NVisibleNsStackDestroy(ctx->ns_rendered);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001448 }
1449 xmlFree(ctx);
1450}
1451
1452/**
1453 * xmlC14NNewCtx:
1454 * @doc: the XML document for canonization
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001455 * @is_visible_callback:the function to use to determine is node visible
1456 * or not
1457 * @user_data: the first parameter for @is_visible_callback function
1458 * (in most cases, it is nodes set)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001459 * @inclusive_ns_prefixe the list of inclusive namespace prefixes
1460 * ended with a NULL or NULL if there is no
1461 * inclusive namespaces (only for exclusive
1462 * canonicalization)
1463 * @with_comments: include comments in the result (!=0) or not (==0)
1464 * @buf: the output buffer to store canonical XML; this
1465 * buffer MUST have encoder==NULL because C14N requires
1466 * UTF-8 output
1467 *
1468 * Creates new C14N context object to store C14N parameters.
1469 *
1470 * Returns pointer to newly created object (success) or NULL (fail)
1471 */
1472static xmlC14NCtxPtr
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001473xmlC14NNewCtx(xmlDocPtr doc,
1474 xmlC14NIsVisibleCallback is_visible_callback, void* user_data,
Daniel Veillard9ff88172002-03-11 09:15:32 +00001475 int exclusive, xmlChar ** inclusive_ns_prefixes,
1476 int with_comments, xmlOutputBufferPtr buf)
1477{
Daniel Veillard659e71e2003-10-10 14:10:40 +00001478 xmlC14NCtxPtr ctx = NULL;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001479
Daniel Veillard9ff88172002-03-11 09:15:32 +00001480 if ((doc == NULL) || (buf == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001481 xmlC14NErrParam("creating new context");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001482 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001483 }
1484
1485 /*
1486 * Validate the encoding output buffer encoding
1487 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001488 if (buf->encoder != NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001489 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
1490"xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001491 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001492 }
1493
1494 /*
1495 * Validate the XML document encoding value, if provided.
1496 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001497 if (doc->charset != XML_CHAR_ENCODING_UTF8) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001498 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
1499 "xmlC14NNewCtx: source document not in UTF8\n");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001500 return (NULL);
1501 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001502
1503 /*
1504 * Allocate a new xmlC14NCtxPtr and fill the fields.
1505 */
1506 ctx = (xmlC14NCtxPtr) xmlMalloc(sizeof(xmlC14NCtx));
1507 if (ctx == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001508 xmlC14NErrMemory("creating context");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001509 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001510 }
1511 memset(ctx, 0, sizeof(xmlC14NCtx));
1512
1513 /*
1514 * initialize C14N context
Daniel Veillard9ff88172002-03-11 09:15:32 +00001515 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001516 ctx->doc = doc;
1517 ctx->with_comments = with_comments;
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001518 ctx->is_visible_callback = is_visible_callback;
1519 ctx->user_data = user_data;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001520 ctx->buf = buf;
1521 ctx->parent_is_doc = 1;
1522 ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT;
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001523 ctx->ns_rendered = xmlC14NVisibleNsStackCreate();
1524
1525 if(ctx->ns_rendered == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001526 xmlC14NErr(ctx, (xmlNodePtr) doc, XML_C14N_CREATE_STACK,
1527 "xmlC14NNewCtx: xmlC14NVisibleNsStackCreate failed\n");
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001528 xmlC14NFreeCtx(ctx);
1529 return (NULL);
1530 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001531
1532 /*
1533 * Set "exclusive" flag, create a nodes set for namespaces
1534 * stack and remember list of incluseve prefixes
1535 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001536 if (exclusive) {
1537 ctx->exclusive = 1;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001538 ctx->inclusive_ns_prefixes = inclusive_ns_prefixes;
1539 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001540 return (ctx);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001541}
1542
1543/**
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001544 * xmlC14NExecute:
1545 * @doc: the XML document for canonization
1546 * @is_visible_callback:the function to use to determine is node visible
1547 * or not
1548 * @user_data: the first parameter for @is_visible_callback function
1549 * (in most cases, it is nodes set)
1550 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization;
1551 * otherwise - exclusive canonicalization)
1552 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
1553 * ended with a NULL or NULL if there is no
1554 * inclusive namespaces (only for exclusive
1555 * canonicalization, ignored otherwise)
1556 * @with_comments: include comments in the result (!=0) or not (==0)
1557 * @buf: the output buffer to store canonical XML; this
1558 * buffer MUST have encoder==NULL because C14N requires
1559 * UTF-8 output
1560 *
1561 * Dumps the canonized image of given XML document into the provided buffer.
1562 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1563 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1564 *
1565 * Returns non-negative value on success or a negative value on fail
1566 */
1567int
1568xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback,
1569 void* user_data, int exclusive, xmlChar **inclusive_ns_prefixes,
1570 int with_comments, xmlOutputBufferPtr buf) {
1571
1572 xmlC14NCtxPtr ctx;
1573 int ret;
1574
1575 if ((buf == NULL) || (doc == NULL)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001576 xmlC14NErrParam("executing c14n");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001577 return (-1);
1578 }
1579
1580 /*
1581 * Validate the encoding output buffer encoding
1582 */
1583 if (buf->encoder != NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001584 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_REQUIRES_UTF8,
1585"xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001586 return (-1);
1587 }
1588
1589 ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data,
1590 exclusive, inclusive_ns_prefixes,
1591 with_comments, buf);
1592 if (ctx == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001593 xmlC14NErr(NULL, (xmlNodePtr) doc, XML_C14N_CREATE_CTXT,
1594 "xmlC14NExecute: unable to create C14N context\n");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001595 return (-1);
1596 }
1597
1598
1599
1600 /*
1601 * Root Node
1602 * The root node is the parent of the top-level document element. The
1603 * result of processing each of its child nodes that is in the node-set
1604 * in document order. The root node does not generate a byte order mark,
1605 * XML declaration, nor anything from within the document type
1606 * declaration.
1607 */
1608 if (doc->children != NULL) {
1609 ret = xmlC14NProcessNodeList(ctx, doc->children);
1610 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001611 xmlC14NErrInternal("processing docs children list");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001612 xmlC14NFreeCtx(ctx);
1613 return (-1);
1614 }
1615 }
1616
1617 /*
1618 * Flush buffer to get number of bytes written
1619 */
1620 ret = xmlOutputBufferFlush(buf);
1621 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001622 xmlC14NErrInternal("flushing output buffer");
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001623 xmlC14NFreeCtx(ctx);
1624 return (-1);
1625 }
1626
1627 /*
1628 * Cleanup
1629 */
1630 xmlC14NFreeCtx(ctx);
1631 return (ret);
1632}
1633
1634/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001635 * xmlC14NDocSaveTo:
1636 * @doc: the XML document for canonization
1637 * @nodes: the nodes set to be included in the canonized image
1638 * or NULL if all document nodes should be included
1639 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization;
1640 * otherwise - exclusive canonicalization)
Daniel Veillarddb1bdba2002-03-09 14:13:11 +00001641 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001642 * ended with a NULL or NULL if there is no
1643 * inclusive namespaces (only for exclusive
1644 * canonicalization, ignored otherwise)
1645 * @with_comments: include comments in the result (!=0) or not (==0)
1646 * @buf: the output buffer to store canonical XML; this
1647 * buffer MUST have encoder==NULL because C14N requires
1648 * UTF-8 output
1649 *
1650 * Dumps the canonized image of given XML document into the provided buffer.
1651 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1652 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1653 *
1654 * Returns non-negative value on success or a negative value on fail
1655 */
1656int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001657xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes,
1658 int exclusive, xmlChar ** inclusive_ns_prefixes,
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001659 int with_comments, xmlOutputBufferPtr buf) {
1660 return(xmlC14NExecute(doc,
1661 (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset,
1662 nodes,
1663 exclusive,
1664 inclusive_ns_prefixes,
1665 with_comments,
1666 buf));
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001667}
1668
Aleksey Sanin2c135a12002-08-01 06:31:50 +00001669
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001670/**
1671 * xmlC14NDocDumpMemory:
1672 * @doc: the XML document for canonization
1673 * @nodes: the nodes set to be included in the canonized image
1674 * or NULL if all document nodes should be included
1675 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization;
1676 * otherwise - exclusive canonicalization)
Daniel Veillarddb1bdba2002-03-09 14:13:11 +00001677 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001678 * ended with a NULL or NULL if there is no
1679 * inclusive namespaces (only for exclusive
1680 * canonicalization, ignored otherwise)
1681 * @with_comments: include comments in the result (!=0) or not (==0)
1682 * @doc_txt_ptr: the memory pointer for allocated canonical XML text;
1683 * the caller of this functions is responsible for calling
1684 * xmlFree() to free allocated memory
1685 *
1686 * Dumps the canonized image of given XML document into memory.
1687 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1688 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1689 *
1690 * Returns the number of bytes written on success or a negative value on fail
1691 */
1692int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001693xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes,
1694 int exclusive, xmlChar ** inclusive_ns_prefixes,
1695 int with_comments, xmlChar ** doc_txt_ptr)
1696{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001697 int ret;
1698 xmlOutputBufferPtr buf;
1699
1700 if (doc_txt_ptr == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001701 xmlC14NErrParam("dumping doc to memory");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001702 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001703 }
1704
1705 *doc_txt_ptr = NULL;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001706
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001707 /*
1708 * create memory buffer with UTF8 (default) encoding
1709 */
1710 buf = xmlAllocOutputBuffer(NULL);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001711 if (buf == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001712 xmlC14NErrMemory("creating output buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001713 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001714 }
1715
1716 /*
1717 * canonize document and write to buffer
1718 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001719 ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes,
1720 with_comments, buf);
1721 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001722 xmlC14NErrInternal("saving doc to output buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001723 (void) xmlOutputBufferClose(buf);
1724 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001725 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001726
Daniel Veillard9ff88172002-03-11 09:15:32 +00001727 ret = buf->buffer->use;
1728 if (ret > 0) {
1729 *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret);
1730 }
1731 (void) xmlOutputBufferClose(buf);
1732
1733 if ((*doc_txt_ptr == NULL) && (ret > 0)) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001734 xmlC14NErrMemory("coping canonicanized document");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001735 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001736 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001737 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001738}
1739
1740/**
1741 * xmlC14NDocSave:
1742 * @doc: the XML document for canonization
1743 * @nodes: the nodes set to be included in the canonized image
1744 * or NULL if all document nodes should be included
1745 * @exclusive: the exclusive flag (0 - non-exclusive canonicalization;
1746 * otherwise - exclusive canonicalization)
Daniel Veillarddb1bdba2002-03-09 14:13:11 +00001747 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001748 * ended with a NULL or NULL if there is no
1749 * inclusive namespaces (only for exclusive
1750 * canonicalization, ignored otherwise)
1751 * @with_comments: include comments in the result (!=0) or not (==0)
1752 * @filename: the filename to store canonical XML image
1753 * @compression: the compression level (zlib requred):
1754 * -1 - libxml default,
1755 * 0 - uncompressed,
1756 * >0 - compression level
1757 *
1758 * Dumps the canonized image of given XML document into the file.
1759 * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
1760 * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
1761 *
1762 * Returns the number of bytes written success or a negative value on fail
1763 */
1764int
Daniel Veillard9ff88172002-03-11 09:15:32 +00001765xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
1766 int exclusive, xmlChar ** inclusive_ns_prefixes,
1767 int with_comments, const char *filename, int compression)
1768{
1769 xmlOutputBufferPtr buf;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001770 int ret;
1771
Daniel Veillard9ff88172002-03-11 09:15:32 +00001772 if (filename == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001773 xmlC14NErrParam("saving doc");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001774 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001775 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001776#ifdef HAVE_ZLIB_H
Daniel Veillard9ff88172002-03-11 09:15:32 +00001777 if (compression < 0)
1778 compression = xmlGetCompressMode();
1779#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001780
1781 /*
1782 * save the content to a temp buffer, use default UTF8 encoding.
1783 */
1784 buf = xmlOutputBufferCreateFilename(filename, NULL, compression);
1785 if (buf == NULL) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001786 xmlC14NErrInternal("creating temporary filename");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001787 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001788 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001789
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001790 /*
1791 * canonize document and write to buffer
1792 */
Daniel Veillard9ff88172002-03-11 09:15:32 +00001793 ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes,
1794 with_comments, buf);
1795 if (ret < 0) {
Aleksey Sanin9e75e9f2005-03-20 19:16:47 +00001796 xmlC14NErrInternal("cannicanize document to buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001797 (void) xmlOutputBufferClose(buf);
1798 return (-1);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001799 }
Daniel Veillard9ff88172002-03-11 09:15:32 +00001800
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001801 /*
1802 * get the numbers of bytes written
1803 */
1804 ret = xmlOutputBufferClose(buf);
Daniel Veillard9ff88172002-03-11 09:15:32 +00001805 return (ret);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001806}
1807
1808
1809
1810/*
1811 * Macro used to grow the current buffer.
1812 */
1813#define growBufferReentrant() { \
1814 buffer_size *= 2; \
1815 buffer = (xmlChar *) \
1816 xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \
1817 if (buffer == NULL) { \
Daniel Veillardd96cce12003-10-10 12:30:37 +00001818 xmlC14NErrMemory("growing buffer"); \
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001819 return(NULL); \
1820 } \
1821}
1822
1823/**
1824 * xmlC11NNormalizeString:
1825 * @input: the input string
1826 * @mode: the normalization mode (attribute, comment, PI or text)
1827 *
1828 * Converts a string to a canonical (normalized) format. The code is stolen
1829 * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A
1830 * and the @mode parameter
1831 *
1832 * Returns a normalized string (caller is responsible for calling xmlFree())
1833 * or NULL if an error occurs
1834 */
1835static xmlChar *
Daniel Veillard9ff88172002-03-11 09:15:32 +00001836xmlC11NNormalizeString(const xmlChar * input,
1837 xmlC14NNormalizationMode mode)
1838{
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001839 const xmlChar *cur = input;
1840 xmlChar *buffer = NULL;
1841 xmlChar *out = NULL;
1842 int buffer_size = 0;
1843
Daniel Veillard9ff88172002-03-11 09:15:32 +00001844 if (input == NULL)
1845 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001846
1847 /*
1848 * allocate an translation buffer.
1849 */
1850 buffer_size = 1000;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001851 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001852 if (buffer == NULL) {
Daniel Veillardd96cce12003-10-10 12:30:37 +00001853 xmlC14NErrMemory("allocating buffer");
Daniel Veillard9ff88172002-03-11 09:15:32 +00001854 return (NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001855 }
1856 out = buffer;
1857
1858 while (*cur != '\0') {
Daniel Veillard9ff88172002-03-11 09:15:32 +00001859 if ((out - buffer) > (buffer_size - 10)) {
1860 int indx = out - buffer;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001861
Daniel Veillard9ff88172002-03-11 09:15:32 +00001862 growBufferReentrant();
1863 out = &buffer[indx];
1864 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001865
Daniel Veillard9ff88172002-03-11 09:15:32 +00001866 if ((*cur == '<') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
1867 (mode == XMLC14N_NORMALIZE_TEXT))) {
1868 *out++ = '&';
1869 *out++ = 'l';
1870 *out++ = 't';
1871 *out++ = ';';
1872 } else if ((*cur == '>') && (mode == XMLC14N_NORMALIZE_TEXT)) {
1873 *out++ = '&';
1874 *out++ = 'g';
1875 *out++ = 't';
1876 *out++ = ';';
1877 } else if ((*cur == '&') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
1878 (mode == XMLC14N_NORMALIZE_TEXT))) {
1879 *out++ = '&';
1880 *out++ = 'a';
1881 *out++ = 'm';
1882 *out++ = 'p';
1883 *out++ = ';';
1884 } else if ((*cur == '"') && (mode == XMLC14N_NORMALIZE_ATTR)) {
1885 *out++ = '&';
1886 *out++ = 'q';
1887 *out++ = 'u';
1888 *out++ = 'o';
1889 *out++ = 't';
1890 *out++ = ';';
1891 } else if ((*cur == '\x09') && (mode == XMLC14N_NORMALIZE_ATTR)) {
1892 *out++ = '&';
1893 *out++ = '#';
1894 *out++ = 'x';
1895 *out++ = '9';
1896 *out++ = ';';
1897 } else if ((*cur == '\x0A') && (mode == XMLC14N_NORMALIZE_ATTR)) {
1898 *out++ = '&';
1899 *out++ = '#';
1900 *out++ = 'x';
1901 *out++ = 'A';
1902 *out++ = ';';
1903 } else if ((*cur == '\x0D') && ((mode == XMLC14N_NORMALIZE_ATTR) ||
1904 (mode == XMLC14N_NORMALIZE_TEXT) ||
1905 (mode == XMLC14N_NORMALIZE_COMMENT) ||
1906 (mode == XMLC14N_NORMALIZE_PI))) {
1907 *out++ = '&';
1908 *out++ = '#';
1909 *out++ = 'x';
1910 *out++ = 'D';
1911 *out++ = ';';
1912 } else {
1913 /*
1914 * Works because on UTF-8, all extended sequences cannot
1915 * result in bytes in the ASCII range.
1916 */
1917 *out++ = *cur;
1918 }
1919 cur++;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001920 }
1921 *out++ = 0;
Daniel Veillard9ff88172002-03-11 09:15:32 +00001922 return (buffer);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001923}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001924#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001925#define bottom_c14n
1926#include "elfgcchack.h"
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001927#endif /* LIBXML_C14N_ENABLED */