2021-03-17 12:38:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016-2017, 2021 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "reg.h"
|
|
|
|
|
|
|
|
static const WCHAR *reg_type_to_wchar(DWORD type)
|
|
|
|
{
|
|
|
|
int i, array_size = ARRAY_SIZE(type_rels);
|
|
|
|
|
|
|
|
for (i = 0; i < array_size; i++)
|
|
|
|
{
|
|
|
|
if (type == type_rels[i].type)
|
|
|
|
return type_rels[i].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
|
|
|
|
{
|
|
|
|
WCHAR *buffer = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
case REG_EXPAND_SZ:
|
2021-03-24 08:08:58 +00:00
|
|
|
buffer = malloc(size_bytes);
|
2021-03-17 12:38:04 +00:00
|
|
|
lstrcpyW(buffer, (WCHAR *)src);
|
|
|
|
break;
|
|
|
|
case REG_NONE:
|
|
|
|
case REG_BINARY:
|
|
|
|
{
|
|
|
|
WCHAR *ptr;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
buffer = malloc((size_bytes * 2 + 1) * sizeof(WCHAR));
|
2021-05-06 12:47:55 +00:00
|
|
|
|
|
|
|
if (!size_bytes)
|
|
|
|
{
|
|
|
|
*buffer = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-17 12:38:04 +00:00
|
|
|
ptr = buffer;
|
2021-05-06 12:47:55 +00:00
|
|
|
|
2021-03-17 12:38:04 +00:00
|
|
|
for (i = 0; i < size_bytes; i++)
|
2021-03-30 12:00:16 +00:00
|
|
|
ptr += swprintf(ptr, 3, L"%02X", src[i]);
|
2021-03-17 12:38:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REG_DWORD:
|
|
|
|
/* case REG_DWORD_LITTLE_ENDIAN: */
|
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
{
|
|
|
|
const int zero_x_dword = 10;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
buffer = malloc((zero_x_dword + 1) * sizeof(WCHAR));
|
2021-03-30 12:00:16 +00:00
|
|
|
swprintf(buffer, zero_x_dword + 1, L"0x%x", *(DWORD *)src);
|
2021-03-17 12:38:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
{
|
|
|
|
const int two_wchars = 2 * sizeof(WCHAR);
|
|
|
|
DWORD tmp_size;
|
|
|
|
const WCHAR *tmp = (const WCHAR *)src;
|
|
|
|
int len, destindex;
|
|
|
|
|
|
|
|
if (size_bytes <= two_wchars)
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
buffer = malloc(sizeof(WCHAR));
|
2021-03-17 12:38:04 +00:00
|
|
|
*buffer = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
|
2021-03-24 08:08:58 +00:00
|
|
|
buffer = malloc(tmp_size * 2 + sizeof(WCHAR));
|
2021-03-17 12:38:04 +00:00
|
|
|
len = tmp_size / sizeof(WCHAR);
|
|
|
|
|
|
|
|
for (i = 0, destindex = 0; i < len; i++, destindex++)
|
|
|
|
{
|
|
|
|
if (tmp[i])
|
|
|
|
buffer[destindex] = tmp[i];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[destindex++] = '\\';
|
|
|
|
buffer[destindex] = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2021-03-30 12:00:16 +00:00
|
|
|
static const WCHAR *newlineW = L"\n";
|
2021-03-17 12:38:04 +00:00
|
|
|
|
|
|
|
static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
|
|
|
|
{
|
2021-03-30 12:00:16 +00:00
|
|
|
static const WCHAR *fmt = L" %1";
|
2021-03-17 12:38:04 +00:00
|
|
|
WCHAR defval[32];
|
|
|
|
WCHAR *reg_data;
|
|
|
|
|
|
|
|
if (value_name && value_name[0])
|
|
|
|
output_string(fmt, value_name);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
|
|
|
|
output_string(fmt, defval);
|
|
|
|
}
|
|
|
|
output_string(fmt, reg_type_to_wchar(type));
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
reg_data = reg_data_to_wchar(type, data, data_size);
|
|
|
|
output_string(fmt, reg_data);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(reg_data);
|
2021-03-17 12:38:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
|
|
|
|
output_string(fmt, defval);
|
|
|
|
}
|
|
|
|
output_string(newlineW);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int num_values_found = 0;
|
|
|
|
|
2021-04-28 09:19:28 +00:00
|
|
|
static int query_value(HKEY hkey, WCHAR *value_name, WCHAR *path, BOOL recurse)
|
2021-03-17 12:38:04 +00:00
|
|
|
{
|
|
|
|
LONG rc;
|
|
|
|
DWORD max_data_bytes = 2048, data_size;
|
|
|
|
DWORD subkey_len;
|
|
|
|
DWORD type, path_len, i;
|
|
|
|
BYTE *data;
|
2021-03-30 12:00:16 +00:00
|
|
|
static const WCHAR *fmt = L"%1\n";
|
2021-03-17 12:38:04 +00:00
|
|
|
WCHAR *subkey_name, *subkey_path;
|
|
|
|
HKEY subkey;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
data = malloc(max_data_bytes);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
data_size = max_data_bytes;
|
2021-04-28 09:19:28 +00:00
|
|
|
rc = RegQueryValueExW(hkey, value_name, NULL, &type, data, &data_size);
|
2021-03-17 12:38:04 +00:00
|
|
|
if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
max_data_bytes = data_size;
|
2021-03-24 08:08:58 +00:00
|
|
|
data = realloc(data, max_data_bytes);
|
2021-03-17 12:38:04 +00:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
output_string(fmt, path);
|
|
|
|
output_value(value_name, type, data, data_size);
|
|
|
|
output_string(newlineW);
|
|
|
|
num_values_found++;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(data);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
|
|
|
if (!recurse)
|
|
|
|
{
|
|
|
|
if (rc == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
if (value_name && *value_name)
|
|
|
|
{
|
2021-04-01 12:20:52 +00:00
|
|
|
output_message(STRING_VALUE_NONEXIST);
|
2021-03-17 12:38:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
output_string(fmt, path);
|
|
|
|
output_value(NULL, REG_SZ, NULL, 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
subkey_name = malloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
|
2021-03-17 12:38:04 +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);
|
2021-03-17 12:38:04 +00:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
|
2021-04-28 09:19:28 +00:00
|
|
|
if (!RegOpenKeyExW(hkey, subkey_name, 0, KEY_READ, &subkey))
|
2021-03-17 12:38:04 +00:00
|
|
|
{
|
|
|
|
query_value(subkey, value_name, subkey_path, recurse);
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
2021-03-24 08:08:58 +00:00
|
|
|
free(subkey_path);
|
2021-03-17 12:38:04 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(subkey_name);
|
2021-03-17 12:38:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-28 09:19:30 +00:00
|
|
|
static int query_all(HKEY hkey, WCHAR *path, BOOL recurse, BOOL recursing)
|
2021-03-17 12:38:04 +00:00
|
|
|
{
|
|
|
|
LONG rc;
|
2021-04-28 09:19:32 +00:00
|
|
|
DWORD num_subkeys, num_values;
|
2021-03-17 12:38:04 +00:00
|
|
|
DWORD max_value_len = 256, value_len;
|
|
|
|
DWORD max_data_bytes = 2048, data_size;
|
|
|
|
DWORD subkey_len;
|
|
|
|
DWORD i, type, path_len;
|
|
|
|
WCHAR *value_name, *subkey_name, *subkey_path;
|
|
|
|
BYTE *data;
|
|
|
|
HKEY subkey;
|
|
|
|
|
2021-04-28 09:19:32 +00:00
|
|
|
rc = RegQueryInfoKeyW(hkey, NULL, NULL, NULL, &num_subkeys, NULL,
|
|
|
|
NULL, &num_values, NULL, NULL, NULL, NULL);
|
2021-04-28 09:19:30 +00:00
|
|
|
if (rc) return 1;
|
|
|
|
|
|
|
|
if (num_values || recursing)
|
|
|
|
output_string(L"%1\n", path);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
value_name = malloc(max_value_len * sizeof(WCHAR));
|
|
|
|
data = malloc(max_data_bytes);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
value_len = max_value_len;
|
|
|
|
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);
|
2021-03-17 12:38:04 +00:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
output_value(value_name, type, data, data_size);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
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);
|
2021-03-17 12:38:04 +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));
|
2021-03-17 12:38:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(data);
|
|
|
|
free(value_name);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
2021-04-28 09:19:31 +00:00
|
|
|
if (i || recursing)
|
2021-03-17 12:38:04 +00:00
|
|
|
output_string(newlineW);
|
|
|
|
|
2021-04-28 09:19:32 +00:00
|
|
|
if (!num_subkeys)
|
|
|
|
return 0;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
subkey_name = malloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
|
2021-03-17 12:38:04 +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);
|
2021-03-17 12:38:04 +00:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if (recurse)
|
|
|
|
{
|
|
|
|
subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
|
2021-04-28 09:19:28 +00:00
|
|
|
if (!RegOpenKeyExW(hkey, subkey_name, 0, KEY_READ, &subkey))
|
2021-03-17 12:38:04 +00:00
|
|
|
{
|
2021-04-28 09:19:30 +00:00
|
|
|
query_all(subkey, subkey_path, recurse, TRUE);
|
2021-03-17 12:38:04 +00:00
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
2021-03-24 08:08:58 +00:00
|
|
|
free(subkey_path);
|
2021-03-17 12:38:04 +00:00
|
|
|
}
|
2021-03-30 12:00:16 +00:00
|
|
|
else output_string(L"%1\\%2\n", path, subkey_name);
|
2021-03-17 12:38:04 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(subkey_name);
|
2021-03-17 12:38:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-26 12:30:04 +00:00
|
|
|
static int run_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
|
2021-03-17 12:38:04 +00:00
|
|
|
BOOL value_empty, BOOL recurse)
|
|
|
|
{
|
2021-04-28 09:19:28 +00:00
|
|
|
HKEY hkey;
|
2021-03-17 12:38:04 +00:00
|
|
|
int ret;
|
|
|
|
|
2021-04-28 09:19:28 +00:00
|
|
|
if (RegOpenKeyExW(root, path, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
|
2021-03-17 12:38:04 +00:00
|
|
|
{
|
2021-04-01 12:20:49 +00:00
|
|
|
output_message(STRING_KEY_NONEXIST);
|
2021-03-17 12:38:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
output_string(newlineW);
|
|
|
|
|
|
|
|
if (value_name || value_empty)
|
|
|
|
{
|
2021-04-28 09:19:28 +00:00
|
|
|
ret = query_value(hkey, value_name, key_name, recurse);
|
2021-03-17 12:38:04 +00:00
|
|
|
if (recurse)
|
|
|
|
output_message(STRING_MATCHES_FOUND, num_values_found);
|
|
|
|
}
|
|
|
|
else
|
2021-04-28 09:19:30 +00:00
|
|
|
ret = query_all(hkey, key_name, recurse, FALSE);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
2021-04-28 09:19:28 +00:00
|
|
|
RegCloseKey(hkey);
|
2021-03-17 12:38:04 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2021-03-26 12:30:04 +00:00
|
|
|
|
|
|
|
int reg_query(int argc, WCHAR *argvW[])
|
|
|
|
{
|
|
|
|
HKEY root;
|
|
|
|
WCHAR *path, *key_name, *value_name = NULL;
|
|
|
|
BOOL value_empty = FALSE, recurse = FALSE;
|
|
|
|
int i;
|
|
|
|
|
2021-04-05 13:24:11 +00:00
|
|
|
if (!parse_registry_key(argvW[2], &root, &path))
|
2021-03-26 12:30:04 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (i = 3; i < argc; i++)
|
|
|
|
{
|
2021-04-02 11:26:55 +00:00
|
|
|
WCHAR *str;
|
2021-03-26 12:30:04 +00:00
|
|
|
|
2021-04-02 11:26:55 +00:00
|
|
|
if (argvW[i][0] != '/' && argvW[i][0] != '-')
|
|
|
|
goto invalid;
|
2021-03-26 12:30:04 +00:00
|
|
|
|
2021-04-02 11:26:55 +00:00
|
|
|
str = &argvW[i][1];
|
|
|
|
|
|
|
|
if (!lstrcmpiW(str, L"ve"))
|
|
|
|
{
|
|
|
|
if (value_empty) goto invalid;
|
|
|
|
value_empty = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-08 12:10:54 +00:00
|
|
|
else if (!lstrcmpiW(str, L"reg:32") || !lstrcmpiW(str, L"reg:64"))
|
|
|
|
continue;
|
2021-04-02 11:26:55 +00:00
|
|
|
else if (!str[0] || str[1])
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
switch (towlower(*str))
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
if (value_name || !(value_name = argvW[++i]))
|
2021-03-26 12:30:04 +00:00
|
|
|
goto invalid;
|
2021-04-02 11:26:55 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (recurse) goto invalid;
|
|
|
|
recurse = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto invalid;
|
2021-03-26 12:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value_name && value_empty)
|
|
|
|
goto invalid;
|
|
|
|
|
2021-04-05 13:24:11 +00:00
|
|
|
key_name = get_long_key(root, path);
|
|
|
|
|
2021-03-26 12:30:04 +00:00
|
|
|
return run_query(root, path, key_name, value_name, value_empty, recurse);
|
|
|
|
|
|
|
|
invalid:
|
2021-04-08 12:10:53 +00:00
|
|
|
output_message(STRING_INVALID_SYNTAX);
|
|
|
|
output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
|
2021-03-26 12:30:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|