1 /* gstdio-private.c - private glib functions for gstdio.c
3 * Copyright 2004 Tor Lillqvist
4 * Copyright 2018 Руслан Ижбулатов
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library 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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses/>.
22 /* Strips "\\\\?\\" extended prefix or
23 * "\\??\\" NT Object Manager prefix from
24 * @str in-place, using memmove.
25 * @str_size must point to the size of @str
26 * in gunichar2s, including NUL-terminator
27 * (if @str is NUL-terminated; it doesn't have to be).
28 * On return @str_size will correctly reflect changes
29 * in @str size (if any).
30 * Returns TRUE if @str was modified.
33 _g_win32_strip_extended_ntobjm_prefix (gunichar2 *str,
36 const wchar_t *extended_prefix = L"\\\\?\\";
37 const gsize extended_prefix_len = wcslen (extended_prefix);
38 const gsize extended_prefix_len_bytes = sizeof (gunichar2) * extended_prefix_len;
39 const gsize extended_prefix_with_drive_len_bytes = sizeof (gunichar2) * (extended_prefix_len + 2);
40 const wchar_t *ntobjm_prefix = L"\\??\\";
41 const gsize ntobjm_prefix_len = wcslen (ntobjm_prefix);
42 const gsize ntobjm_prefix_len_bytes = sizeof (gunichar2) * ntobjm_prefix_len;
43 const gsize ntobjm_prefix_with_drive_len_bytes = sizeof (gunichar2) * (ntobjm_prefix_len + 2);
44 gboolean do_move = FALSE;
47 if ((*str_size) * sizeof (gunichar2) > extended_prefix_with_drive_len_bytes &&
50 extended_prefix_len_bytes) == 0 &&
51 iswascii (str[extended_prefix_len]) &&
52 iswalpha (str[extended_prefix_len]) &&
53 str[extended_prefix_len + 1] == L':')
56 move_shift = extended_prefix_len;
58 else if ((*str_size) * sizeof (gunichar2) > ntobjm_prefix_with_drive_len_bytes &&
61 ntobjm_prefix_len_bytes) == 0 &&
62 iswascii (str[ntobjm_prefix_len]) &&
63 iswalpha (str[ntobjm_prefix_len]) &&
64 str[ntobjm_prefix_len + 1] == L':')
67 move_shift = ntobjm_prefix_len;
72 *str_size -= move_shift;
75 (*str_size) * sizeof (gunichar2));
82 _g_win32_copy_and_maybe_terminate (const guchar *data,
86 gunichar2 **alloc_buf,
89 gsize to_copy = in_to_copy;
90 /* Number of bytes we can use to add extra zeroes for NUL-termination.
91 * 0 means that we can destroy up to 2 bytes of data,
92 * 1 means that we can destroy up to 1 byte of data,
93 * 2 means that we do not perform destructive NUL-termination
95 gsize extra_bytes = terminate ? 2 : 0;
101 /* 2 bytes is sizeof (wchar_t), for an extra NUL-terminator. */
104 if (to_copy >= buf_size)
109 else if (to_copy > buf_size - 2)
114 memcpy (buf, data, to_copy);
118 /* Note that SubstituteNameLength is USHORT, so to_copy + 2, being
119 * gsize, never overflows.
121 *alloc_buf = g_malloc (to_copy + extra_bytes);
122 memcpy (*alloc_buf, data, to_copy);
129 buf_in_chars = (char *) buf;
131 buf_in_chars = (char *) *alloc_buf;
133 if (to_copy >= 2 && buf_in_chars[to_copy - 2] == 0 &&
134 buf_in_chars[to_copy - 1] == 0)
136 /* Fully NUL-terminated, do nothing */
138 else if ((to_copy == 1 || buf_in_chars[to_copy - 2] != 0) &&
139 buf_in_chars[to_copy - 1] == 0)
141 /* Have one zero, try to add another one */
144 /* Append trailing zero */
145 buf_in_chars[to_copy] = 0;
146 /* Be precise about the number of bytes we return */
149 else if (to_copy >= 2)
151 /* No space for appending, destroy one byte */
152 buf_in_chars[to_copy - 2] = 0;
154 /* else there's no space at all (to_copy == 1), do nothing */
156 else if (extra_bytes > 0 || to_copy >= 2)
158 buf_in_chars[to_copy - 2 + extra_bytes] = 0;
159 buf_in_chars[to_copy - 1 + extra_bytes] = 0;
160 to_copy += extra_bytes;
162 else /* extra_bytes == 0 && to_copy == 1 */