(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.PEToolkit / metadata / TablesHeap.cs
blob5ae11b60df1103a63d9968f065227ff536f6a869
1 /*
2 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
3 */
5 using System;
6 using System.IO;
7 using System.Collections;
9 namespace Mono.PEToolkit.Metadata {
11 /// <summary>
12 /// Metadata tables heap (#~).
13 /// </summary>
14 /// <remarks>
15 /// Partition II; Chapter 21 & 23.1.6
16 /// </remarks>
17 public class TablesHeap : TablesHeapBase {
19 private long valid; // bitvector of valid tables
20 //(64-bit, max index = TableId.MAX)
21 private int numTabs; // number of tables (calculated from valid)
23 private long sorted; // bitvector of sorted tables (64-bit)
25 // schema version (currently 1.0)
26 private byte verMaj;
27 private byte verMin;
29 // bitvector for heap-size flags:
30 // bit 1 - if set #Strings heap uses wide indices (dword)
31 // bit 2 - if set #GUID heap uses wide indices
32 // bit 3 - if set #Blob heap uses wide indices
33 // otherwise (particular bit is not set) index size is word.
34 private byte heapSizes;
37 private Hashtable tables;
40 internal TablesHeap (MDStream stream) : base (stream)
45 /// <summary>
46 /// Gets or sets bitvector of valid tables (64-bit).
47 /// </summary>
48 public override long Valid {
49 get {
50 return valid;
52 set {
53 valid = value;
57 /// <summary>
58 /// Gets or sets bitvector of sorted tables (64-bit).
59 /// </summary>
60 public override long Sorted {
61 get {
62 return sorted;
64 set {
65 sorted = value;
71 // "Universal" accessors for Valid and Sorted bitvectors.
75 public bool IsValid (TableId tab)
77 return (valid & (1L << (int) tab)) != 0;
80 public void SetValid (TableId tab, bool b)
82 long mask = 1L << (int) tab;
83 if (b) {
84 valid |= mask;
85 } else {
86 valid &= ~mask;
91 /// <summary>
92 /// True if the given table in this heap is sorted.
93 /// </summary>
94 /// <param name="tab"></param>
95 /// <returns></returns>
96 public bool IsSorted (TableId tab)
98 return (sorted & (1L << (int) tab)) != 0;
101 /// <summary>
102 /// Marks specified table in this heap as sorted or unsorted.
103 /// </summary>
104 /// <param name="tab"></param>
105 /// <param name="b"></param>
106 public void SetSorted (TableId tab, bool b)
108 long mask = 1L << (int) tab;
109 if (b) {
110 sorted |= mask;
111 } else {
112 sorted &= ~mask;
118 public byte HeapSizes {
119 get {
120 return heapSizes;
122 set {
123 heapSizes = value;
127 public int StringsIndexSize {
128 get {
129 return 2 + ((heapSizes & 1) << 1);
133 public int GUIDIndexSize {
134 get {
135 return 2 + (heapSizes & 2);
139 public int BlobIndexSize {
140 get {
141 return 2 + ((heapSizes & 4) >> 1);
147 unsafe override public void FromRawData (byte [] rawData)
149 valid = 0;
150 sorted = 0;
152 if (rawData == null || rawData.Length < 24) {
153 throw new BadMetaDataException ("Invalid header for #~ heap.");
156 verMaj = rawData [4];
157 verMin = rawData [5];
158 heapSizes = rawData [6];
160 valid = LEBitConverter.ToInt64 (rawData, 8);
161 sorted = LEBitConverter.ToInt64 (rawData, 16);
163 // Calc number of tables from valid bitvector.
164 numTabs = 0;
165 for (int i = (int) TableId.Count; --i >= 0;) {
166 numTabs += (int) (valid >> i) & 1;
169 int [] rows = new int [(int) TableId.Count];
170 Array.Clear (rows, 0, rows.Length);
171 int offs = 24; // offset to #~::Rows
172 for (int i = 0; i < numTabs; i++) {
173 int n = -1;
174 int vpos = -1;
175 long v = valid;
176 while (n < i && v != 0) {
177 n += (int) (v & 1L);
178 v >>= 1;
179 vpos++;
181 if (vpos != -1) {
182 rows [vpos] = LEBitConverter.ToInt32 (rawData, offs);
183 offs += sizeof (int);
187 // TODO: this could be called from constructor
188 // This sequence: MDHeap::.ctor -> FromRawData -> RegisterTable
189 // and we are making "this" available here, before the object
190 // is fully constructed. This is bad, fix it somehow.
191 TabsDecoder.DecodePhysicalTables (this, rawData, offs, rows);
196 public void RegisterTable (MDTable tab)
198 if (tables == null) tables = new Hashtable (64);
199 tables [tab.Id] = tab;
202 public MDTable this [TableId id] {
203 get {
204 return tables [id] as MDTable;
208 public ICollection Tables {
209 get {
210 return tables.Values;