CustomAttribute.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /// Descriptor for a Custom Attribute (.custom)
  26. /// </summary>
  27. public class CustomAttribute : MetaDataElement
  28. {
  29. internal static readonly ushort prolog = 0x0001;
  30. private static readonly int initSize = 5;
  31. MetaDataElement parent;
  32. Method type;
  33. uint valIx, parentIx, typeIx;
  34. Constant[] argVals, vals;
  35. byte[] byteVal;
  36. ushort numNamed = 0;
  37. string[] names;
  38. bool[] isField;
  39. bool changed = false;
  40. /*-------------------- Constructors ---------------------------------*/
  41. internal CustomAttribute(MetaDataElement paren, Method constrType,
  42. Constant[] val)
  43. {
  44. parent = paren;
  45. type = constrType;
  46. argVals = val;
  47. changed = true;
  48. sortTable = true;
  49. tabIx = MDTable.CustomAttribute;
  50. }
  51. internal CustomAttribute(MetaDataElement paren, Method constrType,
  52. byte[] val)
  53. {
  54. parent = paren;
  55. type = constrType;
  56. tabIx = MDTable.CustomAttribute;
  57. byteVal = val;
  58. sortTable = true;
  59. changed = true;
  60. }
  61. internal CustomAttribute(PEReader buff)
  62. {
  63. parentIx = buff.GetCodedIndex(CIx.HasCustomAttr);
  64. typeIx = buff.GetCodedIndex(CIx.CustomAttributeType);
  65. valIx = buff.GetBlobIx();
  66. sortTable = true;
  67. tabIx = MDTable.CustomAttribute;
  68. }
  69. internal static void Read(PEReader buff, TableRow[] attrs)
  70. {
  71. for (int i = 0; i < attrs.Length; i++)
  72. {
  73. attrs[i] = new CustomAttribute(buff);
  74. }
  75. }
  76. internal override void Resolve(PEReader buff)
  77. {
  78. parent = buff.GetCodedElement(CIx.HasCustomAttr, parentIx);
  79. if (parent == null) return;
  80. parent.AddCustomAttribute(this);
  81. type = (Method)buff.GetCodedElement(CIx.CustomAttributeType, typeIx);
  82. byteVal = buff.GetBlob(valIx);
  83. }
  84. /*------------------------- public set and get methods --------------------------*/
  85. public void AddFieldOrProp(string name, Constant val, bool isFld)
  86. {
  87. if ((byteVal != null) && !changed) DecodeCustomAttributeBlob();
  88. if (numNamed == 0)
  89. {
  90. names = new string[initSize];
  91. vals = new Constant[initSize];
  92. isField = new bool[initSize];
  93. }
  94. else if (numNamed >= names.Length)
  95. {
  96. string[] tmpNames = names;
  97. Constant[] tmpVals = vals;
  98. bool[] tmpField = isField;
  99. names = new String[names.Length + initSize];
  100. vals = new Constant[vals.Length + initSize];
  101. isField = new bool[isField.Length + initSize];
  102. for (int i = 0; i < numNamed; i++)
  103. {
  104. names[i] = tmpNames[i];
  105. vals[i] = tmpVals[i];
  106. isField[i] = tmpField[i];
  107. }
  108. }
  109. names[numNamed] = name;
  110. vals[numNamed] = val;
  111. isField[numNamed++] = isFld;
  112. changed = true;
  113. }
  114. public Constant[] Args
  115. {
  116. get
  117. {
  118. if (!changed && (byteVal != null))
  119. {
  120. try
  121. {
  122. DecodeCustomAttributeBlob();
  123. }
  124. catch
  125. {
  126. }
  127. }
  128. return argVals;
  129. }
  130. set
  131. {
  132. argVals = value;
  133. changed = true;
  134. }
  135. }
  136. public string[] GetNames()
  137. {
  138. return names;
  139. }
  140. public bool[] GetIsField()
  141. {
  142. return isField;
  143. }
  144. public Constant[] GetNamedArgs()
  145. {
  146. return vals;
  147. }
  148. /*----------------------------- internal functions ------------------------------*/
  149. internal void DecodeCustomAttributeBlob()
  150. {
  151. MemoryStream caBlob = new MemoryStream(byteVal);
  152. BinaryReader blob = new BinaryReader(caBlob, System.Text.Encoding.UTF8);
  153. if (blob.ReadUInt16() != CustomAttribute.prolog) throw new PEFileException("Invalid Custom Attribute Blob");
  154. Type[] parTypes = type.GetParTypes();
  155. argVals = new Constant[parTypes.Length];
  156. for (int i = 0; i < parTypes.Length; i++)
  157. {
  158. Type argType = parTypes[i];
  159. bool arrayConst = argType is Array;
  160. if (arrayConst) argType = ((ZeroBasedArray)(parTypes[i])).ElemType();
  161. bool boxed = argType is SystemClass;
  162. int eType = argType.GetTypeIndex();
  163. if (arrayConst)
  164. {
  165. Constant[] elems = new Constant[blob.ReadUInt32()];
  166. for (int j = 0; j < elems.Length; j++)
  167. {
  168. if (boxed)
  169. {
  170. eType = blob.ReadByte();
  171. elems[j] = new BoxedSimpleConst((SimpleConstant)PEReader.ReadConst(eType, blob));
  172. }
  173. else
  174. {
  175. elems[j] = PEReader.ReadConst(eType, blob);
  176. }
  177. }
  178. argVals[i] = new ArrayConst(elems);
  179. }
  180. else if (boxed)
  181. {
  182. argVals[i] = new BoxedSimpleConst((SimpleConstant)PEReader.ReadConst(blob.ReadByte(), blob));
  183. }
  184. else
  185. {
  186. argVals[i] = PEReader.ReadConst(eType, blob);
  187. }
  188. }
  189. uint numNamed = 0;
  190. if (blob.BaseStream.Position != byteVal.Length)
  191. numNamed = blob.ReadUInt16();
  192. if (numNamed > 0)
  193. {
  194. names = new string[numNamed];
  195. vals = new Constant[numNamed];
  196. isField = new bool[numNamed];
  197. for (int i = 0; i < numNamed; i++)
  198. {
  199. isField[i] = blob.ReadByte() == 0x53;
  200. int eType = blob.ReadByte();
  201. names[i] = blob.ReadString();
  202. vals[i] = PEReader.ReadConst(eType, blob);
  203. }
  204. }
  205. }
  206. internal void AddFieldOrProps(string[] names, Constant[] vals, bool[] isField)
  207. {
  208. this.names = names;
  209. this.vals = vals;
  210. this.isField = isField;
  211. numNamed = (ushort)names.Length;
  212. }
  213. internal void SetBytes(byte[] bytes)
  214. {
  215. this.byteVal = bytes;
  216. }
  217. internal Method GetCAType()
  218. {
  219. return type;
  220. }
  221. internal override uint SortKey()
  222. {
  223. return (parent.Row << MetaData.CIxShiftMap[(uint)CIx.HasCustomAttr])
  224. | parent.GetCodedIx(CIx.HasCustomAttr);
  225. }
  226. internal sealed override void BuildTables(MetaDataOut md)
  227. {
  228. md.AddToTable(tabIx, this);
  229. type.BuildMDTables(md);
  230. // more adding to tables if data is not bytes
  231. if (changed || (byteVal == null))
  232. {
  233. MemoryStream str = new MemoryStream();
  234. BinaryWriter bw = new BinaryWriter(str);
  235. bw.Write((ushort)1);
  236. if (argVals != null)
  237. {
  238. for (int i = 0; i < argVals.Length; i++)
  239. {
  240. argVals[i].Write(bw);
  241. }
  242. }
  243. bw.Write(numNamed);
  244. for (int i = 0; i < numNamed; i++)
  245. {
  246. if (isField[i]) bw.Write(Field.FieldTag);
  247. else bw.Write(Property.PropertyTag);
  248. bw.Write(vals[i].GetTypeIndex());
  249. bw.Write(names[i]); // check this is the right format!!!
  250. vals[i].Write(bw);
  251. }
  252. byteVal = str.ToArray();
  253. }
  254. valIx = md.AddToBlobHeap(byteVal);
  255. }
  256. internal static uint Size(MetaData md)
  257. {
  258. return md.CodedIndexSize(CIx.HasCustomAttr) + md.CodedIndexSize(CIx.CustomAttributeType) + md.BlobIndexSize();
  259. }
  260. internal sealed override void Write(PEWriter output)
  261. {
  262. output.WriteCodedIndex(CIx.HasCustomAttr, parent);
  263. output.WriteCodedIndex(CIx.CustomAttributeType, type);
  264. output.BlobIndex(valIx);
  265. }
  266. }
  267. }