2 * Win32 utility routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2006 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0+
11 #include "win32-utils.h"
13 /* Quote the argument element if necessary, so that it will get
14 * reconstructed correctly in the C runtime startup code. Note that
15 * the unquoting algorithm in the C runtime is really weird, and
16 * rather different than what Unix shells do. See stdargv.c in the C
17 * runtime sources (in the Platform SDK, in src/crt).
19 * Stolen from GLib's protect_argv(), an internal routine that quotes
20 * string in an argument list so that they arguments will be handled
21 * correctly in the command-line string passed to CreateProcess()
22 * if that string is constructed by gluing those strings together.
25 protect_arg (const gchar *argv)
28 const gchar *p = argv;
31 gboolean need_dblquotes = FALSE;
34 if (*p == ' ' || *p == '\t')
35 need_dblquotes = TRUE;
38 else if (*p == '\\') {
41 while (*pp && *pp == '\\')
50 q = new_arg = g_malloc (len + need_dblquotes*2 + 1);
59 else if (*p == '\\') {
62 while (*pp && *pp == '\\')
79 * Generate a string for a Win32 error.
81 #define ERRBUF_SIZE 1024
83 win32strerror(DWORD error)
85 static char errbuf[ERRBUF_SIZE+1];
89 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
90 NULL, error, 0, errbuf, ERRBUF_SIZE, NULL);
93 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
94 * message. Get rid of it.
96 errlen = strlen(errbuf);
98 errbuf[errlen - 1] = '\0';
99 errbuf[errlen - 2] = '\0';
101 p = strchr(errbuf, '\0');
102 g_snprintf(p, (gulong)(sizeof errbuf - (p-errbuf)), " (%lu)", error);
107 * Generate a string for a Win32 exception code.
110 win32strexception(DWORD exception)
112 static char errbuf[ERRBUF_SIZE+1];
113 static const struct exception_msg {
117 { EXCEPTION_ACCESS_VIOLATION, "Access violation" },
118 { EXCEPTION_ARRAY_BOUNDS_EXCEEDED, "Array bounds exceeded" },
119 { EXCEPTION_BREAKPOINT, "Breakpoint" },
120 { EXCEPTION_DATATYPE_MISALIGNMENT, "Data type misalignment" },
121 { EXCEPTION_FLT_DENORMAL_OPERAND, "Denormal floating-point operand" },
122 { EXCEPTION_FLT_DIVIDE_BY_ZERO, "Floating-point divide by zero" },
123 { EXCEPTION_FLT_INEXACT_RESULT, "Floating-point inexact result" },
124 { EXCEPTION_FLT_INVALID_OPERATION, "Invalid floating-point operation" },
125 { EXCEPTION_FLT_OVERFLOW, "Floating-point overflow" },
126 { EXCEPTION_FLT_STACK_CHECK, "Floating-point stack check" },
127 { EXCEPTION_FLT_UNDERFLOW, "Floating-point underflow" },
128 { EXCEPTION_GUARD_PAGE, "Guard page violation" },
129 { EXCEPTION_ILLEGAL_INSTRUCTION, "Illegal instruction" },
130 { EXCEPTION_IN_PAGE_ERROR, "Page-in error" },
131 { EXCEPTION_INT_DIVIDE_BY_ZERO, "Integer divide by zero" },
132 { EXCEPTION_INT_OVERFLOW, "Integer overflow" },
133 { EXCEPTION_INVALID_DISPOSITION, "Invalid disposition" },
134 { EXCEPTION_INVALID_HANDLE, "Invalid handle" },
135 { EXCEPTION_NONCONTINUABLE_EXCEPTION, "Non-continuable exception" },
136 { EXCEPTION_PRIV_INSTRUCTION, "Privileged instruction" },
137 { EXCEPTION_SINGLE_STEP, "Single-step complete" },
138 { EXCEPTION_STACK_OVERFLOW, "Stack overflow" },
141 #define N_EXCEPTIONS (sizeof exceptions / sizeof exceptions[0])
144 for (i = 0; i < N_EXCEPTIONS; i++) {
145 if (exceptions[i].code == exception)
146 return exceptions[i].msg;
148 g_snprintf(errbuf, (gulong)sizeof errbuf, "Exception 0x%08x", exception);
153 * Editor modelines - http://www.wireshark.org/tools/modelines.html
158 * indent-tabs-mode: nil
161 * ex: set shiftwidth=2 tabstop=8 expandtab:
162 * :indentSize=2:tabSize=8:noTabs=true: