summaryrefslogtreecommitdiff
path: root/src/inc/caparser.h
blob: 0dfbb918b2cca266651516493a6a605ec5f56b8b (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// File: caparser.h
//


//

//
// ============================================================================

#ifndef __CAPARSER_H__
#define __CAPARSER_H__

#include "stgpooli.h"

class CustomAttributeParser {
public:
    CustomAttributeParser(              // Constructor for CustomAttributeParser.
        const void *pvBlob,             // Pointer to the CustomAttribute blob.
        ULONG   cbBlob)                 // Size of the CustomAttribute blob.
     :  m_pbCur(reinterpret_cast<const BYTE*>(pvBlob)),
        m_pbBlob(reinterpret_cast<const BYTE*>(pvBlob)),
        m_cbBlob(cbBlob)
    {
        LIMITED_METHOD_CONTRACT;
    }

private:
    signed __int8    GetI1()
    {
        LIMITED_METHOD_CONTRACT;
        signed __int8 tmp = *reinterpret_cast<const signed __int8*>(m_pbCur);
        m_pbCur += sizeof(signed __int8);
        return tmp;
    }
    unsigned __int8  GetU1()
    {
        LIMITED_METHOD_CONTRACT;
        unsigned __int8 tmp = *reinterpret_cast<const unsigned __int8*>(m_pbCur);
        m_pbCur += sizeof(unsigned __int8);
        return tmp;
    }

    signed __int16   GetI2()
    {
        LIMITED_METHOD_CONTRACT;
        signed __int16 tmp = GET_UNALIGNED_VAL16(m_pbCur);
        m_pbCur += sizeof(signed __int16);
        return tmp;
    }
    unsigned __int16 GetU2()
    {
        LIMITED_METHOD_CONTRACT;
        unsigned __int16 tmp = GET_UNALIGNED_VAL16(m_pbCur);
        m_pbCur += sizeof(unsigned __int16 );
        return tmp;
    }

    signed __int32   GetI4()
    {
        LIMITED_METHOD_CONTRACT;
        signed __int32 tmp = GET_UNALIGNED_VAL32(m_pbCur);
        m_pbCur += sizeof(signed __int32 );
        return tmp;
    }
    unsigned __int32 GetU4()
    {
        LIMITED_METHOD_CONTRACT;
        unsigned __int32 tmp = GET_UNALIGNED_VAL32(m_pbCur);
        m_pbCur += sizeof(unsigned __int32 );
        return tmp;
    }

    signed __int64   GetI8()
    {
        LIMITED_METHOD_CONTRACT;
        signed __int64 tmp = GET_UNALIGNED_VAL64(m_pbCur);
        m_pbCur += sizeof(signed __int64 );
        return tmp;
    }
    unsigned __int64 GetU8()
    {
        LIMITED_METHOD_CONTRACT;
        unsigned __int64 tmp = GET_UNALIGNED_VAL64(m_pbCur);
        m_pbCur += sizeof(unsigned __int64 );
        return tmp;
    }

public:
    float            GetR4()
    {
        LIMITED_METHOD_CONTRACT;
        __int32 tmp = GET_UNALIGNED_VAL32(m_pbCur);
        _ASSERTE(sizeof(__int32) == sizeof(float));
        m_pbCur += sizeof(float);
        return (float &)tmp;
    }

    double           GetR8()
    {
        LIMITED_METHOD_CONTRACT;
        __int64 tmp = GET_UNALIGNED_VAL64(m_pbCur);
        _ASSERTE(sizeof(__int64) == sizeof(double));
        m_pbCur += sizeof(double);
        return (double &)tmp;
    }

private:
    unsigned __int16 GetProlog()
    {
        WRAPPER_NO_CONTRACT;
        unsigned __int16 val;
        VERIFY(SUCCEEDED(GetProlog(&val)));
        return val;
    }

    LPCUTF8 GetString(ULONG *pcbString)
    {
        WRAPPER_NO_CONTRACT;
        LPCUTF8 val;
        VERIFY(SUCCEEDED(GetString(&val, pcbString)));
        return val;
    }

public:
    HRESULT GetI1(signed __int8 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(signed __int8))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetI1();
        return S_OK;
    }
    
    HRESULT GetTag(CorSerializationType *pVal)
    {
        WRAPPER_NO_CONTRACT;
        HRESULT hr;
        signed __int8 tmp;
        IfFailRet(GetI1(&tmp));
        *pVal = (CorSerializationType)((unsigned __int8)tmp);
        return hr;
    }

    HRESULT GetU1(unsigned __int8 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(unsigned __int8))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetU1();
        return S_OK;
    }

    HRESULT GetI2(signed __int16 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(signed __int16))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetI2();
        return S_OK;
    }
    HRESULT GetU2(unsigned __int16 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(unsigned __int16))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetU2();
        return S_OK;
    }

    HRESULT GetI4(signed __int32 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(signed __int32))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetI4();
        return S_OK;
    }
    HRESULT GetU4(unsigned __int32 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(unsigned __int32))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetU4();
        return S_OK;
    }

    HRESULT GetI8(signed __int64 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(signed __int64))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetI8();
        return S_OK;
    }
    HRESULT GetU8(unsigned __int64 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(unsigned __int64))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetU8();
        return S_OK;
    }

    HRESULT GetR4(float *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(float))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetR4();
        return S_OK;
    }
    HRESULT GetR8(double *pVal)
    {
        WRAPPER_NO_CONTRACT;

        if (BytesLeft() < (int) sizeof(double))
            return META_E_CA_INVALID_BLOB;
        *pVal = GetR8();
        return S_OK;
    }

    HRESULT GetProlog(unsigned __int16 *pVal)
    {
        WRAPPER_NO_CONTRACT;

        m_pbCur = m_pbBlob;

        if (BytesLeft() < (int)(sizeof(BYTE) * 2))
            return META_E_CA_INVALID_BLOB;

        return GetU2(pVal);
    }

    // Added for compatibility with anyone that may emit
    // blobs where the prolog is the only incorrect data.
    HRESULT SkipProlog()
    {
        unsigned __int16 val;
        return GetProlog(&val);
    }

    HRESULT ValidateProlog()
    {
        HRESULT hr;
        unsigned __int16 val;
        IfFailRet(GetProlog(&val));
        
        if (val != 0x0001)
            return META_E_CA_INVALID_BLOB;

        return hr;
    }

    //
    // IMPORTANT: the returned string is typically not null-terminated.
    //
    // This can return any of three distinct valid results:
    //   - NULL string, indicated by *pszString==NULL, *pcbString==0
    //   - empty string, indicated by *pszString!=NULL, *pcbString==0
    //   - non-empty string, indicated by *pdzString!=NULL, *pcbString!=0
    //  If you expect non-null or non-empty strings in your usage scenario,
    //  call the GetNonNullString and GetNonEmptyString helpers below.
    //
    HRESULT GetString(LPCUTF8 *pszString, ULONG *pcbString)
    {
        STATIC_CONTRACT_NOTHROW;
        STATIC_CONTRACT_FORBID_FAULT;

        HRESULT hr;

        if (BytesLeft() == 0)
        {   // Need to check for NULL string sentinal (see below),
            // so need to have at least one byte to read.
            IfFailRet(META_E_CA_INVALID_BLOB);
        }

        if (*m_pbCur == 0xFF)
        {   // 0xFF indicates the NULL string, which is semantically
            // different than the empty string.
            *pszString = NULL;
            *pcbString = 0;
            m_pbCur++;
            return S_OK;
        }

        // Get the length, pointer to data following the length.
        return GetData((BYTE const **)pszString, pcbString);
    }

    //
    // This can return any of two distinct valid results:
    //   - empty string, indicated by *pszString!=NULL, *pcbString==0
    //   - non-empty string, indicated by *pszString!=NULL, *pcbString!=0
    //  If you expect non-null or non-empty strings in your usage scenario,
    //  call the GetNonNullString and GetNonEmptyString helpers below.
    //
    HRESULT GetNonNullString(LPCUTF8 *pszString, ULONG *pcbString)
    {
        STATIC_CONTRACT_NOTHROW;
        STATIC_CONTRACT_FORBID_FAULT;

        HRESULT hr;

        IfFailRet(GetString(pszString, pcbString));

        if (*pszString == NULL)
        {
            return META_E_CA_INVALID_BLOB;
        }

        return S_OK;
    }

    //
    // This function will only return success if the string is valid,
    // non-NULL and non-empty; i.e., *pszString!=NULL, *pcbString!=0
    //
    HRESULT GetNonEmptyString(LPCUTF8 *pszString, ULONG *pcbString)
    {
        STATIC_CONTRACT_NOTHROW;
        STATIC_CONTRACT_FORBID_FAULT;

        HRESULT hr;

        IfFailRet(GetNonNullString(pszString, pcbString));

        if (*pcbString == 0)
        {
            return META_E_CA_INVALID_BLOB;
        }

        return S_OK;
    }

    // IMPORTANT: do not use with string fetching - use GetString instead.
    HRESULT GetData(BYTE const **ppbData, ULONG *pcbData)
    {
        HRESULT hr;
        IfFailRet(CPackedLen::SafeGetData(m_pbCur, m_pbBlob + m_cbBlob, pcbData, ppbData));
        // Move past the data we just recovered
        m_pbCur = *ppbData + *pcbData;

        return S_OK;
    }

    // IMPORTANT: do not use with string fetching - use GetString instead.
    HRESULT GetPackedValue(ULONG *pcbData)
    {
        return CPackedLen::SafeGetLength(m_pbCur, m_pbBlob + m_cbBlob, pcbData, &m_pbCur);
    }

    int BytesLeft()
    {
        LIMITED_METHOD_CONTRACT; 
        return (int)(m_cbBlob - (m_pbCur - m_pbBlob));
    }

private:
    const BYTE  *m_pbCur;
    const BYTE  *m_pbBlob;
    ULONG       m_cbBlob;
};

#endif // __CAPARSER_H__