summaryrefslogtreecommitdiff
path: root/solenv/wsl/wsl-lo-helper.cpp
blob: 1a90580b6f19fc2566e5fd4bb637202672dc8ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * This is a tool that will be useful for various tasks if/when we build LO on WSL
 *
 * It is a Win32 program, not a Linux (WSL) one.
 *
 * Compile as: cl -MD wsl-lo-helper.cpp advapi32.lib
 */

#include <cstdio>
#include <cstring>

#include <Windows.h>

static void print_result(const wchar_t* argv0, const wchar_t* result)
{
    char output[1000];
    if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, result, -1, output, sizeof(output), NULL,
                            NULL)
        == 0)
    {
        fprintf(stderr, "%S --8.3: Could not convert result to UTF-8.\n", argv0);
        exit(1);
    }

    // Intentionally output no newline at end
    printf("%s", output);
    fflush(stdout);
}

static REGSAM parse_bitness(const wchar_t* argv0, const wchar_t* command, const wchar_t* bitness)
{
    if (wcscmp(bitness, L"32") == 0)
        return KEY_WOW64_32KEY;
    else if (wcscmp(bitness, L"64") == 0)
        return KEY_WOW64_64KEY;
    else
    {
        fprintf(stderr, "%S %S: Use 32 or 64.\n", argv0, command);
        exit(1);
    }
}

static void parse_hive(const wchar_t* argv0, const wchar_t* path, HKEY* hivep,
                       const wchar_t** hive_namep)
{
    if (wcscmp(path, L"HKEY_CLASSES_ROOT") == 0)
    {
        *hivep = HKEY_CLASSES_ROOT;
        *hive_namep = L"HKEY_CLASSES_ROOT";
    }
    else if (wcscmp(path, L"HKEY_CURRENT_CONFIG") == 0)
    {
        *hivep = HKEY_CURRENT_CONFIG;
        *hive_namep = L"HKEY_CURRENT_CONFIG";
    }
    else if (wcscmp(path, L"HKEY_CURRENT_USER") == 0)
    {
        *hivep = HKEY_CURRENT_USER;
        *hive_namep = L"HKEY_CURRENT_USER";
    }
    else if (wcscmp(path, L"HKEY_LOCAL_MACHINE") == 0)
    {
        *hivep = HKEY_LOCAL_MACHINE;
        *hive_namep = L"HKEY_LOCAL_MACHINE";
    }
    else if (wcscmp(path, L"HKEY_USERS") == 0)
    {
        *hivep = HKEY_USERS;
        *hive_namep = L"HKEY_USERS";
    }
    else
    {
        fprintf(stderr, "%S: Invalid Registry hive %S.\n", argv0, path);
        exit(1);
    }
}

