📄 freeotfe4pdaregistry.c
字号:
// Description:
// By Sarah Dean
// Email: sdean12@sdean12.org
// WWW: http://www.FreeOTFE.org/
//
// -----------------------------------------------------------------------------
//
#include "FreeOTFElib.h"
#include "FreeOTFEDebug.h"
#include "FreeOTFE4PDARegistry.h"
#include <stdlib.h>
#include <Winreg.h>
#define REGHIVE_FILEASSOC HKEY_CLASSES_ROOT
#define REGKEY_FILEASSOC_EXTN TEXT("\\.%s")
#define REGKEY_FILEASSOC_FILETYPE TEXT("\\%s")
#define REGKEY_FILEASSOC_COMMAND TEXT("\\%s\\Shell\\Open\\Command")
#define REGKEY_FILEASSOC_DEFAULTICON TEXT("\\%s\\DefaultIcon")
#define REGVALUE_FILEASSOC_DEFAULTICON TEXT("%s,-%d")
// =========================================================================
BOOL RegistrySetString(HKEY hKey, LPCWSTR name, LPCWSTR value)
{
BOOL retval = FALSE;
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Set registry [s]: NAME: %s\n"), name));
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Set registry [s]: VALUE: %s\n"), value));
retval = (RegSetValueEx(
hKey,
name,
0,
REG_SZ,
(BYTE*)value,
// Note: Include the terminating NULL
((wcslen(value) + 1) * sizeof(WCHAR))
) == ERROR_SUCCESS);
return retval;
}
// =========================================================================
// Note: Storage for "value" is automatically allocated; it is up to the
// *caller* to free this off after use
BOOL RegistryGetString(HKEY hKey, LPCWSTR name, LPCWSTR* value)
{
BOOL retval = FALSE;
DWORD type;
DWORD size;
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Get registry [s]: NAME: %s\n"), name));
retval = (RegQueryValueEx(
hKey,
name,
NULL,
&type,
NULL,
&size
) == ERROR_SUCCESS);
if (retval)
{
*value = malloc(size);
retval = (RegQueryValueEx(
hKey,
name,
NULL,
&type,
(LPBYTE)(*value),
&size
) == ERROR_SUCCESS);
if (!(retval))
{
free((void*)(*value));
}
}
if (!(retval))
{
// Fallback to NULL
*value = NULL;
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Get registry [s]: <Unable to get value>\n")));
}
else
{
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Get registry [s]: VALUE: %ls\n"), *value));
}
return retval;
}
// =========================================================================
BOOL RegistrySetDWORD(HKEY hKey, LPCWSTR name, DWORD value)
{
BOOL retval = FALSE;
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Set registry [dw]: NAME: %s\n"), name));
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Set registry [dw]: VALUE: %d\n"), value));
retval = (RegSetValueEx(
hKey,
name,
0,
REG_DWORD,
(BYTE*)&value,
sizeof(value)
) == ERROR_SUCCESS);
return retval;
}
// =========================================================================
BOOL RegistryGetDWORD(HKEY hKey, LPCWSTR name, DWORD* value)
{
BOOL retval = FALSE;
DWORD type;
DWORD size;
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Get registry [dw]: NAME: %s\n"), name));
retval = (RegQueryValueEx(
hKey,
name,
NULL,
&type,
NULL,
&size
) == ERROR_SUCCESS);
if (
(retval) &&
(size == sizeof(*value))
)
{
retval = (RegQueryValueEx(
hKey,
name,
NULL,
&type,
(LPBYTE)(value),
&size
) == ERROR_SUCCESS);
}
if (!(retval))
{
// Fallback to 0
*value = 0;
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Get registry [dw]: <Unable to get value>\n")));
}
else
{
DEBUGOUTLIB(DEBUGLEV_VERBOSE_INFO, (TEXT("Get registry [dw]: VALUE: %d\n"), *value));
}
return retval;
}
// =========================================================================
// Returns an identifying number that the next FreeOTFE device mounted can
// use as part of it's unique registry key
// Returns: An ID number
int RegGetNextRegNumber()
{
int retval;
REGDETAILS_BUILTIN detailsBuiltin;
REGDETAILS_PROFILE detailsProfile;
BOOL exists;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegGetNextRegNumber\n")));
retval = 1;
exists = TRUE;
while (exists)
{
memset(&detailsBuiltin, 0, sizeof(detailsBuiltin));
memset(&detailsProfile, 0, sizeof(detailsProfile));
exists = RegDetailsGetBuiltinByID(retval, &detailsBuiltin) ||
RegDetailsGetProfileByID(retval, &detailsProfile);
// Ditch the details; we don't care about them, only that we
// can/can't get them
RegDetailsFree(&detailsBuiltin, &detailsProfile, NULL);
if (!(exists))
{
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("Next registry ID: %d\n"), retval));
break;
}
retval++;
}
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegGetNextRegNumber\n")));
return retval;
}
// =========================================================================
// Note: detailsBuiltin.Profile is *ignored* - the profile is automatically
// generated based on the ID passed in
BOOL RegDetailsSetAll(
int id,
REGDETAILS_BUILTIN detailsBuiltin,
REGDETAILS_PROFILE detailsProfile
)
{
BOOL retval = TRUE;
HKEY hKey;
WCHAR* keyBuiltin;
WCHAR* keyProfile;
WCHAR* profile;
DWORD lpdwDisposition;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsSetAll\n")));
profile = ProfileNameGenerate(id);
RegKeyGenerate(id, &keyBuiltin, &keyProfile);
// Write builtin registry data...
if (RegCreateKeyEx(
REGHIVE,
keyBuiltin,
0,
TEXT(""), // Class
0,
0,
NULL,
&hKey,
&lpdwDisposition
) == ERROR_SUCCESS)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Writing values for builtin registry key...\n")));
retval = retval && RegistrySetString(
hKey,
REGNAME_BUILTIN_PREFIX,
detailsBuiltin.Prefix
);
retval = retval && RegistrySetString(
hKey,
REGNAME_BUILTIN_DLL,
detailsBuiltin.Dll
);
retval = retval && RegistrySetString(
hKey,
REGNAME_BUILTIN_FRIENDLYNAME,
detailsBuiltin.FriendlyName
);
retval = retval && RegistrySetDWORD(
hKey,
REGNAME_BUILTIN_ORDER,
detailsBuiltin.Order
);
retval = retval && RegistrySetDWORD(
hKey,
REGNAME_BUILTIN_IOCTL,
detailsBuiltin.Ioctl
);
retval = retval && RegistrySetString(
hKey,
REGNAME_BUILTIN_ICLASS,
detailsBuiltin.IClass
);
retval = retval && RegistrySetString(
hKey,
REGNAME_BUILTIN_PROFILE,
profile
);
RegCloseKey(hKey);
}
// Write profile registry data...
if (RegCreateKeyEx(
REGHIVE,
keyProfile,
0,
TEXT(""), // Class
0,
0,
NULL,
&hKey,
&lpdwDisposition
) == ERROR_SUCCESS)
{
DEBUGOUTLIB(DEBUGLEV_INFO, (TEXT("Writing values for profile registry key...\n")));
retval = retval && RegistrySetString(
hKey,
REGNAME_PROFILE_NAME,
detailsProfile.Name
);
retval = retval && RegistrySetString(
hKey,
REGNAME_PROFILE_FOLDER,
detailsProfile.Folder
);
retval = retval && RegistrySetDWORD(
hKey,
REGNAME_PROFILE_AUTOMOUNT,
detailsProfile.AutoMount
);
retval = retval && RegistrySetDWORD(
hKey,
REGNAME_PROFILE_AUTOPART,
detailsProfile.AutoPart
);
retval = retval && RegistrySetDWORD(
hKey,
REGNAME_PROFILE_AUTOFORMAT,
detailsProfile.AutoFormat
);
retval = retval && RegistrySetDWORD(
hKey,
REGNAME_PROFILE_MOUNTFLAGS,
detailsProfile.MountFlags
);
RegCloseKey(hKey);
}
// Cleanup...
SecZeroAndFreeWCHARMemory(keyProfile);
SecZeroAndFreeWCHARMemory(keyBuiltin);
SecZeroAndFreeWCHARMemory(profile);
if (!(retval))
{
// Failed; clear down any registry entries which may have been
// created
// Ignore return value, as there's really nothing we can do if it
// fails; we're already returning a failure
RegDetailsDeleteAllByID(id, TRUE);
}
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsSetAll\n")));
return retval;
}
// =========================================================================
BOOL RegDetailsGetBuiltinByID(int id, REGDETAILS_BUILTIN *detailsBuiltin)
{
BOOL retval = FALSE;
WCHAR* key;
DEBUGOUTLIB(DEBUGLEV_ENTER, (TEXT("RegDetailsGetBuiltinByID\n")));
RegKeyGenerate(id, &key, NULL);
retval = RegDetailsGetBuiltinByKey(key, detailsBuiltin);
// Cleanup...
SecZeroAndFreeWCHARMemory(key);
DEBUGOUTLIB(DEBUGLEV_EXIT, (TEXT("RegDetailsGetBuiltinByID\n")));
return retval;
}
// =========================================================================
BOOL RegDetailsGetBuiltinByKey(WCHAR* key, REGDETAILS_BUILTIN *detailsBuiltin)
{
BOOL retval = FALSE;
HKEY hKey;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -