feature gated ffi deps and CRC support

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

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

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

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

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

@ -1,4 +1,5 @@
use malloc_array::*;
#[cfg(feature="ffi")] use malloc_array::*;
use getrandom::{
getrandom,
Error,
@ -104,8 +105,9 @@ pub(crate) const SALT_TYPE_RANDOM: u8 = 3;
/// We won't try to copy more than this much data.
const MAX_FFI_SALT_SIZE: usize = 1024;
/// 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;
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`.
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 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.
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
{
@ -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()
}

Loading…
Cancel
Save