[winpr, crt] Fix wcs*cmp and wcs*len checks

(cherry picked from commit b60fac1a0470fe83e8d0b448f0fd7e9e6d6a0f96)
This commit is contained in:
akallabeth 2022-11-10 14:21:22 +01:00 committed by akallabeth
parent 5101fbbba3
commit 3ac3a2ce9e

View File

@ -26,6 +26,7 @@
#include <wctype.h>
#include <winpr/crt.h>
#include <winpr/assert.h>
#include <winpr/endian.h>
/* String Manipulation (CRT): http://msdn.microsoft.com/en-us/library/f0151s4x.aspx */
@ -97,21 +98,28 @@ int _strnicmp(const char* string1, const char* string2, size_t count)
int _wcscmp(const WCHAR* string1, const WCHAR* string2)
{
WCHAR value1, value2;
WINPR_ASSERT(string1);
WINPR_ASSERT(string2);
while (*string1 && (*string1 == *string2))
while (TRUE)
{
string1++;
string2++;
const WCHAR w1 = *string1++;
const WCHAR w2 = *string2++;
if (w1 != w2)
return (int)w1 - w2;
else if ((w1 == '\0') || (w2 == '\0'))
return (int)w1 - w2;
}
Data_Read_UINT16(string1, value1);
Data_Read_UINT16(string2, value2);
return (int)value1 - value2;
return 0;
}
int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count)
{
WINPR_ASSERT(string1);
WINPR_ASSERT(string2);
for (size_t x = 0; x < count; x++)
{
const WCHAR a = string1[x];
@ -119,6 +127,8 @@ int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count)
if (a != b)
return (int)a - b;
else if ((a == '\0') || (b == '\0'))
return (int)a - b;
}
return 0;
}
@ -129,8 +139,7 @@ size_t _wcslen(const WCHAR* str)
{
const WCHAR* p = (const WCHAR*)str;
if (!p)
return 0;
WINPR_ASSERT(p);
while (*p)
p++;
@ -144,8 +153,7 @@ size_t _wcsnlen(const WCHAR* str, size_t max)
{
size_t x;
if (!str)
return 0;
WINPR_ASSERT(str);
for (x = 0; x < max; x++)
{