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// LiteWeightStgdb.h
6//
7
8//
9// This contains definition of class CLiteWeightStgDB. This is light weight
10// read-only implementation for accessing compressed meta data format.
11//
12//*****************************************************************************
13#ifndef __LiteWeightStgdb_h__
14#define __LiteWeightStgdb_h__
15
16#include "metamodelro.h"
17#include "metamodelrw.h"
18
19#include "stgtiggerstorage.h"
20
21class StgIO;
22
23#include "mdcommon.h"
24
25#ifdef _PREFAST_
26#pragma warning(push)
27#pragma warning(disable:28718) // public header missing SAL annotations
28#endif // _PREFAST_
29class TiggerStorage;
30#ifdef _PREFAST_
31#pragma warning(pop)
32#endif // _PREFAST_
33
34//*****************************************************************************
35// This class provides common definitions for heap segments. It is both the
36// base class for the heap, and the class for heap extensions (additional
37// memory that must be allocated to grow the heap).
38//*****************************************************************************
39template <class MiniMd>
40class CLiteWeightStgdb
41{
42 friend class VerifyLayoutsMD;
43public:
44 CLiteWeightStgdb() : m_pvMd(NULL), m_cbMd(0)
45 {}
46
47 ~CLiteWeightStgdb()
48 { Uninit(); }
49
50 // open an in-memory metadata section for read.
51 __checkReturn
52 HRESULT InitOnMem(
53 ULONG cbData,
54 LPCVOID pbData);
55
56 __checkReturn
57 HRESULT InitHotPools(DataBuffer hotMetaData);
58
59 void Uninit();
60
61protected:
62 MiniMd m_MiniMd; // embedded compress meta data schemas definition
63 const void *m_pvMd; // Pointer to meta data.
64 ULONG m_cbMd; // Size of the meta data.
65
66 friend class CorMetaDataScope;
67 friend class COR;
68 friend class RegMeta;
69 friend class MDInternalRO;
70 friend class MDInternalRW;
71};
72
73//*****************************************************************************
74// Open an in-memory metadata section for read
75//*****************************************************************************
76template <class MiniMd>
77void CLiteWeightStgdb<MiniMd>::Uninit()
78{
79 m_MiniMd.m_StringHeap.Delete();
80 m_MiniMd.m_UserStringHeap.Delete();
81 m_MiniMd.m_GuidHeap.Delete();
82 m_MiniMd.m_BlobHeap.Delete();
83 m_pvMd = NULL;
84 m_cbMd = 0;
85}
86
87class CLiteWeightStgdbRW : public CLiteWeightStgdb<CMiniMdRW>
88{
89 friend class CImportTlb;
90 friend class RegMeta;
91 friend class VerifyLayoutsMD;
92 friend HRESULT TranslateSigHelper(
93 IMDInternalImport* pImport,
94 IMDInternalImport* pAssemImport,
95 const void* pbHashValue,
96 ULONG cbHashValue,
97 PCCOR_SIGNATURE pbSigBlob,
98 ULONG cbSigBlob,
99 IMetaDataAssemblyEmit* pAssemEmit,
100 IMetaDataEmit* emit,
101 CQuickBytes* pqkSigEmit,
102 ULONG* pcbSig);
103public:
104 CLiteWeightStgdbRW() : m_cbSaveSize(0), m_pStreamList(0), m_pNextStgdb(NULL), m_pStgIO(NULL)
105 {
106 m_wszFileName = NULL;
107 m_pImage = NULL;
108 m_dwImageSize = 0;
109 m_dwPEKind = (DWORD)(-1);
110 m_dwDatabaseLFS = 0;
111 m_dwDatabaseLFT = 0;
112 }
113 ~CLiteWeightStgdbRW();
114
115 __checkReturn
116 HRESULT InitNew();
117
118 // open an in-memory metadata section for read.
119 __checkReturn
120 HRESULT InitOnMem(
121 ULONG cbData,
122 LPCVOID pbData,
123 int bReadOnly);
124
125 __checkReturn
126 HRESULT GetSaveSize(
127 CorSaveSize fSize,
128 UINT32 *pcbSaveSize,
129 MetaDataReorderingOptions reorderingOptions = NoReordering,
130 CorProfileData *pProfileData = NULL); // optional IBC profile data for working set optimization
131
132 __checkReturn
133 HRESULT SaveToStream(
134 IStream *pIStream, // Stream to which to write
135 MetaDataReorderingOptions reorderingOptions = NoReordering,
136 CorProfileData *pProfileData = NULL); // optional IBC profile data for working set optimization
137
138 __checkReturn
139 HRESULT Save(
140 LPCWSTR szFile,
141 DWORD dwSaveFlags);
142
143 // Open a metadata section for read/write
144 __checkReturn
145 HRESULT OpenForRead(
146 LPCWSTR szDatabase, // Name of database.
147 void *pbData, // Data to open on top of, 0 default.
148 ULONG cbData, // How big is the data.
149 DWORD dwFlags); // Flags for the open.
150
151#ifdef FEATURE_METADATA_CUSTOM_DATA_SOURCE
152 // Open a metadata section for read/write
153 __checkReturn
154 HRESULT OpenForRead(
155 IMDCustomDataSource *pDataSource, // data to open on top of
156 DWORD dwFlags); // Flags for the open.
157#endif
158
159 __checkReturn
160 HRESULT FindImageMetaData(
161 PVOID pImage, // Pointer to head of a file
162 DWORD dwFileLength, // length of a flat file
163 BOOL bMappedImage, // Is the file mapped
164 PVOID *ppMetaData, // [out] pointer to the metadata
165 ULONG *pcbMetaData); // [out] size of the metadata
166
167 __checkReturn
168 HRESULT FindObjMetaData(
169 PVOID pImage, // Pointer to an OBJ file
170 DWORD dwFileLength, // Length of the file
171 PVOID *ppMetaData, // [out] pointer to the metadata
172 ULONG *pcbMetaData); // [out] size of the metadata
173
174 __checkReturn
175 HRESULT GetPEKind( // S_OK or error.
176 MAPPINGTYPE mtMapping, // The type of mapping the image has
177 DWORD* pdwPEKind, // [OUT] The kind of PE (0 - not a PE)
178 DWORD* pdwMachine); // [OUT] Machine as defined in NT header
179
180 // Low level data access; not useful for most clients.
181 __checkReturn
182 HRESULT GetRawData(
183 const void **ppvMd, // [OUT] put pointer to MD section here (aka, 'BSJB').
184 ULONG *pcbMd); // [OUT] put size of the stream here.
185
186 __checkReturn
187 STDMETHODIMP GetRawStreamInfo( // Get info about the MD stream.
188 ULONG ix, // [IN] Stream ordinal desired.
189 const char **ppchName, // [OUT] put pointer to stream name here.
190 const void **ppv, // [OUT] put pointer to MD stream here.
191 ULONG *pcb); // [OUT] put size of the stream here.
192
193 UINT32 m_cbSaveSize; // Size of the saved streams.
194 int m_bSaveCompressed; // If true, save as compressed stream (#-, not #~)
195 VOID* m_pImage; // Set in OpenForRead, NULL for anything but PE files
196 DWORD m_dwImageSize; // On-disk size of image
197
198protected:
199 DWORD m_dwPEKind; // The kind of PE - 0: not a PE.
200 DWORD m_dwMachine; // Machine as defined in NT header.
201
202 __checkReturn
203 HRESULT GetPoolSaveSize(
204 LPCWSTR szHeap, // Name of the heap stream.
205 int iPool, // The pool whose size to get.
206 UINT32 *pcbSaveSize); // Add pool data to this value.
207
208 __checkReturn
209 HRESULT GetTablesSaveSize(
210 CorSaveSize fSave,
211 UINT32 *pcbSaveSize,
212 MetaDataReorderingOptions reorderingOptions,
213 CorProfileData *pProfileData = NULL); // Add pool data to this value.
214
215 __checkReturn
216 HRESULT AddStreamToList(
217 UINT32 cbSize, // Size of the stream data.
218 LPCWSTR szName); // Name of the stream.
219
220 __checkReturn
221 HRESULT SaveToStorage(
222 TiggerStorage *pStorage,
223 MetaDataReorderingOptions reorderingOptions = NoReordering,
224 CorProfileData *pProfileData = NULL);
225
226 __checkReturn
227 HRESULT SavePool(LPCWSTR szName, TiggerStorage *pStorage, int iPool);
228
229 STORAGESTREAMLST *m_pStreamList;
230
231 __checkReturn
232 HRESULT InitFileForRead(
233 StgIO *pStgIO, // For file i/o.
234 int bReadOnly=true); // If read-only.
235
236 // Set file name of this database (makes copy of the file name).
237 __checkReturn HRESULT SetFileName(const WCHAR * wszFileName);
238 // Returns TRUE if wszFileName has valid file name length.
239 static BOOL IsValidFileNameLength(const WCHAR * wszFileName);
240
241 CLiteWeightStgdbRW *m_pNextStgdb;
242
243public:
244 FORCEINLINE FILETYPE GetFileType() { return m_eFileType; }
245
246private:
247 FILETYPE m_eFileType;
248 WCHAR * m_wszFileName; // Database file name (NULL or non-empty string)
249 DWORD m_dwDatabaseLFT; // Low bytes of the database file's last write time
250 DWORD m_dwDatabaseLFS; // Low bytes of the database file's size
251 StgIO * m_pStgIO; // For file i/o.
252
253}; // class CLiteWeightStgdbRW
254
255#endif // __LiteWeightStgdb_h__
256