2017-08-18 09:29:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Hugh McMaster
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2017-08-18 09:30:28 +00:00
|
|
|
#include <errno.h>
|
2017-08-18 09:30:04 +00:00
|
|
|
#include <stdio.h>
|
2017-08-18 09:29:54 +00:00
|
|
|
#include "reg.h"
|
2021-03-18 11:36:49 +00:00
|
|
|
#include <wine/debug.h>
|
2017-08-18 09:29:54 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(reg);
|
|
|
|
|
2017-08-18 09:30:04 +00:00
|
|
|
static WCHAR *GetWideString(const char *strA)
|
|
|
|
{
|
|
|
|
if (strA)
|
|
|
|
{
|
|
|
|
WCHAR *strW;
|
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
strW = malloc(len * sizeof(WCHAR));
|
2017-08-18 09:30:04 +00:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
|
|
|
|
return strW;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:50 +00:00
|
|
|
static WCHAR *GetWideStringN(const char *strA, int size, DWORD *len)
|
|
|
|
{
|
|
|
|
if (strA)
|
|
|
|
{
|
|
|
|
WCHAR *strW;
|
|
|
|
*len = MultiByteToWideChar(CP_ACP, 0, strA, size, NULL, 0);
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
strW = malloc(*len * sizeof(WCHAR));
|
2017-08-18 09:30:50 +00:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, strA, size, strW, *len);
|
|
|
|
return strW;
|
|
|
|
}
|
|
|
|
*len = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:04 +00: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 */
|
2017-08-18 09:30:17 +00:00
|
|
|
LINE_START, /* at the beginning of a registry line */
|
|
|
|
KEY_NAME, /* parsing a key name */
|
2017-08-18 09:31:17 +00:00
|
|
|
DELETE_KEY, /* deleting a registry key */
|
2017-08-18 09:30:17 +00: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 */
|
2017-08-18 09:31:08 +00:00
|
|
|
DELETE_VALUE, /* deleting a registry value */
|
2017-08-18 09:30:28 +00:00
|
|
|
DATA_TYPE, /* parsing the registry data type */
|
|
|
|
STRING_DATA, /* parsing REG_SZ data */
|
2017-08-18 09:30:38 +00:00
|
|
|
DWORD_DATA, /* parsing DWORD data */
|
2017-08-18 09:30:50 +00:00
|
|
|
HEX_DATA, /* parsing REG_BINARY, REG_NONE, REG_EXPAND_SZ or REG_MULTI_SZ data */
|
|
|
|
EOL_BACKSLASH, /* preparing to parse multiple lines of hex data */
|
|
|
|
HEX_MULTILINE, /* parsing multiple lines of hex data */
|
2017-08-18 09:30:59 +00:00
|
|
|
UNKNOWN_DATA, /* parsing an unhandled or invalid data type */
|
2017-08-18 09:30:04 +00: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 */
|
2021-07-08 11:12:16 +00:00
|
|
|
REGSAM sam; /* 32-bit or 64-bit registry view (if set) */
|
2017-08-18 09:30:04 +00:00
|
|
|
HKEY hkey; /* current registry key */
|
|
|
|
WCHAR *key_name; /* current key name */
|
|
|
|
WCHAR *value_name; /* value name */
|
2017-08-18 09:30:28 +00:00
|
|
|
DWORD parse_type; /* generic data type for parsing */
|
2017-08-18 09:30:04 +00:00
|
|
|
DWORD data_type; /* data type */
|
|
|
|
void *data; /* value data */
|
|
|
|
DWORD data_size; /* size of the data (in bytes) */
|
2017-08-18 09:30:50 +00:00
|
|
|
BOOL backslash; /* TRUE if the current line contains a backslash */
|
2017-08-18 09:30:04 +00: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);
|
2017-08-18 09:30:17 +00:00
|
|
|
static WCHAR *line_start_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *key_name_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:31:17 +00:00
|
|
|
static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:30:17 +00: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);
|
2017-08-18 09:31:08 +00:00
|
|
|
static WCHAR *delete_value_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:30:28 +00:00
|
|
|
static WCHAR *data_type_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *string_data_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:30:38 +00:00
|
|
|
static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:30:50 +00:00
|
|
|
static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *eol_backslash_state(struct parser *parser, WCHAR *pos);
|
|
|
|
static WCHAR *hex_multiline_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:30:59 +00:00
|
|
|
static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos);
|
2017-08-18 09:30:04 +00: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 */
|
2017-08-18 09:30:17 +00:00
|
|
|
line_start_state, /* LINE_START */
|
|
|
|
key_name_state, /* KEY_NAME */
|
2017-08-18 09:31:17 +00:00
|
|
|
delete_key_state, /* DELETE_KEY */
|
2017-08-18 09:30:17 +00:00
|
|
|
default_value_name_state, /* DEFAULT_VALUE_NAME */
|
|
|
|
quoted_value_name_state, /* QUOTED_VALUE_NAME */
|
|
|
|
data_start_state, /* DATA_START */
|
2017-08-18 09:31:08 +00:00
|
|
|
delete_value_state, /* DELETE_VALUE */
|
2017-08-18 09:30:28 +00:00
|
|
|
data_type_state, /* DATA_TYPE */
|
|
|
|
string_data_state, /* STRING_DATA */
|
2017-08-18 09:30:38 +00:00
|
|
|
dword_data_state, /* DWORD_DATA */
|
2017-08-18 09:30:50 +00:00
|
|
|
hex_data_state, /* HEX_DATA */
|
|
|
|
eol_backslash_state, /* EOL_BACKSLASH */
|
|
|
|
hex_multiline_state, /* HEX_MULTILINE */
|
2017-08-18 09:30:59 +00:00
|
|
|
unknown_data_state, /* UNKNOWN_DATA */
|
2017-08-18 09:30:04 +00: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;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:38 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Converts a hex representation of a DWORD into a DWORD.
|
|
|
|
*/
|
|
|
|
static BOOL convert_hex_to_dword(WCHAR *str, DWORD *dw)
|
|
|
|
{
|
|
|
|
WCHAR *p, *end;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (*str == ' ' || *str == '\t') str++;
|
|
|
|
if (!*str) goto error;
|
|
|
|
|
|
|
|
p = str;
|
2019-04-01 09:44:50 +00:00
|
|
|
while (iswxdigit(*p))
|
2017-08-18 09:30:38 +00:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (count > 8) goto error;
|
|
|
|
|
|
|
|
end = p;
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
if (*p && *p != ';') goto error;
|
|
|
|
|
|
|
|
*end = 0;
|
2019-04-01 09:44:50 +00:00
|
|
|
*dw = wcstoul(str, &end, 16);
|
2017-08-18 09:30:38 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:50 +00: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.
|
|
|
|
*/
|
|
|
|
static BOOL convert_hex_csv_to_hex(struct parser *parser, WCHAR **str)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
BYTE *d;
|
|
|
|
WCHAR *s;
|
|
|
|
|
|
|
|
parser->backslash = FALSE;
|
|
|
|
|
|
|
|
/* The worst case is 1 digit + 1 comma per byte */
|
|
|
|
size = ((lstrlenW(*str) + 1) / 2) + parser->data_size;
|
2021-03-24 08:08:58 +00:00
|
|
|
parser->data = realloc(parser->data, size);
|
2017-08-18 09:30:50 +00:00
|
|
|
|
|
|
|
s = *str;
|
|
|
|
d = (BYTE *)parser->data + parser->data_size;
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
WCHAR *end;
|
|
|
|
unsigned long wc;
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
wc = wcstoul(s, &end, 16);
|
2017-08-18 09:30:50 +00: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;
|
|
|
|
}
|
|
|
|
else if (*end == ';')
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = wc;
|
|
|
|
parser->data_size++;
|
|
|
|
|
|
|
|
if (*end && *end != ',')
|
|
|
|
{
|
|
|
|
while (*end == ' ' || *end == '\t') end++;
|
|
|
|
if (*end && *end != ';') return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*end) end++;
|
|
|
|
s = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:28 +00: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.
|
|
|
|
*
|
|
|
|
* Returns TRUE or FALSE to indicate whether a data type was found.
|
|
|
|
*/
|
|
|
|
static BOOL parse_data_type(struct parser *parser, WCHAR **line)
|
|
|
|
{
|
|
|
|
struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
|
|
|
|
|
|
|
|
static const struct data_type data_types[] = {
|
2021-03-30 12:00:17 +00:00
|
|
|
/* tag len type parse type */
|
|
|
|
{ L"\"", 1, REG_SZ, REG_SZ },
|
|
|
|
{ L"hex:", 4, REG_BINARY, REG_BINARY },
|
|
|
|
{ L"dword:", 6, REG_DWORD, REG_DWORD },
|
|
|
|
{ L"hex(", 4, -1, REG_BINARY }, /* REG_NONE, REG_EXPAND_SZ, REG_MULTI_SZ */
|
|
|
|
{ NULL, 0, 0, 0 }
|
2017-08-18 09:30:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct data_type *ptr;
|
|
|
|
|
|
|
|
for (ptr = data_types; ptr->tag; ptr++)
|
|
|
|
{
|
2019-04-01 09:44:50 +00:00
|
|
|
if (wcsncmp(ptr->tag, *line, ptr->len))
|
2017-08-18 09:30:28 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
parser->parse_type = ptr->parse_type;
|
|
|
|
parser->data_type = ptr->parse_type;
|
|
|
|
*line += ptr->len;
|
|
|
|
|
|
|
|
if (ptr->type == -1)
|
|
|
|
{
|
|
|
|
WCHAR *end;
|
|
|
|
DWORD val;
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
if (!**line || towlower((*line)[1]) == 'x')
|
2017-08-18 09:30:28 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* "hex(xx):" is special */
|
|
|
|
val = wcstoul(*line, &end, 16);
|
|
|
|
if (*end != ')' || *(end + 1) != ':' || (val == ~0u && errno == ERANGE))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
parser->data_type = val;
|
|
|
|
*line = end + 2;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:17 +00: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.
|
|
|
|
*/
|
|
|
|
static BOOL unescape_string(WCHAR *str, WCHAR **unparsed)
|
|
|
|
{
|
|
|
|
int str_idx = 0; /* current character under analysis */
|
|
|
|
int val_idx = 0; /* the last character of the unescaped string */
|
|
|
|
int len = lstrlenW(str);
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
for (str_idx = 0; str_idx < len; str_idx++, val_idx++)
|
|
|
|
{
|
|
|
|
if (str[str_idx] == '\\')
|
|
|
|
{
|
|
|
|
str_idx++;
|
|
|
|
switch (str[str_idx])
|
|
|
|
{
|
|
|
|
case 'n':
|
|
|
|
str[val_idx] = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
str[val_idx] = '\r';
|
|
|
|
break;
|
|
|
|
case '0':
|
2021-03-12 11:32:24 +00:00
|
|
|
return FALSE;
|
2017-08-18 09:30:17 +00:00
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
str[val_idx] = str[str_idx];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!str[str_idx]) return FALSE;
|
|
|
|
output_message(STRING_ESCAPE_SEQUENCE, str[str_idx]);
|
|
|
|
str[val_idx] = str[str_idx];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (str[str_idx] == '"')
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
str[val_idx] = str[str_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (str[str_idx] == '"');
|
|
|
|
*unparsed = str + str_idx + 1;
|
|
|
|
str[val_idx] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:04 +00:00
|
|
|
static HKEY parse_key_name(WCHAR *key_name, WCHAR **key_path)
|
|
|
|
{
|
|
|
|
if (!key_name) return 0;
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
*key_path = wcschr(key_name, '\\');
|
2017-08-18 09:30:04 +00:00
|
|
|
if (*key_path) (*key_path)++;
|
|
|
|
|
|
|
|
return path_get_rootkey(key_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_key(struct parser *parser)
|
|
|
|
{
|
|
|
|
if (parser->hkey)
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
free(parser->key_name);
|
2017-08-18 09:30:04 +00:00
|
|
|
parser->key_name = NULL;
|
|
|
|
|
|
|
|
RegCloseKey(parser->hkey);
|
|
|
|
parser->hkey = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG open_key(struct parser *parser, WCHAR *path)
|
|
|
|
{
|
|
|
|
HKEY key_class;
|
|
|
|
WCHAR *key_path;
|
|
|
|
LONG res;
|
|
|
|
|
|
|
|
close_key(parser);
|
|
|
|
|
|
|
|
/* Get the registry class */
|
|
|
|
if (!path || !(key_class = parse_key_name(path, &key_path)))
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
res = RegCreateKeyExW(key_class, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
|
2021-07-08 11:12:16 +00:00
|
|
|
KEY_ALL_ACCESS|parser->sam, NULL, &parser->hkey, NULL);
|
2017-08-18 09:30:04 +00:00
|
|
|
|
|
|
|
if (res == ERROR_SUCCESS)
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
parser->key_name = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
|
2017-08-18 09:30:04 +00:00
|
|
|
lstrcpyW(parser->key_name, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
parser->hkey = NULL;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_parser_data(struct parser *parser)
|
|
|
|
{
|
2017-08-18 09:30:50 +00:00
|
|
|
if (parser->parse_type == REG_DWORD || parser->parse_type == REG_BINARY)
|
2021-03-24 08:08:58 +00:00
|
|
|
free(parser->data);
|
2017-08-18 09:30:38 +00:00
|
|
|
|
2017-08-18 09:30:04 +00:00
|
|
|
parser->data = NULL;
|
|
|
|
parser->data_size = 0;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:50 +00:00
|
|
|
static void prepare_hex_string_data(struct parser *parser)
|
|
|
|
{
|
2018-04-23 13:37:28 +00:00
|
|
|
if (parser->data_type == REG_EXPAND_SZ || parser->data_type == REG_MULTI_SZ ||
|
|
|
|
parser->data_type == REG_SZ)
|
2017-08-18 09:30:50 +00:00
|
|
|
{
|
2017-08-28 12:50:38 +00:00
|
|
|
if (parser->is_unicode)
|
2017-08-18 09:30:50 +00:00
|
|
|
{
|
2017-08-28 12:50:38 +00:00
|
|
|
WCHAR *data = parser->data;
|
|
|
|
DWORD len = parser->data_size / sizeof(WCHAR);
|
2017-08-18 09:30:50 +00:00
|
|
|
|
2017-08-28 12:50:38 +00:00
|
|
|
if (data[len - 1] != 0)
|
|
|
|
{
|
|
|
|
data[len] = 0;
|
|
|
|
parser->data_size += sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-08-18 09:30:50 +00:00
|
|
|
{
|
2017-08-28 12:50:38 +00:00
|
|
|
BYTE *data = parser->data;
|
|
|
|
|
|
|
|
if (data[parser->data_size - 1] != 0)
|
|
|
|
{
|
|
|
|
data[parser->data_size] = 0;
|
|
|
|
parser->data_size++;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:50 +00:00
|
|
|
parser->data = GetWideStringN(parser->data, parser->data_size, &parser->data_size);
|
|
|
|
parser->data_size *= sizeof(WCHAR);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(data);
|
2017-08-18 09:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:04 +00:00
|
|
|
enum reg_versions {
|
|
|
|
REG_VERSION_31,
|
|
|
|
REG_VERSION_40,
|
|
|
|
REG_VERSION_50,
|
|
|
|
REG_VERSION_FUZZY,
|
|
|
|
REG_VERSION_INVALID
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum reg_versions parse_file_header(const WCHAR *s)
|
|
|
|
{
|
2021-03-30 12:00:17 +00:00
|
|
|
static const WCHAR *header_31 = L"REGEDIT";
|
2017-08-18 09:30:04 +00:00
|
|
|
|
|
|
|
while (*s == ' ' || *s == '\t') s++;
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
if (!lstrcmpW(s, header_31))
|
2017-08-18 09:30:04 +00:00
|
|
|
return REG_VERSION_31;
|
|
|
|
|
2021-03-30 12:00:17 +00:00
|
|
|
if (!lstrcmpW(s, L"REGEDIT4"))
|
2017-08-18 09:30:04 +00:00
|
|
|
return REG_VERSION_40;
|
|
|
|
|
2021-03-30 12:00:17 +00:00
|
|
|
if (!lstrcmpW(s, L"Windows Registry Editor Version 5.00"))
|
2017-08-18 09:30:04 +00:00
|
|
|
return REG_VERSION_50;
|
|
|
|
|
|
|
|
/* 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 09:44:50 +00:00
|
|
|
if (!wcsncmp(s, header_31, 7)) /* "REGEDIT" without NUL */
|
2017-08-18 09:30:04 +00:00
|
|
|
return REG_VERSION_FUZZY;
|
|
|
|
|
|
|
|
return REG_VERSION_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
header = malloc((lstrlenW(line) + 3) * sizeof(WCHAR));
|
2017-08-18 09:30:04 +00:00
|
|
|
header[0] = parser->two_wchars[0];
|
|
|
|
header[1] = parser->two_wchars[1];
|
|
|
|
lstrcpyW(header + 2, line);
|
|
|
|
parser->reg_version = parse_file_header(header);
|
2021-03-24 08:08:58 +00:00
|
|
|
free(header);
|
2017-08-18 09:30:04 +00: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:
|
2017-08-18 09:30:17 +00:00
|
|
|
set_state(parser, LINE_START);
|
|
|
|
break;
|
2017-08-18 09:30:04 +00:00
|
|
|
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;
|
|
|
|
unsigned int key_end = 0;
|
|
|
|
|
|
|
|
if (!(line = get_line(parser->file)))
|
|
|
|
return NULL;
|
|
|
|
|
2021-03-30 12:00:17 +00:00
|
|
|
if (wcsncmp(line, L"HKEY_CLASSES_ROOT", 17)) /* "HKEY_CLASSES_ROOT" without NUL */
|
2017-08-18 09:30:04 +00:00
|
|
|
return line;
|
|
|
|
|
|
|
|
/* get key name */
|
2019-04-01 09:44:50 +00:00
|
|
|
while (line[key_end] && !iswspace(line[key_end])) key_end++;
|
2017-08-18 09:30:04 +00: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;
|
|
|
|
|
|
|
|
if (open_key(parser, line) != ERROR_SUCCESS)
|
|
|
|
{
|
2021-04-01 12:20:50 +00:00
|
|
|
output_message(STRING_KEY_IMPORT_FAILED, line);
|
2017-08-18 09:30:04 +00:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:17 +00:00
|
|
|
/* 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 '[':
|
|
|
|
set_state(parser, KEY_NAME);
|
|
|
|
return p + 1;
|
|
|
|
case '@':
|
|
|
|
set_state(parser, DEFAULT_VALUE_NAME);
|
|
|
|
return p;
|
|
|
|
case '"':
|
|
|
|
set_state(parser, QUOTED_VALUE_NAME);
|
|
|
|
return p + 1;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser KEY_NAME state */
|
|
|
|
static WCHAR *key_name_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos, *key_end;
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
if (*p == ' ' || *p == '\t' || !(key_end = wcsrchr(p, ']')))
|
2017-08-18 09:30:17 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
*key_end = 0;
|
|
|
|
|
|
|
|
if (*p == '-')
|
|
|
|
{
|
2017-08-18 09:31:17 +00:00
|
|
|
set_state(parser, DELETE_KEY);
|
|
|
|
return p + 1;
|
2017-08-18 09:30:17 +00:00
|
|
|
}
|
|
|
|
else if (open_key(parser, p) != ERROR_SUCCESS)
|
2021-04-01 12:20:50 +00:00
|
|
|
output_message(STRING_KEY_IMPORT_FAILED, p);
|
2017-08-18 09:30:17 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:31:17 +00:00
|
|
|
/* handler for parser DELETE_KEY state */
|
|
|
|
static WCHAR *delete_key_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *p = pos;
|
|
|
|
|
2017-08-25 05:37:06 +00:00
|
|
|
close_key(parser);
|
|
|
|
|
2017-08-18 09:31:17 +00:00
|
|
|
if (*p == 'H' || *p == 'h')
|
|
|
|
{
|
|
|
|
HKEY root;
|
|
|
|
WCHAR *path;
|
|
|
|
|
|
|
|
root = parse_key_name(p, &path);
|
|
|
|
|
|
|
|
if (root && path && *path)
|
|
|
|
RegDeleteTreeW(root, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:17 +00:00
|
|
|
/* handler for parser DEFAULT_VALUE_NAME state */
|
|
|
|
static WCHAR *default_value_name_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
2021-03-24 08:08:58 +00:00
|
|
|
free(parser->value_name);
|
2017-08-18 09:30:17 +00: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;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(parser->value_name);
|
2019-01-29 19:38:07 +00:00
|
|
|
parser->value_name = NULL;
|
2017-08-18 09:30:17 +00:00
|
|
|
|
|
|
|
if (!unescape_string(val_name, &p))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
/* copy the value name in case we need to parse multiple lines and the buffer is overwritten */
|
2021-03-24 08:08:58 +00:00
|
|
|
parser->value_name = malloc((lstrlenW(val_name) + 1) * sizeof(WCHAR));
|
2017-08-18 09:30:17 +00:00
|
|
|
lstrcpyW(parser->value_name, val_name);
|
|
|
|
|
|
|
|
set_state(parser, DATA_START);
|
|
|
|
return p;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return val_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 invalid;
|
|
|
|
p++;
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
|
|
|
|
/* trim trailing whitespace */
|
2019-04-01 09:44:50 +00:00
|
|
|
len = lstrlenW(p);
|
2017-08-18 09:30:17 +00:00
|
|
|
while (len > 0 && (p[len - 1] == ' ' || p[len - 1] == '\t')) len--;
|
|
|
|
p[len] = 0;
|
|
|
|
|
2017-08-18 09:30:28 +00:00
|
|
|
if (*p == '-')
|
2017-08-18 09:31:08 +00:00
|
|
|
set_state(parser, DELETE_VALUE);
|
2017-08-18 09:30:28 +00:00
|
|
|
else
|
|
|
|
set_state(parser, DATA_TYPE);
|
|
|
|
return p;
|
2017-08-18 09:30:17 +00:00
|
|
|
|
|
|
|
invalid:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:31:08 +00:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
RegDeleteValueW(parser->hkey, parser->value_name);
|
|
|
|
|
|
|
|
done:
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:28 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parser->parse_type)
|
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
set_state(parser, STRING_DATA);
|
|
|
|
break;
|
|
|
|
case REG_DWORD:
|
2017-08-18 09:30:38 +00:00
|
|
|
set_state(parser, DWORD_DATA);
|
|
|
|
break;
|
2017-08-18 09:30:28 +00:00
|
|
|
case REG_BINARY: /* all hex data types, including undefined */
|
2017-08-18 09:30:50 +00:00
|
|
|
set_state(parser, HEX_DATA);
|
|
|
|
break;
|
2017-08-18 09:30:28 +00:00
|
|
|
default:
|
2017-08-18 09:30:59 +00:00
|
|
|
set_state(parser, UNKNOWN_DATA);
|
2017-08-18 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handler for parser STRING_DATA state */
|
|
|
|
static WCHAR *string_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line;
|
|
|
|
|
|
|
|
parser->data = pos;
|
|
|
|
|
|
|
|
if (!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:
|
|
|
|
free_parser_data(parser);
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:38 +00:00
|
|
|
/* handler for parser DWORD_DATA state */
|
|
|
|
static WCHAR *dword_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line = pos;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
parser->data = malloc(sizeof(DWORD));
|
2017-08-18 09:30:38 +00:00
|
|
|
|
|
|
|
if (!convert_hex_to_dword(line, parser->data))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
parser->data_size = sizeof(DWORD);
|
|
|
|
|
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return line;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
free_parser_data(parser);
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:50 +00:00
|
|
|
/* handler for parser HEX_DATA state */
|
|
|
|
static WCHAR *hex_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
WCHAR *line = pos;
|
|
|
|
|
2017-10-16 10:27:25 +00:00
|
|
|
if (!*line)
|
|
|
|
goto set_value;
|
|
|
|
|
2017-08-18 09:30:50 +00:00
|
|
|
if (!convert_hex_csv_to_hex(parser, &line))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
if (parser->backslash)
|
|
|
|
{
|
|
|
|
set_state(parser, EOL_BACKSLASH);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_hex_string_data(parser);
|
|
|
|
|
2017-10-16 10:27:25 +00:00
|
|
|
set_value:
|
2017-08-18 09:30:50 +00:00
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return line;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
free_parser_data(parser);
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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:
|
|
|
|
free_parser_data(parser);
|
|
|
|
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)))
|
|
|
|
{
|
|
|
|
prepare_hex_string_data(parser);
|
|
|
|
set_state(parser, SET_VALUE);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*line == ' ' || *line == '\t') line++;
|
|
|
|
if (!*line || *line == ';') return line;
|
|
|
|
|
2019-04-01 09:44:50 +00:00
|
|
|
if (!iswxdigit(*line)) goto invalid;
|
2017-08-18 09:30:50 +00:00
|
|
|
|
|
|
|
set_state(parser, HEX_DATA);
|
|
|
|
return line;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
free_parser_data(parser);
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:59 +00:00
|
|
|
/* handler for parser UNKNOWN_DATA state */
|
|
|
|
static WCHAR *unknown_data_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
2022-02-04 08:57:29 +00:00
|
|
|
FIXME("Unknown registry data type [0x%lx]\n", parser->data_type);
|
2017-08-18 09:30:59 +00:00
|
|
|
|
|
|
|
set_state(parser, LINE_START);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:30:04 +00:00
|
|
|
/* handler for parser SET_VALUE state */
|
|
|
|
static WCHAR *set_value_state(struct parser *parser, WCHAR *pos)
|
|
|
|
{
|
|
|
|
RegSetValueExW(parser->hkey, parser->value_name, 0, parser->data_type,
|
|
|
|
parser->data, parser->data_size);
|
|
|
|
|
|
|
|
free_parser_data(parser);
|
|
|
|
|
2017-08-18 09:30:28 +00:00
|
|
|
if (parser->reg_version == REG_VERSION_31)
|
|
|
|
set_state(parser, PARSE_WIN31_LINE);
|
|
|
|
else
|
|
|
|
set_state(parser, LINE_START);
|
2017-08-18 09:30:04 +00:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REG_VAL_BUF_SIZE 4096
|
|
|
|
|
|
|
|
static WCHAR *get_lineA(FILE *fp)
|
|
|
|
{
|
|
|
|
static WCHAR *lineW;
|
|
|
|
static size_t size;
|
|
|
|
static char *buf, *next;
|
|
|
|
char *line;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(lineW);
|
2017-08-18 09:30:04 +00:00
|
|
|
|
|
|
|
if (!fp) goto cleanup;
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
size = REG_VAL_BUF_SIZE;
|
2021-03-24 08:08:58 +00:00
|
|
|
buf = malloc(size);
|
2017-08-18 09:30:04 +00:00
|
|
|
*buf = 0;
|
|
|
|
next = buf;
|
|
|
|
}
|
|
|
|
line = next;
|
|
|
|
|
|
|
|
while (next)
|
|
|
|
{
|
|
|
|
char *p = strpbrk(line, "\r\n");
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
size_t len, count;
|
|
|
|
len = strlen(next);
|
|
|
|
memmove(buf, next, len + 1);
|
|
|
|
if (size - len < 3)
|
|
|
|
{
|
|
|
|
size *= 2;
|
2021-03-24 08:08:58 +00:00
|
|
|
buf = realloc(buf, size);
|
2017-08-18 09:30:04 +00:00
|
|
|
}
|
|
|
|
if (!(count = fread(buf + len, 1, size - len - 1, fp)))
|
|
|
|
{
|
|
|
|
next = NULL;
|
|
|
|
lineW = GetWideString(buf);
|
|
|
|
return lineW;
|
|
|
|
}
|
|
|
|
buf[len + count] = 0;
|
|
|
|
next = buf;
|
|
|
|
line = buf;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
next = p + 1;
|
|
|
|
if (*p == '\r' && *(p + 1) == '\n') next++;
|
|
|
|
*p = 0;
|
|
|
|
lineW = GetWideString(line);
|
|
|
|
return lineW;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
lineW = NULL;
|
2021-03-24 08:08:58 +00:00
|
|
|
free(buf);
|
2017-08-18 09:30:04 +00:00
|
|
|
size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WCHAR *get_lineW(FILE *fp)
|
|
|
|
{
|
|
|
|
static size_t size;
|
|
|
|
static WCHAR *buf, *next;
|
|
|
|
WCHAR *line;
|
|
|
|
|
|
|
|
if (!fp) goto cleanup;
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
size = REG_VAL_BUF_SIZE;
|
2021-03-24 08:08:58 +00:00
|
|
|
buf = malloc(size * sizeof(WCHAR));
|
2017-08-18 09:30:04 +00:00
|
|
|
*buf = 0;
|
|
|
|
next = buf;
|
|
|
|
}
|
|
|
|
line = next;
|
|
|
|
|
|
|
|
while (next)
|
|
|
|
{
|
2021-03-30 12:00:17 +00:00
|
|
|
WCHAR *p = wcspbrk(line, L"\r\n");
|
2017-08-18 09:30:04 +00:00
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
size_t len, count;
|
2019-04-01 09:44:50 +00:00
|
|
|
len = lstrlenW(next);
|
2017-08-18 09:30:04 +00:00
|
|
|
memmove(buf, next, (len + 1) * sizeof(WCHAR));
|
|
|
|
if (size - len < 3)
|
|
|
|
{
|
|
|
|
size *= 2;
|
2021-03-24 08:08:58 +00:00
|
|
|
buf = realloc(buf, size * sizeof(WCHAR));
|
2017-08-18 09:30:04 +00:00
|
|
|
}
|
|
|
|
if (!(count = fread(buf + len, sizeof(WCHAR), size - len - 1, fp)))
|
|
|
|
{
|
|
|
|
next = NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
buf[len + count] = 0;
|
|
|
|
next = buf;
|
|
|
|
line = buf;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
next = p + 1;
|
|
|
|
if (*p == '\r' && *(p + 1) == '\n') next++;
|
|
|
|
*p = 0;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2021-03-24 08:08:58 +00:00
|
|
|
free(buf);
|
2017-08-18 09:30:04 +00:00
|
|
|
size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-18 11:36:51 +00:00
|
|
|
int reg_import(int argc, WCHAR *argvW[])
|
2017-08-18 09:29:54 +00:00
|
|
|
{
|
2021-03-18 11:36:51 +00:00
|
|
|
WCHAR *filename, *pos;
|
2017-08-18 09:30:04 +00:00
|
|
|
FILE *fp;
|
|
|
|
BYTE s[2];
|
|
|
|
struct parser parser;
|
2021-03-18 11:36:51 +00:00
|
|
|
|
2021-04-08 12:10:54 +00:00
|
|
|
if (argc > 4) goto invalid;
|
|
|
|
|
2021-07-08 11:12:16 +00:00
|
|
|
parser.sam = 0;
|
|
|
|
|
2021-04-08 12:10:54 +00:00
|
|
|
if (argc == 4)
|
2021-03-18 11:36:51 +00:00
|
|
|
{
|
2021-04-08 12:10:54 +00:00
|
|
|
WCHAR *str = argvW[3];
|
|
|
|
|
|
|
|
if (*str != '/' && *str != '-')
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
str++;
|
|
|
|
|
2021-07-08 11:12:16 +00:00
|
|
|
if (!lstrcmpiW(str, L"reg:32"))
|
|
|
|
parser.sam = KEY_WOW64_32KEY;
|
|
|
|
else if (!lstrcmpiW(str, L"reg:64"))
|
|
|
|
parser.sam = KEY_WOW64_64KEY;
|
|
|
|
else
|
2021-04-08 12:10:54 +00:00
|
|
|
goto invalid;
|
2021-03-18 11:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filename = argvW[2];
|
2017-08-18 09:30:04 +00:00
|
|
|
|
2021-03-30 12:00:17 +00:00
|
|
|
fp = _wfopen(filename, L"rb");
|
2017-08-18 09:30:04 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
output_message(STRING_FILE_NOT_FOUND, filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(s, sizeof(WCHAR), 1, fp) != 1)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
parser.is_unicode = (s[0] == 0xff && s[1] == 0xfe);
|
|
|
|
get_line = parser.is_unicode ? get_lineW : get_lineA;
|
|
|
|
|
|
|
|
parser.file = fp;
|
|
|
|
parser.two_wchars[0] = s[0];
|
|
|
|
parser.two_wchars[1] = s[1];
|
|
|
|
parser.reg_version = -1;
|
|
|
|
parser.hkey = NULL;
|
|
|
|
parser.key_name = NULL;
|
2017-08-18 09:30:17 +00:00
|
|
|
parser.value_name = NULL;
|
2017-08-18 09:30:28 +00:00
|
|
|
parser.parse_type = 0;
|
2017-08-18 09:30:04 +00:00
|
|
|
parser.data_type = 0;
|
|
|
|
parser.data = NULL;
|
|
|
|
parser.data_size = 0;
|
2017-08-18 09:30:50 +00:00
|
|
|
parser.backslash = FALSE;
|
2017-08-18 09:30:04 +00:00
|
|
|
parser.state = HEADER;
|
|
|
|
|
|
|
|
pos = parser.two_wchars;
|
|
|
|
|
|
|
|
/* parser main loop */
|
|
|
|
while (pos)
|
|
|
|
pos = (parser_funcs[parser.state])(&parser, pos);
|
|
|
|
|
|
|
|
if (parser.reg_version == REG_VERSION_INVALID)
|
|
|
|
goto error;
|
|
|
|
|
2021-03-24 08:08:58 +00:00
|
|
|
free(parser.value_name);
|
2017-08-18 09:30:04 +00:00
|
|
|
close_key(&parser);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
fclose(fp);
|
2017-08-18 09:29:54 +00:00
|
|
|
return 1;
|
2021-04-08 12:10:54 +00:00
|
|
|
|
|
|
|
invalid:
|
|
|
|
output_message(STRING_INVALID_SYNTAX);
|
|
|
|
output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
|
|
|
|
return 1;
|
2017-08-18 09:29:54 +00:00
|
|
|
}
|