]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Move format_win32_error into lib/log/
authorNick Mathewson <nickm@torproject.org>
Wed, 27 Jun 2018 16:15:22 +0000 (12:15 -0400)
committerNick Mathewson <nickm@torproject.org>
Wed, 27 Jun 2018 16:30:11 +0000 (12:30 -0400)
src/common/compat.c
src/common/compat.h
src/lib/log/include.am
src/lib/log/win32err.c [new file with mode: 0644]
src/lib/log/win32err.h [new file with mode: 0644]

index 9cc0b4f040af4e29b4c98475ef7556e57f7120e6..b462ee1b4ccfb9c980f33de579453bd9ab7c9614 100644 (file)
@@ -1934,51 +1934,6 @@ network_init(void)
   return 0;
 }
 
-#ifdef _WIN32
-/** Return a newly allocated string describing the windows system error code
- * <b>err</b>.  Note that error codes are different from errno.  Error codes
- * come from GetLastError() when a winapi call fails.  errno is set only when
- * ANSI functions fail.  Whee. */
-char *
-format_win32_error(DWORD err)
-{
-  TCHAR *str = NULL;
-  char *result;
-  DWORD n;
-
-  /* Somebody once decided that this interface was better than strerror(). */
-  n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
-                 FORMAT_MESSAGE_FROM_SYSTEM |
-                 FORMAT_MESSAGE_IGNORE_INSERTS,
-                 NULL, err,
-                 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
-                 (LPVOID)&str,
-                 0, NULL);
-
-  if (str && n) {
-#ifdef UNICODE
-    size_t len;
-    if (n > 128*1024)
-      len = (128 * 1024) * 2 + 1; /* This shouldn't be possible, but let's
-                                   * make sure. */
-    else
-      len = n * 2 + 1;
-    result = tor_malloc(len);
-    wcstombs(result,str,len);
-    result[len-1] = '\0';
-#else /* !(defined(UNICODE)) */
-    result = tor_strdup(str);
-#endif /* defined(UNICODE) */
-  } else {
-    result = tor_strdup("<unformattable error>");
-  }
-  if (str) {
-    LocalFree(str); /* LocalFree != free() */
-  }
-  return result;
-}
-#endif /* defined(_WIN32) */
-
 #if defined(HW_PHYSMEM64)
 /* This appears to be an OpenBSD thing */
 #define INT64_HW_MEM HW_PHYSMEM64
index efd4363246130f3513bd1845d7baacf03be78f60..dd45f22462f865ee8d00c12e99464df8ed138e63 100644 (file)
@@ -50,6 +50,7 @@
 #include "lib/string/compat_ctype.h"
 #include "lib/string/compat_string.h"
 #include "lib/string/printf.h"
+#include "lib/log/win32err.h"
 #include "lib/net/socket.h"
 #include "lib/net/ipv4.h"
 #include "lib/net/ipv6.h"
@@ -250,11 +251,6 @@ int tor_mlockall(void);
 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
 #endif
 
-/* Platform-specific helpers. */
-#ifdef _WIN32
-char *format_win32_error(DWORD err);
-#endif
-
 /*for some reason my compiler doesn't have these version flags defined
   a nice homework assignment for someone one day is to define the rest*/
 //these are the values as given on MSDN
index 235c95fdf13a529ddbb4e0c6920bf0a85048af7b..22b141d3169a8be64bd9d425946c253d86b5b618 100644 (file)
@@ -9,7 +9,8 @@ src_lib_libtor_log_a_SOURCES =                  \
        src/lib/log/escape.c                    \
        src/lib/log/ratelim.c                   \
        src/lib/log/torlog.c                    \
-       src/lib/log/util_bug.c
+       src/lib/log/util_bug.c                  \
+       src/lib/log/win32err.c
 
 src_lib_libtor_log_testing_a_SOURCES = \
        $(src_lib_libtor_log_a_SOURCES)
@@ -23,4 +24,5 @@ noinst_HEADERS +=                                     \
        src/lib/log/escape.h                            \
        src/lib/log/ratelim.h                           \
        src/lib/log/torlog.h                            \
-       src/lib/log/util_bug.h
+       src/lib/log/util_bug.h                          \
+       src/lib/log/win32err.h
diff --git a/src/lib/log/win32err.c b/src/lib/log/win32err.c
new file mode 100644 (file)
index 0000000..4586c23
--- /dev/null
@@ -0,0 +1,56 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifdef _WIN32
+#include "orconfig.h"
+#include "lib/log/win32err.h"
+#include "lib/malloc/util_malloc.h"
+
+#include <tchar.h>
+#include <windows.h>
+
+/** Return a newly allocated string describing the windows system error code
+ * <b>err</b>.  Note that error codes are different from errno.  Error codes
+ * come from GetLastError() when a winapi call fails.  errno is set only when
+ * ANSI functions fail.  Whee. */
+char *
+format_win32_error(DWORD err)
+{
+  TCHAR *str = NULL;
+  char *result;
+  DWORD n;
+
+  /* Somebody once decided that this interface was better than strerror(). */
+  n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                 FORMAT_MESSAGE_FROM_SYSTEM |
+                 FORMAT_MESSAGE_IGNORE_INSERTS,
+                 NULL, err,
+                 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
+                 (LPVOID)&str,
+                 0, NULL);
+
+  if (str && n) {
+#ifdef UNICODE
+    size_t len;
+    if (n > 128*1024)
+      len = (128 * 1024) * 2 + 1; /* This shouldn't be possible, but let's
+                                   * make sure. */
+    else
+      len = n * 2 + 1;
+    result = tor_malloc(len);
+    wcstombs(result,str,len);
+    result[len-1] = '\0';
+#else /* !(defined(UNICODE)) */
+    result = tor_strdup(str);
+#endif /* defined(UNICODE) */
+  } else {
+    result = tor_strdup("<unformattable error>");
+  }
+  if (str) {
+    LocalFree(str); /* LocalFree != free() */
+  }
+  return result;
+}
+#endif /* defined(_WIN32) */
diff --git a/src/lib/log/win32err.h b/src/lib/log/win32err.h
new file mode 100644 (file)
index 0000000..61d3af5
--- /dev/null
@@ -0,0 +1,17 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_WIN32ERR_H
+#define TOR_WIN32ERR_H
+
+#include "orconfig.h"
+
+/* Platform-specific helpers. */
+#ifdef _WIN32
+#include <windef.h>
+char *format_win32_error(DWORD err);
+#endif
+
+#endif