From ebbe98afa56283127c22854977a7aed2c16d9436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=AE=D1=80=D1=8C?= =?UTF-8?q?=D0=B5=D0=B2=20=28Leonid=20Yuriev=29?= Date: Thu, 9 Feb 2023 14:57:50 +0300 Subject: [PATCH] =?UTF-8?q?mdbx-windows:=20=D0=BB=D0=B8=D0=BA=D0=B2=D0=B8?= =?UTF-8?q?=D0=B4=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BC=D0=B0=D0=BA=D1=80=D0=BE?= =?UTF-8?q?=D1=81=D0=B0=20`OSAL=5FMB2WIDE()`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.c | 32 ++++++++++++++++++++------------ src/osal.c | 37 +++++++++++++++++++++++++------------ src/osal.h | 21 +-------------------- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/core.c b/src/core.c index e32e9ba8..42727794 100644 --- a/src/core.c +++ b/src/core.c @@ -14487,9 +14487,11 @@ __cold int mdbx_env_open_for_recovery(MDBX_env *env, const char *pathname, unsigned target_meta, bool writeable) { #if defined(_WIN32) || defined(_WIN64) wchar_t *pathnameW = nullptr; - OSAL_MB2WIDE(pathname, pathnameW); - int rc = mdbx_env_open_for_recoveryW(env, pathnameW, target_meta, writeable); - osal_free(pathnameW); + int rc = osal_mb2w(pathname, &pathnameW); + if (likely(rc == MDBX_SUCCESS)) { + rc = mdbx_env_open_for_recoveryW(env, pathnameW, target_meta, writeable); + osal_free(pathnameW); + } return rc; } @@ -14678,9 +14680,11 @@ __cold static int handle_env_pathname(MDBX_handle_env_pathname *ctx, __cold int mdbx_env_delete(const char *pathname, MDBX_env_delete_mode_t mode) { #if defined(_WIN32) || defined(_WIN64) wchar_t *pathnameW = nullptr; - OSAL_MB2WIDE(pathname, pathnameW); - int rc = mdbx_env_deleteW(pathnameW, mode); - osal_free(pathnameW); + int rc = osal_mb2w(pathname, &pathnameW); + if (likely(rc == MDBX_SUCCESS)) { + rc = mdbx_env_deleteW(pathnameW, mode); + osal_free(pathnameW); + } return rc; } @@ -14771,9 +14775,11 @@ __cold int mdbx_env_open(MDBX_env *env, const char *pathname, MDBX_env_flags_t flags, mdbx_mode_t mode) { #if defined(_WIN32) || defined(_WIN64) wchar_t *pathnameW = nullptr; - OSAL_MB2WIDE(pathname, pathnameW); - int rc = mdbx_env_openW(env, pathnameW, flags, mode); - osal_free(pathnameW); + int rc = osal_mb2w(pathname, &pathnameW); + if (likely(rc == MDBX_SUCCESS)) { + rc = mdbx_env_openW(env, pathnameW, flags, mode); + osal_free(pathnameW); + } return rc; } @@ -21779,9 +21785,11 @@ __cold int mdbx_env_copy(MDBX_env *env, const char *dest_path, MDBX_copy_flags_t flags) { #if defined(_WIN32) || defined(_WIN64) wchar_t *dest_pathW = nullptr; - OSAL_MB2WIDE(dest_path, dest_pathW); - int rc = mdbx_env_copyW(env, dest_pathW, flags); - osal_free(dest_pathW); + int rc = osal_mb2w(dest_path, &dest_pathW); + if (likely(rc == MDBX_SUCCESS)) { + rc = mdbx_env_copyW(env, dest_pathW, flags); + osal_free(dest_pathW); + } return rc; } diff --git a/src/osal.c b/src/osal.c index 45cb92c8..f5630324 100644 --- a/src/osal.c +++ b/src/osal.c @@ -546,19 +546,32 @@ MDBX_INTERNAL_FUNC int osal_fastmutex_release(osal_fastmutex_t *fastmutex) { #if defined(_WIN32) || defined(_WIN64) -#ifndef WC_ERR_INVALID_CHARS -static const DWORD WC_ERR_INVALID_CHARS = - (6 /* Windows Vista */ <= /* MajorVersion */ LOBYTE(LOWORD(GetVersion()))) - ? 0x00000080 - : 0; -#endif /* WC_ERR_INVALID_CHARS */ +MDBX_INTERNAL_FUNC int osal_mb2w(const char *const src, wchar_t **const pdst) { + const size_t dst_wlen = MultiByteToWideChar( + CP_THREAD_ACP, MB_ERR_INVALID_CHARS, src, -1, nullptr, 0); + wchar_t *dst = *pdst; + int rc = ERROR_INVALID_NAME; + if (unlikely(dst_wlen < 2 || dst_wlen > /* MAX_PATH */ INT16_MAX)) + goto bailout; -MDBX_MAYBE_UNUSED MDBX_INTERNAL_FUNC size_t osal_mb2w(wchar_t *dst, - size_t dst_n, - const char *src, - size_t src_n) { - return MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS, src, - (int)src_n, dst, (int)dst_n); + dst = osal_realloc(dst, dst_wlen * sizeof(wchar_t)); + rc = MDBX_ENOMEM; + if (unlikely(!dst)) + goto bailout; + + *pdst = dst; + if (likely(dst_wlen == (size_t)MultiByteToWideChar(CP_THREAD_ACP, + MB_ERR_INVALID_CHARS, src, + -1, dst, (int)dst_wlen))) + return MDBX_SUCCESS; + + rc = ERROR_INVALID_NAME; +bailout: + if (*pdst) { + osal_free(*pdst); + *pdst = nullptr; + } + return rc; } #endif /* Windows */ diff --git a/src/osal.h b/src/osal.h index a8e50a20..4e228ed7 100644 --- a/src/osal.h +++ b/src/osal.h @@ -757,26 +757,7 @@ MDBX_INTERNAL_FUNC int osal_rpid_check(MDBX_env *env, uint32_t pid); #if defined(_WIN32) || defined(_WIN64) -MDBX_INTERNAL_FUNC size_t osal_mb2w(wchar_t *dst, size_t dst_n, const char *src, - size_t src_n); - -#define OSAL_MB2WIDE(FROM, TO) \ - do { \ - const char *const from_tmp = (FROM); \ - const size_t from_mblen = strlen(from_tmp); \ - const size_t to_wlen = osal_mb2w(nullptr, 0, from_tmp, from_mblen); \ - if (unlikely(to_wlen < 1 || to_wlen > /* MAX_PATH */ INT16_MAX)) \ - return ERROR_INVALID_NAME; \ - wchar_t *const to_tmp = osal_malloc((to_wlen + 1) * sizeof(wchar_t)); \ - if (unlikely(!to_tmp)) \ - return MDBX_ENOMEM; \ - if (unlikely(to_wlen + 1 != \ - osal_mb2w(to_tmp, to_wlen + 1, from_tmp, from_mblen + 1))) { \ - osal_free(to_tmp); \ - return ERROR_INVALID_NAME; \ - } \ - (TO) = to_tmp; \ - } while (0) +MDBX_INTERNAL_FUNC int osal_mb2w(const char *const src, wchar_t **const pdst); typedef void(WINAPI *osal_srwlock_t_function)(osal_srwlock_t *); MDBX_INTERNAL_VAR osal_srwlock_t_function osal_srwlock_Init,