#[cfg(all(feature = "rustsha1", not(feature = "fast-sha1")))]
mod _impl {
use super::Sha1Digest;
#[derive(Default, Clone)]
pub struct Sha1(sha1_smol::Sha1);
impl Sha1 {
pub fn update(&mut self, bytes: &[u8]) {
self.0.update(bytes)
}
pub fn digest(self) -> Sha1Digest {
self.0.digest().bytes()
}
}
}
#[cfg(any(feature = "fast-sha1", feature = "rustsha1"))]
pub type Sha1Digest = [u8; 20];
#[cfg(feature = "fast-sha1")]
mod _impl {
use sha1::Digest;
use super::Sha1Digest;
#[derive(Default, Clone)]
pub struct Sha1(sha1::Sha1);
impl Sha1 {
pub fn update(&mut self, bytes: &[u8]) {
self.0.update(bytes)
}
pub fn digest(self) -> Sha1Digest {
self.0.finalize().into()
}
}
}
#[cfg(any(feature = "rustsha1", feature = "fast-sha1"))]
pub use _impl::Sha1;
#[cfg(feature = "crc32")]
pub fn crc32_update(previous_value: u32, bytes: &[u8]) -> u32 {
let mut h = crc32fast::Hasher::new_with_initial(previous_value);
h.update(bytes);
h.finalize()
}
#[cfg(feature = "crc32")]
pub fn crc32(bytes: &[u8]) -> u32 {
let mut h = crc32fast::Hasher::new();
h.update(bytes);
h.finalize()
}
#[cfg(any(feature = "rustsha1", feature = "fast-sha1"))]
pub fn hasher(kind: git_hash::Kind) -> Sha1 {
match kind {
git_hash::Kind::Sha1 => Sha1::default(),
}
}
#[cfg(all(feature = "progress", any(feature = "rustsha1", feature = "fast-sha1")))]
pub fn bytes_of_file(
path: impl AsRef<std::path::Path>,
num_bytes_from_start: usize,
kind: git_hash::Kind,
progress: &mut impl crate::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> std::io::Result<git_hash::ObjectId> {
bytes(
std::fs::File::open(path)?,
num_bytes_from_start,
kind,
progress,
should_interrupt,
)
}
#[cfg(all(feature = "progress", any(feature = "rustsha1", feature = "fast-sha1")))]
pub fn bytes(
mut read: impl std::io::Read,
num_bytes_from_start: usize,
kind: git_hash::Kind,
progress: &mut impl crate::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> std::io::Result<git_hash::ObjectId> {
let mut hasher = hasher(kind);
let start = std::time::Instant::now();
progress.init(Some(num_bytes_from_start), crate::progress::bytes());
const BUF_SIZE: usize = u16::MAX as usize;
let mut buf = [0u8; BUF_SIZE];
let mut bytes_left = num_bytes_from_start;
while bytes_left > 0 {
let out = &mut buf[..BUF_SIZE.min(bytes_left)];
read.read_exact(out)?;
bytes_left -= out.len();
progress.inc_by(out.len());
hasher.update(out);
if should_interrupt.load(std::sync::atomic::Ordering::SeqCst) {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Interrupted"));
}
}
let id = git_hash::ObjectId::from(hasher.digest());
progress.show_throughput(start);
Ok(id)
}
#[cfg(any(feature = "rustsha1", feature = "fast-sha1"))]
mod write {
use crate::hash::Sha1;
pub struct Write<T> {
pub hash: Sha1,
pub inner: T,
}
impl<T> std::io::Write for Write<T>
where
T: std::io::Write,
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let written = self.inner.write(buf)?;
self.hash.update(&buf[..written]);
Ok(written)
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}
impl<T> Write<T>
where
T: std::io::Write,
{
pub fn new(inner: T, object_hash: git_hash::Kind) -> Self {
match object_hash {
git_hash::Kind::Sha1 => Write {
inner,
hash: Sha1::default(),
},
}
}
}
}
#[cfg(any(feature = "rustsha1", feature = "fast-sha1"))]
pub use write::Write;