1 /* gstdio-private.c - private glib functions for gstdio.c
2 *
3 * Copyright 2004 Tor Lillqvist
4 * Copyright 2018 Руслан Ижбулатов
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* Strips "\\\\?\\" extended prefix or
21 * "\\??\\" NT Object Manager prefix from
22 * @str in-place, using memmove.
23 * @str_size must point to the size of @str
24 * in gunichar2s, including NUL-terminator
25 * (if @str is NUL-terminated; it doesn't have to be).
26 * On return @str_size will correctly reflect changes
27 * in @str size (if any).
28 * Returns TRUE if @str was modified.
29 */
30 static gboolean
_g_win32_strip_extended_ntobjm_prefix(gunichar2 * str,gsize * str_size)31 _g_win32_strip_extended_ntobjm_prefix (gunichar2 *str,
32 gsize *str_size)
33 {
34 const wchar_t *extended_prefix = L"\\\\?\\";
35 const gsize extended_prefix_len = wcslen (extended_prefix);
36 const gsize extended_prefix_len_bytes = sizeof (gunichar2) * extended_prefix_len;
37 const gsize extended_prefix_with_drive_len_bytes = sizeof (gunichar2) * (extended_prefix_len + 2);
38 const wchar_t *ntobjm_prefix = L"\\??\\";
39 const gsize ntobjm_prefix_len = wcslen (ntobjm_prefix);
40 const gsize ntobjm_prefix_len_bytes = sizeof (gunichar2) * ntobjm_prefix_len;
41 const gsize ntobjm_prefix_with_drive_len_bytes = sizeof (gunichar2) * (ntobjm_prefix_len + 2);
42 gboolean do_move = FALSE;
43 gsize move_shift = 0;
44
45 if ((*str_size) * sizeof (gunichar2) > extended_prefix_with_drive_len_bytes &&
46 memcmp (str,
47 extended_prefix,
48 extended_prefix_len_bytes) == 0 &&
49 iswascii (str[extended_prefix_len]) &&
50 iswalpha (str[extended_prefix_len]) &&
51 str[extended_prefix_len + 1] == L':')
52 {
53 do_move = TRUE;
54 move_shift = extended_prefix_len;
55 }
56 else if ((*str_size) * sizeof (gunichar2) > ntobjm_prefix_with_drive_len_bytes &&
57 memcmp (str,
58 ntobjm_prefix,
59 ntobjm_prefix_len_bytes) == 0 &&
60 iswascii (str[ntobjm_prefix_len]) &&
61 iswalpha (str[ntobjm_prefix_len]) &&
62 str[ntobjm_prefix_len + 1] == L':')
63 {
64 do_move = TRUE;
65 move_shift = ntobjm_prefix_len;
66 }
67
68 if (do_move)
69 {
70 *str_size -= move_shift;
71 memmove (str,
72 str + move_shift,
73 (*str_size) * sizeof (gunichar2));
74 }
75
76 return do_move;
77 }
78
79 static int
_g_win32_copy_and_maybe_terminate(const guchar * data,gsize in_to_copy,gunichar2 * buf,gsize buf_size,gunichar2 ** alloc_buf,gboolean terminate)80 _g_win32_copy_and_maybe_terminate (const guchar *data,
81 gsize in_to_copy,
82 gunichar2 *buf,
83 gsize buf_size,
84 gunichar2 **alloc_buf,
85 gboolean terminate)
86 {
87 gsize to_copy = in_to_copy;
88 /* Number of bytes we can use to add extra zeroes for NUL-termination.
89 * 0 means that we can destroy up to 2 bytes of data,
90 * 1 means that we can destroy up to 1 byte of data,
91 * 2 means that we do not perform destructive NUL-termination
92 */
93 gsize extra_bytes = terminate ? 2 : 0;
94 char *buf_in_chars;
95
96 if (to_copy == 0)
97 return 0;
98
99 /* 2 bytes is sizeof (wchar_t), for an extra NUL-terminator. */
100 if (buf)
101 {
102 if (to_copy >= buf_size)
103 {
104 extra_bytes = 0;
105 to_copy = buf_size;
106 }
107 else if (to_copy > buf_size - 2)
108 {
109 extra_bytes = 1;
110 }
111
112 memcpy (buf, data, to_copy);
113 }
114 else
115 {
116 /* Note that SubstituteNameLength is USHORT, so to_copy + 2, being
117 * gsize, never overflows.
118 */
119 *alloc_buf = g_malloc (to_copy + extra_bytes);
120 memcpy (*alloc_buf, data, to_copy);
121 }
122
123 if (!terminate)
124 return to_copy;
125
126 if (buf)
127 buf_in_chars = (char *) buf;
128 else
129 buf_in_chars = (char *) *alloc_buf;
130
131 if (to_copy >= 2 && buf_in_chars[to_copy - 2] == 0 &&
132 buf_in_chars[to_copy - 1] == 0)
133 {
134 /* Fully NUL-terminated, do nothing */
135 }
136 else if ((to_copy == 1 || buf_in_chars[to_copy - 2] != 0) &&
137 buf_in_chars[to_copy - 1] == 0)
138 {
139 /* Have one zero, try to add another one */
140 if (extra_bytes > 0)
141 {
142 /* Append trailing zero */
143 buf_in_chars[to_copy] = 0;
144 /* Be precise about the number of bytes we return */
145 to_copy += 1;
146 }
147 else if (to_copy >= 2)
148 {
149 /* No space for appending, destroy one byte */
150 buf_in_chars[to_copy - 2] = 0;
151 }
152 /* else there's no space at all (to_copy == 1), do nothing */
153 }
154 else if (extra_bytes > 0 || to_copy >= 2)
155 {
156 buf_in_chars[to_copy - 2 + extra_bytes] = 0;
157 buf_in_chars[to_copy - 1 + extra_bytes] = 0;
158 to_copy += extra_bytes;
159 }
160 else /* extra_bytes == 0 && to_copy == 1 */
161 {
162 buf_in_chars[0] = 0;
163 }
164
165 return to_copy;
166 }
167