- xpath.c: fixed xmlXPathNodeCollectAndTest() to do proper
  prefix lookup.
- parserInternals.c: fixed the bug reported by Morus Walter
  due to an off by one typo in xmlStringCurrentChar()
Daniel
diff --git a/ChangeLog b/ChangeLog
index 9ad5c1f..b525458 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Tue Apr 17 10:08:19 CEST 2001 Daniel Veillard <Daniel.Veillard@imag.fr>
+
+	* xpath.c: fixed xmlXPathNodeCollectAndTest() to do proper
+	  prefix lookup.
+	* parserInternals.c: fixed the bug reported by Morus Walter
+	  due to an off by one typo in xmlStringCurrentChar()
+	
 Thu Apr 12 17:41:09 CEST 2001 Daniel Veillard <Daniel.Veillard@imag.fr>
 
 	* HTMLparser.c result/HTML/*: revamped the way the HTML
diff --git a/encoding.c b/encoding.c
index 41f2325..db7b0cf 100644
--- a/encoding.c
+++ b/encoding.c
@@ -82,6 +82,47 @@
  */
 
 /**
+ * xmlUTF8Strlen:
+ * @utf:  a sequence of UTF-8 encoded bytes
+ *
+ * compute the lenght of an UTF8 string, it doesn't do a full UTF8
+ * checking of the content of the string.
+ *
+ * Returns the number of characters in the string or -1 in case of error
+ */
+int
+xmlUTF8Strlen(const unsigned char *utf) {
+    int ret = 0;
+
+    if (utf == NULL)
+	return(-1);
+
+    while (*utf != 0) {
+	if (utf[0] & 0x80) {
+	    if ((utf[1] & 0xc0) != 0x80)
+		return(-1);
+	    if ((utf[0] & 0xe0) == 0xe0) {
+		if ((utf[2] & 0xc0) != 0x80)
+		    return(-1);
+		if ((utf[0] & 0xf0) == 0xf0) {
+		    if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
+			return(-1);
+		    utf += 4;
+		} else {
+		    utf += 3;
+		}
+	    } else {
+		utf += 2;
+	    }
+	} else {
+	    utf++;
+	}
+	ret++;
+    }
+    return(ret);
+}
+
+/**
  * xmlGetUTF8Char:
  * @utf:  a sequence of UTF-8 encoded bytes
  * @len:  a pointer to @bytes len
diff --git a/encoding.h b/encoding.h
index 527a08c..555558f 100644
--- a/encoding.h
+++ b/encoding.h
@@ -166,8 +166,6 @@
 	xmlDetectCharEncoding		(const unsigned char* in,
 					 int len);
 
-int	xmlCheckUTF8			(const unsigned char *utf);
-
 int	xmlCharEncOutFunc		(xmlCharEncodingHandler *handler,
 					 xmlBufferPtr out,
 					 xmlBufferPtr in);
@@ -191,6 +189,9 @@
 					 int *outlen,
 					 const unsigned char* in,
 					 int *inlen);
+int	xmlCheckUTF8			(const unsigned char *utf);
+int	xmlUTF8Strlen			(const unsigned char *utf);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/libxml/encoding.h b/include/libxml/encoding.h
index 527a08c..555558f 100644
--- a/include/libxml/encoding.h
+++ b/include/libxml/encoding.h
@@ -166,8 +166,6 @@
 	xmlDetectCharEncoding		(const unsigned char* in,
 					 int len);
 
-int	xmlCheckUTF8			(const unsigned char *utf);
-
 int	xmlCharEncOutFunc		(xmlCharEncodingHandler *handler,
 					 xmlBufferPtr out,
 					 xmlBufferPtr in);
@@ -191,6 +189,9 @@
 					 int *outlen,
 					 const unsigned char* in,
 					 int *inlen);
+int	xmlCheckUTF8			(const unsigned char *utf);
+int	xmlUTF8Strlen			(const unsigned char *utf);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/parserInternals.c b/parserInternals.c
index a89c451..37cb19e 100644
--- a/parserInternals.c
+++ b/parserInternals.c
@@ -1435,7 +1435,7 @@
 	      /* 2-byte code */
 		*len = 2;
 		val = (cur[0] & 0x1f) << 6;
