-
Notifications
You must be signed in to change notification settings - Fork 125
/
MetadataTable.cs
304 lines (256 loc) · 9.83 KB
/
MetadataTable.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using AsmResolver.Collections;
using AsmResolver.IO;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
namespace AsmResolver.PE.DotNet.Metadata.Tables
{
// TODO: Implement a more granular lazy initialization.
/// <summary>
/// Provides a base implementation of a metadata table in the table stream of a managed executable file.
/// </summary>
/// <typeparam name="TRow">The type of rows that this table stores.</typeparam>
public class MetadataTable<TRow> : IMetadataTable, ICollection<TRow>
where TRow : struct, IMetadataRow
{
private RefList<TRow>? _items;
/// <summary>
/// Creates a new metadata table using the provided layout.
/// </summary>
/// <param name="tableIndex">The index of the table.</param>
/// <param name="layout">The layout of the table.</param>
public MetadataTable(TableIndex tableIndex, TableLayout layout)
: this(tableIndex, layout, false)
{
}
/// <summary>
/// Creates a new metadata table using the provided layout.
/// </summary>
/// <param name="tableIndex">The index of the table.</param>
/// <param name="layout">The layout of the table.</param>
/// <param name="isSorted">Indicates the table is sorted or not.</param>
public MetadataTable(TableIndex tableIndex, TableLayout layout, bool isSorted)
{
TableIndex = tableIndex;
Layout = layout;
IsSorted = isSorted;
}
/// <summary>
/// Gets the index of the table in the tables stream.
/// </summary>
public TableIndex TableIndex
{
get;
}
/// <inheritdoc />
public TableLayout Layout
{
get;
private set;
}
/// <inheritdoc />
public IndexSize IndexSize => Count > 0xFFFF ? IndexSize.Long : IndexSize.Short;
/// <inheritdoc cref="IMetadataTable" />
public TRow this[int index]
{
get => Rows[index];
set => Rows[index] = value;
}
/// <inheritdoc />
public bool IsSorted
{
get;
set;
}
/// <inheritdoc />
IMetadataRow IMetadataTable.this[int index]
{
get => this[index];
set => this[index] = (TRow) value;
}
/// <summary>
/// Gets the version number of the list.
/// </summary>
public int Version => Rows.Version;
/// <summary>
/// Gets the internal list of rows that are stored in the metadata table.
/// </summary>
protected RefList<TRow> Rows
{
get
{
if (_items is null)
Interlocked.CompareExchange(ref _items, GetRows(), null);
return _items;
}
}
/// <summary>
/// Gets a value indicating the <see cref="Rows"/> property is initialized or not.
/// </summary>
protected bool IsInitialized => _items != null;
/// <inheritdoc cref="ICollection{T}.Count" />
public virtual int Count => Rows.Count;
/// <summary>
/// Gets or sets the total number of rows that the underlying array can store.
/// </summary>
public int Capacity
{
get => Rows.Capacity;
set => Rows.Capacity = value;
}
/// <inheritdoc />
public bool IsReadOnly => false; // TODO: it might be necessary later to make this configurable.
/// <inheritdoc />
public object SyncRoot => this;
/// <inheritdoc />
public bool IsSynchronized => false;
/// <summary>
/// Gets a mutable reference to a row within the table.
/// </summary>
/// <param name="rid">The RID of the row to obtain a reference for.</param>
/// <returns>The row reference.</returns>
public ref TRow GetRowRef(uint rid) => ref Rows.GetElementRef((int) (rid - 1));
/// <summary>
/// Gets a mutable reference to a row within the table.
/// </summary>
/// <param name="rid">The RID of the row to obtain a reference for.</param>
/// <param name="version">The version of the underlying buffer upon obtaining the reference.</param>
/// <returns>The row reference.</returns>
public ref TRow GetRowRef(uint rid, out int version) => ref Rows.GetElementRef((int) (rid - 1), out version);
/// <inheritdoc />
public void Add(TRow item) => Rows.Add(item);
/// <inheritdoc />
public void Clear() => Rows.Clear();
/// <inheritdoc />
public bool Contains(TRow item) => Rows.Contains(item);
/// <inheritdoc />
public void CopyTo(TRow[] array, int arrayIndex) => Rows.CopyTo(array, arrayIndex);
/// <inheritdoc />
void ICollection.CopyTo(Array array, int index) => ((ICollection) Rows).CopyTo(array, index);
/// <inheritdoc />
public bool Remove(TRow item) => Rows.Remove(item);
/// <inheritdoc />
public IEnumerator<TRow> GetEnumerator() => Rows.GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Obtains all rows in the metadata table.
/// </summary>
/// <returns>The rows.</returns>
/// <remarks>
/// This method is called upon initialization of the <see cref="Rows"/> property.
/// </remarks>
protected virtual RefList<TRow> GetRows() => new();
/// <summary>
/// Gets the contents of a row by its row identifier.
/// </summary>
/// <param name="rid">The row identifier.</param>
/// <returns>The row.</returns>
public TRow GetByRid(uint rid) => this[(int) (rid - 1)];
/// <inheritdoc />
public bool TryGetCell(uint rid, int column, out uint value)
{
if (column >= 0 && column < Layout.Columns.Count && TryGetByRid(rid, out var row))
{
value = row[column];
return true;
}
value = 0;
return false;
}
IMetadataRow IMetadataTable.GetByRid(uint rid) => GetByRid(rid);
/// <summary>
/// Attempts to get the contents of a row by its row identifier.
/// </summary>
/// <param name="rid">The row identifier.</param>
/// <param name="row">When successful, the read row.</param>
/// <returns><c>true</c> if the RID existed an the row was obtained successfully, <c>false</c> otherwise.</returns>
public bool TryGetByRid(uint rid, out TRow row)
{
if (rid >= 1 && rid <= Count)
{
row = GetByRid(rid);
return true;
}
row = default;
return false;
}
bool IMetadataTable.TryGetByRid(uint rid, out IMetadataRow row)
{
bool result = TryGetByRid(rid, out var r);
row = r;
return result;
}
/// <summary>
/// Gets a single row in a table by a key. This requires the table to be sorted.
/// </summary>
/// <param name="keyColumnIndex">The column number to get the key from.</param>
/// <param name="key">The key to search.</param>
/// <param name="row">When this functions returns <c>true</c>, this parameter contains the first row that
/// contains the given key.</param>
/// <returns><c>true</c> if the row was found, <c>false</c> otherwise.</returns>
public bool TryGetRowByKey(int keyColumnIndex, uint key, out TRow row)
{
row = default;
if (Count == 0)
return false;
int left = 0;
int right = Count - 1;
while (left <= right)
{
int m = (left + right) / 2;
var currentRow = Rows[m];
uint currentKey = currentRow[keyColumnIndex];
if (currentKey > key)
{
right = m - 1;
}
else if (currentKey < key)
{
left = m + 1;
}
else
{
row = currentRow;
return true;
}
}
return false;
}
/// <inheritdoc />
bool IMetadataTable.TryGetRowByKey(int keyColumnIndex, uint key, out IMetadataRow row)
{
bool result = TryGetRowByKey(keyColumnIndex, key, out var r);
row = r;
return result;
}
/// <summary>
/// Sets the contents of a row by its row identifier.
/// </summary>
/// <param name="rid">The row identifier.</param>
/// <param name="row">The new contents of the row.</param>
public void SetByRid(uint rid, TRow row) => this[(int) (rid - 1)] = row;
void IMetadataTable.SetByRid(uint rid, IMetadataRow row) => SetByRid(rid, (TRow) row);
/// <inheritdoc />
public virtual void UpdateTableLayout(TableLayout layout)
{
for (int i = 0; i < Layout.Columns.Count; i++)
{
if (Layout.Columns[i].Name != layout.Columns[i].Name
|| Layout.Columns[i].Type != layout.Columns[i].Type)
{
throw new ArgumentException("New table layout does not match the original one.");
}
}
Layout = layout;
}
/// <inheritdoc />
public void Write(IBinaryStreamWriter writer)
{
for (int i = 0; i < Rows.Count; i++)
Rows[i].Write(writer, Layout);
}
}
}