1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! ## Example
//!
//! ```
//! use adler32fast::Adler32;
//!
//! let mut adler32 = Adler32::new();
//! adler32.update(b"foo bar baz");
//! let checksum = adler32.as_u32();
//! ```
//!
//! ## Performance
//!
//! This crate contains multiple Adler-32 implementations:
//!
//! - A fast baseline implementation which processes up to 16 bytes per iteration
//! - An optimized implementation for modern `x86`/`x86_64` using SSE instructions
//!
//! Calling the `Adler32::new`/`Adler32::from` constructors at runtime will perform a feature
//! detection to select the most optimal implementation for the current CPU feature set.
#![cfg_attr(not(any(feature = "std", test)), no_std)]

// These are exported for benchmarking and fuzzing; not part of the API.
#[doc(hidden)]
pub mod baseline;
#[doc(hidden)]
pub mod specialized;

#[cfg(not(feature = "std"))]
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::hash::Hasher;

const DEFAULT_INIT_STATE: u32 = 1;

#[derive(Copy, Clone, Debug)]
enum State {
    Baseline(baseline::State),
    Specialized(specialized::State),
}

#[derive(Copy, Clone, Debug)]
/// Represents an in-progress Adler-32 computation.
pub struct Adler32 {
    state: State,
}

impl Adler32 {
    /// Create a new `Adler32`.
    ///
    /// This will perform a CPU feature detection at runtime to select the most
    /// optimal implementation for the current processor architecture.
    pub fn new() -> Self {
        Self::from(DEFAULT_INIT_STATE)
    }

    /// Return the computed Adler-32 value.
    pub fn as_u32(&self) -> u32 {
        match self.state {
            State::Baseline(state) => state.finalize(),
            State::Specialized(state) => state.finalize(),
        }
    }

    /// Indicates whether the current implementation is SIMD-accelerated.
    pub fn is_simd_enabled(&self) -> bool {
        match self.state {
            State::Specialized(_) => true,
            _ => false,
        }
    }

    /// Reset the hash state.
    pub fn reset(&mut self) {
        match self.state {
            State::Baseline(ref mut state) => state.reset(),
            State::Specialized(ref mut state) => state.reset(),
        }
    }

    /// Process the given byte slice and update the hash state.
    pub fn update(&mut self, buf: &[u8]) {
        match self.state {
            State::Baseline(ref mut state) => state.update(buf),
            State::Specialized(ref mut state) => state.update(buf),
        }
    }

    fn internal_new_baseline(initial: u32) -> Self {
        Self {
            state: State::Baseline(baseline::State::new(initial)),
        }
    }

    #[doc(hidden)]
    fn internal_new_specialized(initial: u32) -> Option<Self> {
        specialized::State::new(initial).map(|state| Self {
            state: State::Specialized(state),
        })
    }
}

impl Default for Adler32 {
    fn default() -> Self {
        Self::new()
    }
}

impl From<u32> for Adler32 {
    fn from(initial: u32) -> Self {
        Self::internal_new_specialized(initial)
            .unwrap_or_else(|| Self::internal_new_baseline(initial))
    }
}

impl Hasher for Adler32 {
    fn finish(&self) -> u64 {
        u64::from(self.as_u32())
    }

    fn write(&mut self, bytes: &[u8]) {
        self.update(bytes);
    }
}

impl PartialEq<u32> for Adler32 {
    fn eq(&self, &other: &u32) -> bool {
        self.as_u32() == other
    }
}