Fixed string encoding (at least for Win)

This commit is contained in:
shermp
2019-04-10 23:41:58 +12:00
parent 9db04085b4
commit 4899af9e7b

View File

@@ -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) {