summaryrefslogtreecommitdiff
path: root/src/strongname/inc/strongnameholders.h
blob: 36597ea97d728c88bc81c341127f37b70925965d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#ifndef __STRONGNAME_HOLDERS_H__
#define __STRONGNAME_HOLDERS_H__

#include <holder.h>
#include <strongname.h>
#include <wincrypt.h>

//
// Holder classes for types returned from and used in strong name APIs
//

// Holder for any memory allocated by the strong name APIs
template<class T>
void VoidStrongNameFreeBuffer(__in T *pBuffer)
{
    StrongNameFreeBuffer(reinterpret_cast<BYTE *>(pBuffer));
}
NEW_WRAPPER_TEMPLATE1(StrongNameBufferHolder, VoidStrongNameFreeBuffer<_TYPE>);

#if !defined(FEATURE_CORECLR) || (defined(CROSSGEN_COMPILE) && !defined(PLATFORM_UNIX))
// Holder for HCRYPTPROV handles directly allocated from CAPI
inline void ReleaseCapiProvider(HCRYPTPROV hProv)
{
    CryptReleaseContext(hProv, 0);
}
typedef Wrapper<HCRYPTPROV, DoNothing, ReleaseCapiProvider, 0> CapiProviderHolder;

inline void ReleaseCapiKey(HCRYPTKEY hKey)
{
    CryptDestroyKey(hKey);
}
typedef Wrapper<HCRYPTKEY, DoNothing, ReleaseCapiKey, 0> CapiKeyHolder;

inline void ReleaseCapiHash(HCRYPTHASH hHash)
{
    CryptDestroyHash(hHash);
}
typedef Wrapper<HCRYPTHASH, DoNothing, ReleaseCapiHash, 0> CapiHashHolder;
#endif // !FEATURE_CORECLR || CROSSGEN_COMPILE

#if SNAPI_INTERNAL

// Context structure tracking information for a loaded assembly.
struct SN_LOAD_CTX
{
    HANDLE              m_hFile;        // Open file handle
    HANDLE              m_hMap;         // Mapping file handle
    BYTE               *m_pbBase;       // Base address of mapped file
    DWORD               m_dwLength;     // Length of file in bytes
    IMAGE_NT_HEADERS32  *m_pNtHeaders;   // Address of NT headers
    IMAGE_COR20_HEADER *m_pCorHeader;   // Address of COM+ 2.0 header
    BYTE               *m_pbSignature;  // Address of signature blob
    DWORD               m_cbSignature;  // Size of signature blob
    BOOLEAN             m_fReadOnly;    // File mapped for read-only access
    BOOLEAN             m_fPreMapped;   // File was already mapped for us
    PEDecoder           *m_pedecoder;    // PEDecoder corresponding to this file
    SN_LOAD_CTX() { ZeroMemory(this, sizeof(*this)); }
};

BOOLEAN LoadAssembly(SN_LOAD_CTX *pLoadCtx, LPCWSTR szFilePath, DWORD inFlags = 0, BOOLEAN fRequireSignature = TRUE);
BOOLEAN UnloadAssembly(SN_LOAD_CTX *pLoadCtx);

// Holder for loading an assembly into an SN_LOAD_CTX
class StrongNameAssemblyLoadHolder
{
private:
    SN_LOAD_CTX m_snLoadCtx;
    bool m_fLoaded;

public:
    StrongNameAssemblyLoadHolder(LPCWSTR wszAssembly, bool fReadOnly)
    {
        m_snLoadCtx.m_fReadOnly = !!fReadOnly;
        m_fLoaded = !!LoadAssembly(&m_snLoadCtx, wszAssembly);
    }

    ~StrongNameAssemblyLoadHolder()
    {
        if (m_fLoaded)
        {
            UnloadAssembly(&m_snLoadCtx);
        }
    }

public:
    SN_LOAD_CTX *GetLoadContext()
    {
        return &m_snLoadCtx;
    }

    bool IsLoaded()
    {
        return m_fLoaded;
    }
};

#endif // SNAPI_INTERNAL

#endif // !__STRONGNAME_HOLDERS_H__