diff --git a/dlls/msvcrt/mbcs.c b/dlls/msvcrt/mbcs.c index 718e3871332..91efadd73dd 100644 --- a/dlls/msvcrt/mbcs.c +++ b/dlls/msvcrt/mbcs.c @@ -1058,18 +1058,22 @@ int CDECL _ismbbtrail(unsigned int c) */ int CDECL _ismbslead(const unsigned char* start, const unsigned char* str) { - /* Lead bytes can also be trail bytes if caller messed up - * iterating through the string... - */ - if(MSVCRT___mb_cur_max > 1) - { - while(start < str) - start += MSVCRT_isleadbyte(*str) ? 2 : 1; + int lead = 0; - if(start == str) - return MSVCRT_isleadbyte(*str); + if(!g_mbcp_is_multibyte) + return 0; + + /* Lead bytes can also be trail bytes so we need to analise the string + */ + while (start <= str) + { + if (!*start) + return 0; + lead = !lead && _ismbblead(*start); + start++; } - return 0; /* Must have been a trail, we skipped it */ + + return lead ? -1 : 0; } /********************************************************************* @@ -1077,8 +1081,11 @@ int CDECL _ismbslead(const unsigned char* start, const unsigned char* str) */ int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str) { - /* Must not be a lead, and must be preceded by one */ - return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]); + /* Note: this function doesn't check _ismbbtrail */ + if ((str > start) && _ismbslead(start, str-1)) + return -1; + else + return 0; } /********************************************************************* diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c index 004d059c6f8..919964132d7 100644 --- a/dlls/msvcrt/tests/string.c +++ b/dlls/msvcrt/tests/string.c @@ -183,7 +183,7 @@ static void test_mbcp(void) int curr_mbcp = _getmbcp(); unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */ unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */ - unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2"; + unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3"; unsigned char buf[16]; int step; @@ -205,6 +205,40 @@ static void test_mbcp(void) ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n"); ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n"); + /* _ismbslead */ + expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d"); + expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d"); + + expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d"); + expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d"); + expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d"); + expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d"); + + /* _ismbstrail */ + expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d"); + + expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d"); + expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d"); + expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d"); + /* _mbsnextc */ expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x"); expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */