Skip to content

Commit 6fa824b

Browse files
authored
Merge pull request #1405 from julianxhokaxhiu/fix/rar-crc
2 parents 26c1797 + e4cc79f commit 6fa824b

1 file changed

Lines changed: 8 additions & 31 deletions

File tree

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
11
using System;
2+
using SharpCompress.Crypto;
23

34
namespace SharpCompress.Compressors.Rar;
45

56
internal static class RarCRC
67
{
7-
private static readonly uint[] crcTab;
8+
// Reuse Crc32Stream's cached slice-by-16 table (same polynomial) instead of a separate
9+
// byte-at-a-time table, so bulk CRC checks during Rar extraction get the same throughput
10+
// as Zip/GZip/7Zip/LZip CRC32 validation instead of a much slower one-byte-per-iteration loop.
11+
private static readonly uint[] crcTab = Crc32Stream.InitializeTable(
12+
Crc32Stream.DEFAULT_POLYNOMIAL
13+
);
814

915
public static uint CheckCrc(uint startCrc, byte b) =>
1016
(crcTab[((int)startCrc ^ b) & 0xff] ^ (startCrc >> 8));
1117

1218
public static uint CheckCrc(uint startCrc, ReadOnlySpan<byte> data, int offset, int count)
1319
{
1420
var size = Math.Min(data.Length - offset, count);
15-
16-
for (var i = 0; i < size; i++)
17-
{
18-
startCrc = (crcTab[((int)startCrc ^ data[offset + i]) & 0xff] ^ (startCrc >> 8));
19-
}
20-
return (startCrc);
21-
}
22-
23-
static RarCRC()
24-
{
25-
{
26-
crcTab = new uint[256];
27-
for (uint i = 0; i < 256; i++)
28-
{
29-
var c = i;
30-
for (var j = 0; j < 8; j++)
31-
{
32-
if ((c & 1) != 0)
33-
{
34-
c >>= 1;
35-
c ^= 0xEDB88320;
36-
}
37-
else
38-
{
39-
c >>= 1;
40-
}
41-
}
42-
crcTab[i] = c;
43-
}
44-
}
21+
return Crc32Stream.CalculateCrc(crcTab, startCrc, data.Slice(offset, size));
4522
}
4623
}

0 commit comments

Comments
 (0)