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