8240725: Some functions might not work with CJK character

Reviewed-by: naoto
This commit is contained in:
Yasumasa Suenaga 2020-03-11 13:14:40 +09:00
parent 80ca356e7e
commit 99b28daf56
4 changed files with 72 additions and 36 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -496,6 +496,38 @@ JLI_Snprintf(char* buffer, size_t size, const char* format, ...) {
return rc;
}
static errno_t convert_to_unicode(const char* path, const wchar_t* prefix, wchar_t** wpath) {
int unicode_path_len;
size_t prefix_len, wpath_len;
/*
* Get required buffer size to convert to Unicode.
* The return value includes the terminating null character.
*/
unicode_path_len = MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
path, -1, NULL, 0);
if (unicode_path_len == 0) {
return EINVAL;
}
prefix_len = wcslen(prefix);
wpath_len = prefix_len + unicode_path_len;
*wpath = (wchar_t*)JLI_MemAlloc(wpath_len * sizeof(wchar_t));
if (*wpath == NULL) {
return ENOMEM;
}
wcsncpy(*wpath, prefix, prefix_len);
if (MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
path, -1, &((*wpath)[prefix_len]), (int)wpath_len) == 0) {
JLI_MemFree(*wpath);
*wpath = NULL;
return EINVAL;
}
return ERROR_SUCCESS;
}
/* taken from hotspot and slightly adjusted for jli lib;
* creates a UNC/ELP path from input 'path'
* the return buffer is allocated in C heap and needs to be freed using
@ -508,30 +540,13 @@ static wchar_t* create_unc_path(const char* path, errno_t* err) {
if (path[0] == '\\' && path[1] == '\\') {
if (path[2] == '?' && path[3] == '\\') {
/* if it already has a \\?\ don't do the prefix */
wpath = (wchar_t*) JLI_MemAlloc(path_len * sizeof(wchar_t));
if (wpath != NULL) {
*err = mbstowcs_s(&converted_chars, wpath, path_len, path, path_len);
} else {
*err = ENOMEM;
}
*err = convert_to_unicode(path, L"", &wpath);
} else {
/* only UNC pathname includes double slashes here */
wpath = (wchar_t*) JLI_MemAlloc((path_len + 7) * sizeof(wchar_t));
if (wpath != NULL) {
wcscpy(wpath, L"\\\\?\\UNC\0");
*err = mbstowcs_s(&converted_chars, &wpath[7], path_len, path, path_len);
} else {
*err = ENOMEM;
}
*err = convert_to_unicode(path, L"\\\\?\\UNC", &wpath);
}
} else {
wpath = (wchar_t*) JLI_MemAlloc((path_len + 4) * sizeof(wchar_t));
if (wpath != NULL) {
wcscpy(wpath, L"\\\\?\\\0");
*err = mbstowcs_s(&converted_chars, &wpath[4], path_len, path, path_len);
} else {
*err = ENOMEM;
}
*err = convert_to_unicode(path, L"\\\\?\\", &wpath);
}
return wpath;
}