aboutsummaryrefslogtreecommitdiffstats
path: root/xpath.c
diff options
context:
space:
mode:
authorDaniel Veillard <veillard@src.gnome.org>2002-08-22 20:52:17 +0000
committerDaniel Veillard <veillard@src.gnome.org>2002-08-22 20:52:17 +0000
commit42766c0eea0fa40c7b721fa4c9cf56b4c484b4c7 (patch)
treed9ead6d50eec3c1dd90dfd99b0f88652b00951cc /xpath.c
parent84ec40a51c2c5b4525d3350727ecf3b7d7af1109 (diff)
downloadandroid_external_libxml2-42766c0eea0fa40c7b721fa4c9cf56b4c484b4c7.tar.gz
android_external_libxml2-42766c0eea0fa40c7b721fa4c9cf56b4c484b4c7.tar.bz2
android_external_libxml2-42766c0eea0fa40c7b721fa4c9cf56b4c484b4c7.zip
possible mem leak patch from Jason Adams integrated xf:escape-uri() from
* xpath.c: possible mem leak patch from Jason Adams * xpath.c: integrated xf:escape-uri() from Wesley Terpstra in the XQuery namespace * configure.in: preparing 2.4.24 * doc/*.html: updated the web pages * python/generator.py: closing bug #85258 by generating conditional compile check to avoid linking to routines not configured in. Daniel
Diffstat (limited to 'xpath.c')
-rw-r--r--xpath.c121
1 files changed, 119 insertions, 2 deletions
diff --git a/xpath.c b/xpath.c
index b176ed52..333d2000 100644
--- a/xpath.c
+++ b/xpath.c
@@ -2825,7 +2825,7 @@ xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
ctxt->nsHash = xmlHashCreate(10);
if (ctxt->nsHash == NULL)
return(-1);
- return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) ns_uri,
+ return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri),
(xmlHashDeallocator)xmlFree));
}
@@ -2875,7 +2875,7 @@ xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) {
if (ctxt == NULL)
return;
- xmlHashFree(ctxt->nsHash, NULL);
+ xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree);
ctxt->nsHash = NULL;
}
@@ -10773,6 +10773,119 @@ xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) {
return(res);
}
+/************************************************************************
+ * *
+ * Extra functions not pertaining to the XPath spec *
+ * *
+ ************************************************************************/
+/**
+ * xmlXPathEscapeUriFunction:
+ * @ctxt: the XPath Parser context
+ * @nargs: the number of arguments
+ *
+ * Implement the escape-uri() XPath function
+ * string escape-uri(string $str, bool $escape-reserved)
+ *
+ * This function applies the URI escaping rules defined in section 2 of [RFC
+ * 2396] to the string supplied as $uri-part, which typically represents all
+ * or part of a URI. The effect of the function is to replace any special
+ * character in the string by an escape sequence of the form %xx%yy...,
+ * where xxyy... is the hexadecimal representation of the octets used to
+ * represent the character in UTF-8.
+ *
+ * The set of characters that are escaped depends on the setting of the
+ * boolean argument $escape-reserved.
+ *
+ * If $escape-reserved is true, all characters are escaped other than lower
+ * case letters a-z, upper case letters A-Z, digits 0-9, and the characters
+ * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!"
+ * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only
+ * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and
+ * A-F).
+ *
+ * If $escape-reserved is false, the behavior differs in that characters
+ * referred to in [RFC 2396] as reserved characters are not escaped. These
+ * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",".
+ *
+ * [RFC 2396] does not define whether escaped URIs should use lower case or
+ * upper case for hexadecimal digits. To ensure that escaped URIs can be
+ * compared using string comparison functions, this function must always use
+ * the upper-case letters A-F.
+ *
+ * Generally, $escape-reserved should be set to true when escaping a string
+ * that is to form a single part of a URI, and to false when escaping an
+ * entire URI or URI reference.
+ *
+ * In the case of non-ascii characters, the string is encoded according to
+ * utf-8 and then converted according to RFC 2396.
+ *
+ * Examples
+ * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true())
+ * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean"
+ * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false())
+ * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean"
+ *
+ */
+void
+xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) {
+ xmlXPathObjectPtr str;
+ int escape_reserved;
+ xmlBufferPtr target;
+ xmlChar *cptr;
+ xmlChar escape[4];
+
+ CHECK_ARITY(2);
+
+ escape_reserved = xmlXPathPopBoolean(ctxt);
+
+ CAST_TO_STRING;
+ str = valuePop(ctxt);
+
+ target = xmlBufferCreate();
+
+ escape[0] = '%';
+ escape[3] = 0;
+
+ if (target) {
+ for (cptr = str->stringval; *cptr; cptr++) {
+ if ((*cptr >= 'A' && *cptr <= 'Z') ||
+ (*cptr >= 'a' && *cptr <= 'z') ||
+ (*cptr >= '0' && *cptr <= '9') ||
+ *cptr == '-' || *cptr == '_' || *cptr == '.' ||
+ *cptr == '!' || *cptr == '~' || *cptr == '*' ||
+ *cptr == '\''|| *cptr == '(' || *cptr == ')' ||
+ (*cptr == '%' &&
+ ((cptr[1] >= 'A' && cptr[1] <= 'F') ||
+ (cptr[1] >= 'a' && cptr[1] <= 'f') ||
+ (cptr[1] >= '0' && cptr[1] <= '9')) &&
+ ((cptr[2] >= 'A' && cptr[2] <= 'F') ||
+ (cptr[2] >= 'a' && cptr[2] <= 'f') ||
+ (cptr[2] >= '0' && cptr[2] <= '9'))) ||
+ (!escape_reserved &&
+ (*cptr == ';' || *cptr == '/' || *cptr == '?' ||
+ *cptr == ':' || *cptr == '@' || *cptr == '&' ||
+ *cptr == '=' || *cptr == '+' || *cptr == '$' ||
+ *cptr == ','))) {
+ xmlBufferAdd(target, cptr, 1);
+ } else {
+ if ((*cptr >> 4) < 10)
+ escape[1] = '0' + (*cptr >> 4);
+ else
+ escape[1] = 'A' - 10 + (*cptr >> 4);
+ if ((*cptr & 0xF) < 10)
+ escape[2] = '0' + (*cptr & 0xF);
+ else
+ escape[2] = 'A' - 10 + (*cptr & 0xF);
+
+ xmlBufferAdd(target, &escape[0], 3);
+ }
+ }
+ }
+ valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
+ xmlBufferFree(target);
+ xmlXPathFreeObject(str);
+}
+
/**
* xmlXPathRegisterAllFunctions:
* @ctxt: the XPath context
@@ -10836,6 +10949,10 @@ xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
xmlXPathTrueFunction);
xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
xmlXPathTranslateFunction);
+
+ xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri",
+ (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions",
+ xmlXPathEscapeUriFunction);
}
#endif /* LIBXML_XPATH_ENABLED */