nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /* win32-utils.c
2 * Win32 utility routines
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2006 Gerald Combs
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22  
23 #include "win32-utils.h"
24  
25 /* Quote the argument element if necessary, so that it will get
26 * reconstructed correctly in the C runtime startup code. Note that
27 * the unquoting algorithm in the C runtime is really weird, and
28 * rather different than what Unix shells do. See stdargv.c in the C
29 * runtime sources (in the Platform SDK, in src/crt).
30 *
31 * Stolen from GLib's protect_argv(), an internal routine that quotes
32 * string in an argument list so that they arguments will be handled
33 * correctly in the command-line string passed to CreateProcess()
34 * if that string is constructed by gluing those strings together.
35 */
36 gchar *
37 protect_arg (const gchar *argv)
38 {
39 gchar *new_arg;
40 const gchar *p = argv;
41 gchar *q;
42 gint len = 0;
43 gboolean need_dblquotes = FALSE;
44  
45 while (*p) {
46 if (*p == ' ' || *p == '\t')
47 need_dblquotes = TRUE;
48 else if (*p == '"')
49 len++;
50 else if (*p == '\\') {
51 const gchar *pp = p;
52  
53 while (*pp && *pp == '\\')
54 pp++;
55 if (*pp == '"')
56 len++;
57 }
58 len++;
59 p++;
60 }
61  
62 q = new_arg = g_malloc (len + need_dblquotes*2 + 1);
63 p = argv;
64  
65 if (need_dblquotes)
66 *q++ = '"';
67  
68 while (*p) {
69 if (*p == '"')
70 *q++ = '\\';
71 else if (*p == '\\') {
72 const gchar *pp = p;
73  
74 while (*pp && *pp == '\\')
75 pp++;
76 if (*pp == '"')
77 *q++ = '\\';
78 }
79 *q++ = *p;
80 p++;
81 }
82  
83 if (need_dblquotes)
84 *q++ = '"';
85 *q++ = '\0';
86  
87 return new_arg;
88 }
89  
90 /*
91 * Generate a string for a Win32 error.
92 */
93 #define ERRBUF_SIZE 1024
94 const char *
95 win32strerror(DWORD error)
96 {
97 static char errbuf[ERRBUF_SIZE+1];
98 size_t errlen;
99 char *p;
100  
101 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
102 NULL, error, 0, errbuf, ERRBUF_SIZE, NULL);
103  
104 /*
105 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
106 * message. Get rid of it.
107 */
108 errlen = strlen(errbuf);
109 if (errlen >= 2) {
110 errbuf[errlen - 1] = '\0';
111 errbuf[errlen - 2] = '\0';
112 }
113 p = strchr(errbuf, '\0');
114 g_snprintf(p, (gulong)(sizeof errbuf - (p-errbuf)), " (%lu)", error);
115 return errbuf;
116 }
117  
118 /*
119 * Generate a string for a Win32 exception code.
120 */
121 const char *
122 win32strexception(DWORD exception)
123 {
124 static char errbuf[ERRBUF_SIZE+1];
125 static const struct exception_msg {
126 int code;
127 char *msg;
128 } exceptions[] = {
129 { EXCEPTION_ACCESS_VIOLATION, "Access violation" },
130 { EXCEPTION_ARRAY_BOUNDS_EXCEEDED, "Array bounds exceeded" },
131 { EXCEPTION_BREAKPOINT, "Breakpoint" },
132 { EXCEPTION_DATATYPE_MISALIGNMENT, "Data type misalignment" },
133 { EXCEPTION_FLT_DENORMAL_OPERAND, "Denormal floating-point operand" },
134 { EXCEPTION_FLT_DIVIDE_BY_ZERO, "Floating-point divide by zero" },
135 { EXCEPTION_FLT_INEXACT_RESULT, "Floating-point inexact result" },
136 { EXCEPTION_FLT_INVALID_OPERATION, "Invalid floating-point operation" },
137 { EXCEPTION_FLT_OVERFLOW, "Floating-point overflow" },
138 { EXCEPTION_FLT_STACK_CHECK, "Floating-point stack check" },
139 { EXCEPTION_FLT_UNDERFLOW, "Floating-point underflow" },
140 { EXCEPTION_GUARD_PAGE, "Guard page violation" },
141 { EXCEPTION_ILLEGAL_INSTRUCTION, "Illegal instruction" },
142 { EXCEPTION_IN_PAGE_ERROR, "Page-in error" },
143 { EXCEPTION_INT_DIVIDE_BY_ZERO, "Integer divide by zero" },
144 { EXCEPTION_INT_OVERFLOW, "Integer overflow" },
145 { EXCEPTION_INVALID_DISPOSITION, "Invalid disposition" },
146 { EXCEPTION_INVALID_HANDLE, "Invalid handle" },
147 { EXCEPTION_NONCONTINUABLE_EXCEPTION, "Non-continuable exception" },
148 { EXCEPTION_PRIV_INSTRUCTION, "Privileged instruction" },
149 { EXCEPTION_SINGLE_STEP, "Single-step complete" },
150 { EXCEPTION_STACK_OVERFLOW, "Stack overflow" },
151 { 0, NULL }
152 };
153 #define N_EXCEPTIONS (sizeof exceptions / sizeof exceptions[0])
154 int i;
155  
156 for (i = 0; i < N_EXCEPTIONS; i++) {
157 if (exceptions[i].code == exception)
158 return exceptions[i].msg;
159 }
160 g_snprintf(errbuf, (gulong)sizeof errbuf, "Exception 0x%08x", exception);
161 return errbuf;
162 }
163  
164 /*
165 * Editor modelines - http://www.wireshark.org/tools/modelines.html
166 *
167 * Local Variables:
168 * c-basic-offset: 2
169 * tab-width: 8
170 * indent-tabs-mode: nil
171 * End:
172 *
173 * ex: set shiftwidth=2 tabstop=8 expandtab:
174 * :indentSize=2:tabSize=8:noTabs=true:
175 */