TablesHeap.cs :  » Aspect-Oriented-Frameworks » Runtime-Assembly-Instrumentation-Library » Mono » PEToolkit » Metadata » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Aspect Oriented Frameworks » Runtime Assembly Instrumentation Library 
Runtime Assembly Instrumentation Library » Mono » PEToolkit » Metadata » TablesHeap.cs
/*
 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
 */

using System;
using System.IO;
using System.Collections;

namespace Mono.PEToolkit.Metadata{

  /// <summary>
  /// Metadata tables heap (#~).
  /// </summary>
  /// <remarks>
  /// Partition II; Chapter 21 & 23.1.6
  /// </remarks>
  public class TablesHeap : TablesHeapBase {

    private long valid;  // bitvector of valid tables
                         //(64-bit, max index = TableId.MAX)
    private int numTabs; // number of tables (calculated from valid)

    private long sorted; // bitvector of sorted tables (64-bit)

    // schema version (currently 1.0)
    private byte verMaj;
    private byte verMin;

    // bitvector for heap-size flags:
    // bit 1 - if set #Strings heap uses wide indices (dword)
    // bit 2 - if set #GUID heap uses wide indices
    // bit 3 - if set #Blob heap uses wide indices
    // otherwise (particular bit is not set) index size is word.
    private byte heapSizes;


    private Hashtable tables;


    internal TablesHeap (MDStream stream) : base (stream)
    {
    }


    /// <summary>
    /// Gets or sets bitvector of valid tables (64-bit).
    /// </summary>
    public override long Valid {
      get {
        return valid;
      }
      set {
        valid = value;
      }
    }

    /// <summary>
    /// Gets or sets bitvector of sorted tables (64-bit).
    /// </summary>
    public override long Sorted {
      get {
        return sorted;
      }
      set {
        sorted = value;
      }
    }


    //
    // "Universal" accessors for Valid and Sorted bitvectors.
    //


    public bool IsValid (TableId tab)
    {
      return (valid & (1L << (int) tab)) != 0;
    }

    public void SetValid (TableId tab, bool b)
    {
      long mask = 1L << (int) tab;
      if (b) {
        valid |= mask;
      } else {
        valid &= ~mask;
      }
    }


    /// <summary>
    /// True if the given table in this heap is sorted.
    /// </summary>
    /// <param name="tab"></param>
    /// <returns></returns>
    public bool IsSorted (TableId tab)
    {
      return (sorted & (1L << (int) tab)) != 0;
    }

    /// <summary>
    /// Marks specified table in this heap as sorted or unsorted.
    /// </summary>
    /// <param name="tab"></param>
    /// <param name="b"></param>
    public void SetSorted (TableId tab, bool b)
    {
      long mask = 1L << (int) tab;
      if (b) {
        sorted |= mask;
      } else {
        sorted &= ~mask;
      }
    }



    public byte HeapSizes {
      get {
        return heapSizes;
      }
      set {
        heapSizes = value;
      }
    }

    public int StringsIndexSize {
      get {
        return 2 + ((heapSizes & 1) << 1);
      }
    }

    public int GUIDIndexSize {
      get {
        return 2 + (heapSizes & 2);
      }
    }

    public int BlobIndexSize {
      get {
        return 2 + ((heapSizes & 4) >> 1);
      }
    }



    unsafe override public void FromRawData (byte [] rawData)
    {
      valid = 0;
      sorted = 0;

      if (rawData == null || rawData.Length < 24) {
        throw new BadMetaDataException ("Invalid header for #~ heap.");
      }

      verMaj = rawData [4];
      verMin = rawData [5];
      heapSizes = rawData [6];

      valid = LEBitConverter.ToInt64 (rawData, 8);
      sorted = LEBitConverter.ToInt64 (rawData, 16);

      // Calc number of tables from valid bitvector.
      numTabs = 0;
      for (int i = (int) TableId.Count; --i >= 0;) {
        numTabs += (int) (valid >> i) & 1;
      }

      int [] rows = new int [(int) TableId.Count];
      Array.Clear (rows, 0, rows.Length);
      int offs = 24; // offset to #~::Rows
      for (int i = 0; i < numTabs; i++) {
        int n = -1;
        int vpos = -1;
        long v = valid;
        while (n < i && v != 0) {
          n += (int) (v & 1L);
          v >>= 1;
          vpos++;
        }
        if (vpos != -1) {
          rows [vpos] = LEBitConverter.ToInt32 (rawData, offs);
          offs += sizeof (int);
        }
      }

      // TODO: this could be called from constructor
      // This sequence: MDHeap::.ctor -> FromRawData -> RegisterTable
      // and we are making "this" available here, before the object
      // is fully constructed. This is bad, fix it somehow.
      TabsDecoder.DecodePhysicalTables (this, rawData, offs, rows);

    }


    public void RegisterTable (MDTable tab)
    {
      if (tables == null) tables = new Hashtable (64);
      tables [tab.Id] = tab;
    }

    public MDTable this [TableId id] {
      get {
        return tables [id] as MDTable;
      }
    }

    public ICollection Tables {
      get {
        return tables.Values;
      }
    }


  }

}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.