diff --git a/ChangeLog b/ChangeLog index 9ad5c1ff..b5254588 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Apr 17 10:08:19 CEST 2001 Daniel Veillard + + * 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 * HTMLparser.c result/HTML/*: revamped the way the HTML diff --git a/encoding.c b/encoding.c index 41f23254..db7b0cf0 100644 --- a/encoding.c +++ b/encoding.c @@ -81,6 +81,47 @@ static int xmlLittleEndian = 1; * I hope we won't use values > 0xFFFF anytime soon ! */ +/** + * 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 diff --git a/encoding.h b/encoding.h index 527a08cb..555558fe 100644 --- a/encoding.h +++ b/encoding.h @@ -166,8 +166,6 @@ xmlCharEncoding 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 isolat1ToUTF8 (unsigned char* out, 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 527a08cb..555558fe 100644 --- a/include/libxml/encoding.h +++ b/include/libxml/encoding.h @@ -166,8 +166,6 @@ xmlCharEncoding 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 isolat1ToUTF8 (unsigned char* out, 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 a89c451e..37cb19e7 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -1435,7 +1435,7 @@ xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar *cur, int *len) { /* 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 c08d2d88..4ee333d5 100644 --- a/tree.c +++ b/tree.c @@ -106,7 +106,7 @@ xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { */ xmlBufferAllocationScheme xmlGetBufferAllocationScheme(void) { - return xmlBufferAllocScheme; + return(xmlBufferAllocScheme); } /** diff --git a/xpath.c b/xpath.c index 2854ba40..48ddc75b 100644 --- a/xpath.c +++ b/xpath.c @@ -4280,7 +4280,7 @@ xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { xmlChar *content; content = xmlNodeGetContent(ctxt->context->node); - valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(content))); + valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); xmlFree(content); } return; @@ -4289,7 +4289,7 @@ xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { 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 @@ xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { 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 @@ xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { */ 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 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, 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 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, 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 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, #endif addNode(list, cur); } else if ((cur->ns != NULL) && - (xmlStrEqual(prefix, + (xmlStrEqual(URI, cur->ns->href))) { #ifdef DEBUG_STEP n++; @@ -6805,7 +6813,7 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, } } else { if ((cur->ns != NULL) && - (xmlStrEqual(prefix, + (xmlStrEqual(URI, cur->ns->href))) { #ifdef DEBUG_STEP n++; @@ -6828,7 +6836,7 @@ xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, } } else { if ((attr->ns != NULL) && - (xmlStrEqual(prefix, + (xmlStrEqual(URI, attr->ns->href))) { #ifdef DEBUG_STEP n++;