2
0

CILWriter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.Diagnostics;
  20. using System.Collections;
  21. namespace QUT.PERWAPI
  22. {
  23. /**************************************************************************/
  24. // Class to Write CIL File
  25. /**************************************************************************/
  26. public class CILWriter : StreamWriter
  27. {
  28. PEFile pefile;
  29. ArrayList externRefs = new ArrayList();
  30. FieldDef[] fields;
  31. MethodDef[] methods;
  32. ClassDef[] classes;
  33. private bool debug;
  34. public CILWriter(string filename, bool debug, PEFile pefile)
  35. : base(new FileStream(filename, FileMode.Create))
  36. {
  37. this.pefile = pefile;
  38. WriteLine("// ILASM output by PERWAPI");
  39. WriteLine("// for file <" + pefile.GetFileName() + ">");
  40. }
  41. internal void AddRef(ReferenceScope refScope)
  42. {
  43. if (!externRefs.Contains(refScope))
  44. {
  45. externRefs.Add(refScope);
  46. }
  47. }
  48. internal bool Debug
  49. {
  50. get { return debug; }
  51. }
  52. internal void BuildCILInfo()
  53. {
  54. fields = pefile.GetFields();
  55. methods = pefile.GetMethods();
  56. classes = pefile.GetClasses();
  57. if (fields != null)
  58. {
  59. for (int i = 0; i < fields.Length; i++)
  60. {
  61. fields[i].BuildCILInfo(this);
  62. }
  63. }
  64. if (methods != null)
  65. {
  66. for (int i = 0; i < methods.Length; i++)
  67. {
  68. methods[i].BuildCILInfo(this);
  69. }
  70. }
  71. if (classes != null)
  72. {
  73. for (int i = 0; i < classes.Length; i++)
  74. {
  75. classes[i].BuildCILInfo(this);
  76. }
  77. }
  78. }
  79. public void WriteFile(bool debug)
  80. {
  81. this.debug = debug;
  82. for (int i = 0; i < externRefs.Count; i++)
  83. {
  84. ((ReferenceScope)externRefs[i]).Write(this);
  85. }
  86. Assembly assem = pefile.GetThisAssembly();
  87. if (assem != null)
  88. {
  89. assem.Write(this);
  90. }
  91. WriteLine(".module " + pefile.GetFileName());
  92. if (fields != null)
  93. {
  94. for (int i = 0; i < fields.Length; i++)
  95. {
  96. fields[i].Write(this);
  97. }
  98. }
  99. if (methods != null)
  100. {
  101. for (int i = 0; i < methods.Length; i++)
  102. {
  103. methods[i].Write(this);
  104. }
  105. }
  106. if (classes != null)
  107. {
  108. for (int i = 0; i < classes.Length; i++)
  109. {
  110. classes[i].Write(this);
  111. }
  112. }
  113. this.Flush();
  114. this.Close();
  115. }
  116. }
  117. }