# HG changeset patch # User Andre Heinecke # Date 1396349315 0 # Node ID c596568fa45bd004ebab269d6d20cea047f4bb56 # Parent 1e112cf41e9299836bcae64c91bc3d5e63b3c450 Add utf16 conversion functions for Windows. This also includes a null pointer checking free. diff -r 1e112cf41e92 -r c596568fa45b common/strhelp.c --- a/common/strhelp.c Mon Mar 31 11:41:43 2014 +0200 +++ b/common/strhelp.c Tue Apr 01 10:48:35 2014 +0000 @@ -7,6 +7,10 @@ #include +#ifdef WIN32 +#include +#endif + /* Remarks regarding the "Flawfinder: ignore" comments in this file: * * - strlen: @@ -182,3 +186,65 @@ } return ret; } + +void +xfree (void *p) +{ + if (p) + free (p); +} + +#ifdef WIN32 +/* Adapted from GPGOL rev. e512053 */ +char * +wchar_to_utf8 (const wchar_t *string, size_t len) +{ + int n, ilen; + char *result; + + ilen = (int) len; + if (ilen < 0) + return NULL; + + /* Note, that CP_UTF8 is not defined in Windows versions earlier + than NT.*/ + n = WideCharToMultiByte (CP_UTF8, 0, string, ilen, NULL, 0, NULL, NULL); + if (n < 0) + return NULL; + + result = xmalloc ((size_t)n+1); + n = WideCharToMultiByte (CP_UTF8, 0, string, ilen, result, n, NULL, NULL); + if (n < 0) + { + xfree (result); + return NULL; + } + return result; +} + +/* Adapted from GPGOL rev. e512053 */ +wchar_t * +utf8_to_wchar (const char *string, size_t len) +{ + int n, ilen; + wchar_t *result; + + ilen = (int) len; + if (ilen < 0) + return NULL; + + n = MultiByteToWideChar (CP_UTF8, 0, string, ilen, NULL, 0); + if (n < 0 || n + 1 < 0) + return NULL; + + result = xmalloc ((size_t)(n+1) * sizeof *result); + n = MultiByteToWideChar (CP_UTF8, 0, string, ilen, result, n); + if (n < 0) + { + xfree (result); + return NULL; + } + result[n] = 0; + return result; +} +#endif diff -r 1e112cf41e92 -r c596568fa45b common/strhelp.h --- a/common/strhelp.h Mon Mar 31 11:41:43 2014 +0200 +++ b/common/strhelp.h Tue Apr 01 10:48:35 2014 +0000 @@ -6,6 +6,7 @@ #endif #include +#include /** * @file strhelp.h @@ -19,6 +20,7 @@ void *xrealloc( void *a, size_t n ); void *xcalloc( size_t n, size_t m ); char *xstrndup( const char *string, const size_t len ); +void xfree ( void *p ); /** * @brief Returns the length of the given %NULL-terminated @@ -101,6 +103,30 @@ */ int str_base64_decode(char **dst, size_t *dst_size, char *src, size_t src_size); + +#ifdef WIN32 + +/** @brief convert a utf8 string to utf16 wchar + * + * @param[in] string utf8 string. Must be at least len characters long. + * @param[in] len number of characters to be converted. + * + * @returns pointer to a newly allocated wchar array. NULL on error. + * + **/ +wchar_t *utf8_to_wchar (const char *string, size_t len); + +/** @brief convert a utf16 string to utf8 + * + * @param[in] string utf16 string. Must be at least len characters long. + * @param[in] len number of characters to be converted. + * + * @returns pointer to a newly allocated char array. NULL on error. + * + **/ +char *wchar_to_utf8 (const wchar_t *string, size_t len); +#endif + #ifdef __cplusplus } #endif