NativeTypes.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. namespace QUT.PERWAPI
  21. {
  22. /**************************************************************************/
  23. // Descriptors for Native Types for parameter marshalling
  24. /**************************************************************************/
  25. /// <summary>
  26. /// Descriptors for native types used for marshalling
  27. /// </summary>
  28. public class NativeType
  29. {
  30. public static readonly NativeType Void = new NativeType(0x01);
  31. public static readonly NativeType Boolean = new NativeType(0x02);
  32. public static readonly NativeType Int8 = new NativeType(0x03);
  33. public static readonly NativeType UInt8 = new NativeType(0x04);
  34. public static readonly NativeType Int16 = new NativeType(0x05);
  35. public static readonly NativeType UInt16 = new NativeType(0x06);
  36. public static readonly NativeType Int32 = new NativeType(0x07);
  37. public static readonly NativeType UInt32 = new NativeType(0x08);
  38. public static readonly NativeType Int64 = new NativeType(0x09);
  39. public static readonly NativeType UInt64 = new NativeType(0x0A);
  40. public static readonly NativeType Float32 = new NativeType(0x0B);
  41. public static readonly NativeType Float64 = new NativeType(0x0C);
  42. public static readonly NativeType Currency = new NativeType(0x0F);
  43. public static readonly NativeType BStr = new NativeType(0x13);
  44. public static readonly NativeType LPStr = new NativeType(0x14);
  45. public static readonly NativeType LPWStr = new NativeType(0x15);
  46. public static readonly NativeType LPTStr = new NativeType(0x16);
  47. public static readonly NativeType FixedSysString = new NativeType(0x17);
  48. public static readonly NativeType IUnknown = new NativeType(0x19);
  49. public static readonly NativeType IDispatch = new NativeType(0x1A);
  50. public static readonly NativeType Struct = new NativeType(0x1B);
  51. public static readonly NativeType Interface = new NativeType(0x1C);
  52. public static readonly NativeType Int = new NativeType(0x1F);
  53. public static readonly NativeType UInt = new NativeType(0x20);
  54. public static readonly NativeType ByValStr = new NativeType(0x22);
  55. public static readonly NativeType AnsiBStr = new NativeType(0x23);
  56. public static readonly NativeType TBstr = new NativeType(0x24);
  57. public static readonly NativeType VariantBool = new NativeType(0x25);
  58. public static readonly NativeType FuncPtr = new NativeType(0x26);
  59. public static readonly NativeType AsAny = new NativeType(0x28);
  60. private static readonly NativeType[] nativeTypes = { null, Void, Boolean, Int8,
  61. UInt8, Int16, UInt16, Int32,
  62. UInt32, Int64, UInt64,
  63. Float32, Float64, null, null,
  64. Currency, null, null, null,
  65. BStr, LPStr, LPWStr, LPTStr,
  66. FixedSysString, null, IUnknown,
  67. IDispatch, Struct, Interface,
  68. null, null, Int, UInt, null,
  69. ByValStr, AnsiBStr, TBstr,
  70. VariantBool, FuncPtr, null,
  71. AsAny};
  72. protected byte typeIndex;
  73. /*-------------------- Constructors ---------------------------------*/
  74. internal NativeType(byte tyIx) { typeIndex = tyIx; }
  75. internal byte GetTypeIndex() { return typeIndex; }
  76. internal static NativeType GetNativeType(int ix)
  77. {
  78. if (ix < nativeTypes.Length)
  79. return nativeTypes[ix];
  80. return null;
  81. }
  82. internal virtual byte[] ToBlob()
  83. {
  84. byte[] bytes = new byte[1];
  85. bytes[0] = GetTypeIndex();
  86. return bytes;
  87. }
  88. internal void Write(CILWriter output)
  89. {
  90. throw new NotYetImplementedException("Native types for CIL");
  91. }
  92. }
  93. /**************************************************************************/
  94. public class NativeArray : NativeType
  95. {
  96. NativeType elemType;
  97. uint len = 0, parNum = 0, elemMult = 1;
  98. internal static readonly byte ArrayTag = 0x2A;
  99. /*-------------------- Constructors ---------------------------------*/
  100. public NativeArray(NativeType elemType)
  101. : base((byte)NativeTypeIx.Array)
  102. {
  103. this.elemType = elemType;
  104. }
  105. public NativeArray(NativeType elemType, int len)
  106. : base((byte)NativeTypeIx.Array)
  107. {
  108. this.elemType = elemType;
  109. this.len = (uint)len;
  110. }
  111. public NativeArray(NativeType elemType, int numElem, int parNumForLen)
  112. : base((byte)NativeTypeIx.Array)
  113. {
  114. this.elemType = elemType;
  115. len = (uint)numElem;
  116. parNum = (uint)parNumForLen;
  117. }
  118. internal NativeArray(NativeType elemType, uint pNum, uint elemMult, uint numElem)
  119. : base((byte)NativeTypeIx.Array)
  120. {
  121. this.elemType = elemType;
  122. parNum = pNum;
  123. this.elemMult = elemMult;
  124. len = numElem;
  125. }
  126. internal override byte[] ToBlob()
  127. {
  128. MemoryStream str = new MemoryStream();
  129. str.WriteByte(GetTypeIndex());
  130. if (elemType == null) str.WriteByte(0x50); // no info (MAX)
  131. else str.WriteByte(elemType.GetTypeIndex());
  132. MetaDataOut.CompressNum(BlobUtil.CompressUInt(parNum), str);
  133. MetaDataOut.CompressNum(BlobUtil.CompressUInt(elemMult), str);
  134. MetaDataOut.CompressNum(BlobUtil.CompressUInt(len), str);
  135. return str.ToArray();
  136. }
  137. }
  138. }