Blame winpr/libwinpr/crt/test/TestUnicodeConversion.c

Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/error.h>
Packit 1fb8d4
#include <winpr/windows.h>
Packit 1fb8d4
Packit 1fb8d4
/* Letters */
Packit 1fb8d4
Packit 1fb8d4
static BYTE c_cedilla_UTF8[] = "\xC3\xA7\x00";
Packit 1fb8d4
static BYTE c_cedilla_UTF16[] = "\xE7\x00\x00\x00";
Packit 1fb8d4
static int c_cedilla_cchWideChar = 2;
Packit 1fb8d4
static int c_cedilla_cbMultiByte = 3;
Packit 1fb8d4
Packit 1fb8d4
/* English */
Packit 1fb8d4
Packit 1fb8d4
static BYTE en_Hello_UTF8[] = "Hello\0";
Packit 1fb8d4
static BYTE en_Hello_UTF16[] = "\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00\x00\x00";
Packit 1fb8d4
static int en_Hello_cchWideChar = 6;
Packit 1fb8d4
static int en_Hello_cbMultiByte = 6;
Packit 1fb8d4
Packit 1fb8d4
static BYTE en_HowAreYou_UTF8[] = "How are you?\0";
Packit Service 5a9772
static BYTE en_HowAreYou_UTF16[] =
Packit Service 5a9772
    "\x48\x00\x6F\x00\x77\x00\x20\x00\x61\x00\x72\x00\x65\x00\x20\x00"
Packit Service 5a9772
    "\x79\x00\x6F\x00\x75\x00\x3F\x00\x00\x00";
Packit 1fb8d4
static int en_HowAreYou_cchWideChar = 13;
Packit 1fb8d4
static int en_HowAreYou_cbMultiByte = 13;
Packit 1fb8d4
Packit 1fb8d4
/* French */
Packit 1fb8d4
Packit 1fb8d4
static BYTE fr_Hello_UTF8[] = "Allo\0";
Packit 1fb8d4
static BYTE fr_Hello_UTF16[] = "\x41\x00\x6C\x00\x6C\x00\x6F\x00\x00\x00";
Packit 1fb8d4
static int fr_Hello_cchWideChar = 5;
Packit 1fb8d4
static int fr_Hello_cbMultiByte = 5;
Packit 1fb8d4
Packit Service 5a9772
static BYTE fr_HowAreYou_UTF8[] =
Packit Service 5a9772
    "\x43\x6F\x6D\x6D\x65\x6E\x74\x20\xC3\xA7\x61\x20\x76\x61\x3F\x00";
Packit Service 5a9772
static BYTE fr_HowAreYou_UTF16[] =
Packit Service 5a9772
    "\x43\x00\x6F\x00\x6D\x00\x6D\x00\x65\x00\x6E\x00\x74\x00\x20\x00"
Packit Service 5a9772
    "\xE7\x00\x61\x00\x20\x00\x76\x00\x61\x00\x3F\x00\x00\x00";
Packit 1fb8d4
static int fr_HowAreYou_cchWideChar = 15;
Packit 1fb8d4
static int fr_HowAreYou_cbMultiByte = 16;
Packit 1fb8d4
Packit 1fb8d4
/* Russian */
Packit 1fb8d4
Packit 1fb8d4
static BYTE ru_Hello_UTF8[] = "\xD0\x97\xD0\xB4\xD0\xBE\xD1\x80\xD0\xBE\xD0\xB2\xD0\xBE\x00";
Packit 1fb8d4
static BYTE ru_Hello_UTF16[] = "\x17\x04\x34\x04\x3E\x04\x40\x04\x3E\x04\x32\x04\x3E\x04\x00\x00";
Packit 1fb8d4
static int ru_Hello_cchWideChar = 8;
Packit 1fb8d4
static int ru_Hello_cbMultiByte = 15;
Packit 1fb8d4
Packit Service 5a9772
static BYTE ru_HowAreYou_UTF8[] =
Packit Service 5a9772
    "\xD0\x9A\xD0\xB0\xD0\xBA\x20\xD0\xB4\xD0\xB5\xD0\xBB\xD0\xB0\x3F\x00";
