ClassLayout.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * PERWAPI - An API for Reading and Writing PE Files
  3. *
  4. * Copyright (c) Diane Corney, Queensland University of Technology, 2004.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the PERWAPI Copyright as included with this
  8. * distribution in the file PERWAPIcopyright.rtf.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY as is explained in the copyright notice.
  12. *
  13. * The author may be contacted at d.corney@qut.edu.au
  14. *
  15. * Version Date: 26/01/07
  16. */
  17. using System;
  18. using System.IO;
  19. using System.Collections;
  20. using System.Security.Cryptography;
  21. namespace QUT.PERWAPI
  22. {
  23. /**************************************************************************/
  24. /// <summary>
  25. /// Layout information for a class (.class [sequential | explicit])
  26. /// </summary>
  27. internal class ClassLayout : MetaDataElement
  28. {
  29. ClassDef parent;
  30. ushort packSize = 0;
  31. uint classSize = 0;
  32. uint parentIx = 0;
  33. /*-------------------- Constructors ---------------------------------*/
  34. internal ClassLayout(int pack, int cSize, ClassDef par)
  35. {
  36. packSize = (ushort)pack;
  37. classSize = (uint)cSize;
  38. parent = par;
  39. tabIx = MDTable.ClassLayout;
  40. }
  41. internal ClassLayout(ushort pack, uint cSize, ClassDef par)
  42. {
  43. packSize = pack;
  44. classSize = cSize;
  45. parent = par;
  46. tabIx = MDTable.ClassLayout;
  47. }
  48. internal ClassLayout(PEReader buff)
  49. {
  50. packSize = buff.ReadUInt16();
  51. classSize = buff.ReadUInt32();
  52. parentIx = buff.GetIndex(MDTable.TypeDef);
  53. tabIx = MDTable.ClassLayout;
  54. }
  55. internal static ClassLayout FindLayout(PEReader buff, ClassDef paren, uint classIx)
  56. {
  57. buff.SetElementPosition(MDTable.ClassLayout, 0);
  58. for (int i = 0; i < buff.GetTableSize(MDTable.ClassLayout); i++)
  59. {
  60. ushort packSize = buff.ReadUInt16();
  61. uint classSize = buff.ReadUInt32();
  62. if (buff.GetIndex(MDTable.TypeDef) == classIx)
  63. return new ClassLayout(packSize, classSize, paren);
  64. }
  65. return null;
  66. }
  67. internal static void Read(PEReader buff, TableRow[] layouts)
  68. {
  69. for (int i = 0; i < layouts.Length; i++)
  70. {
  71. layouts[i] = new ClassLayout(buff);
  72. }
  73. }
  74. internal override void Resolve(PEReader buff)
  75. {
  76. parent = (ClassDef)buff.GetElement(MDTable.TypeDef, parentIx);
  77. if (parent != null) parent.Layout = this;
  78. }
  79. /*------------------------- public set and get methods --------------------------*/
  80. public void SetPack(int pack) { packSize = (ushort)pack; }
  81. public int GetPack() { return (int)packSize; }
  82. public void SetSize(int size) { classSize = (uint)size; }
  83. public int GetSize() { return (int)classSize; }
  84. /*----------------------------- internal functions ------------------------------*/
  85. internal sealed override void BuildTables(MetaDataOut md)
  86. {
  87. md.AddToTable(tabIx, this);
  88. }
  89. internal static uint Size(MetaData md)
  90. {
  91. return 6 + md.TableIndexSize(MDTable.TypeDef);
  92. }
  93. internal sealed override void Write(PEWriter output)
  94. {
  95. output.Write(packSize);
  96. output.Write(classSize);
  97. output.WriteIndex(MDTable.TypeDef, parent.Row);
  98. }
  99. }
  100. }