virxml: Add function to check if string contains some illegal chars

This new function can be used to check if e.g. name of XML
node don't contains forbidden chars like "/" or "\n".

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Sławek Kapłoński 2016-10-19 22:57:46 +02:00 committed by Michal Privoznik
parent 5f2a132786
commit 7a2216460f
3 changed files with 32 additions and 0 deletions

View File

@ -2676,6 +2676,7 @@ virUUIDParse;
# util/virxml.h
virXMLCheckIllegalChars;
virXMLChildElementCount;
virXMLExtractNamespaceXML;
virXMLNodeSanitizeNamespaces;

View File

@ -462,6 +462,33 @@ virXPathLongLong(const char *xpath,
return ret;
}
/**
* virXMLCheckIllegalChars:
* @nodeName: Name of checked node
* @str: string to check
* @illegal: illegal chars to check
*
* If string contains any of illegal chars VIR_ERR_XML_DETAIL error will be
* reported.
*
* Returns: 0 if string don't contains any of given characters, -1 otherwise
*/
int
virXMLCheckIllegalChars(const char *nodeName,
const char *str,
const char *illegal)
{
char *c;
if ((c = strpbrk(str, illegal))) {
virReportError(VIR_ERR_XML_DETAIL,
_("invalid char in %s: %c"), nodeName, *c);
return -1;
}
return 0;
}
/**
* virXMLPropString:
* @node: XML dom node pointer

View File

@ -181,6 +181,10 @@ int virXMLInjectNamespace(xmlNodePtr node,
void virXMLNodeSanitizeNamespaces(xmlNodePtr node);
int virXMLCheckIllegalChars(const char *nodeName,
const char *str,
const char *illegal);
struct _virXMLValidator {
xmlRelaxNGParserCtxtPtr rngParser;
xmlRelaxNGPtr rng;