Packit Service 5a9772
static BYTE ru_HowAreYou_UTF16[] =
Packit Service 5a9772
    "\x1A\x04\x30\x04\x3A\x04\x20\x00\x34\x04\x35\x04\x3B\x04\x30\x04"
Packit Service 5a9772
    "\x3F\x00\x00\x00";
Packit 1fb8d4
static int ru_HowAreYou_cchWideChar = 10;
Packit 1fb8d4
static int ru_HowAreYou_cbMultiByte = 17;
Packit 1fb8d4
Packit 1fb8d4
/* Arabic */
Packit 1fb8d4
Packit 1fb8d4
static BYTE ar_Hello_UTF8[] = "\xD8\xA7\xD9\x84\xD8\xB3\xD9\x84\xD8\xA7\xD9\x85\x20\xD8\xB9\xD9"
Packit Service 5a9772
                              "\x84\xD9\x8A\xD9\x83\xD9\x85\x00";
Packit 1fb8d4
static BYTE ar_Hello_UTF16[] = "\x27\x06\x44\x06\x33\x06\x44\x06\x27\x06\x45\x06\x20\x00\x39\x06"
Packit Service 5a9772
                               "\x44\x06\x4A\x06\x43\x06\x45\x06\x00\x00";
Packit 1fb8d4
static int ar_Hello_cchWideChar = 13;
Packit 1fb8d4
static int ar_Hello_cbMultiByte = 24;
Packit 1fb8d4
Packit 1fb8d4
static BYTE ar_HowAreYou_UTF8[] = "\xD9\x83\xD9\x8A\xD9\x81\x20\xD8\xAD\xD8\xA7\xD9\x84\xD9\x83\xD8"
Packit Service 5a9772
                                  "\x9F\x00";
Packit Service 5a9772
static BYTE ar_HowAreYou_UTF16[] =
Packit Service 5a9772
    "\x43\x06\x4A\x06\x41\x06\x20\x00\x2D\x06\x27\x06\x44\x06\x43\x06"
Packit Service 5a9772
    "\x1F\x06\x00\x00";
Packit 1fb8d4
static int ar_HowAreYou_cchWideChar = 10;
Packit 1fb8d4
static int ar_HowAreYou_cbMultiByte = 18;
Packit 1fb8d4
Packit 1fb8d4
/* Chinese */
Packit 1fb8d4
Packit 1fb8d4
static BYTE ch_Hello_UTF8[] = "\xE4\xBD\xA0\xE5\xA5\xBD\x00";
Packit 1fb8d4
static BYTE ch_Hello_UTF16[] = "\x60\x4F\x7D\x59\x00\x00";
Packit 1fb8d4
static int ch_Hello_cchWideChar = 3;
Packit 1fb8d4
static int ch_Hello_cbMultiByte = 7;
Packit 1fb8d4
Packit 1fb8d4
static BYTE ch_HowAreYou_UTF8[] = "\xE4\xBD\xA0\xE5\xA5\xBD\xE5\x90\x97\x00";
Packit 1fb8d4
static BYTE ch_HowAreYou_UTF16[] = "\x60\x4F\x7D\x59\x17\x54\x00\x00";
Packit 1fb8d4
static int ch_HowAreYou_cchWideChar = 4;
Packit 1fb8d4
static int ch_HowAreYou_cbMultiByte = 10;
Packit 1fb8d4
Packit 1fb8d4
/* Uppercasing */
Packit 1fb8d4
Packit Service 5a9772
static BYTE ru_Administrator_lower[] = "\xd0\x90\xd0\xb4\xd0\xbc\xd0\xb8\xd0\xbd\xd0\xb8\xd1\x81"
Packit Service 5a9772
                                       "\xd1\x82\xd1\x80\xd0\xb0\xd1\x82\xd0\xbe\xd1\x80\x00";
