summaryrefslogtreecommitdiff
path: root/src/vm/assemblynamesconfigfactory.cpp
blob: 68fefc8029ab17c80400ce1a18f0d6337870ca42 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// AssemblyNamesConfigFactory.cpp
//

//
// 
// Parses XML files and adding runtime entries to assembly list
// Abstract, derived classes need to override AddAssemblyName
#include "common.h"
#include "common.h"
#include <xmlparser.h>
#include <objbase.h>
#include "parse.h"
#include "assemblynamesconfigfactory.h"


#define ISWHITE(ch) ((ch) >= 0x09 && (ch) <= 0x0D || (ch) == 0x20)

#define CONST_STRING_AND_LEN(str) str, NumItems(str)-1

extern int EEXMLStringCompare(const WCHAR *pStr1, 
                    DWORD cchStr1, 
                    const WCHAR *pStr2, 
                    DWORD cchStr2);
extern HRESULT VersionFromString(LPCWSTR wzVersion, WORD *pwVerMajor, WORD *pwVerMinor,
                          WORD *pwVerBld, WORD *pwVerRev);
extern HRESULT MapProcessorArchitectureToPEKIND(LPCWSTR pwzProcArch, PEKIND *pe);

AssemblyNamesConfigFactory::AssemblyNamesConfigFactory()
{
    LIMITED_METHOD_CONTRACT;
    m_pAssemblyName = NULL;
    m_bCurrentEntryInvalid = TRUE;
    m_dwCurrentElementDepth = 0;
    m_dwProperty = ASM_NAME_MAX_PARAMS;
}

AssemblyNamesConfigFactory::~AssemblyNamesConfigFactory()
{
    LIMITED_METHOD_CONTRACT;
}


HRESULT STDMETHODCALLTYPE AssemblyNamesConfigFactory::NotifyEvent( 
            /* [in] */ IXMLNodeSource __RPC_FAR *pSource,
            /* [in] */ XML_NODEFACTORY_EVENT iEvt)
{
    LIMITED_METHOD_CONTRACT;
    
    return S_OK;
}

HRESULT STDMETHODCALLTYPE AssemblyNamesConfigFactory::BeginChildren( 
    /* [in] */ IXMLNodeSource __RPC_FAR *pSource,
    /* [in] */ XML_NODE_INFO __RPC_FAR *pNodeInfo)
{
    LIMITED_METHOD_CONTRACT;
    m_dwCurrentElementDepth ++;

    return S_OK;
}

//---------------------------------------------------------------------------
HRESULT STDMETHODCALLTYPE AssemblyNamesConfigFactory::EndChildren( 
    /* [in] */ IXMLNodeSource __RPC_FAR *pSource,
    /* [in] */ BOOL fEmptyNode,
    /* [in] */ XML_NODE_INFO __RPC_FAR *pNodeInfo)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        INJECT_FAULT(return E_OUTOFMEMORY;);
    }
    CONTRACTL_END;

    HRESULT hr = S_OK;
    EX_TRY
    {
        if (m_dwCurrentElementDepth == 1 && m_pAssemblyName != NULL)
        {
            if (!m_bCurrentEntryInvalid)
            {
                // publish 
                AddAssemblyName(m_pAssemblyName);
            };
            m_pAssemblyName->Release();
            m_pAssemblyName = NULL;
        }

        if (!fEmptyNode)
            m_dwCurrentElementDepth --;
    }
    EX_CATCH_HRESULT(hr);
    return hr;
}



