Deduplicate FormatMessage UTF-16 to UTF-8 conversion code Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
diff --git a/src/common.h b/src/common.h
index 23a1e25..ca203ee 100644
--- a/src/common.h
+++ b/src/common.h
@@ -28,6 +28,7 @@
# include <windows.h>
# include "win32/msvc-compat.h"
# include "win32/mingw-compat.h"
+# include "win32/error.h"
# ifdef GIT_THREADS
# include "win32/pthread.h"
#endif
diff --git a/src/errors.c b/src/errors.c
index 56f07b6..3aa1757 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -51,34 +51,11 @@ void giterr_set(int error_class, const char *string, ...)
if (error_class == GITERR_OS) {
#ifdef GIT_WIN32
- if (win32_error_code) {
- LPWSTR lpMsgBuf = NULL;
- int size = FormatMessageW(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, win32_error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPWSTR)&lpMsgBuf, 0, NULL);
-
- if (size) {
- int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL);
-
- char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char));
- if (lpMsgBuf_utf8 == NULL) {
- LocalFree(lpMsgBuf);
- return;
- }
- if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) {
- LocalFree(lpMsgBuf);
- git__free(lpMsgBuf_utf8);
- return;
- }
-
- git_buf_PUTS(&buf, ": ");
- git_buf_puts(&buf, lpMsgBuf_utf8);
- LocalFree(lpMsgBuf);
- git__free(lpMsgBuf_utf8);
- }
+ char * win32_error = git_win32_get_error_message(win32_error_code);
+ if (win32_error) {
+ git_buf_PUTS(&buf, ": ");
+ git_buf_puts(&buf, win32_error);
+ git__free(win32_error);
SetLastError(0);
}
diff --git a/src/netops.c b/src/netops.c
index 86579c7..fd83a1c 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -41,27 +41,14 @@
static void net_set_error(const char *str)
{
int error = WSAGetLastError();
- LPWSTR err_str = NULL;
+ char * win32_error = git_win32_get_error_message(error);
- int size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&err_str, 0, 0);
-
- int utf8_size = WideCharToMultiByte(CP_UTF8, 0, err_str, -1, NULL, 0, NULL, NULL);
- char * err_str_utf8 = git__malloc(utf8_size * sizeof(char));
- if (err_str_utf8 == NULL) {
- LocalFree(err_str);
- return;
- }
-
- if (!WideCharToMultiByte(CP_UTF8, 0, err_str, -1, err_str_utf8, utf8_size, NULL, NULL)) {
- LocalFree(err_str);
- git__free(err_str_utf8);
- return;
+ if (win32_error) {
+ giterr_set(GITERR_NET, "%s: %s", str, win32_error);
+ git__free(win32_error);
+ } else {
+ giterr_set(GITERR_NET, str);
}
-
- giterr_set(GITERR_NET, "%s: %s", str, err_str_utf8);
- LocalFree(err_str);
- git__free(err_str_utf8);
}
#else
static void net_set_error(const char *str)
diff --git a/src/win32/error.c b/src/win32/error.c
new file mode 100644
index 0000000..3851ff0
--- /dev/null
+++ b/src/win32/error.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "error.h"
+
+char *git_win32_get_error_message(DWORD error_code)
+{
+ LPWSTR lpMsgBuf = NULL;
+
+ if (!error_code)
+ return NULL;
+
+ if (FormatMessageW(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPWSTR)&lpMsgBuf, 0, NULL)) {
+ int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL);
+
+ char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char));
+ if (lpMsgBuf_utf8 == NULL) {
+ LocalFree(lpMsgBuf);
+ return NULL;
+ }
+ if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) {
+ LocalFree(lpMsgBuf);
+ git__free(lpMsgBuf_utf8);
+ return NULL;
+ }
+
+ LocalFree(lpMsgBuf);
+ return lpMsgBuf_utf8;
+ }
+ return NULL;
+}
diff --git a/src/win32/error.h b/src/win32/error.h
new file mode 100644
index 0000000..12947a2
--- /dev/null
+++ b/src/win32/error.h
@@ -0,0 +1,13 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_git_win32_error_h__
+#define INCLUDE_git_win32_error_h__
+
+extern char *git_win32_get_error_message(DWORD error_code);
+
+#endif