2008-03-19 07:36:28 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Andrew Riedi
|
|
|
|
*
|
|
|
|
* 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 <windows.h>
|
2017-08-01 14:30:51 +02:00
|
|
|
#include <errno.h>
|
2019-04-01 11:44:50 +02:00
|
|
|
#include <stdio.h>
|
2017-08-01 14:30:51 +02:00
|
|
|
#include <stdlib.h>
|
2016-02-09 12:54:03 +01:00
|
|
|
#include <wine/debug.h>
|
2018-02-07 23:48:40 +01:00
|
|
|
#include <wine/heap.h>
|
2017-08-18 11:29:54 +02:00
|
|
|
#include "reg.h"
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:03 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(reg);
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
static const WCHAR short_hklm[] = {'H','K','L','M',0};
|
|
|
|
static const WCHAR short_hkcu[] = {'H','K','C','U',0};
|
|
|
|
static const WCHAR short_hkcr[] = {'H','K','C','R',0};
|
|
|
|
static const WCHAR short_hku[] = {'H','K','U',0};
|
|
|
|
static const WCHAR short_hkcc[] = {'H','K','C','C',0};
|
|
|
|
static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
|
|
|
|
static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
|
|
|
|
static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
|
|
|
|
static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
|
|
|
|
static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
HKEY key;
|
|
|
|
const WCHAR *short_name;
|
|
|
|
const WCHAR *long_name;
|
|
|
|
}
|
|
|
|
root_rels[] =
|
|
|
|
{
|
|
|
|
{HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
|
|
|
|
{HKEY_CURRENT_USER, short_hkcu, long_hkcu},
|
|
|
|
{HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
|
|
|
|
{HKEY_USERS, short_hku, long_hku},
|
|
|
|
{HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
|
|
|
|
};
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
|
|
|
|
static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
|
|
|
|
static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
|
|
|
|
static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
|
|
|
|
static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
|
|
|
|
static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
|
|
|
|
static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
|
|
|
|
static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
DWORD type;
|
|
|
|
const WCHAR *name;
|
|
|
|
}
|
|
|
|
type_rels[] =
|
|
|
|
{
|
|
|
|
{REG_NONE, type_none},
|
|
|
|
{REG_SZ, type_sz},
|
|
|
|
{REG_EXPAND_SZ, type_expand_sz},
|
|
|
|
{REG_BINARY, type_binary},
|
|
|
|
{REG_DWORD, type_dword},
|
|
|
|
{REG_DWORD_LITTLE_ENDIAN, type_dword_le},
|
|
|
|
{REG_DWORD_BIG_ENDIAN, type_dword_be},
|
|
|
|
{REG_MULTI_SZ, type_multi_sz},
|
|
|
|
};
|
|
|
|
|
2017-12-05 12:25:15 +01:00
|
|
|
static const WCHAR newlineW[] = {'\n',0};
|
|
|
|
|
2017-08-18 11:30:04 +02:00
|
|
|
void *heap_xalloc(size_t size)
|
2017-08-01 14:30:51 +02:00
|
|
|
{
|
2018-02-07 23:48:40 +01:00
|
|
|
void *buf = heap_alloc(size);
|
2017-08-01 14:30:51 +02:00
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
ERR("Out of memory!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-08-18 11:30:04 +02:00
|
|
|
void *heap_xrealloc(void *buf, size_t size)
|
2017-08-01 14:31:09 +02:00
|
|
|
{
|
2018-02-07 23:48:40 +01:00
|
|
|
void *new_buf = heap_realloc(buf, size);
|
2017-08-01 14:31:09 +02:00
|
|
|
|
|
|
|
if (!new_buf)
|
|
|
|
{
|
|
|
|
ERR("Out of memory!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_buf;
|
|
|
|
}
|
|
|
|
|
2017-12-03 07:50:15 +01:00
|
|
|
void output_writeconsole(const WCHAR *str, DWORD wlen)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
|
|
|
DWORD count, ret;
|
|
|
|
|
2016-02-09 12:54:02 +01:00
|
|
|
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
|
2008-03-19 07:36:28 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *msgA;
|
|
|
|
|
2011-08-24 14:29:31 +02:00
|
|
|
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
|
|
|
|
* back to WriteFile(), assuming the console encoding is still the right
|
|
|
|
* one in that case.
|
|
|
|
*/
|
2016-02-09 12:54:02 +01:00
|
|
|
len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
|
2017-08-01 14:30:51 +02:00
|
|
|
msgA = heap_xalloc(len);
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:02 +01:00
|
|
|
WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
|
2008-03-19 07:36:28 +01:00
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(msgA);
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
2016-02-09 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
|
|
|
|
{
|
2016-02-09 12:54:03 +01:00
|
|
|
WCHAR *str;
|
|
|
|
DWORD len;
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:03 +01:00
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
|
|
|
|
if (len == 0 && GetLastError() != NO_ERROR)
|
|
|
|
{
|
|
|
|
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
output_writeconsole(str, len);
|
|
|
|
LocalFree(str);
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 10:21:30 +01:00
|
|
|
void WINAPIV output_message(unsigned int id, ...)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
WCHAR fmt[1024];
|
|
|
|
__ms_va_list va_args;
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:04 +01:00
|
|
|
if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
|
|
|
|
{
|
|
|
|
WINE_FIXME("LoadString failed with %d\n", GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
__ms_va_start(va_args, id);
|
|
|
|
output_formatstring(fmt, va_args);
|
|
|
|
__ms_va_end(va_args);
|
2016-02-09 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 10:21:30 +01:00
|
|
|
static void WINAPIV output_string(const WCHAR *fmt, ...)
|
2016-02-09 12:54:02 +01:00
|
|
|
{
|
|
|
|
__ms_va_list va_args;
|
|
|
|
|
|
|
|
__ms_va_start(va_args, fmt);
|
|
|
|
output_formatstring(fmt, va_args);
|
|
|
|
__ms_va_end(va_args);
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 12:54:07 +01:00
|
|
|
/* ask_confirm() adapted from programs/cmd/builtins.c */
|
2017-12-03 07:50:15 +01:00
|
|
|
BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
|
2016-02-09 12:54:07 +01:00
|
|
|
{
|
|
|
|
HMODULE hmod;
|
|
|
|
WCHAR Ybuffer[4];
|
|
|
|
WCHAR Nbuffer[4];
|
2016-02-10 12:23:17 +01:00
|
|
|
WCHAR defval[32];
|
2016-02-09 12:54:07 +01:00
|
|
|
WCHAR answer[MAX_PATH];
|
2016-03-02 11:31:19 +01:00
|
|
|
WCHAR *str;
|
2016-02-09 12:54:07 +01:00
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
hmod = GetModuleHandleW(NULL);
|
|
|
|
LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
|
|
|
|
LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
|
2016-02-10 12:23:17 +01:00
|
|
|
LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
|
2016-02-09 12:54:07 +01:00
|
|
|
|
2016-05-24 14:48:15 +02:00
|
|
|
str = (reg_info && *reg_info) ? reg_info : defval;
|
2016-03-02 11:31:19 +01:00
|
|
|
|
2016-02-09 12:54:07 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2016-03-02 11:31:19 +01:00
|
|
|
output_message(msgid, str);
|
2016-02-09 12:54:07 +01:00
|
|
|
output_message(STRING_YESNO);
|
|
|
|
ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
|
2019-04-01 11:44:50 +02:00
|
|
|
answer[0] = towupper(answer[0]);
|
2016-02-09 12:54:07 +01:00
|
|
|
if (answer[0] == Ybuffer[0])
|
|
|
|
return TRUE;
|
|
|
|
if (answer[0] == Nbuffer[0])
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
|
2014-11-19 08:54:41 +01:00
|
|
|
{
|
2019-04-01 11:44:50 +02:00
|
|
|
DWORD length = lstrlenW(rootkey_name);
|
2014-11-19 08:54:41 +01:00
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
return (!wcsnicmp(input_path, rootkey_name, length) &&
|
2015-10-04 21:14:46 +02:00
|
|
|
(input_path[length] == 0 || input_path[length] == '\\'));
|
2014-11-19 08:54:41 +01:00
|
|
|
}
|
|
|
|
|
2017-08-18 11:30:04 +02:00
|
|
|
HKEY path_get_rootkey(const WCHAR *path)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2015-10-04 21:14:46 +02:00
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(root_rels); i++)
|
|
|
|
{
|
|
|
|
if (path_rootname_cmp(path, root_rels[i].short_name) ||
|
|
|
|
path_rootname_cmp(path, root_rels[i].long_name))
|
|
|
|
return root_rels[i].key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
static DWORD wchar_get_type(const WCHAR *type_name)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2015-02-19 13:19:12 +01:00
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (!type_name)
|
2009-01-28 14:14:07 +01:00
|
|
|
return REG_SZ;
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
for (i = 0; i < ARRAY_SIZE(type_rels); i++)
|
|
|
|
{
|
2019-04-11 11:50:18 +02:00
|
|
|
if (!wcsicmp(type_rels[i].name, type_name))
|
2015-02-19 13:19:12 +01:00
|
|
|
return type_rels[i].type;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
return ~0u;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-09-06 16:07:18 +02:00
|
|
|
/* hexchar_to_byte from programs/regedit/hexedit.c */
|
|
|
|
static inline BYTE hexchar_to_byte(WCHAR ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
return ch - '0';
|
|
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
|
|
return ch - 'a' + 10;
|
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
|
|
return ch - 'A' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-15 09:58:56 +01:00
|
|
|
static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2016-03-15 09:58:56 +01:00
|
|
|
static const WCHAR empty;
|
2009-01-28 14:14:07 +01:00
|
|
|
LPBYTE out_data = NULL;
|
|
|
|
*reg_count = 0;
|
|
|
|
|
2016-03-15 09:58:56 +01:00
|
|
|
if (!data) data = ∅
|
|
|
|
|
2009-01-28 14:14:07 +01:00
|
|
|
switch (reg_type)
|
|
|
|
{
|
2016-02-15 13:15:31 +01:00
|
|
|
case REG_NONE:
|
2009-01-28 14:14:07 +01:00
|
|
|
case REG_SZ:
|
2016-02-15 13:15:32 +01:00
|
|
|
case REG_EXPAND_SZ:
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
|
|
|
*reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
|
2017-08-01 14:30:51 +02:00
|
|
|
out_data = heap_xalloc(*reg_count);
|
2009-01-28 14:14:07 +01:00
|
|
|
lstrcpyW((LPWSTR)out_data,data);
|
|
|
|
break;
|
|
|
|
}
|
2010-02-03 15:04:55 +01:00
|
|
|
case REG_DWORD:
|
2016-02-15 13:15:33 +01:00
|
|
|
/* case REG_DWORD_LITTLE_ENDIAN: */
|
|
|
|
case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
|
2010-02-03 15:04:55 +01:00
|
|
|
{
|
|
|
|
LPWSTR rest;
|
2016-08-18 11:59:53 +02:00
|
|
|
unsigned long val;
|
2019-04-01 11:44:50 +02:00
|
|
|
val = wcstoul(data, &rest, (towlower(data[1]) == 'x') ? 16 : 10);
|
2017-08-18 11:29:35 +02:00
|
|
|
if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_MISSING_INTEGER);
|
2010-02-03 15:04:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*reg_count = sizeof(DWORD);
|
2017-08-01 14:30:51 +02:00
|
|
|
out_data = heap_xalloc(*reg_count);
|
2010-02-03 15:04:55 +01:00
|
|
|
((LPDWORD)out_data)[0] = val;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-06 16:07:18 +02:00
|
|
|
case REG_BINARY:
|
|
|
|
{
|
|
|
|
BYTE hex0, hex1;
|
|
|
|
int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
|
|
|
|
*reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
|
2017-08-01 14:30:51 +02:00
|
|
|
out_data = heap_xalloc(*reg_count);
|
2015-09-06 16:07:18 +02:00
|
|
|
if(datalen % 2)
|
|
|
|
{
|
|
|
|
hex1 = hexchar_to_byte(data[i++]);
|
|
|
|
if(hex1 == 0xFF)
|
|
|
|
goto no_hex_data;
|
|
|
|
out_data[destByteIndex++] = hex1;
|
|
|
|
}
|
|
|
|
for(;i + 1 < datalen;i += 2)
|
|
|
|
{
|
|
|
|
hex0 = hexchar_to_byte(data[i]);
|
|
|
|
hex1 = hexchar_to_byte(data[i + 1]);
|
|
|
|
if(hex0 == 0xFF || hex1 == 0xFF)
|
|
|
|
goto no_hex_data;
|
|
|
|
out_data[destByteIndex++] = (hex0 << 4) | hex1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
no_hex_data:
|
|
|
|
/* cleanup, print error */
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(out_data);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_MISSING_HEXDATA);
|
2015-09-06 16:07:18 +02:00
|
|
|
out_data = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-15 13:15:34 +01:00
|
|
|
case REG_MULTI_SZ:
|
2016-02-24 05:30:20 +01:00
|
|
|
{
|
2019-04-01 11:44:50 +02:00
|
|
|
int i, destindex, len = lstrlenW(data);
|
2017-08-01 14:30:51 +02:00
|
|
|
WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
|
2016-02-24 05:30:20 +01:00
|
|
|
|
|
|
|
for (i = 0, destindex = 0; i < len; i++, destindex++)
|
|
|
|
{
|
2016-02-25 09:54:09 +01:00
|
|
|
if (!separator && data[i] == '\\' && data[i + 1] == '0')
|
2016-02-24 05:30:20 +01:00
|
|
|
{
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (data[i] == separator)
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
else
|
|
|
|
buffer[destindex] = data[i];
|
2016-02-29 05:54:21 +01:00
|
|
|
|
2016-02-29 05:54:22 +01:00
|
|
|
if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
|
2016-02-29 05:54:21 +01:00
|
|
|
{
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(buffer);
|
2016-02-29 05:54:21 +01:00
|
|
|
output_message(STRING_INVALID_STRING);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-02-24 05:30:20 +01:00
|
|
|
}
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
if (destindex && buffer[destindex - 1])
|
|
|
|
buffer[++destindex] = 0;
|
|
|
|
*reg_count = (destindex + 1) * sizeof(WCHAR);
|
|
|
|
return (BYTE *)buffer;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
default:
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_UNHANDLED_TYPE, reg_type, data);
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return out_data;
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:10 +01:00
|
|
|
static BOOL sane_path(const WCHAR *key)
|
|
|
|
{
|
2019-04-01 11:44:50 +02:00
|
|
|
unsigned int i = lstrlenW(key);
|
2015-02-19 13:19:11 +01:00
|
|
|
|
|
|
|
if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2015-02-19 13:19:11 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:10 +01:00
|
|
|
if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_NO_REMOTE);
|
2015-02-19 13:19:10 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:45 +02:00
|
|
|
static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
|
2016-06-03 07:24:23 +02:00
|
|
|
WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
HKEY key;
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2016-06-06 07:31:45 +02:00
|
|
|
if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 02:59:27 +01:00
|
|
|
if (value_name || value_empty || data)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
|
|
|
DWORD reg_type;
|
|
|
|
DWORD reg_count = 0;
|
|
|
|
BYTE* reg_data = NULL;
|
|
|
|
|
|
|
|
if (!force)
|
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2016-02-09 12:54:07 +01:00
|
|
|
if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
|
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-09 12:54:07 +01:00
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
reg_type = wchar_get_type(type);
|
|
|
|
if (reg_type == ~0u)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-09 12:54:05 +01:00
|
|
|
output_message(STRING_UNSUPPORTED_TYPE, type);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
2016-03-04 02:59:28 +01:00
|
|
|
if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
|
2016-02-15 13:15:30 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-15 13:15:30 +01:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2016-03-15 09:58:56 +01:00
|
|
|
if (!(reg_data = get_regdata(data, reg_type, separator, ®_count)))
|
2016-02-15 13:15:35 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-15 13:15:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2016-06-06 07:31:35 +02:00
|
|
|
RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(reg_data);
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2009-07-01 07:41:41 +02:00
|
|
|
return 0;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:45 +02:00
|
|
|
static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
|
|
|
|
BOOL value_empty, BOOL value_all, BOOL force)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
HKEY key;
|
2009-01-28 14:14:18 +01:00
|
|
|
|
|
|
|
if (!force)
|
|
|
|
{
|
2016-02-10 12:23:18 +01:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (value_name || value_empty)
|
|
|
|
ret = ask_confirm(STRING_DELETE_VALUE, value_name);
|
|
|
|
else if (value_all)
|
|
|
|
ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
|
|
|
|
else
|
|
|
|
ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
2009-12-06 22:45:48 +01:00
|
|
|
/* Delete subtree only if no /v* option is given */
|
|
|
|
if (!value_name && !value_empty && !value_all)
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
2016-06-06 07:31:45 +02:00
|
|
|
if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_CANNOT_FIND);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 0;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:45 +02:00
|
|
|
if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_CANNOT_FIND);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value_all)
|
|
|
|
{
|
2017-08-07 14:28:44 +02:00
|
|
|
DWORD max_value_len = 256, value_len;
|
|
|
|
WCHAR *value_name;
|
2009-01-28 14:14:18 +01:00
|
|
|
LONG rc;
|
|
|
|
|
2017-08-07 14:28:44 +02:00
|
|
|
value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
|
2009-01-28 14:14:18 +01:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2017-08-07 14:28:44 +02:00
|
|
|
value_len = max_value_len;
|
|
|
|
rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
|
2009-01-28 14:14:18 +01:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
2017-08-07 14:28:44 +02:00
|
|
|
rc = RegDeleteValueW(key, value_name);
|
2009-01-28 14:14:18 +01:00
|
|
|
if (rc != ERROR_SUCCESS)
|
2016-03-31 11:26:56 +02:00
|
|
|
{
|
2017-08-07 14:28:44 +02:00
|
|
|
heap_free(value_name);
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-03-31 11:26:56 +02:00
|
|
|
output_message(STRING_VALUEALL_FAILED, key_name);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
2017-08-07 14:28:44 +02:00
|
|
|
else if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
max_value_len *= 2;
|
|
|
|
value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
|
|
|
|
}
|
2009-01-28 14:14:18 +01:00
|
|
|
else break;
|
|
|
|
}
|
2017-08-07 14:28:44 +02:00
|
|
|
heap_free(value_name);
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
2016-03-02 11:31:18 +01:00
|
|
|
else if (value_name || value_empty)
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_CANNOT_FIND);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:35 +02:00
|
|
|
RegCloseKey(key);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 0;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-04-21 13:46:15 +02:00
|
|
|
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:
|
2017-08-01 14:30:51 +02:00
|
|
|
buffer = heap_xalloc(size_bytes);
|
2019-04-01 11:44:50 +02:00
|
|
|
lstrcpyW(buffer, (WCHAR *)src);
|
2016-04-21 13:46:15 +02:00
|
|
|
break;
|
|
|
|
case REG_NONE:
|
|
|
|
case REG_BINARY:
|
|
|
|
{
|
|
|
|
WCHAR *ptr;
|
2017-08-10 14:06:38 +02:00
|
|
|
static const WCHAR fmt[] = {'%','0','2','X',0};
|
2016-04-21 13:46:15 +02:00
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
|
2016-04-21 13:46:15 +02:00
|
|
|
ptr = buffer;
|
|
|
|
for (i = 0; i < size_bytes; i++)
|
2019-04-11 10:04:28 +02:00
|
|
|
ptr += swprintf(ptr, 3, fmt, src[i]);
|
2016-04-21 13:46:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REG_DWORD:
|
|
|
|
/* case REG_DWORD_LITTLE_ENDIAN: */
|
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
{
|
|
|
|
const int zero_x_dword = 10;
|
2017-08-10 14:06:38 +02:00
|
|
|
static const WCHAR fmt[] = {'0','x','%','x',0};
|
2016-04-21 13:46:15 +02:00
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
swprintf(buffer, zero_x_dword + 1, fmt, *(DWORD *)src);
|
2016-04-21 13:46:15 +02: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)
|
|
|
|
{
|
2017-08-01 14:30:51 +02:00
|
|
|
buffer = heap_xalloc(sizeof(WCHAR));
|
2016-04-21 13:46:15 +02:00
|
|
|
*buffer = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
|
2017-08-01 14:30:51 +02:00
|
|
|
buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
|
2016-04-21 13:46:15 +02: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;
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:45:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:46:15 +02:00
|
|
|
static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
|
2016-04-21 13:45:54 +02:00
|
|
|
{
|
2017-08-10 14:06:38 +02:00
|
|
|
static const WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
|
2016-07-21 09:36:44 +02:00
|
|
|
WCHAR defval[32];
|
2016-04-21 13:46:15 +02:00
|
|
|
WCHAR *reg_data;
|
2016-04-21 13:45:54 +02:00
|
|
|
|
2016-04-21 13:46:05 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-04-21 13:45:54 +02:00
|
|
|
output_string(fmt, reg_type_to_wchar(type));
|
2016-07-21 09:36:44 +02:00
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
reg_data = reg_data_to_wchar(type, data, data_size);
|
|
|
|
output_string(fmt, reg_data);
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(reg_data);
|
2016-07-21 09:36:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
|
|
|
|
output_string(fmt, defval);
|
|
|
|
}
|
2016-04-21 13:45:54 +02:00
|
|
|
output_string(newlineW);
|
|
|
|
}
|
|
|
|
|
2017-12-03 07:50:24 +01:00
|
|
|
WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
|
2016-04-25 13:26:38 +02:00
|
|
|
{
|
|
|
|
WCHAR *subkey_path;
|
2017-08-10 14:06:38 +02:00
|
|
|
static const WCHAR fmt[] = {'%','s','\\','%','s',0};
|
2016-04-25 13:26:38 +02:00
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
swprintf(subkey_path, path_len + subkey_len + 2, fmt, path, subkey_name);
|
2017-08-01 14:30:51 +02:00
|
|
|
|
2016-04-25 13:26:38 +02:00
|
|
|
return subkey_path;
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:06:23 +02:00
|
|
|
static unsigned int num_values_found = 0;
|
|
|
|
|
2016-04-26 14:28:24 +02:00
|
|
|
static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
|
2016-04-25 13:26:49 +02:00
|
|
|
{
|
|
|
|
LONG rc;
|
2017-07-20 13:33:33 +02:00
|
|
|
DWORD max_data_bytes = 2048, data_size;
|
|
|
|
DWORD subkey_len;
|
2016-04-26 14:28:24 +02:00
|
|
|
DWORD type, path_len, i;
|
2016-04-25 13:26:49 +02:00
|
|
|
BYTE *data;
|
|
|
|
WCHAR fmt[] = {'%','1','\n',0};
|
2016-04-26 14:28:24 +02:00
|
|
|
WCHAR *subkey_name, *subkey_path;
|
|
|
|
HKEY subkey;
|
2016-04-25 13:26:49 +02:00
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
data = heap_xalloc(max_data_bytes);
|
2016-04-25 13:26:49 +02:00
|
|
|
|
2017-07-20 13:33:33 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
data_size = max_data_bytes;
|
|
|
|
rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
|
|
|
|
if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
max_data_bytes = data_size;
|
2017-08-01 14:31:09 +02:00
|
|
|
data = heap_xrealloc(data, max_data_bytes);
|
2017-07-20 13:33:33 +02:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2016-04-25 13:26:49 +02:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
output_string(fmt, path);
|
|
|
|
output_value(value_name, type, data, data_size);
|
|
|
|
output_string(newlineW);
|
2016-05-02 14:06:23 +02:00
|
|
|
num_values_found++;
|
2016-04-25 13:26:49 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(data);
|
2016-04-25 13:26:49 +02:00
|
|
|
|
2016-04-26 14:28:24 +02:00
|
|
|
if (!recurse)
|
2016-04-25 13:26:49 +02:00
|
|
|
{
|
2016-04-26 14:28:24 +02:00
|
|
|
if (rc == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
2016-07-21 09:36:44 +02:00
|
|
|
if (value_name && *value_name)
|
|
|
|
{
|
|
|
|
output_message(STRING_CANNOT_FIND);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
output_string(fmt, path);
|
|
|
|
output_value(NULL, REG_SZ, NULL, 0);
|
2016-04-26 14:28:24 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
|
2016-04-26 14:28:24 +02:00
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
path_len = lstrlenW(path);
|
2016-04-26 14:28:24 +02:00
|
|
|
|
2017-07-20 13:33:33 +02:00
|
|
|
i = 0;
|
|
|
|
for (;;)
|
2016-04-26 14:28:24 +02:00
|
|
|
{
|
2017-07-20 13:33:33 +02:00
|
|
|
subkey_len = MAX_SUBKEY_LEN;
|
2016-04-26 14:28:24 +02:00
|
|
|
rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
|
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
|
|
|
|
if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
|
|
|
|
{
|
|
|
|
query_value(subkey, value_name, subkey_path, recurse);
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(subkey_path);
|
2017-07-20 13:33:33 +02:00
|
|
|
i++;
|
2016-04-26 14:28:24 +02:00
|
|
|
}
|
2017-07-20 13:33:33 +02:00
|
|
|
else break;
|
2016-04-26 14:28:24 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(subkey_name);
|
2016-04-25 13:26:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-25 13:26:38 +02:00
|
|
|
static int query_all(HKEY key, WCHAR *path, BOOL recurse)
|
2016-04-21 13:45:21 +02:00
|
|
|
{
|
|
|
|
LONG rc;
|
2017-07-20 13:33:33 +02:00
|
|
|
DWORD max_value_len = 256, value_len;
|
|
|
|
DWORD max_data_bytes = 2048, data_size;
|
|
|
|
DWORD subkey_len;
|
2016-04-25 13:26:38 +02:00
|
|
|
DWORD i, type, path_len;
|
2016-04-21 13:45:21 +02:00
|
|
|
WCHAR fmt[] = {'%','1','\n',0};
|
2016-04-21 13:45:33 +02:00
|
|
|
WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
|
2016-04-25 13:26:38 +02:00
|
|
|
WCHAR *value_name, *subkey_name, *subkey_path;
|
2016-04-21 13:46:15 +02:00
|
|
|
BYTE *data;
|
2016-04-25 13:26:38 +02:00
|
|
|
HKEY subkey;
|
2016-04-21 13:45:21 +02:00
|
|
|
|
|
|
|
output_string(fmt, path);
|
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
|
|
|
|
data = heap_xalloc(max_data_bytes);
|
2016-04-21 13:46:15 +02:00
|
|
|
|
2017-07-20 13:33:33 +02:00
|
|
|
i = 0;
|
|
|
|
for (;;)
|
2016-04-21 13:45:21 +02:00
|
|
|
{
|
|
|
|
value_len = max_value_len;
|
2016-04-21 13:46:15 +02:00
|
|
|
data_size = max_data_bytes;
|
|
|
|
rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
|
2016-04-21 13:45:21 +02:00
|
|
|
if (rc == ERROR_SUCCESS)
|
2017-07-20 13:33:33 +02:00
|
|
|
{
|
2016-04-21 13:46:15 +02:00
|
|
|
output_value(value_name, type, data, data_size);
|
2017-07-20 13:33:33 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
if (data_size > max_data_bytes)
|
|
|
|
{
|
|
|
|
max_data_bytes = data_size;
|
2017-08-01 14:31:09 +02:00
|
|
|
data = heap_xrealloc(data, max_data_bytes);
|
2017-07-20 13:33:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_value_len *= 2;
|
2017-08-01 14:31:09 +02:00
|
|
|
value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
|
2017-07-20 13:33:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else break;
|
2016-04-21 13:45:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(data);
|
|
|
|
heap_free(value_name);
|
2016-04-21 13:45:21 +02:00
|
|
|
|
2017-07-20 13:33:33 +02:00
|
|
|
if (i || recurse)
|
2016-04-21 13:45:21 +02:00
|
|
|
output_string(newlineW);
|
|
|
|
|
2017-08-01 14:30:51 +02:00
|
|
|
subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
|
2016-04-21 13:45:33 +02:00
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
path_len = lstrlenW(path);
|
2016-04-25 13:26:38 +02:00
|
|
|
|
2017-07-20 13:33:33 +02:00
|
|
|
i = 0;
|
|
|
|
for (;;)
|
2016-04-21 13:45:33 +02:00
|
|
|
{
|
2017-07-20 13:33:33 +02:00
|
|
|
subkey_len = MAX_SUBKEY_LEN;
|
2016-04-21 13:45:33 +02:00
|
|
|
rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
|
|
|
|
if (rc == ERROR_SUCCESS)
|
2016-04-25 13:26:38 +02:00
|
|
|
{
|
|
|
|
if (recurse)
|
|
|
|
{
|
|
|
|
subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
|
|
|
|
if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
|
|
|
|
{
|
|
|
|
query_all(subkey, subkey_path, recurse);
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(subkey_path);
|
2016-04-25 13:26:38 +02:00
|
|
|
}
|
|
|
|
else output_string(fmt_path, path, subkey_name);
|
2017-07-20 13:33:33 +02:00
|
|
|
i++;
|
2016-04-25 13:26:38 +02:00
|
|
|
}
|
2017-07-20 13:33:33 +02:00
|
|
|
else break;
|
2016-04-21 13:45:33 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 14:31:00 +02:00
|
|
|
heap_free(subkey_name);
|
2016-04-21 13:45:33 +02:00
|
|
|
|
2017-07-20 13:33:33 +02:00
|
|
|
if (i && !recurse)
|
2016-04-21 13:45:33 +02:00
|
|
|
output_string(newlineW);
|
|
|
|
|
2016-04-21 13:45:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:45 +02:00
|
|
|
static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
|
|
|
|
BOOL value_empty, BOOL recurse)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-06-03 07:24:23 +02:00
|
|
|
HKEY key;
|
2016-04-21 13:45:21 +02:00
|
|
|
int ret;
|
2016-04-21 13:45:11 +02:00
|
|
|
|
2016-06-06 07:31:45 +02:00
|
|
|
if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
|
2016-04-21 13:45:21 +02:00
|
|
|
{
|
|
|
|
output_message(STRING_CANNOT_FIND);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:06:33 +02:00
|
|
|
output_string(newlineW);
|
|
|
|
|
2016-04-25 13:26:38 +02:00
|
|
|
if (value_name || value_empty)
|
2016-05-02 14:06:23 +02:00
|
|
|
{
|
2016-04-26 14:28:24 +02:00
|
|
|
ret = query_value(key, value_name, key_name, recurse);
|
2016-05-02 14:06:23 +02:00
|
|
|
if (recurse)
|
|
|
|
output_message(STRING_MATCHES_FOUND, num_values_found);
|
|
|
|
}
|
2016-04-25 13:26:49 +02:00
|
|
|
else
|
|
|
|
ret = query_all(key, key_name, recurse);
|
2016-04-21 13:45:21 +02:00
|
|
|
|
|
|
|
RegCloseKey(key);
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-04-21 13:45:21 +02:00
|
|
|
return ret;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 04:31:05 +02:00
|
|
|
static WCHAR *get_long_key(HKEY root, WCHAR *path)
|
|
|
|
{
|
|
|
|
DWORD i, array_size = ARRAY_SIZE(root_rels), len;
|
|
|
|
WCHAR *long_key;
|
|
|
|
WCHAR fmt[] = {'%','s','\\','%','s',0};
|
|
|
|
|
|
|
|
for (i = 0; i < array_size; i++)
|
|
|
|
{
|
|
|
|
if (root == root_rels[i].key)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
len = lstrlenW(root_rels[i].long_name);
|
2016-06-15 04:31:05 +02:00
|
|
|
|
|
|
|
if (!path)
|
|
|
|
{
|
2017-08-01 14:30:51 +02:00
|
|
|
long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
|
2019-04-01 11:44:50 +02:00
|
|
|
lstrcpyW(long_key, root_rels[i].long_name);
|
2016-06-15 04:31:05 +02:00
|
|
|
return long_key;
|
|
|
|
}
|
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
len += lstrlenW(path) + 1; /* add one for the backslash */
|
2017-08-01 14:30:51 +02:00
|
|
|
long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
swprintf(long_key, len + 1, fmt, root_rels[i].long_name, path);
|
2016-06-15 04:31:05 +02:00
|
|
|
return long_key;
|
|
|
|
}
|
|
|
|
|
2017-12-03 07:50:11 +01:00
|
|
|
BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
|
2016-06-15 04:30:53 +02:00
|
|
|
{
|
|
|
|
if (!sane_path(key))
|
|
|
|
return FALSE;
|
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
*path = wcschr(key, '\\');
|
2017-12-03 07:50:12 +01:00
|
|
|
if (*path) (*path)++;
|
|
|
|
|
2016-06-15 04:30:53 +02:00
|
|
|
*root = path_get_rootkey(key);
|
|
|
|
if (!*root)
|
|
|
|
{
|
2017-12-03 07:50:12 +01:00
|
|
|
if (*path) *(*path - 1) = 0;
|
|
|
|
output_message(STRING_INVALID_SYSTEM_KEY, key);
|
2016-06-15 04:30:53 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-15 04:31:05 +02:00
|
|
|
*long_key = get_long_key(*root, *path);
|
|
|
|
|
2016-06-15 04:30:53 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-12-05 12:25:19 +01:00
|
|
|
static BOOL is_switch(const WCHAR *s, const WCHAR c)
|
2016-06-03 07:23:17 +02:00
|
|
|
{
|
2019-04-01 11:44:50 +02:00
|
|
|
if (lstrlenW(s) > 2)
|
2016-06-03 07:23:17 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
if ((s[0] == '/' || s[0] == '-') && (s[1] == c || s[1] == towupper(c)))
|
2017-12-05 12:25:19 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL is_help_switch(const WCHAR *s)
|
|
|
|
{
|
|
|
|
if (is_switch(s, '?') || is_switch(s, 'h'))
|
2016-06-03 07:23:17 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-03 07:23:27 +02:00
|
|
|
enum operations {
|
|
|
|
REG_ADD,
|
|
|
|
REG_DELETE,
|
2017-08-18 11:29:54 +02:00
|
|
|
REG_IMPORT,
|
2017-12-03 07:50:09 +01:00
|
|
|
REG_EXPORT,
|
2016-06-03 07:23:27 +02:00
|
|
|
REG_QUERY,
|
|
|
|
REG_INVALID
|
|
|
|
};
|
|
|
|
|
2016-08-18 13:54:27 +02:00
|
|
|
static enum operations get_operation(const WCHAR *str, int *op_help)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2017-08-18 11:29:54 +02:00
|
|
|
struct op_info { const WCHAR *op; int id; int help_id; };
|
2016-06-03 07:23:27 +02:00
|
|
|
|
2017-08-18 11:29:54 +02:00
|
|
|
static const WCHAR add[] = {'a','d','d',0};
|
|
|
|
static const WCHAR delete[] = {'d','e','l','e','t','e',0};
|
|
|
|
static const WCHAR import[] = {'i','m','p','o','r','t',0};
|
2017-12-03 07:50:09 +01:00
|
|
|
static const WCHAR export[] = {'e','x','p','o','r','t',0};
|
2017-08-18 11:29:54 +02:00
|
|
|
static const WCHAR query[] = {'q','u','e','r','y',0};
|
|
|
|
|
|
|
|
static const struct op_info op_array[] =
|
2016-08-18 13:54:27 +02:00
|
|
|
{
|
2017-08-18 11:29:54 +02:00
|
|
|
{ add, REG_ADD, STRING_ADD_USAGE },
|
|
|
|
{ delete, REG_DELETE, STRING_DELETE_USAGE },
|
|
|
|
{ import, REG_IMPORT, STRING_IMPORT_USAGE },
|
2017-12-03 07:50:09 +01:00
|
|
|
{ export, REG_EXPORT, STRING_EXPORT_USAGE },
|
2017-08-18 11:29:54 +02:00
|
|
|
{ query, REG_QUERY, STRING_QUERY_USAGE },
|
|
|
|
{ NULL, -1, 0 }
|
|
|
|
};
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2017-08-18 11:29:54 +02:00
|
|
|
const struct op_info *ptr;
|
|
|
|
|
|
|
|
for (ptr = op_array; ptr->op; ptr++)
|
2016-08-18 13:54:27 +02:00
|
|
|
{
|
2017-08-18 11:29:54 +02:00
|
|
|
if (!lstrcmpiW(str, ptr->op))
|
|
|
|
{
|
|
|
|
*op_help = ptr->help_id;
|
|
|
|
return ptr->id;
|
|
|
|
}
|
2016-08-18 13:54:27 +02:00
|
|
|
}
|
2016-06-03 07:23:27 +02:00
|
|
|
|
|
|
|
return REG_INVALID;
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain(int argc, WCHAR *argvW[])
|
2016-06-03 07:23:27 +02:00
|
|
|
{
|
2016-08-18 13:54:27 +02:00
|
|
|
int i, op, op_help, ret;
|
2016-06-03 07:23:51 +02:00
|
|
|
BOOL show_op_help = FALSE;
|
2016-06-08 12:48:18 +02:00
|
|
|
static const WCHAR switchVAW[] = {'v','a',0};
|
|
|
|
static const WCHAR switchVEW[] = {'v','e',0};
|
2016-06-06 07:31:55 +02:00
|
|
|
WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
|
|
|
|
BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
|
2016-06-03 07:24:23 +02:00
|
|
|
HKEY root;
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-05-24 14:48:26 +02:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_SYNTAX);
|
|
|
|
output_message(STRING_REG_HELP);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-03 07:23:17 +02:00
|
|
|
if (is_help_switch(argvW[1]))
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_USAGE);
|
2009-06-28 00:45:26 +02:00
|
|
|
return 0;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 13:54:27 +02:00
|
|
|
op = get_operation(argvW[1], &op_help);
|
2016-06-03 07:23:27 +02:00
|
|
|
|
2016-06-03 07:23:39 +02:00
|
|
|
if (op == REG_INVALID)
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_OPTION, argvW[1]);
|
|
|
|
output_message(STRING_REG_HELP);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-03 07:23:51 +02:00
|
|
|
if (argc > 2)
|
|
|
|
show_op_help = is_help_switch(argvW[2]);
|
|
|
|
|
2017-08-18 11:29:54 +02:00
|
|
|
if (argc == 2 || ((show_op_help || op == REG_IMPORT) && argc > 3))
|
2016-06-03 07:23:51 +02:00
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_SYNTAX);
|
2019-04-01 11:44:50 +02:00
|
|
|
output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
|
2016-06-03 07:23:51 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-03 07:24:00 +02:00
|
|
|
else if (show_op_help)
|
|
|
|
{
|
2016-08-18 13:54:27 +02:00
|
|
|
output_message(op_help);
|
2016-06-03 07:24:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-03 07:23:51 +02:00
|
|
|
|
2017-08-18 11:29:54 +02:00
|
|
|
if (op == REG_IMPORT)
|
|
|
|
return reg_import(argvW[2]);
|
|
|
|
|
2017-12-03 07:50:09 +01:00
|
|
|
if (op == REG_EXPORT)
|
|
|
|
return reg_export(argc, argvW);
|
|
|
|
|
2016-06-15 04:31:05 +02:00
|
|
|
if (!parse_registry_key(argvW[2], &root, &path, &key_name))
|
2016-06-03 07:24:23 +02:00
|
|
|
return 1;
|
|
|
|
|
2016-06-06 07:31:55 +02:00
|
|
|
for (i = 3; i < argc; i++)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-06-08 12:48:18 +02:00
|
|
|
if (argvW[i][0] == '/' || argvW[i][0] == '-')
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-06-08 12:48:18 +02:00
|
|
|
WCHAR *ptr = &argvW[i][1];
|
|
|
|
|
|
|
|
if (!lstrcmpiW(ptr, switchVEW))
|
2016-02-17 13:41:05 +01:00
|
|
|
{
|
2016-06-08 12:48:18 +02:00
|
|
|
value_empty = TRUE;
|
|
|
|
continue;
|
2016-02-17 13:41:05 +01:00
|
|
|
}
|
2016-06-08 12:48:18 +02:00
|
|
|
else if (!lstrcmpiW(ptr, switchVAW))
|
2016-06-08 12:47:56 +02:00
|
|
|
{
|
2016-06-08 12:48:18 +02:00
|
|
|
value_all = TRUE;
|
|
|
|
continue;
|
2016-06-08 12:47:56 +02:00
|
|
|
}
|
2016-06-09 07:48:29 +02:00
|
|
|
else if (!ptr[0] || ptr[1])
|
2016-02-11 13:34:22 +01:00
|
|
|
{
|
2016-06-06 07:31:55 +02:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
2016-02-11 13:34:22 +01:00
|
|
|
}
|
2016-06-06 07:31:55 +02:00
|
|
|
|
2019-04-01 11:44:50 +02:00
|
|
|
switch(towlower(argvW[i][1]))
|
2016-03-15 13:23:55 +01:00
|
|
|
{
|
2016-06-08 12:48:18 +02:00
|
|
|
case 'v':
|
|
|
|
if (value_name || !(value_name = argvW[++i]))
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (type || !(type = argvW[++i]))
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (data || !(data = argvW[++i]))
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (op == REG_QUERY)
|
|
|
|
{
|
|
|
|
recurse = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-06-08 12:48:18 +02:00
|
|
|
ptr = argvW[++i];
|
2019-04-01 11:44:50 +02:00
|
|
|
if (!ptr || lstrlenW(ptr) != 1)
|
2016-06-08 12:48:18 +02:00
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
separator = ptr[0];
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
2016-06-06 07:31:55 +02:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
2016-03-15 13:23:55 +01:00
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 07:31:55 +02:00
|
|
|
|
2016-06-06 07:32:16 +02:00
|
|
|
if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-06 07:31:55 +02:00
|
|
|
if (op == REG_ADD)
|
2016-06-06 07:32:05 +02:00
|
|
|
ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
|
2016-06-06 07:31:55 +02:00
|
|
|
else if (op == REG_DELETE)
|
2016-06-06 07:32:05 +02:00
|
|
|
ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
|
2017-08-10 14:06:29 +02:00
|
|
|
else
|
2016-06-06 07:32:05 +02:00
|
|
|
ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
|
|
|
|
return ret;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|