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.
7 #include "sigbuilder.h"
10 void SigBuilder::AppendByte(BYTE b)
15 m_pBuffer[m_dwLength++] = b;
18 void SigBuilder::AppendData(ULONG data)
23 // Inlined logic from CorSigCompressData
29 m_pBuffer[m_dwLength++] = BYTE(data);
37 DWORD dwLength = m_dwLength;
38 BYTE * pBuffer = m_pBuffer;
40 pBuffer[dwLength] = BYTE((data >> 8) | 0x80);
41 pBuffer[dwLength+1] = BYTE(data);
43 m_dwLength = dwLength + 2;
47 if (data <= 0x1FFFFFFF)
51 DWORD dwLength = m_dwLength;
52 BYTE * pBuffer = m_pBuffer;
54 pBuffer[dwLength] = BYTE((data >> 24) | 0xC0);
55 pBuffer[dwLength+1] = BYTE(data >> 16);
56 pBuffer[dwLength+2] = BYTE(data >> 8);
57 pBuffer[dwLength+3] = BYTE(data);
59 m_dwLength = dwLength + 4;
63 // We currently can only represent to 0x1FFFFFFF.
64 ThrowHR(COR_E_OVERFLOW);
67 void SigBuilder::AppendToken(mdToken tk)
72 // Inlined logic from CorSigCompressToken
75 RID rid = RidFromToken(tk);
76 ULONG32 ulTyp = TypeFromToken(tk);
78 _ASSERTE(rid <= 0x3FFFFFF);
81 // TypeDef is encoded with low bits 00
82 // TypeRef is encoded with low bits 01
83 // TypeSpec is encoded with low bits 10
84 // BaseType is encoded with low bit 11
86 if (ulTyp == g_tkCorEncodeToken[0])
88 // make the last two bits 00
91 else if (ulTyp == g_tkCorEncodeToken[1])
93 // make the last two bits 01
96 else if (ulTyp == g_tkCorEncodeToken[2])
98 // make last two bits 0
101 else if (ulTyp == g_tkCorEncodeToken[3])
107 ThrowHR(COR_E_BADIMAGEFORMAT);
113 void SigBuilder::AppendBlob(const PVOID pBlob, SIZE_T cbBlob)
115 STANDARD_VM_CONTRACT;
118 memcpy(m_pBuffer + m_dwLength, pBlob, cbBlob);
119 m_dwLength += (DWORD)cbBlob;
122 void SigBuilder::Grow(SIZE_T cbMin)
124 STANDARD_VM_CONTRACT;
126 DWORD dwNewAllocation = max(m_dwLength + (DWORD)cbMin, 2 * m_dwAllocation);
129 if (dwNewAllocation < m_dwLength || (dwNewAllocation - m_dwLength) < cbMin)
132 BYTE * pNewAllocation = new BYTE[dwNewAllocation];
133 memcpy(pNewAllocation, m_pBuffer, m_dwLength);
135 BYTE * pOldAllocation = m_pBuffer;
137 m_pBuffer = pNewAllocation;
138 m_dwAllocation = dwNewAllocation;
140 if (pOldAllocation != m_prealloc)
141 delete [] pOldAllocation;
144 SigBuilder::~SigBuilder()
146 if (m_pBuffer != m_prealloc)
150 SigBuilder::SigBuilder(DWORD cbPreallocationSize)
152 STANDARD_VM_CONTRACT;
155 if (cbPreallocationSize <= sizeof(m_prealloc))
157 m_pBuffer = m_prealloc;
158 m_dwAllocation = sizeof(m_prealloc);
162 m_pBuffer = new BYTE[cbPreallocationSize];
163 m_dwAllocation = cbPreallocationSize;