-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Added Adler32 to System.IO.Hashing. #123601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AraHaan
wants to merge
20
commits into
dotnet:main
Choose a base branch
from
AraHaan:add-adler32-to-system-io-hashing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
619a51f
Added Adler32 to System.IO.Hashing.
AraHaan 3def57c
Update Adler32.cs
AraHaan b22a0c2
Removed Residue test cases from Adler32Tests.
AraHaan 294352b
Fixed empty hash value tests.
AraHaan 69718e3
Optimized away needless bounds checking in each access to buf.
AraHaan 14e6189
Moved Adler32 up in the ref source.
AraHaan 1bcea92
Fix Adler32.Update to return current adler when empty
AraHaan 1293ece
Moved Base and NMax into locals inside of Update().
AraHaan 2d15968
Merge branch 'main' into add-adler32-to-system-io-hashing
AraHaan 73eadf3
Update src/libraries/System.IO.Hashing/src/System/IO/Hashing/Adler32.cs
AraHaan 1401e24
Fixed writing LittleEndian -> BigEndian
AraHaan 9a57517
Fixed tests.
AraHaan 4ddf40b
Added method for vectorized adler32, and moved the normal adler32 cal…
AraHaan 5033ba1
Added functions for Avx2 and Sse Scalar implementations.
AraHaan 655e584
Commented out the optimization paths until they are properly implemen…
AraHaan a3dccb9
Removed commented out code.
AraHaan b9bc5d3
Merge branch 'main' into add-adler32-to-system-io-hashing
AraHaan 318e331
Merge branch 'main' into add-adler32-to-system-io-hashing
AraHaan cd8a8a4
Removed Adler32.Vectorized.cs as it is not used at the moment.
AraHaan 62a3e81
Merge branch 'main' into add-adler32-to-system-io-hashing
AraHaan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
src/libraries/System.IO.Hashing/src/System/IO/Hashing/Adler32.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers.Binary; | ||
|
|
||
| namespace System.IO.Hashing | ||
| { | ||
| /// <summary> | ||
| /// Provides an implementation of the Adler-32 algorithm, as used in | ||
| /// RFC1950. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The Adler-32 algorithm is designed for fast, lightweight integrity checking and is commonly used in | ||
| /// data compression and transmission scenarios. This class is not suitable for cryptographic purposes. | ||
| /// </para> | ||
| /// <para> | ||
| /// Adler-32 is not as robust as other checksum algorithms like CRC32, but it is faster to compute. | ||
| /// It also originally comes from zlib. | ||
| /// </para> | ||
| /// <para> | ||
| /// The Adler-32 checksum is stored as s2*65536 + s1 in most-significant-byte first(network) order. | ||
| /// </para> | ||
| /// </remarks> | ||
| public sealed partial class Adler32 : NonCryptographicHashAlgorithm | ||
| { | ||
| private const uint InitialState = 1u; | ||
| private const int Size = sizeof(uint); | ||
| private uint _adler = InitialState; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Adler32"/> class. | ||
| /// </summary> | ||
| public Adler32() | ||
| : base(Size) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Adler32"/> class using the state from another instance. | ||
| /// </summary> | ||
| private Adler32(uint adler) | ||
| : base(Size) | ||
| => _adler = adler; | ||
|
|
||
| /// <summary> | ||
| /// Returns a clone of the current instance, with a copy of the current instance's internal state. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// A new instance that will produce the same sequence of values as the current instance. | ||
| /// </returns> | ||
| public Adler32 Clone() | ||
| => new(_adler); | ||
|
|
||
| /// <summary> | ||
| /// Appends the contents of <paramref name="source"/> to the data already | ||
| /// processed for the current hash computation. | ||
| /// </summary> | ||
| /// <param name="source">The data to process.</param> | ||
| public override void Append(ReadOnlySpan<byte> source) | ||
| => _adler = Update(_adler, source); | ||
|
|
||
| /// <summary> | ||
| /// Resets the hash computation to the initial state. | ||
| /// </summary> | ||
| public override void Reset() | ||
| => _adler = InitialState; | ||
|
|
||
| /// <summary> | ||
| /// Writes the computed hash value to <paramref name="destination"/> | ||
| /// without modifying accumulated state. | ||
| /// </summary> | ||
| /// <param name="destination">The buffer that receives the computed hash value.</param> | ||
| protected override void GetCurrentHashCore(Span<byte> destination) | ||
| => BinaryPrimitives.WriteUInt32BigEndian(destination, _adler); | ||
|
|
||
| /// <summary> | ||
| /// Writes the computed hash value to <paramref name="destination"/> | ||
| /// then clears the accumulated state. | ||
| /// </summary> | ||
| protected override void GetHashAndResetCore(Span<byte> destination) | ||
| { | ||
| BinaryPrimitives.WriteUInt32BigEndian(destination, _adler); | ||
| _adler = InitialState; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the current computed hash value without modifying accumulated state. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The hash value for the data already provided. | ||
| /// </returns> | ||
| [CLSCompliant(false)] | ||
| public uint GetCurrentHashAsUInt32() | ||
| => _adler; | ||
|
|
||
| /// <summary> | ||
| /// Computes the Adler-32 hash of the provided data. | ||
| /// </summary> | ||
| /// <param name="source">The data to hash.</param> | ||
| /// <returns>The Adler-32 hash of the provided data.</returns> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="source"/> is <see langword="null"/>. | ||
| /// </exception> | ||
| public static byte[] Hash(byte[] source) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(source); | ||
| return Hash(new ReadOnlySpan<byte>(source)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Computes the Adler-32 hash of the provided data. | ||
| /// </summary> | ||
| /// <param name="source">The data to hash.</param> | ||
| /// <returns>The Adler-32 hash of the provided data.</returns> | ||
| public static byte[] Hash(ReadOnlySpan<byte> source) | ||
| { | ||
| byte[] ret = new byte[Size]; | ||
| uint hash = HashToUInt32(source); | ||
| BinaryPrimitives.WriteUInt32BigEndian(ret, hash); | ||
| return ret; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to compute the Adler-32 hash of the provided data into the provided destination. | ||
| /// </summary> | ||
| /// <param name="source">The data to hash.</param> | ||
| /// <param name="destination">The buffer that receives the computed hash value.</param> | ||
| /// <param name="bytesWritten"> | ||
| /// On success, receives the number of bytes written to <paramref name="destination"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if <paramref name="destination"/> is long enough to receive | ||
| /// the computed hash value (4 bytes); otherwise, <see langword="false"/>. | ||
| /// </returns> | ||
| public static bool TryHash(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) | ||
| { | ||
| if (destination.Length < Size) | ||
| { | ||
| bytesWritten = 0; | ||
| return false; | ||
| } | ||
|
|
||
| uint hash = HashToUInt32(source); | ||
| BinaryPrimitives.WriteUInt32BigEndian(destination, hash); | ||
| bytesWritten = Size; | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Computes the Adler-32 hash of the provided data into the provided destination. | ||
| /// </summary> | ||
| /// <param name="source">The data to hash.</param> | ||
| /// <param name="destination">The buffer that receives the computed hash value.</param> | ||
| /// <returns> | ||
| /// The number of bytes written to <paramref name="destination"/>. | ||
| /// </returns> | ||
| public static int Hash(ReadOnlySpan<byte> source, Span<byte> destination) | ||
| { | ||
| if (destination.Length < Size) | ||
| { | ||
| ThrowDestinationTooShort(); | ||
| } | ||
|
|
||
| uint hash = HashToUInt32(source); | ||
| BinaryPrimitives.WriteUInt32BigEndian(destination, hash); | ||
| return Size; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Computes the Adler-32 hash of the provided data. | ||
| /// </summary> | ||
| /// <param name="source">The data to hash.</param> | ||
| /// <returns> | ||
| /// The computed Adler-32 hash. | ||
| /// </returns> | ||
| [CLSCompliant(false)] | ||
| public static uint HashToUInt32(ReadOnlySpan<byte> source) | ||
| => Update(InitialState, source); | ||
|
|
||
| private static uint Update(uint adler, ReadOnlySpan<byte> buf) | ||
| { | ||
| if (buf.IsEmpty) | ||
| { | ||
| return adler; | ||
| } | ||
|
|
||
| return UpdateScalar(adler, buf); | ||
| } | ||
|
|
||
| private static uint UpdateScalar(uint adler, ReadOnlySpan<byte> buf) | ||
| { | ||
| const uint Base = 65521; // largest prime smaller than 65536 | ||
| const int NMax = 5552; // NMax is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 | ||
|
|
||
| uint s1 = adler & 0xFFFF; | ||
| uint s2 = (adler >> 16) & 0xFFFF; | ||
| while (buf.Length > 0) | ||
| { | ||
| int k = buf.Length < NMax ? buf.Length : NMax; | ||
| foreach (byte b in buf.Slice(0, k)) | ||
| { | ||
| s1 += b; | ||
| s2 += s1; | ||
| } | ||
AraHaan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| s1 %= Base; | ||
| s2 %= Base; | ||
| buf = buf.Slice(k); | ||
| } | ||
|
|
||
| return (s2 << 16) | s1; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.