Crc32Stream.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace SharpCompress.Crypto
  5. {
  6. internal sealed class Crc32Stream : Stream
  7. {
  8. public const uint DefaultPolynomial = 0xedb88320u;
  9. public const uint DefaultSeed = 0xffffffffu;
  10. private static uint[] defaultTable;
  11. private readonly uint[] table;
  12. private uint hash;
  13. private readonly Stream stream;
  14. public Crc32Stream(Stream stream)
  15. : this(stream, DefaultPolynomial, DefaultSeed)
  16. {
  17. }
  18. public Crc32Stream(Stream stream, uint polynomial, uint seed)
  19. {
  20. this.stream = stream;
  21. table = InitializeTable(polynomial);
  22. hash = seed;
  23. }
  24. public Stream WrappedStream => stream;
  25. public override void Flush()
  26. {
  27. stream.Flush();
  28. }
  29. public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
  30. public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
  31. public override void SetLength(long value) => throw new NotSupportedException();
  32. public override void Write(byte[] buffer, int offset, int count)
  33. {
  34. stream.Write(buffer, offset, count);
  35. hash = CalculateCrc(table, hash, buffer, offset, count);
  36. }
  37. public override void WriteByte(byte value)
  38. {
  39. stream.WriteByte(value);
  40. hash = CalculateCrc(table, hash, value);
  41. }
  42. public override bool CanRead => stream.CanRead;
  43. public override bool CanSeek => false;
  44. public override bool CanWrite => stream.CanWrite;
  45. public override long Length => throw new NotSupportedException();
  46. public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
  47. public uint Crc => ~hash;
  48. public static uint Compute(byte[] buffer)
  49. {
  50. return Compute(DefaultSeed, buffer);
  51. }
  52. public static uint Compute(uint seed, byte[] buffer)
  53. {
  54. return Compute(DefaultPolynomial, seed, buffer);
  55. }
  56. public static uint Compute(uint polynomial, uint seed, byte[] buffer)
  57. {
  58. return ~CalculateCrc(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  59. }
  60. private static uint[] InitializeTable(uint polynomial)
  61. {
  62. if (polynomial == DefaultPolynomial && defaultTable != null)
  63. return defaultTable;
  64. var createTable = new uint[256];
  65. for (var i = 0; i < 256; i++)
  66. {
  67. var entry = (uint)i;
  68. for (var j = 0; j < 8; j++)
  69. if ((entry & 1) == 1)
  70. entry = (entry >> 1) ^ polynomial;
  71. else
  72. entry = entry >> 1;
  73. createTable[i] = entry;
  74. }
  75. if (polynomial == DefaultPolynomial)
  76. defaultTable = createTable;
  77. return createTable;
  78. }
  79. private static uint CalculateCrc(uint[] table, uint crc, byte[] buffer, int offset, int count)
  80. {
  81. unchecked
  82. {
  83. for (int i = offset, end = offset + count; i < end; i++)
  84. {
  85. crc = CalculateCrc(table, crc, buffer[i]);
  86. }
  87. }
  88. return crc;
  89. }
  90. private static uint CalculateCrc(uint[] table, uint crc, byte b)
  91. {
  92. return (crc >> 8) ^ table[(crc ^ b) & 0xFF];
  93. }
  94. }
  95. }