SourceFileEntry.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. namespace Mono.CompilerServices.SymbolWriter
  5. {
  6. public class SourceFileEntry
  7. {
  8. public readonly int Index;
  9. private int DataOffset;
  10. private MonoSymbolFile file;
  11. private string file_name;
  12. private byte[] guid;
  13. private byte[] hash;
  14. private bool creating;
  15. private bool auto_generated;
  16. public static int Size
  17. {
  18. get
  19. {
  20. return 8;
  21. }
  22. }
  23. public string FileName
  24. {
  25. get
  26. {
  27. return this.file_name;
  28. }
  29. }
  30. public bool AutoGenerated
  31. {
  32. get
  33. {
  34. return this.auto_generated;
  35. }
  36. }
  37. public SourceFileEntry(MonoSymbolFile file, string file_name)
  38. {
  39. this.file = file;
  40. this.file_name = file_name;
  41. this.Index = file.AddSource(this);
  42. this.creating = true;
  43. }
  44. public SourceFileEntry(MonoSymbolFile file, string file_name, byte[] guid, byte[] checksum) : this(file, file_name)
  45. {
  46. this.guid = guid;
  47. this.hash = checksum;
  48. }
  49. //internal void WriteData(MyBinaryWriter bw)
  50. //{
  51. // this.DataOffset = (int)bw.BaseStream.Position;
  52. // bw.Write(this.file_name);
  53. // if (this.guid == null)
  54. // {
  55. // this.guid = Guid.NewGuid().ToByteArray();
  56. // try
  57. // {
  58. // using (FileStream fs = new FileStream(this.file_name, FileMode.Open, FileAccess.Read))
  59. // {
  60. // MD5 md5 = MD5.Create();
  61. // this.hash = md5.ComputeHash(fs);
  62. // }
  63. // }
  64. // catch
  65. // {
  66. // this.hash = new byte[16];
  67. // }
  68. // }
  69. // bw.Write(this.guid);
  70. // bw.Write(this.hash);
  71. // bw.Write(this.auto_generated ? 1 : 0);
  72. //}
  73. //internal void Write(BinaryWriter bw)
  74. //{
  75. // bw.Write(this.Index);
  76. // bw.Write(this.DataOffset);
  77. //}
  78. internal SourceFileEntry(MonoSymbolFile file, MyBinaryReader reader)
  79. {
  80. this.file = file;
  81. this.Index = reader.ReadInt32();
  82. this.DataOffset = reader.ReadInt32();
  83. int old_pos = (int)reader.BaseStream.Position;
  84. reader.BaseStream.Position = (long)this.DataOffset;
  85. this.file_name = reader.ReadString();
  86. this.guid = reader.ReadBytes(16);
  87. this.hash = reader.ReadBytes(16);
  88. this.auto_generated = (reader.ReadByte() == 1);
  89. reader.BaseStream.Position = (long)old_pos;
  90. }
  91. public void SetAutoGenerated()
  92. {
  93. if (!this.creating)
  94. {
  95. throw new InvalidOperationException();
  96. }
  97. this.auto_generated = true;
  98. this.file.OffsetTable.FileFlags |= OffsetTable.Flags.IsAspxSource;
  99. }
  100. public bool CheckChecksum()
  101. {
  102. bool result;
  103. try
  104. {
  105. using (FileStream fs = new FileStream(this.file_name, FileMode.Open))
  106. {
  107. MD5 md5 = MD5.Create();
  108. byte[] data = md5.ComputeHash(fs);
  109. for (int i = 0; i < 16; i++)
  110. {
  111. if (data[i] != this.hash[i])
  112. {
  113. result = false;
  114. return result;
  115. }
  116. }
  117. result = true;
  118. }
  119. }
  120. catch
  121. {
  122. result = false;
  123. }
  124. return result;
  125. }
  126. public override string ToString()
  127. {
  128. return string.Format("SourceFileEntry ({0}:{1})", this.Index, this.DataOffset);
  129. }
  130. }
  131. }