VirtualizedBinaryReader.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Collections;
  10. namespace BmffViewer
  11. {
  12. public class VirtualizedBinaryChunk : IEnumerable<byte?>
  13. {
  14. public long Location { get; private set; }
  15. private byte?[] _Bytes;
  16. public int Length { get { return _Bytes.Length; } }
  17. public VirtualizedBinaryChunk(long location, byte?[] bytes)
  18. {
  19. Location = location;
  20. _Bytes = bytes;
  21. }
  22. public IEnumerator<byte?> GetEnumerator()
  23. {
  24. foreach (byte? b in _Bytes) yield return b;
  25. }
  26. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  27. {
  28. return GetEnumerator();
  29. }
  30. public override string ToString()
  31. {
  32. StringBuilder chunk = new StringBuilder();
  33. chunk.Append(String.Format("{0:X8} ", Location));
  34. for (int i = 0; i < _Bytes.Length; i++)
  35. {
  36. byte? b = _Bytes[i];
  37. if (i % 8 == 0) chunk.Append(" ");
  38. if(b.HasValue) chunk.Append(String.Format("{0:X2} ", b.Value));
  39. else chunk.Append(" ");
  40. }
  41. chunk.Append(String.Format(" "));
  42. foreach(byte? b in _Bytes)
  43. {
  44. if (b.HasValue)
  45. {
  46. if (!char.IsControl((char)b.Value)) chunk.Append((char)b.Value);
  47. else chunk.Append(".");
  48. }
  49. else chunk.Append(" ");
  50. }
  51. return chunk.ToString();
  52. }
  53. }
  54. public class VirtualizedBinaryReader : IList<VirtualizedBinaryChunk>, IList
  55. {
  56. private Stream _BaseStream;
  57. private long EffectiveOffset;
  58. private long EffectiveLength;
  59. public long Offset { get; private set; }
  60. public long Length { get; private set; }
  61. public int ChunkSize { get; private set; }
  62. public VirtualizedBinaryReader(Stream stream, long offset = 0, long? length = null, int chunkSize = 16)
  63. {
  64. _BaseStream = stream;
  65. Offset = offset;
  66. Length = length.HasValue ? length.Value : stream.Length - offset;
  67. ChunkSize = chunkSize;
  68. EffectiveLength = Length;
  69. if (Offset % ChunkSize == 0) EffectiveOffset = Offset;
  70. else
  71. {
  72. // Doesn't begin on a chunk boundary. Move the effective offset back to the nearest chunk boundary
  73. // and add an equal amount to the effective length
  74. EffectiveOffset = (Offset / ChunkSize) * ChunkSize;
  75. EffectiveLength += Offset - EffectiveOffset;
  76. }
  77. if (EffectiveLength % ChunkSize != 0)
  78. // Doesn't end on a chunk boundary. Round the length up to the nearest even number of chunks.
  79. EffectiveLength = ((EffectiveLength / ChunkSize) + 1) * ChunkSize;
  80. Debug.WriteLine("ChunkSize: " + ChunkSize);
  81. Debug.WriteLine("Offset: " + Offset);
  82. Debug.WriteLine("Length: " + Length);
  83. Debug.WriteLine("EffectiveOffset: " + EffectiveOffset);
  84. Debug.WriteLine("EffectiveLength: " + EffectiveLength);
  85. Debug.WriteLine("Count: " + Count);
  86. }
  87. public int IndexOf(VirtualizedBinaryChunk item)
  88. {
  89. if (item.Location < EffectiveOffset) return -1;
  90. if (item.Location >= EffectiveOffset + EffectiveLength) return -1;
  91. if (item.Location % ChunkSize == 0) return (int)(item.Location / ChunkSize);
  92. else return -1;
  93. }
  94. public void Insert(int index, VirtualizedBinaryChunk item)
  95. {
  96. throw new NotSupportedException();
  97. }
  98. public void RemoveAt(int index)
  99. {
  100. throw new NotSupportedException();
  101. }
  102. public VirtualizedBinaryChunk this[int index]
  103. {
  104. get
  105. {
  106. //Debug.WriteLine("Accessing Item " + index);
  107. List<byte?> bytes = new List<byte?>();
  108. long chunkOffset = ((long)index * (long)ChunkSize) + EffectiveOffset;
  109. int prefix = chunkOffset < Offset ? (int)(Offset - EffectiveOffset) : 0;
  110. int suffix = chunkOffset + ChunkSize > Offset + Length ? (int)((EffectiveOffset + EffectiveLength) - (Offset + Length)) : 0;
  111. for(int i=0; i<prefix; i++) bytes.Add(null);
  112. int size = ChunkSize - prefix - suffix;
  113. if (size < 0) size = 0;
  114. byte[] readBytes = new byte[size];
  115. if (size > 0)
  116. {
  117. try
  118. {
  119. _BaseStream.Seek(chunkOffset + prefix, SeekOrigin.Begin);
  120. _BaseStream.Read(readBytes, 0, readBytes.Length);
  121. for (int i = 0; i < readBytes.Length; i++) bytes.Add(readBytes[i]);
  122. }
  123. catch (System.IO.EndOfStreamException)
  124. {
  125. Debug.WriteLine("WARNING: Data not available for VirtualizedBinaryChunk at offset " + chunkOffset);
  126. }
  127. }
  128. for (int i = 0; i < suffix; i++) bytes.Add(null);
  129. return new VirtualizedBinaryChunk(chunkOffset, bytes.ToArray());
  130. }
  131. set
  132. {
  133. throw new NotSupportedException();
  134. }
  135. }
  136. public void Add(VirtualizedBinaryChunk item)
  137. {
  138. throw new NotSupportedException();
  139. }
  140. public void Clear()
  141. {
  142. throw new NotSupportedException();
  143. }
  144. public bool Contains(VirtualizedBinaryChunk item)
  145. {
  146. if (item.Location % ChunkSize == 0 && item.Location >= Offset && item.Location < Offset + Length) return true;
  147. else return false;
  148. }
  149. public void CopyTo(VirtualizedBinaryChunk[] array, int arrayIndex)
  150. {
  151. throw new NotSupportedException();
  152. }
  153. public int Count
  154. {
  155. get { return (int) (EffectiveLength / ChunkSize); }
  156. }
  157. public bool IsReadOnly
  158. {
  159. get { return true; }
  160. }
  161. public bool Remove(VirtualizedBinaryChunk item)
  162. {
  163. throw new NotSupportedException();
  164. }
  165. public IEnumerator<VirtualizedBinaryChunk> GetEnumerator()
  166. {
  167. for (int i = 0; i < Count; i++) yield return this[i];
  168. }
  169. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  170. {
  171. return GetEnumerator();
  172. }
  173. public int Add(object value)
  174. {
  175. throw new NotSupportedException();
  176. }
  177. public bool Contains(object value)
  178. {
  179. return Contains(value as VirtualizedBinaryChunk);
  180. }
  181. public int IndexOf(object value)
  182. {
  183. return IndexOf(value as VirtualizedBinaryChunk);
  184. }
  185. public void Insert(int index, object value)
  186. {
  187. throw new NotSupportedException();
  188. }
  189. public bool IsFixedSize
  190. {
  191. get { return true; }
  192. }
  193. public void Remove(object value)
  194. {
  195. throw new NotSupportedException();
  196. }
  197. object IList.this[int index]
  198. {
  199. get
  200. {
  201. return this[index];
  202. }
  203. set
  204. {
  205. throw new NotSupportedException();
  206. }
  207. }
  208. public void CopyTo(Array array, int index)
  209. {
  210. throw new NotSupportedException();
  211. }
  212. public bool IsSynchronized
  213. {
  214. get { return false; }
  215. }
  216. private object _SyncRoot = new object();
  217. public object SyncRoot
  218. {
  219. get { return _SyncRoot; }
  220. }
  221. }
  222. }