Diff for /embedaddon/libxml2/c14n.c between versions 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2012/02/21 23:37:58 version 1.1.1.2, 2014/06/15 19:53:29
Line 1 Line 1
 /*  /*
 * "Canonical XML" implementation  * "Canonical XML" implementation
  * http://www.w3.org/TR/xml-c14n   * http://www.w3.org/TR/xml-c14n
 *  *
  * "Exclusive XML Canonicalization" implementation   * "Exclusive XML Canonicalization" implementation
  * http://www.w3.org/TR/xml-exc-c14n   * http://www.w3.org/TR/xml-exc-c14n
  *   *
  * See Copyright for the status of this software.   * See Copyright for the status of this software.
 *  *
  * Author: Aleksey Sanin <aleksey@aleksey.com>   * Author: Aleksey Sanin <aleksey@aleksey.com>
  */   */
 #define IN_LIBXML  #define IN_LIBXML
Line 27 Line 27
 #include <libxml/xpathInternals.h>  #include <libxml/xpathInternals.h>
 #include <libxml/c14n.h>  #include <libxml/c14n.h>
   
   #include "buf.h"
   
 /************************************************************************  /************************************************************************
  *                                                                      *   *                                                                      *
  *              Some declaration better left private ATM                *   *              Some declaration better left private ATM                *
Line 44  typedef struct _xmlC14NVisibleNsStack { Line 46  typedef struct _xmlC14NVisibleNsStack {
     int nsPrevStart;        /* the begginning of the stack for previous visible node */      int nsPrevStart;        /* the begginning of the stack for previous visible node */
     int nsPrevEnd;          /* the end of the stack for previous visible node */      int nsPrevEnd;          /* the end of the stack for previous visible node */
     int nsMax;              /* size of the array as allocated */      int nsMax;              /* size of the array as allocated */
    xmlNsPtr    *nsTab;     /* array of ns in no particular order */              xmlNsPtr    *nsTab;     /* array of ns in no particular order */
     xmlNodePtr  *nodeTab;   /* array of nodes in no particular order */      xmlNodePtr  *nodeTab;   /* array of nodes in no particular order */
 } xmlC14NVisibleNsStack, *xmlC14NVisibleNsStackPtr;  } xmlC14NVisibleNsStack, *xmlC14NVisibleNsStackPtr;
   
