1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | #ifndef __STRONGNAME_HOLDERS_H__ |
6 | #define __STRONGNAME_HOLDERS_H__ |
7 | |
8 | #include <holder.h> |
9 | #include <strongname.h> |
10 | #include <wincrypt.h> |
11 | |
12 | // |
13 | // Holder classes for types returned from and used in strong name APIs |
14 | // |
15 | |
16 | // Holder for any memory allocated by the strong name APIs |
17 | template<class T> |
18 | void VoidStrongNameFreeBuffer(__in T *pBuffer) |
19 | { |
20 | StrongNameFreeBuffer(reinterpret_cast<BYTE *>(pBuffer)); |
21 | } |
22 | NEW_WRAPPER_TEMPLATE1(StrongNameBufferHolder, VoidStrongNameFreeBuffer<_TYPE>); |
23 | |
24 | #if defined(CROSSGEN_COMPILE) && !defined(PLATFORM_UNIX) |
25 | // Holder for HCRYPTPROV handles directly allocated from CAPI |
26 | inline void ReleaseCapiProvider(HCRYPTPROV hProv) |
27 | { |
28 | CryptReleaseContext(hProv, 0); |
29 | } |
30 | typedef Wrapper<HCRYPTPROV, DoNothing, ReleaseCapiProvider, 0> CapiProviderHolder; |
31 | |
32 | inline void ReleaseCapiKey(HCRYPTKEY hKey) |
33 | { |
34 | CryptDestroyKey(hKey); |
35 | } |
36 | typedef Wrapper<HCRYPTKEY, DoNothing, ReleaseCapiKey, 0> CapiKeyHolder; |
37 | |
38 | inline void ReleaseCapiHash(HCRYPTHASH hHash) |
39 | { |
40 | CryptDestroyHash(hHash); |
41 | } |
42 | typedef Wrapper<HCRYPTHASH, DoNothing, ReleaseCapiHash, 0> CapiHashHolder; |
43 | #endif // defined(CROSSGEN_COMPILE) && !defined(PLATFORM_UNIX) |
44 | |
45 | #if SNAPI_INTERNAL |
46 | |
47 | // Context structure tracking information for a loaded assembly. |
48 | struct SN_LOAD_CTX |
49 | { |
50 | HANDLE m_hFile; // Open file handle |
51 | HANDLE m_hMap; // Mapping file handle |
52 | BYTE *m_pbBase; // Base address of mapped file |
53 | DWORD m_dwLength; // Length of file in bytes |
54 | IMAGE_NT_HEADERS32 *m_pNtHeaders; // Address of NT headers |
55 | IMAGE_COR20_HEADER *m_pCorHeader; // Address of COM+ 2.0 header |
56 | BYTE *m_pbSignature; // Address of signature blob |
57 | DWORD m_cbSignature; // Size of signature blob |
58 | BOOLEAN m_fReadOnly; // File mapped for read-only access |
59 | BOOLEAN m_fPreMapped; // File was already mapped for us |
60 | PEDecoder *m_pedecoder; // PEDecoder corresponding to this file |
61 | SN_LOAD_CTX() { ZeroMemory(this, sizeof(*this)); } |
62 | }; |
63 | |
64 | BOOLEAN LoadAssembly(SN_LOAD_CTX *pLoadCtx, LPCWSTR szFilePath, DWORD inFlags = 0, BOOLEAN fRequireSignature = TRUE); |
65 | BOOLEAN UnloadAssembly(SN_LOAD_CTX *pLoadCtx); |
66 | |
67 | // Holder for loading an assembly into an SN_LOAD_CTX |
68 | class StrongNameAssemblyLoadHolder |
69 | { |
70 | private: |
71 | SN_LOAD_CTX m_snLoadCtx; |
72 | bool m_fLoaded; |
73 | |
74 | public: |
75 | StrongNameAssemblyLoadHolder(LPCWSTR wszAssembly, bool fReadOnly) |
76 | { |
77 | m_snLoadCtx.m_fReadOnly = !!fReadOnly; |
78 | m_fLoaded = !!LoadAssembly(&m_snLoadCtx, wszAssembly); |
79 | } |
80 | |
81 | ~StrongNameAssemblyLoadHolder() |
82 | { |
83 | if (m_fLoaded) |
84 | { |
85 | UnloadAssembly(&m_snLoadCtx); |
86 | } |
87 | } |
88 | |
89 | public: |
90 | SN_LOAD_CTX *GetLoadContext() |
91 | { |
92 | return &m_snLoadCtx; |
93 | } |
94 | |
95 | bool IsLoaded() |
96 | { |
97 | return m_fLoaded; |
98 | } |
99 | }; |
100 | |
101 | #endif // SNAPI_INTERNAL |
102 | |
103 | #endif // !__STRONGNAME_HOLDERS_H__ |
104 | |