feature gated ffi deps and CRC support

master
Avril 4 years ago
parent b701c246f5
commit 1786ca4f88
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,7 +1,7 @@
[package] [package]
name = "khash" name = "khash"
description = "Kana hashes" description = "Kana hashes"
version = "2.0.2" version = "2.0.3"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018" edition = "2018"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
@ -11,9 +11,9 @@ license = "GPL-3.0-or-later"
crate-type = ["rlib", "cdylib", "staticlib"] crate-type = ["rlib", "cdylib", "staticlib"]
[features] [features]
default = ["ffi"] default = ["ffi", "crc"]
ffi = [] ffi = ["malloc-array"]
[profile.release] [profile.release]
opt-level = 3 opt-level = 3
@ -23,9 +23,9 @@ panic = "unwind"
[dependencies] [dependencies]
sha2 = "0.9" sha2 = "0.9"
malloc-array = "1.4" malloc-array = {version = "1.4", optional=true}
libc = "0.2" libc = "0.2"
crc = "1.8" crc = {version = "1.8", optional=true}
hex-literal = "0.2" hex-literal = "0.2"
getrandom = "0.1" getrandom = "0.1"

@ -10,7 +10,9 @@ use std::{
#[derive(Clone,Debug,PartialEq,Eq,Hash)] #[derive(Clone,Debug,PartialEq,Eq,Hash)]
pub enum Algorithm pub enum Algorithm
{ {
#[cfg(feature="crc")]
Crc32, Crc32,
#[cfg(feature="crc")]
Crc64, Crc64,
Sha256, Sha256,
Sha256Truncated, Sha256Truncated,
@ -67,8 +69,8 @@ impl Context
let mut output = 0usize; let mut output = 0usize;
let bytes = match self.algo let bytes = match self.algo
{ {
Algorithm::Crc32 => provide::<hash::Crc32Checksum, _>(&mut from, &self.salt, &mut output)?, #[cfg(feature="crc")] Algorithm::Crc32 => provide::<hash::Crc32Checksum, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Crc64 => provide::<hash::Crc64Checksum, _>(&mut from, &self.salt, &mut output)?, #[cfg(feature="crc")] Algorithm::Crc64 => provide::<hash::Crc64Checksum, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Sha256 => provide::<hash::Sha256Hash, _>(&mut from, &self.salt, &mut output)?, Algorithm::Sha256 => provide::<hash::Sha256Hash, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Sha256Truncated => provide::<hash::Sha256Truncated, _>(&mut from, &self.salt, &mut output)?, Algorithm::Sha256Truncated => provide::<hash::Sha256Truncated, _>(&mut from, &self.salt, &mut output)?,
}.into_boxed_slice(); }.into_boxed_slice();
@ -76,6 +78,7 @@ impl Context
Ok((output, bytes)) Ok((output, bytes))
} }
#[cfg(feature="ffi")]
pub(crate) unsafe fn into_raw(self) -> CContext pub(crate) unsafe fn into_raw(self) -> CContext
{ {
CContext{ CContext{
@ -85,6 +88,7 @@ impl Context
} }
} }
#[cfg(feature="ffi")]
pub(crate) unsafe fn clone_from_raw(from: *const CContext) -> Self pub(crate) unsafe fn clone_from_raw(from: *const CContext) -> Self
{ {
let from = &*from; let from = &*from;
@ -94,6 +98,7 @@ impl Context
} }
} }
#[cfg(feature="ffi")]
pub(crate) unsafe fn from_raw(from: *mut CContext) -> Self pub(crate) unsafe fn from_raw(from: *mut CContext) -> Self
{ {
let from = &mut *from; let from = &mut *from;
@ -139,8 +144,8 @@ impl From<Algorithm> for u8
fn from(al: Algorithm) -> Self fn from(al: Algorithm) -> Self
{ {
match al { match al {
Algorithm::Crc32 => ALGO_CRC32, #[cfg(feature="crc")] Algorithm::Crc32 => ALGO_CRC32,
Algorithm::Crc64 => ALGO_CRC64, #[cfg(feature="crc")] Algorithm::Crc64 => ALGO_CRC64,
Algorithm::Sha256 => ALGO_SHA256, Algorithm::Sha256 => ALGO_SHA256,
Algorithm::Sha256Truncated => ALGO_SHA256_TRUNCATED, Algorithm::Sha256Truncated => ALGO_SHA256_TRUNCATED,
} }
@ -151,8 +156,8 @@ impl From<u8> for Algorithm
fn from(al: u8) -> Self fn from(al: u8) -> Self
{ {
match al { match al {
ALGO_CRC32 => Algorithm::Crc32, #[cfg(feature="crc")] ALGO_CRC32 => Algorithm::Crc32,
ALGO_CRC64 => Algorithm::Crc64, #[cfg(feature="crc")] ALGO_CRC64 => Algorithm::Crc64,
ALGO_SHA256 => Algorithm::Sha256, ALGO_SHA256 => Algorithm::Sha256,
ALGO_SHA256_TRUNCATED => Algorithm::Sha256Truncated, ALGO_SHA256_TRUNCATED => Algorithm::Sha256Truncated,
_ => Self::default(), _ => Self::default(),

@ -1,26 +1,29 @@
use crate::*; use crate::*;
pub trait HeapArrayExt: Sized #[cfg(feature="ffi")]
{ const _:() = {
fn into_unsafe(self) -> Self; pub trait HeapArrayExt: Sized
fn into_safe(self) -> Self;
fn set_unsafe(self, un: bool) -> Self;
}
impl<T> HeapArrayExt for HeapArray<T>
{
fn into_unsafe(mut self) -> Self
{ {
self.drop_check = false; fn into_unsafe(self) -> Self;
self fn into_safe(self) -> Self;
fn set_unsafe(self, un: bool) -> Self;
} }
fn into_safe(mut self) -> Self impl<T> HeapArrayExt for HeapArray<T>
{ {
self.drop_check = true; fn into_unsafe(mut self) -> Self
self {
self.drop_check = false;
self
}
fn into_safe(mut self) -> Self
{
self.drop_check = true;
self
}
fn set_unsafe(mut self, un: bool) -> Self
{
self.drop_check = un;
self
}
} }
fn set_unsafe(mut self, un: bool) -> Self };
{
self.drop_check = un;
self
}
}

@ -3,10 +3,14 @@ use crate::*;
mod sha256; mod sha256;
pub use sha256::*; pub use sha256::*;
#[cfg(feature="crc")]
mod crc64; mod crc64;
#[cfg(feature="crc")]
pub use crc64::*; pub use crc64::*;
#[cfg(feature="crc")]
mod crc32; mod crc32;
#[cfg(feature="crc")]
pub use crc32::*; pub use crc32::*;
mod sha256t; mod sha256t;

@ -1,5 +1,6 @@
#![cfg_attr(nightly, feature(test))] #![cfg_attr(nightly, feature(test))]
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused_imports)]
#[cfg(nightly)] extern crate test; #[cfg(nightly)] extern crate test;
@ -10,7 +11,7 @@ use std::{
fmt::Write, fmt::Write,
}; };
type HASHER = hash::Crc64Checksum; //type HASHER = hash::Crc64Checksum; //was unused?
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -145,7 +146,9 @@ mod tests {
} }
} }
#[test] #[test]
#[cfg(feature="ffi")]
fn max_len() fn max_len()
{ {
fn max_length(algo: ctx::Algorithm, data_len: usize) -> usize fn max_length(algo: ctx::Algorithm, data_len: usize) -> usize
@ -239,6 +242,7 @@ use libc::{
c_char, c_char,
}; };
#[cfg(feature="ffi")]
use malloc_array::{ use malloc_array::{
HeapArray, HeapArray,
}; };
@ -246,7 +250,6 @@ use malloc_array::{
// FFI section // FFI section
#[cfg(feature="ffi")] #[cfg(feature="ffi")]
mod c; mod c;
#[cfg(feature="ffi")] #[cfg(feature="ffi")]

@ -1,4 +1,5 @@
use malloc_array::*; #[cfg(feature="ffi")] use malloc_array::*;
use getrandom::{ use getrandom::{
getrandom, getrandom,
Error, Error,
@ -104,8 +105,9 @@ pub(crate) const SALT_TYPE_RANDOM: u8 = 3;
/// We won't try to copy more than this much data. /// We won't try to copy more than this much data.
const MAX_FFI_SALT_SIZE: usize = 1024; const MAX_FFI_SALT_SIZE: usize = 1024;
/// Clone a new `Salt` from an `FFI` salt. /// Clone a new `Salt` from an `FFI` salt.
pub(crate) unsafe fn clone_from_raw(ptr: *const FFI) -> Salt #[cfg(feature="ffi")] pub(crate) unsafe fn clone_from_raw(ptr: *const FFI) -> Salt
{ {
let ffi = &*ptr; let ffi = &*ptr;
match ffi.salt_type { match ffi.salt_type {
@ -119,7 +121,7 @@ pub(crate) unsafe fn clone_from_raw(ptr: *const FFI) -> Salt
} }
} }
/// Consume an `FFI` salt and return a `Salt`. /// Consume an `FFI` salt and return a `Salt`.
pub(crate) unsafe fn from_raw(ptr: *mut FFI) -> Salt #[cfg(feature="ffi")] pub(crate) unsafe fn from_raw(ptr: *mut FFI) -> Salt
{ {
let ffi = &mut *ptr; let ffi = &mut *ptr;
let out = match ffi.salt_type { let out = match ffi.salt_type {
@ -138,7 +140,7 @@ pub(crate) unsafe fn from_raw(ptr: *mut FFI) -> Salt
} }
/// Consume a `Salt` and output a new `FFI` salt. /// Consume a `Salt` and output a new `FFI` salt.
pub(crate) unsafe fn into_raw(salt: Salt) -> FFI #[cfg(feature="ffi")] pub(crate) unsafe fn into_raw(salt: Salt) -> FFI
{ {
unsafe fn allocate(slice: impl AsRef<[u8]>) -> FFI unsafe fn allocate(slice: impl AsRef<[u8]>) -> FFI
{ {
@ -166,8 +168,7 @@ pub(crate) unsafe fn into_raw(salt: Salt) -> FFI
} }
} }
fn box_with_malloc(slice: impl AsRef<[u8]>) -> (*mut u8, usize) #[cfg(feature="ffi")] fn box_with_malloc(slice: impl AsRef<[u8]>) -> (*mut u8, usize)
{ {
unsafe { HeapArray::from_slice_copied(slice) }.into_raw_parts() unsafe { HeapArray::from_slice_copied(slice) }.into_raw_parts()
} }

Loading…
Cancel
Save