From 4899af9e7bbacd120abac1a64cbff0b2be3d8277 Mon Sep 17 00:00:00 2001 From: shermp <14854761+shermp@users.noreply.github.com> Date: Wed, 10 Apr 2019 23:41:58 +1200 Subject: [PATCH] Fixed string encoding (at least for Win) --- vhdutil.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/vhdutil.c b/vhdutil.c index 71f73a8..22040c2 100644 --- a/vhdutil.c +++ b/vhdutil.c @@ -22,15 +22,14 @@ int vhd_utf_convert(VHDUtfType toUTF, void* in_str, char** out_str) out_buff_len = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)in_str, -1, NULL, 0); out_type = sizeof(WCHAR); } - char *out = *out_str; - out = calloc(out_buff_len, out_type); - if (out == NULL) { + *out_str = calloc(out_buff_len, out_type); + if (*out_str == NULL) { return -1; } if (toUTF == VHD_UTF_8) { - WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)in_str, -1, (LPSTR)out, out_buff_len, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)in_str, -1, (LPSTR)*out_str, out_buff_len, NULL, NULL); } else { - MultiByteToWideChar(CP_UTF8, 0, (LPCCH)in_str, -1, (LPWSTR)out, out_buff_len); + MultiByteToWideChar(CP_UTF8, 0, (LPCCH)in_str, -1, (LPWSTR)*out_str, out_buff_len); } #else iconv_t cd = (toUTF == VHD_UTF_8) ? iconv_open("UTF-8", "UTF-16LE") : iconv_open("UTF-16LE", "UTF-8"); @@ -39,16 +38,16 @@ int vhd_utf_convert(VHDUtfType toUTF, void* in_str, char** out_str) char *in = (char*)in_str; size_t in_len = (toUTF == VHD_UTF_8) ? (vhd_u16_strlen(in) * sizeof(uint16_t)) : strlen(in); size_t out_len = (toUTF == VHD_UTF_8) ? ((in_len * 2) + 1) : ((in_len * 4) + 2); - char *out = *out_str; - out = calloc(out_len, sizeof *out); - if (out == NULL) { + *out_str = calloc(out_len, sizeof **out_str); + if (*out_str == NULL) { return -1; } - size_t res = iconv(cd, &in, &in_len, &out, &out_len); + size_t res = iconv(cd, &in, &in_len, &*out_str, &out_len); if (res == (size_t)-1) { return -1; } #endif + return 0; } size_t vhd_u16_strlen(uint16_t* u16_str) {