Skip to content
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

Update crc requirement from 1 to 3 #14

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Dan Burkert <[email protected]>", "Andrey Vasnetsov <andrey@vasnets

[dependencies]
byteorder = "1.4"
crc = "1"
crc = "3"
eventual = "0.1.4"
fs2 = "0.4"
log = "0.4"
Expand Down
24 changes: 9 additions & 15 deletions src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::ptr;
use std::thread;

use byteorder::{ByteOrder, LittleEndian};
use crc::crc32;
use crc::{Crc, CRC_32_ISCSI};
use eventual::Future;
use fs2::FileExt;
use log;
Expand All @@ -26,7 +26,7 @@ const HEADER_LEN: usize = 8;
/// The length of a CRC value.
const CRC_LEN: usize = 4;

type Crc = u32;
pub const CASTAGNOLI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);

pub struct Entry {
view: MmapViewSync,
Expand Down Expand Up @@ -107,7 +107,7 @@ pub struct Segment {
/// Index of entry offset and lengths.
index: Vec<(usize, usize)>,
/// The crc of the last appended entry.
crc: Crc,
crc: u32,
/// Offset of last flush.
flush_offset: usize,
}
Expand Down Expand Up @@ -222,12 +222,9 @@ impl Segment {
if offset + HEADER_LEN + padded_len + CRC_LEN > capacity {
break;
}

let entry_crc = crc32::update(
crc,
&crc32::CASTAGNOLI_TABLE,
&segment[offset..offset + HEADER_LEN + padded_len],
);
let mut digest = CASTAGNOLI.digest_with_initial(crc);
digest.update(&segment[offset..offset + HEADER_LEN + padded_len]);
let entry_crc = digest.finalize();
if entry_crc != LittleEndian::read_u32(&segment[offset + HEADER_LEN + padded_len..])
{
break;
Expand Down Expand Up @@ -300,6 +297,7 @@ impl Segment {
let offset = self.size();

let mut crc = self.crc;
let mut digest = CASTAGNOLI.digest_with_initial(crc);

LittleEndian::write_u64(&mut self.as_mut_slice()[offset..], entry.len() as u64);
copy_memory(
Expand All @@ -314,12 +312,8 @@ impl Segment {
&mut self.as_mut_slice()[offset + HEADER_LEN + entry.len()..],
);
}

crc = crc32::update(
crc,
&crc32::CASTAGNOLI_TABLE,
&self.as_slice()[offset..offset + HEADER_LEN + padded_len],
);
digest.update(&self.as_slice()[offset..offset + HEADER_LEN + padded_len]);
crc = digest.finalize();

LittleEndian::write_u32(
&mut self.as_mut_slice()[offset + HEADER_LEN + padded_len..],
Expand Down