Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * "Canonical XML" implementation |
| 3 | * http://www.w3.org/TR/xml-c14n |
| 4 | * |
| 5 | * "Exclusive XML Canonicalization" implementation |
| 6 | * http://www.w3.org/TR/xml-exc-c14n |
| 7 | * |
| 8 | * See Copyright for the status of this software. |
| 9 | * |
| 10 | * Author: Aleksey Sanin <aleksey@aleksey.com> |
| 11 | */ |
Daniel 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 |
| 15 | |
| 16 | #ifdef HAVE_STDLIB_H |
| 17 | #include <stdlib.h> |
| 18 | #endif |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <libxml/tree.h> |
| 22 | #include <libxml/parser.h> |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 23 | #include <libxml/uri.h> |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 24 | #include <libxml/xmlerror.h> |
| 25 | #include <libxml/globals.h> |
| 26 | #include <libxml/xpathInternals.h> |
| 27 | #include <libxml/c14n.h> |
| 28 | |
| 29 | /************************************************************************ |
| 30 | * * |
| 31 | * Some declaration better left private ATM * |
| 32 | * * |
| 33 | ************************************************************************/ |
| 34 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 35 | typedef enum { |
| 36 | XMLC14N_BEFORE_DOCUMENT_ELEMENT = 0, |
| 37 | XMLC14N_INSIDE_DOCUMENT_ELEMENT = 1, |
| 38 | XMLC14N_AFTER_DOCUMENT_ELEMENT = 2 |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 39 | } xmlC14NPosition; |
| 40 | |
| 41 | typedef struct _xmlC14NCtx { |
| 42 | /* input parameters */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 43 | xmlDocPtr doc; |
| 44 | xmlNodeSetPtr visible_nodes; |
| 45 | int with_comments; |
| 46 | xmlOutputBufferPtr buf; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 47 | |
| 48 | /* position in the XML document */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 49 | xmlC14NPosition pos; |
| 50 | int parent_is_doc; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 51 | |
| 52 | /* exclusive canonicalization */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 53 | int exclusive; |
| 54 | xmlNodeSetPtr ns_rendered; |
| 55 | xmlChar **inclusive_ns_prefixes; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 56 | } xmlC14NCtx, *xmlC14NCtxPtr; |
| 57 | |
| 58 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 59 | static int xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur); |
| 60 | static int xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 61 | typedef enum { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 62 | XMLC14N_NORMALIZE_ATTR = 0, |
| 63 | XMLC14N_NORMALIZE_COMMENT = 1, |
| 64 | XMLC14N_NORMALIZE_PI = 2, |
| 65 | XMLC14N_NORMALIZE_TEXT = 3 |
| 66 | } xmlC14NNormalizationMode; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 68 | static xmlChar *xmlC11NNormalizeString(const xmlChar * input, |
| 69 | xmlC14NNormalizationMode mode); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 70 | |
| 71 | #define xmlC11NNormalizeAttr( a ) \ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 72 | xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 73 | #define xmlC11NNormalizeComment( a ) \ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 74 | xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 75 | #define xmlC11NNormalizePI( a ) \ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 76 | xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 77 | #define xmlC11NNormalizeText( a ) \ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 78 | xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 79 | |
| 80 | /************************************************************************ |
| 81 | * * |
| 82 | * The implementation internals * |
| 83 | * * |
| 84 | ************************************************************************/ |
| 85 | |
| 86 | /** |
| 87 | * xmlC14NIsVisible: |
| 88 | * @ctx: the C14N context |
| 89 | * @node: the node to check |
| 90 | * |
| 91 | * Checks whether the given node is visible. If the XML document normalization |
| 92 | * was called for the whole document then it is always "true". |
| 93 | * |
| 94 | * Returns 1 if the node is visible or 0 otherwise. |
| 95 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 96 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 97 | /* todo: make it a define? */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 98 | static int |
| 99 | xmlC14NIsVisible(xmlC14NCtxPtr ctx, void *node) |
| 100 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 101 | /* |
| 102 | * If the input is an XPath node-set, then the node-set must explicitly |
| 103 | * contain every node to be rendered to the canonical form. |
| 104 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 105 | if ((ctx->visible_nodes != NULL) && |
| 106 | (!xmlXPathNodeSetContains(ctx->visible_nodes, (xmlNodePtr) node))) |
| 107 | { |
| 108 | return (0); |
| 109 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 110 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 111 | return (1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * xmlC14NIsXmlNs: |
| 116 | * @ns: the namespace to check |
| 117 | * |
| 118 | * Checks whether the given namespace is a default "xml:" namespace |
| 119 | * with href="http://www.w3.org/XML/1998/namespace" |
| 120 | * |
| 121 | * Returns 1 if the node is default or 0 otherwise |
| 122 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 123 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 124 | /* todo: make it a define? */ |
| 125 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 126 | xmlC14NIsXmlNs(xmlNsPtr ns) |
| 127 | { |
| 128 | return ((ns != NULL) && |
| 129 | (xmlStrEqual(ns->prefix, BAD_CAST "xml")) && |
| 130 | (xmlStrEqual(ns->href, |
| 131 | BAD_CAST |
| 132 | "http://www.w3.org/XML/1998/namespace"))); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * xmlExcC14NIsRendered: |
| 137 | * @ctx the C14N context |
| 138 | * @ns the namespace to check |
| 139 | * |
| 140 | * Checks whether the given namespace was already rendered or not |
| 141 | * |
| 142 | * Returns 1 if we already wrote this namespace or 0 otherwise |
| 143 | */ |
| 144 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 145 | xmlExcC14NIsRendered(xmlC14NCtxPtr ctx, xmlNsPtr ns) |
| 146 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 147 | int i; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 148 | |
| 149 | if ((ctx == NULL) || (ctx->ns_rendered == NULL) || (ns == NULL)) { |
| 150 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 153 | if (ctx->ns_rendered->nodeTab != NULL) { |
| 154 | for (i = ctx->ns_rendered->nodeNr - 1; i >= 0; --i) { |
| 155 | xmlNsPtr ns1 = (xmlNsPtr) ctx->ns_rendered->nodeTab[i]; |
| 156 | |
| 157 | if (xmlStrEqual(ns1->prefix, ns->prefix)) { |
| 158 | return (xmlStrEqual(ns1->href, ns->href)); |
| 159 | } |
| 160 | } |
| 161 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 162 | /* |
| 163 | * if the default namespace xmlns="" is not defined yet then |
| 164 | * we do not want to print it out |
| 165 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 166 | return ((xmlStrlen(ns->prefix) == 0) && (xmlStrlen(ns->href) == 0)); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * xmlC14NNamespacesCompare: |
| 171 | * @ns1: the pointer to first namespace |
| 172 | * @ns2: the pointer to second namespace |
| 173 | * |
| 174 | * Compares the namespaces by names (prefixes). |
| 175 | * |
| 176 | * Returns -1 if ns1 < ns2, 0 if ns1 == ns2 or 1 if ns1 > ns2. |
| 177 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 178 | static int |
| 179 | xmlC14NNamespacesCompare(xmlNsPtr ns1, xmlNsPtr ns2) |
| 180 | { |
| 181 | if (ns1 == ns2) |
| 182 | return (0); |
| 183 | if (ns1 == NULL) |
| 184 | return (-1); |
| 185 | if (ns2 == NULL) |
| 186 | return (1); |
| 187 | |
| 188 | return (xmlStrcmp(ns1->prefix, ns2->prefix)); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * xmlC14NPrintNamespaces: |
| 194 | * @ns: the pointer to namespace |
| 195 | * @ctx: the C14N context |
| 196 | * |
| 197 | * Prints the given namespace to the output buffer from C14N context. |
| 198 | * |
| 199 | * Returns 1 on success or 0 on fail. |
| 200 | */ |
| 201 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 202 | xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPtr ctx) |
| 203 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 204 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 205 | if ((ns == NULL) || (ctx == NULL)) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 206 | #ifdef DEBUG_C14N |
| 207 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 208 | "xmlC14NPrintNamespace: namespace or context pointer is null\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 209 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 210 | return 0; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 213 | if (ns->prefix != NULL) { |
| 214 | xmlOutputBufferWriteString(ctx->buf, " xmlns:"); |
| 215 | xmlOutputBufferWriteString(ctx->buf, (const char *) ns->prefix); |
| 216 | xmlOutputBufferWriteString(ctx->buf, "=\""); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 217 | } else { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 218 | xmlOutputBufferWriteString(ctx->buf, " xmlns=\""); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 219 | } |
| 220 | xmlOutputBufferWriteString(ctx->buf, (const char *) ns->href); |
| 221 | xmlOutputBufferWriteString(ctx->buf, "\""); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 222 | return (1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
| 226 | * xmlC14NProcessNamespacesAxis: |
| 227 | * @ctx: the C14N context |
| 228 | * @node: the current node |
| 229 | * |
| 230 | * Prints out canonical namespace axis of the current node to the |
| 231 | * buffer from C14N context as follows |
| 232 | * |
| 233 | * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
| 234 | * |
| 235 | * Namespace Axis |
| 236 | * Consider a list L containing only namespace nodes in the |
| 237 | * axis and in the node-set in lexicographic order (ascending). To begin |
| 238 | * processing L, if the first node is not the default namespace node (a node |
| 239 | * with no namespace URI and no local name), then generate a space followed |
| 240 | * by xmlns="" if and only if the following conditions are met: |
| 241 | * - the element E that owns the axis is in the node-set |
| 242 | * - The nearest ancestor element of E in the node-set has a default |
| 243 | * namespace node in the node-set (default namespace nodes always |
| 244 | * have non-empty values in XPath) |
| 245 | * The latter condition eliminates unnecessary occurrences of xmlns="" in |
| 246 | * the canonical form since an element only receives an xmlns="" if its |
| 247 | * default namespace is empty and if it has an immediate parent in the |
| 248 | * canonical form that has a non-empty default namespace. To finish |
| 249 | * processing L, simply process every namespace node in L, except omit |
| 250 | * namespace node with local name xml, which defines the xml prefix, |
| 251 | * if its string value is http://www.w3.org/XML/1998/namespace. |
| 252 | * |
| 253 | * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n) |
| 254 | * Canonical XML applied to a document subset requires the search of the |
| 255 | * ancestor nodes of each orphan element node for attributes in the xml |
| 256 | * namespace, such as xml:lang and xml:space. These are copied into the |
| 257 | * element node except if a declaration of the same attribute is already |
| 258 | * in the attribute axis of the element (whether or not it is included in |
| 259 | * the document subset). This search and copying are omitted from the |
| 260 | * Exclusive XML Canonicalization method. |
| 261 | * |
| 262 | * Returns 0 on success or -1 on fail. |
| 263 | */ |
| 264 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 265 | xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
| 266 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 267 | xmlNsPtr ns; |
| 268 | xmlListPtr list; |
| 269 | xmlNodePtr visible_parent; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 270 | xmlNodePtr node; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 271 | xmlNsPtr prev; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 272 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 273 | if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
| 274 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 275 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 276 | "xmlC14NProcessNamespacesAxis: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
| 277 | #endif |
| 278 | return (-1); |
| 279 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * Create a sorted list to store element namespaces |
| 283 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 284 | list = |
| 285 | xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNamespacesCompare); |
| 286 | if (list == NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 287 | #ifdef DEBUG_C14N |
| 288 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 289 | "xmlC14NProcessNamespacesAxis: list creation failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 290 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 291 | return (-1); |
| 292 | } |
| 293 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 294 | /* find nearest visible parent */ |
| 295 | visible_parent = cur->parent; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 296 | while ((visible_parent != NULL) && |
| 297 | (!xmlC14NIsVisible(ctx, visible_parent))) { |
| 298 | visible_parent = visible_parent->parent; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * todo: the libxml XPath implementation does not create |
| 303 | * nodes for all namespaces known to the node (i.e. for namespaces |
| 304 | * defined in node parents). By this we need to now walk thru |
| 305 | * all namespace in current node and all invisible ancesstors |
| 306 | */ |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 307 | node = cur; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 308 | while (cur != visible_parent) { |
| 309 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
| 310 | /* |
| 311 | * first of all ignore default "xml" namespace and |
| 312 | * already included namespace |
| 313 | */ |
| 314 | if ((xmlC14NIsXmlNs(ns)) || (xmlListSearch(list, ns) != NULL)) { |
| 315 | continue; |
| 316 | } |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 317 | prev = xmlSearchNs(ctx->doc, node, ns->prefix); |
| 318 | if(prev != ns) { |
| 319 | /* we already processed a namespace with this name */ |
| 320 | continue; |
| 321 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 322 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 323 | /* |
| 324 | * Lookup nearest namespace after visible parent having |
| 325 | * the same prefix. Namespace included if and only if one of |
| 326 | * the following: |
| 327 | * - another namespace having the same prefix but |
| 328 | * different value found or |
| 329 | * - there is no namespaces having the same prefix and |
| 330 | * it is not a default xmlns="" namespace (empty prefix |
| 331 | * and empty href) |
| 332 | */ |
| 333 | prev = xmlSearchNs(ctx->doc, visible_parent, ns->prefix); |
| 334 | if ((prev == NULL) && ((xmlStrlen(ns->prefix) > 0) || |
| 335 | (xmlStrlen(ns->href) > 0))) { |
| 336 | xmlListInsert(list, ns); |
| 337 | } else if ((prev != NULL) |
| 338 | && (!xmlStrEqual(ns->href, prev->href))) { |
| 339 | xmlListInsert(list, ns); |
| 340 | } |
| 341 | } |
| 342 | cur = cur->parent; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | * print out all elements from list |
| 347 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 348 | xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, |
| 349 | (const void *) ctx); |
| 350 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 351 | /* |
| 352 | * Cleanup |
| 353 | */ |
| 354 | xmlListDelete(list); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 355 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * xmlExcC14NProcessNamespacesAxis: |
| 360 | * @ctx: the C14N context |
| 361 | * @node: the current node |
| 362 | * |
| 363 | * Prints out exclusive canonical namespace axis of the current node to the |
| 364 | * buffer from C14N context as follows |
| 365 | * |
| 366 | * Exclusive XML Canonicalization |
| 367 | * http://www.w3.org/TR/xml-exc-c14n |
| 368 | * |
| 369 | * If the element node is in the XPath subset then output the node in |
| 370 | * accordance with Canonical XML except for namespace nodes which are |
| 371 | * rendered as follows: |
| 372 | * |
| 373 | * 1. Render each namespace node iff: |
| 374 | * * it is visibly utilized by the immediate parent element or one of |
| 375 | * its attributes, or is present in InclusiveNamespaces PrefixList, and |
| 376 | * * its prefix and value do not appear in ns_rendered. ns_rendered is |
| 377 | * obtained by popping the state stack in order to obtain a list of |
| 378 | * prefixes and their values which have already been rendered by |
| 379 | * an output ancestor of the namespace node's parent element. |
| 380 | * 2. Append the rendered namespace node to the list ns_rendered of namespace |
| 381 | * nodes rendered by output ancestors. Push ns_rendered on state stack and |
| 382 | * recurse. |
| 383 | * 3. After the recursion returns, pop thestate stack. |
| 384 | * |
| 385 | * |
| 386 | * Returns 0 on success or -1 on fail. |
| 387 | */ |
| 388 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 389 | xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
| 390 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 391 | xmlListPtr list; |
| 392 | xmlAttrPtr attr; |
| 393 | xmlNsPtr ns; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 394 | |
| 395 | if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
| 396 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 397 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 398 | "xmlExcC14NProcessNamespacesAxis: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
| 399 | #endif |
| 400 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 401 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 402 | |
| 403 | if ((!ctx->exclusive) || (ctx->ns_rendered == NULL)) { |
| 404 | #ifdef DEBUG_C14N |
| 405 | xmlGenericError(xmlGenericErrorContext, |
| 406 | "xmlExcC14NProcessNamespacesAxis: called for non-exclusive canonization or rendered stack is NULL.\n"); |
| 407 | #endif |
| 408 | return (-1); |
| 409 | |
| 410 | } |
| 411 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 412 | /* |
| 413 | * Create a sorted list to store element namespaces |
| 414 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 415 | list = |
| 416 | xmlListCreate(NULL, (xmlListDataCompare) xmlC14NNamespacesCompare); |
| 417 | if (list == NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 418 | #ifdef DEBUG_C14N |
| 419 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 420 | "xmlExcC14NProcessNamespacesAxis: list creation failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 421 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 422 | return (-1); |
| 423 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * First of all, add all namespaces required by current node |
| 427 | * (i.e. node namespace and all attribute namespaces) |
| 428 | * todo: do we need to check for default "xml:" namespace |
| 429 | */ |
| 430 | ns = (cur->ns != NULL) ? cur->ns : xmlSearchNs(ctx->doc, cur, NULL); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 431 | if ((ns != NULL) && (!xmlC14NIsXmlNs(ns)) && |
| 432 | (xmlListSearch(list, ns) == NULL)) { |
| 433 | if (!xmlExcC14NIsRendered(ctx, ns)) { |
| 434 | xmlListInsert(list, ns); |
| 435 | xmlXPathNodeSetAdd(ctx->ns_rendered, (xmlNodePtr) ns); |
| 436 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 437 | } |
| 438 | attr = cur->properties; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 439 | while (attr != NULL) { |
| 440 | /* |
Daniel Veillard | 2d347fa | 2002-03-17 10:34:11 +0000 | [diff] [blame] | 441 | * we need to check that attribute is visible and has non |
| 442 | * default namespace (XML Namespaces: "default namespaces |
| 443 | * do not apply directly to attributes") |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 444 | */ |
Daniel Veillard | 2d347fa | 2002-03-17 10:34:11 +0000 | [diff] [blame] | 445 | if ((attr->ns != NULL) && xmlC14NIsVisible(ctx, attr) && |
| 446 | (xmlListSearch(list, attr->ns) == NULL) && |
| 447 | (!xmlExcC14NIsRendered(ctx, attr->ns))) { |
| 448 | xmlListInsert(list, attr->ns); |
| 449 | xmlXPathNodeSetAdd(ctx->ns_rendered, (xmlNodePtr) attr->ns); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 450 | } |
| 451 | attr = attr->next; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 452 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 453 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 454 | /* |
| 455 | * Next add all inclusive namespaces if needed. |
| 456 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 457 | if (ctx->inclusive_ns_prefixes != NULL) { |
| 458 | int i; |
| 459 | xmlChar *prefix; |
| 460 | |
| 461 | for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) { |
| 462 | prefix = ctx->inclusive_ns_prefixes[i]; |
| 463 | /* |
| 464 | * Special values for namespace with empty prefix |
| 465 | */ |
| 466 | if (xmlStrEqual(prefix, BAD_CAST "#default") |
| 467 | || xmlStrEqual(prefix, BAD_CAST "")) { |
| 468 | prefix = NULL; |
| 469 | } |
| 470 | ns = xmlSearchNs(ctx->doc, cur, prefix); |
| 471 | if ((ns != NULL) && (!xmlC14NIsXmlNs(ns))) { |
| 472 | if (xmlListSearch(list, ns) == NULL && |
| 473 | !xmlExcC14NIsRendered(ctx, ns)) { |
| 474 | xmlListInsert(list, ns); |
| 475 | xmlXPathNodeSetAdd(ctx->ns_rendered, (xmlNodePtr) ns); |
| 476 | } |
| 477 | } |
| 478 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 479 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 480 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 481 | /* |
| 482 | * print out all elements from list |
| 483 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 484 | xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, |
| 485 | (const void *) ctx); |
| 486 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 487 | /* |
| 488 | * Cleanup |
| 489 | */ |
| 490 | xmlListDelete(list); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 491 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
| 495 | /** |
| 496 | * xmlC14NAttrsCompare: |
| 497 | * @attr1: the pointer to first attr |
| 498 | * @attr2: the pointer to second attr |
| 499 | * |
| 500 | * Prints the given attribute to the output buffer from C14N context. |
| 501 | * |
| 502 | * Returns -1 if attr1 < attr2, 0 if attr1 == attr2 or 1 if attr1 > attr2. |
| 503 | */ |
| 504 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 505 | xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2) |
| 506 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 507 | int ret = 0; |
| 508 | |
| 509 | /* |
| 510 | * Simple cases |
| 511 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 512 | if (attr1 == attr2) |
| 513 | return (0); |
| 514 | if (attr1 == NULL) |
| 515 | return (-1); |
| 516 | if (attr2 == NULL) |
| 517 | return (1); |
| 518 | if (attr1->ns == attr2->ns) { |
| 519 | return (xmlStrcmp(attr1->name, attr2->name)); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Attributes in the default namespace are first |
| 524 | * because the default namespace is not applied to |
| 525 | * unqualified attributes |
| 526 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 527 | if (attr1->ns == NULL) |
| 528 | return (-1); |
| 529 | if (attr2->ns == NULL) |
| 530 | return (1); |
| 531 | if (attr1->ns->prefix == NULL) |
| 532 | return (-1); |
| 533 | if (attr2->ns->prefix == NULL) |
| 534 | return (1); |
| 535 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 536 | ret = xmlStrcmp(attr1->ns->href, attr2->ns->href); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 537 | if (ret == 0) { |
| 538 | ret = xmlStrcmp(attr1->name, attr2->name); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 539 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 540 | return (ret); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | |
| 544 | /** |
| 545 | * xmlC14NPrintAttrs: |
| 546 | * @attr: the pointer to attr |
| 547 | * @ctx: the C14N context |
| 548 | * |
| 549 | * Prints out canonical attribute urrent node to the |
| 550 | * buffer from C14N context as follows |
| 551 | * |
| 552 | * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
| 553 | * |
| 554 | * Returns 1 on success or 0 on fail. |
| 555 | */ |
| 556 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 557 | xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr ctx) |
| 558 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 559 | xmlChar *value; |
| 560 | xmlChar *buffer; |
| 561 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 562 | if ((attr == NULL) || (ctx == NULL)) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 563 | #ifdef DEBUG_C14N |
| 564 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 565 | "xmlC14NPrintAttrs: attr == NULL or ctx == NULL\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 566 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 567 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | xmlOutputBufferWriteString(ctx->buf, " "); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 571 | if (attr->ns != NULL && xmlStrlen(attr->ns->prefix) > 0) { |
| 572 | xmlOutputBufferWriteString(ctx->buf, |
| 573 | (const char *) attr->ns->prefix); |
| 574 | xmlOutputBufferWriteString(ctx->buf, ":"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 575 | } |
| 576 | xmlOutputBufferWriteString(ctx->buf, (const char *) attr->name); |
| 577 | xmlOutputBufferWriteString(ctx->buf, "=\""); |
| 578 | |
| 579 | value = xmlNodeListGetString(attr->doc, attr->children, 1); |
| 580 | /* todo: should we log an error if value==NULL ? */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 581 | if (value != NULL) { |
| 582 | buffer = xmlC11NNormalizeAttr(value); |
| 583 | xmlFree(value); |
| 584 | if (buffer != NULL) { |
| 585 | xmlOutputBufferWriteString(ctx->buf, (const char *) buffer); |
| 586 | xmlFree(buffer); |
| 587 | } else { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 588 | #ifdef DEBUG_C14N |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 589 | xmlGenericError(xmlGenericErrorContext, |
| 590 | "xmlC14NPrintAttrs: xmlC11NNormalizeAttr failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 591 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 592 | return (0); |
| 593 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 594 | } |
| 595 | xmlOutputBufferWriteString(ctx->buf, "\""); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 596 | return (1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
| 600 | * xmlC14NProcessAttrsAxis: |
| 601 | * @ctx: the C14N context |
| 602 | * @cur: the current node |
| 603 | * |
| 604 | * Prints out canonical attribute axis of the current node to the |
| 605 | * buffer from C14N context as follows |
| 606 | * |
| 607 | * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
| 608 | * |
| 609 | * Attribute Axis |
| 610 | * In lexicographic order (ascending), process each node that |
| 611 | * is in the element's attribute axis and in the node-set. |
| 612 | * |
| 613 | * The processing of an element node E MUST be modified slightly |
| 614 | * when an XPath node-set is given as input and the element's |
| 615 | * parent is omitted from the node-set. |
| 616 | * |
| 617 | * |
| 618 | * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n) |
| 619 | * |
| 620 | * Canonical XML applied to a document subset requires the search of the |
| 621 | * ancestor nodes of each orphan element node for attributes in the xml |
| 622 | * namespace, such as xml:lang and xml:space. These are copied into the |
| 623 | * element node except if a declaration of the same attribute is already |
| 624 | * in the attribute axis of the element (whether or not it is included in |
| 625 | * the document subset). This search and copying are omitted from the |
| 626 | * Exclusive XML Canonicalization method. |
| 627 | * |
| 628 | * Returns 0 on success or -1 on fail. |
| 629 | */ |
| 630 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 631 | xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
| 632 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 633 | xmlAttrPtr attr; |
| 634 | xmlListPtr list; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 635 | |
| 636 | if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
| 637 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 638 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 639 | "xmlC14NProcessAttrsAxis: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
| 640 | #endif |
| 641 | return (-1); |
| 642 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Create a sorted list to store element attributes |
| 646 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 647 | list = xmlListCreate(NULL, (xmlListDataCompare) xmlC14NAttrsCompare); |
| 648 | if (list == NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 649 | #ifdef DEBUG_C14N |
| 650 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 651 | "xmlC14NProcessAttrsAxis: list creation failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 652 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 653 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Add all visible attributes from current node. |
| 658 | */ |
| 659 | attr = cur->properties; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 660 | while (attr != NULL) { |
| 661 | /* check that attribute is visible */ |
| 662 | if (xmlC14NIsVisible(ctx, attr)) { |
| 663 | xmlListInsert(list, attr); |
| 664 | } |
| 665 | attr = attr->next; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 666 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 667 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 668 | /* |
| 669 | * include attributes in "xml" namespace defined in ancestors |
| 670 | * (only for non-exclusive XML Canonicalization) |
| 671 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 672 | if ((!ctx->exclusive) && (cur->parent != NULL) |
| 673 | && (!xmlC14NIsVisible(ctx, cur->parent))) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 674 | /* |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 675 | * If XPath node-set is not specified then the parent is always |
| 676 | * visible! |
| 677 | */ |
| 678 | cur = cur->parent; |
| 679 | while (cur != NULL) { |
| 680 | attr = cur->properties; |
| 681 | while (attr != NULL) { |
| 682 | if ((attr->ns != NULL) |
| 683 | && (xmlStrEqual(attr->ns->prefix, BAD_CAST "xml"))) { |
| 684 | if (xmlListSearch(list, attr) == NULL) { |
| 685 | xmlListInsert(list, attr); |
| 686 | } |
| 687 | } |
| 688 | attr = attr->next; |
| 689 | } |
| 690 | cur = cur->parent; |
| 691 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 692 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 693 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 694 | /* |
| 695 | * print out all elements from list |
| 696 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 697 | xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, |
| 698 | (const void *) ctx); |
| 699 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 700 | /* |
| 701 | * Cleanup |
| 702 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 703 | xmlListDelete(list); |
| 704 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
| 708 | * xmlC14NCheckForRelativeNamespaces: |
| 709 | * @ctx: the C14N context |
| 710 | * @cur: the current element node |
| 711 | * |
| 712 | * Checks that current element node has no relative namespaces defined |
| 713 | * |
| 714 | * Returns 0 if the node has no relative namespaces or -1 otherwise. |
| 715 | */ |
| 716 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 717 | xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
| 718 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 719 | xmlNsPtr ns; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 720 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 721 | if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
| 722 | #ifdef DEBUG_C14N |
| 723 | xmlGenericError(xmlGenericErrorContext, |
| 724 | "xmlC14NCheckForRelativeNamespaces: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
| 725 | #endif |
| 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 | |
| 729 | ns = cur->nsDef; |
| 730 | while (ns != NULL) { |
| 731 | if (xmlStrlen(ns->href) > 0) { |
| 732 | xmlURIPtr uri; |
| 733 | |
| 734 | uri = xmlParseURI((const char *) ns->href); |
| 735 | if (uri == NULL) { |
| 736 | #ifdef DEBUG_C14N |
| 737 | xmlGenericError(xmlGenericErrorContext, |
| 738 | "xmlC14NCheckForRelativeNamespaces: unable to parse uri=\"%s\".\n", |
| 739 | ns->href); |
| 740 | #endif |
| 741 | return (-1); |
| 742 | } |
| 743 | if (xmlStrlen((const xmlChar *) uri->scheme) == 0) { |
| 744 | xmlFreeURI(uri); |
| 745 | return (-1); |
| 746 | } |
| 747 | if ((!xmlStrEqual |
| 748 | ((const xmlChar *) uri->scheme, BAD_CAST "urn")) |
| 749 | && (xmlStrlen((const xmlChar *) uri->server) == 0)) { |
| 750 | xmlFreeURI(uri); |
| 751 | return (-1); |
| 752 | } |
| 753 | xmlFreeURI(uri); |
| 754 | } |
| 755 | ns = ns->next; |
| 756 | } |
| 757 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
| 761 | * xmlC14NProcessElementNode: |
| 762 | * @ctx: the pointer to C14N context object |
| 763 | * @cur: the node to process |
| 764 | * |
| 765 | * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n) |
| 766 | * |
| 767 | * Element Nodes |
| 768 | * If the element is not in the node-set, then the result is obtained |
| 769 | * by processing the namespace axis, then the attribute axis, then |
| 770 | * processing the child nodes of the element that are in the node-set |
| 771 | * (in document order). If the element is in the node-set, then the result |
| 772 | * is an open angle bracket (<), the element QName, the result of |
| 773 | * processing the namespace axis, the result of processing the attribute |
| 774 | * axis, a close angle bracket (>), the result of processing the child |
| 775 | * nodes of the element that are in the node-set (in document order), an |
| 776 | * open angle bracket, a forward slash (/), the element QName, and a close |
| 777 | * angle bracket. |
| 778 | * |
| 779 | * Returns non-negative value on success or negative value on fail |
| 780 | */ |
| 781 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 782 | xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePtr cur, int visible) |
| 783 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 784 | int ret; |
| 785 | int ns_rendered_pos = 0; |
Daniel Veillard | 6f293b1 | 2002-03-15 09:42:33 +0000 | [diff] [blame] | 786 | int parent_is_doc = 0; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 787 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 788 | if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { |
| 789 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 790 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 791 | "xmlC14NProcessElementNode: Null context or node pointer or type != XML_ELEMENT_NODE.\n"); |
| 792 | #endif |
| 793 | return (-1); |
| 794 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 795 | |
| 796 | /* |
| 797 | * Check relative relative namespaces: |
| 798 | * implementations of XML canonicalization MUST report an operation |
| 799 | * failure on documents containing relative namespace URIs. |
| 800 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 801 | if (xmlC14NCheckForRelativeNamespaces(ctx, cur) < 0) { |
| 802 | #ifdef DEBUG_C14N |
| 803 | xmlGenericError(xmlGenericErrorContext, |
| 804 | "xmlC14NProcessElementNode: xmlC14NCheckForRelativeNamespaces failed.\n"); |
| 805 | #endif |
| 806 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | /* |
| 811 | * Save ns_rendered stack position for exclusive |
| 812 | * processing |
| 813 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 814 | if ((ctx->exclusive) && (ctx->ns_rendered != NULL)) { |
| 815 | ns_rendered_pos = ctx->ns_rendered->nodeNr; |
| 816 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 817 | |
Daniel Veillard | 6f293b1 | 2002-03-15 09:42:33 +0000 | [diff] [blame] | 818 | if (visible) { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 819 | if (ctx->parent_is_doc) { |
Daniel Veillard | 6f293b1 | 2002-03-15 09:42:33 +0000 | [diff] [blame] | 820 | /* save this flag into the stack */ |
| 821 | parent_is_doc = ctx->parent_is_doc; |
| 822 | ctx->parent_is_doc = 0; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 823 | ctx->pos = XMLC14N_INSIDE_DOCUMENT_ELEMENT; |
| 824 | } |
| 825 | xmlOutputBufferWriteString(ctx->buf, "<"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 826 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 827 | if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) { |
| 828 | xmlOutputBufferWriteString(ctx->buf, |
| 829 | (const char *) cur->ns->prefix); |
| 830 | xmlOutputBufferWriteString(ctx->buf, ":"); |
| 831 | } |
| 832 | xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 833 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 834 | if (ctx->exclusive) { |
| 835 | ret = xmlExcC14NProcessNamespacesAxis(ctx, cur); |
| 836 | } else { |
| 837 | ret = xmlC14NProcessNamespacesAxis(ctx, cur); |
| 838 | } |
| 839 | if (ret < 0) { |
| 840 | #ifdef DEBUG_C14N |
| 841 | xmlGenericError(xmlGenericErrorContext, |
| 842 | "xmlC14NProcessElementNode: xmlC14NProcessNamespacesAxis failed.\n"); |
| 843 | #endif |
| 844 | return (-1); |
| 845 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 846 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 847 | ret = xmlC14NProcessAttrsAxis(ctx, cur); |
| 848 | if (ret < 0) { |
| 849 | #ifdef DEBUG_C14N |
| 850 | xmlGenericError(xmlGenericErrorContext, |
| 851 | "xmlC14NProcessElementNode: xmlC14NProcessAttrsAxis failed.\n"); |
| 852 | #endif |
| 853 | return (-1); |
| 854 | } |
| 855 | |
| 856 | xmlOutputBufferWriteString(ctx->buf, ">"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 857 | } |
| 858 | if (cur->children != NULL) { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 859 | ret = xmlC14NProcessNodeList(ctx, cur->children); |
| 860 | if (ret < 0) { |
| 861 | #ifdef DEBUG_C14N |
| 862 | xmlGenericError(xmlGenericErrorContext, |
| 863 | "xmlC14NProcessElementNode: xmlC14NProcessNodeList failed.\n"); |
| 864 | #endif |
| 865 | return (-1); |
| 866 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 867 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 868 | if (visible) { |
| 869 | xmlOutputBufferWriteString(ctx->buf, "</"); |
| 870 | if ((cur->ns != NULL) && (xmlStrlen(cur->ns->prefix) > 0)) { |
| 871 | xmlOutputBufferWriteString(ctx->buf, |
| 872 | (const char *) cur->ns->prefix); |
| 873 | xmlOutputBufferWriteString(ctx->buf, ":"); |
| 874 | } |
| 875 | xmlOutputBufferWriteString(ctx->buf, (const char *) cur->name); |
| 876 | xmlOutputBufferWriteString(ctx->buf, ">"); |
Daniel Veillard | 6f293b1 | 2002-03-15 09:42:33 +0000 | [diff] [blame] | 877 | if (parent_is_doc) { |
| 878 | /* restore this flag from the stack for next node */ |
| 879 | ctx->parent_is_doc = parent_is_doc; |
| 880 | ctx->pos = XMLC14N_AFTER_DOCUMENT_ELEMENT; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 881 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Restore ns_rendered stack position for exclusive |
| 886 | * processing |
| 887 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 888 | if ((ctx->exclusive) && (ctx->ns_rendered != NULL)) { |
| 889 | ctx->ns_rendered->nodeNr = ns_rendered_pos; |
| 890 | } |
| 891 | return (0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /** |
| 895 | * xmlC14NProcessNode: |
| 896 | * @ctx: the pointer to C14N context object |
| 897 | * @cur: the node to process |
| 898 | * |
| 899 | * Processes the given node |
| 900 | * |
| 901 | * Returns non-negative value on success or negative value on fail |
| 902 | */ |
| 903 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 904 | xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
| 905 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 906 | int ret = 0; |
| 907 | int visible; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 908 | |
| 909 | if ((ctx == NULL) || (cur == NULL)) { |
| 910 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 911 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 912 | "xmlC14NProcessNode: Null context or node pointer.\n"); |
| 913 | #endif |
| 914 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | visible = xmlC14NIsVisible(ctx, cur); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 918 | switch (cur->type) { |
| 919 | case XML_ELEMENT_NODE: |
| 920 | ret = xmlC14NProcessElementNode(ctx, cur, visible); |
| 921 | break; |
| 922 | case XML_CDATA_SECTION_NODE: |
| 923 | case XML_TEXT_NODE: |
| 924 | /* |
| 925 | * Text Nodes |
| 926 | * the string value, except all ampersands are replaced |
| 927 | * by &, all open angle brackets (<) are replaced by <, all closing |
| 928 | * angle brackets (>) are replaced by >, and all #xD characters are |
| 929 | * replaced by 
. |
| 930 | */ |
| 931 | /* cdata sections are processed as text nodes */ |
| 932 | /* todo: verify that cdata sections are included in XPath nodes set */ |
| 933 | if ((visible) && (cur->content != NULL)) { |
| 934 | xmlChar *buffer; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 935 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 936 | buffer = xmlC11NNormalizeText(cur->content); |
| 937 | if (buffer != NULL) { |
| 938 | xmlOutputBufferWriteString(ctx->buf, |
| 939 | (const char *) buffer); |
| 940 | xmlFree(buffer); |
| 941 | } else { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 942 | #ifdef DEBUG_C14N |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 943 | xmlGenericError(xmlGenericErrorContext, |
| 944 | "xmlC14NProcessNode: xmlC11NNormalizeText() failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 945 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 946 | return (-1); |
| 947 | } |
| 948 | } |
| 949 | break; |
| 950 | case XML_PI_NODE: |
| 951 | /* |
| 952 | * Processing Instruction (PI) Nodes- |
| 953 | * The opening PI symbol (<?), the PI target name of the node, |
| 954 | * a leading space and the string value if it is not empty, and |
| 955 | * the closing PI symbol (?>). If the string value is empty, |
| 956 | * then the leading space is not added. Also, a trailing #xA is |
| 957 | * rendered after the closing PI symbol for PI children of the |
| 958 | * root node with a lesser document order than the document |
| 959 | * element, and a leading #xA is rendered before the opening PI |
| 960 | * symbol of PI children of the root node with a greater document |
| 961 | * order than the document element. |
| 962 | */ |
| 963 | if (visible) { |
| 964 | if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) { |
| 965 | xmlOutputBufferWriteString(ctx->buf, "\x0A<?"); |
| 966 | } else { |
| 967 | xmlOutputBufferWriteString(ctx->buf, "<?"); |
| 968 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 969 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 970 | xmlOutputBufferWriteString(ctx->buf, |
| 971 | (const char *) cur->name); |
| 972 | if ((cur->content != NULL) && (*(cur->content) != '\0')) { |
| 973 | xmlChar *buffer; |
| 974 | |
| 975 | xmlOutputBufferWriteString(ctx->buf, " "); |
| 976 | |
| 977 | /* todo: do we need to normalize pi? */ |
| 978 | buffer = xmlC11NNormalizePI(cur->content); |
| 979 | if (buffer != NULL) { |
| 980 | xmlOutputBufferWriteString(ctx->buf, |
| 981 | (const char *) buffer); |
| 982 | xmlFree(buffer); |
| 983 | } else { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 984 | #ifdef DEBUG_C14N |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 985 | xmlGenericError(xmlGenericErrorContext, |
| 986 | "xmlC14NProcessNode: xmlC11NNormalizePI() failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 987 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 988 | return (-1); |
| 989 | } |
| 990 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 991 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 992 | if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) { |
| 993 | xmlOutputBufferWriteString(ctx->buf, "?>\x0A"); |
| 994 | } else { |
| 995 | xmlOutputBufferWriteString(ctx->buf, "?>"); |
| 996 | } |
| 997 | } |
| 998 | break; |
| 999 | case XML_COMMENT_NODE: |
| 1000 | /* |
| 1001 | * Comment Nodes |
| 1002 | * Nothing if generating canonical XML without comments. For |
| 1003 | * canonical XML with comments, generate the opening comment |
| 1004 | * symbol (<!--), the string value of the node, and the |
| 1005 | * closing comment symbol (-->). Also, a trailing #xA is rendered |
| 1006 | * after the closing comment symbol for comment children of the |
| 1007 | * root node with a lesser document order than the document |
| 1008 | * element, and a leading #xA is rendered before the opening |
| 1009 | * comment symbol of comment children of the root node with a |
| 1010 | * greater document order than the document element. (Comment |
| 1011 | * children of the root node represent comments outside of the |
| 1012 | * top-level document element and outside of the document type |
| 1013 | * declaration). |
| 1014 | */ |
| 1015 | if (visible && ctx->with_comments) { |
| 1016 | if (ctx->pos == XMLC14N_AFTER_DOCUMENT_ELEMENT) { |
| 1017 | xmlOutputBufferWriteString(ctx->buf, "\x0A<!--"); |
| 1018 | } else { |
| 1019 | xmlOutputBufferWriteString(ctx->buf, "<!--"); |
| 1020 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1021 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1022 | if (cur->content != NULL) { |
| 1023 | xmlChar *buffer; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1024 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1025 | /* todo: do we need to normalize comment? */ |
| 1026 | buffer = xmlC11NNormalizeComment(cur->content); |
| 1027 | if (buffer != NULL) { |
| 1028 | xmlOutputBufferWriteString(ctx->buf, |
| 1029 | (const char *) buffer); |
| 1030 | xmlFree(buffer); |
| 1031 | } else { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1032 | #ifdef DEBUG_C14N |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1033 | xmlGenericError(xmlGenericErrorContext, |
| 1034 | "xmlC14NProcessNode: xmlC11NNormalizeComment() failed\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1035 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1036 | return (-1); |
| 1037 | } |
| 1038 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1039 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1040 | if (ctx->pos == XMLC14N_BEFORE_DOCUMENT_ELEMENT) { |
| 1041 | xmlOutputBufferWriteString(ctx->buf, "-->\x0A"); |
| 1042 | } else { |
| 1043 | xmlOutputBufferWriteString(ctx->buf, "-->"); |
| 1044 | } |
| 1045 | } |
| 1046 | break; |
| 1047 | case XML_DOCUMENT_NODE: |
| 1048 | case XML_DOCUMENT_FRAG_NODE: /* should be processed as document? */ |
| 1049 | case XML_DOCB_DOCUMENT_NODE: /* should be processed as document? */ |
| 1050 | case XML_HTML_DOCUMENT_NODE: /* should be processed as document? */ |
| 1051 | if (cur->children != NULL) { |
| 1052 | ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT; |
| 1053 | ctx->parent_is_doc = 1; |
| 1054 | ret = xmlC14NProcessNodeList(ctx, cur->children); |
| 1055 | } |
| 1056 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1057 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1058 | case XML_ATTRIBUTE_NODE: |
| 1059 | xmlGenericError(xmlGenericErrorContext, |
| 1060 | "xmlC14NProcessNode: XML_ATTRIBUTE_NODE is illegal here\n"); |
| 1061 | return (-1); |
| 1062 | case XML_NAMESPACE_DECL: |
| 1063 | xmlGenericError(xmlGenericErrorContext, |
| 1064 | "xmlC14NProcessNode: XML_NAMESPACE_DECL is illegal here\n"); |
| 1065 | return (-1); |
| 1066 | case XML_ENTITY_REF_NODE: |
| 1067 | xmlGenericError(xmlGenericErrorContext, |
| 1068 | "xmlC14NProcessNode: XML_ENTITY_REF_NODE is illegal here\n"); |
| 1069 | return (-1); |
| 1070 | case XML_ENTITY_NODE: |
| 1071 | xmlGenericError(xmlGenericErrorContext, |
| 1072 | "xmlC14NProcessNode: XML_ENTITY_NODE is illegal here\n"); |
| 1073 | return (-1); |
| 1074 | |
| 1075 | case XML_DOCUMENT_TYPE_NODE: |
| 1076 | case XML_NOTATION_NODE: |
| 1077 | case XML_DTD_NODE: |
| 1078 | case XML_ELEMENT_DECL: |
| 1079 | case XML_ATTRIBUTE_DECL: |
| 1080 | case XML_ENTITY_DECL: |
| 1081 | case XML_XINCLUDE_START: |
| 1082 | case XML_XINCLUDE_END: |
| 1083 | /* |
| 1084 | * should be ignored according to "W3C Canonical XML" |
| 1085 | */ |
| 1086 | break; |
| 1087 | default: |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1088 | #ifdef DEBUG_C14N |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1089 | xmlGenericError(xmlGenericErrorContext, |
| 1090 | "xmlC14NProcessNode: unknown node type = %d\n", |
| 1091 | cur->type); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1092 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1093 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1096 | return (ret); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * xmlC14NProcessNodeList: |
| 1101 | * @ctx: the pointer to C14N context object |
| 1102 | * @cur: the node to start from |
| 1103 | * |
| 1104 | * Processes all nodes in the row starting from cur. |
| 1105 | * |
| 1106 | * Returns non-negative value on success or negative value on fail |
| 1107 | */ |
| 1108 | static int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1109 | xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr cur) |
| 1110 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1111 | int ret; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (ctx == NULL) { |
| 1114 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1115 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1116 | "xmlC14NProcessNodeList: Null context pointer.\n"); |
| 1117 | #endif |
| 1118 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1121 | for (ret = 0; cur != NULL && ret >= 0; cur = cur->next) { |
| 1122 | ret = xmlC14NProcessNode(ctx, cur); |
| 1123 | } |
| 1124 | return (ret); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | /** |
| 1129 | * xmlC14NFreeCtx: |
| 1130 | * @ctx: the pointer to C14N context object |
| 1131 | * |
| 1132 | * Cleanups the C14N context object. |
| 1133 | */ |
| 1134 | |
| 1135 | static void |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1136 | xmlC14NFreeCtx(xmlC14NCtxPtr ctx) |
| 1137 | { |
| 1138 | if (ctx == NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1139 | #ifdef DEBUG_C14N |
| 1140 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1141 | "xmlC14NFreeCtx: ctx == NULL\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1142 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1143 | return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1144 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1145 | |
| 1146 | if (ctx->ns_rendered != NULL) { |
| 1147 | xmlXPathFreeNodeSet(ctx->ns_rendered); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1148 | } |
| 1149 | xmlFree(ctx); |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * xmlC14NNewCtx: |
| 1154 | * @doc: the XML document for canonization |
| 1155 | * @nodes: the nodes set to be included in the canonized image |
| 1156 | * or NULL if all document nodes should be included |
| 1157 | * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
| 1158 | * otherwise - exclusive canonicalization) |
| 1159 | * @inclusive_ns_prefixe the list of inclusive namespace prefixes |
| 1160 | * ended with a NULL or NULL if there is no |
| 1161 | * inclusive namespaces (only for exclusive |
| 1162 | * canonicalization) |
| 1163 | * @with_comments: include comments in the result (!=0) or not (==0) |
| 1164 | * @buf: the output buffer to store canonical XML; this |
| 1165 | * buffer MUST have encoder==NULL because C14N requires |
| 1166 | * UTF-8 output |
| 1167 | * |
| 1168 | * Creates new C14N context object to store C14N parameters. |
| 1169 | * |
| 1170 | * Returns pointer to newly created object (success) or NULL (fail) |
| 1171 | */ |
| 1172 | static xmlC14NCtxPtr |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1173 | xmlC14NNewCtx(xmlDocPtr doc, xmlNodeSetPtr nodes, |
| 1174 | int exclusive, xmlChar ** inclusive_ns_prefixes, |
| 1175 | int with_comments, xmlOutputBufferPtr buf) |
| 1176 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1177 | xmlC14NCtxPtr ctx; |
| 1178 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1179 | if ((doc == NULL) || (buf == NULL)) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1180 | #ifdef DEBUG_C14N |
| 1181 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1182 | "xmlC14NNewCtx: pointer to document or output buffer is NULL\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1183 | #endif |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1184 | return (NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * Validate the encoding output buffer encoding |
| 1189 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1190 | if (buf->encoder != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1191 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1192 | "xmlC14NNewCtx: output buffer encoder != NULL but C14N requires UTF8 output\n"); |
| 1193 | return (NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Validate the XML document encoding value, if provided. |
| 1198 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1199 | if (doc->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1200 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1201 | "xmlC14NNewCtx: source document not in UTF8\n"); |
| 1202 | return (NULL); |
| 1203 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* |
| 1206 | * Allocate a new xmlC14NCtxPtr and fill the fields. |
| 1207 | */ |
| 1208 | ctx = (xmlC14NCtxPtr) xmlMalloc(sizeof(xmlC14NCtx)); |
| 1209 | if (ctx == NULL) { |
| 1210 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1211 | "xmlC14NNewCtx: malloc failed\n"); |
| 1212 | return (NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1213 | } |
| 1214 | memset(ctx, 0, sizeof(xmlC14NCtx)); |
| 1215 | |
| 1216 | /* |
| 1217 | * initialize C14N context |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1218 | */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1219 | ctx->doc = doc; |
| 1220 | ctx->with_comments = with_comments; |
| 1221 | ctx->visible_nodes = nodes; |
| 1222 | ctx->buf = buf; |
| 1223 | ctx->parent_is_doc = 1; |
| 1224 | ctx->pos = XMLC14N_BEFORE_DOCUMENT_ELEMENT; |
| 1225 | |
| 1226 | /* |
| 1227 | * Set "exclusive" flag, create a nodes set for namespaces |
| 1228 | * stack and remember list of incluseve prefixes |
| 1229 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1230 | if (exclusive) { |
| 1231 | ctx->exclusive = 1; |
| 1232 | ctx->ns_rendered = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1233 | ctx->inclusive_ns_prefixes = inclusive_ns_prefixes; |
| 1234 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1235 | return (ctx); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /** |
| 1239 | * xmlC14NDocSaveTo: |
| 1240 | * @doc: the XML document for canonization |
| 1241 | * @nodes: the nodes set to be included in the canonized image |
| 1242 | * or NULL if all document nodes should be included |
| 1243 | * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
| 1244 | * otherwise - exclusive canonicalization) |
Daniel Veillard | db1bdba | 2002-03-09 14:13:11 +0000 | [diff] [blame] | 1245 | * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1246 | * ended with a NULL or NULL if there is no |
| 1247 | * inclusive namespaces (only for exclusive |
| 1248 | * canonicalization, ignored otherwise) |
| 1249 | * @with_comments: include comments in the result (!=0) or not (==0) |
| 1250 | * @buf: the output buffer to store canonical XML; this |
| 1251 | * buffer MUST have encoder==NULL because C14N requires |
| 1252 | * UTF-8 output |
| 1253 | * |
| 1254 | * Dumps the canonized image of given XML document into the provided buffer. |
| 1255 | * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
| 1256 | * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
| 1257 | * |
| 1258 | * Returns non-negative value on success or a negative value on fail |
| 1259 | */ |
| 1260 | int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1261 | xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes, |
| 1262 | int exclusive, xmlChar ** inclusive_ns_prefixes, |
| 1263 | int with_comments, xmlOutputBufferPtr buf) |
| 1264 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1265 | xmlC14NCtxPtr ctx; |
| 1266 | int ret; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1267 | |
| 1268 | if ((buf == NULL) || (doc == NULL)) { |
| 1269 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1270 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1271 | "xmlC14NDocSaveTo: null return buffer or doc pointer\n"); |
| 1272 | #endif |
| 1273 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * Validate the encoding output buffer encoding |
| 1278 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1279 | if (buf->encoder != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1280 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1281 | "xmlC14NDocSaveTo: output buffer encoder != NULL but C14N requires UTF8 output\n"); |
| 1282 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | ctx = xmlC14NNewCtx(doc, nodes, exclusive, inclusive_ns_prefixes, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1286 | with_comments, buf); |
| 1287 | if (ctx == NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1288 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1289 | "xmlC14NDocSaveTo: unable to create C14N context\n"); |
| 1290 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | /* |
| 1296 | * Root Node |
| 1297 | * The root node is the parent of the top-level document element. The |
| 1298 | * result of processing each of its child nodes that is in the node-set |
| 1299 | * in document order. The root node does not generate a byte order mark, |
| 1300 | * XML declaration, nor anything from within the document type |
| 1301 | * declaration. |
| 1302 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1303 | if (doc->children != NULL) { |
| 1304 | ret = xmlC14NProcessNodeList(ctx, doc->children); |
| 1305 | if (ret < 0) { |
| 1306 | #ifdef DEBUG_C14N |
| 1307 | xmlGenericError(xmlGenericErrorContext, |
| 1308 | "xmlC14NDocSaveTo: process childrens' list failed.\n"); |
| 1309 | #endif |
| 1310 | xmlC14NFreeCtx(ctx); |
| 1311 | return (-1); |
| 1312 | } |
| 1313 | } |
| 1314 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1315 | /* |
| 1316 | * Flush buffer to get number of bytes written |
| 1317 | */ |
| 1318 | ret = xmlOutputBufferFlush(buf); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1319 | if (ret < 0) { |
| 1320 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1321 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1322 | "xmlC14NDocSaveTo: buffer flush failed.\n"); |
| 1323 | #endif |
| 1324 | xmlC14NFreeCtx(ctx); |
| 1325 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Cleanup |
| 1330 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1331 | xmlC14NFreeCtx(ctx); |
| 1332 | return (ret); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * xmlC14NDocDumpMemory: |
| 1337 | * @doc: the XML document for canonization |
| 1338 | * @nodes: the nodes set to be included in the canonized image |
| 1339 | * or NULL if all document nodes should be included |
| 1340 | * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
| 1341 | * otherwise - exclusive canonicalization) |
Daniel Veillard | db1bdba | 2002-03-09 14:13:11 +0000 | [diff] [blame] | 1342 | * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1343 | * ended with a NULL or NULL if there is no |
| 1344 | * inclusive namespaces (only for exclusive |
| 1345 | * canonicalization, ignored otherwise) |
| 1346 | * @with_comments: include comments in the result (!=0) or not (==0) |
| 1347 | * @doc_txt_ptr: the memory pointer for allocated canonical XML text; |
| 1348 | * the caller of this functions is responsible for calling |
| 1349 | * xmlFree() to free allocated memory |
| 1350 | * |
| 1351 | * Dumps the canonized image of given XML document into memory. |
| 1352 | * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
| 1353 | * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
| 1354 | * |
| 1355 | * Returns the number of bytes written on success or a negative value on fail |
| 1356 | */ |
| 1357 | int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1358 | xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes, |
| 1359 | int exclusive, xmlChar ** inclusive_ns_prefixes, |
| 1360 | int with_comments, xmlChar ** doc_txt_ptr) |
| 1361 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1362 | int ret; |
| 1363 | xmlOutputBufferPtr buf; |
| 1364 | |
| 1365 | if (doc_txt_ptr == NULL) { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1366 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1367 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1368 | "xmlC14NDocDumpMemory: null return buffer pointer\n"); |
| 1369 | #endif |
| 1370 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | *doc_txt_ptr = NULL; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1374 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1375 | /* |
| 1376 | * create memory buffer with UTF8 (default) encoding |
| 1377 | */ |
| 1378 | buf = xmlAllocOutputBuffer(NULL); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1379 | if (buf == NULL) { |
| 1380 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1381 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1382 | "xmlC14NDocDumpMemory: failed to allocate output buffer.\n"); |
| 1383 | #endif |
| 1384 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * canonize document and write to buffer |
| 1389 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1390 | ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes, |
| 1391 | with_comments, buf); |
| 1392 | if (ret < 0) { |
| 1393 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1394 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1395 | "xmlC14NDocDumpMemory: xmlC14NDocSaveTo failed.\n"); |
| 1396 | #endif |
| 1397 | (void) xmlOutputBufferClose(buf); |
| 1398 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1399 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1400 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1401 | ret = buf->buffer->use; |
| 1402 | if (ret > 0) { |
| 1403 | *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret); |
| 1404 | } |
| 1405 | (void) xmlOutputBufferClose(buf); |
| 1406 | |
| 1407 | if ((*doc_txt_ptr == NULL) && (ret > 0)) { |
| 1408 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1409 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1410 | "xmlC14NDocDumpMemory: failed to allocate memory for document text representation\n"); |
| 1411 | #endif |
| 1412 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1413 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1414 | return (ret); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * xmlC14NDocSave: |
| 1419 | * @doc: the XML document for canonization |
| 1420 | * @nodes: the nodes set to be included in the canonized image |
| 1421 | * or NULL if all document nodes should be included |
| 1422 | * @exclusive: the exclusive flag (0 - non-exclusive canonicalization; |
| 1423 | * otherwise - exclusive canonicalization) |
Daniel Veillard | db1bdba | 2002-03-09 14:13:11 +0000 | [diff] [blame] | 1424 | * @inclusive_ns_prefixes: the list of inclusive namespace prefixes |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1425 | * ended with a NULL or NULL if there is no |
| 1426 | * inclusive namespaces (only for exclusive |
| 1427 | * canonicalization, ignored otherwise) |
| 1428 | * @with_comments: include comments in the result (!=0) or not (==0) |
| 1429 | * @filename: the filename to store canonical XML image |
| 1430 | * @compression: the compression level (zlib requred): |
| 1431 | * -1 - libxml default, |
| 1432 | * 0 - uncompressed, |
| 1433 | * >0 - compression level |
| 1434 | * |
| 1435 | * Dumps the canonized image of given XML document into the file. |
| 1436 | * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or |
| 1437 | * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n) |
| 1438 | * |
| 1439 | * Returns the number of bytes written success or a negative value on fail |
| 1440 | */ |
| 1441 | int |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1442 | xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, |
| 1443 | int exclusive, xmlChar ** inclusive_ns_prefixes, |
| 1444 | int with_comments, const char *filename, int compression) |
| 1445 | { |
| 1446 | xmlOutputBufferPtr buf; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1447 | int ret; |
| 1448 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1449 | if (filename == NULL) { |
| 1450 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1451 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1452 | "xmlC14NDocSave: filename is NULL\n"); |
| 1453 | #endif |
| 1454 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1455 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1456 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1457 | if (compression < 0) |
| 1458 | compression = xmlGetCompressMode(); |
| 1459 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1460 | |
| 1461 | /* |
| 1462 | * save the content to a temp buffer, use default UTF8 encoding. |
| 1463 | */ |
| 1464 | buf = xmlOutputBufferCreateFilename(filename, NULL, compression); |
| 1465 | if (buf == NULL) { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1466 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1467 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1468 | "xmlC14NDocSave: unable to create buffer for file=\"%s\" with compressin=%d\n", |
| 1469 | filename, compression); |
| 1470 | #endif |
| 1471 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1472 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1473 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1474 | /* |
| 1475 | * canonize document and write to buffer |
| 1476 | */ |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1477 | ret = xmlC14NDocSaveTo(doc, nodes, exclusive, inclusive_ns_prefixes, |
| 1478 | with_comments, buf); |
| 1479 | if (ret < 0) { |
| 1480 | #ifdef DEBUG_C14N |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1481 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1482 | "xmlC14NDocSave: xmlC14NDocSaveTo failed.\n"); |
| 1483 | #endif |
| 1484 | (void) xmlOutputBufferClose(buf); |
| 1485 | return (-1); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1486 | } |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1487 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1488 | /* |
| 1489 | * get the numbers of bytes written |
| 1490 | */ |
| 1491 | ret = xmlOutputBufferClose(buf); |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1492 | return (ret); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | /* |
| 1498 | * Macro used to grow the current buffer. |
| 1499 | */ |
| 1500 | #define growBufferReentrant() { \ |
| 1501 | buffer_size *= 2; \ |
| 1502 | buffer = (xmlChar *) \ |
| 1503 | xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ |
| 1504 | if (buffer == NULL) { \ |
| 1505 | perror("realloc failed"); \ |
| 1506 | return(NULL); \ |
| 1507 | } \ |
| 1508 | } |
| 1509 | |
| 1510 | /** |
| 1511 | * xmlC11NNormalizeString: |
| 1512 | * @input: the input string |
| 1513 | * @mode: the normalization mode (attribute, comment, PI or text) |
| 1514 | * |
| 1515 | * Converts a string to a canonical (normalized) format. The code is stolen |
| 1516 | * from xmlEncodeEntitiesReentrant(). Added normalization of \x09, \x0a, \x0A |
| 1517 | * and the @mode parameter |
| 1518 | * |
| 1519 | * Returns a normalized string (caller is responsible for calling xmlFree()) |
| 1520 | * or NULL if an error occurs |
| 1521 | */ |
| 1522 | static xmlChar * |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1523 | xmlC11NNormalizeString(const xmlChar * input, |
| 1524 | xmlC14NNormalizationMode mode) |
| 1525 | { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1526 | const xmlChar *cur = input; |
| 1527 | xmlChar *buffer = NULL; |
| 1528 | xmlChar *out = NULL; |
| 1529 | int buffer_size = 0; |
| 1530 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1531 | if (input == NULL) |
| 1532 | return (NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1533 | |
| 1534 | /* |
| 1535 | * allocate an translation buffer. |
| 1536 | */ |
| 1537 | buffer_size = 1000; |
| 1538 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
| 1539 | if (buffer == NULL) { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1540 | perror("malloc failed"); |
| 1541 | return (NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1542 | } |
| 1543 | out = buffer; |
| 1544 | |
| 1545 | while (*cur != '\0') { |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1546 | if ((out - buffer) > (buffer_size - 10)) { |
| 1547 | int indx = out - buffer; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1548 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1549 | growBufferReentrant(); |
| 1550 | out = &buffer[indx]; |
| 1551 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1552 | |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1553 | if ((*cur == '<') && ((mode == XMLC14N_NORMALIZE_ATTR) || |
| 1554 | (mode == XMLC14N_NORMALIZE_TEXT))) { |
| 1555 | *out++ = '&'; |
| 1556 | *out++ = 'l'; |
| 1557 | *out++ = 't'; |
| 1558 | *out++ = ';'; |
| 1559 | } else if ((*cur == '>') && (mode == XMLC14N_NORMALIZE_TEXT)) { |
| 1560 | *out++ = '&'; |
| 1561 | *out++ = 'g'; |
| 1562 | *out++ = 't'; |
| 1563 | *out++ = ';'; |
| 1564 | } else if ((*cur == '&') && ((mode == XMLC14N_NORMALIZE_ATTR) || |
| 1565 | (mode == XMLC14N_NORMALIZE_TEXT))) { |
| 1566 | *out++ = '&'; |
| 1567 | *out++ = 'a'; |
| 1568 | *out++ = 'm'; |
| 1569 | *out++ = 'p'; |
| 1570 | *out++ = ';'; |
| 1571 | } else if ((*cur == '"') && (mode == XMLC14N_NORMALIZE_ATTR)) { |
| 1572 | *out++ = '&'; |
| 1573 | *out++ = 'q'; |
| 1574 | *out++ = 'u'; |
| 1575 | *out++ = 'o'; |
| 1576 | *out++ = 't'; |
| 1577 | *out++ = ';'; |
| 1578 | } else if ((*cur == '\x09') && (mode == XMLC14N_NORMALIZE_ATTR)) { |
| 1579 | *out++ = '&'; |
| 1580 | *out++ = '#'; |
| 1581 | *out++ = 'x'; |
| 1582 | *out++ = '9'; |
| 1583 | *out++ = ';'; |
| 1584 | } else if ((*cur == '\x0A') && (mode == XMLC14N_NORMALIZE_ATTR)) { |
| 1585 | *out++ = '&'; |
| 1586 | *out++ = '#'; |
| 1587 | *out++ = 'x'; |
| 1588 | *out++ = 'A'; |
| 1589 | *out++ = ';'; |
| 1590 | } else if ((*cur == '\x0D') && ((mode == XMLC14N_NORMALIZE_ATTR) || |
| 1591 | (mode == XMLC14N_NORMALIZE_TEXT) || |
| 1592 | (mode == XMLC14N_NORMALIZE_COMMENT) || |
| 1593 | (mode == XMLC14N_NORMALIZE_PI))) { |
| 1594 | *out++ = '&'; |
| 1595 | *out++ = '#'; |
| 1596 | *out++ = 'x'; |
| 1597 | *out++ = 'D'; |
| 1598 | *out++ = ';'; |
| 1599 | } else { |
| 1600 | /* |
| 1601 | * Works because on UTF-8, all extended sequences cannot |
| 1602 | * result in bytes in the ASCII range. |
| 1603 | */ |
| 1604 | *out++ = *cur; |
| 1605 | } |
| 1606 | cur++; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1607 | } |
| 1608 | *out++ = 0; |
Daniel Veillard | 9ff8817 | 2002-03-11 09:15:32 +0000 | [diff] [blame] | 1609 | return (buffer); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1610 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1611 | #endif /* LIBXML_C14N_ENABLED */ |