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