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