]> wimlib.net Git - wimlib/blob - programs/imagex-win32.c
wimlib_iterate_dir_tree(): iterate in default case order
[wimlib] / programs / imagex-win32.c
1 /* Windows-specific code for wimlib-imagex.  */
2
3 #ifndef __WIN32__
4 #  error "This file contains Windows code"
5 #endif
6
7 #include "imagex-win32.h"
8 #include <fcntl.h>
9 #include <io.h>
10 #include <stdio.h>
11 #include <windows.h>
12
13 /* Convert a string from the "current Windows codepage" to UTF-16LE.  */
14 wchar_t *
15 win32_mbs_to_wcs(const char *mbs, size_t mbs_nbytes, size_t *num_wchars_ret)
16 {
17         if (mbs_nbytes > INT_MAX) {
18                 fwprintf(stderr, L"ERROR: too much data (%zu bytes)!\n",
19                          mbs_nbytes);
20                 return NULL;
21         }
22         if (mbs_nbytes == 0) {
23                 *num_wchars_ret = 0;
24                 return (wchar_t*)mbs;
25         }
26         int len = MultiByteToWideChar(CP_ACP,
27                                       MB_ERR_INVALID_CHARS,
28                                       mbs,
29                                       mbs_nbytes,
30                                       NULL,
31                                       0);
32         if (len <= 0)
33                 goto out_invalid;
34         wchar_t *wcs = malloc(len * sizeof(wchar_t));
35         if (!wcs) {
36                 fwprintf(stderr, L"ERROR: out of memory!\n");
37                 return NULL;
38         }
39         int len2 = MultiByteToWideChar(CP_ACP,
40                                        MB_ERR_INVALID_CHARS,
41                                        mbs,
42                                        mbs_nbytes,
43                                        wcs,
44                                        len);
45         if (len2 != len) {
46                 free(wcs);
47                 goto out_invalid;
48         }
49         *num_wchars_ret = len;
50         return wcs;
51 out_invalid:
52         fwprintf(stderr,
53 L"ERROR: Invalid multi-byte string in the text file you provided as input!\n"
54 L"       Maybe try converting your text file to UTF-16LE?\n"
55         );
56         return NULL;
57 }
58
59 /* Set a file descriptor to binary mode.  */
60 void set_fd_to_binary_mode(int fd)
61 {
62         _setmode(fd, _O_BINARY);
63 }
64
65 #include <sddl.h>
66
67 static wchar_t *
68 get_security_descriptor_string(PSECURITY_DESCRIPTOR desc)
69 {
70         wchar_t *str = NULL;
71         /* 52 characters!!!  */
72         ConvertSecurityDescriptorToStringSecurityDescriptorW(
73                         desc,
74                         SDDL_REVISION_1,
75                         OWNER_SECURITY_INFORMATION |
76                                 GROUP_SECURITY_INFORMATION |
77                                 DACL_SECURITY_INFORMATION |
78                                 SACL_SECURITY_INFORMATION,
79                         &str,
80                         NULL);
81         return str;
82 }
83
84 void
85 win32_print_security_descriptor(const uint8_t *sd, size_t size)
86 {
87         wchar_t *str;
88         const wchar_t *printstr;
89
90         /* 'size' is ignored here due to the crappy Windows APIs.  Oh well, this
91          * is just for debugging anyway.  */
92         str = get_security_descriptor_string((PSECURITY_DESCRIPTOR)sd);
93         if (str)
94                 printstr = str;
95         else
96                 printstr = L"(invalid)";
97
98         wprintf(L"Security Descriptor = %ls\n", printstr);
99
100         LocalFree(str);
101 }