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