xmlNode* child_node_list::create_element_(const std::string& qname) { // Split QName into prefix and local name. std::pair<std::string, std::string> name_pair = detail::split_qname(qname); const std::string& prefix = name_pair.first; const std::string& name = name_pair.second; // Find xmlns by prefix (if this child node list belongs to an element). xmlNs* ns = 0; if (raw_->type == XML_ELEMENT_NODE) { ns = xmlSearchNs( raw_->doc, raw_, prefix.empty() ? 0 : detail::to_xml_chars(prefix.c_str()) ); } if (!prefix.empty() && ns == 0) { std::string what = "fail to create element " + qname + ": xmlns for prefix " + prefix + " not found"; throw bad_dom_operation(what); } // Create element under the xmlns. xmlNode* px = xmlNewDocNode( raw_->doc, ns, detail::to_xml_chars(name.c_str()), 0 ); if (px == 0) { std::string what = "fail to create libxml2 element node for " + name; throw internal_dom_error(what); } // Return the new element. return px; }
xmlNsPtr node_find_ns(xmlNodePtr node) { if (node->ns) { return node->ns; } else { return xmlSearchNs(node->doc, node, NULL); } }
virtual bool PatchCamFlags(xmlDocPtr doc, const char *uuid, const char *slot, const char *flags) { bool uuid_match=false; bool flags_set =false; xmlNode *node = xmlDocGetRootElement(doc); node = node ? node->children : NULL; while(node && xmlStrcmp(node->name, (const xmlChar *)"Description")) node=node->next; node = node ? node->children : NULL; while(node) { if(node && !xmlStrcmp(node->name, (const xmlChar *)"component")) { xmlNode *item = node->children; while(item && xmlStrcmp(item->name, (const xmlChar *)"Description")) { item = item->next; } // while xmlChar *about = item ? xmlGetProp(item, (const xmlChar *)"about") : NULL; if(about) { if (!xmlStrcmp(about, (const xmlChar *)"Platform")) { xmlNode *sub = item->children; while(sub) { if(!xmlStrcmp(sub->name, (const xmlChar *)"UUID")) { xmlChar *value=xmlNodeListGetString(doc, sub->children, 1); if(value) { uuid_match=!xmlStrcmp(value, (const xmlChar *)uuid); xmlFree(value); } // if } // if sub = sub->next; } // while } else if(!xmlStrcmp(about, (const xmlChar *)"CAM")) { xmlNode *sub = item->children; while(sub) { if(!xmlStrcmp(sub->name, (const xmlChar *)"Slot")) { xmlChar *value=xmlNodeListGetString(doc, sub->children, 1); if(value) { if (!xmlStrcmp(value, (const xmlChar *)slot)) { xmlNode *tst = item->children; while(tst) { if(!xmlStrcmp(tst->name, (const xmlChar *)"Flags")) { xmlReplaceNode(tst, xmlNewChild(item, xmlSearchNs(doc, tst, (const xmlChar *)"prf"), (const xmlChar *)"Flags", (const xmlChar *)flags)); xmlFreeNode(tst); flags_set=true; tst = NULL; continue; } // if tst = tst->next; } // while } // if xmlFree(value); } // if } // if sub = sub->next; } // while } // if xmlFree(about); } // if } // if node = node->next; } // while return uuid_match && flags_set; }; // PatchCamFlags
XMLNode XMLNode::NewChild(const char *name, int n, bool global_order) { if (node_ == NULL) return XMLNode(); if (node_->type != XML_ELEMENT_NODE) return XMLNode(); const char *name_ = strchr(name, ':'); xmlNsPtr ns = NULL; if (name_ != NULL) { std::string ns_(name, name_ - name); ns = xmlSearchNs(node_->doc, node_, (const xmlChar*)(ns_.c_str())); ++name_; } else name_ = name; xmlNodePtr new_node = xmlNewNode(ns, (const xmlChar*)name_); if (new_node == NULL) return XMLNode(); if (n < 0) return XMLNode(xmlAddChild(node_, new_node)); XMLNode old_node = global_order ? Child(n) : operator[](name)[n]; if (!old_node) // TODO: find last old_node return XMLNode(xmlAddChild(node_, new_node)); if (old_node) return XMLNode(xmlAddPrevSibling(old_node.node_, new_node)); return XMLNode(xmlAddChild(node_, new_node)); }
/** * xsltGetSpecialNamespace: * @ctxt: a transformation context * @cur: the input node * @URI: the namespace URI * @prefix: the suggested prefix * @out: the output node (or its parent) * * Find the right namespace value for this URI, if needed create * and add a new namespace decalaration on the node * * Returns the namespace node to use or NULL */ xmlNsPtr xsltGetSpecialNamespace(xsltTransformContextPtr ctxt, xmlNodePtr cur, const xmlChar *URI, const xmlChar *prefix, xmlNodePtr out) { xmlNsPtr ret; static int prefixno = 1; char nprefix[10]; if ((ctxt == NULL) || (cur == NULL) || (out == NULL) || (URI == NULL)) return(NULL); if ((out->parent != NULL) && (out->parent->type == XML_ELEMENT_NODE) && (out->parent->ns != NULL) && (xmlStrEqual(out->parent->ns->href, URI))) ret = out->parent->ns; else ret = xmlSearchNsByHref(out->doc, out, URI); if (ret == NULL) { if (prefix == NULL) { do { sprintf(nprefix, "ns%d", prefixno++); ret = xmlSearchNs(out->doc, out, (xmlChar *)nprefix); } while (ret != NULL); prefix = (const xmlChar *) &nprefix[0]; } if (out->type == XML_ELEMENT_NODE) ret = xmlNewNs(out, URI, prefix); } return(ret); }
/* * Find or construct a (possibly temporary) namespace node * for the "func" exslt library and put the given node into * that namespace. We also have to add this as an "extension" * namespace. */ static xmlNsPtr slaxSetNs (slax_data_t *sdp, xmlNodePtr nodep, const char *prefix, const xmlChar *uri, int local) { xmlNsPtr nsp; xmlNodePtr root = xmlDocGetRootElement(sdp->sd_docp); nsp = xmlSearchNs(sdp->sd_docp, root, (const xmlChar *) prefix); if (nsp == NULL) { nsp = xmlNewNs(root, uri, (const xmlChar *) prefix); if (nsp == NULL) { xmlParserError(sdp->sd_ctxt, "%s:%d: out of memory", sdp->sd_filename, sdp->sd_line); return NULL; } /* * Since we added this namespace, we need to add it to the * list of extension prefixes. */ slaxNodeAttribExtend(sdp, root, ATT_EXTENSION_ELEMENT_PREFIXES, prefix, NULL); } if (nodep) { /* Add a distinct namespace to the current node */ nsp = xmlNewNs(nodep, uri, (const xmlChar *) prefix); if (local) nodep->ns = nsp; } return nsp; }
/** * xlinkIsLink: * @doc: the document containing the node * @node: the node pointer itself * * Check whether the given node carries the attributes needed * to be a link element (or is one of the linking elements issued * from the (X)HTML DtDs). * This routine don't try to do full checking of the link validity * but tries to detect and return the appropriate link type. * * Returns the xlinkType of the node (XLINK_TYPE_NONE if there is no * link detected. */ xlinkType xlinkIsLink (xmlDocPtr doc, xmlNodePtr node) { xmlChar *type = NULL, *role = NULL; xlinkType ret = XLINK_TYPE_NONE; if (node == NULL) return(XLINK_TYPE_NONE); if (doc == NULL) doc = node->doc; if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { /* * This is an HTML document. */ } else if ((node->ns != NULL) && (xmlStrEqual(node->ns->href, XHTML_NAMESPACE))) { /* * !!!! We really need an IS_XHTML_ELEMENT function from HTMLtree.h @@@ */ /* * This is an XHTML element within an XML document * Check whether it's one of the element able to carry links * and in that case if it holds the attributes. */ } /* * We don't prevent a-priori having XML Linking constructs on * XHTML elements */ type = xmlGetNsProp(node, BAD_CAST"type", XLINK_NAMESPACE); if (type != NULL) { if (xmlStrEqual(type, BAD_CAST "simple")) { ret = XLINK_TYPE_SIMPLE; } if (xmlStrEqual(type, BAD_CAST "extended")) { role = xmlGetNsProp(node, BAD_CAST "role", XLINK_NAMESPACE); if (role != NULL) { xmlNsPtr xlink; xlink = xmlSearchNs(doc, node, XLINK_NAMESPACE); if (xlink == NULL) { /* Humm, fallback method */ if (xmlStrEqual(role, BAD_CAST"xlink:external-linkset")) ret = XLINK_TYPE_EXTENDED_SET; } else { xmlChar buf[200]; snprintf((char *) buf, sizeof(buf), "%s:external-linkset", (char *) xlink->prefix); buf[sizeof(buf) - 1] = 0; if (xmlStrEqual(role, buf)) ret = XLINK_TYPE_EXTENDED_SET; } } ret = XLINK_TYPE_EXTENDED; } } if (type != NULL) xmlFree(type); if (role != NULL) xmlFree(role); return(ret); }
/** * xsltGetNamespace: * @ctxt: a transformation context * @cur: the input node * @ns: the namespace * @out: the output node (or its parent) * * Find a matching (prefix and ns-name) ns-declaration * for the requested @ns->prefix and @ns->href in the result tree. * If none is found then a new ns-declaration will be * added to @resultElem. If, in this case, the given prefix is * already in use, then a ns-declaration with a modified ns-prefix * be we created. * * Called by: * - xsltCopyPropList() (*not* anymore) * - xsltShallowCopyElement() * - xsltCopyTreeInternal() (*not* anymore) * - xsltApplySequenceConstructor() (*not* in the refactored code), * - xsltElement() (*not* anymore) * * Returns a namespace declaration or NULL in case of * namespace fixup failures or API or internal errors. */ xmlNsPtr xsltGetNamespace(xsltTransformContextPtr ctxt, xmlNodePtr cur, xmlNsPtr ns, xmlNodePtr out) { if (ns == NULL) return(NULL); #ifdef XSLT_REFACTORED /* * Namespace exclusion and ns-aliasing is performed at * compilation-time in the refactored code. * Additionally, aliasing is not intended for non Literal * Result Elements. */ return(xsltGetSpecialNamespace(ctxt, cur, ns->href, ns->prefix, out)); #else { xsltStylesheetPtr style; const xmlChar *URI = NULL; /* the replacement URI */ if ((ctxt == NULL) || (cur == NULL) || (out == NULL)) return(NULL); style = ctxt->style; while (style != NULL) { if (style->nsAliases != NULL) URI = (const xmlChar *) xmlHashLookup(style->nsAliases, ns->href); if (URI != NULL) break; style = xsltNextImport(style); } if (URI == UNDEFINED_DEFAULT_NS) { return(xsltGetSpecialNamespace(ctxt, cur, NULL, NULL, out)); #if 0 /* * TODO: Removed, since wrong. If there was no default * namespace in the stylesheet then this must resolve to * the NULL namespace. */ xmlNsPtr dflt; dflt = xmlSearchNs(cur->doc, cur, NULL); if (dflt != NULL) URI = dflt->href; else return NULL; #endif } else if (URI == NULL) URI = ns->href; return(xsltGetSpecialNamespace(ctxt, cur, URI, ns->prefix, out)); } #endif }
/** * xsltElementAvailableFunction: * @ctxt: the XPath Parser context * @nargs: the number of arguments * * Implement the element-available() XSLT function * boolean element-available(string) */ void xsltElementAvailableFunction(xmlXPathParserContextPtr ctxt, int nargs){ xmlXPathObjectPtr obj; xmlChar *prefix, *name; const xmlChar *nsURI = NULL; xsltTransformContextPtr tctxt; if (nargs != 1) { xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL, "element-available() : expects one string arg\n"); ctxt->error = XPATH_INVALID_ARITY; return; } xmlXPathStringFunction(ctxt, 1); if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_STRING)) { xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL, "element-available() : invalid arg expecting a string\n"); ctxt->error = XPATH_INVALID_TYPE; return; } obj = valuePop(ctxt); tctxt = xsltXPathGetTransformContext(ctxt); if (tctxt == NULL) { xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL, "element-available() : internal error tctxt == NULL\n"); xmlXPathFreeObject(obj); valuePush(ctxt, xmlXPathNewBoolean(0)); return; } name = xmlSplitQName2(obj->stringval, &prefix); if (name == NULL) { xmlNsPtr ns; name = xmlStrdup(obj->stringval); ns = xmlSearchNs(tctxt->inst->doc, tctxt->inst, NULL); if (ns != NULL) nsURI = xmlStrdup(ns->href); } else { nsURI = xmlXPathNsLookup(ctxt->context, prefix); if (nsURI == NULL) { xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL, "element-available() : prefix %s is not bound\n", prefix); } } if (xsltExtElementLookup(tctxt, name, nsURI) != NULL) { valuePush(ctxt, xmlXPathNewBoolean(1)); } else { valuePush(ctxt, xmlXPathNewBoolean(0)); } xmlXPathFreeObject(obj); if (name != NULL) xmlFree(name); if (prefix != NULL) xmlFree(prefix); }
xmlNsPtr attr_find_ns(xmlAttrPtr node) { if (node->ns) { return node->ns; } else if (node->parent->ns) { return node->parent->ns; } else { return xmlSearchNs(node->doc, node->parent, NULL); } }
char * epp_getSubtree(void *pool, epp_command_data *cdata, const char *xpath_expr, int position) { char *subtree; xmlBufferPtr buf; xmlDocPtr doc; xmlNodePtr node; xmlXPathObjectPtr xpath_obj; xmlXPathContextPtr xpath_ctx; doc = (xmlDocPtr) cdata->parsed_doc; xpath_ctx = (xmlXPathContextPtr) cdata->xpath_ctx; xpath_obj = xmlXPathEvalExpression(BAD_CAST xpath_expr, xpath_ctx); if (xpath_obj == NULL) return NULL; /* correct position for non-list elements */ if (position == 0) position++; if (xmlXPathNodeSetGetLength(xpath_obj->nodesetval) < position) { xmlXPathFreeObject(xpath_obj); /* return empty string if the node is not there */ return epp_strdup(pool, ""); } /* * Get content of problematic tag. It's not so easy task. We have * to declare namespaces defined higher in the tree which are relevant * to the part of document being dumped. Fortunatelly there is a * function from libxml library doing exactly that (xmlreconsiliatens). */ buf = xmlBufferCreate(); if (buf == NULL) return NULL; node = xmlXPathNodeSetItem(xpath_obj->nodesetval, position - 1); if (node->ns != NULL) { xmlNsPtr nsdef; nsdef = xmlSearchNs(doc, node, node->ns->prefix); if (nsdef != NULL) xmlNewNs(node, nsdef->href, nsdef->prefix); } if (xmlNodeDump(buf, doc, node, 0, 0) < 0) { xmlXPathFreeObject(xpath_obj); xmlBufferFree(buf); return NULL; } subtree = epp_strdup(pool, (char *) buf->content); xmlXPathFreeObject(xpath_obj); xmlBufferFree(buf); return subtree; }
//#################################################################### xmlAttrPtr find_prop (xmlNodePtr xmlnode, const char *name, const ns *nspace) { // The similar check is in libxml2 static function // xmlGetPropNodeInternal(...) if ( (xmlnode == NULL) || (xmlnode->type != XML_ELEMENT_NODE) || (name == NULL) ) return NULL; const ns * ns_to_match = nspace; const char * name_to_match = name; // Check first if the name is qualified const char * column = strchr(name, ':'); if (column) { if (nspace) return NULL; // Both namespace and the name is qualified if (column == name) return NULL; // The name starts with : if (*(column + 1) == '\0') return NULL; // No attribute name is given std::string prefix(name, column - name); xmlNsPtr resolved_ns = xmlSearchNs(xmlnode->doc, xmlnode, reinterpret_cast<const xmlChar*>(prefix.c_str())); if (!resolved_ns) return NULL; // No such namespace found name_to_match = column + 1; ns_to_match = new ns(reinterpret_cast<const char *>(resolved_ns->prefix), reinterpret_cast<const char *>(resolved_ns->href)); } xmlAttrPtr prop = xmlnode->properties; for (; prop != NULL; prop = prop->next) { if (xmlStrEqual(prop->name, reinterpret_cast<const xmlChar*>(name_to_match))) { if (ns_util::attr_ns_match(prop, ns_to_match)) { if (ns_to_match != nspace) delete ns_to_match; return prop; } } } if (ns_to_match != nspace) delete ns_to_match; return NULL; }
std::string cXmlDoc::searchNs(const std::string& prefix, xmlNodePtr node) { if (!m_doc) throw eXmlInvalid("Tried to search namespace for bad document"); xmlNsPtr ns = xmlSearchNs(m_doc.get(), node?node:xmlDocGetRootElement(m_doc.get()), (prefix=="")?NULL:reinterpret_cast<const xmlChar*>(prefix.c_str())); if (ns) { return ns->href?string(reinterpret_cast<const char*>(ns->href)):""; } else { return ""; } }
const XMLNs *XMLElement::getNamespaceByPrefix(const char *prefix) const { xmlNs *ns = xmlSearchNs(doc.getRealDocument(), node, (const xmlChar *)prefix); XMLObject *obj = scope->getXMLObjectFromLibXMLPtr(ns); if (obj) { return static_cast < XMLNs * >(obj); } return new XMLNs(*this, ns); }
xmlNodePtr Node::createChild(const string &name, const string &prefix) const { xmlNs * ns = nullptr; if ( Type() != NodeType::Element ) throw InternalError("Cannot add children to non-element node of type '" + TypeString(Type()) + "'"); if ( prefix.empty() ) { ns = xmlSearchNs(_xml->doc, _xml, nullptr); } else { // use the existing namespace if one exists ns = xmlSearchNs(_xml->doc, _xml, prefix.utf8()); if ( ns == nullptr ) throw InternalError(std::string("The namespace prefix '") + prefix.c_str() + "' is unknown"); } return xmlNewNode(ns, name.utf8()); }
void XMLElement::setNodeNameSpace(const XMLNs & ns) const { xmlNs *n = ns.getRealNs(); if (n) { if (!n->prefix || !xmlSearchNs(doc.getRealDocument(), node, n->prefix)) { n = xmlNewNs(node, (const xmlChar *)ns.getHref(), (const xmlChar *)ns.getPrefix()); } xmlSetNs(node, n); } }
int f_manage_input_xml(const char *p_name,int s_type,audiovideo_t *p_audiovideo) { static xmlDocPtr p_doc; xmlNodePtr p_node; xmlNsPtr ns; if (s_type) //read the file from p_name { p_doc = xmlParseFile(p_name); p_node = xmlDocGetRootElement(p_doc); if (p_node == NULL) { xmlFreeDoc(p_doc); tc_log_error(__FILE__,"Invalid file format"); return(-1); } ns = xmlSearchNsByHref(p_doc, p_node, (const xmlChar *) "http://www.w3.org/2001/SMIL20/Language"); if (ns == NULL) { xmlFreeDoc(p_doc); tc_log_error(__FILE__,"Invalid Namespace"); return(-1); } ns = xmlSearchNs(p_doc, p_node, (const xmlChar *) "smil2"); if (ns == NULL) { xmlFreeDoc(p_doc); tc_log_error(__FILE__,"Invalid Namespace"); return(-1); } if (xmlStrcmp(p_node->name, (const xmlChar *) "smil")) { xmlFreeDoc(p_doc); tc_log_error(__FILE__,"Invalid Namespace"); return(-1); } f_delete_unused_node(p_node); memset(p_audiovideo,'\0',sizeof(audiovideo_t)); if(f_parse_tree(p_node,p_audiovideo)) return(1); if (f_complete_tree(p_audiovideo)) return(1); } else { f_free_tree(p_audiovideo); xmlFreeDoc(p_doc); } return(0); }
static xmlNsPtr _dom_new_reconNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) /* {{{ */ { xmlNsPtr def; xmlChar prefix[50]; int counter = 1; if ((tree == NULL) || (ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { return NULL; } /* Code taken from libxml2 (2.6.20) xmlNewReconciliedNs * * Find a close prefix which is not already in use. * Let's strip namespace prefixes longer than 20 chars ! */ if (ns->prefix == NULL) snprintf((char *) prefix, sizeof(prefix), "default"); else snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); def = xmlSearchNs(doc, tree, prefix); while (def != NULL) { if (counter > 1000) return(NULL); if (ns->prefix == NULL) snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); else snprintf((char *) prefix, sizeof(prefix), "%.20s%d", (char *)ns->prefix, counter++); def = xmlSearchNs(doc, tree, prefix); } /* * OK, now we are ready to create a new one. */ def = xmlNewNs(tree, ns->href, prefix); return(def); }
int exclude_namespace(elcgen *gen, expression *expr, const char *uri) { int found = 0; expression *xp; if (!strcmp(uri,XSLT_NS)) return 1; for (xp = expr; xp && !found; xp = xp->parent) { char *str; if (xp->xmlnode->ns && !strcmp((char*)xp->xmlnode->ns->href,XSLT_NS)) str = xmlGetNsProp(xp->xmlnode,"exclude-result-prefixes",NULL); else str = xmlGetNsProp(xp->xmlnode,"exclude-result-prefixes",XSLT_NS); if (str) { int end = 0; char *start = str; char *c; for (c = str; !found && !end; c++) { end = ('\0' == *c); if (end || isspace(*c)) { if (c > start) { xmlNsPtr ns; *c = '\0'; if (!strcmp(start,"#all")) { found = (NULL != xmlSearchNsByHref(gen->parse_doc,xp->xmlnode,(xmlChar*)uri)); } else if (!strcmp(start,"#default")) { found = ((NULL != xp->xmlnode->ns) && !strcmp((char*)xp->xmlnode->ns->href,uri)); } else { ns = xmlSearchNs(gen->parse_doc,xp->xmlnode,(xmlChar*)start); found = ((NULL != ns) && !strcmp((char*)ns->href,uri)); } } start = c+1; } } free(str); } } return found; }
static xmlNsPtr fetch_ns (ESoapMessage *msg, const gchar *prefix, const gchar *ns_uri) { ESoapMessagePrivate *priv = E_SOAP_MESSAGE_GET_PRIVATE (msg); xmlNsPtr ns = NULL; if (prefix && ns_uri) ns = xmlNewNs (priv->last_node, (const xmlChar *) ns_uri, (const xmlChar *) prefix); else if (prefix && !ns_uri) { ns = xmlSearchNs (priv->doc, priv->last_node, (const xmlChar *) prefix); if (!ns) ns = xmlNewNs (priv->last_node, (const xmlChar *)"", (const xmlChar *)prefix); } return ns; }
void XMLAttr::setAttributeValue(xmlNode * node, const char *prefix, const char *name, const char *value) { if (node && node->type == XML_ELEMENT_NODE) { xmlAttr *attrs = 0; for (xmlAttr * cur = node->properties; cur; cur = cur->next) { if (cur->ns && !strcmp(name, (const char *)cur->name) && (!strcmp(prefix, (const char *)cur->ns->prefix) || !strcmp(prefix, (const char *)cur->ns->href))) { attrs = cur; break; } } if (attrs) { xmlSetNsProp(node, attrs->ns, (const xmlChar *)name, (const xmlChar *)value); } else { xmlNs *ns = 0; if (!strncmp(prefix, "http://", strlen("http://"))) { ns = xmlSearchNsByHref(node->doc, node, (const xmlChar *)prefix); } else { ns = xmlSearchNs(node->doc, node, (const xmlChar *)prefix); } if (ns) { xmlSetNsProp(node, ns, (const xmlChar *)name, (const xmlChar *)value); } else { xmlSetProp(node, (const xmlChar *)name, (const xmlChar *)value); } } } }
xlinkType xlinkIsLink (xmlDocPtr doc, xmlNodePtr node) { xmlChar *type = NULL, *role = NULL; xlinkType ret = XLINK_TYPE_NONE; if (node == NULL) return(XLINK_TYPE_NONE); if (doc == NULL) doc = node->doc; if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { } else if ((node->ns != NULL) && (xmlStrEqual(node->ns->href, XHTML_NAMESPACE))) { } type = xmlGetNsProp(node, BAD_CAST"type", XLINK_NAMESPACE); if (type != NULL) { if (xmlStrEqual(type, BAD_CAST "simple")) { ret = XLINK_TYPE_SIMPLE; } if (xmlStrEqual(type, BAD_CAST "extended")) { role = xmlGetNsProp(node, BAD_CAST "role", XLINK_NAMESPACE); if (role != NULL) { xmlNsPtr xlink; xlink = xmlSearchNs(doc, node, XLINK_NAMESPACE); if (xlink == NULL) { if (xmlStrEqual(role, BAD_CAST"xlink:external-linkset")) ret = XLINK_TYPE_EXTENDED_SET; } else { xmlChar buf[200]; snprintf((char *) buf, sizeof(buf), "%s:external-linkset", (char *) xlink->prefix); buf[sizeof(buf) - 1] = 0; if (xmlStrEqual(role, buf)) ret = XLINK_TYPE_EXTENDED_SET; } } ret = XLINK_TYPE_EXTENDED; } } if (type != NULL) xmlFree(type); if (role != NULL) xmlFree(role); return(ret); }
int EB_Msg__prepareSignature(xmlDocPtr doc) { xmlNodePtr node; xmlNsPtr ns; xmlNodePtr n; xmlNodePtr nn; xmlNodePtr nnn; xmlNodePtr nnnn; node=xmlNewChild(xmlDocGetRootElement(doc), NULL, BAD_CAST "AuthSignature", NULL); ns=xmlSearchNs(doc, node, BAD_CAST "ds"); assert(ns); n=xmlNewChild(node, ns, BAD_CAST "SignedInfo", NULL); nn=xmlNewChild(n, ns, BAD_CAST "CanonicalizationMethod", NULL); xmlNewProp(nn, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"); nn=xmlNewChild(n, ns, BAD_CAST "SignatureMethod", NULL); xmlNewProp(nn, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/2000/09/xmldsig#rsa-sha1"); nn=xmlNewChild(n, ns, BAD_CAST "Reference", NULL); xmlNewProp(nn, BAD_CAST "URI", BAD_CAST "#xpointer(//*[@authenticate='true'])"); nnn=xmlNewChild(nn, ns, BAD_CAST "Transforms", NULL); nnnn=xmlNewChild(nnn, ns, BAD_CAST "Transform", NULL); xmlNewProp(nnnn, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"); nn=xmlNewChild(n, ns, BAD_CAST "DigestMethod", NULL); xmlNewProp(nn, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/2000/09/xmldsig#sha1"); return 0; }
static xmlNodePtr dom_get_dom1_attribute(xmlNodePtr elem, xmlChar *name) /* {{{ */ { int len; const xmlChar *nqname; nqname = xmlSplitQName3(name, &len); if (nqname != NULL) { xmlNsPtr ns; xmlChar *prefix = xmlStrndup(name, len); if (prefix && xmlStrEqual(prefix, (xmlChar *)"xmlns")) { ns = elem->nsDef; while (ns) { if (xmlStrEqual(ns->prefix, nqname)) { break; } ns = ns->next; } xmlFree(prefix); return (xmlNodePtr)ns; } ns = xmlSearchNs(elem->doc, elem, prefix); if (prefix != NULL) { xmlFree(prefix); } if (ns != NULL) { return (xmlNodePtr)xmlHasNsProp(elem, nqname, ns->href); } } else { if (xmlStrEqual(name, (xmlChar *)"xmlns")) { xmlNsPtr nsPtr = elem->nsDef; while (nsPtr) { if (nsPtr->prefix == NULL) { return (xmlNodePtr)nsPtr; } nsPtr = nsPtr->next; } return NULL; } } return (xmlNodePtr)xmlHasNsProp(elem, name, NULL); }
char * soap_env_find_urn(SoapEnv * env) { xmlNsPtr ns; xmlNodePtr body, node; if (!(body = soap_env_get_body(env))) { log_verbose1("body is NULL"); return 0; } /* node is the first child */ if (!(node = soap_xml_get_children(body))) { log_error1("No namespace found"); return 0; } /* if (node->ns && node->ns->prefix) MRC 1/25/2006 */ if (node->ns) { ns = xmlSearchNs(body->doc, node, node->ns->prefix); if (ns != NULL) { return((char *) ns->href); /* namespace found! */ } } else { static char *empty = ""; log_warn1("No namespace found"); return(empty); } return 0; }
/** * This is a callback for validator errors. * * Purpose is to cumulate all encountered errors in a list, which is further * processed after the validation is done. If any malloc inside this routine * fails, the error is silently dropped and is not queued in the list of * errors. That makes algorithm a bit less complicated. * * @param ctx Hook's context pointer. * @param error Specification of encountered error. */ static void validerr_callback(void *ctx, xmlErrorPtr error) { /* used to get content of problematic xml tag */ xmlBufferPtr buf; xmlNodePtr node; int len; /* used for new list item creation */ epp_error *valerr; /* get context parameters */ qhead *error_list = ((valerr_ctx *) ctx)->err_list; xmlDocPtr doc = ((valerr_ctx *) ctx)->doc; void *pool = ((valerr_ctx *) ctx)->pool; /* in case of allocation failure simply don't log the error and exit */ if ((valerr = epp_malloc(pool, sizeof *valerr)) == NULL) return; /* * xmlError has quite a lot of fields, we are interested only in 3 * of them: code, message, node. */ /* * XXX error code should be further examined in order to get * more detailed error * valerr->code = error->code; */ /* * get error message (we don't use strdup because we have to * truncate trailing newline) */ len = strlen(error->message); valerr->reason = (char *) epp_malloc(pool, len); if (valerr->reason == NULL) return; strncpy(valerr->reason, error->message, --len); /*truncate trailing \n */ (valerr->reason)[len] = '\0'; /* XXX this needs to be done better way */ /* * recognized errors: * unknown command (2000) * required parameter missing (2003) * Parameter value range error (2004) * Parameter value syntax error (2005) * Unimplemented extension (2103) * ???Unimplemented command (2101)??? * ???Unimplemented option (2102)??? * all other errors are reported as: * command syntax error (2001) */ /* get content of problematic tag */ buf = xmlBufferCreate(); if (buf == NULL) return; node = (xmlNodePtr) error->node; if (node->ns != NULL) { xmlNsPtr nsdef; nsdef = xmlSearchNs(doc, node, node->ns->prefix); if (nsdef != NULL) xmlNewNs(node, nsdef->href, nsdef->prefix); } if (xmlNodeDump(buf, doc, (xmlNodePtr) node, 0, 0) < 0) { xmlBufferFree(buf); return; } valerr->value = epp_strdup(pool, (char *) buf->content); xmlBufferFree(buf); if (valerr->value == NULL) return; valerr->spec = errspec_not_valid; /* surrounding tags are included */ /* enqueue new error item */ if (q_add(pool, error_list, valerr)) /* we have nothing to do here in case of error */ return; }
/* :nodoc: */ static void relink_namespace(xmlNodePtr reparented) { xmlChar *name, *prefix; xmlNodePtr child; xmlNsPtr ns; if (reparented->type != XML_ATTRIBUTE_NODE && reparented->type != XML_ELEMENT_NODE) return; if (reparented->ns == NULL || reparented->ns->prefix == NULL) { name = xmlSplitQName2(reparented->name, &prefix); if(reparented->type == XML_ATTRIBUTE_NODE) { if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) return; } ns = xmlSearchNs(reparented->doc, reparented, prefix); if (ns == NULL && reparented->parent) { ns = xmlSearchNs(reparented->doc, reparented->parent, prefix); } if (ns != NULL) { xmlNodeSetName(reparented, name); xmlSetNs(reparented, ns); } } /* Avoid segv when relinking against unlinked nodes. */ if (reparented->type != XML_ELEMENT_NODE || !reparented->parent) return; /* Make sure that our reparented node has the correct namespaces */ if(!reparented->ns && reparented->doc != (xmlDocPtr)reparented->parent) xmlSetNs(reparented, reparented->parent->ns); /* Search our parents for an existing definition */ if(reparented->nsDef) { xmlNsPtr curr = reparented->nsDef; xmlNsPtr prev = NULL; while(curr) { xmlNsPtr ns = xmlSearchNsByHref( reparented->doc, reparented->parent, curr->href ); /* If we find the namespace is already declared, remove it from this * definition list. */ if(ns && ns != curr) { if (prev) { prev->next = curr->next; } else { reparented->nsDef = curr->next; } nokogiri_root_nsdef(curr, reparented->doc); } else { prev = curr; } curr = curr->next; } } /* Only walk all children if there actually is a namespace we need to */ /* reparent. */ if(NULL == reparented->ns) return; /* When a node gets reparented, walk it's children to make sure that */ /* their namespaces are reparented as well. */ child = reparented->children; while(NULL != child) { relink_namespace(child); child = child->next; } if (reparented->type == XML_ELEMENT_NODE) { child = (xmlNodePtr)((xmlElementPtr)reparented)->attributes; while(NULL != child) { relink_namespace(child); child = child->next; } } }
static int EBC_Provider_SignMessage_X001(AB_PROVIDER *pro, EB_MSG *msg, AB_USER *u, xmlNodePtr node) { EBC_PROVIDER *dp; int rv; GWEN_CRYPT_TOKEN *ct; const GWEN_CRYPT_TOKEN_CONTEXT *ctx; const GWEN_CRYPT_TOKEN_KEYINFO *ki; uint32_t keyId; GWEN_BUFFER *hbuf; GWEN_BUFFER *bbuf; xmlNodePtr nodeX = NULL; xmlNodePtr nodeXX = NULL; xmlNodePtr nodeXXX = NULL; xmlNodePtr nodeXXXX = NULL; xmlNsPtr ns; assert(pro); dp=GWEN_INHERIT_GETDATA(AB_PROVIDER, EBC_PROVIDER, pro); assert(dp); /* get crypt token and context */ rv=EBC_Provider_MountToken(pro, u, &ct, &ctx); if (rv<0) { DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv); return rv; } /* get key id */ keyId=GWEN_Crypt_Token_Context_GetAuthSignKeyId(ctx); ki=GWEN_Crypt_Token_GetKeyInfo(ct, keyId, 0xffffffff, 0); if (ki==NULL) { DBG_INFO(AQEBICS_LOGDOMAIN, "Keyinfo %04x not found on crypt token [%s:%s]", keyId, GWEN_Crypt_Token_GetTypeName(ct), GWEN_Crypt_Token_GetTokenName(ct)); GWEN_Crypt_Token_Close(ct, 0, 0); return GWEN_ERROR_NOT_FOUND; } /* prepare signature nodes */ ns=xmlSearchNs(EB_Msg_GetDoc(msg), node, BAD_CAST "ds"); assert(ns); /* build hash */ bbuf=GWEN_Buffer_new(0, 256, 0, 1); rv=EB_Msg_BuildHashSha1(msg, bbuf); if (rv) { DBG_ERROR(AQEBICS_LOGDOMAIN, "Could not build hash"); GWEN_Buffer_free(bbuf); return rv; } /* base64 encode */ hbuf=GWEN_Buffer_new(0, 256, 0, 1); rv=GWEN_Base64_Encode((const uint8_t*)GWEN_Buffer_GetStart(bbuf), GWEN_Buffer_GetUsedBytes(bbuf), hbuf, 0); if (rv<0) { DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv); GWEN_Buffer_free(hbuf); GWEN_Buffer_free(bbuf); return rv; } GWEN_Buffer_free(bbuf); /* create signature node */ nodeX=xmlNewChild(node, ns, BAD_CAST "SignedInfo", NULL); nodeXX=xmlNewChild(nodeX, ns, BAD_CAST "CanonicalizationMethod", NULL); xmlNewProp(nodeXX, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"); nodeXX=xmlNewChild(nodeX, ns, BAD_CAST "SignatureMethod", NULL); xmlNewProp(nodeXX, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/2000/09/xmldsig#rsa-sha1"); nodeXX=xmlNewChild(nodeX, ns, BAD_CAST "Reference", NULL); xmlNewProp(nodeXX, BAD_CAST "URI", BAD_CAST "#xpointer(//*[@authenticate='true'])"); nodeXXX=xmlNewChild(nodeXX, ns, BAD_CAST "Transforms", NULL); nodeXXXX=xmlNewChild(nodeXXX, ns, BAD_CAST "Transform", NULL); xmlNewProp(nodeXXXX, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"); nodeXXX=xmlNewChild(nodeXX, ns, BAD_CAST "DigestMethod", NULL); xmlNewProp(nodeXXX, BAD_CAST "Algorithm", BAD_CAST "http://www.w3.org/2000/09/xmldsig#sha1"); /* store hash value */ xmlNewTextChild(nodeXX, ns, BAD_CAST "DigestValue", BAD_CAST GWEN_Buffer_GetStart(hbuf)); GWEN_Buffer_free(hbuf); /* build hash over SignedInfo */ bbuf=GWEN_Buffer_new(0, 256, 0, 1); rv=EB_Xml_BuildNodeHashSha1(nodeX, "#xpointer(//*)", bbuf); if (rv<0) { DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv); GWEN_Buffer_free(bbuf); return rv; } /* sign hash */ if (1) { GWEN_CRYPT_PADDALGO *algo; int ksize; uint32_t l; const uint8_t prefix[]={ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14}; /* add prefix to hash of SignedInfo */ hbuf=GWEN_Buffer_new(0, 256, 0, 1); ksize=GWEN_Crypt_Token_KeyInfo_GetKeySize(ki); GWEN_Buffer_AppendBytes(hbuf, (const char*)prefix, sizeof(prefix)); GWEN_Buffer_AppendBuffer(hbuf, bbuf); GWEN_Buffer_Reset(bbuf); /* select padd algo */ algo=GWEN_Crypt_PaddAlgo_new(GWEN_Crypt_PaddAlgoId_Pkcs1_1); GWEN_Crypt_PaddAlgo_SetPaddSize(algo, ksize); /* actually sign */ GWEN_Buffer_AllocRoom(bbuf, ksize+16); l=GWEN_Buffer_GetMaxUnsegmentedWrite(bbuf); rv=GWEN_Crypt_Token_Sign(ct, keyId, algo, (const uint8_t*)GWEN_Buffer_GetStart(hbuf), GWEN_Buffer_GetUsedBytes(hbuf), (uint8_t*)GWEN_Buffer_GetPosPointer(bbuf), &l, NULL, /* ignore seq counter */ 0); GWEN_Crypt_PaddAlgo_free(algo); if (rv<0) { DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv); GWEN_Buffer_free(bbuf); GWEN_Buffer_free(hbuf); return rv; } GWEN_Buffer_IncrementPos(bbuf, l); GWEN_Buffer_AdjustUsedBytes(bbuf); /* base 64 encode signature */ GWEN_Buffer_Reset(hbuf); rv=GWEN_Base64_Encode((const uint8_t*)GWEN_Buffer_GetStart(bbuf), GWEN_Buffer_GetUsedBytes(bbuf), hbuf, 0); if (rv<0) { DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv); GWEN_Buffer_free(hbuf); GWEN_Buffer_free(bbuf); return rv; } GWEN_Buffer_free(bbuf); /* store signature */ xmlNewTextChild(node, ns, BAD_CAST "SignatureValue", BAD_CAST GWEN_Buffer_GetStart(hbuf)); GWEN_Buffer_free(hbuf); } return 0; }
static int rest_show_check(mtev_http_rest_closure_t *restc, int npats, char **pats) { mtev_http_session_ctx *ctx = restc->http_ctx; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; xmlDocPtr doc = NULL; xmlNodePtr node, root, attr, config, state, tmp, anode; uuid_t checkid; noit_check_t *check; char xpath[1024], *uuid_conf, *module = NULL, *value = NULL; int rv, mod, mod_cnt, cnt, error_code = 500; mtev_hash_iter iter = MTEV_HASH_ITER_ZERO; const char *k; int klen; void *data; mtev_hash_table *configh; if(npats != 2 && npats != 3) goto error; rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); if(rv == 0) goto not_found; if(rv < 0) goto error; mtev_conf_xml_xpath(NULL, &xpath_ctxt); pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto not_found; cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(cnt != 1) goto error; node = (mtev_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error; if(npats == 3 && !strcmp(pats[2], ".json")) { return rest_show_check_json(restc, checkid); } doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); #define MYATTR(node,a,n,b) _mtev_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(node,a,n,b) \ _mtev_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(parent, node, a) do { \ char *_value = NULL; \ INHERIT(node, a, anode, _value); \ if(_value != NULL) { \ int clen, plen;\ char *_cpath, *_apath; \ xmlNodePtr child; \ _cpath = node ? (char *)xmlGetNodePath(node) : strdup(""); \ _apath = anode ? (char *)xmlGetNodePath(anode) : strdup(""); \ clen = strlen(_cpath); \ plen = strlen("/noit/checks"); \ child = xmlNewNode(NULL, (xmlChar *)#a); \ xmlNodeAddContent(child, (xmlChar *)_value); \ if(!strncmp(_cpath, _apath, clen) && _apath[clen] == '/') { \ } \ else { \ xmlSetProp(child, (xmlChar *)"inherited", (xmlChar *)_apath+plen); \ } \ xmlAddChild(parent, child); \ free(_cpath); \ free(_apath); \ free(_value); \ } \ } while(0) attr = xmlNewNode(NULL, (xmlChar *)"attributes"); xmlAddChild(root, attr); SHOW_ATTR(attr,node,uuid); SHOW_ATTR(attr,node,seq); /* Name is odd, it falls back transparently to module */ if(!INHERIT(node, module, tmp, module)) module = NULL; xmlAddChild(attr, (tmp = xmlNewNode(NULL, (xmlChar *)"name"))); if(MYATTR(node, name, anode, value)) xmlNodeAddContent(tmp, (xmlChar *)value); else if(module) xmlNodeAddContent(tmp, (xmlChar *)module); if(value) free(value); if(module) free(module); SHOW_ATTR(attr,node,module); SHOW_ATTR(attr,node,target); SHOW_ATTR(attr,node,resolve_rtype); SHOW_ATTR(attr,node,seq); SHOW_ATTR(attr,node,period); SHOW_ATTR(attr,node,timeout); SHOW_ATTR(attr,node,oncheck); SHOW_ATTR(attr,node,filterset); SHOW_ATTR(attr,node,disable); /* Add the config */ config = xmlNewNode(NULL, (xmlChar *)"config"); configh = mtev_conf_get_hash(node, "config"); while(mtev_hash_next(configh, &iter, &k, &klen, &data)) NODE_CONTENT(config, k, data); mtev_hash_destroy(configh, free, free); free(configh); mod_cnt = noit_check_registered_module_cnt(); for(mod=0; mod<mod_cnt; mod++) { xmlNsPtr ns; const char *nsname; char buff[256]; nsname = noit_check_registered_module(mod); snprintf(buff, sizeof(buff), "noit://module/%s", nsname); ns = xmlSearchNs(root->doc, root, (xmlChar *)nsname); if(!ns) ns = xmlNewNs(root, (xmlChar *)buff, (xmlChar *)nsname); if(ns) { configh = mtev_conf_get_namespaced_hash(node, "config", nsname); if(configh) { memset(&iter, 0, sizeof(iter)); while(mtev_hash_next(configh, &iter, &k, &klen, &data)) { NS_NODE_CONTENT(config, ns, "value", data, xmlSetProp(tmp, (xmlChar *)"name", (xmlChar *)k); ); } mtev_hash_destroy(configh, free, free); free(configh); } } }
static HRESULT WINAPI xmlelem_getAttribute(IXMLElement *iface, BSTR name, VARIANT *value) { static const WCHAR xmllangW[] = { 'x','m','l',':','l','a','n','g',0 }; xmlelem *This = impl_from_IXMLElement(iface); xmlChar *val = NULL; TRACE("(%p)->(%s, %p)\n", This, debugstr_w(name), value); if (!value) return E_INVALIDARG; VariantInit(value); V_BSTR(value) = NULL; if (!name) return E_INVALIDARG; /* case for xml:lang attribute */ if (!lstrcmpiW(name, xmllangW)) { xmlNsPtr ns; ns = xmlSearchNs(This->node->doc, This->node, (xmlChar*)"xml"); val = xmlGetNsProp(This->node, (xmlChar*)"lang", ns->href); } else { xmlAttrPtr attr; xmlChar *xml_name; xml_name = xmlchar_from_wchar(name); attr = This->node->properties; while (attr) { BSTR attr_name; attr_name = bstr_from_xmlChar(attr->name); if (!lstrcmpiW(name, attr_name)) { val = xmlNodeListGetString(attr->doc, attr->children, 1); SysFreeString(attr_name); break; } attr = attr->next; SysFreeString(attr_name); } heap_free(xml_name); } if (val) { V_VT(value) = VT_BSTR; V_BSTR(value) = bstr_from_xmlChar(val); } xmlFree(val); TRACE("returning %s\n", debugstr_w(V_BSTR(value))); return (val) ? S_OK : S_FALSE; }