2002-04-12 01:58:40 +02:00
|
|
|
/*
|
|
|
|
* Registry processing routines. Routines, common for registry
|
|
|
|
* processing frontends.
|
|
|
|
*
|
|
|
|
* Copyright 1999 Sylvain St-Germain
|
|
|
|
* Copyright 2002 Andriy Palamarchuk
|
2008-09-14 02:21:23 +02:00
|
|
|
* Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
|
2002-04-12 01:58:40 +02:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-21 14:18:53 +02:00
|
|
|
#include <errno.h>
|
2012-01-23 12:02:39 +01:00
|
|
|
#include <stdlib.h>
|
2011-03-04 15:00:51 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
2002-04-12 01:58:40 +02:00
|
|
|
#include <windows.h>
|
2018-05-21 15:34:50 +02:00
|
|
|
#include <commctrl.h>
|
2017-07-27 14:24:49 +02:00
|
|
|
#include <wine/debug.h>
|
2018-02-07 00:20:07 +01:00
|
|
|
#include <wine/heap.h>
|
2018-05-21 15:34:50 +02:00
|
|
|
#include "main.h"
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(regedit);
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
#define REG_VAL_BUF_SIZE 4096
|
|
|
|
|
2011-01-21 11:58:31 +01:00
|
|
|
static HKEY reg_class_keys[] = {
|
2003-12-08 23:48:07 +01:00
|
|
|
HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
|
2004-07-08 22:14:10 +02:00
|
|
|
HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
|
2003-12-08 23:48:07 +01:00
|
|
|
};
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2017-07-27 14:25:17 +02:00
|
|
|
void *heap_xalloc(size_t size)
|
2017-07-27 14:24:49 +02:00
|
|
|
{
|
2018-02-07 00:20:07 +01:00
|
|
|
void *buf = heap_alloc(size);
|
2017-07-27 14:24:49 +02:00
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
ERR("Out of memory!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:25:26 +02:00
|
|
|
void *heap_xrealloc(void *buf, size_t size)
|
2017-07-27 14:25:07 +02:00
|
|
|
{
|
2018-02-07 00:20:07 +01:00
|
|
|
void *new_buf = heap_realloc(buf, size);
|
2017-07-27 14:25:07 +02:00
|
|
|
|
|
|
|
if (!new_buf)
|
|
|
|
{
|
|
|
|
ERR("Out of memory!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_buf;
|
|
|
|
}
|
|
|
|
|
2008-07-09 18:18:14 +02:00
|
|
|
/******************************************************************************
|
2008-11-17 15:02:01 +01:00
|
|
|
* Allocates memory and converts input from multibyte to wide chars
|
2008-07-09 18:18:14 +02:00
|
|
|
* Returned string must be freed by the caller
|
|
|
|
*/
|
2016-07-06 17:05:17 +02:00
|
|
|
static WCHAR* GetWideString(const char* strA)
|
2008-07-09 18:18:14 +02:00
|
|
|
{
|
2008-08-30 01:23:09 +02:00
|
|
|
if(strA)
|
|
|
|
{
|
2008-11-12 11:10:00 +01:00
|
|
|
WCHAR* strW;
|
2008-08-30 01:23:09 +02:00
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
strW = heap_xalloc(len * sizeof(WCHAR));
|
2008-08-30 01:23:09 +02:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
|
|
|
|
return strW;
|
|
|
|
}
|
|
|
|
return NULL;
|
2008-07-09 18:18:14 +02:00
|
|
|
}
|
|
|
|
|
2008-09-09 21:48:33 +02:00
|
|
|
/******************************************************************************
|
2008-11-17 15:02:01 +01:00
|
|
|
* Allocates memory and converts input from multibyte to wide chars
|
2008-09-09 21:48:33 +02:00
|
|
|
* Returned string must be freed by the caller
|
|
|
|
*/
|
2009-01-26 12:53:12 +01:00
|
|
|
static WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
|
2008-09-09 21:48:33 +02:00
|
|
|
{
|
|
|
|
if(strA)
|
|
|
|
{
|
2008-11-12 11:10:00 +01:00
|
|
|
WCHAR* strW;
|
2008-09-09 21:48:33 +02:00
|
|
|
*len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
strW = heap_xalloc(*len * sizeof(WCHAR));
|
2008-09-09 21:48:33 +02:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
|
|
|
|
return strW;
|
|
|
|
}
|
|
|
|
*len = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-07 23:12:21 +02:00
|
|
|
/******************************************************************************
|
2008-11-17 15:02:01 +01:00
|
|
|
* Allocates memory and converts input from wide chars to multibyte
|
2008-07-07 23:12:21 +02:00
|
|
|
* Returned string must be freed by the caller
|
|
|
|
*/
|
2008-07-10 17:49:36 +02:00
|
|
|
char* GetMultiByteString(const WCHAR* strW)
|
2008-07-07 23:12:21 +02:00
|
|
|
{
|
2008-08-30 01:23:09 +02:00
|
|
|
if(strW)
|
|
|
|
{
|
2008-11-12 11:10:00 +01:00
|
|
|
char* strA;
|
2008-08-30 01:23:09 +02:00
|
|
|
int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
strA = heap_xalloc(len);
|
2008-08-30 01:23:09 +02:00
|
|
|
WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
|
|
|
|
return strA;
|
|
|
|
}
|
|
|
|
return NULL;
|
2008-07-07 23:12:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-08 21:44:08 +02:00
|
|
|
/******************************************************************************
|
2008-11-17 15:02:01 +01:00
|
|
|
* Allocates memory and converts input from wide chars to multibyte
|
2008-09-08 21:44:08 +02:00
|
|
|
* Returned string must be freed by the caller
|
|
|
|
*/
|
2009-01-26 12:53:12 +01:00
|
|
|
static char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
|
2008-09-08 21:44:08 +02:00
|
|
|
{
|
|
|
|
if(strW)
|
|
|
|
{
|
2008-11-12 11:10:00 +01:00
|
|
|
char* strA;
|
2008-09-08 21:44:08 +02:00
|
|
|
*len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
strA = heap_xalloc(*len);
|
2008-09-08 21:44:08 +02:00
|
|
|
WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
|
|
|
|
return strA;
|
|
|
|
}
|
|
|
|
*len = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
static WCHAR *(*get_line)(FILE *);
|
|
|
|
|
|
|
|
/* parser definitions */
|
|
|
|
enum parser_state
|
|
|
|
{
|
|
|
|
HEADER, /* parsing the registry file version header */
|
|
|
|
PARSE_WIN31_LINE, /* parsing a Windows 3.1 registry line */
|
|
|
|
LINE_START, /* at the beginning of a registry line */
|
2017-06-15 14:00:55 +02:00
|
|
|
KEY_NAME, /* parsing a key name */
|
|
|
|
DELETE_KEY, /* deleting a registry key */
|
2017-06-15 14:01:26 +02:00
|
|
|
DEFAULT_VALUE_NAME, /* parsing a default value name */
|
|
|
|
QUOTED_VALUE_NAME, /* parsing a double-quoted value name */
|
|
|
|
DATA_START, /* preparing for data parsing operations */
|
|
|
|
DELETE_VALUE, /* deleting a registry value */
|
2017-06-15 14:01:47 +02:00
|
|
|
DATA_TYPE, /* parsing the registry data type */
|
|
|
|
STRING_DATA, /* parsing REG_SZ data */
|
|
|
|
DWORD_DATA, /* parsing DWORD data */
|
|
|
|
HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
|
2017-06-30 15:42:55 +02:00
|
|
|
EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
|
|
|
|
HEX_MULTILINE, /* parsing multiple lines of hex data */
|
2017-06-15 14:01:47 +02:00
|
|
|
UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
|
2017-06-15 14:00:31 +02:00
|
|
|
SET_VALUE, /* adding a value to the registry */
|
|
|
|
NB_PARSER_STATES
|
|
|
|
};
|
|
|
|
|
|
|
|
struct parser
|
|
|
|
{
|
|
|
|
FILE *file; /* pointer to a registry file */
|
|
|
|
WCHAR two_wchars[2]; /* first two characters from the encoding check */
|
|
|
|
BOOL is_unicode; /* parsing Unicode or ASCII data */
|
|
|
|
short int reg_version; /* registry file version */
|
2017-06-15 14:01:47 +02:00
|
|
|
HKEY hkey; /* current registry key */
|
|
|
|
WCHAR *key_name; /* current key name */
|
2017-06-15 14:00:31 +02:00
|
|
|
WCHAR *value_name; /* value name */
|
2017-06-20 15:25:32 +02:00
|
|
|
DWORD parse_type; /* generic data type for parsing */
|
2017-06-15 14:00:31 +02:00
|
|
|
DWORD data_type; /* data type */
|
|
|
|
void *data; /* value data */
|
|
|
|
DWORD data_size; /* size of the data (in bytes) */
|
2017-06-30 15:42:55 +02:00
|
|
|
BOOL backslash; /* TRUE if the current line contains a backslash */
|
2017-06-15 14:00:31 +02:00
|
|
|
enum parser_state state; /* current parser state */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef WCHAR *(*parser_state_func)(struct parser *parser, WCHAR *pos);
|
|
|
|
|
|
|
|
/* parser state machine functions */
|
|
|
|
static WCHAR *header_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
|
2017-06-15 14:00:55 +02:00
|
|
|
static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
|
2017-06-15 14:01:26 +02:00
|
|
|
static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *data_start_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
|
2017-06-15 14:01:47 +02:00
|
|
|
static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
|
2017-06-30 15:42:55 +02:00
|
|
|
static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
|
2017-06-15 14:01:47 +02:00
|
|
|
static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
|
2017-06-15 14:00:31 +02:00
|
|
|
static WCHAR *set_value_state(struct parser *parser, WCHAR *pos);
|
|
|
|
|
|
|
|
static const parser_state_func parser_funcs[NB_PARSER_STATES] =
|
|
|
|
{
|
|
|
|
header_state, /* HEADER */
|
|
|
|
parse_win31_line_state, /* PARSE_WIN31_LINE */
|
|
|
|
line_start_state, /* LINE_START */
|
2017-06-15 14:00:55 +02:00
|
|
|
key_name_state, /* KEY_NAME */
|
|
|
|
delete_key_state, /* DELETE_KEY */
|
2017-06-15 14:01:26 +02:00
|
|
|
default_value_name_state, /* DEFAULT_VALUE_NAME */
|
|
|
|
quoted_value_name_state, /* QUOTED_VALUE_NAME */
|
|
|
|
data_start_state, /* DATA_START */
|
|
|
|
delete_value_state, /* DELETE_VALUE */
|
2017-06-15 14:01:47 +02:00
|
|
|
data_type_state, /* DATA_TYPE */
|
|
|
|
string_data_state, /* STRING_DATA */
|
|
|
|
dword_data_state, /* DWORD_DATA */
|
|
|
|
hex_data_state, /* HEX_DATA */
|
2017-06-30 15:42:55 +02:00
|
|
|
eol_backslash_state, /* EOL_BACKSLASH */
|
|
|
|
hex_multiline_state, /* HEX_MULTILINE */
|
2017-06-15 14:01:47 +02:00
|
|
|
unknown_data_state, /* UNKNOWN_DATA */
|
2017-06-15 14:00:31 +02:00
|
|
|
set_value_state, /* SET_VALUE */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* set the new parser state and return the previous one */
|
|
|
|
static inline enum parser_state set_state(struct parser *parser, enum parser_state state)
|
|
|
|
{
|
|
|
|
enum parser_state ret = parser->state;
|
|
|
|
parser->state = state;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Converts a hex representation of a DWORD into a DWORD.
|
|
|
|
*/
|
2017-06-15 14:01:47 +02:00
|
|
|
static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
2017-04-11 15:11:54 +02:00
|
|
|
WCHAR *p, *end;
|
|
|
|
int count = 0;
|
2008-07-09 20:59:01 +02:00
|
|
|
|
2017-04-11 15:11:54 +02:00
|
|
|
while (*str == ' ' || *str == '\t') str++;
|
|
|
|
if (!*str) goto error;
|
|
|
|
|
|
|
|
p = str;
|
2019-04-01 11:47:50 +02:00
|
|
|
while (iswxdigit(*p))
|
2017-04-11 15:11:54 +02:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
p++;
|
2007-06-14 11:46:29 +02:00
|
|
|
}
|
2017-04-11 15:11:54 +02:00
|
|
|
if (count > 8) goto error;
|
|
|
|
|
|
|
|
end = p;
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
if (*p && *p != ';') goto error;
|
|
|
|
|
|
|
|
*end = 0;
|
2019-04-01 11:47:50 +02:00
|
|
|
*dw = wcstoul(str, &end, 16);
|
2007-06-14 11:46:29 +02:00
|
|
|
return TRUE;
|
2017-04-11 15:11:54 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
return FALSE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2017-06-30 15:42:55 +02:00
|
|
|
* Converts comma-separated hex data into a binary string and modifies
|
|
|
|
* the input parameter to skip the concatenating backslash, if found.
|
|
|
|
*
|
|
|
|
* Returns TRUE or FALSE to indicate whether parsing was successful.
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
2017-06-30 15:42:55 +02:00
|
|
|
static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
2017-06-30 15:42:55 +02:00
|
|
|
size_t size;
|
|
|
|
BYTE *d;
|
2008-08-27 23:29:08 +02:00
|
|
|
WCHAR *s;
|
2017-06-30 15:42:55 +02:00
|
|
|
|
|
|
|
parser->backslash = FALSE;
|
2007-06-14 11:46:29 +02:00
|
|
|
|
|
|
|
/* The worst case is 1 digit + 1 comma per byte */
|
2017-06-30 15:42:55 +02:00
|
|
|
size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
|
2017-07-27 14:25:07 +02:00
|
|
|
parser->data = heap_xrealloc(parser->data, size);
|
2017-06-30 15:42:55 +02:00
|
|
|
|
|
|
|
s = *str;
|
|
|
|
d = (BYTE *)parser->data + parser->data_size;
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
{
|
2008-08-27 23:29:08 +02:00
|
|
|
WCHAR *end;
|
2017-06-30 15:42:55 +02:00
|
|
|
unsigned long wc;
|
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
wc = wcstoul(s, &end, 16);
|
2017-06-30 15:42:55 +02:00
|
|
|
if (wc > 0xff) return FALSE;
|
|
|
|
|
|
|
|
if (s == end && wc == 0)
|
|
|
|
{
|
|
|
|
while (*end == ' ' || *end == '\t') end++;
|
|
|
|
if (*end == '\\')
|
|
|
|
{
|
|
|
|
parser->backslash = TRUE;
|
|
|
|
*str = end + 1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2017-07-06 13:18:43 +02:00
|
|
|
else if (*end == ';')
|
|
|
|
return TRUE;
|
2017-06-30 15:42:55 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-12-08 23:48:07 +01:00
|
|
|
|
2017-06-30 15:42:55 +02:00
|
|
|
*d++ = wc;
|
|
|
|
parser->data_size++;
|
|
|
|
|
|
|
|
if (*end && *end != ',')
|
|
|
|
{
|
|
|
|
while (*end == ' ' || *end == '\t') end++;
|
|
|
|
if (*end && *end != ';') return FALSE;
|
|
|
|
return TRUE;
|
2007-06-14 11:46:29 +02:00
|
|
|
}
|
2017-06-30 15:42:55 +02:00
|
|
|
|
2008-01-07 19:56:15 +01:00
|
|
|
if (*end) end++;
|
|
|
|
s = end;
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 15:42:55 +02:00
|
|
|
return TRUE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2017-06-15 14:01:47 +02:00
|
|
|
* Parses the data type of the registry value being imported and modifies
|
|
|
|
* the input parameter to skip the string representation of the data type.
|
2002-04-12 01:58:40 +02:00
|
|
|
*
|
2017-06-15 14:01:47 +02:00
|
|
|
* Returns TRUE or FALSE to indicate whether a data type was found.
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
2017-06-15 14:01:47 +02:00
|
|
|
static BOOL parse_data_type(struct parser *parser, WCHAR **line)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
2008-07-09 20:59:01 +02:00
|
|
|
struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
|
|
|
|
|
|
|
|
static const WCHAR quote[] = {'"'};
|
|
|
|
static const WCHAR hex[] = {'h','e','x',':'};
|
|
|
|
static const WCHAR dword[] = {'d','w','o','r','d',':'};
|
|
|
|
static const WCHAR hexp[] = {'h','e','x','('};
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2017-03-29 07:25:31 +02:00
|
|
|
static const struct data_type data_types[] = {
|
|
|
|
/* tag len type parse type */
|
|
|
|
{ quote, 1, REG_SZ, REG_SZ },
|
|
|
|
{ hex, 4, REG_BINARY, REG_BINARY },
|
|
|
|
{ dword, 6, REG_DWORD, REG_DWORD },
|
|
|
|
{ hexp, 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
2002-04-12 01:58:40 +02:00
|
|
|
|
|
|
|
const struct data_type *ptr;
|
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
for (ptr = data_types; ptr->tag; ptr++)
|
|
|
|
{
|
2019-04-01 11:47:50 +02:00
|
|
|
if (wcsncmp(ptr->tag, *line, ptr->len))
|
2002-04-12 01:58:40 +02:00
|
|
|
continue;
|
|
|
|
|
2017-06-20 15:25:32 +02:00
|
|
|
parser->parse_type = ptr->parse_type;
|
2017-06-15 14:01:47 +02:00
|
|
|
parser->data_type = ptr->parse_type;
|
|
|
|
*line += ptr->len;
|
|
|
|
|
|
|
|
if (ptr->type == -1)
|
|
|
|
{
|
|
|
|
WCHAR *end;
|
|
|
|
DWORD val;
|
2008-07-09 20:59:01 +02:00
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!**line || towlower((*line)[1]) == 'x')
|
2017-07-05 15:33:02 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
/* "hex(xx):" is special */
|
2017-06-21 14:18:53 +02:00
|
|
|
val = wcstoul(*line, &end, 16);
|
2017-07-05 15:33:02 +02:00
|
|
|
if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
|
2017-06-15 14:01:47 +02:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
parser->data_type = val;
|
|
|
|
*line = end + 2;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
2017-06-15 14:01:47 +02:00
|
|
|
return TRUE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
2017-06-15 14:01:47 +02:00
|
|
|
return FALSE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2017-04-25 15:31:10 +02:00
|
|
|
* Replaces escape sequences with their character equivalents and
|
|
|
|
* null-terminates the string on the first non-escaped double quote.
|
|
|
|
*
|
|
|
|
* Assigns a pointer to the remaining unparsed data in the line.
|
|
|
|
* Returns TRUE or FALSE to indicate whether a closing double quote was found.
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
2017-04-25 15:31:10 +02:00
|
|
|
static BOOL REGPROC_unescape_string(WCHAR *str, WCHAR **unparsed)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
|
|
|
int str_idx = 0; /* current character under analysis */
|
|
|
|
int val_idx = 0; /* the last character of the unescaped string */
|
2008-07-09 20:02:02 +02:00
|
|
|
int len = lstrlenW(str);
|
2017-04-25 15:31:10 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2003-12-08 23:48:07 +01:00
|
|
|
for (str_idx = 0; str_idx < len; str_idx++, val_idx++) {
|
|
|
|
if (str[str_idx] == '\\') {
|
2002-04-12 01:58:40 +02:00
|
|
|
str_idx++;
|
2003-12-08 23:48:07 +01:00
|
|
|
switch (str[str_idx]) {
|
2002-04-12 01:58:40 +02:00
|
|
|
case 'n':
|
|
|
|
str[val_idx] = '\n';
|
|
|
|
break;
|
2014-10-30 02:55:08 +01:00
|
|
|
case 'r':
|
|
|
|
str[val_idx] = '\r';
|
|
|
|
break;
|
2014-10-29 03:30:13 +01:00
|
|
|
case '0':
|
2021-03-12 12:27:33 +01:00
|
|
|
return FALSE;
|
2002-04-12 01:58:40 +02:00
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
str[val_idx] = str[str_idx];
|
|
|
|
break;
|
|
|
|
default:
|
2017-07-26 15:15:57 +02:00
|
|
|
if (!str[str_idx]) return FALSE;
|
2016-07-15 09:49:48 +02:00
|
|
|
output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
|
2002-04-12 01:58:40 +02:00
|
|
|
str[val_idx] = str[str_idx];
|
|
|
|
break;
|
|
|
|
}
|
2017-04-25 15:31:10 +02:00
|
|
|
} else if (str[str_idx] == '"') {
|
|
|
|
break;
|
2002-04-12 01:58:40 +02:00
|
|
|
} else {
|
|
|
|
str[val_idx] = str[str_idx];
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 15:31:10 +02:00
|
|
|
|
|
|
|
ret = (str[str_idx] == '"');
|
|
|
|
*unparsed = str + str_idx + 1;
|
2002-04-12 01:58:40 +02:00
|
|
|
str[val_idx] = '\0';
|
2017-04-25 15:31:10 +02:00
|
|
|
return ret;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 14:00:55 +02:00
|
|
|
static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
|
2008-07-07 23:12:21 +02:00
|
|
|
{
|
2017-04-25 15:30:56 +02:00
|
|
|
unsigned int i;
|
2008-07-07 23:12:21 +02:00
|
|
|
|
2017-06-15 14:00:55 +02:00
|
|
|
if (!key_name) return 0;
|
2008-07-07 23:12:21 +02:00
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
*key_path = wcschr(key_name, '\\');
|
2017-06-15 14:00:55 +02:00
|
|
|
if (*key_path) (*key_path)++;
|
2008-07-07 23:12:21 +02:00
|
|
|
|
2017-04-25 15:30:56 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(reg_class_keys); i++)
|
2008-07-07 23:12:21 +02:00
|
|
|
{
|
2017-04-25 15:30:56 +02:00
|
|
|
int len = lstrlenW(reg_class_namesW[i]);
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!wcsnicmp(key_name, reg_class_namesW[i], len) &&
|
2017-06-15 14:00:55 +02:00
|
|
|
(key_name[len] == 0 || key_name[len] == '\\'))
|
2017-04-25 15:30:56 +02:00
|
|
|
{
|
|
|
|
return reg_class_keys[i];
|
2008-07-07 23:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:30:56 +02:00
|
|
|
return 0;
|
2008-07-07 23:12:21 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
static void close_key(struct parser *parser)
|
2017-06-15 14:00:55 +02:00
|
|
|
{
|
2017-06-15 14:01:47 +02:00
|
|
|
if (parser->hkey)
|
2017-06-15 14:00:55 +02:00
|
|
|
{
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(parser->key_name);
|
2017-06-15 14:01:47 +02:00
|
|
|
parser->key_name = NULL;
|
2017-06-15 14:00:55 +02:00
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
RegCloseKey(parser->hkey);
|
|
|
|
parser->hkey = NULL;
|
2017-06-15 14:00:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
/******************************************************************************
|
2017-06-15 14:00:55 +02:00
|
|
|
* Opens the registry key given by the input path.
|
|
|
|
* This key must be closed by calling close_key().
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
2017-06-15 14:01:47 +02:00
|
|
|
static LONG open_key(struct parser *parser, WCHAR *path)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
2017-06-15 14:00:55 +02:00
|
|
|
HKEY key_class;
|
|
|
|
WCHAR *key_path;
|
2007-06-14 11:46:46 +02:00
|
|
|
LONG res;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
close_key(parser);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2007-06-14 11:42:41 +02:00
|
|
|
/* Get the registry class */
|
2017-06-15 14:01:47 +02:00
|
|
|
if (!path || !(key_class = parse_key_name(path, &key_path)))
|
2007-06-14 11:42:41 +02:00
|
|
|
return ERROR_INVALID_PARAMETER;
|
2003-12-08 23:48:07 +01:00
|
|
|
|
2017-06-15 14:00:55 +02:00
|
|
|
res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
|
2017-06-15 14:01:47 +02:00
|
|
|
KEY_ALL_ACCESS, NULL, &parser->hkey, NULL);
|
2007-06-14 11:42:41 +02:00
|
|
|
|
2007-06-14 11:46:46 +02:00
|
|
|
if (res == ERROR_SUCCESS)
|
2016-07-15 09:50:16 +02:00
|
|
|
{
|
2017-07-27 14:24:49 +02:00
|
|
|
parser->key_name = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
|
2017-06-15 14:01:47 +02:00
|
|
|
lstrcpyW(parser->key_name, path);
|
2016-07-15 09:50:16 +02:00
|
|
|
}
|
2007-06-15 18:59:55 +02:00
|
|
|
else
|
2017-06-15 14:01:47 +02:00
|
|
|
parser->hkey = NULL;
|
2007-06-14 11:42:41 +02:00
|
|
|
|
2007-06-14 11:46:46 +02:00
|
|
|
return res;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 13:47:51 +02:00
|
|
|
static void free_parser_data(struct parser *parser)
|
|
|
|
{
|
|
|
|
if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(parser->data);
|
2017-07-07 13:47:51 +02:00
|
|
|
|
|
|
|
parser->data = NULL;
|
|
|
|
parser->data_size = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:28:17 +02:00
|
|
|
static void prepare_hex_string_data(struct parser *parser)
|
|
|
|
{
|
2018-04-23 15:37:27 +02:00
|
|
|
if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ ||
|
|
|
|
parser->data_type == REG_SZ)
|
2017-07-10 13:28:17 +02:00
|
|
|
{
|
2017-08-28 13:05:42 +02:00
|
|
|
if (parser->is_unicode)
|
2017-07-10 13:28:17 +02:00
|
|
|
{
|
2017-08-28 13:05:42 +02:00
|
|
|
WCHAR *data = parser->data;
|
|
|
|
DWORD len = parser->data_size / sizeof(WCHAR);
|
2017-07-10 13:28:17 +02:00
|
|
|
|
2017-08-28 13:05:42 +02:00
|
|
|
if (data[len - 1] != 0)
|
|
|
|
{
|
|
|
|
data[len] = 0;
|
|
|
|
parser->data_size += sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-07-10 13:28:17 +02:00
|
|
|
{
|
2017-08-28 13:05:42 +02:00
|
|
|
BYTE *data = parser->data;
|
|
|
|
|
|
|
|
if (data[parser->data_size - 1] != 0)
|
|
|
|
{
|
|
|
|
data[parser->data_size] = 0;
|
|
|
|
parser->data_size++;
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:28:17 +02:00
|
|
|
parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
|
|
|
|
parser->data_size *= sizeof(WCHAR);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(data);
|
2017-07-10 13:28:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 12:53:53 +01:00
|
|
|
enum reg_versions {
|
|
|
|
REG_VERSION_31,
|
|
|
|
REG_VERSION_40,
|
|
|
|
REG_VERSION_50,
|
2017-03-22 12:54:28 +01:00
|
|
|
REG_VERSION_FUZZY,
|
2017-03-22 12:53:53 +01:00
|
|
|
REG_VERSION_INVALID
|
|
|
|
};
|
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
static enum reg_versions parse_file_header(const WCHAR *s)
|
2017-03-22 12:53:53 +01:00
|
|
|
{
|
|
|
|
static const WCHAR header_31[] = {'R','E','G','E','D','I','T',0};
|
|
|
|
static const WCHAR header_40[] = {'R','E','G','E','D','I','T','4',0};
|
|
|
|
static const WCHAR header_50[] = {'W','i','n','d','o','w','s',' ',
|
|
|
|
'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
|
|
|
|
'V','e','r','s','i','o','n',' ','5','.','0','0',0};
|
|
|
|
|
2017-07-04 14:46:09 +02:00
|
|
|
while (*s == ' ' || *s == '\t') s++;
|
2017-01-30 05:09:27 +01:00
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!lstrcmpW(s, header_31))
|
2017-03-22 12:53:53 +01:00
|
|
|
return REG_VERSION_31;
|
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!lstrcmpW(s, header_40))
|
2017-03-22 12:53:53 +01:00
|
|
|
return REG_VERSION_40;
|
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!lstrcmpW(s, header_50))
|
2017-03-22 12:53:53 +01:00
|
|
|
return REG_VERSION_50;
|
|
|
|
|
2017-03-22 12:54:28 +01:00
|
|
|
/* The Windows version accepts registry file headers beginning with "REGEDIT" and ending
|
|
|
|
* with other characters, as long as "REGEDIT" appears at the start of the line. For example,
|
|
|
|
* "REGEDIT 4", "REGEDIT9" and "REGEDIT4FOO" are all treated as valid file headers.
|
|
|
|
* In all such cases, however, the contents of the registry file are not imported.
|
|
|
|
*/
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!wcsncmp(s, header_31, 7)) /* "REGEDIT" without NUL */
|
2017-03-22 12:54:28 +01:00
|
|
|
return REG_VERSION_FUZZY;
|
|
|
|
|
2017-03-22 12:53:53 +01:00
|
|
|
return REG_VERSION_INVALID;
|
|
|
|
}
|
2017-01-30 05:09:27 +01:00
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
/* handler for parser HEADER state */
|
|
|
|
static WCHAR *header_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line, *header;
|
|
|
|
|
|
|
|
if (!(line = get_line(parser->file)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!parser->is_unicode)
|
|
|
|
{
|
2017-07-27 14:24:49 +02:00
|
|
|
header = heap_xalloc((lstrlenW(line) + 3) * sizeof(WCHAR));
|
2017-06-15 14:00:31 +02:00
|
|
|
header[0] = parser->two_wchars[0];
|
|
|
|
header[1] = parser->two_wchars[1];
|
|
|
|
lstrcpyW(header + 2, line);
|
|
|
|
parser->reg_version = parse_file_header(header);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(header);
|
2017-06-15 14:00:31 +02:00
|
|
|
}
|
|
|
|
else parser->reg_version = parse_file_header(line);
|
|
|
|
|
|
|
|
switch (parser->reg_version)
|
|
|
|
{
|
|
|
|
case REG_VERSION_31:
|
|
|
|
set_state(parser, PARSE_WIN31_LINE);
|
|
|
|
break;
|
|
|
|
case REG_VERSION_40:
|
|
|
|
case REG_VERSION_50:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
get_line(NULL); /* Reset static variables */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser PARSE_WIN31_LINE state */
|
|
|
|
static WCHAR *parse_win31_line_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line, *value;
|
|
|
|
static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T'};
|
|
|
|
unsigned int key_end = 0;
|
|
|
|
|
|
|
|
if (!(line = get_line(parser->file)))
|
|
|
|
return NULL;
|
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (wcsncmp(line, hkcr, ARRAY_SIZE(hkcr)))
|
2017-06-30 15:42:43 +02:00
|
|
|
return line;
|
2017-06-15 14:00:31 +02:00
|
|
|
|
|
|
|
/* get key name */
|
2019-04-01 11:47:50 +02:00
|
|
|
while (line[key_end] && !iswspace(line[key_end])) key_end++;
|
2017-06-15 14:00:31 +02:00
|
|
|
|
|
|
|
value = line + key_end;
|
|
|
|
while (*value == ' ' || *value == '\t') value++;
|
|
|
|
|
|
|
|
if (*value == '=') value++;
|
|
|
|
if (*value == ' ') value++; /* at most one space is skipped */
|
|
|
|
|
|
|
|
line[key_end] = 0;
|
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
if (open_key(parser, line) != ERROR_SUCCESS)
|
2017-06-15 14:00:31 +02:00
|
|
|
{
|
|
|
|
output_message(STRING_OPEN_KEY_FAILED, line);
|
2017-06-30 15:42:43 +02:00
|
|
|
return line;
|
2017-06-15 14:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parser->value_name = NULL;
|
|
|
|
parser->data_type = REG_SZ;
|
|
|
|
parser->data = value;
|
|
|
|
parser->data_size = (lstrlenW(value) + 1) * sizeof(WCHAR);
|
|
|
|
|
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser LINE_START state */
|
|
|
|
static WCHAR *line_start_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line, *p;
|
|
|
|
|
|
|
|
if (!(line = get_line(parser->file)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (p = line; *p; p++)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '[':
|
2017-06-15 14:00:55 +02:00
|
|
|
set_state(parser, KEY_NAME);
|
|
|
|
return p + 1;
|
2017-06-15 14:00:31 +02:00
|
|
|
case '@':
|
2017-06-15 14:01:26 +02:00
|
|
|
set_state(parser, DEFAULT_VALUE_NAME);
|
2017-06-15 14:00:55 +02:00
|
|
|
return p;
|
2017-06-15 14:01:26 +02:00
|
|
|
case '"':
|
|
|
|
set_state(parser, QUOTED_VALUE_NAME);
|
|
|
|
return p + 1;
|
2017-06-15 14:00:31 +02:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:00:55 +02:00
|
|
|
/* handler for parser KEY_NAME state */
|
|
|
|
static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos, *key_end;
|
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (*p == ' ' || *p == '\t' || !(key_end = wcsrchr(p, ']')))
|
2017-06-15 14:00:55 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
*key_end = 0;
|
|
|
|
|
|
|
|
if (*p == '-')
|
|
|
|
{
|
|
|
|
set_state(parser, DELETE_KEY);
|
|
|
|
return p + 1;
|
|
|
|
}
|
2017-06-15 14:01:47 +02:00
|
|
|
else if (open_key(parser, p) != ERROR_SUCCESS)
|
2017-06-15 14:00:55 +02:00
|
|
|
output_message(STRING_OPEN_KEY_FAILED, p);
|
|
|
|
|
|
|
|
done:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser DELETE_KEY state */
|
|
|
|
static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos;
|
|
|
|
|
2017-08-25 07:36:49 +02:00
|
|
|
close_key(parser);
|
|
|
|
|
2017-06-16 15:06:41 +02:00
|
|
|
if (*p == 'H' || *p == 'h')
|
2017-06-15 14:00:55 +02:00
|
|
|
delete_registry_key(p);
|
|
|
|
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:01:26 +02:00
|
|
|
/* handler for parser DEFAULT_VALUE_NAME state */
|
|
|
|
static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(parser->value_name);
|
2017-06-15 14:01:26 +02:00
|
|
|
parser->value_name = NULL;
|
|
|
|
|
|
|
|
set_state(parser, DATA_START);
|
|
|
|
return pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser QUOTED_VALUE_NAME state */
|
|
|
|
static WCHAR *quoted_value_name_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *val_name = pos, *p;
|
|
|
|
|
2019-01-29 20:38:06 +01:00
|
|
|
heap_free(parser->value_name);
|
|
|
|
parser->value_name = NULL;
|
2017-06-15 14:01:26 +02:00
|
|
|
|
|
|
|
if (!REGPROC_unescape_string(val_name, &p))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
/* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
|
2017-07-27 14:24:49 +02:00
|
|
|
parser->value_name = heap_xalloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
|
2017-06-15 14:01:26 +02:00
|
|
|
lstrcpyW(parser->value_name, val_name);
|
|
|
|
|
|
|
|
set_state(parser, DATA_START);
|
|
|
|
return p;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
set_state(parser, LINE_START);
|
2017-08-18 11:14:14 +02:00
|
|
|
return val_name;
|
2017-06-15 14:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser DATA_START state */
|
|
|
|
static WCHAR *data_start_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
if (*p != '=') goto done;
|
|
|
|
p++;
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
|
|
|
|
/* trim trailing whitespace */
|
2019-04-01 11:47:50 +02:00
|
|
|
len = lstrlenW(p);
|
2017-06-15 14:01:26 +02:00
|
|
|
while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
|
|
|
|
p[len] = 0;
|
|
|
|
|
|
|
|
if (*p == '-')
|
|
|
|
set_state(parser, DELETE_VALUE);
|
2017-06-15 14:01:47 +02:00
|
|
|
else
|
|
|
|
set_state(parser, DATA_TYPE);
|
|
|
|
return p;
|
2017-06-15 14:01:26 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser DELETE_VALUE state */
|
|
|
|
static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos + 1;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
if (*p && *p != ';') goto done;
|
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
RegDeleteValueW(parser->hkey, parser->value_name);
|
2017-06-15 14:01:26 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
/* handler for parser DATA_TYPE state */
|
|
|
|
static WCHAR *data_type_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line = pos;
|
|
|
|
|
|
|
|
if (!parse_data_type(parser, &line))
|
|
|
|
{
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:25:32 +02:00
|
|
|
switch (parser->parse_type)
|
2017-06-15 14:01:47 +02:00
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
set_state(parser, STRING_DATA);
|
|
|
|
break;
|
|
|
|
case REG_DWORD:
|
|
|
|
set_state(parser, DWORD_DATA);
|
|
|
|
break;
|
2017-06-20 15:25:32 +02:00
|
|
|
case REG_BINARY: /* all hex data types, including undefined */
|
2017-06-15 14:01:47 +02:00
|
|
|
set_state(parser, HEX_DATA);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_state(parser, UNKNOWN_DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser STRING_DATA state */
|
|
|
|
static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line;
|
|
|
|
|
|
|
|
parser->data = pos;
|
|
|
|
|
|
|
|
if (!REGPROC_unescape_string(parser->data, &line))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
while (*line == ' ' || *line == '\t') line++;
|
|
|
|
if (*line && *line != ';') goto invalid;
|
|
|
|
|
|
|
|
parser->data_size = (lstrlenW(parser->data) + 1) * sizeof(WCHAR);
|
|
|
|
|
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return line;
|
|
|
|
|
|
|
|
invalid:
|
2017-07-07 13:47:51 +02:00
|
|
|
free_parser_data(parser);
|
2017-06-15 14:01:47 +02:00
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser DWORD_DATA state */
|
|
|
|
static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line = pos;
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
parser->data = heap_xalloc(sizeof(DWORD));
|
2017-06-15 14:01:47 +02:00
|
|
|
|
|
|
|
if (!convert_hex_to_dword(line, parser->data))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
parser->data_size = sizeof(DWORD);
|
|
|
|
|
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return line;
|
|
|
|
|
|
|
|
invalid:
|
2017-07-07 13:47:51 +02:00
|
|
|
free_parser_data(parser);
|
2017-06-15 14:01:47 +02:00
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser HEX_DATA state */
|
|
|
|
static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line = pos;
|
|
|
|
|
2017-10-16 12:27:25 +02:00
|
|
|
if (!*line)
|
|
|
|
goto set_value;
|
|
|
|
|
2017-06-30 15:42:55 +02:00
|
|
|
if (!convert_hex_csv_to_hex(parser, &line))
|
2017-06-15 14:01:47 +02:00
|
|
|
goto invalid;
|
|
|
|
|
2017-06-30 15:42:55 +02:00
|
|
|
if (parser->backslash)
|
|
|
|
{
|
|
|
|
set_state(parser, EOL_BACKSLASH);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:28:17 +02:00
|
|
|
prepare_hex_string_data(parser);
|
2017-06-15 14:01:47 +02:00
|
|
|
|
2017-10-16 12:27:25 +02:00
|
|
|
set_value:
|
2017-06-15 14:01:47 +02:00
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return line;
|
|
|
|
|
|
|
|
invalid:
|
2017-07-07 13:47:51 +02:00
|
|
|
free_parser_data(parser);
|
2017-06-15 14:01:47 +02:00
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-06-30 15:42:55 +02:00
|
|
|
/* handler for parser EOL_BACKSLASH state */
|
|
|
|
static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
if (*p && *p != ';') goto invalid;
|
|
|
|
|
|
|
|
set_state(parser, HEX_MULTILINE);
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
invalid:
|
2017-07-07 13:47:51 +02:00
|
|
|
free_parser_data(parser);
|
2017-06-30 15:42:55 +02:00
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser HEX_MULTILINE state */
|
|
|
|
static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line;
|
|
|
|
|
|
|
|
if (!(line = get_line(parser->file)))
|
|
|
|
{
|
2017-07-10 13:28:17 +02:00
|
|
|
prepare_hex_string_data(parser);
|
2017-06-30 15:42:55 +02:00
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*line == ' ' || *line == '\t') line++;
|
2017-07-04 14:46:26 +02:00
|
|
|
if (!*line || *line == ';') return line;
|
2017-06-30 15:42:55 +02:00
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!iswxdigit(*line)) goto invalid;
|
2017-07-04 14:46:18 +02:00
|
|
|
|
2017-06-30 15:42:55 +02:00
|
|
|
set_state(parser, HEX_DATA);
|
|
|
|
return line;
|
2017-07-04 14:46:18 +02:00
|
|
|
|
|
|
|
invalid:
|
2017-07-07 13:47:51 +02:00
|
|
|
free_parser_data(parser);
|
2017-07-04 14:46:18 +02:00
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
2017-06-30 15:42:55 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
/* handler for parser UNKNOWN_DATA state */
|
|
|
|
static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
output_message(STRING_UNKNOWN_DATA_FORMAT, parser->data_type);
|
|
|
|
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
/* handler for parser SET_VALUE state */
|
|
|
|
static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
2017-06-15 14:01:47 +02:00
|
|
|
RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
|
2017-06-15 14:00:31 +02:00
|
|
|
parser->data, parser->data_size);
|
|
|
|
|
2017-07-07 13:47:51 +02:00
|
|
|
free_parser_data(parser);
|
2017-06-16 15:06:23 +02:00
|
|
|
|
2017-06-15 14:01:47 +02:00
|
|
|
if (parser->reg_version == REG_VERSION_31)
|
|
|
|
set_state(parser, PARSE_WIN31_LINE);
|
|
|
|
else
|
|
|
|
set_state(parser, LINE_START);
|
2017-06-15 14:00:31 +02:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2017-05-03 13:28:26 +02:00
|
|
|
static WCHAR *get_lineA(FILE *fp)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
2017-05-03 13:28:26 +02:00
|
|
|
static WCHAR *lineW;
|
2017-03-22 12:54:05 +01:00
|
|
|
static size_t size;
|
|
|
|
static char *buf, *next;
|
|
|
|
char *line;
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(lineW);
|
2017-05-03 13:28:26 +02:00
|
|
|
|
2017-05-03 13:28:14 +02:00
|
|
|
if (!fp) goto cleanup;
|
2016-07-21 07:48:30 +02:00
|
|
|
|
2017-03-22 12:54:05 +01:00
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
size = REG_VAL_BUF_SIZE;
|
2017-07-27 14:24:49 +02:00
|
|
|
buf = heap_xalloc(size);
|
2017-03-22 12:54:05 +01:00
|
|
|
*buf = 0;
|
|
|
|
next = buf;
|
|
|
|
}
|
|
|
|
line = next;
|
2009-06-16 15:46:33 +02:00
|
|
|
|
2017-03-22 12:54:05 +01:00
|
|
|
while (next)
|
|
|
|
{
|
|
|
|
char *p = strpbrk(line, "\r\n");
|
|
|
|
if (!p)
|
2009-06-16 15:46:33 +02:00
|
|
|
{
|
2017-03-22 12:54:05 +01:00
|
|
|
size_t len, count;
|
|
|
|
len = strlen(next);
|
|
|
|
memmove(buf, next, len + 1);
|
|
|
|
if (size - len < 3)
|
|
|
|
{
|
|
|
|
size *= 2;
|
2017-07-27 14:25:07 +02:00
|
|
|
buf = heap_xrealloc(buf, size);
|
2017-03-22 12:54:05 +01:00
|
|
|
}
|
|
|
|
if (!(count = fread(buf + len, 1, size - len - 1, fp)))
|
|
|
|
{
|
|
|
|
next = NULL;
|
2017-05-03 13:28:26 +02:00
|
|
|
lineW = GetWideString(buf);
|
|
|
|
return lineW;
|
2017-03-22 12:54:05 +01:00
|
|
|
}
|
|
|
|
buf[len + count] = 0;
|
|
|
|
next = buf;
|
2016-07-21 07:48:30 +02:00
|
|
|
line = buf;
|
2017-03-22 12:54:05 +01:00
|
|
|
continue;
|
2009-06-16 15:46:33 +02:00
|
|
|
}
|
2017-03-22 12:54:05 +01:00
|
|
|
next = p + 1;
|
|
|
|
if (*p == '\r' && *(p + 1) == '\n') next++;
|
|
|
|
*p = 0;
|
2017-05-03 13:28:26 +02:00
|
|
|
lineW = GetWideString(line);
|
|
|
|
return lineW;
|
2017-03-22 12:54:05 +01:00
|
|
|
}
|
2017-05-03 13:28:14 +02:00
|
|
|
|
|
|
|
cleanup:
|
2017-05-03 13:28:26 +02:00
|
|
|
lineW = NULL;
|
2017-07-27 14:24:58 +02:00
|
|
|
if (size) heap_free(buf);
|
2017-03-22 12:54:05 +01:00
|
|
|
size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-21 22:12:02 +02:00
|
|
|
|
2017-03-16 13:05:00 +01:00
|
|
|
static WCHAR *get_lineW(FILE *fp)
|
2008-07-09 23:39:08 +02:00
|
|
|
{
|
2017-03-16 13:05:00 +01:00
|
|
|
static size_t size;
|
|
|
|
static WCHAR *buf, *next;
|
|
|
|
WCHAR *line;
|
2008-07-09 23:39:08 +02:00
|
|
|
|
2017-05-03 13:28:14 +02:00
|
|
|
if (!fp) goto cleanup;
|
2017-03-22 12:53:53 +01:00
|
|
|
|
2017-03-16 13:05:00 +01:00
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
size = REG_VAL_BUF_SIZE;
|
2017-07-27 14:24:49 +02:00
|
|
|
buf = heap_xalloc(size * sizeof(WCHAR));
|
2017-03-16 13:05:00 +01:00
|
|
|
*buf = 0;
|
|
|
|
next = buf;
|
|
|
|
}
|
|
|
|
line = next;
|
2008-07-09 23:39:08 +02:00
|
|
|
|
2017-03-16 13:05:00 +01:00
|
|
|
while (next)
|
|
|
|
{
|
|
|
|
static const WCHAR line_endings[] = {'\r','\n',0};
|
2019-04-01 11:47:50 +02:00
|
|
|
WCHAR *p = wcspbrk(line, line_endings);
|
2017-03-16 13:05:00 +01:00
|
|
|
if (!p)
|
2008-07-09 23:39:08 +02:00
|
|
|
{
|
2017-03-16 13:05:00 +01:00
|
|
|
size_t len, count;
|
2019-04-01 11:47:50 +02:00
|
|
|
len = lstrlenW(next);
|
2017-03-16 13:05:00 +01:00
|
|
|
memmove(buf, next, (len + 1) * sizeof(WCHAR));
|
|
|
|
if (size - len < 3)
|
|
|
|
{
|
|
|
|
size *= 2;
|
2017-07-27 14:25:07 +02:00
|
|
|
buf = heap_xrealloc(buf, size * sizeof(WCHAR));
|
2017-03-16 13:05:00 +01:00
|
|
|
}
|
|
|
|
if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
|
|
|
|
{
|
|
|
|
next = NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
buf[len + count] = 0;
|
|
|
|
next = buf;
|
2009-03-08 16:45:54 +01:00
|
|
|
line = buf;
|
2017-03-16 13:05:00 +01:00
|
|
|
continue;
|
2008-07-09 23:39:08 +02:00
|
|
|
}
|
2017-03-16 13:05:00 +01:00
|
|
|
next = p + 1;
|
|
|
|
if (*p == '\r' && *(p + 1) == '\n') next++;
|
|
|
|
*p = 0;
|
|
|
|
return line;
|
2008-07-09 23:39:08 +02:00
|
|
|
}
|
2017-05-03 13:28:14 +02:00
|
|
|
|
|
|
|
cleanup:
|
2017-07-27 14:24:58 +02:00
|
|
|
if (size) heap_free(buf);
|
2017-03-16 13:05:00 +01:00
|
|
|
size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-09 23:39:08 +02:00
|
|
|
|
2003-08-07 05:10:13 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Reads contents of the specified file into the registry.
|
|
|
|
*/
|
2017-06-15 14:00:31 +02:00
|
|
|
BOOL import_registry_file(FILE *reg_file)
|
2003-08-07 05:10:13 +02:00
|
|
|
{
|
2017-03-22 12:54:16 +01:00
|
|
|
BYTE s[2];
|
2017-06-15 14:00:31 +02:00
|
|
|
struct parser parser;
|
|
|
|
WCHAR *pos;
|
2017-03-22 12:54:16 +01:00
|
|
|
|
|
|
|
if (!reg_file || (fread(s, 2, 1, reg_file) != 1))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
|
|
|
|
get_line = parser.is_unicode ? get_lineW : get_lineA;
|
2017-05-05 11:42:21 +02:00
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
parser.file = reg_file;
|
|
|
|
parser.two_wchars[0] = s[0];
|
|
|
|
parser.two_wchars[1] = s[1];
|
|
|
|
parser.reg_version = -1;
|
2017-06-15 14:01:47 +02:00
|
|
|
parser.hkey = NULL;
|
|
|
|
parser.key_name = NULL;
|
2017-06-15 14:00:31 +02:00
|
|
|
parser.value_name = NULL;
|
2017-06-20 15:25:32 +02:00
|
|
|
parser.parse_type = 0;
|
2017-06-15 14:00:31 +02:00
|
|
|
parser.data_type = 0;
|
|
|
|
parser.data = NULL;
|
|
|
|
parser.data_size = 0;
|
2017-06-30 15:42:55 +02:00
|
|
|
parser.backslash = FALSE;
|
2017-06-15 14:00:31 +02:00
|
|
|
parser.state = HEADER;
|
2017-05-05 11:42:21 +02:00
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
pos = parser.two_wchars;
|
2017-05-05 11:42:21 +02:00
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
/* parser main loop */
|
|
|
|
while (pos)
|
|
|
|
pos = (parser_funcs[parser.state])(&parser, pos);
|
2017-05-05 11:42:21 +02:00
|
|
|
|
2017-06-15 14:00:31 +02:00
|
|
|
if (parser.reg_version == REG_VERSION_FUZZY || parser.reg_version == REG_VERSION_INVALID)
|
|
|
|
return parser.reg_version == REG_VERSION_FUZZY;
|
2017-05-05 11:42:33 +02:00
|
|
|
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(parser.value_name);
|
2017-06-15 14:01:47 +02:00
|
|
|
close_key(&parser);
|
|
|
|
|
2017-05-05 11:42:33 +02:00
|
|
|
return TRUE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Removes the registry key with all subkeys. Parses full key name.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* reg_key_name - full name of registry branch to delete. Ignored if is NULL,
|
|
|
|
* empty, points to register key class, does not exist.
|
|
|
|
*/
|
2008-07-09 18:18:14 +02:00
|
|
|
void delete_registry_key(WCHAR *reg_key_name)
|
2008-07-07 23:12:21 +02:00
|
|
|
{
|
|
|
|
WCHAR *key_name = NULL;
|
|
|
|
HKEY key_class;
|
|
|
|
|
|
|
|
if (!reg_key_name || !reg_key_name[0])
|
|
|
|
return;
|
|
|
|
|
2017-07-12 13:09:05 +02:00
|
|
|
if (!(key_class = parse_key_name(reg_key_name, &key_name)))
|
2017-07-12 13:09:14 +02:00
|
|
|
{
|
|
|
|
if (key_name) *(key_name - 1) = 0;
|
|
|
|
error_exit(STRING_INVALID_SYSTEM_KEY, reg_key_name);
|
|
|
|
}
|
2017-07-12 13:09:05 +02:00
|
|
|
|
2017-07-24 14:47:56 +02:00
|
|
|
if (!key_name || !*key_name)
|
2017-07-12 13:09:14 +02:00
|
|
|
error_exit(STRING_DELETE_FAILED, reg_key_name);
|
2008-07-07 23:12:21 +02:00
|
|
|
|
|
|
|
RegDeleteTreeW(key_class, key_name);
|
|
|
|
}
|
2017-07-19 07:28:21 +02:00
|
|
|
|
2017-07-19 07:29:05 +02:00
|
|
|
static void REGPROC_write_line(FILE *fp, const WCHAR *str, BOOL unicode)
|
|
|
|
{
|
|
|
|
if (unicode)
|
|
|
|
fwrite(str, sizeof(WCHAR), lstrlenW(str), fp);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *strA = GetMultiByteString(str);
|
|
|
|
fputs(strA, fp);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(strA);
|
2017-07-19 07:29:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 07:28:38 +02:00
|
|
|
static WCHAR *REGPROC_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-08 01:50:42 +02:00
|
|
|
|
|
|
|
if (!c) break;
|
|
|
|
|
|
|
|
if (c == '\r' || c == '\n' || c == '\\' || c == '"')
|
2017-07-19 07:28:38 +02:00
|
|
|
escape_count++;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
buf = heap_xalloc((str_len + escape_count + 1) * sizeof(WCHAR));
|
2017-07-19 07:28:38 +02:00
|
|
|
|
|
|
|
for (i = 0, pos = 0; i < str_len; i++, pos++)
|
|
|
|
{
|
|
|
|
WCHAR c = str[i];
|
|
|
|
|
2018-05-08 01:50:42 +02:00
|
|
|
if (!c) break;
|
|
|
|
|
2017-07-19 07:28:38 +02: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(FILE *fp, WCHAR *name, size_t len, BOOL unicode)
|
|
|
|
{
|
|
|
|
static const WCHAR quoted_fmt[] = {'"','%','s','"','=',0};
|
|
|
|
static const WCHAR default_name[] = {'@','=',0};
|
|
|
|
size_t line_len;
|
|
|
|
|
|
|
|
if (name && *name)
|
|
|
|
{
|
|
|
|
WCHAR *str = REGPROC_escape_string(name, len, &line_len);
|
2017-07-27 14:24:49 +02:00
|
|
|
WCHAR *buf = heap_xalloc((line_len + 4) * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
line_len = swprintf(buf, line_len + 4, quoted_fmt, str);
|
2017-07-19 07:28:38 +02:00
|
|
|
REGPROC_write_line(fp, buf, unicode);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(buf);
|
|
|
|
heap_free(str);
|
2017-07-19 07:28:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line_len = lstrlenW(default_name);
|
|
|
|
REGPROC_write_line(fp, default_name, unicode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line_len;
|
|
|
|
}
|
|
|
|
|
2017-07-19 07:29:05 +02:00
|
|
|
static void export_string_data(WCHAR **buf, WCHAR *data, size_t size)
|
|
|
|
{
|
2017-10-02 10:05:21 +02:00
|
|
|
size_t len = 0, line_len;
|
2017-07-19 07:29:05 +02:00
|
|
|
WCHAR *str;
|
|
|
|
static const WCHAR fmt[] = {'"','%','s','"',0};
|
|
|
|
|
2017-10-02 10:05:21 +02:00
|
|
|
if (size)
|
|
|
|
len = size / sizeof(WCHAR) - 1;
|
2017-07-19 07:29:05 +02:00
|
|
|
str = REGPROC_escape_string(data, len, &line_len);
|
2017-07-27 14:24:49 +02:00
|
|
|
*buf = heap_xalloc((line_len + 3) * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
swprintf(*buf, line_len + 3, fmt, str);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(str);
|
2017-07-19 07:29:05 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 07:28:56 +02:00
|
|
|
static void export_dword_data(WCHAR **buf, DWORD *data)
|
2017-07-19 07:28:47 +02:00
|
|
|
{
|
2017-07-19 07:28:56 +02:00
|
|
|
static const WCHAR fmt[] = {'d','w','o','r','d',':','%','0','8','x',0};
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
*buf = heap_xalloc(15 * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
swprintf(*buf, 15, fmt, *data);
|
2017-07-19 07:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t export_hex_data_type(FILE *fp, DWORD type, BOOL unicode)
|
|
|
|
{
|
|
|
|
static const WCHAR hex[] = {'h','e','x',':',0};
|
|
|
|
static const WCHAR hexp_fmt[] = {'h','e','x','(','%','x',')',':',0};
|
|
|
|
size_t line_len;
|
|
|
|
|
|
|
|
if (type == REG_BINARY)
|
|
|
|
{
|
|
|
|
line_len = lstrlenW(hex);
|
|
|
|
REGPROC_write_line(fp, hex, unicode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-27 14:24:49 +02:00
|
|
|
WCHAR *buf = heap_xalloc(15 * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
line_len = swprintf(buf, 15, hexp_fmt, type);
|
2017-07-19 07:28:56 +02:00
|
|
|
REGPROC_write_line(fp, buf, unicode);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(buf);
|
2017-07-19 07:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return line_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_HEX_CHARS 77
|
|
|
|
|
|
|
|
static void export_hex_data(FILE *fp, WCHAR **buf, DWORD type, DWORD line_len,
|
|
|
|
void *data, DWORD size, BOOL unicode)
|
|
|
|
{
|
|
|
|
static const WCHAR fmt[] = {'%','0','2','x',0};
|
|
|
|
static const WCHAR hex_concat[] = {'\\','\r','\n',' ',' ',0};
|
|
|
|
size_t num_commas, i, pos;
|
|
|
|
|
|
|
|
line_len += export_hex_data_type(fp, type, unicode);
|
|
|
|
|
2017-11-10 13:23:04 +01:00
|
|
|
if (!size) return;
|
|
|
|
|
2017-07-19 07:28:56 +02:00
|
|
|
if (!unicode && (type == REG_EXPAND_SZ || type == REG_MULTI_SZ))
|
|
|
|
data = GetMultiByteStringN(data, size / sizeof(WCHAR), &size);
|
|
|
|
|
|
|
|
num_commas = size - 1;
|
2017-07-27 14:24:49 +02:00
|
|
|
*buf = heap_xalloc(size * 3 * sizeof(WCHAR));
|
2017-07-19 07:28:56 +02:00
|
|
|
|
|
|
|
for (i = 0, pos = 0; i < size; i++)
|
|
|
|
{
|
2019-04-11 10:04:28 +02:00
|
|
|
pos += swprintf(*buf + pos, 3, fmt, ((BYTE *)data)[i]);
|
2017-07-19 07:28:56 +02:00
|
|
|
if (i == num_commas) break;
|
|
|
|
(*buf)[pos++] = ',';
|
|
|
|
(*buf)[pos] = 0;
|
|
|
|
line_len += 3;
|
|
|
|
|
|
|
|
if (line_len >= MAX_HEX_CHARS)
|
|
|
|
{
|
|
|
|
REGPROC_write_line(fp, *buf, unicode);
|
|
|
|
REGPROC_write_line(fp, hex_concat, unicode);
|
|
|
|
line_len = 2;
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 07:29:16 +02:00
|
|
|
static void export_newline(FILE *fp, BOOL unicode)
|
|
|
|
{
|
|
|
|
static const WCHAR newline[] = {'\r','\n',0};
|
|
|
|
|
|
|
|
REGPROC_write_line(fp, newline, unicode);
|
|
|
|
}
|
|
|
|
|
2017-07-19 07:29:22 +02:00
|
|
|
static void export_data(FILE *fp, WCHAR *value_name, DWORD value_len, DWORD type,
|
|
|
|
void *data, size_t size, BOOL unicode)
|
2017-07-19 07:28:56 +02:00
|
|
|
{
|
|
|
|
WCHAR *buf = NULL;
|
2017-07-19 07:29:22 +02:00
|
|
|
size_t line_len = export_value_name(fp, value_name, value_len, unicode);
|
2017-07-19 07:28:56 +02:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2017-07-19 07:29:05 +02:00
|
|
|
case REG_SZ:
|
|
|
|
export_string_data(&buf, data, size);
|
|
|
|
break;
|
2017-07-19 07:28:56 +02:00
|
|
|
case REG_DWORD:
|
2017-11-10 13:23:05 +01:00
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
export_dword_data(&buf, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
2017-07-19 07:28:56 +02:00
|
|
|
case REG_NONE:
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
case REG_BINARY:
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
default:
|
|
|
|
export_hex_data(fp, &buf, type, line_len, data, size, unicode);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-19 07:28:47 +02:00
|
|
|
|
2017-11-10 13:23:04 +01:00
|
|
|
if (size || type == REG_SZ)
|
|
|
|
{
|
|
|
|
REGPROC_write_line(fp, buf, unicode);
|
|
|
|
heap_free(buf);
|
|
|
|
}
|
|
|
|
|
2017-07-19 07:29:16 +02:00
|
|
|
export_newline(fp, unicode);
|
2017-07-19 07:28:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 07:28:29 +02:00
|
|
|
static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
|
|
|
|
{
|
|
|
|
WCHAR *subkey_path;
|
|
|
|
static const WCHAR fmt[] = {'%','s','\\','%','s',0};
|
|
|
|
|
2017-07-27 14:24:49 +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-07-19 07:28:29 +02:00
|
|
|
|
|
|
|
return subkey_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void export_key_name(FILE *fp, WCHAR *name, BOOL unicode)
|
|
|
|
{
|
|
|
|
static const WCHAR fmt[] = {'\r','\n','[','%','s',']','\r','\n',0};
|
|
|
|
WCHAR *buf;
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
buf = heap_xalloc((lstrlenW(name) + 7) * sizeof(WCHAR));
|
2019-04-11 10:04:28 +02:00
|
|
|
swprintf(buf, lstrlenW(name) + 7, fmt, name);
|
2017-07-19 07:28:29 +02:00
|
|
|
REGPROC_write_line(fp, buf, unicode);
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(buf);
|
2017-07-19 07:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_SUBKEY_LEN 257
|
|
|
|
|
2017-07-19 07:28:21 +02:00
|
|
|
static int export_registry_data(FILE *fp, HKEY key, WCHAR *path, BOOL unicode)
|
|
|
|
{
|
2017-07-19 07:28:29 +02:00
|
|
|
LONG rc;
|
|
|
|
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;
|
2017-07-19 07:28:21 +02:00
|
|
|
|
2017-07-19 07:28:29 +02:00
|
|
|
export_key_name(fp, path, unicode);
|
2017-07-19 07:28:21 +02:00
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
|
|
|
|
data = heap_xalloc(max_data_bytes);
|
2017-07-19 07:28:21 +02:00
|
|
|
|
2017-07-19 07:28:29 +02:00
|
|
|
i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
value_len = max_value_len;
|
|
|
|
data_size = max_data_bytes;
|
|
|
|
rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
|
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
2017-07-19 07:29:22 +02:00
|
|
|
export_data(fp, value_name, value_len, type, data, data_size, unicode);
|
2017-07-19 07:28:29 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
if (data_size > max_data_bytes)
|
|
|
|
{
|
2017-07-20 13:33:24 +02:00
|
|
|
max_data_bytes = data_size;
|
2017-07-27 14:25:07 +02:00
|
|
|
data = heap_xrealloc(data, max_data_bytes);
|
2017-07-19 07:28:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_value_len *= 2;
|
2017-07-27 14:25:07 +02:00
|
|
|
value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
|
2017-07-19 07:28:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
2017-07-19 07:28:21 +02:00
|
|
|
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(data);
|
|
|
|
heap_free(value_name);
|
2017-07-19 07:28:21 +02:00
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
|
2017-07-19 07:28:29 +02:00
|
|
|
|
|
|
|
path_len = lstrlenW(path);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
subkey_len = MAX_SUBKEY_LEN;
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
export_registry_data(fp, subkey, subkey_path, unicode);
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
}
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(subkey_path);
|
2017-07-19 07:28:29 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(subkey_name);
|
2017-07-19 07:28:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
static const WCHAR hyphen[] = {'-',0};
|
|
|
|
|
2019-04-01 11:47:50 +02:00
|
|
|
if (!lstrcmpW(file_name, hyphen))
|
2017-07-19 07:28:21 +02:00
|
|
|
{
|
|
|
|
file = stdout;
|
|
|
|
_setmode(_fileno(file), _O_BINARY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static const WCHAR wb_mode[] = {'w','b',0};
|
|
|
|
|
|
|
|
file = _wfopen(file_name, wb_mode);
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
static const WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
|
|
|
|
_wperror(regedit);
|
|
|
|
error_exit(STRING_CANNOT_OPEN_FILE, file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unicode)
|
|
|
|
{
|
|
|
|
static const BYTE bom[] = {0xff,0xfe};
|
|
|
|
static const WCHAR header[] = {'W','i','n','d','o','w','s',' ',
|
|
|
|
'R','e','g','i','s','t','r','y',' ','E','d','i','t','o','r',' ',
|
|
|
|
'V','e','r','s','i','o','n',' ','5','.','0','0','\r','\n'};
|
|
|
|
|
|
|
|
fwrite(bom, sizeof(BYTE), ARRAY_SIZE(bom), file);
|
|
|
|
fwrite(header, sizeof(WCHAR), ARRAY_SIZE(header), file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fputs("REGEDIT4\r\n", file);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HKEY open_export_key(HKEY key_class, WCHAR *subkey, WCHAR *path)
|
|
|
|
{
|
|
|
|
HKEY key;
|
|
|
|
|
|
|
|
if (!RegOpenKeyExW(key_class, subkey, 0, KEY_READ, &key))
|
|
|
|
return key;
|
|
|
|
|
|
|
|
output_message(STRING_OPEN_KEY_FAILED, path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL export_key(WCHAR *file_name, WCHAR *path, BOOL unicode)
|
|
|
|
{
|
|
|
|
HKEY key_class, key;
|
|
|
|
WCHAR *subkey;
|
|
|
|
FILE *fp;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!(key_class = parse_key_name(path, &subkey)))
|
|
|
|
{
|
|
|
|
if (subkey) *(subkey - 1) = 0;
|
|
|
|
output_message(STRING_INVALID_SYSTEM_KEY, path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(key = open_export_key(key_class, subkey, path)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
fp = REGPROC_open_export_file(file_name, unicode);
|
|
|
|
ret = export_registry_data(fp, key, path, unicode);
|
2017-07-19 07:29:16 +02:00
|
|
|
export_newline(fp, unicode);
|
2017-07-19 07:28:21 +02:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
RegCloseKey(key);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL export_all(WCHAR *file_name, WCHAR *path, BOOL unicode)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int i;
|
|
|
|
HKEY classes[] = {HKEY_LOCAL_MACHINE, HKEY_USERS}, key;
|
|
|
|
WCHAR *class_name;
|
|
|
|
|
|
|
|
fp = REGPROC_open_export_file(file_name, unicode);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(classes); i++)
|
|
|
|
{
|
|
|
|
if (!(key = open_export_key(classes[i], NULL, path)))
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:24:49 +02:00
|
|
|
class_name = heap_xalloc((lstrlenW(reg_class_namesW[i]) + 1) * sizeof(WCHAR));
|
2017-07-19 07:28:21 +02:00
|
|
|
lstrcpyW(class_name, reg_class_namesW[i]);
|
|
|
|
|
|
|
|
export_registry_data(fp, classes[i], class_name, unicode);
|
|
|
|
|
2017-07-27 14:24:58 +02:00
|
|
|
heap_free(class_name);
|
2017-07-19 07:28:21 +02:00
|
|
|
RegCloseKey(key);
|
|
|
|
}
|
|
|
|
|
2017-07-19 07:29:16 +02:00
|
|
|
export_newline(fp, unicode);
|
2017-07-19 07:28:21 +02:00
|
|
|
fclose(fp);
|
2017-07-19 07:29:16 +02:00
|
|
|
|
2017-07-19 07:28:21 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL export_registry_key(WCHAR *file_name, WCHAR *path, DWORD format)
|
|
|
|
{
|
|
|
|
BOOL unicode = (format == REG_FORMAT_5);
|
|
|
|
|
|
|
|
if (path && *path)
|
|
|
|
return export_key(file_name, path, unicode);
|
|
|
|
else
|
|
|
|
return export_all(file_name, path, unicode);
|
|
|
|
}
|