DataReader.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using SharpCompress.Compressors.LZMA;
  5. namespace SharpCompress.Common.SevenZip
  6. {
  7. internal class DataReader
  8. {
  9. #region Static Methods
  10. public static uint Get32(byte[] buffer, int offset)
  11. {
  12. return buffer[offset]
  13. + ((uint)buffer[offset + 1] << 8)
  14. + ((uint)buffer[offset + 2] << 16)
  15. + ((uint)buffer[offset + 3] << 24);
  16. }
  17. public static ulong Get64(byte[] buffer, int offset)
  18. {
  19. return buffer[offset]
  20. + ((ulong)buffer[offset + 1] << 8)
  21. + ((ulong)buffer[offset + 2] << 16)
  22. + ((ulong)buffer[offset + 3] << 24)
  23. + ((ulong)buffer[offset + 4] << 32)
  24. + ((ulong)buffer[offset + 5] << 40)
  25. + ((ulong)buffer[offset + 6] << 48)
  26. + ((ulong)buffer[offset + 7] << 56);
  27. }
  28. #endregion
  29. #region Variables
  30. private readonly byte[] _buffer;
  31. private readonly int _ending;
  32. #endregion
  33. #region Public Methods
  34. public DataReader(byte[] buffer, int offset, int length)
  35. {
  36. _buffer = buffer;
  37. Offset = offset;
  38. _ending = offset + length;
  39. }
  40. public int Offset { get; private set; }
  41. public Byte ReadByte()
  42. {
  43. if (Offset >= _ending)
  44. {
  45. throw new EndOfStreamException();
  46. }
  47. return _buffer[Offset++];
  48. }
  49. public void ReadBytes(byte[] buffer, int offset, int length)
  50. {
  51. if (length > _ending - Offset)
  52. {
  53. throw new EndOfStreamException();
  54. }
  55. while (length-- > 0)
  56. {
  57. buffer[offset++] = _buffer[Offset++];
  58. }
  59. }
  60. public void SkipData(long size)
  61. {
  62. if (size > _ending - Offset)
  63. {
  64. throw new EndOfStreamException();
  65. }
  66. Offset += (int)size;
  67. #if DEBUG
  68. Log.WriteLine("SkipData {0}", size);
  69. #endif
  70. }
  71. public void SkipData()
  72. {
  73. SkipData(checked((long)ReadNumber()));
  74. }
  75. public ulong ReadNumber()
  76. {
  77. if (Offset >= _ending)
  78. {
  79. throw new EndOfStreamException();
  80. }
  81. byte firstByte = _buffer[Offset++];
  82. byte mask = 0x80;
  83. ulong value = 0;
  84. for (int i = 0; i < 8; i++)
  85. {
  86. if ((firstByte & mask) == 0)
  87. {
  88. ulong highPart = firstByte & (mask - 1u);
  89. value += highPart << (i * 8);
  90. return value;
  91. }
  92. if (Offset >= _ending)
  93. {
  94. throw new EndOfStreamException();
  95. }
  96. value |= (ulong)_buffer[Offset++] << (8 * i);
  97. mask >>= 1;
  98. }
  99. return value;
  100. }
  101. public int ReadNum()
  102. {
  103. ulong value = ReadNumber();
  104. if (value > Int32.MaxValue)
  105. {
  106. throw new NotSupportedException();
  107. }
  108. return (int)value;
  109. }
  110. public uint ReadUInt32()
  111. {
  112. if (Offset + 4 > _ending)
  113. {
  114. throw new EndOfStreamException();
  115. }
  116. uint res = Get32(_buffer, Offset);
  117. Offset += 4;
  118. return res;
  119. }
  120. public ulong ReadUInt64()
  121. {
  122. if (Offset + 8 > _ending)
  123. {
  124. throw new EndOfStreamException();
  125. }
  126. ulong res = Get64(_buffer, Offset);
  127. Offset += 8;
  128. return res;
  129. }
  130. public string ReadString()
  131. {
  132. int ending = Offset;
  133. for (;;)
  134. {
  135. if (ending + 2 > _ending)
  136. {
  137. throw new EndOfStreamException();
  138. }
  139. if (_buffer[ending] == 0 && _buffer[ending + 1] == 0)
  140. {
  141. break;
  142. }
  143. ending += 2;
  144. }
  145. string str = Encoding.Unicode.GetString(_buffer, Offset, ending - Offset);
  146. Offset = ending + 2;
  147. return str;
  148. }
  149. #endregion
  150. }
  151. }