int wmain(int argc, wchar_t** argv)
{
    if (argc <= 1)
    {
        fprintf(stderr, "%S: Usage:\n", argv[0]);
        fprintf(stderr, "%S --8.3 <windows-pathname>\n", argv[0]);
        fprintf(stderr, "    Print the 8.3 form of a Windows pathnam. Fail it not present.\n");
        fprintf(stderr, "%S --read-registry [32|64] <path>\n", argv[0]);
        fprintf(stderr, "    Read a string value from the Registry and print it.\n");
        fprintf(stderr, "%S --list-registry [32|64] <path>\n", argv[0]);
        fprintf(stderr, "    List subkeys of a key in the Registry.\n");
        exit(1);
    }

    if (wcscmp(argv[1], L"--8.3") == 0)
    {
        if (argc != 3)
        {
            fprintf(stderr, "%S --8.3: One pathname argument expected\n", argv[0]);
            exit(1);
        }

        // The argument should be a pathname in Windows format. The
        // output will be the 8.3 pathname if present. If not present,
        // return failure.

        wchar_t woutput[1000];
        if (GetShortPathNameW(argv[2], woutput, sizeof(woutput) / sizeof(woutput[0])) == 0)
        {
            fprintf(stderr, "%S --8.3: Could not get short pathname of %S.\n", argv[0], argv[2]);
            exit(1);
        }

        print_result(argv[0], woutput);
    }
    else if (wcscmp(argv[1], L"--read-registry") == 0)
    {
        if (argc != 4)
        {
            fprintf(stderr, "%S --read-registry: Bitness and path arguments expected.\n", argv[0]);
            exit(1);
        }

        REGSAM sam = parse_bitness(argv[0], L"--read-registry", argv[2]);

        wchar_t* path = wcsdup(argv[3]);

        for (wchar_t* p = path; *p != L'\0'; p++)
            if (*p == '/')
                *p = '\\';

        wchar_t* const first_backslash = wcschr(path, L'\\');
        if (first_backslash == NULL)
        {
            fprintf(stderr, "%S: Invalid path %S to value in the Registry.\n", argv[0], path);
            exit(1);
        }

        *first_backslash = L'\0';
        wchar_t* const key_path = first_backslash + 1;

        HKEY hive;
        const wchar_t* hive_name;
        parse_hive(argv[0], path, &hive, &hive_name);

        DWORD type;
        wchar_t result[1000];
        DWORD count = sizeof(result);

        wchar_t* last_backslash = wcsrchr(key_path, L'\\');
        if (last_backslash == NULL)
        {
            fprintf(stderr, "%S: Invalid path %S\\%S to value in the Registry.\n", argv[0],
                    hive_name, key_path);
            exit(1);
        }

        *last_backslash = L'\0';
        wchar_t* value = last_backslash + 1;

        HKEY key;
        if (RegOpenKeyExW(hive, key_path, 0, KEY_QUERY_VALUE | sam, &key) != ERROR_SUCCESS)
        {
            fprintf(stderr, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv[0],
                    hive_name, key_path, argv[2]);
            exit(1);
        }

        if (RegQueryValueExW(key, value, NULL, &type, (LPBYTE)result, &count) != ERROR_SUCCESS)
        {
            fprintf(stderr, "%S: Reading value %S\\%S\\%S from %S-bit Registry failed.\n", argv[0],
                    hive_name, key_path, value, argv[2]);
            exit(1);
        }
        if (type != REG_SZ)
        {
            fprintf(stderr, "%S: Value %S\\%S\\%S is not a string.\n", argv[0], hive_name, key_path,
                    value);
            exit(1);
        }
        print_result(argv[0], result);
    }
    else if (wcscmp(argv[1], L"--list-registry") == 0)
    {
        if (argc != 4)
        {
            fprintf(stderr, "%S --list-registry: Bitness and path arguments expected.\n", argv[0]);
            exit(1);
        }

        REGSAM sam = parse_bitness(argv[0], L"--list-registry", argv[2]);

        wchar_t* path = wcsdup(argv[3]);

        for (wchar_t* p = path; *p != L'\0'; p++)
            if (*p == '/')
                *p = '\\';

        wchar_t* const first_backslash = wcschr(path, L'\\');
        if (first_backslash == NULL)
        {
            fprintf(stderr, "%S: Invalid path %S to key in the Registry.\n", argv[0], path);
            exit(1);
        }

        *first_backslash = L'\0';
        wchar_t* const key_path = first_backslash + 1;

        HKEY hive;
        const wchar_t* hive_name;
        parse_hive(argv[0], path, &hive, &hive_name);

        HKEY key;
        if (RegOpenKeyExW(hive, key_path, 0, KEY_ENUMERATE_SUB_KEYS | sam, &key) != ERROR_SUCCESS)
        {
            fprintf(stderr, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv[0],
                    hive_name, key_path, argv[2]);
            exit(1);
        }

        DWORD index = 0;
        wchar_t name[256];
        DWORD namelen = sizeof(name) / sizeof(name[0]);
        while (RegEnumKeyExW(key, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
        {
            printf("%S\n", name);
            index++;
            namelen = sizeof(name) / sizeof(name[0]);
        }
    }
    else
    {
        fprintf(stderr, "%S: Unrecognized sub-command %S.\n", argv[0], argv[1]);
        exit(1);
    }

    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */