forked from daynix/UsbDk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UsbDkHelper: Introduce class for hide rules management
Signed-off-by: Kirill Moizik <[email protected]> Signed-off-by: Dmitry Fleytman <[email protected]>
- Loading branch information
Dmitry Fleytman
committed
Mar 23, 2015
1 parent
73ba48b
commit 408a787
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include "stdafx.h" | ||
#include "UsbDkDataHider.h" | ||
#include "UsbDkNames.h" | ||
#include "HideRulesRegPublic.h" | ||
#include "RegAccess.h" | ||
#include "RuleManager.h" | ||
#include "GuidGen.h" | ||
|
||
CRulesManager::CRulesManager() | ||
: m_RegAccess(HKEY_LOCAL_MACHINE, USBDK_HIDE_RULES_PATH) | ||
{} | ||
|
||
static bool operator == (const USB_DK_HIDE_RULE& r1, const USB_DK_HIDE_RULE& r2) | ||
{ | ||
return (r1.VID == r2.VID) && | ||
(r1.PID == r2.PID) && | ||
(r1.BCD == r2.BCD) && | ||
(r1.Class == r2.Class) && | ||
(r1.Hide == r2.Hide); | ||
} | ||
|
||
DWORD CRulesManager::ReadDword(LPCTSTR RuleName, LPCTSTR ValueName) const | ||
{ | ||
DWORD RawValue; | ||
|
||
if (!m_RegAccess.ReadDWord(ValueName, &RawValue, RuleName)) | ||
{ | ||
tstring ErrorText = tstring(TEXT("Failed to read rule ")) + ValueName; | ||
throw UsbDkRuleManagerException(ErrorText, ERROR_FUNCTION_FAILED); | ||
} | ||
|
||
return RawValue; | ||
} | ||
|
||
void CRulesManager::WriteDword(const tstring &RuleName, LPCTSTR ValueName, ULONG Value) | ||
{ | ||
if (!m_RegAccess.WriteValue(ValueName, Value, RuleName.c_str())) | ||
{ | ||
tstring ErrorText = tstring(TEXT("Failed to write rule ")) + ValueName; | ||
throw UsbDkRuleManagerException(ErrorText, ERROR_FUNCTION_FAILED); | ||
} | ||
} | ||
|
||
ULONG64 CRulesManager::ReadDwordMask(LPCTSTR RuleName, LPCTSTR ValueName) const | ||
{ | ||
return HideRuleUlongMaskFromRegistry(ReadDword(RuleName, ValueName)); | ||
} | ||
|
||
ULONG64 CRulesManager::ReadBool(LPCTSTR RuleName, LPCTSTR ValueName) const | ||
{ | ||
return HideRuleBoolFromRegistry(ReadDword(RuleName, ValueName)); | ||
} | ||
|
||
void CRulesManager::ReadRule(LPCTSTR RuleName, USB_DK_HIDE_RULE &Rule) const | ||
{ | ||
Rule.Hide = ReadBool(RuleName, USBDK_HIDE_RULE_SHOULD_HIDE); | ||
Rule.VID = ReadDwordMask(RuleName, USBDK_HIDE_RULE_VID); | ||
Rule.PID = ReadDwordMask(RuleName, USBDK_HIDE_RULE_PID); | ||
Rule.BCD = ReadDwordMask(RuleName, USBDK_HIDE_RULE_BCD); | ||
Rule.Class = ReadDwordMask(RuleName, USBDK_HIDE_RULE_CLASS); | ||
} | ||
|
||
template <typename TFunctor> | ||
bool CRulesManager::FindRule(const USB_DK_HIDE_RULE &Rule, TFunctor Functor) | ||
{ | ||
for (const auto &SubKey : m_RegAccess) | ||
{ | ||
try | ||
{ | ||
USB_DK_HIDE_RULE ExistingRule; | ||
ReadRule(SubKey, ExistingRule); | ||
|
||
if (Rule == ExistingRule) | ||
{ | ||
Functor(SubKey); | ||
return true; | ||
} | ||
} | ||
catch (const UsbDkRuleManagerException &e) | ||
{ | ||
auto ErrorText = tstring(TEXT("Error while processing rule ")) + | ||
SubKey + TEXT(": ") + string2tstring(e.what()); | ||
OutputDebugString(ErrorText.c_str()); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool CRulesManager::RuleExists(const USB_DK_HIDE_RULE &Rule) | ||
{ | ||
return FindRule(Rule, [](LPCTSTR){}); | ||
} | ||
|
||
void CRulesManager::AddRule(const USB_DK_HIDE_RULE &Rule) | ||
{ | ||
if (RuleExists(Rule)) | ||
{ | ||
throw UsbDkRuleManagerException(TEXT("Rule already exists"), ERROR_FILE_EXISTS); | ||
} | ||
|
||
CGuid RuleName; | ||
|
||
if (!m_RegAccess.AddKey(RuleName)) | ||
{ | ||
throw UsbDkRuleManagerException(TEXT("Failed to create rule key"), ERROR_FUNCTION_FAILED); | ||
} | ||
|
||
WriteDword(RuleName, USBDK_HIDE_RULE_SHOULD_HIDE, static_cast<ULONG>(Rule.Hide)); | ||
WriteDword(RuleName, USBDK_HIDE_RULE_VID, static_cast<ULONG>(Rule.VID)); | ||
WriteDword(RuleName, USBDK_HIDE_RULE_PID, static_cast<ULONG>(Rule.PID)); | ||
WriteDword(RuleName, USBDK_HIDE_RULE_BCD, static_cast<ULONG>(Rule.BCD)); | ||
WriteDword(RuleName, USBDK_HIDE_RULE_CLASS, static_cast<ULONG>(Rule.Class)); | ||
} | ||
|
||
void CRulesManager::DeleteRule(const USB_DK_HIDE_RULE &Rule) | ||
{ | ||
tstring RuleName; | ||
|
||
if (FindRule(Rule, [&RuleName](LPCTSTR Name){ RuleName = Name; })) | ||
{ | ||
if (!m_RegAccess.DeleteKey(RuleName.c_str())) | ||
{ | ||
throw UsbDkRuleManagerException(TEXT("Failed to delete rule key"), ERROR_FUNCTION_FAILED); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
#define RULE_MANAGER_EXCEPTION_STRING TEXT("RuleManager exception: ") | ||
|
||
class UsbDkRuleManagerException : public UsbDkW32ErrorException | ||
{ | ||
public: | ||
UsbDkRuleManagerException() : UsbDkW32ErrorException(RULE_MANAGER_EXCEPTION_STRING){} | ||
UsbDkRuleManagerException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + lpzMessage){} | ||
UsbDkRuleManagerException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} | ||
UsbDkRuleManagerException(tstring errMsg) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + errMsg){} | ||
UsbDkRuleManagerException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + errMsg, dwErrorCode){} | ||
}; | ||
|
||
class CRulesManager | ||
{ | ||
public: | ||
CRulesManager(); | ||
|
||
void AddRule(const USB_DK_HIDE_RULE &Rule); | ||
void DeleteRule(const USB_DK_HIDE_RULE &Rule); | ||
private: | ||
template <typename TFunctor> | ||
bool FindRule(const USB_DK_HIDE_RULE &Rule, TFunctor Functor); | ||
bool RuleExists(const USB_DK_HIDE_RULE &Rule); | ||
|
||
DWORD ReadDword(LPCTSTR RuleName, LPCTSTR ValueName) const; | ||
ULONG64 ReadDwordMask(LPCTSTR RuleName, LPCTSTR ValueName) const; | ||
ULONG64 ReadBool(LPCTSTR RuleName, LPCTSTR ValueName) const; | ||
void WriteDword(const tstring &RuleName, LPCTSTR ValueName, ULONG Value); | ||
|
||
void ReadRule(LPCTSTR RuleName, USB_DK_HIDE_RULE &Rule) const; | ||
|
||
UsbDkRegAccess m_RegAccess; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters