From 45a741d7db75ef134565e83fcadd464ff4bf2d44 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 9 Apr 2015 08:50:24 +0000 Subject: [PATCH] =?UTF-8?q?Gradual=20code=20migration=20to=20ATL=20and=20o?= =?UTF-8?q?bject-oriented=20approach:=20-=20Configuration,=20user=20and=20?= =?UTF-8?q?session=20data=20are=20now=20internally=20stored=20as=20objects?= =?UTF-8?q?.=20-=20Object=20wrappers=20for=20many=20structs=20and=20primit?= =?UTF-8?q?ives=20added.=20-=20Code=20partially=20rewritten=20to=20work=20?= =?UTF-8?q?with=20objects=20and=20benefit=20out-of-scope=20clean-up=20usin?= =?UTF-8?q?g=20object=20destructors.=20-=20=E2=80=A6And=20a=20lot=20more?= =?UTF-8?q?=20code=20internal=20changes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Configuration, user and other data is now passed as compact BLOBs in inter-process communication greatly reducing communication payload. --- atl.vcxproj | 1 + atl.vcxproj.filters | 3 + atlcrypt.h | 42 ++++++++++- atleap.h | 46 ++++++++++++ atlex.h | 179 +++++++++++++++++++++++++++++++++++++++++--- atlwin.h | 73 ++++++++++++++++++ 6 files changed, 331 insertions(+), 13 deletions(-) create mode 100644 atleap.h diff --git a/atl.vcxproj b/atl.vcxproj index be2a208..a8d6020 100644 --- a/atl.vcxproj +++ b/atl.vcxproj @@ -104,6 +104,7 @@ + diff --git a/atl.vcxproj.filters b/atl.vcxproj.filters index 37be935..98c6f2d 100644 --- a/atl.vcxproj.filters +++ b/atl.vcxproj.filters @@ -37,5 +37,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/atlcrypt.h b/atlcrypt.h index 19c3d60..5d08c36 100644 --- a/atlcrypt.h +++ b/atlcrypt.h @@ -102,7 +102,7 @@ namespace ATL // // CCertContext // - class CCertContext : public ATL::CObjectWithHandleT + class CCertContext : public ATL::CObjectWithHandleDuplT { public: virtual ~CCertContext() throw() @@ -126,6 +126,46 @@ namespace ATL { CertFreeCertificateContext(m_h); } + + virtual HANDLE InternalDuplicate(HANDLE h) const + { + return CertDuplicateCertificateContext(h); + } + }; + + + // + // CCertChainContext + // + class CCertChainContext : public ATL::CObjectWithHandleDuplT + { + public: + virtual ~CCertChainContext() throw() + { + if (m_h) + CertFreeCertificateChain(m_h); + } + + inline BOOL Create(__in_opt HCERTCHAINENGINE hChainEngine, __in PCCERT_CONTEXT pCertContext, __in_opt LPFILETIME pTime, __in_opt HCERTSTORE hAdditionalStore, __in PCERT_CHAIN_PARA pChainPara, __in DWORD dwFlags, __reserved LPVOID pvReserved) throw() + { + HANDLE h; + if (CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &h)) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + protected: + virtual void InternalFree() + { + CertFreeCertificateChain(m_h); + } + + virtual HANDLE InternalDuplicate(HANDLE h) const + { + return CertDuplicateCertificateChain(h); + } }; diff --git a/atleap.h b/atleap.h new file mode 100644 index 0000000..5951c70 --- /dev/null +++ b/atleap.h @@ -0,0 +1,46 @@ +/* + Copyright 1991-2015 Amebis + + This file is part of libatl. + + Setup is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Setup 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Setup. If not, see . +*/ + +#pragma once + +#include + + +namespace ATL +{ + namespace EAP + { + class CEAPAttribute : public EAP_ATTRIBUTE + { + public: + CEAPAttribute() + { + eaType = eatReserved; + dwLength = 0; + pValue = NULL; + } + + ~CEAPAttribute() + { + if (pValue) + delete pValue; + } + }; + } +} diff --git a/atlex.h b/atlex.h index dc29262..e9a5a60 100644 --- a/atlex.h +++ b/atlex.h @@ -20,6 +20,7 @@ #pragma once #include +#include namespace ATL @@ -37,28 +38,28 @@ namespace ATL { } - inline CObjectWithHandleT(T h) throw() : m_h(h) + inline CObjectWithHandleT(HANDLE h) throw() : m_h(h) { } - inline operator T() const throw() + inline operator HANDLE() const throw() { return m_h; } - inline T*& operator*() const + inline HANDLE*& operator*() const { ATLENSURE(m_h != NULL); return *m_h; } - inline T* operator&() throw() + inline HANDLE* operator&() throw() { ATLASSERT(m_h == NULL); return &m_h; } - inline T operator->() const throw() + inline HANDLE operator->() const throw() { ATLASSERT(m_h != NULL); return m_h; @@ -69,31 +70,31 @@ namespace ATL return m_h == NULL; } - inline bool operator<(_In_opt_ T h) const throw() + inline bool operator<(_In_opt_ HANDLE h) const throw() { return m_h < h; } - inline bool operator!=(_In_opt_ T h) const + inline bool operator!=(_In_opt_ HANDLE h) const { return !operator==(h); } - inline bool operator==(_In_opt_ T h) const throw() + inline bool operator==(_In_opt_ HANDLE h) const throw() { return m_h == h; } - inline void Attach(_In_opt_ T h) throw() + inline void Attach(_In_opt_ HANDLE h) throw() { if (m_h) InternalFree(); m_h = h; } - inline T Detach() throw() + inline HANDLE Detach() throw() { - T h = m_h; + HANDLE h = m_h; m_h = NULL; return h; } @@ -110,6 +111,160 @@ namespace ATL virtual void InternalFree() = 0; protected: - T m_h; + HANDLE m_h; }; + + + // + // CObjectWithHandleDuplT + // + template + class CObjectWithHandleDuplT : public CObjectWithHandleT + { + public: + inline HANDLE GetDuplicate() const + { + return m_h ? InternalDuplicate(m_h) : NULL; + } + + inline BOOL DuplicateAndAttach(_In_opt_ HANDLE h) throw() + { + if (m_h) + InternalFree(); + + return h ? (m_h = InternalDuplicate(h)) != NULL : (m_h = NULL, TRUE); + } + + // + // Do not allow = operators. They are semantically ambigious: + // Do they attach the class to the existing instance of object, or do they duplicate it? + // To avoid confusion, user should use Attach() and Duplicate() methods explicitly. + // + //inline const CObjectWithHandleDuplT& operator=(_In_ const HANDLE src) + //{ + // Attach(src ? InternalDuplicate(src) : NULL); + // return *this; + //} + + //inline const CObjectWithHandleDuplT& operator=(_In_ const CObjectWithHandleDuplT &src) + //{ + // Attach(src.m_h ? InternalDuplicate(src.m_h) : NULL); + // return *this; + //} + + protected: + virtual HANDLE InternalDuplicate(HANDLE h) const = 0; + }; + + + // + // CStrFormatT, CStrFormatW, CStrFormatA, CStrFormat + // + template + class CStrFormatT : public CStringT + { + public: + CStrFormatT(_In_z_ _FormatMessage_format_string_ PCXSTR pszFormat, ...) + { + ATLASSERT(AtlIsValidString(pszFormat)); + + va_list argList; + va_start(argList, pszFormat); + FormatV(pszFormat, argList); + va_end(argList); + } + + CStrFormatT(_In_ _FormatMessage_format_string_ UINT nFormatID, ...) + { + CStringT strFormat(GetManager()); + ATLENSURE(strFormat.LoadString(nFormatID)); + + va_list argList; + va_start(argList, nFormatID); + FormatV(strFormat, argList); + va_end(argList); + } + + CStrFormatT(_In_ HINSTANCE hInstance, _In_ _FormatMessage_format_string_ UINT nFormatID, ...) + { + CStringT strFormat(GetManager()); + ATLENSURE(strFormat.LoadString(hInstance, nFormatID)); + + va_list argList; + va_start(argList, nFormatID); + FormatV(strFormat, argList); + va_end(argList); + } + + CStrFormatT(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ _FormatMessage_format_string_ UINT nFormatID, ...) + { + CStringT strFormat(GetManager()); + ATLENSURE(strFormat.LoadString(hInstance, nFormatID, wLanguageID)); + + va_list argList; + va_start(argList, nFormatID); + FormatV(strFormat, argList); + va_end(argList); + } + }; + + typedef CStrFormatT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT< wchar_t > > > CStrFormatW; + typedef CStrFormatT< char, StrTraitATL< char, ChTraitsCRT< char > > > CStrFormatA; + typedef CStrFormatT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CStrFormat; + + + // + // CStrFormatMsgT, CStrFormatMsgW, CStrFormatMsgA, CStrFormatMsg + // + template + class CStrFormatMsgT : public CStringT + { + public: + CStrFormatMsgT(_In_z_ _FormatMessage_format_string_ PCXSTR pszFormat, ...) + { + ATLASSERT(AtlIsValidString(pszFormat)); + + va_list argList; + va_start(argList, pszFormat); + FormatMessageV(pszFormat, &argList); + va_end(argList); + } + + CStrFormatMsgT(_In_ _FormatMessage_format_string_ UINT nFormatID, ...) + { + CStringT strFormat(GetManager()); + ATLENSURE(strFormat.LoadString(nFormatID)); + + va_list argList; + va_start(argList, nFormatID); + FormatMessageV(strFormat, &argList); + va_end(argList); + } + + CStrFormatMsgT(_In_ HINSTANCE hInstance, _In_ _FormatMessage_format_string_ UINT nFormatID, ...) + { + CStringT strFormat(GetManager()); + ATLENSURE(strFormat.LoadString(hInstance, nFormatID)); + + va_list argList; + va_start(argList, nFormatID); + FormatMessageV(strFormat, &argList); + va_end(argList); + } + + CStrFormatMsgT(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ _FormatMessage_format_string_ UINT nFormatID, ...) + { + CStringT strFormat(GetManager()); + ATLENSURE(strFormat.LoadString(hInstance, nFormatID, wLanguageID)); + + va_list argList; + va_start(argList, nFormatID); + FormatMessageV(strFormat, &argList); + va_end(argList); + } + }; + + typedef CStrFormatMsgT< wchar_t, StrTraitATL< wchar_t, ChTraitsCRT< wchar_t > > > CStrFormatMsgW; + typedef CStrFormatMsgT< char, StrTraitATL< char, ChTraitsCRT< char > > > CStrFormatMsgA; + typedef CStrFormatMsgT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CStrFormatMsg; } diff --git a/atlwin.h b/atlwin.h index c213767..f4056de 100644 --- a/atlwin.h +++ b/atlwin.h @@ -19,6 +19,7 @@ #pragma once +#include "atlex.h" #include #include #include @@ -348,3 +349,75 @@ inline LSTATUS RegQueryValueExW(__in HKEY hKey, __in_opt LPCWSTR lpValueName, __ return lResult; } + + +#if _WIN32_WINNT >= _WIN32_WINNT_VISTA + +inline LSTATUS RegLoadMUIStringA(__in HKEY hKey, __in_opt LPCSTR pszValue, __out ATL::CAtlStringA &sOut, __in DWORD Flags, __in_opt LPCSTR pszDirectory) +{ + LSTATUS lResult; + DWORD dwSize; + + Flags &= ~REG_MUI_STRING_TRUNCATE; + + lResult = RegLoadMUIStringA(hKey, pszValue, NULL, 0, &dwSize, Flags, pszDirectory); + if (lResult == ERROR_MORE_DATA) { + LPSTR szBuffer = sOut.GetBuffer(dwSize - 1); + if (!szBuffer) return ERROR_OUTOFMEMORY; + sOut.ReleaseBuffer((lResult = RegLoadMUIStringA(hKey, pszValue, szBuffer, dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? dwSize - 1 : 0); + } else if (lResult == ERROR_SUCCESS) + sOut.Empty(); + + return lResult; +} + + +inline LSTATUS RegLoadMUIStringW(__in HKEY hKey, __in_opt LPCWSTR pszValue, __out ATL::CAtlStringW &sOut, __in DWORD Flags, __in_opt LPCWSTR pszDirectory) +{ + LSTATUS lResult; + DWORD dwSize; + + Flags &= ~REG_MUI_STRING_TRUNCATE; + + lResult = RegLoadMUIStringW(hKey, pszValue, NULL, 0, &dwSize, Flags, pszDirectory); + if (lResult == ERROR_MORE_DATA) { + LPWSTR szBuffer = sOut.GetBuffer(dwSize - 1); + if (!szBuffer) return ERROR_OUTOFMEMORY; + sOut.ReleaseBuffer((lResult = RegLoadMUIStringW(hKey, pszValue, szBuffer, dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? dwSize - 1 : 0); + } else if (lResult == ERROR_SUCCESS) + sOut.Empty(); + + return lResult; +} + +#endif + + +namespace ATL +{ + class CAtlLibrary : public CObjectWithHandleT + { + public: + virtual ~CAtlLibrary() throw() + { + if (m_h) + FreeLibrary(m_h); + } + + inline BOOL Load(__in LPCTSTR lpFileName, __reserved HANDLE hFile, __in DWORD dwFlags) throw() + { + HANDLE h = LoadLibraryEx(lpFileName, hFile, dwFlags); + if (h) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + protected: + virtual void InternalFree() + { + FreeLibrary(m_h); + } + }; +}