1365 lines
46 KiB
C
1365 lines
46 KiB
C
/*
|
|
* Registry processing routines. Routines, common for registry
|
|
* processing frontends.
|
|
*
|
|
* Copyright 1999 Sylvain St-Germain
|
|
* Copyright 2002 Andriy Palamarchuk
|
|
* Copyright 2008 Alexander N. Sørnes <alex@thehandofagony.com>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
*/
|
|
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <windows.h>
|
|
#include <winnt.h>
|
|
#include <winreg.h>
|
|
#include <assert.h>
|
|
#include <wine/unicode.h>
|
|
#include "regproc.h"
|
|
|
|
#define REG_VAL_BUF_SIZE 4096
|
|
|
|
/* maximal number of characters in hexadecimal data line,
|
|
not including '\' character */
|
|
#define REG_FILE_HEX_LINE_LEN 76
|
|
|
|
static const CHAR *reg_class_names[] = {
|
|
"HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
|
|
"HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_DYN_DATA"
|
|
};
|
|
|
|
#define REG_CLASS_NUMBER (sizeof(reg_class_names) / sizeof(reg_class_names[0]))
|
|
|
|
extern const WCHAR* reg_class_namesW[];
|
|
|
|
static HKEY reg_class_keys[REG_CLASS_NUMBER] = {
|
|
HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT,
|
|
HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_DYN_DATA
|
|
};
|
|
|
|
/* return values */
|
|
#define NOT_ENOUGH_MEMORY 1
|
|
#define IO_ERROR 2
|
|
|
|
/* processing macros */
|
|
|
|
/* common check of memory allocation results */
|
|
#define CHECK_ENOUGH_MEMORY(p) \
|
|
if (!(p)) \
|
|
{ \
|
|
fprintf(stderr,"%s: file %s, line %d: Not enough memory\n", \
|
|
getAppName(), __FILE__, __LINE__); \
|
|
exit(NOT_ENOUGH_MEMORY); \
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Allocates memory and convers input from multibyte to wide chars
|
|
* Returned string must be freed by the caller
|
|
*/
|
|
WCHAR* GetWideString(const char* strA)
|
|
{
|
|
if(strA)
|
|
{
|
|
WCHAR* strW = NULL;
|
|
int len = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
|
|
|
|
strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
|
|
CHECK_ENOUGH_MEMORY(strW);
|
|
MultiByteToWideChar(CP_ACP, 0, strA, -1, strW, len);
|
|
return strW;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Allocates memory and convers input from multibyte to wide chars
|
|
* Returned string must be freed by the caller
|
|
*/
|
|
WCHAR* GetWideStringN(const char* strA, int chars, DWORD *len)
|
|
{
|
|
if(strA)
|
|
{
|
|
WCHAR* strW = NULL;
|
|
*len = MultiByteToWideChar(CP_ACP, 0, strA, chars, NULL, 0);
|
|
|
|
strW = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(WCHAR));
|
|
CHECK_ENOUGH_MEMORY(strW);
|
|
MultiByteToWideChar(CP_ACP, 0, strA, chars, strW, *len);
|
|
return strW;
|
|
}
|
|
*len = 0;
|
|
return NULL;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Allocates memory and convers input from wide chars to multibyte
|
|
* Returned string must be freed by the caller
|
|
*/
|
|
char* GetMultiByteString(const WCHAR* strW)
|
|
{
|
|
if(strW)
|
|
{
|
|
char* strA = NULL;
|
|
int len = WideCharToMultiByte(CP_ACP, 0, strW, -1, NULL, 0, NULL, NULL);
|
|
|
|
strA = HeapAlloc(GetProcessHeap(), 0, len);
|
|
CHECK_ENOUGH_MEMORY(strA);
|
|
WideCharToMultiByte(CP_ACP, 0, strW, -1, strA, len, NULL, NULL);
|
|
return strA;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Allocates memory and convers input from wide chars to multibyte
|
|
* Returned string must be freed by the caller
|
|
*/
|
|
char* GetMultiByteStringN(const WCHAR* strW, int chars, DWORD* len)
|
|
{
|
|
if(strW)
|
|
{
|
|
char* strA = NULL;
|
|
*len = WideCharToMultiByte(CP_ACP, 0, strW, chars, NULL, 0, NULL, NULL);
|
|
|
|
strA = HeapAlloc(GetProcessHeap(), 0, *len);
|
|
CHECK_ENOUGH_MEMORY(strA);
|
|
WideCharToMultiByte(CP_ACP, 0, strW, chars, strA, *len, NULL, NULL);
|
|
return strA;
|
|
}
|
|
*len = 0;
|
|
return NULL;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Converts a hex representation of a DWORD into a DWORD.
|
|
*/
|
|
static BOOL convertHexToDWord(WCHAR* str, DWORD *dw)
|
|
{
|
|
char buf[9];
|
|
char dummy;
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, str, -1, buf, 9, NULL, NULL);
|
|
if (lstrlenW(str) > 8 || sscanf(buf, "%x%c", dw, &dummy) != 1) {
|
|
fprintf(stderr,"%s: ERROR, invalid hex value\n", getAppName());
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Converts a hex comma separated values list into a binary string.
|
|
*/
|
|
static BYTE* convertHexCSVToHex(WCHAR *str, DWORD *size)
|
|
{
|
|
WCHAR *s;
|
|
BYTE *d, *data;
|
|
|
|
/* The worst case is 1 digit + 1 comma per byte */
|
|
*size=(lstrlenW(str)+1)/2;
|
|
data=HeapAlloc(GetProcessHeap(), 0, *size);
|
|
CHECK_ENOUGH_MEMORY(data);
|
|
|
|
s = str;
|
|
d = data;
|
|
*size=0;
|
|
while (*s != '\0') {
|
|
UINT wc;
|
|
WCHAR *end;
|
|
|
|
wc = strtoulW(s,&end,16);
|
|
if (end == s || wc > 0xff || (*end && *end != ',')) {
|
|
char* strA = GetMultiByteString(s);
|
|
fprintf(stderr,"%s: ERROR converting CSV hex stream. Invalid value at '%s'\n",
|
|
getAppName(), strA);
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
HeapFree(GetProcessHeap(), 0, strA);
|
|
return NULL;
|
|
}
|
|
*d++ =(BYTE)wc;
|
|
(*size)++;
|
|
if (*end) end++;
|
|
s = end;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* This function returns the HKEY associated with the data type encoded in the
|
|
* value. It modifies the input parameter (key value) in order to skip this
|
|
* "now useless" data type information.
|
|
*
|
|
* Note: Updated based on the algorithm used in 'server/registry.c'
|
|
*/
|
|
static DWORD getDataType(LPWSTR *lpValue, DWORD* parse_type)
|
|
{
|
|
struct data_type { const WCHAR *tag; int len; int type; int parse_type; };
|
|
|
|
static const WCHAR quote[] = {'"'};
|
|
static const WCHAR str[] = {'s','t','r',':','"'};
|
|
static const WCHAR str2[] = {'s','t','r','(','2',')',':','"'};
|
|
static const WCHAR hex[] = {'h','e','x',':'};
|
|
static const WCHAR dword[] = {'d','w','o','r','d',':'};
|
|
static const WCHAR hexp[] = {'h','e','x','('};
|
|
|
|
static const struct data_type data_types[] = { /* actual type */ /* type to assume for parsing */
|
|
{ quote, 1, REG_SZ, REG_SZ },
|
|
{ str, 5, REG_SZ, REG_SZ },
|
|
{ str2, 8, REG_EXPAND_SZ, REG_SZ },
|
|
{ hex, 4, REG_BINARY, REG_BINARY },
|
|
{ dword, 6, REG_DWORD, REG_DWORD },
|
|
{ hexp, 4, -1, REG_BINARY },
|
|
{ NULL, 0, 0, 0 }
|
|
};
|
|
|
|
const struct data_type *ptr;
|
|
int type;
|
|
|
|
for (ptr = data_types; ptr->tag; ptr++) {
|
|
if (strncmpW( ptr->tag, *lpValue, ptr->len ))
|
|
continue;
|
|
|
|
/* Found! */
|
|
*parse_type = ptr->parse_type;
|
|
type=ptr->type;
|
|
*lpValue+=ptr->len;
|
|
if (type == -1) {
|
|
WCHAR* end;
|
|
|
|
/* "hex(xx):" is special */
|
|
type = (int)strtoulW( *lpValue , &end, 16 );
|
|
if (**lpValue=='\0' || *end!=')' || *(end+1)!=':') {
|
|
type=REG_NONE;
|
|
} else {
|
|
*lpValue = end + 2;
|
|
}
|
|
}
|
|
return type;
|
|
}
|
|
*parse_type=REG_NONE;
|
|
return REG_NONE;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Replaces escape sequences with the characters.
|
|
*/
|
|
static void REGPROC_unescape_string(WCHAR* str)
|
|
{
|
|
int str_idx = 0; /* current character under analysis */
|
|
int val_idx = 0; /* the last character of the unescaped string */
|
|
int len = lstrlenW(str);
|
|
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 '\\':
|
|
case '"':
|
|
str[val_idx] = str[str_idx];
|
|
break;
|
|
default:
|
|
fprintf(stderr,"Warning! Unrecognized escape sequence: \\%c'\n",
|
|
str[str_idx]);
|
|
str[val_idx] = str[str_idx];
|
|
break;
|
|
}
|
|
} else {
|
|
str[val_idx] = str[str_idx];
|
|
}
|
|
}
|
|
str[val_idx] = '\0';
|
|
}
|
|
|
|
static BOOL parseKeyName(LPWSTR lpKeyName, HKEY *hKey, LPWSTR *lpKeyPath)
|
|
{
|
|
WCHAR* lpSlash = NULL;
|
|
unsigned int i, len;
|
|
|
|
if (lpKeyName == NULL)
|
|
return FALSE;
|
|
|
|
for(i = 0; *(lpKeyName+i) != 0; i++)
|
|
{
|
|
if(*(lpKeyName+i) == '\\')
|
|
{
|
|
lpSlash = lpKeyName+i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (lpSlash)
|
|
{
|
|
len = lpSlash-lpKeyName;
|
|
}
|
|
else
|
|
{
|
|
len = lstrlenW(lpKeyName);
|
|
lpSlash = lpKeyName+len;
|
|
}
|
|
*hKey = NULL;
|
|
|
|
for (i = 0; i < REG_CLASS_NUMBER; i++) {
|
|
if (CompareStringW(LOCALE_USER_DEFAULT, 0, lpKeyName, len, reg_class_namesW[i], len) == CSTR_EQUAL &&
|
|
len == lstrlenW(reg_class_namesW[i])) {
|
|
*hKey = reg_class_keys[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (*hKey == NULL)
|
|
return FALSE;
|
|
|
|
|
|
if (*lpSlash != '\0')
|
|
lpSlash++;
|
|
*lpKeyPath = lpSlash;
|
|
return TRUE;
|
|
}
|
|
|
|
/* Globals used by the setValue() & co */
|
|
static LPSTR currentKeyName;
|
|
static HKEY currentKeyHandle = NULL;
|
|
|
|
/******************************************************************************
|
|
* Sets the value with name val_name to the data in val_data for the currently
|
|
* opened key.
|
|
*
|
|
* Parameters:
|
|
* val_name - name of the registry value
|
|
* val_data - registry value data
|
|
*/
|
|
static LONG setValue(WCHAR* val_name, WCHAR* val_data, BOOL is_unicode)
|
|
{
|
|
LONG res;
|
|
DWORD dwDataType, dwParseType;
|
|
LPBYTE lpbData;
|
|
DWORD dwData, dwLen;
|
|
WCHAR del[] = {'-',0};
|
|
|
|
if ( (val_name == NULL) || (val_data == NULL) )
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
if (lstrcmpW(val_data, del) == 0)
|
|
{
|
|
res=RegDeleteValueW(currentKeyHandle,val_name);
|
|
return (res == ERROR_FILE_NOT_FOUND ? ERROR_SUCCESS : res);
|
|
}
|
|
|
|
/* Get the data type stored into the value field */
|
|
dwDataType = getDataType(&val_data, &dwParseType);
|
|
|
|
if (dwParseType == REG_SZ) /* no conversion for string */
|
|
{
|
|
REGPROC_unescape_string(val_data);
|
|
/* Compute dwLen after REGPROC_unescape_string because it may
|
|
* have changed the string length and we don't want to store
|
|
* the extra garbage in the registry.
|
|
*/
|
|
dwLen = lstrlenW(val_data);
|
|
if (dwLen>0 && val_data[dwLen-1]=='"')
|
|
{
|
|
dwLen--;
|
|
val_data[dwLen]='\0';
|
|
}
|
|
lpbData = (BYTE*) val_data;
|
|
dwLen++; /* include terminating null */
|
|
dwLen = dwLen * sizeof(WCHAR); /* size is in bytes */
|
|
}
|
|
else if (dwParseType == REG_DWORD) /* Convert the dword types */
|
|
{
|
|
if (!convertHexToDWord(val_data, &dwData))
|
|
return ERROR_INVALID_DATA;
|
|
lpbData = (BYTE*)&dwData;
|
|
dwLen = sizeof(dwData);
|
|
}
|
|
else if (dwParseType == REG_BINARY) /* Convert the binary data */
|
|
{
|
|
lpbData = convertHexCSVToHex(val_data, &dwLen);
|
|
if (!lpbData)
|
|
return ERROR_INVALID_DATA;
|
|
|
|
if(dwDataType == REG_MULTI_SZ && !is_unicode)
|
|
{
|
|
LPBYTE tmp = lpbData;
|
|
lpbData = (LPBYTE)GetWideStringN((char*)lpbData, dwLen, &dwLen);
|
|
dwLen *= sizeof(WCHAR);
|
|
HeapFree(GetProcessHeap(), 0, tmp);
|
|
}
|
|
}
|
|
else /* unknown format */
|
|
{
|
|
fprintf(stderr,"%s: ERROR, unknown data format\n", getAppName());
|
|
return ERROR_INVALID_DATA;
|
|
}
|
|
|
|
res = RegSetValueExW(
|
|
currentKeyHandle,
|
|
val_name,
|
|
0, /* Reserved */
|
|
dwDataType,
|
|
lpbData,
|
|
dwLen);
|
|
if (dwParseType == REG_BINARY)
|
|
HeapFree(GetProcessHeap(), 0, lpbData);
|
|
return res;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* A helper function for processRegEntry() that opens the current key.
|
|
* That key must be closed by calling closeKey().
|
|
*/
|
|
static LONG openKeyW(WCHAR* stdInput)
|
|
{
|
|
HKEY keyClass;
|
|
WCHAR* keyPath;
|
|
DWORD dwDisp;
|
|
LONG res;
|
|
|
|
/* Sanity checks */
|
|
if (stdInput == NULL)
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
/* Get the registry class */
|
|
if (!parseKeyName(stdInput, &keyClass, &keyPath))
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
res = RegCreateKeyExW(
|
|
keyClass, /* Class */
|
|
keyPath, /* Sub Key */
|
|
0, /* MUST BE 0 */
|
|
NULL, /* object type */
|
|
REG_OPTION_NON_VOLATILE, /* option, REG_OPTION_NON_VOLATILE ... */
|
|
KEY_ALL_ACCESS, /* access mask, KEY_ALL_ACCESS */
|
|
NULL, /* security attribute */
|
|
¤tKeyHandle, /* result */
|
|
&dwDisp); /* disposition, REG_CREATED_NEW_KEY or
|
|
REG_OPENED_EXISTING_KEY */
|
|
|
|
if (res == ERROR_SUCCESS)
|
|
currentKeyName = GetMultiByteString(stdInput);
|
|
else
|
|
currentKeyHandle = NULL;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Close the currently opened key.
|
|
*/
|
|
static void closeKey(void)
|
|
{
|
|
if (currentKeyHandle)
|
|
{
|
|
HeapFree(GetProcessHeap(), 0, currentKeyName);
|
|
RegCloseKey(currentKeyHandle);
|
|
currentKeyHandle = NULL;
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* This function is a wrapper for the setValue function. It prepares the
|
|
* land and cleans the area once completed.
|
|
* Note: this function modifies the line parameter.
|
|
*
|
|
* line - registry file unwrapped line. Should have the registry value name and
|
|
* complete registry value data.
|
|
*/
|
|
static void processSetValue(WCHAR* line, BOOL is_unicode)
|
|
{
|
|
WCHAR* val_name; /* registry value name */
|
|
WCHAR* val_data; /* registry value data */
|
|
int line_idx = 0; /* current character under analysis */
|
|
LONG res;
|
|
|
|
/* get value name */
|
|
if (line[line_idx] == '@' && line[line_idx + 1] == '=') {
|
|
line[line_idx] = '\0';
|
|
val_name = line;
|
|
line_idx++;
|
|
} else if (line[line_idx] == '\"') {
|
|
line_idx++;
|
|
val_name = line + line_idx;
|
|
while (TRUE) {
|
|
if (line[line_idx] == '\\') /* skip escaped character */
|
|
{
|
|
line_idx += 2;
|
|
} else {
|
|
if (line[line_idx] == '\"') {
|
|
line[line_idx] = '\0';
|
|
line_idx++;
|
|
break;
|
|
} else {
|
|
line_idx++;
|
|
}
|
|
}
|
|
}
|
|
if (line[line_idx] != '=') {
|
|
char* lineA;
|
|
line[line_idx] = '\"';
|
|
lineA = GetMultiByteString(line);
|
|
fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
|
|
HeapFree(GetProcessHeap(), 0, lineA);
|
|
return;
|
|
}
|
|
|
|
} else {
|
|
char* lineA = GetMultiByteString(line);
|
|
fprintf(stderr,"Warning! unrecognized line:\n%s\n", lineA);
|
|
HeapFree(GetProcessHeap(), 0, lineA);
|
|
return;
|
|
}
|
|
line_idx++; /* skip the '=' character */
|
|
val_data = line + line_idx;
|
|
|
|
REGPROC_unescape_string(val_name);
|
|
res = setValue(val_name, val_data, is_unicode);
|
|
if ( res != ERROR_SUCCESS )
|
|
{
|
|
char* val_nameA = GetMultiByteString(val_name);
|
|
char* val_dataA = GetMultiByteString(val_data);
|
|
fprintf(stderr,"%s: ERROR Key %s not created. Value: %s, Data: %s\n",
|
|
getAppName(),
|
|
currentKeyName,
|
|
val_nameA,
|
|
val_dataA);
|
|
HeapFree(GetProcessHeap(), 0, val_nameA);
|
|
HeapFree(GetProcessHeap(), 0, val_dataA);
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* This function receives the currently read entry and performs the
|
|
* corresponding action.
|
|
* isUnicode affects parsing of REG_MULTI_SZ values
|
|
*/
|
|
static void processRegEntry(WCHAR* stdInput, BOOL isUnicode)
|
|
{
|
|
/*
|
|
* We encountered the end of the file, make sure we
|
|
* close the opened key and exit
|
|
*/
|
|
if (stdInput == NULL) {
|
|
closeKey();
|
|
return;
|
|
}
|
|
|
|
if ( stdInput[0] == '[') /* We are reading a new key */
|
|
{
|
|
WCHAR* keyEnd;
|
|
closeKey(); /* Close the previous key */
|
|
|
|
/* Get rid of the square brackets */
|
|
stdInput++;
|
|
keyEnd = strrchrW(stdInput, ']');
|
|
if (keyEnd)
|
|
*keyEnd='\0';
|
|
|
|
/* delete the key if we encounter '-' at the start of reg key */
|
|
if ( stdInput[0] == '-')
|
|
{
|
|
delete_registry_key(stdInput + 1);
|
|
} else if ( openKeyW(stdInput) != ERROR_SUCCESS )
|
|
{
|
|
char* stdInputA = GetMultiByteString(stdInput);
|
|
fprintf(stderr,"%s: setValue failed to open key %s\n",
|
|
getAppName(), stdInputA);
|
|
HeapFree(GetProcessHeap(), 0, stdInputA);
|
|
}
|
|
} else if( currentKeyHandle &&
|
|
(( stdInput[0] == '@') || /* reading a default @=data pair */
|
|
( stdInput[0] == '\"'))) /* reading a new value=data pair */
|
|
{
|
|
processSetValue(stdInput, isUnicode);
|
|
} else
|
|
{
|
|
/* Since we are assuming that the file format is valid we must be
|
|
* reading a blank line which indicates the end of this key processing
|
|
*/
|
|
closeKey();
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Processes a registry file.
|
|
* Correctly processes comments (in # form), line continuation.
|
|
*
|
|
* Parameters:
|
|
* in - input stream to read from
|
|
*/
|
|
void processRegLinesA(FILE *in)
|
|
{
|
|
LPSTR line = NULL; /* line read from input stream */
|
|
ULONG lineSize = REG_VAL_BUF_SIZE;
|
|
|
|
line = HeapAlloc(GetProcessHeap(), 0, lineSize);
|
|
CHECK_ENOUGH_MEMORY(line);
|
|
|
|
while (!feof(in)) {
|
|
LPSTR s; /* The pointer into line for where the current fgets should read */
|
|
LPSTR check;
|
|
WCHAR* lineW;
|
|
s = line;
|
|
for (;;) {
|
|
size_t size_remaining;
|
|
int size_to_get;
|
|
char *s_eol; /* various local uses */
|
|
|
|
/* Do we need to expand the buffer ? */
|
|
assert (s >= line && s <= line + lineSize);
|
|
size_remaining = lineSize - (s-line);
|
|
if (size_remaining < 2) /* room for 1 character and the \0 */
|
|
{
|
|
char *new_buffer;
|
|
size_t new_size = lineSize + REG_VAL_BUF_SIZE;
|
|
if (new_size > lineSize) /* no arithmetic overflow */
|
|
new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size);
|
|
else
|
|
new_buffer = NULL;
|
|
CHECK_ENOUGH_MEMORY(new_buffer);
|
|
line = new_buffer;
|
|
s = line + lineSize - size_remaining;
|
|
lineSize = new_size;
|
|
size_remaining = lineSize - (s-line);
|
|
}
|
|
|
|
/* Get as much as possible into the buffer, terminated either by
|
|
* eof, error, eol or getting the maximum amount. Abort on error.
|
|
*/
|
|
size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
|
|
|
|
check = fgets (s, size_to_get, in);
|
|
|
|
if (check == NULL) {
|
|
if (ferror(in)) {
|
|
perror ("While reading input");
|
|
exit (IO_ERROR);
|
|
} else {
|
|
assert (feof(in));
|
|
*s = '\0';
|
|
/* It is not clear to me from the definition that the
|
|
* contents of the buffer are well defined on detecting
|
|
* an eof without managing to read anything.
|
|
*/
|
|
}
|
|
}
|
|
|
|
/* If we didn't read the eol nor the eof go around for the rest */
|
|
s_eol = strchr (s, '\n');
|
|
if (!feof (in) && !s_eol) {
|
|
s = strchr (s, '\0');
|
|
/* It should be s + size_to_get - 1 but this is safer */
|
|
continue;
|
|
}
|
|
|
|
/* If it is a comment line then discard it and go around again */
|
|
if (line [0] == '#') {
|
|
s = line;
|
|
continue;
|
|
}
|
|
|
|
/* Remove any line feed. Leave s_eol on the \0 */
|
|
if (s_eol) {
|
|
*s_eol = '\0';
|
|
if (s_eol > line && *(s_eol-1) == '\r')
|
|
*--s_eol = '\0';
|
|
} else
|
|
s_eol = strchr (s, '\0');
|
|
|
|
/* If there is a concatenating \\ then go around again */
|
|
if (s_eol > line && *(s_eol-1) == '\\') {
|
|
int c;
|
|
s = s_eol-1;
|
|
|
|
do
|
|
{
|
|
c = fgetc(in);
|
|
} while(c == ' ' || c == '\t');
|
|
|
|
if(c == EOF)
|
|
{
|
|
fprintf(stderr,"%s: ERROR - invalid continuation.\n",
|
|
getAppName());
|
|
}
|
|
else
|
|
{
|
|
*s = c;
|
|
s++;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
lineW = GetWideString(line);
|
|
|
|
break; /* That is the full virtual line */
|
|
}
|
|
|
|
processRegEntry(lineW, FALSE);
|
|
HeapFree(GetProcessHeap(), 0, lineW);
|
|
}
|
|
processRegEntry(NULL, FALSE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, line);
|
|
}
|
|
|
|
void processRegLinesW(FILE *in)
|
|
{
|
|
WCHAR* buf = NULL; /* line read from input stream */
|
|
ULONG lineSize = REG_VAL_BUF_SIZE;
|
|
size_t CharsInBuf = -1;
|
|
|
|
WCHAR* s; /* The pointer into line for where the current fgets should read */
|
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(WCHAR));
|
|
CHECK_ENOUGH_MEMORY(buf);
|
|
|
|
s = buf;
|
|
|
|
while(!feof(in)) {
|
|
size_t size_remaining;
|
|
int size_to_get;
|
|
WCHAR *s_eol = NULL; /* various local uses */
|
|
|
|
/* Do we need to expand the buffer ? */
|
|
assert (s >= buf && s <= buf + lineSize);
|
|
size_remaining = lineSize - (s-buf);
|
|
if (size_remaining < 2) /* room for 1 character and the \0 */
|
|
{
|
|
WCHAR *new_buffer;
|
|
size_t new_size = lineSize + (REG_VAL_BUF_SIZE / sizeof(WCHAR));
|
|
if (new_size > lineSize) /* no arithmetic overflow */
|
|
new_buffer = HeapReAlloc (GetProcessHeap(), 0, buf, new_size * sizeof(WCHAR));
|
|
else
|
|
new_buffer = NULL;
|
|
CHECK_ENOUGH_MEMORY(new_buffer);
|
|
buf = new_buffer;
|
|
s = buf + lineSize - size_remaining;
|
|
lineSize = new_size;
|
|
size_remaining = lineSize - (s-buf);
|
|
}
|
|
|
|
/* Get as much as possible into the buffer, terminated either by
|
|
* eof, error or getting the maximum amount. Abort on error.
|
|
*/
|
|
size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining);
|
|
|
|
CharsInBuf = fread(s, sizeof(WCHAR), size_to_get - 1, in);
|
|
s[CharsInBuf] = 0;
|
|
|
|
if (CharsInBuf == 0) {
|
|
if (ferror(in)) {
|
|
perror ("While reading input");
|
|
exit (IO_ERROR);
|
|
} else {
|
|
assert (feof(in));
|
|
*s = '\0';
|
|
/* It is not clear to me from the definition that the
|
|
* contents of the buffer are well defined on detecting
|
|
* an eof without managing to read anything.
|
|
*/
|
|
}
|
|
}
|
|
|
|
/* If we didn't read the eol nor the eof go around for the rest */
|
|
while(1)
|
|
{
|
|
s_eol = strchrW(s, '\n');
|
|
|
|
if(!s_eol)
|
|
break;
|
|
|
|
/* If it is a comment line then discard it and go around again */
|
|
if (*s == '#') {
|
|
s = s_eol + 1;
|
|
continue;
|
|
}
|
|
|
|
/* If there is a concatenating \\ then go around again */
|
|
if ((*(s_eol-1) == '\\') ||
|
|
(*(s_eol-1) == '\r' && *(s_eol-2) == '\\')) {
|
|
WCHAR* NextLine = s_eol;
|
|
|
|
while(*(NextLine+1) == ' ' || *(NextLine+1) == '\t')
|
|
NextLine++;
|
|
|
|
NextLine++;
|
|
|
|
if(*(s_eol-1) == '\r')
|
|
s_eol--;
|
|
|
|
MoveMemory(s_eol - 1, NextLine, (CharsInBuf - (NextLine - buf) + 1)*sizeof(WCHAR));
|
|
CharsInBuf -= NextLine - s_eol + 1;
|
|
s_eol = 0;
|
|
continue;
|
|
}
|
|
|
|
/* Remove any line feed. Leave s_eol on the \0 */
|
|
if (s_eol) {
|
|
*s_eol = '\0';
|
|
if (s_eol > buf && *(s_eol-1) == '\r')
|
|
*(s_eol-1) = '\0';
|
|
}
|
|
|
|
if(!s_eol)
|
|
break;
|
|
|
|
processRegEntry(s, TRUE);
|
|
s = s_eol + 1;
|
|
s_eol = 0;
|
|
continue; /* That is the full virtual line */
|
|
}
|
|
}
|
|
|
|
processRegEntry(NULL, TRUE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* REGPROC_print_error
|
|
*
|
|
* Print the message for GetLastError
|
|
*/
|
|
|
|
static void REGPROC_print_error(void)
|
|
{
|
|
LPVOID lpMsgBuf;
|
|
DWORD error_code;
|
|
int status;
|
|
|
|
error_code = GetLastError ();
|
|
status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
|
|
if (!status) {
|
|
fprintf(stderr,"%s: Cannot display message for error %d, status %d\n",
|
|
getAppName(), error_code, GetLastError());
|
|
exit(1);
|
|
}
|
|
puts(lpMsgBuf);
|
|
LocalFree((HLOCAL)lpMsgBuf);
|
|
exit(1);
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Checks whether the buffer has enough room for the string or required size.
|
|
* Resizes the buffer if necessary.
|
|
*
|
|
* Parameters:
|
|
* buffer - pointer to a buffer for string
|
|
* len - current length of the buffer in characters.
|
|
* required_len - length of the string to place to the buffer in characters.
|
|
* The length does not include the terminating null character.
|
|
*/
|
|
static void REGPROC_resize_char_buffer(WCHAR **buffer, DWORD *len, DWORD required_len)
|
|
{
|
|
required_len++;
|
|
if (required_len > *len) {
|
|
*len = required_len;
|
|
if (!*buffer)
|
|
*buffer = HeapAlloc(GetProcessHeap(), 0, *len * sizeof(**buffer));
|
|
else
|
|
*buffer = HeapReAlloc(GetProcessHeap(), 0, *buffer, *len * sizeof(**buffer));
|
|
CHECK_ENOUGH_MEMORY(*buffer);
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Prints string str to file
|
|
*/
|
|
static void REGPROC_export_string(WCHAR **line_buf, DWORD *line_buf_size, DWORD *line_size, WCHAR *str)
|
|
{
|
|
DWORD len = lstrlenW(str);
|
|
DWORD i;
|
|
DWORD extra = 0;
|
|
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, len + *line_size + 10);
|
|
|
|
/* escaping characters */
|
|
for (i = 0; i < len; i++) {
|
|
WCHAR c = str[i];
|
|
switch (c) {
|
|
case '\\':
|
|
{
|
|
const WCHAR escape[] = {'\\','\\'};
|
|
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, len + *line_size + extra + 1);
|
|
memcpy(*line_buf + *line_size + i + extra - 1, escape, 2 * sizeof(WCHAR));
|
|
extra++;
|
|
break;
|
|
}
|
|
case '"':
|
|
{
|
|
const WCHAR escape[] = {'\\','"'};
|
|
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, len + *line_size + extra + 1);
|
|
memcpy(*line_buf + *line_size + i + extra - 1, escape, 2 * sizeof(WCHAR));
|
|
extra++;
|
|
break;
|
|
}
|
|
case '\n':
|
|
{
|
|
const WCHAR escape[] = {'\\','n'};
|
|
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, len + *line_size + extra + 1);
|
|
memcpy(*line_buf + *line_size + i + extra - 1, escape, 2 * sizeof(WCHAR));
|
|
extra++;
|
|
break;
|
|
}
|
|
default:
|
|
memcpy(*line_buf + *line_size + i + extra - 1, &c, sizeof(WCHAR));
|
|
break;
|
|
}
|
|
}
|
|
*line_size += len + extra;
|
|
*(*line_buf + *line_size - 1) = 0;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Writes the given line to a file, in multi-byte or wide characters
|
|
*/
|
|
static void REGPROC_write_line(FILE *file, const WCHAR* str, BOOL unicode)
|
|
{
|
|
if(unicode)
|
|
{
|
|
fwrite(str, sizeof(WCHAR), lstrlenW(str), file);
|
|
} else
|
|
{
|
|
char* strA = GetMultiByteString(str);
|
|
fputs(strA, file);
|
|
HeapFree(GetProcessHeap(), 0, strA);
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Writes contents of the registry key to the specified file stream.
|
|
*
|
|
* Parameters:
|
|
* file - writable file stream to export registry branch to.
|
|
* key - registry branch to export.
|
|
* reg_key_name_buf - name of the key with registry class.
|
|
* Is resized if necessary.
|
|
* reg_key_name_len - length of the buffer for the registry class in characters.
|
|
* val_name_buf - buffer for storing value name.
|
|
* Is resized if necessary.
|
|
* val_name_len - length of the buffer for storing value names in characters.
|
|
* val_buf - buffer for storing values while extracting.
|
|
* Is resized if necessary.
|
|
* val_size - size of the buffer for storing values in bytes.
|
|
*/
|
|
static void export_hkey(FILE *file, HKEY key,
|
|
WCHAR **reg_key_name_buf, DWORD *reg_key_name_len,
|
|
WCHAR **val_name_buf, DWORD *val_name_len,
|
|
BYTE **val_buf, DWORD *val_size,
|
|
WCHAR **line_buf, DWORD *line_buf_size,
|
|
BOOL unicode)
|
|
{
|
|
DWORD max_sub_key_len;
|
|
DWORD max_val_name_len;
|
|
DWORD max_val_size;
|
|
DWORD curr_len;
|
|
DWORD i;
|
|
BOOL more_data;
|
|
LONG ret;
|
|
WCHAR key_format[] = {'\n','[','%','s',']','\n',0};
|
|
DWORD line_pos;
|
|
|
|
/* get size information and resize the buffers if necessary */
|
|
if (RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL,
|
|
&max_sub_key_len, NULL,
|
|
NULL, &max_val_name_len, &max_val_size, NULL, NULL
|
|
) != ERROR_SUCCESS) {
|
|
REGPROC_print_error();
|
|
}
|
|
curr_len = strlenW(*reg_key_name_buf);
|
|
REGPROC_resize_char_buffer(reg_key_name_buf, reg_key_name_len,
|
|
max_sub_key_len + curr_len + 1);
|
|
REGPROC_resize_char_buffer(val_name_buf, val_name_len,
|
|
max_val_name_len);
|
|
if (max_val_size > *val_size) {
|
|
*val_size = max_val_size;
|
|
if (!*val_buf) *val_buf = HeapAlloc(GetProcessHeap(), 0, *val_size);
|
|
else *val_buf = HeapReAlloc(GetProcessHeap(), 0, *val_buf, *val_size);
|
|
CHECK_ENOUGH_MEMORY(val_buf);
|
|
}
|
|
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, lstrlenW(*reg_key_name_buf) + 4);
|
|
/* output data for the current key */
|
|
wsprintfW(*line_buf, key_format, *reg_key_name_buf);
|
|
REGPROC_write_line(file, *line_buf, unicode);
|
|
|
|
/* print all the values */
|
|
i = 0;
|
|
more_data = TRUE;
|
|
while(more_data) {
|
|
DWORD value_type;
|
|
DWORD val_name_len1 = *val_name_len;
|
|
DWORD val_size1 = *val_size;
|
|
DWORD line_size = 0;
|
|
ret = RegEnumValueW(key, i, *val_name_buf, &val_name_len1, NULL,
|
|
&value_type, *val_buf, &val_size1);
|
|
if (ret != ERROR_SUCCESS) {
|
|
more_data = FALSE;
|
|
if (ret != ERROR_NO_MORE_ITEMS) {
|
|
REGPROC_print_error();
|
|
}
|
|
} else {
|
|
i++;
|
|
|
|
if ((*val_name_buf)[0]) {
|
|
const WCHAR val_start[] = {'"','%','s','"','=',0};
|
|
|
|
line_size = 4 + lstrlenW(*val_name_buf);
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, line_size);
|
|
wsprintfW(*line_buf, val_start, *val_name_buf);
|
|
line_pos = lstrlenW(*line_buf);
|
|
} else {
|
|
const WCHAR std_val[] = {'@','=',0};
|
|
line_size = 3;
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, line_size);
|
|
lstrcpyW(*line_buf, std_val);
|
|
line_pos = lstrlenW(*line_buf);
|
|
}
|
|
|
|
switch (value_type) {
|
|
case REG_SZ:
|
|
case REG_EXPAND_SZ:
|
|
{
|
|
const WCHAR start[] = {'"',0};
|
|
const WCHAR end[] = {'"','\n',0};
|
|
|
|
line_size += lstrlenW(start);
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, line_size);
|
|
lstrcatW(*line_buf, start);
|
|
|
|
if (val_size1) REGPROC_export_string(line_buf, line_buf_size, &line_size, (WCHAR*) *val_buf);
|
|
|
|
line_size += lstrlenW(end);
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, line_size);
|
|
lstrcatW(*line_buf, end);
|
|
break;
|
|
}
|
|
|
|
case REG_DWORD:
|
|
{
|
|
WCHAR format[] = {'d','w','o','r','d',':','%','0','8','x','\n',0};
|
|
|
|
line_size += 20;
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, line_size);
|
|
wsprintfW(*line_buf + line_pos, format, *((DWORD *)*val_buf));
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
char* key_nameA = GetMultiByteString(*reg_key_name_buf);
|
|
char* value_nameA = GetMultiByteString(*val_name_buf);
|
|
fprintf(stderr,"%s: warning - unsupported registry format '%d', "
|
|
"treat as binary\n",
|
|
getAppName(), value_type);
|
|
fprintf(stderr,"key name: \"%s\"\n", key_nameA);
|
|
fprintf(stderr,"value name:\"%s\"\n\n", value_nameA);
|
|
HeapFree(GetProcessHeap(), 0, key_nameA);
|
|
HeapFree(GetProcessHeap(), 0, value_nameA);
|
|
}
|
|
/* falls through */
|
|
case REG_MULTI_SZ:
|
|
/* falls through */
|
|
case REG_BINARY: {
|
|
DWORD i1;
|
|
const WCHAR *hex_prefix;
|
|
WCHAR buf[20];
|
|
int cur_pos;
|
|
const WCHAR hex[] = {'h','e','x',':',0};
|
|
const WCHAR delim[] = {'"','"','=',0};
|
|
const WCHAR format[] = {'%','0','2','x',0};
|
|
const WCHAR comma[] = {',',0};
|
|
const WCHAR concat[] = {'\\','\n',' ',' ',0};
|
|
const WCHAR newline[] = {'\n',0};
|
|
BYTE* val_buf1 = *val_buf;
|
|
DWORD val_buf1_size = val_size1;
|
|
|
|
if (value_type == REG_BINARY) {
|
|
hex_prefix = hex;
|
|
} else {
|
|
const WCHAR hex_format[] = {'h','e','x','(','%','d',')',':',0};
|
|
hex_prefix = buf;
|
|
wsprintfW(buf, hex_format, value_type);
|
|
if(value_type == REG_MULTI_SZ && !unicode)
|
|
val_buf1 = (BYTE*)GetMultiByteStringN((WCHAR*)*val_buf, val_size1 / sizeof(WCHAR), &val_buf1_size);
|
|
}
|
|
|
|
/* position of where the next character will be printed */
|
|
/* NOTE: yes, strlen("hex:") is used even for hex(x): */
|
|
cur_pos = lstrlenW(delim) + lstrlenW(hex) +
|
|
lstrlenW(*val_name_buf);
|
|
|
|
line_size += lstrlenW(hex_prefix);
|
|
line_size += val_buf1_size * 3 + lstrlenW(concat) * ((int)((float)val_buf1_size * 3.0 / (float)REG_FILE_HEX_LINE_LEN) + 1 ) + 1;
|
|
REGPROC_resize_char_buffer(line_buf, line_buf_size, line_size);
|
|
lstrcatW(*line_buf, hex_prefix);
|
|
line_pos += lstrlenW(hex_prefix);
|
|
for (i1 = 0; i1 < val_buf1_size; i1++) {
|
|
wsprintfW(*line_buf + line_pos, format, (unsigned int)(val_buf1)[i1]);
|
|
line_pos += 2;
|
|
if (i1 + 1 < val_buf1_size) {
|
|
lstrcpyW(*line_buf + line_pos, comma);
|
|
line_pos++;
|
|
}
|
|
cur_pos += 3;
|
|
|
|
/* wrap the line */
|
|
if (cur_pos > REG_FILE_HEX_LINE_LEN) {
|
|
lstrcpyW(*line_buf + line_pos, concat);
|
|
line_pos += lstrlenW(concat);
|
|
cur_pos = 2;
|
|
}
|
|
}
|
|
if(value_type == REG_MULTI_SZ && !unicode)
|
|
HeapFree(GetProcessHeap(), 0, val_buf1);
|
|
lstrcpyW(*line_buf + line_pos, newline);
|
|
break;
|
|
}
|
|
}
|
|
REGPROC_write_line(file, *line_buf, unicode);
|
|
}
|
|
}
|
|
|
|
i = 0;
|
|
more_data = TRUE;
|
|
(*reg_key_name_buf)[curr_len] = '\\';
|
|
while(more_data) {
|
|
DWORD buf_len = *reg_key_name_len - curr_len;
|
|
|
|
ret = RegEnumKeyExW(key, i, *reg_key_name_buf + curr_len + 1, &buf_len,
|
|
NULL, NULL, NULL, NULL);
|
|
if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA) {
|
|
more_data = FALSE;
|
|
if (ret != ERROR_NO_MORE_ITEMS) {
|
|
REGPROC_print_error();
|
|
}
|
|
} else {
|
|
HKEY subkey;
|
|
|
|
i++;
|
|
if (RegOpenKeyW(key, *reg_key_name_buf + curr_len + 1,
|
|
&subkey) == ERROR_SUCCESS) {
|
|
export_hkey(file, subkey, reg_key_name_buf, reg_key_name_len,
|
|
val_name_buf, val_name_len, val_buf, val_size,
|
|
line_buf, line_buf_size, unicode);
|
|
RegCloseKey(subkey);
|
|
} else {
|
|
REGPROC_print_error();
|
|
}
|
|
}
|
|
}
|
|
(*reg_key_name_buf)[curr_len] = '\0';
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Open file for export.
|
|
*/
|
|
static FILE *REGPROC_open_export_file(WCHAR *file_name, BOOL unicode)
|
|
{
|
|
FILE *file;
|
|
WCHAR dash = '-';
|
|
|
|
if (strncmpW(file_name,&dash,1)==0)
|
|
file=stdout;
|
|
else
|
|
{
|
|
CHAR* file_nameA = GetMultiByteString(file_name);
|
|
file = fopen(file_nameA, "w");
|
|
if (!file) {
|
|
perror("");
|
|
fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), file_nameA);
|
|
HeapFree(GetProcessHeap(), 0, file_nameA);
|
|
exit(1);
|
|
}
|
|
HeapFree(GetProcessHeap(), 0, file_nameA);
|
|
}
|
|
if(unicode)
|
|
{
|
|
const BYTE unicode_seq[] = {0xff,0xfe};
|
|
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','\n'};
|
|
fwrite(unicode_seq, sizeof(BYTE), sizeof(unicode_seq)/sizeof(unicode_seq[0]), file);
|
|
fwrite(header, sizeof(WCHAR), sizeof(header)/sizeof(header[0]), file);
|
|
} else
|
|
{
|
|
fputs("REGEDIT4\n", file);
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Writes contents of the registry key to the specified file stream.
|
|
*
|
|
* Parameters:
|
|
* file_name - name of a file to export registry branch to.
|
|
* reg_key_name - registry branch to export. The whole registry is exported if
|
|
* reg_key_name is NULL or contains an empty string.
|
|
*/
|
|
BOOL export_registry_key(WCHAR *file_name, WCHAR *reg_key_name, DWORD format)
|
|
{
|
|
WCHAR *reg_key_name_buf;
|
|
WCHAR *val_name_buf;
|
|
BYTE *val_buf;
|
|
WCHAR *line_buf;
|
|
DWORD reg_key_name_len = KEY_MAX_LEN;
|
|
DWORD val_name_len = KEY_MAX_LEN;
|
|
DWORD val_size = REG_VAL_BUF_SIZE;
|
|
DWORD line_buf_size = KEY_MAX_LEN + REG_VAL_BUF_SIZE;
|
|
FILE *file = NULL;
|
|
BOOL unicode = (format == REG_FORMAT_5);
|
|
|
|
reg_key_name_buf = HeapAlloc(GetProcessHeap(), 0,
|
|
reg_key_name_len * sizeof(*reg_key_name_buf));
|
|
val_name_buf = HeapAlloc(GetProcessHeap(), 0,
|
|
val_name_len * sizeof(*val_name_buf));
|
|
val_buf = HeapAlloc(GetProcessHeap(), 0, val_size);
|
|
line_buf = HeapAlloc(GetProcessHeap(), 0, line_buf_size);
|
|
CHECK_ENOUGH_MEMORY(reg_key_name_buf && val_name_buf && val_buf);
|
|
|
|
if (reg_key_name && reg_key_name[0]) {
|
|
HKEY reg_key_class;
|
|
WCHAR *branch_name = NULL;
|
|
HKEY key;
|
|
|
|
REGPROC_resize_char_buffer(®_key_name_buf, ®_key_name_len,
|
|
lstrlenW(reg_key_name));
|
|
lstrcpyW(reg_key_name_buf, reg_key_name);
|
|
|
|
/* open the specified key */
|
|
if (!parseKeyName(reg_key_name, ®_key_class, &branch_name)) {
|
|
CHAR* key_nameA = GetMultiByteString(reg_key_name);
|
|
fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
|
|
getAppName(), key_nameA);
|
|
HeapFree(GetProcessHeap(), 0, key_nameA);
|
|
exit(1);
|
|
}
|
|
if (!branch_name[0]) {
|
|
/* no branch - registry class is specified */
|
|
file = REGPROC_open_export_file(file_name, unicode);
|
|
export_hkey(file, reg_key_class,
|
|
®_key_name_buf, ®_key_name_len,
|
|
&val_name_buf, &val_name_len,
|
|
&val_buf, &val_size, &line_buf,
|
|
&line_buf_size, unicode);
|
|
} else if (RegOpenKeyW(reg_key_class, branch_name, &key) == ERROR_SUCCESS) {
|
|
file = REGPROC_open_export_file(file_name, unicode);
|
|
export_hkey(file, key,
|
|
®_key_name_buf, ®_key_name_len,
|
|
&val_name_buf, &val_name_len,
|
|
&val_buf, &val_size, &line_buf,
|
|
&line_buf_size, unicode);
|
|
RegCloseKey(key);
|
|
} else {
|
|
CHAR* key_nameA = GetMultiByteString(reg_key_name);
|
|
fprintf(stderr,"%s: Can't export. Registry key '%s' does not exist!\n",
|
|
getAppName(), key_nameA);
|
|
HeapFree(GetProcessHeap(), 0, key_nameA);
|
|
REGPROC_print_error();
|
|
}
|
|
} else {
|
|
unsigned int i;
|
|
|
|
/* export all registry classes */
|
|
file = REGPROC_open_export_file(file_name, unicode);
|
|
for (i = 0; i < REG_CLASS_NUMBER; i++) {
|
|
/* do not export HKEY_CLASSES_ROOT */
|
|
if (reg_class_keys[i] != HKEY_CLASSES_ROOT &&
|
|
reg_class_keys[i] != HKEY_CURRENT_USER &&
|
|
reg_class_keys[i] != HKEY_CURRENT_CONFIG &&
|
|
reg_class_keys[i] != HKEY_DYN_DATA) {
|
|
lstrcpyW(reg_key_name_buf, reg_class_namesW[i]);
|
|
export_hkey(file, reg_class_keys[i],
|
|
®_key_name_buf, ®_key_name_len,
|
|
&val_name_buf, &val_name_len,
|
|
&val_buf, &val_size, &line_buf,
|
|
&line_buf_size, unicode);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (file) {
|
|
fclose(file);
|
|
}
|
|
HeapFree(GetProcessHeap(), 0, reg_key_name);
|
|
HeapFree(GetProcessHeap(), 0, val_name_buf);
|
|
HeapFree(GetProcessHeap(), 0, val_buf);
|
|
HeapFree(GetProcessHeap(), 0, line_buf);
|
|
return TRUE;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* Reads contents of the specified file into the registry.
|
|
*/
|
|
BOOL import_registry_file(FILE* reg_file)
|
|
{
|
|
if (reg_file)
|
|
{
|
|
BYTE s[2];
|
|
if (fread( s, 2, 1, reg_file) == 1)
|
|
{
|
|
if (s[0] == 0xff && s[1] == 0xfe)
|
|
{
|
|
processRegLinesW(reg_file);
|
|
} else
|
|
{
|
|
rewind(reg_file);
|
|
processRegLinesA(reg_file);
|
|
}
|
|
}
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/******************************************************************************
|
|
* 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.
|
|
*/
|
|
void delete_registry_key(WCHAR *reg_key_name)
|
|
{
|
|
WCHAR *key_name = NULL;
|
|
HKEY key_class;
|
|
|
|
if (!reg_key_name || !reg_key_name[0])
|
|
return;
|
|
|
|
if (!parseKeyName(reg_key_name, &key_class, &key_name)) {
|
|
char* reg_key_nameA = GetMultiByteString(reg_key_name);
|
|
fprintf(stderr,"%s: Incorrect registry class specification in '%s'\n",
|
|
getAppName(), reg_key_nameA);
|
|
HeapFree(GetProcessHeap(), 0, reg_key_nameA);
|
|
exit(1);
|
|
}
|
|
if (!*key_name) {
|
|
char* reg_key_nameA = GetMultiByteString(reg_key_name);
|
|
fprintf(stderr,"%s: Can't delete registry class '%s'\n",
|
|
getAppName(), reg_key_nameA);
|
|
HeapFree(GetProcessHeap(), 0, reg_key_nameA);
|
|
exit(1);
|
|
}
|
|
|
|
RegDeleteTreeW(key_class, key_name);
|
|
}
|