MultiByteIntegers.cs 901 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.IO;
  3. namespace SharpCompress.Compressors.Xz
  4. {
  5. internal static class MultiByteIntegers
  6. {
  7. public static ulong ReadXZInteger(this BinaryReader reader, int MaxBytes = 9)
  8. {
  9. if (MaxBytes <= 0)
  10. throw new ArgumentOutOfRangeException();
  11. if (MaxBytes > 9)
  12. MaxBytes = 9;
  13. byte LastByte = reader.ReadByte();
  14. ulong Output = (ulong)LastByte & 0x7F;
  15. int i = 0;
  16. while ((LastByte & 0x80) != 0)
  17. {
  18. if (++i >= MaxBytes)
  19. throw new InvalidDataException();
  20. LastByte = reader.ReadByte();
  21. if (LastByte == 0)
  22. throw new InvalidDataException();
  23. Output |= ((ulong)(LastByte & 0x7F)) << (i * 7);
  24. }
  25. return Output;
  26. }
  27. }
  28. }