gitlab: Rename build-all job to build-linux.
[wine.git] / programs / reg / export.c
blobe8b7e22c6dd361977ec511e6b91c73e2b695c25a
1 /*
2 * Copyright 2017 Hugh McMaster
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
20 #include "reg.h"
22 static void write_file(HANDLE hFile, const WCHAR *str)
24 DWORD written;
26 WriteFile(hFile, str, lstrlenW(str) * sizeof(WCHAR), &written, NULL);
29 static WCHAR *escape_string(WCHAR *str, size_t str_len, size_t *line_len)
31 size_t i, escape_count, pos;
32 WCHAR *buf;
34 for (i = 0, escape_count = 0; i < str_len; i++)
36 WCHAR c = str[i];
38 if (!c) break;
40 if (c == '\r' || c == '\n' || c == '\\' || c == '"')
41 escape_count++;
44 buf = malloc((str_len + escape_count + 1) * sizeof(WCHAR));
46 for (i = 0, pos = 0; i < str_len; i++, pos++)
48 WCHAR c = str[i];
50 if (!c) break;
52 switch (c)
54 case '\r':
55 buf[pos++] = '\\';
56 buf[pos] = 'r';
57 break;
58 case '\n':
59 buf[pos++] = '\\';
60 buf[pos] = 'n';
61 break;
62 case '\\':
63 buf[pos++] = '\\';
64 buf[pos] = '\\';
65 break;
66 case '"':
67 buf[pos++] = '\\';
68 buf[pos] = '"';
69 break;
70 default:
71 buf[pos] = c;
75 buf[pos] = 0;
76 *line_len = pos;
77 return buf;
80 static size_t export_value_name(HANDLE hFile, WCHAR *name, size_t len)
82 static const WCHAR *default_name = L"@=";
83 size_t line_len;
85 if (name && *name)
87 WCHAR *str = escape_string(name, len, &line_len);
88 WCHAR *buf = malloc((line_len + 4) * sizeof(WCHAR));
89 line_len = swprintf(buf, line_len + 4, L"\"%s\"=", str);
90 write_file(hFile, buf);
91 free(buf);
92 free(str);
94 else
96 line_len = lstrlenW(default_name);
97 write_file(hFile, default_name);
100 return line_len;
103 static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
105 size_t len = 0, line_len;
106 WCHAR *str;
108 if (size)
109 len = size / sizeof(WCHAR) - 1;
110 str = escape_string(data, len, &line_len);
111 *buf = malloc((line_len + 3) * sizeof(WCHAR));
112 swprintf(*buf, line_len + 3, L"\"%s\"", str);
113 free(str);
116 static void export_dword_data(WCHAR **buf, DWORD *data)
118 *buf = malloc(15 * sizeof(WCHAR));
119 swprintf(*buf, 15, L"dword:%08x", *data);
122 static size_t export_hex_data_type(HANDLE hFile, DWORD type)
124 static const WCHAR *hex = L"hex:";
125 size_t line_len;
127 if (type == REG_BINARY)
129 line_len = lstrlenW(hex);
130 write_file(hFile, hex);
132 else
134 WCHAR *buf = malloc(15 * sizeof(WCHAR));
135 line_len = swprintf(buf, 15, L"hex(%x):", type);
136 write_file(hFile, buf);
137 free(buf);
140 return line_len;
143 #define MAX_HEX_CHARS 77
145 static void export_hex_data(HANDLE hFile, WCHAR **buf, DWORD type,
146 DWORD line_len, void *data, DWORD size)
148 size_t num_commas, i, pos;
150 line_len += export_hex_data_type(hFile, type);
152 if (!size) return;
154 num_commas = size - 1;
155 *buf = malloc(size * 3 * sizeof(WCHAR));
157 for (i = 0, pos = 0; i < size; i++)
159 pos += swprintf(*buf + pos, 3, L"%02x", ((BYTE *)data)[i]);
160 if (i == num_commas) break;
161 (*buf)[pos++] = ',';
162 (*buf)[pos] = 0;
163 line_len += 3;
165 if (line_len >= MAX_HEX_CHARS)
167 write_file(hFile, *buf);
168 write_file(hFile, L"\\\r\n ");
169 line_len = 2;
170 pos = 0;
175 static void export_newline(HANDLE hFile)
177 static const WCHAR *newline = L"\r\n";
179 write_file(hFile, newline);
182 static void export_data(HANDLE hFile, WCHAR *value_name, DWORD value_len,
183 DWORD type, void *data, size_t size)
185 WCHAR *buf = NULL;
186 size_t line_len = export_value_name(hFile, value_name, value_len);
188 switch (type)
190 case REG_SZ:
191 export_string_data(&buf, data, size);
192 break;
193 case REG_DWORD:
194 if (size)
196 export_dword_data(&buf, data);
197 break;
199 /* fall through */
200 case REG_NONE:
201 case REG_EXPAND_SZ:
202 case REG_BINARY:
203 case REG_MULTI_SZ:
204 default:
205 export_hex_data(hFile, &buf, type, line_len, data, size);
206 break;
209 if (size || type == REG_SZ)
211 write_file(hFile, buf);
212 free(buf);
215 export_newline(hFile);
218 static void export_key_name(HANDLE hFile, WCHAR *name)
220 WCHAR *buf;
222 buf = malloc((lstrlenW(name) + 7) * sizeof(WCHAR));
223 swprintf(buf, lstrlenW(name) + 7, L"\r\n[%s]\r\n", name);
224 write_file(hFile, buf);
225 free(buf);
228 static int export_registry_data(HANDLE hFile, HKEY hkey, WCHAR *path, REGSAM sam)
230 LONG rc;
231 DWORD max_value_len = 256, value_len;
232 DWORD max_data_bytes = 2048, data_size;
233 DWORD subkey_len;
234 DWORD i, type, path_len;
235 WCHAR *value_name, *subkey_name, *subkey_path;
236 BYTE *data;
237 HKEY subkey;
239 export_key_name(hFile, path);
241 value_name = malloc(max_value_len * sizeof(WCHAR));
242 data = malloc(max_data_bytes);
244 i = 0;
245 for (;;)
247 value_len = max_value_len;
248 data_size = max_data_bytes;
249 rc = RegEnumValueW(hkey, i, value_name, &value_len, NULL, &type, data, &data_size);
251 if (rc == ERROR_SUCCESS)
253 export_data(hFile, value_name, value_len, type, data, data_size);
254 i++;
256 else if (rc == ERROR_MORE_DATA)
258 if (data_size > max_data_bytes)
260 max_data_bytes = data_size;
261 data = realloc(data, max_data_bytes);
263 else
265 max_value_len *= 2;
266 value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
269 else break;
272 free(data);
273 free(value_name);
275 subkey_name = malloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
277 path_len = lstrlenW(path);
279 i = 0;
280 for (;;)
282 subkey_len = MAX_SUBKEY_LEN;
283 rc = RegEnumKeyExW(hkey, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
284 if (rc == ERROR_SUCCESS)
286 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
287 if (!RegOpenKeyExW(hkey, subkey_name, 0, KEY_READ|sam, &subkey))
289 export_registry_data(hFile, subkey, subkey_path, sam);
290 RegCloseKey(subkey);
292 free(subkey_path);
293 i++;
295 else break;
298 free(subkey_name);
299 return 0;
302 static void export_file_header(HANDLE hFile)
304 static const WCHAR header[] = L"\xFEFFWindows Registry Editor Version 5.00\r\n";
306 write_file(hFile, header);
309 static HANDLE create_file(const WCHAR *filename, DWORD action)
311 return CreateFileW(filename, GENERIC_WRITE, 0, NULL, action, FILE_ATTRIBUTE_NORMAL, NULL);
314 static HANDLE get_file_handle(WCHAR *filename, BOOL overwrite_file)
316 HANDLE hFile = create_file(filename, overwrite_file ? CREATE_ALWAYS : CREATE_NEW);
318 if (hFile == INVALID_HANDLE_VALUE)
320 DWORD error = GetLastError();
322 if (error == ERROR_FILE_EXISTS)
324 if (!ask_confirm(STRING_OVERWRITE_FILE, filename))
326 output_message(STRING_CANCELLED);
327 exit(0);
330 hFile = create_file(filename, CREATE_ALWAYS);
332 else
334 WCHAR *str;
336 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
337 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0, (WCHAR *)&str, 0, NULL);
338 output_writeconsole(str, lstrlenW(str));
339 LocalFree(str);
340 exit(1);
344 return hFile;
347 int reg_export(int argc, WCHAR *argvW[])
349 HKEY root, hkey;
350 WCHAR *path, *key_name;
351 BOOL overwrite_file = FALSE;
352 REGSAM sam = 0;
353 HANDLE hFile;
354 int i, ret;
356 if (argc < 4) goto invalid;
358 if (!parse_registry_key(argvW[2], &root, &path))
359 return 1;
361 for (i = 4; i < argc; i++)
363 WCHAR *str;
365 if (argvW[i][0] != '/' && argvW[i][0] != '-')
366 goto invalid;
368 str = &argvW[i][1];
370 if (is_char(*str, 'y') && !str[1])
371 overwrite_file = TRUE;
372 else if (!lstrcmpiW(str, L"reg:32"))
374 if (sam & KEY_WOW64_32KEY) goto invalid;
375 sam |= KEY_WOW64_32KEY;
376 continue;
378 else if (!lstrcmpiW(str, L"reg:64"))
380 if (sam & KEY_WOW64_64KEY) goto invalid;
381 sam |= KEY_WOW64_64KEY;
382 continue;
384 else
385 goto invalid;
388 if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
389 goto invalid;
391 if (RegOpenKeyExW(root, path, 0, KEY_READ|sam, &hkey))
393 output_message(STRING_KEY_NONEXIST);
394 return 1;
397 key_name = get_long_key(root, path);
399 hFile = get_file_handle(argvW[3], overwrite_file);
400 export_file_header(hFile);
401 ret = export_registry_data(hFile, hkey, key_name, sam);
402 export_newline(hFile);
403 CloseHandle(hFile);
405 RegCloseKey(hkey);
407 return ret;
409 invalid:
410 output_message(STRING_INVALID_SYNTAX);
411 output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
412 return 1;