FieldMarshal.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /// Marshalling information for a field or param
  26. /// </summary>
  27. public class FieldMarshal : MetaDataElement
  28. {
  29. MetaDataElement field;
  30. NativeType nt;
  31. uint ntIx, parentIx;
  32. /*-------------------- Added by Carlo Kok ---------------------------------*/
  33. private SafeArrayType safeArraySubType;
  34. public SafeArrayType SafeArraySubType { get { return safeArraySubType; ; } set { safeArraySubType = value; } }
  35. private string safeArrayUserDefinedSubType;
  36. public string SafeArrayUserDefinedSubType { get { return safeArrayUserDefinedSubType; } set { safeArrayUserDefinedSubType = value; } }
  37. private NativeTypeIx arraySubType = (NativeTypeIx)0x50; // default, important
  38. public NativeTypeIx ArraySubType { get { return arraySubType; } set { arraySubType = value; } }
  39. private int sizeConst = -1;
  40. public int SizeConst { get { return sizeConst; } set { sizeConst = value; } }
  41. private int sizeParamIndex = -1;
  42. public int SizeParamIndex { get { return sizeParamIndex; } set { sizeParamIndex = value; } }
  43. private string customMarshallingType;
  44. public string CustomMarshallingType { get { return customMarshallingType; } set { customMarshallingType = value; } }
  45. private string customMarshallingCookie;
  46. public string CustomMarshallingCookie { get { return customMarshallingCookie; } set { customMarshallingCookie = value; } }
  47. /*-------------------- Constructors ---------------------------------*/
  48. internal FieldMarshal(MetaDataElement field, NativeType nType)
  49. {
  50. this.field = field;
  51. this.nt = nType;
  52. sortTable = true;
  53. tabIx = MDTable.FieldMarshal;
  54. }
  55. internal FieldMarshal(PEReader buff)
  56. {
  57. parentIx = buff.GetCodedIndex(CIx.HasFieldMarshal);
  58. ntIx = buff.GetBlobIx();
  59. sortTable = true;
  60. tabIx = MDTable.FieldMarshal;
  61. }
  62. internal static void Read(PEReader buff, TableRow[] fMarshal)
  63. {
  64. for (int i = 0; i < fMarshal.Length; i++)
  65. fMarshal[i] = new FieldMarshal(buff);
  66. }
  67. internal override void Resolve(PEReader buff)
  68. {
  69. field = buff.GetCodedElement(CIx.HasFieldMarshal, parentIx);
  70. nt = buff.GetBlobNativeType(ntIx);
  71. if (field is FieldDef)
  72. {
  73. ((FieldDef)field).SetMarshalType(nt);
  74. }
  75. else
  76. {
  77. ((Param)field).SetMarshalType(nt);
  78. }
  79. }
  80. internal override uint SortKey()
  81. {
  82. return (field.Row << MetaData.CIxShiftMap[(uint)CIx.HasFieldMarshal])
  83. | field.GetCodedIx(CIx.HasFieldMarshal);
  84. }
  85. internal sealed override void BuildTables(MetaDataOut md)
  86. {
  87. md.AddToTable(MDTable.FieldMarshal, this);
  88. ntIx = md.AddToBlobHeap(nt.ToBlob());
  89. }
  90. internal static uint Size(MetaData md)
  91. {
  92. return md.CodedIndexSize(CIx.HasFieldMarshal) + md.BlobIndexSize();
  93. }
  94. internal sealed override void Write(PEWriter output)
  95. {
  96. output.WriteCodedIndex(CIx.HasFieldMarshal, field);
  97. output.BlobIndex(ntIx);
  98. }
  99. }
  100. }