adler-1.0.2/.cargo_vcs_info.json0000644000000001120000000000000121140ustar { "git": { "sha1": "a94f525f62698d699d1fb3cc9112db8c35662b16" } } adler-1.0.2/.github/workflows/ci.yml000064400000000000000000000032640000000000000154100ustar 00000000000000name: CI on: push: branches: - master - staging - trying pull_request: branches: - master env: CARGO_TERM_COLOR: always RUSTFLAGS: "--deny warnings" MSRV: 1.31.0 NO_STD_TARGET: thumbv6m-none-eabi jobs: test: strategy: matrix: rust: - stable - nightly os: - ubuntu-latest - macOS-latest - windows-latest runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: ${{ matrix.rust }} override: true - name: Build run: cargo build --all --all-targets - name: Run tests run: | cargo test --all --all-targets cargo test --all --no-default-features no-std: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true target: ${{ env.NO_STD_TARGET }} - name: Build run: cargo build --verbose --no-default-features --target ${{ env.NO_STD_TARGET }} msrv: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: ${{ env.MSRV }} override: true - name: Build run: cargo build --verbose lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true components: rustfmt - name: Check code formatting run: cargo fmt -- --check adler-1.0.2/.gitignore000064400000000000000000000000220000000000000126520ustar 00000000000000target Cargo.lock adler-1.0.2/CHANGELOG.md000064400000000000000000000032620000000000000125040ustar 00000000000000# Changelog ## Unreleased No changes. ## [1.0.2 - 2021-02-26](https://github.com/jonas-schievink/adler/releases/tag/v1.0.2) - Fix doctest on big-endian systems ([#9]). [#9]: https://github.com/jonas-schievink/adler/pull/9 ## [1.0.1 - 2020-11-08](https://github.com/jonas-schievink/adler/releases/tag/v1.0.1) ### Fixes - Fix documentation on docs.rs. ## [1.0.0 - 2020-11-08](https://github.com/jonas-schievink/adler/releases/tag/v1.0.0) ### Fixes - Fix `cargo test --no-default-features` ([#5]). ### Improvements - Extended and clarified documentation. - Added more rustdoc examples. - Extended CI to test the crate with `--no-default-features`. ### Breaking Changes - `adler32_reader` now takes its generic argument by value instead of as a `&mut`. - Renamed `adler32_reader` to `adler32`. ## [0.2.3 - 2020-07-11](https://github.com/jonas-schievink/adler/releases/tag/v0.2.3) - Process 4 Bytes at a time, improving performance by up to 50% ([#2]). ## [0.2.2 - 2020-06-27](https://github.com/jonas-schievink/adler/releases/tag/v0.2.2) - Bump MSRV to 1.31.0. ## [0.2.1 - 2020-06-27](https://github.com/jonas-schievink/adler/releases/tag/v0.2.1) - Add a few `#[inline]` annotations to small functions. - Fix CI badge. - Allow integration into libstd. ## [0.2.0 - 2020-06-27](https://github.com/jonas-schievink/adler/releases/tag/v0.2.0) - Support `#![no_std]` when using `default-features = false`. - Improve performance by around 7x. - Support Rust 1.8.0. - Improve API naming. ## [0.1.0 - 2020-06-26](https://github.com/jonas-schievink/adler/releases/tag/v0.1.0) Initial release. [#2]: https://github.com/jonas-schievink/adler/pull/2 [#5]: https://github.com/jonas-schievink/adler/pull/5 adler-1.0.2/Cargo.toml0000644000000037700000000000000101270ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "adler" version = "1.0.2" authors = ["Jonas Schievink "] description = "A simple clean-room implementation of the Adler-32 checksum" documentation = "https://docs.rs/adler/" readme = "README.md" keywords = ["checksum", "integrity", "hash", "adler32", "zlib"] categories = ["algorithms"] license = "0BSD OR MIT OR Apache-2.0" repository = "https://github.com/jonas-schievink/adler.git" [package.metadata.docs.rs] rustdoc-args = ["--cfg=docsrs"] [package.metadata.release] no-dev-version = true pre-release-commit-message = "Release {{version}}" tag-message = "{{version}}" [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" replace = "## Unreleased\n\nNo changes.\n\n## [{{version}} - {{date}}](https://github.com/jonas-schievink/adler/releases/tag/v{{version}})\n" search = "## Unreleased\n" [[package.metadata.release.pre-release-replacements]] file = "README.md" replace = "adler = \"{{version}}\"" search = "adler = \"[a-z0-9\\\\.-]+\"" [[package.metadata.release.pre-release-replacements]] file = "src/lib.rs" replace = "https://docs.rs/adler/{{version}}" search = "https://docs.rs/adler/[a-z0-9\\.-]+" [[bench]] name = "bench" harness = false [dependencies.compiler_builtins] version = "0.1.2" optional = true [dependencies.core] version = "1.0.0" optional = true package = "rustc-std-workspace-core" [dev-dependencies.criterion] version = "0.3.2" [features] default = ["std"] rustc-dep-of-std = ["core", "compiler_builtins"] std = [] adler-1.0.2/Cargo.toml.orig000064400000000000000000000040440000000000000135610ustar 00000000000000[package] name = "adler" version = "1.0.2" authors = ["Jonas Schievink "] description = "A simple clean-room implementation of the Adler-32 checksum" documentation = "https://docs.rs/adler/" repository = "https://github.com/jonas-schievink/adler.git" keywords = ["checksum", "integrity", "hash", "adler32", "zlib"] categories = ["algorithms"] readme = "README.md" license = "0BSD OR MIT OR Apache-2.0" [[bench]] name = "bench" harness = false [dependencies] # Internal features, only used when building as part of libstd, not part of the # stable interface of this crate. core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' } compiler_builtins = { version = '0.1.2', optional = true } [dev-dependencies] criterion = "0.3.2" [features] # Disable default features to enable `#![no_std]` support. default = ["std"] std = [] # Internal feature, only used when building as part of libstd, not part of the # stable interface of this crate. rustc-dep-of-std = ['core', 'compiler_builtins'] [package.metadata.docs.rs] rustdoc-args = [ "--cfg=docsrs" ] # cargo-release configuration [package.metadata.release] tag-message = "{{version}}" no-dev-version = true pre-release-commit-message = "Release {{version}}" # Change the changelog's `Unreleased` section to refer to this release and # prepend a new `Unreleased` section [[package.metadata.release.pre-release-replacements]] file = "CHANGELOG.md" search = "## Unreleased\n" replace = """ ## Unreleased No changes. ## [{{version}} - {{date}}](https://github.com/jonas-schievink/adler/releases/tag/v{{version}}) """ # Bump the version inside the example manifest in `README.md` [[package.metadata.release.pre-release-replacements]] file = "README.md" search = 'adler = "[a-z0-9\\.-]+"' replace = 'adler = "{{version}}"' # Bump the version referenced by the `html_root_url` attribute in `lib.rs` [[package.metadata.release.pre-release-replacements]] file = "src/lib.rs" search = "https://docs.rs/adler/[a-z0-9\\.-]+" replace = "https://docs.rs/adler/{{version}}" adler-1.0.2/LICENSE-0BSD000064400000000000000000000012310000000000000123600ustar 00000000000000Copyright (C) Jonas Schievink Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. adler-1.0.2/LICENSE-APACHE000064400000000000000000000251540000000000000126230ustar 00000000000000 Apache License Version 2.0, January 2004 https://www.apache.org/licenses/LICENSE-2.0 TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. adler-1.0.2/LICENSE-MIT000064400000000000000000000017770000000000000123400ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. adler-1.0.2/README.md000064400000000000000000000022040000000000000121450ustar 00000000000000# Adler-32 checksums for Rust [![crates.io](https://img.shields.io/crates/v/adler.svg)](https://crates.io/crates/adler) [![docs.rs](https://docs.rs/adler/badge.svg)](https://docs.rs/adler/) ![CI](https://github.com/jonas-schievink/adler/workflows/CI/badge.svg) This crate provides a simple implementation of the Adler-32 checksum, used in the zlib compression format. Please refer to the [changelog](CHANGELOG.md) to see what changed in the last releases. ## Features - Permissively licensed (0BSD) clean-room implementation. - Zero dependencies. - Zero `unsafe`. - Decent performance (3-4 GB/s). - Supports `#![no_std]` (with `default-features = false`). ## Usage Add an entry to your `Cargo.toml`: ```toml [dependencies] adler = "1.0.2" ``` Check the [API Documentation](https://docs.rs/adler/) for how to use the crate's functionality. ## Rust version support Currently, this crate supports all Rust versions starting at Rust 1.31.0. Bumping the Minimum Supported Rust Version (MSRV) is *not* considered a breaking change, but will not be done without good reasons. The latest 3 stable Rust versions will always be supported no matter what. adler-1.0.2/RELEASE_PROCESS.md000064400000000000000000000010170000000000000134670ustar 00000000000000# What to do to publish a new release 1. Ensure all notable changes are in the changelog under "Unreleased". 2. Execute `cargo release ` to bump version(s), tag and publish everything. External subcommand, must be installed with `cargo install cargo-release`. `` can be one of `major|minor|patch`. If this is the first release (`0.1.0`), use `minor`, since the version starts out as `0.0.0`. 3. Go to the GitHub releases, edit the just-pushed tag. Copy the release notes from the changelog. adler-1.0.2/benches/bench.rs000064400000000000000000000055660000000000000137400ustar 00000000000000extern crate adler; extern crate criterion; use adler::{adler32_slice, Adler32}; use criterion::{criterion_group, criterion_main, Criterion, Throughput}; fn simple(c: &mut Criterion) { { const SIZE: usize = 100; let mut group = c.benchmark_group("simple-100b"); group.throughput(Throughput::Bytes(SIZE as u64)); group.bench_function("zeroes-100", |bencher| { bencher.iter(|| { adler32_slice(&[0; SIZE]); }); }); group.bench_function("ones-100", |bencher| { bencher.iter(|| { adler32_slice(&[0xff; SIZE]); }); }); } { const SIZE: usize = 1024; let mut group = c.benchmark_group("simple-1k"); group.throughput(Throughput::Bytes(SIZE as u64)); group.bench_function("zeroes-1k", |bencher| { bencher.iter(|| { adler32_slice(&[0; SIZE]); }); }); group.bench_function("ones-1k", |bencher| { bencher.iter(|| { adler32_slice(&[0xff; SIZE]); }); }); } { const SIZE: usize = 1024 * 1024; let mut group = c.benchmark_group("simple-1m"); group.throughput(Throughput::Bytes(SIZE as u64)); group.bench_function("zeroes-1m", |bencher| { bencher.iter(|| { adler32_slice(&[0; SIZE]); }); }); group.bench_function("ones-1m", |bencher| { bencher.iter(|| { adler32_slice(&[0xff; SIZE]); }); }); } } fn chunked(c: &mut Criterion) { const SIZE: usize = 16 * 1024 * 1024; let data = vec![0xAB; SIZE]; let mut group = c.benchmark_group("chunked-16m"); group.throughput(Throughput::Bytes(SIZE as u64)); group.bench_function("5552", |bencher| { bencher.iter(|| { let mut h = Adler32::new(); for chunk in data.chunks(5552) { h.write_slice(chunk); } h.checksum() }); }); group.bench_function("8k", |bencher| { bencher.iter(|| { let mut h = Adler32::new(); for chunk in data.chunks(8 * 1024) { h.write_slice(chunk); } h.checksum() }); }); group.bench_function("64k", |bencher| { bencher.iter(|| { let mut h = Adler32::new(); for chunk in data.chunks(64 * 1024) { h.write_slice(chunk); } h.checksum() }); }); group.bench_function("1m", |bencher| { bencher.iter(|| { let mut h = Adler32::new(); for chunk in data.chunks(1024 * 1024) { h.write_slice(chunk); } h.checksum() }); }); } criterion_group!(benches, simple, chunked); criterion_main!(benches); adler-1.0.2/src/algo.rs000064400000000000000000000120720000000000000127510ustar 00000000000000use crate::Adler32; use std::ops::{AddAssign, MulAssign, RemAssign}; impl Adler32 { pub(crate) fn compute(&mut self, bytes: &[u8]) { // The basic algorithm is, for every byte: // a = (a + byte) % MOD // b = (b + a) % MOD // where MOD = 65521. // // For efficiency, we can defer the `% MOD` operations as long as neither a nor b overflows: // - Between calls to `write`, we ensure that a and b are always in range 0..MOD. // - We use 32-bit arithmetic in this function. // - Therefore, a and b must not increase by more than 2^32-MOD without performing a `% MOD` // operation. // // According to Wikipedia, b is calculated as follows for non-incremental checksumming: // b = n×D1 + (n−1)×D2 + (n−2)×D3 + ... + Dn + n*1 (mod 65521) // Where n is the number of bytes and Di is the i-th Byte. We need to change this to account // for the previous values of a and b, as well as treat every input Byte as being 255: // b_inc = n×255 + (n-1)×255 + ... + 255 + n*65520 // Or in other words: // b_inc = n*65520 + n(n+1)/2*255 // The max chunk size is thus the largest value of n so that b_inc <= 2^32-65521. // 2^32-65521 = n*65520 + n(n+1)/2*255 // Plugging this into an equation solver since I can't math gives n = 5552.18..., so 5552. // // On top of the optimization outlined above, the algorithm can also be parallelized with a // bit more work: // // Note that b is a linear combination of a vector of input bytes (D1, ..., Dn). // // If we fix some value k Self { U32X4([ u32::from(bytes[0]), u32::from(bytes[1]), u32::from(bytes[2]), u32::from(bytes[3]), ]) } } impl AddAssign for U32X4 { fn add_assign(&mut self, other: Self) { for (s, o) in self.0.iter_mut().zip(other.0.iter()) { *s += o; } } } impl RemAssign for U32X4 { fn rem_assign(&mut self, quotient: u32) { for s in self.0.iter_mut() { *s %= quotient; } } } impl MulAssign for U32X4 { fn mul_assign(&mut self, rhs: u32) { for s in self.0.iter_mut() { *s *= rhs; } } } adler-1.0.2/src/lib.rs000064400000000000000000000172040000000000000125770ustar 00000000000000//! Adler-32 checksum implementation. //! //! This implementation features: //! //! - Permissively licensed (0BSD) clean-room implementation. //! - Zero dependencies. //! - Zero `unsafe`. //! - Decent performance (3-4 GB/s). //! - `#![no_std]` support (with `default-features = false`). #![doc(html_root_url = "https://docs.rs/adler/1.0.2")] // Deny a few warnings in doctests, since rustdoc `allow`s many warnings by default #![doc(test(attr(deny(unused_imports, unused_must_use))))] #![cfg_attr(docsrs, feature(doc_cfg))] #![warn(missing_debug_implementations)] #![forbid(unsafe_code)] #![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] extern crate core as std; mod algo; use std::hash::Hasher; #[cfg(feature = "std")] use std::io::{self, BufRead}; /// Adler-32 checksum calculator. /// /// An instance of this type is equivalent to an Adler-32 checksum: It can be created in the default /// state via [`new`] (or the provided `Default` impl), or from a precalculated checksum via /// [`from_checksum`], and the currently stored checksum can be fetched via [`checksum`]. /// /// This type also implements `Hasher`, which makes it easy to calculate Adler-32 checksums of any /// type that implements or derives `Hash`. This also allows using Adler-32 in a `HashMap`, although /// that is not recommended (while every checksum is a hash function, they are not necessarily a /// good one). /// /// # Examples /// /// Basic, piecewise checksum calculation: /// /// ``` /// use adler::Adler32; /// /// let mut adler = Adler32::new(); /// /// adler.write_slice(&[0, 1, 2]); /// adler.write_slice(&[3, 4, 5]); /// /// assert_eq!(adler.checksum(), 0x00290010); /// ``` /// /// Using `Hash` to process structures: /// /// ``` /// use std::hash::Hash; /// use adler::Adler32; /// /// #[derive(Hash)] /// struct Data { /// byte: u8, /// word: u16, /// big: u64, /// } /// /// let mut adler = Adler32::new(); /// /// let data = Data { byte: 0x1F, word: 0xABCD, big: !0 }; /// data.hash(&mut adler); /// /// // hash value depends on architecture endianness /// if cfg!(target_endian = "little") { /// assert_eq!(adler.checksum(), 0x33410990); /// } /// if cfg!(target_endian = "big") { /// assert_eq!(adler.checksum(), 0x331F0990); /// } /// /// ``` /// /// [`new`]: #method.new /// [`from_checksum`]: #method.from_checksum /// [`checksum`]: #method.checksum #[derive(Debug, Copy, Clone)] pub struct Adler32 { a: u16, b: u16, } impl Adler32 { /// Creates a new Adler-32 instance with default state. #[inline] pub fn new() -> Self { Self::default() } /// Creates an `Adler32` instance from a precomputed Adler-32 checksum. /// /// This allows resuming checksum calculation without having to keep the `Adler32` instance /// around. /// /// # Example /// /// ``` /// # use adler::Adler32; /// let parts = [ /// "rust", /// "acean", /// ]; /// let whole = adler::adler32_slice(b"rustacean"); /// /// let mut sum = Adler32::new(); /// sum.write_slice(parts[0].as_bytes()); /// let partial = sum.checksum(); /// /// // ...later /// /// let mut sum = Adler32::from_checksum(partial); /// sum.write_slice(parts[1].as_bytes()); /// assert_eq!(sum.checksum(), whole); /// ``` #[inline] pub fn from_checksum(sum: u32) -> Self { Adler32 { a: sum as u16, b: (sum >> 16) as u16, } } /// Returns the calculated checksum at this point in time. #[inline] pub fn checksum(&self) -> u32 { (u32::from(self.b) << 16) | u32::from(self.a) } /// Adds `bytes` to the checksum calculation. /// /// If efficiency matters, this should be called with Byte slices that contain at least a few /// thousand Bytes. pub fn write_slice(&mut self, bytes: &[u8]) { self.compute(bytes); } } impl Default for Adler32 { #[inline] fn default() -> Self { Adler32 { a: 1, b: 0 } } } impl Hasher for Adler32 { #[inline] fn finish(&self) -> u64 { u64::from(self.checksum()) } fn write(&mut self, bytes: &[u8]) { self.write_slice(bytes); } } /// Calculates the Adler-32 checksum of a byte slice. /// /// This is a convenience function around the [`Adler32`] type. /// /// [`Adler32`]: struct.Adler32.html pub fn adler32_slice(data: &[u8]) -> u32 { let mut h = Adler32::new(); h.write_slice(data); h.checksum() } /// Calculates the Adler-32 checksum of a `BufRead`'s contents. /// /// The passed `BufRead` implementor will be read until it reaches EOF (or until it reports an /// error). /// /// If you only have a `Read` implementor, you can wrap it in `std::io::BufReader` before calling /// this function. /// /// # Errors /// /// Any error returned by the reader are bubbled up by this function. /// /// # Examples /// /// ```no_run /// # fn run() -> Result<(), Box> { /// use adler::adler32; /// /// use std::fs::File; /// use std::io::BufReader; /// /// let file = File::open("input.txt")?; /// let mut file = BufReader::new(file); /// /// adler32(&mut file)?; /// # Ok(()) } /// # fn main() { run().unwrap() } /// ``` #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] pub fn adler32(mut reader: R) -> io::Result { let mut h = Adler32::new(); loop { let len = { let buf = reader.fill_buf()?; if buf.is_empty() { return Ok(h.checksum()); } h.write_slice(buf); buf.len() }; reader.consume(len); } } #[cfg(test)] mod tests { use super::*; #[test] fn zeroes() { assert_eq!(adler32_slice(&[]), 1); assert_eq!(adler32_slice(&[0]), 1 | 1 << 16); assert_eq!(adler32_slice(&[0, 0]), 1 | 2 << 16); assert_eq!(adler32_slice(&[0; 100]), 0x00640001); assert_eq!(adler32_slice(&[0; 1024]), 0x04000001); assert_eq!(adler32_slice(&[0; 1024 * 1024]), 0x00f00001); } #[test] fn ones() { assert_eq!(adler32_slice(&[0xff; 1024]), 0x79a6fc2e); assert_eq!(adler32_slice(&[0xff; 1024 * 1024]), 0x8e88ef11); } #[test] fn mixed() { assert_eq!(adler32_slice(&[1]), 2 | 2 << 16); assert_eq!(adler32_slice(&[40]), 41 | 41 << 16); assert_eq!(adler32_slice(&[0xA5; 1024 * 1024]), 0xd5009ab1); } /// Example calculation from https://en.wikipedia.org/wiki/Adler-32. #[test] fn wiki() { assert_eq!(adler32_slice(b"Wikipedia"), 0x11E60398); } #[test] fn resume() { let mut adler = Adler32::new(); adler.write_slice(&[0xff; 1024]); let partial = adler.checksum(); assert_eq!(partial, 0x79a6fc2e); // from above adler.write_slice(&[0xff; 1024 * 1024 - 1024]); assert_eq!(adler.checksum(), 0x8e88ef11); // from above // Make sure that we can resume computing from the partial checksum via `from_checksum`. let mut adler = Adler32::from_checksum(partial); adler.write_slice(&[0xff; 1024 * 1024 - 1024]); assert_eq!(adler.checksum(), 0x8e88ef11); // from above } #[cfg(feature = "std")] #[test] fn bufread() { use std::io::BufReader; fn test(data: &[u8], checksum: u32) { // `BufReader` uses an 8 KB buffer, so this will test buffer refilling. let mut buf = BufReader::new(data); let real_sum = adler32(&mut buf).unwrap(); assert_eq!(checksum, real_sum); } test(&[], 1); test(&[0; 1024], 0x04000001); test(&[0; 1024 * 1024], 0x00f00001); test(&[0xA5; 1024 * 1024], 0xd5009ab1); } }