Skip to content

Commit

Permalink
libfreerdp-utils: adapt freerdp_uniconv_out to WinPR API
Browse files Browse the repository at this point in the history
  • Loading branch information
awakecoding committed Sep 23, 2012
1 parent a11a69c commit 9ddcd4f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 116 deletions.
6 changes: 5 additions & 1 deletion include/freerdp/utils/unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ FREERDP_API UNICONV* freerdp_uniconv_new();
FREERDP_API void freerdp_uniconv_free(UNICONV *uniconv);
FREERDP_API char* freerdp_uniconv_in(UNICONV *uniconv, unsigned char* pin, size_t in_len);
FREERDP_API char* freerdp_uniconv_out(UNICONV *uniconv, const char *str, size_t *pout_len);
FREERDP_API void freerdp_uniconv_uppercase(UNICONV *uniconv, char *wstr, int length);

#include <freerdp/types.h>

FREERDP_API WCHAR* freerdp_AsciiToUnicode(const char* str, int* length);
FREERDP_API CHAR* freerdp_UnicodeToAscii(const WCHAR* wstr, int* length);

#endif /* __UNICODE_UTILS_H */
3 changes: 3 additions & 0 deletions libfreerdp/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ if(WITH_MONOLITHIC_BUILD)
set(FREERDP_UTILS_LIBS ${FREERDP_UTILS_LIBS} winpr)
set(FREERDP_LIBS ${FREERDP_LIBS} ${FREERDP_UTILS_LIBS} PARENT_SCOPE)
else()
if(NOT WIN32)
set(FREERDP_UTILS_LIBS ${FREERDP_UTILS_LIBS} winpr-crt)
endif()
set(FREERDP_UTILS_LIBS ${FREERDP_UTILS_LIBS} winpr-synch)
target_link_libraries(freerdp-utils ${FREERDP_UTILS_LIBS})
install(TARGETS freerdp-utils DESTINATION ${CMAKE_INSTALL_LIBDIR})
Expand Down
162 changes: 47 additions & 115 deletions libfreerdp/utils/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <freerdp/utils/unicode.h>

#include <winpr/crt.h>

/* Convert pin/in_len from WINDOWS_CODEPAGE - return like xstrdup, 0-terminated */

char* freerdp_uniconv_in(UNICONV* uniconv, unsigned char* pin, size_t in_len)
Expand Down Expand Up @@ -99,121 +101,6 @@ char* freerdp_uniconv_in(UNICONV* uniconv, unsigned char* pin, size_t in_len)
return pout;
}

/* Convert str from DEFAULT_CODEPAGE to WINDOWS_CODEPAGE and return buffer like xstrdup.
* Buffer is 0-terminated but that is not included in the returned length. */

char* freerdp_uniconv_out(UNICONV* uniconv, const char *str, size_t* pout_len)
{
size_t ibl;
size_t obl;
char* pin;
char* pout;
char* pout0;

if (str == NULL)
{
*pout_len = 0;
return NULL;
}

ibl = strlen(str);
obl = 2 * ibl;
pin = (char*) str;
pout0 = xmalloc(obl + 2);
pout = pout0;

#ifdef HAVE_ICONV
if (iconv(uniconv->out_iconv_h, (ICONV_CONST char **) &pin, &ibl, &pout, &obl) == (size_t) - 1)
{
printf("freerdp_uniconv_out: iconv() error\n");
return NULL;
}
#else
while ((ibl > 0) && (obl > 0))
{
unsigned int wc;

wc = (unsigned int)(unsigned char)(*pin++);
ibl--;
if (wc >= 0xF0)
{
wc = (wc - 0xF0) << 18;
wc += ((unsigned int)(unsigned char)(*pin++) - 0x80) << 12;
wc += ((unsigned int)(unsigned char)(*pin++) - 0x80) << 6;
wc += ((unsigned int)(unsigned char)(*pin++) - 0x80);
ibl -= 3;
}
else if (wc >= 0xE0)
{
wc = (wc - 0xE0) << 12;
wc += ((unsigned int)(unsigned char)(*pin++) - 0x80) << 6;
wc += ((unsigned int)(unsigned char)(*pin++) - 0x80);
ibl -= 2;
}
else if (wc >= 0xC0)
{
wc = (wc - 0xC0) << 6;
wc += ((unsigned int)(unsigned char)(*pin++) - 0x80);
ibl -= 1;
}

if (wc <= 0xFFFF)
{
*pout++ = (char)(wc & 0xFF);
*pout++ = (char)(wc >> 8);
obl -= 2;
}
else
{
wc -= 0x10000;
*pout++ = (char)((wc >> 10) & 0xFF);
*pout++ = (char)((wc >> 18) + 0xD8);
*pout++ = (char)(wc & 0xFF);
*pout++ = (char)(((wc >> 8) & 0x03) + 0xDC);
obl -= 4;
}
}
#endif

if (ibl > 0)
{
printf("freerdp_uniconv_out: string not fully converted - %d chars left\n", (int) ibl);
}

*pout_len = pout - pout0;
*pout++ = 0; /* Add extra double zero termination */
*pout = 0;

return pout0;
}

/* Uppercase a unicode string */

void freerdp_uniconv_uppercase(UNICONV *uniconv, char *wstr, int length)
{
int i;
unsigned char* p;
unsigned int wc, uwc;

p = (unsigned char*) wstr;

for (i = 0; i < length; i++)
{
wc = (unsigned int)(*p);
wc += (unsigned int)(*(p + 1)) << 8;

uwc = towupper(wc);

if (uwc != wc)
{
*p = uwc & 0xFF;
*(p + 1) = (uwc >> 8) & 0xFF;
}

p += 2;
}
}

UNICONV* freerdp_uniconv_new()
{
UNICONV *uniconv = xnew(UNICONV);
Expand Down Expand Up @@ -246,3 +133,48 @@ void freerdp_uniconv_free(UNICONV *uniconv)
xfree(uniconv);
}
}

char* freerdp_uniconv_out(UNICONV* uniconv, const char *str, size_t* pout_len)
{
WCHAR* wstr;
int length;

wstr = freerdp_AsciiToUnicode(str, &length);
*pout_len = (size_t) length;

return (char*) wstr;
}

WCHAR* freerdp_AsciiToUnicode(const char* str, int* length)
{
WCHAR* wstr;

if (!str)
{
*length = 0;
return NULL;
}

*length = MultiByteToWideChar(CP_UTF8, 0, str, strlen(str), NULL, 0);
wstr = (WCHAR*) malloc((*length + 1) * sizeof(WCHAR));

MultiByteToWideChar(CP_UTF8, 0, str, *length, (LPWSTR) wstr, *length * sizeof(WCHAR));
wstr[*length] = 0;

*length *= 2;

return wstr;
}

CHAR* freerdp_UnicodeToAscii(const WCHAR* wstr, int* length)
{
CHAR* str;

*length = WideCharToMultiByte(CP_UTF8, 0, wstr, lstrlenW(wstr), NULL, 0, NULL, NULL);
str = (char*) malloc(*length + 1);

WideCharToMultiByte(CP_UTF8, 0, wstr, *length, str, *length, NULL, NULL);
str[*length] = '\0';

return str;
}

0 comments on commit 9ddcd4f

Please sign in to comment.