MarkingBinaryReader.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.IO;
  3. using SharpCompress.Converters;
  4. namespace SharpCompress.IO
  5. {
  6. internal class MarkingBinaryReader : BinaryReader
  7. {
  8. public MarkingBinaryReader(Stream stream)
  9. : base(stream)
  10. {
  11. }
  12. public virtual long CurrentReadByteCount { get; protected set; }
  13. public virtual void Mark()
  14. {
  15. CurrentReadByteCount = 0;
  16. }
  17. public override int Read()
  18. {
  19. throw new NotSupportedException();
  20. }
  21. public override int Read(byte[] buffer, int index, int count)
  22. {
  23. throw new NotSupportedException();
  24. }
  25. public override int Read(char[] buffer, int index, int count)
  26. {
  27. throw new NotSupportedException();
  28. }
  29. public override bool ReadBoolean()
  30. {
  31. return ReadByte() != 0;
  32. }
  33. // NOTE: there is a somewhat fragile dependency on the internals of this class
  34. // with RarCrcBinaryReader and RarCryptoBinaryReader.
  35. //
  36. // RarCrcBinaryReader/RarCryptoBinaryReader need to override any specific methods
  37. // that call directly to the base BinaryReader and do not delegate to other methods
  38. // in this class so that it can track the each byte being read.
  39. //
  40. // if altering this class in a way that changes the implementation be sure to
  41. // update RarCrcBinaryReader/RarCryptoBinaryReader.
  42. public override byte ReadByte()
  43. {
  44. CurrentReadByteCount++;
  45. return base.ReadByte();
  46. }
  47. public override byte[] ReadBytes(int count)
  48. {
  49. CurrentReadByteCount += count;
  50. var bytes = base.ReadBytes(count);
  51. if (bytes.Length != count)
  52. {
  53. throw new EndOfStreamException(string.Format("Could not read the requested amount of bytes. End of stream reached. Requested: {0} Read: {1}", count, bytes.Length));
  54. }
  55. return bytes;
  56. }
  57. public override char ReadChar()
  58. {
  59. throw new NotSupportedException();
  60. }
  61. public override char[] ReadChars(int count)
  62. {
  63. throw new NotSupportedException();
  64. }
  65. #if !SILVERLIGHT
  66. public override decimal ReadDecimal()
  67. {
  68. throw new NotSupportedException();
  69. }
  70. #endif
  71. public override double ReadDouble()
  72. {
  73. throw new NotSupportedException();
  74. }
  75. public override short ReadInt16()
  76. {
  77. return DataConverter.LittleEndian.GetInt16(ReadBytes(2), 0);
  78. }
  79. public override int ReadInt32()
  80. {
  81. return DataConverter.LittleEndian.GetInt32(ReadBytes(4), 0);
  82. }
  83. public override long ReadInt64()
  84. {
  85. return DataConverter.LittleEndian.GetInt64(ReadBytes(8), 0);
  86. }
  87. public override sbyte ReadSByte()
  88. {
  89. return (sbyte)ReadByte();
  90. }
  91. public override float ReadSingle()
  92. {
  93. throw new NotSupportedException();
  94. }
  95. public override string ReadString()
  96. {
  97. throw new NotSupportedException();
  98. }
  99. public override ushort ReadUInt16()
  100. {
  101. return DataConverter.LittleEndian.GetUInt16(ReadBytes(2), 0);
  102. }
  103. public override uint ReadUInt32()
  104. {
  105. return DataConverter.LittleEndian.GetUInt32(ReadBytes(4), 0);
  106. }
  107. public override ulong ReadUInt64()
  108. {
  109. return DataConverter.LittleEndian.GetUInt64(ReadBytes(8), 0);
  110. }
  111. // RAR5 style variable length encoded value
  112. // maximum value of 0xffffffffffffffff (64 bits)
  113. // technote: "implies max 10 bytes consumed" -- but not really because we could extend indefinitely using 0x80 0x80 ... 0x80 00
  114. //
  115. // Variable length integer. Can include one or more bytes, where lower 7 bits of every byte contain integer data
  116. // and highest bit in every byte is the continuation flag. If highest bit is 0, this is the last byte in sequence.
  117. // So first byte contains 7 least significant bits of integer and continuation flag. Second byte, if present,
  118. // contains next 7 bits and so on.
  119. public ulong ReadRarVInt(int maxBytes = 10) {
  120. // hopefully this gets inlined
  121. return DoReadRarVInt((maxBytes - 1) * 7);
  122. }
  123. private ulong DoReadRarVInt(int maxShift) {
  124. int shift = 0;
  125. ulong result = 0;
  126. do {
  127. byte b0 = ReadByte();
  128. uint b1 = ((uint)b0) & 0x7f;
  129. ulong n = b1;
  130. ulong shifted = n << shift;
  131. if (n != shifted >> shift) {
  132. // overflow
  133. break;
  134. }
  135. result |= shifted;
  136. if (b0 == b1) {
  137. return result;
  138. }
  139. shift += 7;
  140. } while (shift <= maxShift);
  141. throw new FormatException("malformed vint");
  142. }
  143. public uint ReadRarVIntUInt32(int maxBytes = 5) {
  144. // hopefully this gets inlined
  145. return DoReadRarVIntUInt32((maxBytes - 1) * 7);
  146. }
  147. public ushort ReadRarVIntUInt16(int maxBytes = 3) {
  148. // hopefully this gets inlined
  149. return checked((ushort)DoReadRarVIntUInt32((maxBytes - 1) * 7));
  150. }
  151. public byte ReadRarVIntByte(int maxBytes = 2) {
  152. // hopefully this gets inlined
  153. return checked((byte)DoReadRarVIntUInt32((maxBytes - 1) * 7));
  154. }
  155. private uint DoReadRarVIntUInt32(int maxShift) {
  156. int shift = 0;
  157. uint result = 0;
  158. do {
  159. byte b0 = ReadByte();
  160. uint b1 = ((uint)b0) & 0x7f;
  161. uint n = b1;
  162. uint shifted = n << shift;
  163. if (n != shifted >> shift) {
  164. // overflow
  165. break;
  166. }
  167. result |= shifted;
  168. if (b0 == b1) {
  169. return result;
  170. }
  171. shift += 7;
  172. } while (shift <= maxShift);
  173. throw new FormatException("malformed vint");
  174. }
  175. }
  176. }