|
| 1 | +// Namespace starting with Sentry makes sure the SDK cuts frames off before reporting |
| 2 | +namespace Sentry.Force.Crc32 |
| 3 | +{ |
| 4 | + /// <summary> |
| 5 | + /// Implementation of CRC-32. |
| 6 | + /// This class supports several convenient static methods returning the CRC as UInt32. |
| 7 | + /// </summary> |
| 8 | + internal class Crc32Algorithm : HashAlgorithm |
| 9 | + { |
| 10 | + private uint _currentCrc; |
| 11 | + |
| 12 | + private readonly bool _isBigEndian = true; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Initializes a new instance of the <see cref="Crc32Algorithm"/> class. |
| 16 | + /// </summary> |
| 17 | + public Crc32Algorithm() |
| 18 | + { |
| 19 | +#if !NETCORE13 |
| 20 | + HashSizeValue = 32; |
| 21 | +#endif |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="Crc32Algorithm"/> class. |
| 26 | + /// </summary> |
| 27 | + /// <param name="isBigEndian">Should return bytes result as big endian or little endian</param> |
| 28 | + // Crc32 by dariogriffo uses big endian, so, we need to be compatible and return big endian as default |
| 29 | + public Crc32Algorithm(bool isBigEndian = true) |
| 30 | + : this() |
| 31 | + { |
| 32 | + _isBigEndian = isBigEndian; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Computes CRC-32 from multiple buffers. |
| 37 | + /// Call this method multiple times to chain multiple buffers. |
| 38 | + /// </summary> |
| 39 | + /// <param name="initial"> |
| 40 | + /// Initial CRC value for the algorithm. It is zero for the first buffer. |
| 41 | + /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method. |
| 42 | + /// </param> |
| 43 | + /// <param name="input">Input buffer with data to be checksummed.</param> |
| 44 | + /// <param name="offset">Offset of the input data within the buffer.</param> |
| 45 | + /// <param name="length">Length of the input data in the buffer.</param> |
| 46 | + /// <returns>Accumulated CRC-32 of all buffers processed so far.</returns> |
| 47 | + public static uint Append(uint initial, byte[] input, int offset, int length) |
| 48 | + { |
| 49 | + if (input == null) |
| 50 | + throw new ArgumentNullException("input"); |
| 51 | + if (offset < 0 || length < 0 || offset + length > input.Length) |
| 52 | + throw new ArgumentOutOfRangeException("length"); |
| 53 | + return AppendInternal(initial, input, offset, length); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Computes CRC-32 from multiple buffers. |
| 58 | + /// Call this method multiple times to chain multiple buffers. |
| 59 | + /// </summary> |
| 60 | + /// <param name="initial"> |
| 61 | + /// Initial CRC value for the algorithm. It is zero for the first buffer. |
| 62 | + /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method. |
| 63 | + /// </param> |
| 64 | + /// <param name="input">Input buffer containing data to be checksummed.</param> |
| 65 | + /// <returns>Accumulated CRC-32 of all buffers processed so far.</returns> |
| 66 | + public static uint Append(uint initial, byte[] input) |
| 67 | + { |
| 68 | + if (input == null) |
| 69 | + throw new ArgumentNullException(); |
| 70 | + return AppendInternal(initial, input, 0, input.Length); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Computes CRC-32 from input buffer. |
| 75 | + /// </summary> |
| 76 | + /// <param name="input">Input buffer with data to be checksummed.</param> |
| 77 | + /// <param name="offset">Offset of the input data within the buffer.</param> |
| 78 | + /// <param name="length">Length of the input data in the buffer.</param> |
| 79 | + /// <returns>CRC-32 of the data in the buffer.</returns> |
| 80 | + public static uint Compute(byte[] input, int offset, int length) |
| 81 | + { |
| 82 | + return Append(0, input, offset, length); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Computes CRC-32 from input buffer. |
| 87 | + /// </summary> |
| 88 | + /// <param name="input">Input buffer containing data to be checksummed.</param> |
| 89 | + /// <returns>CRC-32 of the buffer.</returns> |
| 90 | + public static uint Compute(byte[] input) |
| 91 | + { |
| 92 | + return Append(0, input); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Computes CRC-32 from input buffer and writes it after end of data (buffer should have 4 bytes reserved space for it). Can be used in conjunction with <see cref="IsValidWithCrcAtEnd(byte[],int,int)"/> |
| 97 | + /// </summary> |
| 98 | + /// <param name="input">Input buffer with data to be checksummed.</param> |
| 99 | + /// <param name="offset">Offset of the input data within the buffer.</param> |
| 100 | + /// <param name="length">Length of the input data in the buffer.</param> |
| 101 | + /// <returns>CRC-32 of the data in the buffer.</returns> |
| 102 | + public static uint ComputeAndWriteToEnd(byte[] input, int offset, int length) |
| 103 | + { |
| 104 | + if (length + 4 > input.Length) |
| 105 | + throw new ArgumentOutOfRangeException("length", "Length of data should be less than array length - 4 bytes of CRC data"); |
| 106 | + var crc = Append(0, input, offset, length); |
| 107 | + var r = offset + length; |
| 108 | + input[r] = (byte)crc; |
| 109 | + input[r + 1] = (byte)(crc >> 8); |
| 110 | + input[r + 2] = (byte)(crc >> 16); |
| 111 | + input[r + 3] = (byte)(crc >> 24); |
| 112 | + return crc; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Computes CRC-32 from input buffer - 4 bytes and writes it as last 4 bytes of buffer. Can be used in conjunction with <see cref="IsValidWithCrcAtEnd(byte[])"/> |
| 117 | + /// </summary> |
| 118 | + /// <param name="input">Input buffer with data to be checksummed.</param> |
| 119 | + /// <returns>CRC-32 of the data in the buffer.</returns> |
| 120 | + public static uint ComputeAndWriteToEnd(byte[] input) |
| 121 | + { |
| 122 | + if (input.Length < 4) |
| 123 | + throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least"); |
| 124 | + return ComputeAndWriteToEnd(input, 0, input.Length - 4); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with <see cref="ComputeAndWriteToEnd(byte[],int,int)"/> |
| 129 | + /// </summary> |
| 130 | + /// <param name="input">Input buffer with data to be checksummed.</param> |
| 131 | + /// <param name="offset">Offset of the input data within the buffer.</param> |
| 132 | + /// <param name="lengthWithCrc">Length of the input data in the buffer with CRC-32 bytes.</param> |
| 133 | + /// <returns>Is checksum valid.</returns> |
| 134 | + public static bool IsValidWithCrcAtEnd(byte[] input, int offset, int lengthWithCrc) |
| 135 | + { |
| 136 | + return Append(0, input, offset, lengthWithCrc) == 0x2144DF1C; |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with <see cref="ComputeAndWriteToEnd(byte[],int,int)"/> |
| 141 | + /// </summary> |
| 142 | + /// <param name="input">Input buffer with data to be checksummed.</param> |
| 143 | + /// <returns>Is checksum valid.</returns> |
| 144 | + public static bool IsValidWithCrcAtEnd(byte[] input) |
| 145 | + { |
| 146 | + if (input.Length < 4) |
| 147 | + throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least"); |
| 148 | + return Append(0, input, 0, input.Length) == 0x2144DF1C; |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Resets internal state of the algorithm. Used internally. |
| 153 | + /// </summary> |
| 154 | + public override void Initialize() |
| 155 | + { |
| 156 | + _currentCrc = 0; |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Appends CRC-32 from given buffer |
| 161 | + /// </summary> |
| 162 | + protected override void HashCore(byte[] input, int offset, int length) |
| 163 | + { |
| 164 | + _currentCrc = AppendInternal(_currentCrc, input, offset, length); |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Computes CRC-32 from <see cref="HashCore"/> |
| 169 | + /// </summary> |
| 170 | + protected override byte[] HashFinal() |
| 171 | + { |
| 172 | + if (_isBigEndian) |
| 173 | + return new[] { (byte)(_currentCrc >> 24), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 8), (byte)_currentCrc }; |
| 174 | + else |
| 175 | + return new[] { (byte)_currentCrc, (byte)(_currentCrc >> 8), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 24) }; |
| 176 | + } |
| 177 | + |
| 178 | + private static readonly SafeProxy _proxy = new SafeProxy(); |
| 179 | + |
| 180 | + private static uint AppendInternal(uint initial, byte[] input, int offset, int length) |
| 181 | + { |
| 182 | + if (length > 0) |
| 183 | + { |
| 184 | + return _proxy.Append(initial, input, offset, length); |
| 185 | + } |
| 186 | + else |
| 187 | + return initial; |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments