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
6#ifndef _SIGBUILDER_H_
7#define _SIGBUILDER_H_
8
9#include "contract.h"
10
11//
12// Simple signature builder
13//
14
15class SigBuilder
16{
17 PBYTE m_pBuffer;
18 DWORD m_dwLength;
19 DWORD m_dwAllocation;
20
21 // Preallocate space for small signatures
22 BYTE m_prealloc[64];
23
24 // Grow the buffer to get at least cbMin of free space
25 void Grow(SIZE_T cbMin);
26
27 // Ensure that the buffer has at least cbMin of free space
28 FORCEINLINE void Ensure(SIZE_T cb)
29 {
30 if (m_dwAllocation - m_dwLength < cb)
31 Grow(cb);
32 }
33
34public:
35 SigBuilder()
36 : m_pBuffer(m_prealloc), m_dwLength(0), m_dwAllocation(sizeof(m_prealloc))
37 {
38 LIMITED_METHOD_CONTRACT;
39 }
40
41 ~SigBuilder();
42
43 SigBuilder(DWORD cbPreallocationSize);
44
45 PVOID GetSignature(DWORD * pdwLength)
46 {
47 LIMITED_METHOD_CONTRACT;
48 *pdwLength = m_dwLength;
49 return m_pBuffer;
50 }
51
52 DWORD GetSignatureLength()
53 {
54 LIMITED_METHOD_CONTRACT;
55 return m_dwLength;
56 }
57
58 void AppendByte(BYTE b);
59
60 void AppendData(ULONG data);
61
62 void AppendElementType(CorElementType etype)
63 {
64 WRAPPER_NO_CONTRACT;
65 AppendByte(static_cast<BYTE>(etype));
66 }
67
68 void AppendToken(mdToken tk);
69
70 void AppendPointer(void * ptr)
71 {
72 WRAPPER_NO_CONTRACT;
73 AppendBlob(&ptr, sizeof(ptr));
74 }
75
76 void AppendBlob(const PVOID pBlob, SIZE_T cbBlob);
77};
78
79#endif // _SIGBUILDER_H_
80