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