Utils.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. namespace SharpCompress.Compressors.LZMA.Utilites
  5. {
  6. internal enum BlockType : byte
  7. {
  8. #region Constants
  9. End = 0,
  10. Header = 1,
  11. ArchiveProperties = 2,
  12. AdditionalStreamsInfo = 3,
  13. MainStreamsInfo = 4,
  14. FilesInfo = 5,
  15. PackInfo = 6,
  16. UnpackInfo = 7,
  17. SubStreamsInfo = 8,
  18. Size = 9,
  19. Crc = 10,
  20. Folder = 11,
  21. CodersUnpackSize = 12,
  22. NumUnpackStream = 13,
  23. EmptyStream = 14,
  24. EmptyFile = 15,
  25. Anti = 16,
  26. Name = 17,
  27. CTime = 18,
  28. ATime = 19,
  29. MTime = 20,
  30. WinAttributes = 21,
  31. Comment = 22,
  32. EncodedHeader = 23,
  33. StartPos = 24,
  34. Dummy = 25
  35. #endregion
  36. }
  37. internal static class Utils
  38. {
  39. [Conditional("DEBUG")]
  40. public static void Assert(bool expression)
  41. {
  42. if (!expression)
  43. {
  44. if (Debugger.IsAttached)
  45. {
  46. Debugger.Break();
  47. }
  48. throw new Exception("Assertion failed.");
  49. }
  50. }
  51. public static void ReadExact(this Stream stream, byte[] buffer, int offset, int length)
  52. {
  53. if (stream == null)
  54. {
  55. throw new ArgumentNullException(nameof(stream));
  56. }
  57. if (buffer == null)
  58. {
  59. throw new ArgumentNullException(nameof(buffer));
  60. }
  61. if (offset < 0 || offset > buffer.Length)
  62. {
  63. throw new ArgumentOutOfRangeException(nameof(offset));
  64. }
  65. if (length < 0 || length > buffer.Length - offset)
  66. {
  67. throw new ArgumentOutOfRangeException(nameof(length));
  68. }
  69. while (length > 0)
  70. {
  71. int fetched = stream.Read(buffer, offset, length);
  72. if (fetched <= 0)
  73. {
  74. throw new EndOfStreamException();
  75. }
  76. offset += fetched;
  77. length -= fetched;
  78. }
  79. }
  80. }
  81. }