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