1 /* gwin32-private.c - private glib functions for gwin32.c
2 *
3 * Copyright 2019 Руслан Ижбулатов
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* Copy @cmdline into @debugger, and substitute @pid for `%p`
20 * and @event for `%e`.
21 * If @debugger_size (in bytes) is overflowed, return %FALSE.
22 * Also returns %FALSE when `%` is followed by anything other
23 * than `e` or `p`.
24 */
25 static gboolean
_g_win32_subst_pid_and_event(char * debugger,gsize debugger_size,const char * cmdline,DWORD pid,guintptr event)26 _g_win32_subst_pid_and_event (char *debugger,
27 gsize debugger_size,
28 const char *cmdline,
29 DWORD pid,
30 guintptr event)
31 {
32 gsize i = 0, dbg_i = 0;
33 /* These are integers, and they can't be longer than 20 characters
34 * even when they are 64-bit and in decimal notation.
35 * Use 30 just to be sure.
36 */
37 #define STR_BUFFER_SIZE 30
38 char pid_str[STR_BUFFER_SIZE] = {0};
39 gsize pid_str_len;
40 char event_str[STR_BUFFER_SIZE] = {0};
41 gsize event_str_len;
42
43 _snprintf_s (pid_str, STR_BUFFER_SIZE, G_N_ELEMENTS (pid_str), "%lu", pid);
44 pid_str[G_N_ELEMENTS (pid_str) - 1] = 0;
45 pid_str_len = strlen (pid_str);
46 _snprintf_s (event_str, STR_BUFFER_SIZE, G_N_ELEMENTS (pid_str), "%Iu", event);
47 event_str[G_N_ELEMENTS (pid_str) - 1] = 0;
48 event_str_len = strlen (event_str);
49 #undef STR_BUFFER_SIZE
50
51 while (cmdline[i] != 0 && dbg_i < debugger_size)
52 {
53 if (cmdline[i] != '%')
54 debugger[dbg_i++] = cmdline[i++];
55 else if (cmdline[i + 1] == 'p')
56 {
57 gsize j = 0;
58 while (j < pid_str_len && dbg_i < debugger_size)
59 debugger[dbg_i++] = pid_str[j++];
60 i += 2;
61 }
62 else if (cmdline[i + 1] == 'e')
63 {
64 gsize j = 0;
65 while (j < event_str_len && dbg_i < debugger_size)
66 debugger[dbg_i++] = event_str[j++];
67 i += 2;
68 }
69 else
70 return FALSE;
71 }
72 if (dbg_i < debugger_size)
73 debugger[dbg_i] = 0;
74 else
75 return FALSE;
76
77 return TRUE;
78 }
79