/** * exsltStrDecodeUriFunction: * @ctxt: an XPath parser context * @nargs: the number of arguments * * reverses URI-Escaping of a string */ static void exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { int str_len = 0; xmlChar *str = NULL, *ret = NULL, *tmp; if ((nargs < 1) || (nargs > 2)) { xmlXPathSetArityError(ctxt); return; } if (nargs >= 2) { /* check for UTF-8 if encoding was explicitly given; we don't support anything else yet */ tmp = xmlXPathPopString(ctxt); if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) { xmlXPathReturnEmptyString(ctxt); xmlFree(tmp); return; } xmlFree(tmp); } str = xmlXPathPopString(ctxt); str_len = xmlUTF8Strlen(str); if (str_len == 0) { xmlXPathReturnEmptyString(ctxt); xmlFree(str); return; } ret = (xmlChar *) xmlURIUnescapeString((const char *)str,0,NULL); if (!xmlCheckUTF8(ret)) { /* FIXME: instead of throwing away the whole URI, we should only discard the invalid sequence(s). How to do that? */ xmlXPathReturnEmptyString(ctxt); xmlFree(str); xmlFree(ret); return; } xmlXPathReturnString(ctxt, ret); if (str != NULL) xmlFree(str); }
xmlChar * common_uri_unescape (const xmlChar *url) { return xmlURIUnescapeString (url, -1, NULL); }
/* Code inspired by libvirt src/util/viruri.c, written by danpb, * released under a compatible license. */ static char * query_get (xmlURIPtr uri, const char *search_name) { /* XXX libvirt uses deprecated uri->query field. Why? */ const char *query = uri->query_raw; const char *end, *eq; if (!query || STREQ (query, "")) return NULL; while (*query) { CLEANUP_FREE char *name = NULL; char *value = NULL; /* Find the next separator, or end of the string. */ end = strchr (query, '&'); if (!end) end = strchr(query, ';'); if (!end) end = query + strlen (query); /* Find the first '=' character between here and end. */ eq = strchr(query, '='); if (eq && eq >= end) eq = NULL; /* Empty section (eg. "&&"). */ if (end == query) goto next; /* If there is no '=' character, then we have just "name" * and consistent with CGI.pm we assume value is "". */ else if (!eq) { name = xmlURIUnescapeString (query, end - query, NULL); if (!name) goto no_memory; } /* Or if we have "name=" here (works around annoying * problem when calling xmlURIUnescapeString with len = 0). */ else if (eq+1 == end) { name = xmlURIUnescapeString (query, eq - query, NULL); if (!name) goto no_memory; } /* If the '=' character is at the beginning then we have * "=value" and consistent with CGI.pm we _ignore_ this. */ else if (query == eq) goto next; /* Otherwise it's "name=value". */ else { name = xmlURIUnescapeString (query, eq - query, NULL); if (!name) goto no_memory; value = xmlURIUnescapeString (eq+1, end - (eq+1), NULL); if (!value) { goto no_memory; } } /* Is it the name we're looking for? */ if (STREQ (name, search_name)) { if (!value) { value = strdup (""); if (!value) goto no_memory; } return value; } free (value); next: query = end; if (*query) query++; /* skip '&' separator */ } /* search_name not found */ return NULL; no_memory: perror ("malloc"); return NULL; }
int msiLoadMetadataFromXml(msParam_t *targetObj, msParam_t *xmlObj, ruleExecInfo_t *rei) { /* for parsing msParams and to open iRODS objects */ dataObjInp_t xmlDataObjInp, *myXmlDataObjInp; dataObjInp_t targetObjInp, *myTargetObjInp; int xmlObjID; /* for getting size of objects to read from */ rodsObjStat_t *rodsObjStatOut = NULL; /* for reading from iRODS objects */ openedDataObjInp_t openedDataObjInp; bytesBuf_t *xmlBuf; /* misc. to avoid repeating rei->rsComm */ rsComm_t *rsComm; /* for xml parsing */ xmlDocPtr doc; /* for XPath evaluation */ xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj; xmlChar xpathExpr[] = "//AVU"; xmlNodeSetPtr nodes; int avuNbr, i; /* for new AVU creation */ modAVUMetadataInp_t modAVUMetadataInp; int max_attr_len = 2700; char attrStr[max_attr_len]; /********************************* USUAL INIT PROCEDURE **********************************/ /* For testing mode when used with irule --test */ RE_TEST_MACRO (" Calling msiLoadMetadataFromXml") /* Sanity checks */ if (rei == NULL || rei->rsComm == NULL) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: input rei or rsComm is NULL."); return (SYS_INTERNAL_NULL_INPUT_ERR); } rsComm = rei->rsComm; /********************************** RETRIEVE INPUT PARAMS **************************************/ /* Get path of target object */ rei->status = parseMspForDataObjInp (targetObj, &targetObjInp, &myTargetObjInp, 0); if (rei->status < 0) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: input targetObj error. status = %d", rei->status); return (rei->status); } /* Get path of XML document */ rei->status = parseMspForDataObjInp (xmlObj, &xmlDataObjInp, &myXmlDataObjInp, 0); if (rei->status < 0) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: input xmlObj error. status = %d", rei->status); return (rei->status); } /******************************** OPEN AND READ FROM XML OBJECT ********************************/ /* Open XML file */ if ((xmlObjID = rsDataObjOpen(rsComm, &xmlDataObjInp)) < 0) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Cannot open XML data object. status = %d", xmlObjID); return (xmlObjID); } /* Get size of XML file */ rei->status = rsObjStat (rsComm, &xmlDataObjInp, &rodsObjStatOut); if (rei->status < 0 || !rodsObjStatOut) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Cannot stat XML data object. status = %d", rei->status); return (rei->status); } /* xmlBuf init */ /* memory for xmlBuf->buf is allocated in rsFileRead() */ xmlBuf = (bytesBuf_t *) malloc (sizeof (bytesBuf_t)); memset (xmlBuf, 0, sizeof (bytesBuf_t)); /* Read XML file */ memset (&openedDataObjInp, 0, sizeof (openedDataObjInp_t)); openedDataObjInp.l1descInx = xmlObjID; openedDataObjInp.len = (int)rodsObjStatOut->objSize; rei->status = rsDataObjRead (rsComm, &openedDataObjInp, xmlBuf); /* Make sure that the result is null terminated */ if (strlen((char*)xmlBuf->buf) > (size_t)openedDataObjInp.len) { ((char*)xmlBuf->buf)[openedDataObjInp.len-1]='\0'; } /* Close XML file */ rei->status = rsDataObjClose (rsComm, &openedDataObjInp); /* cleanup */ freeRodsObjStat (rodsObjStatOut); /******************************** PARSE XML DOCUMENT ********************************/ xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; /* Parse xmlBuf.buf into an xmlDocPtr */ doc = xmlParseDoc((xmlChar*)xmlBuf->buf); /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Unable to create new XPath context."); xmlFreeDoc(doc); return(-1); } /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); if(xpathObj == NULL) { rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Unable to evaluate XPath expression \"%s\".", xpathExpr); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return(-1); } /* How many AVU nodes did we get? */ if ((nodes = xpathObj->nodesetval) != NULL) { avuNbr = nodes->nodeNr; } else { avuNbr = 0; } /******************************** CREATE AVU TRIPLETS ********************************/ /* Add a new AVU for each node. It's ok to process the nodes in forward order since we're not modifying them */ for(i = 0; i < avuNbr; i++) { if (nodes->nodeTab[i]) { // /* temporary: Add index number to avoid duplicating attribute names */ // memset(attrStr, '\0', MAX_NAME_LEN); // snprintf(attrStr, MAX_NAME_LEN - 1, "%04d: %s", i+1, (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Attribute")) ); /* Truncate if needed. No prefix. */ memset(attrStr, '\0', max_attr_len); snprintf(attrStr, max_attr_len - 1, "%s", (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Attribute")) ); /* init modAVU input */ memset (&modAVUMetadataInp, 0, sizeof(modAVUMetadataInp_t)); modAVUMetadataInp.arg0 = "add"; modAVUMetadataInp.arg1 = "-d"; /* Use target object if one was provided, otherwise look for it in the XML doc */ if (myTargetObjInp->objPath != NULL && strlen(myTargetObjInp->objPath) > 0) { modAVUMetadataInp.arg2 = myTargetObjInp->objPath; } else { modAVUMetadataInp.arg2 = xmlURIUnescapeString((char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Target")), MAX_NAME_LEN, NULL); } modAVUMetadataInp.arg3 = attrStr; modAVUMetadataInp.arg4 = (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Value")); modAVUMetadataInp.arg5 = (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Unit")); /* invoke rsModAVUMetadata() */ rei->status = rsModAVUMetadata (rsComm, &modAVUMetadataInp); } } /************************************** WE'RE DONE **************************************/ /* cleanup of all xml parsing stuff */ xmlFreeDoc(doc); xmlCleanupParser(); return (rei->status); }
static RejillaTrack * _read_data_track (xmlDocPtr project, xmlNodePtr item) { RejillaTrackDataCfg *track; GSList *grafts= NULL; GSList *excluded = NULL; track = rejilla_track_data_cfg_new (); while (item) { if (!xmlStrcmp (item->name, (const xmlChar *) "graft")) { if (!(grafts = _read_graft_point (project, item->xmlChildrenNode, grafts))) goto error; } else if (!xmlStrcmp (item->name, (const xmlChar *) "icon")) { xmlChar *icon_path; icon_path = xmlNodeListGetString (project, item->xmlChildrenNode, 1); if (!icon_path) goto error; rejilla_track_data_cfg_set_icon (track, (gchar *) icon_path, NULL); g_free (icon_path); } else if (!xmlStrcmp (item->name, (const xmlChar *) "restored")) { xmlChar *restored; restored = xmlNodeListGetString (project, item->xmlChildrenNode, 1); if (!restored) goto error; rejilla_track_data_cfg_dont_filter_uri (track, (gchar *) restored); g_free (restored); } else if (!xmlStrcmp (item->name, (const xmlChar *) "excluded")) { xmlChar *excluded_uri; excluded_uri = xmlNodeListGetString (project, item->xmlChildrenNode, 1); if (!excluded_uri) goto error; excluded = g_slist_prepend (excluded, xmlURIUnescapeString ((char*) excluded_uri, 0, NULL)); g_free (excluded_uri); } else if (item->type == XML_ELEMENT_NODE) goto error; item = item->next; } grafts = g_slist_reverse (grafts); excluded = g_slist_reverse (excluded); rejilla_track_data_set_source (REJILLA_TRACK_DATA (track), grafts, excluded); return REJILLA_TRACK (track); error: g_slist_foreach (grafts, (GFunc) rejilla_graft_point_free, NULL); g_slist_free (grafts); g_slist_foreach (excluded, (GFunc) g_free, NULL); g_slist_free (excluded); g_object_unref (track); return NULL; }
struct qparam_set * qparam_query_parse (const char *query) { struct qparam_set *ps; const char *end, *eq; ps = new_qparam_set (0, NULL); if (!ps) { virReportOOMError(); return NULL; } if (!query || query[0] == '\0') return ps; while (*query) { char *name = NULL, *value = NULL; /* Find the next separator, or end of the string. */ end = strchr (query, '&'); if (!end) end = strchr (query, ';'); if (!end) end = query + strlen (query); /* Find the first '=' character between here and end. */ eq = strchr (query, '='); if (eq && eq >= end) eq = NULL; /* Empty section (eg. "&&"). */ if (end == query) goto next; /* If there is no '=' character, then we have just "name" * and consistent with CGI.pm we assume value is "". */ else if (!eq) { name = xmlURIUnescapeString (query, end - query, NULL); if (!name) goto out_of_memory; } /* Or if we have "name=" here (works around annoying * problem when calling xmlURIUnescapeString with len = 0). */ else if (eq+1 == end) { name = xmlURIUnescapeString (query, eq - query, NULL); if (!name) goto out_of_memory; } /* If the '=' character is at the beginning then we have * "=value" and consistent with CGI.pm we _ignore_ this. */ else if (query == eq) goto next; /* Otherwise it's "name=value". */ else { name = xmlURIUnescapeString (query, eq - query, NULL); if (!name) goto out_of_memory; value = xmlURIUnescapeString (eq+1, end - (eq+1), NULL); if (!value) { VIR_FREE(name); goto out_of_memory; } } /* Append to the parameter set. */ if (append_qparam (ps, name, value ? value : "") == -1) { VIR_FREE(name); VIR_FREE(value); goto out_of_memory; } VIR_FREE(name); VIR_FREE(value); next: query = end; if (*query) query ++; /* skip '&' separator */ } return ps; out_of_memory: virReportOOMError(); free_qparam_set (ps); return NULL; }