2017-12-03 06:50:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Hugh McMaster
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
#include <stdio.h>
|
2017-12-03 06:50:10 +00:00
|
|
|
#include "reg.h"
|
|
|
|
|
2017-12-03 06:50:16 +00:00
|
|
|
static void write_file(HANDLE hFile, const WCHAR *str)
|
|
|
|
{
|
|
|
|
DWORD written;
|
|
|
|
|
|
|
|
WriteFile(hFile, str, lstrlenW(str) * sizeof(WCHAR), &written, NULL);
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:19 +00:00
|
|
|
static WCHAR *escape_string(WCHAR *str, size_t str_len, size_t *line_len)
|
|
|
|
{
|
|
|
|
size_t i, escape_count, pos;
|
|
|
|
WCHAR *buf;
|
|
|
|
|
|
|
|
for (i = 0, escape_count = 0; i < str_len; i++)
|
|
|
|
{
|
|
|
|
WCHAR c = str[i];
|
2018-05-07 23:50:45 +00:00
|
|
|
|
|
|
|
if (!c) break;
|
|
|
|
|
|
|
|
if (c == '\r' || c == '\n' || c == '\\' || c == '"')
|
2017-12-03 06:50:19 +00:00
|
|
|
escape_count++;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
buf = malloc((str_len + escape_count + 1) * sizeof(WCHAR));
|
2017-12-03 06:50:19 +00:00
|
|
|
|
|
|
|
for (i = 0, pos = 0; i < str_len; i++, pos++)
|
|
|
|
{
|
|
|
|
WCHAR c = str[i];
|
|
|
|
|
2018-05-07 23:50:45 +00:00
|
|
|
if (!c) break;
|
|
|
|
|
2017-12-03 06:50:19 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '\r':
|
|
|
|
buf[pos++] = '\\';
|
|
|
|
buf[pos] = 'r';
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
buf[pos++] = '\\';
|
|
|
|
buf[pos] = 'n';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
buf[pos++] = '\\';
|
|
|
|
buf[pos] = '\\';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
buf[pos++] = '\\';
|
|
|
|
buf[pos] = '"';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf[pos] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[pos] = 0;
|
|
|
|
*line_len = pos;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t export_value_name(HANDLE hFile, WCHAR *name, size_t len)
|
|
|
|
{
|
2021-04-01 12:20:47 +00:00
|
|
|
static const WCHAR *default_name = L"@=";
|
2017-12-03 06:50:19 +00:00
|
|
|
size_t line_len;
|
|
|
|
|
|
|
|
if (name && *name)
|
|
|
|
{
|
|
|
|
WCHAR *str = escape_string(name, len, &line_len);
|
2021-03-24 08:08:58 +00:00
|
|
|
WCHAR *buf = malloc((line_len + 4) * sizeof(WCHAR));
|
2021-04-01 12:20:47 +00:00
|
|
|
line_len = swprintf(buf, line_len + 4, L"\"%s\"=", str);
|
2017-12-03 06:50:19 +00:00
|
|
|
write_file(hFile, buf);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(buf);
|
|
|
|
free(str);
|
2017-12-03 06:50:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line_len = lstrlenW(default_name);
|
|
|
|
write_file(hFile, default_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line_len;
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:20 +00:00
|
|
|
static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
|
|
|
|
{
|
|
|
|
size_t len = 0, line_len;
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
len = size / sizeof(WCHAR) - 1;
|
|
|
|
str = escape_string(data, len, &line_len);
|
2021-03-24 08:08:58 +00:00
|
|
|
*buf = malloc((line_len + 3) * sizeof(WCHAR));
|
2021-04-01 12:20:47 +00:00
|
|
|
swprintf(*buf, line_len + 3, L"\"%s\"", str);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(str);
|
2017-12-03 06:50:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:23 +00:00
|
|
|
static void export_dword_data(WCHAR **buf, DWORD *data)
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
*buf = malloc(15 * sizeof(WCHAR));
|
2021-04-01 12:20:47 +00:00
|
|
|
swprintf(*buf, 15, L"dword:%08x", *data);
|
2017-12-03 06:50:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:21 +00:00
|
|
|
static size_t export_hex_data_type(HANDLE hFile, DWORD type)
|
|
|
|
{
|
2021-04-01 12:20:47 +00:00
|
|
|
static const WCHAR *hex = L"hex:";
|
2017-12-03 06:50:21 +00:00
|
|
|
size_t line_len;
|
|
|
|
|
|
|
|
if (type == REG_BINARY)
|
|
|
|
{
|
|
|
|
line_len = lstrlenW(hex);
|
|
|
|
write_file(hFile, hex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
WCHAR *buf = malloc(15 * sizeof(WCHAR));
|
2021-04-01 12:20:47 +00:00
|
|
|
line_len = swprintf(buf, 15, L"hex(%x):", type);
|
2017-12-03 06:50:21 +00:00
|
|
|
write_file(hFile, buf);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(buf);
|
2017-12-03 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return line_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_HEX_CHARS 77
|
|
|
|
|
|
|
|
static void export_hex_data(HANDLE hFile, WCHAR **buf, DWORD type,
|
|
|
|
DWORD line_len, void *data, DWORD size)
|
|
|
|
{
|
|
|
|
size_t num_commas, i, pos;
|
|
|
|
|
|
|
|
line_len += export_hex_data_type(hFile, type);
|
|
|
|
|
|
|
|
if (!size) return;
|
|
|
|
|
|
|
|
num_commas = size - 1;
|
2021-03-24 08:08:58 +00:00
|
|
|
*buf = malloc(size * 3 * sizeof(WCHAR));
|
2017-12-03 06:50:21 +00:00
|
|
|
|
|
|
|
for (i = 0, pos = 0; i < size; i++)
|
|
|
|
{
|
2021-04-01 12:20:47 +00:00
|
|
|
pos += swprintf(*buf + pos, 3, L"%02x", ((BYTE *)data)[i]);
|
2017-12-03 06:50:21 +00:00
|
|
|
if (i == num_commas) break;
|
|
|
|
(*buf)[pos++] = ',';
|
|
|
|
(*buf)[pos] = 0;
|
|
|
|
line_len += 3;
|
|
|
|
|
|
|
|
if (line_len >= MAX_HEX_CHARS)
|
|
|
|
{
|
|
|
|
write_file(hFile, *buf);
|
2021-04-01 12:20:47 +00:00
|
|
|
write_file(hFile, L"\\\r\n ");
|
2017-12-03 06:50:21 +00:00
|
|
|
line_len = 2;
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:19 +00:00
|
|
|
static void export_newline(HANDLE hFile)
|
|
|
|
{
|
2021-04-01 12:20:47 +00:00
|
|
|
static const WCHAR *newline = L"\r\n";
|
2017-12-03 06:50:19 +00:00
|
|
|
|
|
|
|
write_file(hFile, newline);
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:20 +00:00
|
|
|
static void export_data(HANDLE hFile, WCHAR *value_name, DWORD value_len,
|
|
|
|
DWORD type, void *data, size_t size)
|
|
|
|
{
|
|
|
|
WCHAR *buf = NULL;
|
2017-12-03 06:50:21 +00:00
|
|
|
size_t line_len = export_value_name(hFile, value_name, value_len);
|
2017-12-03 06:50:20 +00:00
|
|
|
|
2017-12-03 06:50:21 +00:00
|
|
|
switch (type)
|
2017-12-03 06:50:20 +00:00
|
|
|
{
|
2017-12-03 06:50:21 +00:00
|
|
|
case REG_SZ:
|
2017-12-03 06:50:20 +00:00
|
|
|
export_string_data(&buf, data, size);
|
2017-12-03 06:50:21 +00:00
|
|
|
break;
|
2017-12-03 06:50:23 +00:00
|
|
|
case REG_DWORD:
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
export_dword_data(&buf, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
2017-12-03 06:50:21 +00:00
|
|
|
case REG_NONE:
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
case REG_BINARY:
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
default:
|
|
|
|
export_hex_data(hFile, &buf, type, line_len, data, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size || type == REG_SZ)
|
|
|
|
{
|
2017-12-03 06:50:20 +00:00
|
|
|
write_file(hFile, buf);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(buf);
|
2017-12-03 06:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export_newline(hFile);
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:18 +00:00
|
|
|
static void export_key_name(HANDLE hFile, WCHAR *name)
|
|
|
|
{
|
|
|
|
WCHAR *buf;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
buf = malloc((lstrlenW(name) + 7) * sizeof(WCHAR));
|
2021-04-01 12:20:47 +00:00
|
|
|
swprintf(buf, lstrlenW(name) + 7, L"\r\n[%s]\r\n", name);
|
2017-12-03 06:50:18 +00:00
|
|
|
write_file(hFile, buf);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(buf);
|
2017-12-03 06:50:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 13:36:12 +00:00
|
|
|
static int export_registry_data(HANDLE hFile, HKEY hkey, WCHAR *path, REGSAM sam)
|
2017-12-03 06:50:19 +00:00
|
|
|
{
|
|
|
|
LONG rc;
|
|
|
|
DWORD max_value_len = 256, value_len;
|
2017-12-03 06:50:20 +00:00
|
|
|
DWORD max_data_bytes = 2048, data_size;
|
2017-12-03 06:50:24 +00:00
|
|
|
DWORD subkey_len;
|
|
|
|
DWORD i, type, path_len;
|
|
|
|
WCHAR *value_name, *subkey_name, *subkey_path;
|
2017-12-03 06:50:20 +00:00
|
|
|
BYTE *data;
|
2017-12-03 06:50:24 +00:00
|
|
|
HKEY subkey;
|
2017-12-03 06:50:19 +00:00
|
|
|
|
|
|
|
export_key_name(hFile, path);
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
value_name = malloc(max_value_len * sizeof(WCHAR));
|
|
|
|
data = malloc(max_data_bytes);
|
2017-12-03 06:50:19 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
value_len = max_value_len;
|
2017-12-03 06:50:20 +00:00
|
|
|
data_size = max_data_bytes;
|
2021-04-28 09:19:28 +00:00
|
|
|
rc = RegEnumValueW(hkey, i, value_name, &value_len, NULL, &type, data, &data_size);
|
2017-12-03 06:50:19 +00:00
|
|
|
|
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
2017-12-03 06:50:20 +00:00
|
|
|
export_data(hFile, value_name, value_len, type, data, data_size);
|
2017-12-03 06:50:19 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
2017-12-03 06:50:20 +00:00
|
|
|
if (data_size > max_data_bytes)
|
|
|
|
{
|
|
|
|
max_data_bytes = data_size;
|
2021-03-24 08:08:58 +00:00
|
|
|
data = realloc(data, max_data_bytes);
|
2017-12-03 06:50:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_value_len *= 2;
|
2021-03-24 08:08:58 +00:00
|
|
|
value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
|
2017-12-03 06:50:20 +00:00
|
|
|
}
|
2017-12-03 06:50:19 +00:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(data);
|
|
|
|
free(value_name);
|
2017-12-03 06:50:24 +00:00
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
subkey_name = malloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
|
2017-12-03 06:50:24 +00:00
|
|
|
|
|
|
|
path_len = lstrlenW(path);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
subkey_len = MAX_SUBKEY_LEN;
|
2021-04-28 09:19:28 +00:00
|
|
|
rc = RegEnumKeyExW(hkey, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
|
2017-12-03 06:50:24 +00:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
|
2021-07-15 13:36:12 +00:00
|
|
|
if (!RegOpenKeyExW(hkey, subkey_name, 0, KEY_READ|sam, &subkey))
|
2017-12-03 06:50:24 +00:00
|
|
|
{
|
2021-07-15 13:36:12 +00:00
|
|
|
export_registry_data(hFile, subkey, subkey_path, sam);
|
2017-12-03 06:50:24 +00:00
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
2021-03-24 08:08:58 +00:00
|
|
|
free(subkey_path);
|
2017-12-03 06:50:24 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(subkey_name);
|
2017-12-03 06:50:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:16 +00:00
|
|
|
static void export_file_header(HANDLE hFile)
|
|
|
|
{
|
2021-04-05 20:58:32 +00:00
|
|
|
static const WCHAR header[] = L"\xFEFFWindows Registry Editor Version 5.00\r\n";
|
2017-12-03 06:50:16 +00:00
|
|
|
|
|
|
|
write_file(hFile, header);
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:50:15 +00:00
|
|
|
static HANDLE create_file(const WCHAR *filename, DWORD action)
|
|
|
|
{
|
|
|
|
return CreateFileW(filename, GENERIC_WRITE, 0, NULL, action, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE get_file_handle(WCHAR *filename, BOOL overwrite_file)
|
|
|
|
{
|
|
|
|
HANDLE hFile = create_file(filename, overwrite_file ? CREATE_ALWAYS : CREATE_NEW);
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
|
|
|
|
if (error == ERROR_FILE_EXISTS)
|
|
|
|
{
|
|
|
|
if (!ask_confirm(STRING_OVERWRITE_FILE, filename))
|
|
|
|
{
|
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
hFile = create_file(filename, CREATE_ALWAYS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0, (WCHAR *)&str, 0, NULL);
|
|
|
|
output_writeconsole(str, lstrlenW(str));
|
|
|
|
LocalFree(str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hFile;
|
|
|
|
}
|
|
|
|
|
2021-03-18 11:36:52 +00:00
|
|
|
int reg_export(int argc, WCHAR *argvW[])
|
2017-12-03 06:50:09 +00:00
|
|
|
{
|
2017-12-03 06:50:14 +00:00
|
|
|
HKEY root, hkey;
|
2021-04-05 13:24:11 +00:00
|
|
|
WCHAR *path, *key_name;
|
2017-12-03 06:50:15 +00:00
|
|
|
BOOL overwrite_file = FALSE;
|
2021-07-15 13:36:12 +00:00
|
|
|
REGSAM sam = 0;
|
2017-12-03 06:50:15 +00:00
|
|
|
HANDLE hFile;
|
2021-04-01 12:20:48 +00:00
|
|
|
int i, ret;
|
2017-12-03 06:50:11 +00:00
|
|
|
|
2021-04-01 12:20:48 +00:00
|
|
|
if (argc < 4) goto invalid;
|
2017-12-03 06:50:10 +00:00
|
|
|
|
2021-04-05 13:24:11 +00:00
|
|
|
if (!parse_registry_key(argvW[2], &root, &path))
|
2017-12-03 06:50:11 +00:00
|
|
|
return 1;
|
|
|
|
|
2021-04-01 12:20:48 +00:00
|
|
|
for (i = 4; i < argc; i++)
|
|
|
|
{
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
if (argvW[i][0] != '/' && argvW[i][0] != '-')
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
str = &argvW[i][1];
|
|
|
|
|
|
|
|
if (is_char(*str, 'y') && !str[1])
|
|
|
|
overwrite_file = TRUE;
|
2021-07-15 13:36:12 +00:00
|
|
|
else if (!lstrcmpiW(str, L"reg:32"))
|
|
|
|
{
|
|
|
|
if (sam & KEY_WOW64_32KEY) goto invalid;
|
|
|
|
sam |= KEY_WOW64_32KEY;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!lstrcmpiW(str, L"reg:64"))
|
|
|
|
{
|
|
|
|
if (sam & KEY_WOW64_64KEY) goto invalid;
|
|
|
|
sam |= KEY_WOW64_64KEY;
|
2021-04-08 12:10:54 +00:00
|
|
|
continue;
|
2021-07-15 13:36:12 +00:00
|
|
|
}
|
2021-04-01 12:20:48 +00:00
|
|
|
else
|
|
|
|
goto invalid;
|
|
|
|
}
|
2017-12-03 06:50:10 +00:00
|
|
|
|
2021-07-15 13:36:12 +00:00
|
|
|
if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
if (RegOpenKeyExW(root, path, 0, KEY_READ|sam, &hkey))
|
2017-12-03 06:50:14 +00:00
|
|
|
{
|
2021-04-01 12:20:49 +00:00
|
|
|
output_message(STRING_KEY_NONEXIST);
|
2017-12-03 06:50:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:24:11 +00:00
|
|
|
key_name = get_long_key(root, path);
|
|
|
|
|
2021-03-18 11:36:52 +00:00
|
|
|
hFile = get_file_handle(argvW[3], overwrite_file);
|
2017-12-03 06:50:16 +00:00
|
|
|
export_file_header(hFile);
|
2021-07-15 13:36:12 +00:00
|
|
|
ret = export_registry_data(hFile, hkey, key_name, sam);
|
2017-12-03 06:50:19 +00:00
|
|
|
export_newline(hFile);
|
2017-12-03 06:50:15 +00:00
|
|
|
CloseHandle(hFile);
|
2017-12-03 06:50:14 +00:00
|
|
|
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
|
2017-12-03 06:50:19 +00:00
|
|
|
return ret;
|
2017-12-03 06:50:10 +00:00
|
|
|
|
2021-04-01 12:20:48 +00:00
|
|
|
invalid:
|
2017-12-03 06:50:10 +00:00
|
|
|
output_message(STRING_INVALID_SYNTAX);
|
2021-03-18 11:36:52 +00:00
|
|
|
output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
|
2017-12-03 06:50:10 +00:00
|
|
|
return 1;
|
2017-12-03 06:50:09 +00:00
|
|
|
}
|