summaryrefslogtreecommitdiff
path: root/src/inc/sigbuilder.h
blob: 6d7cbf27bae6b3cad174f7e9539e60417ac7b317 (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
// 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.


#ifndef _SIGBUILDER_H_
#define _SIGBUILDER_H_

#include "contract.h"

//
// Simple signature builder
//

class SigBuilder
{
    PBYTE m_pBuffer;
    DWORD m_dwLength;
    DWORD m_dwAllocation;

    // Preallocate space for small signatures
    BYTE m_prealloc[64];

    // Grow the buffer to get at least cbMin of free space
    void Grow(SIZE_T cbMin);

    // Ensure that the buffer has at least cbMin of free space
    FORCEINLINE void Ensure(SIZE_T cb)
    {
        if (m_dwAllocation - m_dwLength < cb)
            Grow(cb);
    }

public:
    SigBuilder()
        : m_pBuffer(m_prealloc), m_dwLength(0), m_dwAllocation(sizeof(m_prealloc))
    {
        LIMITED_METHOD_CONTRACT;
    }

    ~SigBuilder();
    
    SigBuilder(DWORD cbPreallocationSize);
    
    PVOID GetSignature(DWORD * pdwLength)
    {
        LIMITED_METHOD_CONTRACT;
        *pdwLength = m_dwLength;
        return m_pBuffer;
    }

    DWORD GetSignatureLength()
    {
        LIMITED_METHOD_CONTRACT;
        return m_dwLength;
    }

    void AppendByte(BYTE b);

    void AppendData(ULONG data);

    void AppendElementType(CorElementType etype)
    {
        WRAPPER_NO_CONTRACT;
        AppendByte(static_cast<BYTE>(etype));
    }

    void AppendToken(mdToken tk);

    void AppendPointer(void * ptr)
    {
        WRAPPER_NO_CONTRACT;
        AppendBlob(&ptr, sizeof(ptr));
    }

    void AppendBlob(const PVOID pBlob, SIZE_T cbBlob);
};

#endif // _SIGBUILDER_H_