-		val |= cur[2] & 0x3f;
+		val |= cur[1] & 0x3f;
 	    }
 	    if (!IS_CHAR(val)) {
 		if ((ctxt->sax != NULL) &&
diff --git a/tree.c b/tree.c
index c08d2d8..4ee333d 100644
--- a/tree.c
+++ b/tree.c
@@ -106,7 +106,7 @@
  */
 xmlBufferAllocationScheme
 xmlGetBufferAllocationScheme(void) {
-    return xmlBufferAllocScheme;
+    return(xmlBufferAllocScheme);
 }
 
 /**
diff --git a/xpath.c b/xpath.c
index 2854ba4..48ddc75 100644
--- a/xpath.c
+++ b/xpath.c
@@ -4280,7 +4280,7 @@
 	    xmlChar *content;
 
 	    content = xmlNodeGetContent(ctxt->context->node);
-	    valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(content)));
+	    valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content)));
 	    xmlFree(content);
 	}
 	return;
@@ -4289,7 +4289,7 @@
     CAST_TO_STRING;
     CHECK_TYPE(XPATH_STRING);
     cur = valuePop(ctxt);
-    valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(cur->stringval)));
+    valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval)));
     xmlXPathFreeObject(cur);
 }
 
@@ -4441,7 +4441,7 @@
     xmlChar *ret;
 
     /* 
-     * Conformance needs to be checked !!!!!
+     * TODO: need to be converted to UTF8 strings
      */
     if (nargs < 2) {
 	CHECK_ARITY(2);
@@ -4672,42 +4672,44 @@
  */
 void
 xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
-  xmlXPathObjectPtr str;
-  xmlXPathObjectPtr from;
-  xmlXPathObjectPtr to;
-  xmlBufferPtr target;
-  int i, offset, max;
-  xmlChar ch;
-  const xmlChar *point;
+    xmlXPathObjectPtr str;
+    xmlXPathObjectPtr from;
+    xmlXPathObjectPtr to;
+    xmlBufferPtr target;
+    int i, offset, max;
+    xmlChar ch;
+    const xmlChar *point;
 
-  CHECK_ARITY(3);
+    /* 
+     * TODO: need to be converted to UTF8 strings
+     */
+    CHECK_ARITY(3);
 
-  CAST_TO_STRING;
-  to = valuePop(ctxt);
-  CAST_TO_STRING;
-  from = valuePop(ctxt);
-  CAST_TO_STRING;
-  str = valuePop(ctxt);
+    CAST_TO_STRING;
+    to = valuePop(ctxt);
+    CAST_TO_STRING;
+    from = valuePop(ctxt);
+    CAST_TO_STRING;
+    str = valuePop(ctxt);
 
-  target = xmlBufferCreate();
-  if (target) {
-    max = xmlStrlen(to->stringval);
-    for (i = 0; (ch = str->stringval[i]); i++) {
-      point = xmlStrchr(from->stringval, ch);
-      if (point) {
-	/* Warning: This may not work with UTF-8 */
-	offset = (int)(point - from->stringval);
-	if (offset < max)
-	  xmlBufferAdd(target, &to->stringval[offset], 1);
-      } else
-	xmlBufferAdd(target, &ch, 1);
+    target = xmlBufferCreate();
+    if (target) {
+	max = xmlStrlen(to->stringval);
+	for (i = 0; (ch = str->stringval[i]); i++) {
+	    point = xmlStrchr(from->stringval, ch);
+	    if (point) {
+		offset = (int)(point - from->stringval);
+		if (offset < max)
+		    xmlBufferAdd(target, &to->stringval[offset], 1);
+		} else
+		    xmlBufferAdd(target, &ch, 1);
+	}
     }
-  }
-  valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
-  xmlBufferFree(target);
-  xmlXPathFreeObject(str);
-  xmlXPathFreeObject(from);
-  xmlXPathFreeObject(to);
+    valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
+    xmlBufferFree(target);
+    xmlXPathFreeObject(str);
+    xmlXPathFreeObject(from);
+    xmlXPathFreeObject(to);
 }
 
 /**
@@ -6542,6 +6544,7 @@
     xmlXPathTypeVal type = op->value3;
     const xmlChar *prefix = op->value4;
     const xmlChar *name = op->value5;
+    const xmlChar *URI = NULL;
 
 #ifdef DEBUG_STEP
     int n = 0, t = 0;
@@ -6558,6 +6561,11 @@
     CHECK_TYPE(XPATH_NODESET);
     obj = valuePop(ctxt);
     addNode = xmlXPathNodeSetAdd;
+    if (prefix != NULL) {
+	URI = xmlXPathNsLookup(ctxt->context, prefix);
+	if (URI == NULL)
+	    XP_ERROR(XPATH_UNDEF_PREFIX_ERROR);
+    }
 
 #ifdef DEBUG_STEP
     xmlGenericError(xmlGenericErrorContext,
@@ -6777,7 +6785,7 @@
 #endif
 				addNode(list, cur);
 			    } else if ((cur->ns != NULL) && 
-				(xmlStrEqual(prefix,
+				(xmlStrEqual(URI,
 					     cur->ns->href))) {
 #ifdef DEBUG_STEP
 				n++;
@@ -6805,7 +6813,7 @@
 				    }
 				} else {
 				    if ((cur->ns != NULL) && 
-				        (xmlStrEqual(prefix,
+				        (xmlStrEqual(URI,
 						     cur->ns->href))) {
 #ifdef DEBUG_STEP
 					n++;
@@ -6828,7 +6836,7 @@
 				    }
 				} else {
 				    if ((attr->ns != NULL) && 
-				        (xmlStrEqual(prefix,
+				        (xmlStrEqual(URI,
 						     attr->ns->href))) {
 #ifdef DEBUG_STEP
 					n++;