Packit 1fb8d4
Packit Service 5a9772
static BYTE ru_Administrator_upper[] = "\xd0\x90\xd0\x94\xd0\x9c\xd0\x98\xd0\x9d\xd0\x98\xd0\xa1"
Packit Service 5a9772
                                       "\xd0\xa2\xd0\xa0\xd0\x90\xd0\xa2\xd0\x9e\xd0\xa0\x00";
Packit 1fb8d4
Packit Service 5a9772
static void string_hexdump(const BYTE* data, size_t length)
Packit 1fb8d4
{
Packit Service 5a9772
	const BYTE* p = data;
Packit Service 5a9772
	size_t i, line, offset = 0;
Packit 1fb8d4
Packit 1fb8d4
	while (offset < length)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("%04" PRIxz " ", offset);
Packit 1fb8d4
Packit 1fb8d4
		line = length - offset;
Packit 1fb8d4
Packit 1fb8d4
		if (line > 16)
Packit 1fb8d4
			line = 16;
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i < line; i++)
Packit Service 5a9772
			printf("%02" PRIx8 " ", p[i]);
Packit 1fb8d4
Packit 1fb8d4
		for (; i < 16; i++)
Packit 1fb8d4
			printf("   ");
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i < line; i++)
Packit Service 5a9772
			printf("%c", (p[i] >= 0x20 && p[i] < 0x7F) ? (char)p[i] : '.');
Packit 1fb8d4
Packit 1fb8d4
		printf("\n");
Packit 1fb8d4
Packit 1fb8d4
		offset += line;
Packit 1fb8d4
		p += line;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static int convert_utf8_to_utf16(BYTE* lpMultiByteStr, BYTE* expected_lpWideCharStr,
Packit Service 5a9772
                                 int expected_cchWideChar)
Packit 1fb8d4
{
Packit Service 5a9772
	int rc = -1;
Packit 1fb8d4
	int length;
Packit Service 5a9772
	size_t cbMultiByte;
Packit 1fb8d4
	int cchWideChar;
Packit Service 5a9772
	LPWSTR lpWideCharStr = NULL;
Packit 1fb8d4
Packit Service 5a9772
	cbMultiByte = strlen((char*)lpMultiByteStr);
Packit Service 5a9772
	cchWideChar = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)lpMultiByteStr, -1, NULL, 0);
Packit 1fb8d4
Packit 1fb8d4
	printf("MultiByteToWideChar Input UTF8 String:\n");
Packit 1fb8d4
	string_hexdump(lpMultiByteStr, cbMultiByte + 1);
Packit 1fb8d4
Packit 1fb8d4
	printf("MultiByteToWideChar required cchWideChar: %d\n", cchWideChar);
Packit 1fb8d4
Packit 1fb8d4
	if (cchWideChar != expected_cchWideChar)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("MultiByteToWideChar unexpected cchWideChar: actual: %d expected: %d\n", cchWideChar,
Packit Service 5a9772
		       expected_cchWideChar);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	lpWideCharStr = (LPWSTR)calloc((size_t)cchWideChar, sizeof(WCHAR));
Packit 1fb8d4
	if (!lpWideCharStr)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("MultiByteToWideChar: unable to allocate memory for test\n");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit Service 5a9772
	lpWideCharStr[cchWideChar - 1] =
Packit Service 5a9772
	    0xFFFF; /* should be overwritten if null terminator is inserted properly */
Packit Service 5a9772
	length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)lpMultiByteStr, cbMultiByte + 1, lpWideCharStr,
Packit Service 5a9772
	                             cchWideChar);
Packit 1fb8d4
Packit 1fb8d4
	printf("MultiByteToWideChar converted length (WCHAR): %d\n", length);
Packit 1fb8d4
Packit 1fb8d4
	if (!length)
