khash/
c.rs

1//! FFI exported functions
2use super::*;
3
4
5/// Calculate the length in bytes of a kana hash output.
6///
7/// # Note
8/// Does not consume `salt`
9#[no_mangle]
10pub unsafe extern "C" fn khash_length(context: *const c_void, bin: *const c_void, sz: size_t, out_len: *mut size_t) -> i32
11{
12    let context = context as *const ctx::CContext;
13    no_unwind!{
14	try error::Error::Unknown;
15	let context = ctx::Context::clone_from_raw(context);
16	let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
17	let string = c_try!(generate(&context, &bin));
18	*out_len = string.bytes().len().into();
19
20	GENERIC_SUCCESS
21    }
22}
23
24/// Compute and write a kana hash output to a string.
25///
26/// # Note
27/// Consumes `salt`
28#[no_mangle]
29pub unsafe extern "C" fn khash_do(context: *mut c_void, bin: *const c_void, sz: size_t, out_str: *mut c_char, str_len: size_t) -> i32
30{
31    let context = context as *mut ctx::CContext;
32    no_unwind!{
33	try error::Error::Unknown;
34	
35	let context = ctx::Context::from_raw(context);
36	let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
37	let string: Vec<u8> = c_try!(generate(&context, &bin)).bytes().collect();
38	
39	libc::memcpy(out_str as *mut c_void, &string[0] as *const u8 as *const c_void, std::cmp::min(str_len, string.len()));
40	
41	GENERIC_SUCCESS
42    }
43}
44
45/// Free a context
46#[no_mangle]
47pub unsafe extern "C" fn khash_free_context(context: *mut c_void) -> i32
48{
49    let context = context as *mut ctx::CContext;
50    no_unwind!{
51	drop(ctx::Context::from_raw(context));
52	GENERIC_SUCCESS
53    }
54}
55
56/// Create a new context
57#[no_mangle]
58pub unsafe extern "C" fn khash_new_context(algo: u8, salt_type: u8, bin: *const c_void, sz: size_t, nptr: *mut c_void) -> i32
59{
60    let nptr = nptr as *mut ctx::CContext;
61    no_unwind!{
62	try error::Error::Unknown;
63	let salt = match salt_type {
64	    salt::SALT_TYPE_SPECIFIC => {
65		let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
66		salt::Salt::unfixed(&bin[..])
67	    },
68	    salt::SALT_TYPE_DEFAULT => {
69		salt::Salt::default()
70	    },
71	    salt::SALT_TYPE_RANDOM => {
72		match salt::Salt::random() {
73		    Ok(v) => v,
74		    Err(e) => return i32::from(error::Error::RNG(e)),
75		}
76	    },
77	    _ => {
78		salt::Salt::None
79	    },
80	};
81	let context = ctx::Context::new(algo.into(), salt);
82	*nptr = context.into_raw();
83	GENERIC_SUCCESS
84    }
85}
86
87
88/// Clone a context
89#[no_mangle]
90pub unsafe extern "C" fn khash_clone_context(raw: *const c_void, out: *mut c_void) -> i32
91{
92    let raw = raw as *const ctx::CContext;
93    let out = out as *mut ctx::CContext;
94    no_unwind!{
95	*out = ctx::Context::clone_from_raw(raw).into_raw();
96	GENERIC_SUCCESS
97    }   
98}
99
100/// Free a salt allocated with `khash_new_salt`
101#[no_mangle]
102pub unsafe extern "C" fn khash_free_salt(salt: *mut c_void) -> i32
103{
104    let salt = salt as *mut salt::FFI;
105    no_unwind!{
106	drop(salt::from_raw(salt));
107	GENERIC_SUCCESS
108    }
109}
110
111/// Create a new salt
112#[no_mangle]
113pub unsafe extern "C" fn khash_new_salt(salt_type: u8, bin: *const c_void, sz: size_t, nptr: *mut c_void) -> i32
114{
115    let nptr = nptr as *mut salt::FFI;
116    no_unwind!{
117	try error::Error::Unknown;
118	match salt_type {
119	    salt::SALT_TYPE_SPECIFIC => {
120		let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
121		*nptr = salt::into_raw(salt::Salt::unfixed(&bin[..]));
122	    },
123	    salt::SALT_TYPE_DEFAULT => {
124		*nptr = salt::into_raw(salt::Salt::default());
125	    },
126	    salt::SALT_TYPE_RANDOM => {
127		*nptr = salt::into_raw(match salt::Salt::random() {
128		    Ok(v) => v,
129		    Err(e) => return i32::from(error::Error::RNG(e)),
130		})
131	    },
132	    _ => {
133		*nptr = salt::into_raw(salt::Salt::None);
134	    },
135	}
136	GENERIC_SUCCESS
137    }
138}
139
140/// Clone a salt
141#[no_mangle]
142pub unsafe extern "C" fn khash_clone_salt(salt: *const c_void, out: *mut c_void) -> i32
143{
144    let salt = salt as *const salt::FFI;
145    let out = out as *mut salt::FFI;
146    no_unwind!{
147	*out = salt::into_raw(salt::clone_from_raw(salt));
148	GENERIC_SUCCESS
149    }   
150}
151
152/// Find the maximum length possible for a given algorithm's output.
153#[no_mangle]
154pub unsafe extern "C" fn khash_max_length(algo: u8, _input_sz: libc::size_t, max_len: *mut libc::size_t) -> i32
155{
156    no_unwind!{
157	let hash_sz = match ctx::Algorithm::from(algo) {
158	    #[cfg(feature="crc")] ctx::Algorithm::Crc32 => std::mem::size_of::<hash::Crc32Checksum>(),
159	    #[cfg(feature="crc")] ctx::Algorithm::Crc64 => std::mem::size_of::<hash::Crc64Checksum>(),
160	    ctx::Algorithm::Sha256 => std::mem::size_of::<hash::Sha256Hash>(),
161	    ctx::Algorithm::Sha256Truncated => std::mem::size_of::<hash::Sha256Truncated>(),
162	};
163	*max_len =  std::mem::size_of::<char>() * hash_sz;
164	GENERIC_SUCCESS
165    }
166}