Skip to content

Commit

Permalink
UsbDkHelper: Introduce class for hide rules management
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
127 changes: 127 additions & 0 deletions UsbDkHelper/RuleManager.cpp
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);
}
}
}
34 changes: 34 additions & 0 deletions UsbDkHelper/RuleManager.h
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;
};
19 changes: 19 additions & 0 deletions UsbDkHelper/UsbDkHelper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,24 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|x64'">Use</PrecompiledHeader>
</ClCompile>
<ClCompile Include="RuleManager.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Release|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win7 Release|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='XP Release|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win7 Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='XP Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8.1 Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8.1 Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win7 Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='XP Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win7 Debug|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='XP Debug|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8.1 Debug|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Debug|x64'">Use</PrecompiledHeader>
</ClCompile>
<ClCompile Include="UsbDkHelper.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win8 Debug|x64'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Win7 Debug|x64'">Use</PrecompiledHeader>
Expand Down Expand Up @@ -1001,6 +1019,7 @@
<ClInclude Include="RedirectorAccess.h" />
<ClInclude Include="RegAccess.h" />
<ClInclude Include="DeviceMgr.h" />
<ClInclude Include="RuleManager.h" />
<ClInclude Include="UsbDkCompat.h" />
<ClInclude Include="UsbDkHelper.h" />
<ClInclude Include="ServiceManager.h" />
Expand Down
6 changes: 6 additions & 0 deletions UsbDkHelper/UsbDkHelper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClCompile Include="GuidGen.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RuleManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="UsbDkHelper.h">
Expand Down Expand Up @@ -113,6 +116,9 @@
<ClInclude Include="GuidGen.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RuleManager.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.rc">
Expand Down

0 comments on commit 408a787

Please sign in to comment.