Line 52  typedef struct _xmlC14NCtx { Line 54  typedef struct _xmlC14NCtx {
     /* input parameters */      /* input parameters */
     xmlDocPtr doc;      xmlDocPtr doc;
     xmlC14NIsVisibleCallback is_visible_callback;      xmlC14NIsVisibleCallback is_visible_callback;
    void* user_data;        void* user_data;
     int with_comments;      int with_comments;
     xmlOutputBufferPtr buf;      xmlOutputBufferPtr buf;
   
Line 60  typedef struct _xmlC14NCtx { Line 62  typedef struct _xmlC14NCtx {
     xmlC14NPosition pos;      xmlC14NPosition pos;
     int parent_is_doc;      int parent_is_doc;
     xmlC14NVisibleNsStackPtr ns_rendered;      xmlC14NVisibleNsStackPtr ns_rendered;
    
     /* C14N mode */      /* C14N mode */
     xmlC14NMode mode;      xmlC14NMode mode;
   
Line 73  typedef struct _xmlC14NCtx { Line 75  typedef struct _xmlC14NCtx {
   
 static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate     (void);  static xmlC14NVisibleNsStackPtr xmlC14NVisibleNsStackCreate     (void);
 static void     xmlC14NVisibleNsStackDestroy    (xmlC14NVisibleNsStackPtr cur);  static void     xmlC14NVisibleNsStackDestroy    (xmlC14NVisibleNsStackPtr cur);
static void     xmlC14NVisibleNsStackAdd            (xmlC14NVisibleNsStackPtr cur, static void     xmlC14NVisibleNsStackAdd            (xmlC14NVisibleNsStackPtr cur,
                                                  xmlNsPtr ns,                                                   xmlNsPtr ns,
                                                  xmlNodePtr node);                                                   xmlNodePtr node);
static void                     xmlC14NVisibleNsStackSave       (xmlC14NVisibleNsStackPtr cur,static void                     xmlC14NVisibleNsStackSave       (xmlC14NVisibleNsStackPtr cur,
                                                                  xmlC14NVisibleNsStackPtr state);                                                                   xmlC14NVisibleNsStackPtr state);
static void                     xmlC14NVisibleNsStackRestore    (xmlC14NVisibleNsStackPtr cur,static void                     xmlC14NVisibleNsStackRestore    (xmlC14NVisibleNsStackPtr cur,
                                                                  xmlC14NVisibleNsStackPtr state);                                                                   xmlC14NVisibleNsStackPtr state);
static void                     xmlC14NVisibleNsStackShift      (xmlC14NVisibleNsStackPtr cur);static void                     xmlC14NVisibleNsStackShift      (xmlC14NVisibleNsStackPtr cur);
static int                      xmlC14NVisibleNsStackFind       (xmlC14NVisibleNsStackPtr cur, static int                      xmlC14NVisibleNsStackFind       (xmlC14NVisibleNsStackPtr cur,
                                                                  xmlNsPtr ns);                                                                   xmlNsPtr ns);
static int                      xmlExcC14NVisibleNsStackFind    (xmlC14NVisibleNsStackPtr cur, static int                      xmlExcC14NVisibleNsStackFind    (xmlC14NVisibleNsStackPtr cur,
                                                                  xmlNsPtr ns,                                                                   xmlNsPtr ns,
                                                                  xmlC14NCtxPtr ctx);                                                                   xmlC14NCtxPtr ctx);
   
Line 105  typedef enum { Line 107  typedef enum {
 static xmlChar *xmlC11NNormalizeString(const xmlChar * input,  static xmlChar *xmlC11NNormalizeString(const xmlChar * input,
                                        xmlC14NNormalizationMode mode);                                         xmlC14NNormalizationMode mode);
   
#define         xmlC11NNormalizeAttr( a ) \#define xmlC11NNormalizeAttr( a ) \
     xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR)      xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_ATTR)
#define         xmlC11NNormalizeComment( a ) \#define xmlC11NNormalizeComment( a ) \
     xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT)      xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_COMMENT)
#define         xmlC11NNormalizePI( a ) \#define xmlC11NNormalizePI( a ) \
     xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI)      xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_PI)
#define         xmlC11NNormalizeText( a ) \#define xmlC11NNormalizeText( a ) \
     xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT)      xmlC11NNormalizeString((a), XMLC14N_NORMALIZE_TEXT)
   
#define         xmlC14NIsVisible( ctx, node, parent ) \#define xmlC14NIsVisible( ctx, node, parent ) \
      (((ctx)->is_visible_callback != NULL) ? \       (((ctx)->is_visible_callback != NULL) ? \
         (ctx)->is_visible_callback((ctx)->user_data, \          (ctx)->is_visible_callback((ctx)->user_data, \
                 (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1)                  (xmlNodePtr)(node), (xmlNodePtr)(parent)) : 1)
   
#define         xmlC14NIsExclusive( ctx ) \#define xmlC14NIsExclusive( ctx ) \
     ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 )      ( (ctx)->mode == XML_C14N_EXCLUSIVE_1_0 )
   
 /************************************************************************  /************************************************************************
  *                                                                      *   *                                                                      *
 *              Some factorized error routines                          * *                Some factorized error routines                          *
  *                                                                      *   *                                                                      *
  ************************************************************************/   ************************************************************************/
   
Line 249  xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int er Line 251  xmlC14NErr(xmlC14NCtxPtr ctxt, xmlNodePtr node, int er
  ************************************************************************/   ************************************************************************/
 #define XML_NAMESPACES_DEFAULT          16  #define XML_NAMESPACES_DEFAULT          16
   
static int                      static int
 xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes, xmlNodePtr node, xmlNodePtr parent) {  xmlC14NIsNodeInNodeset(xmlNodeSetPtr nodes, xmlNodePtr node, xmlNodePtr parent) {
     if((nodes != NULL) && (node != NULL)) {      if((nodes != NULL) && (node != NULL)) {
         if(node->type != XML_NAMESPACE_DECL) {          if(node->type != XML_NAMESPACE_DECL) {
             return(xmlXPathNodeSetContains(nodes, node));              return(xmlXPathNodeSetContains(nodes, node));
         } else {          } else {
             xmlNs ns;              xmlNs ns;
            
            memcpy(&ns, node, sizeof(ns));             memcpy(&ns, node, sizeof(ns));
            
             /* this is a libxml hack! check xpath.c for details */              /* this is a libxml hack! check xpath.c for details */
             if((parent != NULL) && (parent->type == XML_ATTRIBUTE_NODE)) {              if((parent != NULL) && (parent->type == XML_ATTRIBUTE_NODE)) {
                 ns.next = (xmlNsPtr)parent->parent;                  ns.next = (xmlNsPtr)parent->parent;
             } else {              } else {
                ns.next = (xmlNsPtr)parent;                 ns.next = (xmlNsPtr)parent;
             }              }
   
            /*             /*
             * If the input is an XPath node-set, then the node-set must explicitly              * If the input is an XPath node-set, then the node-set must explicitly
              * contain every node to be rendered to the canonical form.               * contain every node to be rendered to the canonical form.
              */               */
             return(xmlXPathNodeSetContains(nodes, (xmlNodePtr)&ns));              return(xmlXPathNodeSetContains(nodes, (xmlNodePtr)&ns));
Line 305  xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr  Line 307  xmlC14NVisibleNsStackDestroy(xmlC14NVisibleNsStackPtr 
     }      }
     memset(cur, 0, sizeof(xmlC14NVisibleNsStack));      memset(cur, 0, sizeof(xmlC14NVisibleNsStack));
     xmlFree(cur);      xmlFree(cur);
    
 }  }
   
static void static void
 xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlNodePtr node) {  xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlNodePtr node) {
    if((cur == NULL) ||     if((cur == NULL) ||
        ((cur->nsTab == NULL) && (cur->nodeTab != NULL)) ||         ((cur->nsTab == NULL) && (cur->nodeTab != NULL)) ||
        ((cur->nsTab != NULL) && (cur->nodeTab == NULL))) {         ((cur->nsTab != NULL) && (cur->nodeTab == NULL))) {
         xmlC14NErrParam("adding namespace to stack");          xmlC14NErrParam("adding namespace to stack");
Line 328  xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur, Line 330  xmlC14NVisibleNsStackAdd(xmlC14NVisibleNsStackPtr cur,
         memset(cur->nodeTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));          memset(cur->nodeTab, 0 , XML_NAMESPACES_DEFAULT * sizeof(xmlNodePtr));
         cur->nsMax = XML_NAMESPACES_DEFAULT;          cur->nsMax = XML_NAMESPACES_DEFAULT;
     } else if(cur->nsMax == cur->nsCurEnd) {      } else if(cur->nsMax == cur->nsCurEnd) {
        void *tmp;              void *tmp;
         int tmpSize;          int tmpSize;
        
         tmpSize = 2 * cur->nsMax;          tmpSize = 2 * cur->nsMax;
         tmp = xmlRealloc(cur->nsTab, tmpSize * sizeof(xmlNsPtr));          tmp = xmlRealloc(cur->nsTab, tmpSize * sizeof(xmlNsPtr));
         if (tmp == NULL) {          if (tmp == NULL) {
Line 360  xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur Line 362  xmlC14NVisibleNsStackSave(xmlC14NVisibleNsStackPtr cur
         xmlC14NErrParam("saving namespaces stack");          xmlC14NErrParam("saving namespaces stack");
         return;          return;
     }      }
    
     state->nsCurEnd = cur->nsCurEnd;      state->nsCurEnd = cur->nsCurEnd;
     state->nsPrevStart = cur->nsPrevStart;      state->nsPrevStart = cur->nsPrevStart;
     state->nsPrevEnd = cur->nsPrevEnd;      state->nsPrevEnd = cur->nsPrevEnd;
Line 377  xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr  Line 379  xmlC14NVisibleNsStackRestore(xmlC14NVisibleNsStackPtr 
     cur->nsPrevEnd = state->nsPrevEnd;      cur->nsPrevEnd = state->nsPrevEnd;
 }  }
   
static void static void
 xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur) {  xmlC14NVisibleNsStackShift(xmlC14NVisibleNsStackPtr cur) {
     if(cur == NULL) {      if(cur == NULL) {
         xmlC14NErrParam("shifting namespaces stack");          xmlC14NErrParam("shifting namespaces stack");
Line 400  xmlC14NStrEqual(const xmlChar *str1, const xmlChar *st Line 402  xmlC14NStrEqual(const xmlChar *str1, const xmlChar *st
   
 /**  /**
  * xmlC14NVisibleNsStackFind:   * xmlC14NVisibleNsStackFind:
 * @ctx:                the C14N context  * @ctx:                the C14N context
  * @ns:                 the namespace to check   * @ns:                 the namespace to check
  *   *
  * Checks whether the given namespace was already rendered or not   * Checks whether the given namespace was already rendered or not
Line 414  xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur Line 416  xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
     const xmlChar *prefix;      const xmlChar *prefix;
     const xmlChar *href;      const xmlChar *href;
     int has_empty_ns;      int has_empty_ns;
        
     if(cur == NULL) {      if(cur == NULL) {
         xmlC14NErrParam("searching namespaces stack (c14n)");          xmlC14NErrParam("searching namespaces stack (c14n)");
         return (0);          return (0);
     }      }
   
     /*      /*
     * if the default namespace xmlns="" is not defined yet then      * if the default namespace xmlns="" is not defined yet then
      * we do not want to print it out       * we do not want to print it out
      */       */
     prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;      prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
Line 432  xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur Line 434  xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
         int start = (has_empty_ns) ? 0 : cur->nsPrevStart;          int start = (has_empty_ns) ? 0 : cur->nsPrevStart;
         for (i = cur->nsCurEnd - 1; i >= start; --i) {          for (i = cur->nsCurEnd - 1; i >= start; --i) {
             xmlNsPtr ns1 = cur->nsTab[i];              xmlNsPtr ns1 = cur->nsTab[i];
            
             if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {              if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
                 return(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL));                  return(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL));
             }              }
Line 441  xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur Line 443  xmlC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur
     return(has_empty_ns);      return(has_empty_ns);
 }  }
   
static int                      static int
 xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlC14NCtxPtr ctx) {  xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr cur, xmlNsPtr ns, xmlC14NCtxPtr ctx) {
     int i;      int i;
     const xmlChar *prefix;      const xmlChar *prefix;
     const xmlChar *href;      const xmlChar *href;
     int has_empty_ns;      int has_empty_ns;
        
     if(cur == NULL) {      if(cur == NULL) {
         xmlC14NErrParam("searching namespaces stack (exc c14n)");          xmlC14NErrParam("searching namespaces stack (exc c14n)");
         return (0);          return (0);
     }      }
   
     /*      /*
     * if the default namespace xmlns="" is not defined yet then      * if the default namespace xmlns="" is not defined yet then
      * we do not want to print it out       * we do not want to print it out
      */       */
     prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;      prefix = ((ns == NULL) || (ns->prefix == NULL)) ? BAD_CAST "" : ns->prefix;
Line 465  xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr  Line 467  xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr 
         int start = 0;          int start = 0;
         for (i = cur->nsCurEnd - 1; i >= start; --i) {          for (i = cur->nsCurEnd - 1; i >= start; --i) {
             xmlNsPtr ns1 = cur->nsTab[i];              xmlNsPtr ns1 = cur->nsTab[i];
            
             if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {              if(xmlC14NStrEqual(prefix, (ns1 != NULL) ? ns1->prefix : NULL)) {
                 if(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)) {                  if(xmlC14NStrEqual(href, (ns1 != NULL) ? ns1->href : NULL)) {
                    return(xmlC14NIsVisible(ctx, ns1, cur->nodeTab[i]));                    return(xmlC14NIsVisible(ctx, ns1, cur->nodeTab[i]));
                 } else {                  } else {
                     return(0);                      return(0);
                 }                  }
Line 483  xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr  Line 485  xmlExcC14NVisibleNsStackFind(xmlC14NVisibleNsStackPtr 
   
 /**  /**
  * xmlC14NIsXmlNs:   * xmlC14NIsXmlNs:
 * @ns:                 the namespace to check * @ns:         the namespace to check
 *               *
  * Checks whether the given namespace is a default "xml:" namespace   * Checks whether the given namespace is a default "xml:" namespace
  * with href="http://www.w3.org/XML/1998/namespace"   * with href="http://www.w3.org/XML/1998/namespace"
  *   *
Line 504  xmlC14NIsXmlNs(xmlNsPtr ns) Line 506  xmlC14NIsXmlNs(xmlNsPtr ns)
 /**  /**
  * xmlC14NNsCompare:   * xmlC14NNsCompare:
  * @ns1:                the pointer to first namespace   * @ns1:                the pointer to first namespace
 * @ns2:                the pointer to second namespace * @ns2:                the pointer to second namespace
  *   *
  * Compares the namespaces by names (prefixes).   * Compares the namespaces by names (prefixes).
  *   *
Line 527  xmlC14NNsCompare(xmlNsPtr ns1, xmlNsPtr ns2) Line 529  xmlC14NNsCompare(xmlNsPtr ns1, xmlNsPtr ns2)
 /**  /**
  * xmlC14NPrintNamespaces:   * xmlC14NPrintNamespaces:
  * @ns:                 the pointer to namespace   * @ns:                 the pointer to namespace
 * @ctx:                the C14N context * @ctx:                the C14N context
  *   *
  * Prints the given namespace to the output buffer from C14N context.   * Prints the given namespace to the output buffer from C14N context.
  *   *
Line 558  xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPt Line 560  xmlC14NPrintNamespaces(const xmlNsPtr ns, xmlC14NCtxPt
   
 /**  /**
  * xmlC14NProcessNamespacesAxis:   * xmlC14NProcessNamespacesAxis:
 * @ctx:                the C14N context * @ctx:                the C14N context
  * @node:               the current node   * @node:               the current node
  *   *
  * Prints out canonical namespace axis of the current node to the   * Prints out canonical namespace axis of the current node to the
 * buffer from C14N context as follows  * buffer from C14N context as follows
  *   *
  * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)   * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  *   *
  * Namespace Axis   * Namespace Axis
 * Consider a list L containing only namespace nodes in the  * Consider a list L containing only namespace nodes in the
 * axis and in the node-set in lexicographic order (ascending). To begin  * axis and in the node-set in lexicographic order (ascending). To begin
 * processing L, if the first node is not the default namespace node (a node  * processing L, if the first node is not the default namespace node (a node
 * with no namespace URI and no local name), then generate a space followed  * with no namespace URI and no local name), then generate a space followed
  * by xmlns="" if and only if the following conditions are met:   * by xmlns="" if and only if the following conditions are met:
  *    - the element E that owns the axis is in the node-set   *    - the element E that owns the axis is in the node-set
 *    - The nearest ancestor element of E in the node-set has a default  *    - The nearest ancestor element of E in the node-set has a default
 *          namespace node in the node-set (default namespace nodes always  *          namespace node in the node-set (default namespace nodes always
  *      have non-empty values in XPath)   *      have non-empty values in XPath)
 * The latter condition eliminates unnecessary occurrences of xmlns="" in  * The latter condition eliminates unnecessary occurrences of xmlns="" in
 * the canonical form since an element only receives an xmlns="" if its  * the canonical form since an element only receives an xmlns="" if its
 * default namespace is empty and if it has an immediate parent in the  * default namespace is empty and if it has an immediate parent in the
 * canonical form that has a non-empty default namespace. To finish  * canonical form that has a non-empty default namespace. To finish
 * processing  L, simply process every namespace node in L, except omit  * processing  L, simply process every namespace node in L, except omit
 * namespace node with local name xml, which defines the xml prefix,  * namespace node with local name xml, which defines the xml prefix,
  * if its string value is http://www.w3.org/XML/1998/namespace.   * if its string value is http://www.w3.org/XML/1998/namespace.
  *   *
  * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)   * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
 * Canonical XML applied to a document subset requires the search of the  * Canonical XML applied to a document subset requires the search of the
 * ancestor nodes of each orphan element node for attributes in the xml  * ancestor nodes of each orphan element node for attributes in the xml
 * namespace, such as xml:lang and xml:space. These are copied into the  * namespace, such as xml:lang and xml:space. These are copied into the
 * element node except if a declaration of the same attribute is already  * element node except if a declaration of the same attribute is already
 * in the attribute axis of the element (whether or not it is included in  * in the attribute axis of the element (whether or not it is included in
 * the document subset). This search and copying are omitted from the  * the document subset). This search and copying are omitted from the
  * Exclusive XML Canonicalization method.   * Exclusive XML Canonicalization method.
  *   *
  * Returns 0 on success or -1 on fail.   * Returns 0 on success or -1 on fail.
Line 603  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod Line 605  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod
     xmlListPtr list;      xmlListPtr list;
     int already_rendered;      int already_rendered;
     int has_empty_ns = 0;      int has_empty_ns = 0;
    
     if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {      if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
         xmlC14NErrParam("processing namespaces axis (c14n)");          xmlC14NErrParam("processing namespaces axis (c14n)");
         return (-1);          return (-1);
Line 622  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod Line 624  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod
     for(n = cur; n != NULL; n = n->parent) {      for(n = cur; n != NULL; n = n->parent) {
         for(ns = n->nsDef; ns != NULL; ns = ns->next) {          for(ns = n->nsDef; ns != NULL; ns = ns->next) {
             tmp = xmlSearchNs(cur->doc, cur, ns->prefix);              tmp = xmlSearchNs(cur->doc, cur, ns->prefix);
            
             if((tmp == ns) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {              if((tmp == ns) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
                 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);                  already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
                 if(visible) {                  if(visible) {
                    xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);            xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
                 }                  }
                 if(!already_rendered) {                  if(!already_rendered) {
                    xmlListInsert(list, ns);                     xmlListInsert(list, ns);
                 }                  }
                if(xmlStrlen(ns->prefix) == 0) {                if(xmlStrlen(ns->prefix) == 0) {
                     has_empty_ns = 1;                      has_empty_ns = 1;
                 }                  }
             }              }
         }          }
     }      }
        
     /**      /**
     * if the first node is not the default namespace node (a node with no      * if the first node is not the default namespace node (a node with no
     * namespace URI and no local name), then generate a space followed by      * namespace URI and no local name), then generate a space followed by
      * xmlns="" if and only if the following conditions are met:       * xmlns="" if and only if the following conditions are met:
      *  - the element E that owns the axis is in the node-set       *  - the element E that owns the axis is in the node-set
     *  - the nearest ancestor element of E in the node-set has a default      *  - the nearest ancestor element of E in the node-set has a default
     *     namespace node in the node-set (default namespace nodes always      *     namespace node in the node-set (default namespace nodes always
      *     have non-empty values in XPath)       *     have non-empty values in XPath)
      */       */
     if(visible && !has_empty_ns) {      if(visible && !has_empty_ns) {
Line 652  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod Line 654  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod
   
         memset(&ns_default, 0, sizeof(ns_default));          memset(&ns_default, 0, sizeof(ns_default));
         if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {          if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
            xmlC14NPrintNamespaces(&ns_default, ctx);            xmlC14NPrintNamespaces(&ns_default, ctx);
         }          }
     }      }
        
    
    /*     /*
     * print out all elements from list      * print out all elements from list
      */       */
     xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);      xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
   
    /*     /*
      * Cleanup       * Cleanup
      */       */
     xmlListDelete(list);      xmlListDelete(list);
Line 672  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod Line 674  xmlC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xmlNod
   
 /**  /**
  * xmlExcC14NProcessNamespacesAxis:   * xmlExcC14NProcessNamespacesAxis:
 * @ctx:                the C14N context * @ctx:                the C14N context
  * @node:               the current node   * @node:               the current node
  *   *
  * Prints out exclusive canonical namespace axis of the current node to the   * Prints out exclusive canonical namespace axis of the current node to the
 * buffer from C14N context as follows  * buffer from C14N context as follows
  *   *
  * Exclusive XML Canonicalization   * Exclusive XML Canonicalization
  * http://www.w3.org/TR/xml-exc-c14n   * http://www.w3.org/TR/xml-exc-c14n
  *   *
 * If the element node is in the XPath subset then output the node in  * If the element node is in the XPath subset then output the node in
 * accordance with Canonical XML except for namespace nodes which are  * accordance with Canonical XML except for namespace nodes which are
  * rendered as follows:   * rendered as follows:
  *   *
  * 1. Render each namespace node iff:   * 1. Render each namespace node iff:
 *    * it is visibly utilized by the immediate parent element or one of  *    * it is visibly utilized by the immediate parent element or one of
  *      its attributes, or is present in InclusiveNamespaces PrefixList, and   *      its attributes, or is present in InclusiveNamespaces PrefixList, and
 *    * its prefix and value do not appear in ns_rendered. ns_rendered is  *    * its prefix and value do not appear in ns_rendered. ns_rendered is
 *      obtained by popping the state stack in order to obtain a list of  *      obtained by popping the state stack in order to obtain a list of
 *      prefixes and their values which have already been rendered by  *      prefixes and their values which have already been rendered by
  *      an output ancestor of the namespace node's parent element.   *      an output ancestor of the namespace node's parent element.
 * 2. Append the rendered namespace node to the list ns_rendered of namespace  * 2. Append the rendered namespace node to the list ns_rendered of namespace
 * nodes rendered by output ancestors. Push ns_rendered on state stack and  * nodes rendered by output ancestors. Push ns_rendered on state stack and
  * recurse.   * recurse.
  * 3. After the recursion returns, pop thestate stack.   * 3. After the recursion returns, pop thestate stack.
  *   *
Line 710  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 712  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
     int has_empty_ns = 0;      int has_empty_ns = 0;
     int has_visibly_utilized_empty_ns = 0;      int has_visibly_utilized_empty_ns = 0;
     int has_empty_ns_in_inclusive_list = 0;      int has_empty_ns_in_inclusive_list = 0;
        
     if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {      if ((ctx == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
         xmlC14NErrParam("processing namespaces axis (exc c14n)");          xmlC14NErrParam("processing namespaces axis (exc c14n)");
         return (-1);          return (-1);
Line 731  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 733  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
         return (-1);          return (-1);
     }      }
   
    /*     /*
      * process inclusive namespaces:       * process inclusive namespaces:
     * All namespace nodes appearing on inclusive ns list are      * All namespace nodes appearing on inclusive ns list are
      * handled as provided in Canonical XML       * handled as provided in Canonical XML
      */       */
     if(ctx->inclusive_ns_prefixes != NULL) {      if(ctx->inclusive_ns_prefixes != NULL) {
        xmlChar *prefix;         xmlChar *prefix;
         int i;          int i;
        
         for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) {          for (i = 0; ctx->inclusive_ns_prefixes[i] != NULL; ++i) {
             prefix = ctx->inclusive_ns_prefixes[i];              prefix = ctx->inclusive_ns_prefixes[i];
             /*              /*
Line 750  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 752  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
                 prefix = NULL;                  prefix = NULL;
                 has_empty_ns_in_inclusive_list = 1;                  has_empty_ns_in_inclusive_list = 1;
             }              }
        
            ns = xmlSearchNs(cur->doc, cur, prefix);                    ns = xmlSearchNs(cur->doc, cur, prefix);
             if((ns != NULL) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {              if((ns != NULL) && !xmlC14NIsXmlNs(ns) && xmlC14NIsVisible(ctx, ns, cur)) {
                 already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);                  already_rendered = xmlC14NVisibleNsStackFind(ctx->ns_rendered, ns);
                 if(visible) {                  if(visible) {
                    xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);                    xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
                 }                  }
                 if(!already_rendered) {                  if(!already_rendered) {
                    xmlListInsert(list, ns);                     xmlListInsert(list, ns);
                 }                  }
                if(xmlStrlen(ns->prefix) == 0) {                if(xmlStrlen(ns->prefix) == 0) {
                     has_empty_ns = 1;                      has_empty_ns = 1;
                 }                  }
             }              }
         }          }
     }      }
    
     /* add node namespace */      /* add node namespace */
     if(cur->ns != NULL) {      if(cur->ns != NULL) {
         ns = cur->ns;          ns = cur->ns;
Line 775  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 777  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
         has_visibly_utilized_empty_ns = 1;          has_visibly_utilized_empty_ns = 1;
     }      }
     if((ns != NULL) && !xmlC14NIsXmlNs(ns)) {      if((ns != NULL) && !xmlC14NIsXmlNs(ns)) {
        if(visible && xmlC14NIsVisible(ctx, ns, cur)) {         if(visible && xmlC14NIsVisible(ctx, ns, cur)) {
             if(!xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, ns, ctx)) {              if(!xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, ns, ctx)) {
                 xmlListInsert(list, ns);                  xmlListInsert(list, ns);
             }              }
         }          }
         if(visible) {          if(visible) {
            xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);             xmlC14NVisibleNsStackAdd(ctx->ns_rendered, ns, cur);
         }          }
         if(xmlStrlen(ns->prefix) == 0) {          if(xmlStrlen(ns->prefix) == 0) {
             has_empty_ns = 1;              has_empty_ns = 1;
         }          }
     }      }
    
        
     /* add attributes */      /* add attributes */
     for(attr = cur->properties; attr != NULL; attr = attr->next) {      for(attr = cur->properties; attr != NULL; attr = attr->next) {
        /*         /*
          * we need to check that attribute is visible and has non           * we need to check that attribute is visible and has non
         * default namespace (XML Namespaces: "default namespaces          * default namespace (XML Namespaces: "default namespaces
         * do not apply directly to attributes")                  * do not apply directly to attributes")
          */           */
         if((attr->ns != NULL) && !xmlC14NIsXmlNs(attr->ns) && xmlC14NIsVisible(ctx, attr, cur)) {          if((attr->ns != NULL) && !xmlC14NIsXmlNs(attr->ns) && xmlC14NIsVisible(ctx, attr, cur)) {
             already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, attr->ns, ctx);              already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, attr->ns, ctx);
            xmlC14NVisibleNsStackAdd(ctx->ns_rendered, attr->ns, cur);             xmlC14NVisibleNsStackAdd(ctx->ns_rendered, attr->ns, cur);
             if(!already_rendered && visible) {              if(!already_rendered && visible) {
                xmlListInsert(list, attr->ns);                 xmlListInsert(list, attr->ns);
             }              }
             if(xmlStrlen(attr->ns->prefix) == 0) {              if(xmlStrlen(attr->ns->prefix) == 0) {
                 has_empty_ns = 1;                  has_empty_ns = 1;
Line 813  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 815  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
     /*      /*
      * Process xmlns=""       * Process xmlns=""
      */       */
    if(visible && has_visibly_utilized_empty_ns &&     if(visible && has_visibly_utilized_empty_ns &&
             !has_empty_ns && !has_empty_ns_in_inclusive_list) {              !has_empty_ns && !has_empty_ns_in_inclusive_list) {
         static xmlNs ns_default;          static xmlNs ns_default;
   
         memset(&ns_default, 0, sizeof(ns_default));          memset(&ns_default, 0, sizeof(ns_default));
        
         already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default, ctx);          already_rendered = xmlExcC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default, ctx);
         if(!already_rendered) {          if(!already_rendered) {
            xmlC14NPrintNamespaces(&ns_default, ctx);            xmlC14NPrintNamespaces(&ns_default, ctx);
         }          }
     } else if(visible && !has_empty_ns && has_empty_ns_in_inclusive_list) {      } else if(visible && !has_empty_ns && has_empty_ns_in_inclusive_list) {
         static xmlNs ns_default;          static xmlNs ns_default;
   
         memset(&ns_default, 0, sizeof(ns_default));          memset(&ns_default, 0, sizeof(ns_default));
         if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {          if(!xmlC14NVisibleNsStackFind(ctx->ns_rendered, &ns_default)) {
            xmlC14NPrintNamespaces(&ns_default, ctx);            xmlC14NPrintNamespaces(&ns_default, ctx);
         }          }
     }      }
   
       
   
    /* 
     * print out all elements from list     /*
      * print out all elements from list
      */       */
     xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);      xmlListWalk(list, (xmlListWalker) xmlC14NPrintNamespaces, (const void *) ctx);
   
    /*     /*
      * Cleanup       * Cleanup
      */       */
     xmlListDelete(list);      xmlListDelete(list);
Line 849  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 851  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
   
 /**  /**
  * xmlC14NIsXmlAttr:   * xmlC14NIsXmlAttr:
 * @attr:               the attr to check * @attr:               the attr to check
 *               *
  * Checks whether the given attribute is a default "xml:" namespace   * Checks whether the given attribute is a default "xml:" namespace
  * with href="http://www.w3.org/XML/1998/namespace"   * with href="http://www.w3.org/XML/1998/namespace"
  *   *
Line 861  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml Line 863  xmlExcC14NProcessNamespacesAxis(xmlC14NCtxPtr ctx, xml
 static int  static int
 xmlC14NIsXmlAttr(xmlAttrPtr attr)  xmlC14NIsXmlAttr(xmlAttrPtr attr)
 {  {
    return ((attr->ns != NULL) &&     return ((attr->ns != NULL) &&
            (xmlC14NIsXmlNs(attr->ns) != 0));             (xmlC14NIsXmlNs(attr->ns) != 0));
 }  }
   
Line 869  xmlC14NIsXmlAttr(xmlAttrPtr attr) Line 871  xmlC14NIsXmlAttr(xmlAttrPtr attr)
 /**  /**
  * xmlC14NAttrsCompare:   * xmlC14NAttrsCompare:
  * @attr1:              the pointer tls o first attr   * @attr1:              the pointer tls o first attr
 * @attr2:              the pointer to second attr * @attr2:              the pointer to second attr
  *   *
  * Prints the given attribute to the output buffer from C14N context.   * Prints the given attribute to the output buffer from C14N context.
  *   *
Line 893  xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2 Line 895  xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2
         return (xmlStrcmp(attr1->name, attr2->name));          return (xmlStrcmp(attr1->name, attr2->name));
     }      }
   
    /*     /*
      * Attributes in the default namespace are first       * Attributes in the default namespace are first
      * because the default namespace is not applied to       * because the default namespace is not applied to
      * unqualified attributes       * unqualified attributes
Line 918  xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2 Line 920  xmlC14NAttrsCompare(xmlAttrPtr attr1, xmlAttrPtr attr2
 /**  /**
  * xmlC14NPrintAttrs:   * xmlC14NPrintAttrs:
  * @attr:               the pointer to attr   * @attr:               the pointer to attr
 * @ctx:                the C14N context * @ctx:                the C14N context
  *   *
  * Prints out canonical attribute urrent node to the   * Prints out canonical attribute urrent node to the
 * buffer from C14N context as follows  * buffer from C14N context as follows
  *   *
  * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)   * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  *   *
Line 968  xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr Line 970  xmlC14NPrintAttrs(const xmlAttrPtr attr, xmlC14NCtxPtr
  * xmlC14NFindHiddenParentAttr:   * xmlC14NFindHiddenParentAttr:
  *   *
  * Finds an attribute in a hidden parent node.   * Finds an attribute in a hidden parent node.
 *  *
  * Returns a pointer to the attribute node (if found) or NULL otherwise.   * Returns a pointer to the attribute node (if found) or NULL otherwise.
  */   */
 static xmlAttrPtr  static xmlAttrPtr
Line 996  xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx, xmlNode Line 998  xmlC14NFindHiddenParentAttr(xmlC14NCtxPtr ctx, xmlNode
  */   */
 static xmlAttrPtr  static xmlAttrPtr
 xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml_base_attr)  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml_base_attr)
{    {
     xmlChar * res = NULL;      xmlChar * res = NULL;
     xmlNodePtr cur;      xmlNodePtr cur;
     xmlAttrPtr attr;      xmlAttrPtr attr;
Line 1028  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml Line 1030  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml
   
                 xmlC14NErrInternal("processing xml:base attribute - can't get attr value");                  xmlC14NErrInternal("processing xml:base attribute - can't get attr value");
                 return (NULL);                  return (NULL);
            }             }
   
            /* we need to add '/' if our current base uri ends with '..' or '.'             /* we need to add '/' if our current base uri ends with '..' or '.'
             to ensure that we are forced to go "up" all the time */              to ensure that we are forced to go "up" all the time */
             tmp_str_len = xmlStrlen(tmp_str);              tmp_str_len = xmlStrlen(tmp_str);
             if(tmp_str_len > 1 && tmp_str[tmp_str_len - 2] == '.') {              if(tmp_str_len > 1 && tmp_str[tmp_str_len - 2] == '.') {
Line 1047  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml Line 1049  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml
             }              }
   
             /* build uri */              /* build uri */
            tmp_str2 = xmlBuildURI(res, tmp_str);             tmp_str2 = xmlBuildURI(res, tmp_str);
             if(tmp_str2 == NULL) {              if(tmp_str2 == NULL) {
                 xmlFree(tmp_str);                  xmlFree(tmp_str);
                 xmlFree(res);                  xmlFree(res);
Line 1080  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml Line 1082  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml
         xmlC14NErrInternal("processing xml:base attribute - can't construct attribute");          xmlC14NErrInternal("processing xml:base attribute - can't construct attribute");
         return (NULL);          return (NULL);
     }      }
 
     /* done */      /* done */
     xmlFree(res);      xmlFree(res);
     return (attr);      return (attr);
Line 1088  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml Line 1090  xmlC14NFixupBaseAttr(xmlC14NCtxPtr ctx, xmlAttrPtr xml
   
 /**  /**
  * xmlC14NProcessAttrsAxis:   * xmlC14NProcessAttrsAxis:
 * @ctx:                the C14N context * @ctx:                the C14N context
  * @cur:                the current node   * @cur:                the current node
  * @parent_visible:     the visibility of parent node   * @parent_visible:     the visibility of parent node
  * @all_parents_visible: the visibility of all parent nodes   * @all_parents_visible: the visibility of all parent nodes
  *   *
  * Prints out canonical attribute axis of the current node to the   * Prints out canonical attribute axis of the current node to the
 * buffer from C14N context as follows  * buffer from C14N context as follows
  *   *
  * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)   * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  *   *
 * Attribute Axis  * Attribute Axis
 * In lexicographic order (ascending), process each node that  * In lexicographic order (ascending), process each node that
  * is in the element's attribute axis and in the node-set.   * is in the element's attribute axis and in the node-set.
 *  *
 * The processing of an element node E MUST be modified slightly  * The processing of an element node E MUST be modified slightly
 * when an XPath node-set is given as input and the element's  * when an XPath node-set is given as input and the element's
  * parent is omitted from the node-set.   * parent is omitted from the node-set.
  *   *
  *   *
  * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)   * Exclusive XML Canonicalization v 1.0 (http://www.w3.org/TR/xml-exc-c14n)
  *   *
 * Canonical XML applied to a document subset requires the search of the  * Canonical XML applied to a document subset requires the search of the
 * ancestor nodes of each orphan element node for attributes in the xml  * ancestor nodes of each orphan element node for attributes in the xml
 * namespace, such as xml:lang and xml:space. These are copied into the  * namespace, such as xml:lang and xml:space. These are copied into the
 * element node except if a declaration of the same attribute is already  * element node except if a declaration of the same attribute is already
 * in the attribute axis of the element (whether or not it is included in  * in the attribute axis of the element (whether or not it is included in
 * the document subset). This search and copying are omitted from the  * the document subset). This search and copying are omitted from the
  * Exclusive XML Canonicalization method.   * Exclusive XML Canonicalization method.
  *   *
  * Returns 0 on success or -1 on fail.   * Returns 0 on success or -1 on fail.
Line 1123  static int Line 1125  static int
 xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible)  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr cur, int parent_visible)
 {  {
     xmlAttrPtr attr;      xmlAttrPtr attr;
    xmlListPtr list;        xmlListPtr list;
     xmlAttrPtr attrs_to_delete = NULL;      xmlAttrPtr attrs_to_delete = NULL;
    
     /* special processing for 1.1 spec */      /* special processing for 1.1 spec */
     xmlAttrPtr xml_base_attr = NULL;      xmlAttrPtr xml_base_attr = NULL;
     xmlAttrPtr xml_lang_attr = NULL;      xmlAttrPtr xml_lang_attr = NULL;
Line 1147  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1149  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
   
     switch(ctx->mode) {      switch(ctx->mode) {
     case XML_C14N_1_0:      case XML_C14N_1_0:
        /* The processing of an element node E MUST be modified slightly when an XPath node-set is         /* The processing of an element node E MUST be modified slightly when an XPath node-set is
         * given as input and the element's parent is omitted from the node-set. The method for processing          * given as input and the element's parent is omitted from the node-set. The method for processing
         * the attribute axis of an element E in the node-set is enhanced. All element nodes along E's          * the attribute axis of an element E in the node-set is enhanced. All element nodes along E's
         * ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such          * ancestor axis are examined for nearest occurrences of attributes in the xml namespace, such
         * as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,          * as xml:lang and xml:space (whether or not they are in the node-set). From this list of attributes,
         * remove any that are in E's attribute axis (whether or not they are in the node-set). Then,          * remove any that are in E's attribute axis (whether or not they are in the node-set). Then,
         * lexicographically merge this attribute list with the nodes of E's attribute axis that are in          * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
         * the node-set. The result of visiting the attribute axis is computed by processing the attribute          * the node-set. The result of visiting the attribute axis is computed by processing the attribute
         * nodes in this merged attribute list.          * nodes in this merged attribute list.
          */           */
    
        /*         /*
         * Add all visible attributes from current node.          * Add all visible attributes from current node.
          */           */
         attr = cur->properties;          attr = cur->properties;
         while (attr != NULL) {          while (attr != NULL) {
Line 1170  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1172  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
             attr = attr->next;              attr = attr->next;
         }          }
   
        /*         /*
          * Handle xml attributes           * Handle xml attributes
          */           */
        if (parent_visible && (cur->parent != NULL) &&         if (parent_visible && (cur->parent != NULL) &&
            (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent)))             (!xmlC14NIsVisible(ctx, cur->parent, cur->parent->parent)))
         {          {
             xmlNodePtr tmp;              xmlNodePtr tmp;
   
             /*              /*
             * If XPath node-set is not specified then the parent is always              * If XPath node-set is not specified then the parent is always
              * visible!               * visible!
              */               */
             tmp = cur->parent;              tmp = cur->parent;
Line 1200  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1202  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
         /* done */          /* done */
         break;          break;
     case XML_C14N_EXCLUSIVE_1_0:      case XML_C14N_EXCLUSIVE_1_0:
        /* attributes in the XML namespace, such as xml:lang and xml:space         /* attributes in the XML namespace, such as xml:lang and xml:space
         * are not imported into orphan nodes of the document subset          * are not imported into orphan nodes of the document subset
          */           */
   
        /*         /*
         * Add all visible attributes from current node.          * Add all visible attributes from current node.
          */           */
         attr = cur->properties;          attr = cur->properties;
         while (attr != NULL) {          while (attr != NULL) {
Line 1219  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1221  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
         /* do nothing special for xml attributes */          /* do nothing special for xml attributes */
         break;          break;
     case XML_C14N_1_1:      case XML_C14N_1_1:
        /* The processing of an element node E MUST be modified slightly when an XPath node-set is         /* The processing of an element node E MUST be modified slightly when an XPath node-set is
         * given as input and some of the element's ancestors are omitted from the node-set.          * given as input and some of the element's ancestors are omitted from the node-set.
          *           *
         * Simple inheritable attributes are attributes that have a value that requires at most a simple          * Simple inheritable attributes are attributes that have a value that requires at most a simple
         * redeclaration. This redeclaration is done by supplying a new value in the child axis. The          * redeclaration. This redeclaration is done by supplying a new value in the child axis. The
         * redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done          * redeclaration of a simple inheritable attribute A contained in one of E's ancestors is done
         * by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes          * by supplying a value to an attribute Ae inside E with the same name. Simple inheritable attributes
          * are xml:lang and xml:space.           * are xml:lang and xml:space.
         *          *
         * The method for processing the attribute axis of an element E in the node-set is hence enhanced.          * The method for processing the attribute axis of an element E in the node-set is hence enhanced.
         * All element nodes along E's ancestor axis are examined for the nearest occurrences of simple          * All element nodes along E's ancestor axis are examined for the nearest occurrences of simple
         * inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they          * inheritable attributes in the xml namespace, such as xml:lang and xml:space (whether or not they
         * are in the node-set). From this list of attributes, any simple inheritable attributes that are          * are in the node-set). From this list of attributes, any simple inheritable attributes that are
         * already in E's attribute axis (whether or not they are in the node-set) are removed. Then,          * already in E's attribute axis (whether or not they are in the node-set) are removed. Then,
         * lexicographically merge this attribute list with the nodes of E's attribute axis that are in          * lexicographically merge this attribute list with the nodes of E's attribute axis that are in
         * the node-set. The result of visiting the attribute axis is computed by processing the attribute          * the node-set. The result of visiting the attribute axis is computed by processing the attribute
          * nodes in this merged attribute list.           * nodes in this merged attribute list.
         *          *
         * The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is          * The xml:id attribute is not a simple inheritable attribute and no processing of these attributes is
          * performed.           * performed.
         *          *
         * The xml:base attribute is not a simple inheritable attribute and requires special processing beyond          * The xml:base attribute is not a simple inheritable attribute and requires special processing beyond
          * a simple redeclaration.           * a simple redeclaration.
         *          *
         * Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed          * Attributes in the XML namespace other than xml:base, xml:id, xml:lang, and xml:space MUST be processed
          * as ordinary attributes.           * as ordinary attributes.
          */           */
   
        /*         /*
         * Add all visible attributes from current node.          * Add all visible attributes from current node.
          */           */
         attr = cur->properties;          attr = cur->properties;
         while (attr != NULL) {          while (attr != NULL) {
Line 1265  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1267  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
                 if((!matched) && (xml_lang_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "lang")) {                  if((!matched) && (xml_lang_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "lang")) {
                     xml_lang_attr = attr;                      xml_lang_attr = attr;
                     matched = 1;                      matched = 1;
                }                 }
                 if((!matched) && (xml_space_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "space")) {                  if((!matched) && (xml_space_attr == NULL) && xmlStrEqual(attr->name, BAD_CAST "space")) {
                     xml_space_attr = attr;                      xml_space_attr = attr;
                     matched = 1;                      matched = 1;
Line 1282  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1284  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
                     xmlListInsert(list, attr);                      xmlListInsert(list, attr);
                 }                  }
             }              }
         
             /* move to the next one */              /* move to the next one */
             attr = attr->next;              attr = attr->next;
         }          }
            
         /* special processing for XML attribute kiks in only when we have invisible parents */          /* special processing for XML attribute kiks in only when we have invisible parents */
         if ((parent_visible)) {          if ((parent_visible)) {
   
Line 1311  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1313  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
             }              }
             if(xml_base_attr != NULL) {              if(xml_base_attr != NULL) {
                 xml_base_attr = xmlC14NFixupBaseAttr(ctx, xml_base_attr);                  xml_base_attr = xmlC14NFixupBaseAttr(ctx, xml_base_attr);
                if(xml_base_attr != NULL) {                                    if(xml_base_attr != NULL) {
                     xmlListInsert(list, xml_base_attr);                      xmlListInsert(list, xml_base_attr);
   
                     /* note that we MUST delete returned attr node ourselves! */                      /* note that we MUST delete returned attr node ourselves! */
Line 1325  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1327  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
         break;          break;
     }      }
   
    /*     /*
     * print out all elements from list      * print out all elements from list
      */       */
     xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx);      xmlListWalk(list, (xmlListWalker) xmlC14NPrintAttrs, (const void *) ctx);
   
    /*     /*
      * Cleanup       * Cleanup
      */       */
     xmlFreePropList(attrs_to_delete);      xmlFreePropList(attrs_to_delete);
Line 1338  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr  Line 1340  xmlC14NProcessAttrsAxis(xmlC14NCtxPtr ctx, xmlNodePtr 
     return (0);      return (0);
 }  }
   
/** /**
  * xmlC14NCheckForRelativeNamespaces:   * xmlC14NCheckForRelativeNamespaces:
  * @ctx:                the C14N context   * @ctx:                the C14N context
  * @cur:                the current element node   * @cur:                the current element node
Line 1388  xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, x Line 1390  xmlC14NCheckForRelativeNamespaces(xmlC14NCtxPtr ctx, x
   
 /**  /**
  * xmlC14NProcessElementNode:   * xmlC14NProcessElementNode:
 * @ctx:                the pointer to C14N context object * @ctx:                the pointer to C14N context object
  * @cur:                the node to process   * @cur:                the node to process
  * @visible:    this node is visible   * @visible:    this node is visible
  * @all_parents_visible: whether all the parents of this node are visible   * @all_parents_visible: whether all the parents of this node are visible
 *               *
  * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)   * Canonical XML v 1.0 (http://www.w3.org/TR/xml-c14n)
  *   *
  * Element Nodes   * Element Nodes
 * If the element is not in the node-set, then the result is obtained  * If the element is not in the node-set, then the result is obtained
 * by processing the namespace axis, then the attribute axis, then  * by processing the namespace axis, then the attribute axis, then
 * processing the child nodes of the element that are in the node-set  * processing the child nodes of the element that are in the node-set
 * (in document order). If the element is in the node-set, then the result  * (in document order). If the element is in the node-set, then the result
 * is an open angle bracket (<), the element QName, the result of  * is an open angle bracket (<), the element QName, the result of
 * processing the namespace axis, the result of processing the attribute  * processing the namespace axis, the result of processing the attribute
 * axis, a close angle bracket (>), the result of processing the child  * axis, a close angle bracket (>), the result of processing the child
 * nodes of the element that are in the node-set (in document order), an  * nodes of the element that are in the node-set (in document order), an
 * open angle bracket, a forward slash (/), the element QName, and a close  * open angle bracket, a forward slash (/), the element QName, and a close
  * angle bracket.   * angle bracket.
  *   *
  * Returns non-negative value on success or negative value on fail   * Returns non-negative value on success or negative value on fail
Line 1421  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt Line 1423  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt
         return (-1);          return (-1);
     }      }
   
    /*     /*
      * Check relative relative namespaces:       * Check relative relative namespaces:
      * implementations of XML canonicalization MUST report an operation       * implementations of XML canonicalization MUST report an operation
      * failure on documents containing relative namespace URIs.       * failure on documents containing relative namespace URIs.
Line 1432  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt Line 1434  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt
     }      }
   
   
    /*     /*
      * Save ns_rendered stack position       * Save ns_rendered stack position
      */       */
     memset(&state, 0, sizeof(state));      memset(&state, 0, sizeof(state));
     xmlC14NVisibleNsStackSave(ctx->ns_rendered, &state);      xmlC14NVisibleNsStackSave(ctx->ns_rendered, &state);
   
    if (visible) {          if (visible) {
         if (ctx->parent_is_doc) {          if (ctx->parent_is_doc) {
             /* save this flag into the stack */              /* save this flag into the stack */
             parent_is_doc = ctx->parent_is_doc;              parent_is_doc = ctx->parent_is_doc;
Line 1468  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt Line 1470  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt
     if(visible) {      if(visible) {
         xmlC14NVisibleNsStackShift(ctx->ns_rendered);          xmlC14NVisibleNsStackShift(ctx->ns_rendered);
     }      }
    
     ret = xmlC14NProcessAttrsAxis(ctx, cur, visible);      ret = xmlC14NProcessAttrsAxis(ctx, cur, visible);
     if (ret < 0) {      if (ret < 0) {
         xmlC14NErrInternal("processing attributes axis");          xmlC14NErrInternal("processing attributes axis");
        return (-1);        return (-1);
     }      }
   
    if (visible) {     if (visible) {
         xmlOutputBufferWriteString(ctx->buf, ">");          xmlOutputBufferWriteString(ctx->buf, ">");
     }      }
     if (cur->children != NULL) {      if (cur->children != NULL) {
Line 1501  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt Line 1503  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt
         }          }
     }      }
   
    /*     /*
      * Restore ns_rendered stack position       * Restore ns_rendered stack position
      */       */
     xmlC14NVisibleNsStackRestore(ctx->ns_rendered, &state);      xmlC14NVisibleNsStackRestore(ctx->ns_rendered, &state);
Line 1510  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt Line 1512  xmlC14NProcessElementNode(xmlC14NCtxPtr ctx, xmlNodePt
   
 /**  /**
  * xmlC14NProcessNode:   * xmlC14NProcessNode:
 * @ctx:                the pointer to C14N context object * @ctx:                the pointer to C14N context object
  * @cur:                the node to process   * @cur:                the node to process
 *               *
  * Processes the given node   * Processes the given node
  *   *
  * Returns non-negative value on success or negative value on fail   * Returns non-negative value on success or negative value on fail
Line 1537  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) Line 1539  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
         case XML_TEXT_NODE:          case XML_TEXT_NODE:
             /*              /*
              * Text Nodes               * Text Nodes
             * the string value, except all ampersands are replaced              * the string value, except all ampersands are replaced
             * by &amp;, all open angle brackets (<) are replaced by &lt;, all closing              * by &amp;, all open angle brackets (<) are replaced by &lt;, all closing
             * angle brackets (>) are replaced by &gt;, and all #xD characters are              * angle brackets (>) are replaced by &gt;, and all #xD characters are
              * replaced by &#xD;.               * replaced by &#xD;.
              */               */
             /* cdata sections are processed as text nodes */              /* cdata sections are processed as text nodes */
Line 1559  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) Line 1561  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
             }              }
             break;              break;
         case XML_PI_NODE:          case XML_PI_NODE:
            /*             /*
             * Processing Instruction (PI) Nodes-              * Processing Instruction (PI) Nodes-
             * The opening PI symbol (<?), the PI target name of the node,              * The opening PI symbol (<?), the PI target name of the node,
             * a leading space and the string value if it is not empty, and              * a leading space and the string value if it is not empty, and
             * the closing PI symbol (?>). If the string value is empty,              * the closing PI symbol (?>). If the string value is empty,
             * then the leading space is not added. Also, a trailing #xA is              * then the leading space is not added. Also, a trailing #xA is
             * rendered after the closing PI symbol for PI children of the              * rendered after the closing PI symbol for PI children of the
             * root node with a lesser document order than the document              * root node with a lesser document order than the document
             * element, and a leading #xA is rendered before the opening PI              * element, and a leading #xA is rendered before the opening PI
             * symbol of PI children of the root node with a greater document              * symbol of PI children of the root node with a greater document
              * order than the document element.               * order than the document element.
              */               */
             if (visible) {              if (visible) {
Line 1607  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) Line 1609  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
         case XML_COMMENT_NODE:          case XML_COMMENT_NODE:
             /*              /*
              * Comment Nodes               * Comment Nodes
             * Nothing if generating canonical XML without  comments. For              * Nothing if generating canonical XML without  comments. For
             * canonical XML with comments, generate the opening comment              * canonical XML with comments, generate the opening comment
             * symbol (<!--), the string value of the node, and the              * symbol (<!--), the string value of the node, and the
             * closing comment symbol (-->). Also, a trailing #xA is rendered              * closing comment symbol (-->). Also, a trailing #xA is rendered
             * after the closing comment symbol for comment children of the              * after the closing comment symbol for comment children of the
             * root node with a lesser document order than the document              * root node with a lesser document order than the document
             * element, and a leading #xA is rendered before the opening              * element, and a leading #xA is rendered before the opening
             * comment symbol of comment children of the root node with a              * comment symbol of comment children of the root node with a
             * greater document order than the document element. (Comment              * greater document order than the document element. (Comment
             * children of the root node represent comments outside of the              * children of the root node represent comments outside of the
             * top-level document element and outside of the document type              * top-level document element and outside of the document type
              * declaration).               * declaration).
              */               */
             if (visible && ctx->with_comments) {              if (visible && ctx->with_comments) {
Line 1687  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) Line 1689  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
         case XML_XINCLUDE_START:          case XML_XINCLUDE_START:
         case XML_XINCLUDE_END:          case XML_XINCLUDE_END:
 #endif  #endif
            /*             /*
             * should be ignored according to "W3C Canonical XML"              * should be ignored according to "W3C Canonical XML"
              */               */
             break;              break;
         default:          default:
Line 1701  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur) Line 1703  xmlC14NProcessNode(xmlC14NCtxPtr ctx, xmlNodePtr cur)
   
 /**  /**
  * xmlC14NProcessNodeList:   * xmlC14NProcessNodeList:
 * @ctx:                the pointer to C14N context object * @ctx:                the pointer to C14N context object
  * @cur:                the node to start from   * @cur:                the node to start from
 *               *
  * Processes all nodes in the row starting from cur.   * Processes all nodes in the row starting from cur.
  *   *
  * Returns non-negative value on success or negative value on fail   * Returns non-negative value on success or negative value on fail
Line 1728  xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr c Line 1730  xmlC14NProcessNodeList(xmlC14NCtxPtr ctx, xmlNodePtr c
 /**  /**
  * xmlC14NFreeCtx:   * xmlC14NFreeCtx:
  * @ctx: the pointer to C14N context object   * @ctx: the pointer to C14N context object
 *               *
  * Cleanups the C14N context object.   * Cleanups the C14N context object.
  */   */
   
Line 1748  xmlC14NFreeCtx(xmlC14NCtxPtr ctx) Line 1750  xmlC14NFreeCtx(xmlC14NCtxPtr ctx)
   
 /**  /**
  * xmlC14NNewCtx:   * xmlC14NNewCtx:
 * @doc:                the XML document for canonization * @doc:                the XML document for canonization
 * @is_visible_callback:the function to use to determine is node visible  * @is_visible_callback:the function to use to determine is node visible
  *                      or not   *                      or not
 * @user_data:          the first parameter for @is_visible_callback function * @user_data:          the first parameter for @is_visible_callback function
  *                      (in most cases, it is nodes set)   *                      (in most cases, it is nodes set)
  * @mode:   the c14n mode (see @xmlC14NMode)   * @mode:   the c14n mode (see @xmlC14NMode)
 * @inclusive_ns_prefixe the list of inclusive namespace prefixes  * @inclusive_ns_prefixe the list of inclusive namespace prefixes
  *                      ended with a NULL or NULL if there is no   *                      ended with a NULL or NULL if there is no
 *                      inclusive namespaces (only for `  *                      inclusive namespaces (only for `
  *                      canonicalization)   *                      canonicalization)
 * @with_comments:      include comments in the result (!=0) or not (==0) * @with_comments:      include comments in the result (!=0) or not (==0)
 * @buf:                the output buffer to store canonical XML; this  * @buf:                the output buffer to store canonical XML; this
  *                      buffer MUST have encoder==NULL because C14N requires   *                      buffer MUST have encoder==NULL because C14N requires
  *                      UTF-8 output   *                      UTF-8 output
 *               *
  * Creates new C14N context object to store C14N parameters.   * Creates new C14N context object to store C14N parameters.
  *   *
  * Returns pointer to newly created object (success) or NULL (fail)   * Returns pointer to newly created object (success) or NULL (fail)
  */   */
 static xmlC14NCtxPtr  static xmlC14NCtxPtr
xmlC14NNewCtx(xmlDocPtr doc,  xmlC14NNewCtx(xmlDocPtr doc,
               xmlC14NIsVisibleCallback is_visible_callback, void* user_data,                xmlC14NIsVisibleCallback is_visible_callback, void* user_data,
               xmlC14NMode mode, xmlChar ** inclusive_ns_prefixes,                xmlC14NMode mode, xmlChar ** inclusive_ns_prefixes,
               int with_comments, xmlOutputBufferPtr buf)                int with_comments, xmlOutputBufferPtr buf)
Line 1840  xmlC14NNewCtx(xmlDocPtr doc,   Line 1842  xmlC14NNewCtx(xmlDocPtr doc,  
   
 /**  /**
  * xmlC14NExecute:   * xmlC14NExecute:
 * @doc:                the XML document for canonization * @doc:                the XML document for canonization
 * @is_visible_callback:the function to use to determine is node visible  * @is_visible_callback:the function to use to determine is node visible
  *                      or not   *                      or not
 * @user_data:          the first parameter for @is_visible_callback function * @user_data:          the first parameter for @is_visible_callback function
  *                      (in most cases, it is nodes set)   *                      (in most cases, it is nodes set)
  * @mode:       the c14n mode (see @xmlC14NMode)   * @mode:       the c14n mode (see @xmlC14NMode)
 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes  * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  *                      ended with a NULL or NULL if there is no   *                      ended with a NULL or NULL if there is no
 *                      inclusive namespaces (only for exclusive  *                      inclusive namespaces (only for exclusive
  *                      canonicalization, ignored otherwise)   *                      canonicalization, ignored otherwise)
 * @with_comments:      include comments in the result (!=0) or not (==0) * @with_comments:      include comments in the result (!=0) or not (==0)
 * @buf:                the output buffer to store canonical XML; this  * @buf:                the output buffer to store canonical XML; this
  *                      buffer MUST have encoder==NULL because C14N requires   *                      buffer MUST have encoder==NULL because C14N requires
  *                      UTF-8 output   *                      UTF-8 output
 *               *
  * Dumps the canonized image of given XML document into the provided buffer.   * Dumps the canonized image of given XML document into the provided buffer.
  * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or   * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)   * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  *   *
 * Returns non-negative value on success or a negative value on fail   * Returns non-negative value on success or a negative value on fail
  */   */
int             int
 xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback,  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback is_visible_callback,
          void* user_data, int mode, xmlChar **inclusive_ns_prefixes,           void* user_data, int mode, xmlChar **inclusive_ns_prefixes,
          int with_comments, xmlOutputBufferPtr buf) {           int with_comments, xmlOutputBufferPtr buf) {
Line 1875  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback Line 1877  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback
         return (-1);          return (-1);
     }      }
   
    /* for backward compatibility, we have to have "mode" as "int"     /* for backward compatibility, we have to have "mode" as "int"
        and here we check that user gives valid value */         and here we check that user gives valid value */
     switch(mode) {      switch(mode) {
     case XML_C14N_1_0:      case XML_C14N_1_0:
     case XML_C14N_EXCLUSIVE_1_0:      case XML_C14N_EXCLUSIVE_1_0:
    case XML_C14N_1_1:     case XML_C14N_1_1:
          c14n_mode = (xmlC14NMode)mode;           c14n_mode = (xmlC14NMode)mode;
          break;           break;
    default:           default:
         xmlC14NErrParam("invalid mode for executing c14n");          xmlC14NErrParam("invalid mode for executing c14n");
         return (-1);          return (-1);
     }      }
Line 1897  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback Line 1899  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback
         return (-1);          return (-1);
     }      }
   
    ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data,     ctx = xmlC14NNewCtx(doc, is_visible_callback, user_data,
                     c14n_mode, inclusive_ns_prefixes,                      c14n_mode, inclusive_ns_prefixes,
                     with_comments, buf);                      with_comments, buf);
     if (ctx == NULL) {      if (ctx == NULL) {
Line 1908  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback Line 1910  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback
   
   
   
    /*      /*
      * Root Node       * Root Node
     * The root node is the parent of the top-level document element. The      * The root node is the parent of the top-level document element. The
     * result of processing each of its child nodes that is in the node-set      * result of processing each of its child nodes that is in the node-set
     * in document order. The root node does not generate a byte order mark,      * in document order. The root node does not generate a byte order mark,
     * XML declaration, nor anything from within the document type      * XML declaration, nor anything from within the document type
      * declaration.       * declaration.
      */       */
     if (doc->children != NULL) {      if (doc->children != NULL) {
Line 1935  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback Line 1937  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback
         return (-1);          return (-1);
     }      }
   
    /*     /*
      * Cleanup       * Cleanup
      */       */
     xmlC14NFreeCtx(ctx);      xmlC14NFreeCtx(ctx);
Line 1944  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback Line 1946  xmlC14NExecute(xmlDocPtr doc, xmlC14NIsVisibleCallback
   
 /**  /**
  * xmlC14NDocSaveTo:   * xmlC14NDocSaveTo:
 * @doc:                the XML document for canonization * @doc:                the XML document for canonization
 * @nodes:              the nodes set to be included in the canonized image * @nodes:              the nodes set to be included in the canonized image
 *                      or NULL if all document nodes should be included *              or NULL if all document nodes should be included
  * @mode:               the c14n mode (see @xmlC14NMode)   * @mode:               the c14n mode (see @xmlC14NMode)
 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes  * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  *                      ended with a NULL or NULL if there is no   *                      ended with a NULL or NULL if there is no
 *                      inclusive namespaces (only for exclusive  *                      inclusive namespaces (only for exclusive
  *                      canonicalization, ignored otherwise)   *                      canonicalization, ignored otherwise)
 * @with_comments:      include comments in the result (!=0) or not (==0) * @with_comments:      include comments in the result (!=0) or not (==0)
 * @buf:                the output buffer to store canonical XML; this  * @buf:                the output buffer to store canonical XML; this
  *                      buffer MUST have encoder==NULL because C14N requires   *                      buffer MUST have encoder==NULL because C14N requires
  *                      UTF-8 output   *                      UTF-8 output
 *               *
  * Dumps the canonized image of given XML document into the provided buffer.   * Dumps the canonized image of given XML document into the provided buffer.
  * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or   * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)   * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  *   *
 * Returns non-negative value on success or a negative value on fail   * Returns non-negative value on success or a negative value on fail
  */   */
 int  int
 xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes,  xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes,
                  int mode, xmlChar ** inclusive_ns_prefixes,                   int mode, xmlChar ** inclusive_ns_prefixes,
                  int with_comments, xmlOutputBufferPtr buf) {                   int with_comments, xmlOutputBufferPtr buf) {
    return(xmlC14NExecute(doc,     return(xmlC14NExecute(doc,
                         (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset,                          (xmlC14NIsVisibleCallback)xmlC14NIsNodeInNodeset,
                         nodes,                          nodes,
                         mode,                          mode,
Line 1979  xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes, Line 1981  xmlC14NDocSaveTo(xmlDocPtr doc, xmlNodeSetPtr nodes,
   
 /**  /**
  * xmlC14NDocDumpMemory:   * xmlC14NDocDumpMemory:
 * @doc:                the XML document for canonization * @doc:                the XML document for canonization
 * @nodes:              the nodes set to be included in the canonized image * @nodes:              the nodes set to be included in the canonized image
 *                      or NULL if all document nodes should be included *              or NULL if all document nodes should be included
  * @mode:               the c14n mode (see @xmlC14NMode)   * @mode:               the c14n mode (see @xmlC14NMode)
 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes  * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  *                      ended with a NULL or NULL if there is no   *                      ended with a NULL or NULL if there is no
 *                      inclusive namespaces (only for exclusive  *                      inclusive namespaces (only for exclusive
  *                      canonicalization, ignored otherwise)   *                      canonicalization, ignored otherwise)
 * @with_comments:      include comments in the result (!=0) or not (==0) * @with_comments:      include comments in the result (!=0) or not (==0)
 * @doc_txt_ptr:        the memory pointer for allocated canonical XML text; * @doc_txt_ptr:        the memory pointer for allocated canonical XML text;
  *                      the caller of this functions is responsible for calling   *                      the caller of this functions is responsible for calling
 *                      xmlFree() to free allocated memory  *                      xmlFree() to free allocated memory
 *               *
  * Dumps the canonized image of given XML document into memory.   * Dumps the canonized image of given XML document into memory.
  * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or   * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)   * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  *   *
 * Returns the number of bytes written on success or a negative value on fail   * Returns the number of bytes written on success or a negative value on fail
  */   */
 int  int
 xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes,  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr nodes,
Line 2014  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr node Line 2016  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr node
     *doc_txt_ptr = NULL;      *doc_txt_ptr = NULL;
   
     /*      /*
     * create memory buffer with UTF8 (default) encoding      * create memory buffer with UTF8 (default) encoding
      */       */
     buf = xmlAllocOutputBuffer(NULL);      buf = xmlAllocOutputBuffer(NULL);
     if (buf == NULL) {      if (buf == NULL) {
Line 2033  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr node Line 2035  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr node
         return (-1);          return (-1);
     }      }
   
    ret = buf->buffer->use;    ret = xmlBufUse(buf->buffer);
     if (ret > 0) {      if (ret > 0) {
        *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret);        *doc_txt_ptr = xmlStrndup(xmlBufContent(buf->buffer), ret);
     }      }
     (void) xmlOutputBufferClose(buf);      (void) xmlOutputBufferClose(buf);
   
Line 2048  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr node Line 2050  xmlC14NDocDumpMemory(xmlDocPtr doc, xmlNodeSetPtr node
   
 /**  /**
  * xmlC14NDocSave:   * xmlC14NDocSave:
 * @doc:                the XML document for canonization * @doc:                the XML document for canonization
 * @nodes:              the nodes set to be included in the canonized image * @nodes:              the nodes set to be included in the canonized image
 *                      or NULL if all document nodes should be included *              or NULL if all document nodes should be included
  * @mode:               the c14n mode (see @xmlC14NMode)   * @mode:               the c14n mode (see @xmlC14NMode)
 * @inclusive_ns_prefixes: the list of inclusive namespace prefixes  * @inclusive_ns_prefixes: the list of inclusive namespace prefixes
  *                      ended with a NULL or NULL if there is no   *                      ended with a NULL or NULL if there is no
 *                      inclusive namespaces (only for exclusive  *                      inclusive namespaces (only for exclusive
  *                      canonicalization, ignored otherwise)   *                      canonicalization, ignored otherwise)
 * @with_comments:      include comments in the result (!=0) or not (==0) * @with_comments:      include comments in the result (!=0) or not (==0)
 * @filename:           the filename to store canonical XML image * @filename:           the filename to store canonical XML image
 * @compression:        the compression level (zlib requred):  * @compression:        the compression level (zlib requred):
  *                              -1 - libxml default,   *                              -1 - libxml default,
 *                               0 - uncompressed,  *                               0 - uncompressed,
  *                              >0 - compression level   *                              >0 - compression level
 *               *
  * Dumps the canonized image of given XML document into the file.   * Dumps the canonized image of given XML document into the file.
  * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or   * For details see "Canonical XML" (http://www.w3.org/TR/xml-c14n) or
  * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)   * "Exclusive XML Canonicalization" (http://www.w3.org/TR/xml-exc-c14n)
  *   *
 * Returns the number of bytes written success or a negative value on fail   * Returns the number of bytes written success or a negative value on fail
  */   */
 int  int
 xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
Line 2086  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, Line 2088  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
         compression = xmlGetCompressMode();          compression = xmlGetCompressMode();
 #endif  #endif
   
    /*     /*
      * save the content to a temp buffer, use default UTF8 encoding.       * save the content to a temp buffer, use default UTF8 encoding.
      */       */
     buf = xmlOutputBufferCreateFilename(filename, NULL, compression);      buf = xmlOutputBufferCreateFilename(filename, NULL, compression);
Line 2106  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, Line 2108  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
         return (-1);          return (-1);
     }      }
   
    /*     /*
     * get the numbers of bytes written      * get the numbers of bytes written
      */       */
     ret = xmlOutputBufferClose(buf);      ret = xmlOutputBufferClose(buf);
     return (ret);      return (ret);
Line 2121  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes, Line 2123  xmlC14NDocSave(xmlDocPtr doc, xmlNodeSetPtr nodes,
 #define growBufferReentrant() {                                         \  #define growBufferReentrant() {                                         \
     buffer_size *= 2;                                                   \      buffer_size *= 2;                                                   \
     buffer = (xmlChar *)                                                \      buffer = (xmlChar *)                                                \
                xmlRealloc(buffer, buffer_size * sizeof(xmlChar));      \                xmlRealloc(buffer, buffer_size * sizeof(xmlChar));      \
     if (buffer == NULL) {                                               \      if (buffer == NULL) {                                               \
         xmlC14NErrMemory("growing buffer");                             \          xmlC14NErrMemory("growing buffer");                             \
         return(NULL);                                                   \          return(NULL);                                                   \
     }                                                                   \      }                                                                   \
 }  }
   
/** /**
  * xmlC11NNormalizeString:   * xmlC11NNormalizeString:
  * @input:              the input string   * @input:              the input string
  * @mode:               the normalization mode (attribute, comment, PI or text)   * @mode:               the normalization mode (attribute, comment, PI or text)

Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>