From 79836fa23ae275466fd7414967bd17e0f629c888 Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Thu, 23 Jun 2016 10:43:30 +0200 Subject: wsutil: Move Win32 helper routines from capchild MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move error handling and argument quoting routines from capchild to wsutil, as those methods will be used by extcap_spawn as well. Change-Id: I2c4515fefd5aecad317fcdaefa721734288f792c Reviewed-on: https://code.wireshark.org/review/16123 Petri-Dish: Roland Knall Tested-by: Petri Dish Buildbot Reviewed-by: João Valverde Reviewed-by: Roland Knall --- capchild/capture_sync.c | 144 +----------------------------------------------- 1 file changed, 1 insertion(+), 143 deletions(-) (limited to 'capchild') diff --git a/capchild/capture_sync.c b/capchild/capture_sync.c index 5080907c99..9acb64e6bb 100644 --- a/capchild/capture_sync.c +++ b/capchild/capture_sync.c @@ -33,6 +33,7 @@ #ifdef _WIN32 #include +#include #endif #ifdef HAVE_SYS_WAIT_H @@ -159,149 +160,6 @@ sync_pipe_add_arg(char **args, int *argc, const char *arg) return args; } - - -#ifdef _WIN32 -/* Quote the argument element if necessary, so that it will get - * reconstructed correctly in the C runtime startup code. Note that - * the unquoting algorithm in the C runtime is really weird, and - * rather different than what Unix shells do. See stdargv.c in the C - * runtime sources (in the Platform SDK, in src/crt). - * - * Stolen from GLib's protect_argv(), an internal routine that quotes - * string in an argument list so that they arguments will be handled - * correctly in the command-line string passed to CreateProcess() - * if that string is constructed by gluing those strings together. - */ -static gchar * -protect_arg (const gchar *argv) -{ - gchar *new_arg; - const gchar *p = argv; - gchar *q; - gint len = 0; - gboolean need_dblquotes = FALSE; - - while (*p) { - if (*p == ' ' || *p == '\t') - need_dblquotes = TRUE; - else if (*p == '"') - len++; - else if (*p == '\\') { - const gchar *pp = p; - - while (*pp && *pp == '\\') - pp++; - if (*pp == '"') - len++; - } - len++; - p++; - } - - q = new_arg = g_malloc (len + need_dblquotes*2 + 1); - p = argv; - - if (need_dblquotes) - *q++ = '"'; - - while (*p) { - if (*p == '"') - *q++ = '\\'; - else if (*p == '\\') { - const gchar *pp = p; - - while (*pp && *pp == '\\') - pp++; - if (*pp == '"') - *q++ = '\\'; - } - *q++ = *p; - p++; - } - - if (need_dblquotes) - *q++ = '"'; - *q++ = '\0'; - - return new_arg; -} - -/* - * Generate a string for a Win32 error. - */ -#define ERRBUF_SIZE 1024 -static const char * -win32strerror(DWORD error) -{ - static char errbuf[ERRBUF_SIZE+1]; - size_t errlen; - char *p; - - FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, error, 0, errbuf, ERRBUF_SIZE, NULL); - - /* - * "FormatMessage()" "helpfully" sticks CR/LF at the end of the - * message. Get rid of it. - */ - errlen = strlen(errbuf); - if (errlen >= 2) { - errbuf[errlen - 1] = '\0'; - errbuf[errlen - 2] = '\0'; - } - p = strchr(errbuf, '\0'); - g_snprintf(p, (gulong)(sizeof errbuf - (p-errbuf)), " (%lu)", error); - return errbuf; -} - -/* - * Generate a string for a Win32 exception code. - */ -static const char * -win32strexception(DWORD exception) -{ - static char errbuf[ERRBUF_SIZE+1]; - static const struct exception_msg { - int code; - char *msg; - } exceptions[] = { - { EXCEPTION_ACCESS_VIOLATION, "Access violation" }, - { EXCEPTION_ARRAY_BOUNDS_EXCEEDED, "Array bounds exceeded" }, - { EXCEPTION_BREAKPOINT, "Breakpoint" }, - { EXCEPTION_DATATYPE_MISALIGNMENT, "Data type misalignment" }, - { EXCEPTION_FLT_DENORMAL_OPERAND, "Denormal floating-point operand" }, - { EXCEPTION_FLT_DIVIDE_BY_ZERO, "Floating-point divide by zero" }, - { EXCEPTION_FLT_INEXACT_RESULT, "Floating-point inexact result" }, - { EXCEPTION_FLT_INVALID_OPERATION, "Invalid floating-point operation" }, - { EXCEPTION_FLT_OVERFLOW, "Floating-point overflow" }, - { EXCEPTION_FLT_STACK_CHECK, "Floating-point stack check" }, - { EXCEPTION_FLT_UNDERFLOW, "Floating-point underflow" }, - { EXCEPTION_GUARD_PAGE, "Guard page violation" }, - { EXCEPTION_ILLEGAL_INSTRUCTION, "Illegal instruction" }, - { EXCEPTION_IN_PAGE_ERROR, "Page-in error" }, - { EXCEPTION_INT_DIVIDE_BY_ZERO, "Integer divide by zero" }, - { EXCEPTION_INT_OVERFLOW, "Integer overflow" }, - { EXCEPTION_INVALID_DISPOSITION, "Invalid disposition" }, - { EXCEPTION_INVALID_HANDLE, "Invalid handle" }, - { EXCEPTION_NONCONTINUABLE_EXCEPTION, "Non-continuable exception" }, - { EXCEPTION_PRIV_INSTRUCTION, "Privileged instruction" }, - { EXCEPTION_SINGLE_STEP, "Single-step complete" }, - { EXCEPTION_STACK_OVERFLOW, "Stack overflow" }, - { 0, NULL } - }; -#define N_EXCEPTIONS (sizeof exceptions / sizeof exceptions[0]) - int i; - - for (i = 0; i < N_EXCEPTIONS; i++) { - if (exceptions[i].code == exception) - return exceptions[i].msg; - } - g_snprintf(errbuf, (gulong)sizeof errbuf, "Exception 0x%08x", exception); - return errbuf; -} -#endif - /* Initialize an argument list and add dumpcap to it. */ static char ** init_pipe_args(int *argc) { -- cgit v1.2.1