Packit 1fb8d4
	{
Packit 1fb8d4
		DWORD error = GetLastError();
Packit Service 5a9772
		printf("MultiByteToWideChar error: 0x%08" PRIX32 "\n", error);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (length != expected_cchWideChar)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("MultiByteToWideChar unexpected converted length (WCHAR): actual: %d expected: %d\n",
Packit Service 5a9772
		       length, expected_cchWideChar);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (_wcscmp(lpWideCharStr, (WCHAR*)expected_lpWideCharStr) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("MultiByteToWideChar unexpected string:\n");
Packit 1fb8d4
Packit 1fb8d4
		printf("UTF8 String:\n");
Packit 1fb8d4
		string_hexdump(lpMultiByteStr, cbMultiByte + 1);
Packit 1fb8d4
Packit 1fb8d4
		printf("UTF16 String (actual):\n");
Packit Service 5a9772
		string_hexdump((BYTE*)lpWideCharStr, length * sizeof(WCHAR));
Packit 1fb8d4
Packit 1fb8d4
		printf("UTF16 String (expected):\n");
Packit Service 5a9772
		string_hexdump((BYTE*)expected_lpWideCharStr, expected_cchWideChar * sizeof(WCHAR));
Packit 1fb8d4
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	printf("MultiByteToWideChar Output UTF16 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)lpWideCharStr, length * sizeof(WCHAR));
Packit 1fb8d4
	printf("\n");
Packit 1fb8d4
Packit Service 5a9772
	rc = length;
Packit Service 5a9772
fail:
Packit 1fb8d4
	free(lpWideCharStr);
Packit 1fb8d4
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static int convert_utf16_to_utf8(BYTE* lpWideCharStr, BYTE* expected_lpMultiByteStr,
Packit Service 5a9772
                                 int expected_cbMultiByte)
Packit 1fb8d4
{
Packit Service 5a9772
	int rc = -1;
Packit 1fb8d4
	int length;
Packit 1fb8d4
	int cchWideChar;
Packit 1fb8d4
	int cbMultiByte;
Packit 1fb8d4
	LPSTR lpMultiByteStr = NULL;
Packit 1fb8d4
Packit Service 5a9772
	cchWideChar = _wcslen((WCHAR*)lpWideCharStr);
Packit Service 5a9772
	cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)lpWideCharStr, -1, NULL, 0, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	printf("WideCharToMultiByte Input UTF16 String:\n");
Packit 1fb8d4
	string_hexdump(lpWideCharStr, (cchWideChar + 1) * sizeof(WCHAR));
Packit 1fb8d4
Packit 1fb8d4
	printf("WideCharToMultiByte required cbMultiByte: %d\n", cbMultiByte);
Packit 1fb8d4
Packit 1fb8d4
	if (cbMultiByte != expected_cbMultiByte)
Packit 1fb8d4
	{
Packit Service 5a9772
		printf("WideCharToMultiByte unexpected cbMultiByte: actual: %d expected: %d\n", cbMultiByte,
Packit Service 5a9772
		       expected_cbMultiByte);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	lpMultiByteStr = (LPSTR)malloc(cbMultiByte);
Packit 1fb8d4
	if (!lpMultiByteStr)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("WideCharToMultiByte: unable to allocate memory for test\n");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit Service 5a9772
	lpMultiByteStr[cbMultiByte - 1] =
Packit Service 5a9772
	    (CHAR)0xFF; /* should be overwritten if null terminator is inserted properly */
Packit Service 5a9772
	length = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)lpWideCharStr, cchWideChar + 1,
Packit Service 5a9772
	                             lpMultiByteStr, cbMultiByte, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	printf("WideCharToMultiByte converted length (BYTE): %d\n", length);
Packit 1fb8d4
Packit 1fb8d4
	if (!length)
Packit 1fb8d4
	{
Packit 1fb8d4
		DWORD error = GetLastError();
Packit Service 5a9772
		printf("WideCharToMultiByte error: 0x%08" PRIX32 "\n", error);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (length != expected_cbMultiByte)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("WideCharToMultiByte unexpected converted length (BYTE): actual: %d expected: %d\n",
Packit Service 5a9772
		       length, expected_cbMultiByte);
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (strcmp(lpMultiByteStr, (char*)expected_lpMultiByteStr) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("WideCharToMultiByte unexpected string:\n");
Packit 1fb8d4
Packit 1fb8d4
		printf("UTF16 String:\n");
Packit Service 5a9772
		string_hexdump((BYTE*)lpWideCharStr, (cchWideChar + 1) * sizeof(WCHAR));
Packit 1fb8d4
Packit 1fb8d4
		printf("UTF8 String (actual):\n");
Packit Service 5a9772
		string_hexdump((BYTE*)lpMultiByteStr, cbMultiByte);
Packit 1fb8d4
Packit 1fb8d4
		printf("UTF8 String (expected):\n");
Packit Service 5a9772
		string_hexdump((BYTE*)expected_lpMultiByteStr, expected_cbMultiByte);
Packit 1fb8d4
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	printf("WideCharToMultiByte Output UTF8 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)lpMultiByteStr, cbMultiByte);
Packit 1fb8d4
	printf("\n");
Packit 1fb8d4
Packit Service 5a9772
	rc = length;
Packit Service 5a9772
fail:
Packit 1fb8d4
	free(lpMultiByteStr);
Packit 1fb8d4
Packit Service 5a9772
	return rc;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL test_unicode_uppercasing(BYTE* lower, BYTE* upper)
Packit 1fb8d4
{
Packit 1fb8d4
	WCHAR* lowerW = NULL;
Packit 1fb8d4
	int lowerLength;
Packit 1fb8d4
	WCHAR* upperW = NULL;
Packit 1fb8d4
	int upperLength;
Packit 1fb8d4
Packit Service 5a9772
	lowerLength = ConvertToUnicode(CP_UTF8, 0, (LPSTR)lower, -1, &lowerW, 0);
Packit Service 5a9772
	upperLength = ConvertToUnicode(CP_UTF8, 0, (LPSTR)upper, -1, &upperW, 0);
Packit 1fb8d4
Packit 1fb8d4
	CharUpperBuffW(lowerW, lowerLength);
Packit 1fb8d4
Packit 1fb8d4
	if (_wcscmp(lowerW, upperW) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("Lowercase String:\n");
Packit Service 5a9772
		string_hexdump((BYTE*)lowerW, lowerLength * 2);
Packit 1fb8d4
Packit 1fb8d4
		printf("Uppercase String:\n");
Packit Service 5a9772
		string_hexdump((BYTE*)upperW, upperLength * 2);
Packit 1fb8d4
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(lowerW);
Packit 1fb8d4
	free(upperW);
Packit 1fb8d4
Packit 1fb8d4
	printf("success\n\n");
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL test_ConvertFromUnicode_wrapper(void)
Packit 1fb8d4
{
Packit Service 5a9772
	const BYTE src1[] =
Packit Service 5a9772
	    "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00"
Packit Service 5a9772
	    "\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x40\x00\x40\x00\x40\x00";
Packit Service 5a9772
	const BYTE src2[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00"
Packit Service 5a9772
	                    "\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x00\x00";
Packit 1fb8d4
	/*               00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18 */
Packit Service 5a9772
	const CHAR cmp0[] = { 'R', 'I', 'C', 'H', ' ', 'T', 'E', 'X', 'T',
Packit Service 5a9772
		                  ' ', 'F', 'O', 'R', 'M', 'A', 'T', 0 };
Packit 1fb8d4
	CHAR* dst = NULL;
Packit 1fb8d4
	int i;
Packit 1fb8d4
Packit 1fb8d4
	/* Test unterminated unicode string:
Packit 1fb8d4
	 * ConvertFromUnicode must always null-terminate, even if the src string isn't
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit 1fb8d4
	printf("Input UTF16 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)src1, 19 * sizeof(WCHAR));
Packit 1fb8d4
Packit 1fb8d4
	i = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*)src1, 16, &dst, 0, NULL, NULL);
Packit 1fb8d4
	if (i != 16)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr, "ConvertFromUnicode failure A1: unexpectedly returned %d instead of 16\n",
Packit Service 5a9772
		        i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if (dst == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertFromUnicode failure A2: destination ist NULL\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if ((i = strlen(dst)) != 16)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertFromUnicode failure A3: dst length is %d instead of 16\n", i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if (strcmp(dst, cmp0))
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertFromUnicode failure A4: data mismatch\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	printf("Output UTF8 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)dst, i + 1);
Packit 1fb8d4
Packit 1fb8d4
	free(dst);
Packit 1fb8d4
	dst = NULL;
Packit 1fb8d4
Packit 1fb8d4
	/* Test null-terminated string */
Packit 1fb8d4
Packit 1fb8d4
	printf("Input UTF16 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)src2, (_wcslen((WCHAR*)src2) + 1) * sizeof(WCHAR));
Packit 1fb8d4
Packit 1fb8d4
	i = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*)src2, -1, &dst, 0, NULL, NULL);
Packit 1fb8d4
	if (i != 17)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr, "ConvertFromUnicode failure B1: unexpectedly returned %d instead of 17\n",
Packit Service 5a9772
		        i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if (dst == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertFromUnicode failure B2: destination ist NULL\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if ((i = strlen(dst)) != 16)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertFromUnicode failure B3: dst length is %d instead of 16\n", i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if (strcmp(dst, cmp0))
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertFromUnicode failure B: data mismatch\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	printf("Output UTF8 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)dst, i + 1);
Packit 1fb8d4
Packit 1fb8d4
	free(dst);
Packit 1fb8d4
	dst = NULL;
Packit 1fb8d4
Packit 1fb8d4
	printf("success\n\n");
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(dst);
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL test_ConvertToUnicode_wrapper(void)
Packit 1fb8d4
{
Packit 1fb8d4
	/*               00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18 */
Packit Service 5a9772
	const CHAR src1[] = { 'R', 'I', 'C', 'H', ' ', 'T', 'E', 'X', 'T', ' ',
Packit Service 5a9772
		                  'F', 'O', 'R', 'M', 'A', 'T', '@', '@', '@' };
Packit Service 5a9772
	const CHAR src2[] = { 'R', 'I', 'C', 'H', ' ', 'T', 'E', 'X', 'T',
Packit Service 5a9772
		                  ' ', 'F', 'O', 'R', 'M', 'A', 'T', 0 };
Packit Service 5a9772
	const BYTE cmp0[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00"
Packit Service 5a9772
	                    "\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x00\x00";
Packit 1fb8d4
	WCHAR* dst = NULL;
Packit Service 5a9772
	int ii;
Packit Service 5a9772
	size_t i;
Packit 1fb8d4
Packit 1fb8d4
	/* Test unterminated unicode string:
Packit 1fb8d4
	 * ConvertToUnicode must always null-terminate, even if the src string isn't
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit 1fb8d4
	printf("Input UTF8 String:\n");
Packit Service 5a9772
	string_hexdump((const BYTE*)src1, 19);
Packit 1fb8d4
Packit Service 5a9772
	ii = ConvertToUnicode(CP_UTF8, 0, src1, 16, &dst, 0);
Packit Service 5a9772
	if (ii != 16)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr, "ConvertToUnicode failure A1: unexpectedly returned %d instead of 16\n",
Packit Service 5a9772
		        ii);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit Service 5a9772
	i = (size_t)ii;
Packit 1fb8d4
	if (dst == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertToUnicode failure A2: destination ist NULL\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if ((i = _wcslen(dst)) != 16)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr, "ConvertToUnicode failure A3: dst length is %" PRIuz " instead of 16\n", i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit Service 5a9772
	if (_wcscmp(dst, (const WCHAR*)cmp0))
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertToUnicode failure A4: data mismatch\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	printf("Output UTF16 String:\n");
Packit Service 5a9772
	string_hexdump((const BYTE*)dst, (i + 1) * sizeof(WCHAR));
Packit 1fb8d4
Packit 1fb8d4
	free(dst);
Packit 1fb8d4
	dst = NULL;
Packit 1fb8d4
Packit 1fb8d4
	/* Test null-terminated string */
Packit 1fb8d4
Packit 1fb8d4
	printf("Input UTF8 String:\n");
Packit Service 5a9772
	string_hexdump((const BYTE*)src2, strlen(src2) + 1);
Packit 1fb8d4
Packit 1fb8d4
	i = ConvertToUnicode(CP_UTF8, 0, src2, -1, &dst, 0);
Packit 1fb8d4
	if (i != 17)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr,
Packit Service 5a9772
		        "ConvertToUnicode failure B1: unexpectedly returned %" PRIuz " instead of 17\n", i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if (dst == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertToUnicode failure B2: destination ist NULL\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if ((i = _wcslen(dst)) != 16)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr, "ConvertToUnicode failure B3: dst length is %" PRIuz " instead of 16\n", i);
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	if (_wcscmp(dst, (WCHAR*)cmp0))
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "ConvertToUnicode failure B: data mismatch\n");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	printf("Output UTF16 String:\n");
Packit Service 5a9772
	string_hexdump((BYTE*)dst, (i + 1) * 2);
Packit 1fb8d4
Packit 1fb8d4
	free(dst);
Packit 1fb8d4
	dst = NULL;
Packit 1fb8d4
Packit 1fb8d4
	printf("success\n\n");
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(dst);
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int TestUnicodeConversion(int argc, char* argv[])
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(argc);
Packit Service 5a9772
	WINPR_UNUSED(argv);
Packit Service 5a9772
Packit 1fb8d4
	/* Letters */
Packit 1fb8d4
Packit 1fb8d4
	printf("Letters\n");
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf8_to_utf16(c_cedilla_UTF8, c_cedilla_UTF16, c_cedilla_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf16_to_utf8(c_cedilla_UTF16, c_cedilla_UTF8, c_cedilla_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* English */
Packit 1fb8d4
Packit 1fb8d4
	printf("English\n");
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf8_to_utf16(en_Hello_UTF8, en_Hello_UTF16, en_Hello_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf8_to_utf16(en_HowAreYou_UTF8, en_HowAreYou_UTF16, en_HowAreYou_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf16_to_utf8(en_Hello_UTF16, en_Hello_UTF8, en_Hello_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf16_to_utf8(en_HowAreYou_UTF16, en_HowAreYou_UTF8, en_HowAreYou_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* French */
Packit 1fb8d4
Packit 1fb8d4
	printf("French\n");
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf8_to_utf16(fr_Hello_UTF8, fr_Hello_UTF16, fr_Hello_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf8_to_utf16(fr_HowAreYou_UTF8, fr_HowAreYou_UTF16, fr_HowAreYou_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf16_to_utf8(fr_Hello_UTF16, fr_Hello_UTF8, fr_Hello_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf16_to_utf8(fr_HowAreYou_UTF16, fr_HowAreYou_UTF8, fr_HowAreYou_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* Russian */
Packit 1fb8d4
Packit 1fb8d4
	printf("Russian\n");
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf8_to_utf16(ru_Hello_UTF8, ru_Hello_UTF16, ru_Hello_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf8_to_utf16(ru_HowAreYou_UTF8, ru_HowAreYou_UTF16, ru_HowAreYou_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf16_to_utf8(ru_Hello_UTF16, ru_Hello_UTF8, ru_Hello_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf16_to_utf8(ru_HowAreYou_UTF16, ru_HowAreYou_UTF8, ru_HowAreYou_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* Arabic */
Packit 1fb8d4
Packit 1fb8d4
	printf("Arabic\n");
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf8_to_utf16(ar_Hello_UTF8, ar_Hello_UTF16, ar_Hello_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf8_to_utf16(ar_HowAreYou_UTF8, ar_HowAreYou_UTF16, ar_HowAreYou_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf16_to_utf8(ar_Hello_UTF16, ar_Hello_UTF8, ar_Hello_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf16_to_utf8(ar_HowAreYou_UTF16, ar_HowAreYou_UTF8, ar_HowAreYou_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* Chinese */
Packit 1fb8d4
Packit 1fb8d4
	printf("Chinese\n");
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf8_to_utf16(ch_Hello_UTF8, ch_Hello_UTF16, ch_Hello_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf8_to_utf16(ch_HowAreYou_UTF8, ch_HowAreYou_UTF16, ch_HowAreYou_cchWideChar) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (convert_utf16_to_utf8(ch_Hello_UTF16, ch_Hello_UTF8, ch_Hello_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	if (convert_utf16_to_utf8(ch_HowAreYou_UTF16, ch_HowAreYou_UTF8, ch_HowAreYou_cbMultiByte) < 1)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* Uppercasing */
Packit 1fb8d4
Packit 1fb8d4
	printf("Uppercasing\n");
Packit 1fb8d4
Packit 1fb8d4
	if (!test_unicode_uppercasing(ru_Administrator_lower, ru_Administrator_upper))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* ConvertFromUnicode */
Packit 1fb8d4
Packit 1fb8d4
	printf("ConvertFromUnicode\n");
Packit 1fb8d4
Packit 1fb8d4
	if (!test_ConvertFromUnicode_wrapper())
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/* ConvertToUnicode */
Packit 1fb8d4
Packit 1fb8d4
	printf("ConvertToUnicode\n");
Packit 1fb8d4
Packit 1fb8d4
	if (!test_ConvertToUnicode_wrapper())
Packit 1fb8d4
		return -1;
Packit Service 5a9772
	/*
Packit Service 5a9772
Packit Service 5a9772
	    printf("----------------------------------------------------------\n\n");
Packit Service 5a9772
Packit Service 5a9772
	    if (0)
Packit Service 5a9772
	    {
Packit Service 5a9772
	        BYTE src[] = { 'R',0,'I',0,'C',0,'H',0,' ',0, 'T',0,'E',0,'X',0,'T',0,'
Packit Service 5a9772
	   ',0,'F',0,'O',0,'R',0,'M',0,'A',0,'T',0,'@',0,'@',0 };
Packit Service 5a9772
	        //BYTE src[] = { 'R',0,'I',0,'C',0,'H',0,' ',0,  0,0,  'T',0,'E',0,'X',0,'T',0,'
Packit Service 5a9772
	   ',0,'F',0,'O',0,'R',0,'M',0,'A',0,'T',0,'@',0,'@',0 };
Packit Service 5a9772
	        //BYTE src[] = { 0,0,'R',0,'I',0,'C',0,'H',0,' ',0, 'T',0,'E',0,'X',0,'T',0,'
Packit Service 5a9772
	   ',0,'F',0,'O',0,'R',0,'M',0,'A',0,'T',0,'@',0,'@',0 }; char* dst = NULL; int num; num =
Packit Service 5a9772
	   ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) src, 16, &dst, 0, NULL, NULL);
Packit Service 5a9772
	        printf("ConvertFromUnicode returned %d dst=[%s]\n", num, dst);
Packit Service 5a9772
	        string_hexdump((BYTE*)dst, num+1);
Packit Service 5a9772
	    }
Packit Service 5a9772
	    if (1)
Packit Service 5a9772
	    {
Packit Service 5a9772
	        char src[] = "RICH TEXT FORMAT@@@@@@";
Packit Service 5a9772
	        WCHAR *dst = NULL;
Packit Service 5a9772
	        int num;
Packit Service 5a9772
	        num = ConvertToUnicode(CP_UTF8, 0, src, 16, &dst, 0);
Packit Service 5a9772
	        printf("ConvertToUnicode returned %d dst=%p\n", num, (void*) dst);
Packit Service 5a9772
	        string_hexdump((BYTE*)dst, num * 2 + 2);
Packit Service 5a9772
Packit Service 5a9772
	    }
Packit Service 5a9772
	*/
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}