master
Avril 5 years ago
parent 70cd056fda
commit c42dde9ee9
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,10 +1,13 @@
all: uninstall build install
install: install:
cp -f libsrng.so /usr/local/lib/libsrng.so cp -f libsrng.so /usr/local/lib/libsrng.so
ln -sf /usr/local/lib/libsrng.so /usr/lib/libsrng.so ln -sf /usr/local/lib/libsrng.so /usr/lib/libsrng.so
uninstall: uninstall:
rm -f /usr/local/lib/libsrng.so rm -f /usr/local/lib/libsrng.so
rm /usr/lib/libsrng.so rm -f /usr/lib/libsrng.so
build: build:
cd libsrng && cargo build --release cd libsrng && cargo build --release

@ -30,6 +30,15 @@
(apply '%crandom-list (cons range params)) (apply '%crandom-list (cons range params))
(apply '%crandom-vector (cons range params)))) (apply '%crandom-vector (cons range params))))
(defun crandom-bytes (len &key (transform #'identity) (type :vector))
(multiple-value-bind (vec ok) (cl-rng-ffi:ffi-bytes len)
(and ok
(if (eq type :vector)
(progn
(loop for i from 0 below len do (setf (aref vec i) (funcall transform (aref vec i))))
vec)
(loop for i from 0 below len collect (funcall transform (aref vec i)))))))
(export 'crandom) (export 'crandom)
(export 'crandom-range) (export 'crandom-range)
(export 'crandom-bytes)

@ -8,6 +8,9 @@
(use-foreign-library libsrng) (use-foreign-library libsrng)
(defcfun "sample" :int (to :pointer)) (defcfun "sample" :int (to :pointer))
(defcfun "bytes" :int
(to :pointer)
(size :int))
(defun ffi-sample () (defun ffi-sample ()
(with-foreign-pointer (value 8) (with-foreign-pointer (value 8)
@ -16,4 +19,16 @@
(mem-ref value :double 0) (mem-ref value :double 0)
(= rval 1))))) (= rval 1)))))
(defun ffi-bytes (size)
(with-foreign-pointer (value size)
(let ((rval (bytes value size))
(output (make-array size)))
(and (= rval 1)
(loop for x from 0 below size
do (setf (aref output x) (mem-aref value :unsigned-char x))))
(values
output
(= rval 1)))))
(export 'ffi-sample) (export 'ffi-sample)
(export 'ffi-bytes)

Binary file not shown.

Binary file not shown.

@ -10,6 +10,13 @@ mod tests {
let mut f: f64 = 0.0; let mut f: f64 = 0.0;
assert_eq!(sample(&mut f as *mut f64), 1); assert_eq!(sample(&mut f as *mut f64), 1);
} }
#[test]
fn bytes_works() {
let mut buf: [u8; 16] = [0; 16];
assert_eq!(bytes(&mut buf[0] as *mut u8, 16), 1);
}
} }
fn get<T: Default>() -> Result<T, Error> fn get<T: Default>() -> Result<T, Error>
@ -54,3 +61,22 @@ pub extern "C" fn sample(value: *mut f64) -> i32
} }
} }
#[no_mangle]
pub extern "C" fn bytes(value: *mut u8, size: i32) -> i32
{
let mut buf: &mut [u8];
if size < 0 {
return 0
}
unsafe {
buf = std::slice::from_raw_parts_mut(value, size as usize);
}
match getrandom(&mut buf) {
Ok(_) => 1,
Err(_) => 0,
}
}

Loading…
Cancel
Save