HRESULT STDMETHODCALLTYPE AssemblyNamesConfigFactory::CreateNode( 
    /* [in] */ IXMLNodeSource __RPC_FAR *pSource,
    /* [in] */ PVOID pNode,
    /* [in] */ USHORT cNumRecs,
    /* [in] */ XML_NODE_INFO* __RPC_FAR * __RPC_FAR apNodeInfo)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        INJECT_FAULT(return E_OUTOFMEMORY;);
    }
    CONTRACTL_END;

    if(m_dwCurrentElementDepth > 1)
        return S_OK;

    HRESULT hr = S_OK;

    for(DWORD i = 0; i < cNumRecs; i++) { 
        CONTRACT_VIOLATION(ThrowsViolation); // Lots of stuff in here throws!
        
        if(apNodeInfo[i]->dwType == XML_ELEMENT ||
           apNodeInfo[i]->dwType == XML_ATTRIBUTE ||
           apNodeInfo[i]->dwType == XML_PCDATA) 
        {

            DWORD dwStringSize = apNodeInfo[i]->ulLen;
            LPWSTR pszString = (WCHAR*) apNodeInfo[i]->pwcText;
            // Trim the value

            // we should never decrement lgth if it's 0, because it's unsigned

            for(;*pszString && ISWHITE(*pszString) && dwStringSize>0; pszString++, dwStringSize--);
            while( dwStringSize > 0 && ISWHITE(pszString[dwStringSize-1]))
                   dwStringSize--;
            switch(apNodeInfo[i]->dwType) 
            {
            case XML_ELEMENT : 
                if(EEXMLStringCompare(pszString, dwStringSize, CONST_STRING_AND_LEN(W("assemblyIdentity"))) == 0) 
                {
                    // new entry 
                    _ASSERTE(m_pAssemblyName == NULL);
                    IfFailRet(CreateAssemblyNameObject(&m_pAssemblyName, NULL,0,NULL));
                    m_bCurrentEntryInvalid = FALSE;
                }
                else
                {
                    m_bCurrentEntryInvalid = TRUE;
                }

                break;


            case XML_ATTRIBUTE : 
                if(m_bCurrentEntryInvalid)
                    break;

                if(EEXMLStringCompare(pszString, dwStringSize, CONST_STRING_AND_LEN(W("name"))) == 0) 
                {
                    m_dwProperty = ASM_NAME_NAME;
                }
                else
                if(EEXMLStringCompare(pszString, dwStringSize, CONST_STRING_AND_LEN(W("version"))) == 0) 
                {
                    m_dwProperty = ASM_NAME_MAJOR_VERSION;
                }
                else
                if(EEXMLStringCompare(pszString, dwStringSize, CONST_STRING_AND_LEN(W("publicKeyToken"))) == 0) 
                {
                    m_dwProperty = ASM_NAME_PUBLIC_KEY_TOKEN;
                }
                else
                if(EEXMLStringCompare(pszString, dwStringSize, CONST_STRING_AND_LEN(W("processorArchitecture"))) == 0) 
                {
                    m_dwProperty = ASM_NAME_ARCHITECTURE;
                }
                else
                {
                    m_bCurrentEntryInvalid = TRUE;
                }
                break;


            case XML_PCDATA : 
                if(m_bCurrentEntryInvalid)
                    break;

                _ASSERTE(m_pAssemblyName!= NULL); // can only be null if m_bCurrentEntryInvalid
                switch(m_dwProperty)
                {
                case ASM_NAME_NAME:
                    {
                        StackSString s(pszString,dwStringSize);
                        // takes number of bytes, thus *2
                        IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_NAME, LPCWSTR(s), (dwStringSize+1)*sizeof(WCHAR)));
                    }
                    break;
                case ASM_NAME_MAJOR_VERSION:
                    {
                        StackSString s(pszString,dwStringSize);
                        WORD  wVerMajor = 0;
                        WORD  wVerMinor = 0;
                        WORD  wVerBld = 0;
                        WORD  wVerRev = 0;
                        if (SUCCEEDED(VersionFromString(s, &wVerMajor, &wVerMinor, &wVerBld, &wVerRev)))
                        {
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_MAJOR_VERSION, &wVerMajor, sizeof(WORD)));
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_MINOR_VERSION, &wVerMinor, sizeof(WORD)));
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_BUILD_NUMBER, &wVerBld, sizeof(WORD)));
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_REVISION_NUMBER, &wVerRev, sizeof(WORD)));
                        }
                        else
                            m_bCurrentEntryInvalid = TRUE;

                    }
                    break;
                case ASM_NAME_ARCHITECTURE:
                    {
                        StackSString s(pszString,dwStringSize);
                        PEKIND PeKind = peNone;
                        if(SUCCEEDED(MapProcessorArchitectureToPEKIND(s, &PeKind)))
                        {
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_ARCHITECTURE, (LPBYTE) &PeKind, sizeof(PeKind)));
                        }
                        else
                        {
                            m_bCurrentEntryInvalid = TRUE;
                        }

                    }
                    break;    
                case ASM_NAME_PUBLIC_KEY_TOKEN:
                    {
                       if(EEXMLStringCompare(pszString, dwStringSize, CONST_STRING_AND_LEN(W("null"))) == 0) 
                        {
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_NULL_PUBLIC_KEY_TOKEN, NULL, 0));
                        }
                        else
                        {
                            if (dwStringSize % 2 != 0)
                                return FUSION_E_INVALID_NAME;

                            DWORD cbProp = dwStringSize / 2;
                            NewHolder<BYTE> pbProp = new BYTE[cbProp];
                            CParseUtils::UnicodeHexToBin(pszString, dwStringSize, pbProp); //????
                            IfFailRet(m_pAssemblyName->SetProperty(ASM_NAME_PUBLIC_KEY_TOKEN, pbProp, cbProp));
                        }
                        break;
                    }

                default:
                    _ASSERTE(!"Invalid format");
                    m_bCurrentEntryInvalid = TRUE;
                    break;
                }
                break;
            }

        }
    }
    return S_OK;
}