123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- use core::slice;
- use std::sync::atomic::{Ordering};
- use std::{io};
- use std::io::Write;
- use ffi_support::{ConcurrentHandleMap, IntoFfi};
- use lazy_static::lazy_static;
- use libc::*;
- use crate::errno::{ZRFUErrBehavior, ZRFUErrNo, ZRFUErrPrefix};
- use crate::get_handle_from_ptr;
- use bytebuffer::ByteBuffer;
- pub struct ZRFUBytesVec {
- buf: ByteBuffer,
- }
- lazy_static! {
- static ref BYTES_VEC_MM: ConcurrentHandleMap<ZRFUBytesVec> = ConcurrentHandleMap::new();
- }
- impl ZRFUBytesVec {
- pub fn empty() -> ZRFUBytesVec {
- ZRFUBytesVec {
- buf: ByteBuffer::new(),
- }
- }
- pub fn push_mm(obj : ZRFUBytesVec) -> u64 {
- BYTES_VEC_MM.insert(obj).into_ffi_value()
- }
- pub fn do_with_object<F, R, E>(h: u64, callback: F) -> Result<R, ZRFUErrBehavior>
- where
- F: FnOnce(&ZRFUBytesVec) -> Result<R, ZRFUErrBehavior>,
- {
- BYTES_VEC_MM.get_u64(h, |bv| {
- callback(bv)
- })
- }
- pub fn do_with_mut_object<F, R>(h: u64, callback: F) -> Result<R, ZRFUErrBehavior>
- where
- F: FnOnce(&mut ZRFUBytesVec) -> Result<R, ZRFUErrBehavior>,
- {
- BYTES_VEC_MM.get_mut_u64(h, |bv| {
- callback(bv)
- })
- }
- pub fn get_usage() -> i32 {
- BYTES_VEC_MM.len() as i32
- }
- pub fn write_u8_slice(&mut self, dat: &[u8]) -> Result<usize, io::Error> {
- self.buf.write(dat)
- }
- pub fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>, io::Error> {
- let total_len = self.buf.len();
- let rpos = self.buf.get_rpos();
- let remain = total_len - rpos;
- if remain <= 0 {
- return Result::Ok(Vec::new());
- }
- let clen = if len > remain {
- remain
- }else{
- len
- };
- self.buf.read_bytes(clen)
- }
- pub fn reset_cursors(&mut self) -> () {
- self.buf.reset_cursors();
- }
- }
- impl ciborium_io::Read for &mut ZRFUBytesVec{
- type Error = std::io::Error;
- fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), std::io::Error> {
- let res = <ByteBuffer as io::Read>::read_exact(&mut self.buf, buf);
- res
- }
- }
- impl ciborium_io::Write for &mut ZRFUBytesVec{
- type Error = std::io::Error;
- fn write_all(&mut self, dat: &[u8]) -> Result<(), <Self as ciborium_io::Write>::Error> {
- let res = <ByteBuffer as io::Write>::write_all(&mut self.buf,dat);
- res
- }
- fn flush(&mut self) -> Result<(), <Self as ciborium_io::Write>::Error> {
- let res = <ByteBuffer as io::Write>::flush(&mut self.buf);
- res
- }
- }
- #[no_mangle]
- pub extern fn ZRFU_Debug_On() -> () {
- crate::errno::DEBUG_MODE.store(true, Ordering::Relaxed);
- }
- #[no_mangle]
- pub extern fn ZRFU_Debug_Off() -> () {
- crate::errno::DEBUG_MODE.store(false, Ordering::Relaxed);
- }
- #[no_mangle]
- pub extern fn ZRFU_BytesVec_New(hptr: *mut u64) -> c_int {
- if hptr.is_null() {
- return ZRFUErrNo::Generic_NullPointerBufferPtr.get_return_code()
- }
- let b = ZRFUBytesVec::empty();
- let hdl = BYTES_VEC_MM.insert(b);
- let h = hdl.into_ffi_value();
- unsafe { hptr.write(h) };
- 0
- }
- #[no_mangle]
- pub extern fn ZRFU_BytesVec_Del(hptr : *const u64) -> c_int {
- get_handle_from_ptr!(hptr as h);
- let result = BYTES_VEC_MM.delete_u64(h);
- match result {
- Ok(_) => 0,
- Err(e) => ZRFUErrBehavior::from(e).to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
- }
- }
- #[no_mangle]
- pub extern fn ZRFU_BytesVec_Len(hptr : *const u64) -> c_int {
- get_handle_from_ptr!(hptr as h);
- let result:Result<usize, ZRFUErrBehavior> = BYTES_VEC_MM.get_u64(h, |bv| {
- Result::Ok(bv.buf.len())
- });
- match result {
- Ok(sz) => sz as c_int,
- Err(err) => err.to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
- }
- }
- #[no_mangle]
- pub extern fn ZRFU_BytesVec_MMUsage() -> c_int {
- ZRFUBytesVec::get_usage()
- }
- #[no_mangle]
- pub extern fn ZRFU_BytesVec_Write(hptr: *const u64, ptr: *const c_uchar, len: usize) -> c_int {
- get_handle_from_ptr!(hptr as h);
- if len == 0 {
- return 0
- }
- if ptr.is_null() {
- return ZRFUErrNo::BytesVec_NullPointerBufferPtr.get_return_code()
- }
- let result = BYTES_VEC_MM.get_mut_u64(h, |bv| {
- let b = unsafe{ slice::from_raw_parts(ptr, len) };
- let res = bv.write_u8_slice(b);
- match res {
- Ok(sz) => Result::Ok(sz),
- Err(err) => Result::Err(ZRFUErrBehavior::IOError
- .with_complex_error(format!("ByteVec IO Error: {}", err.to_string()).as_str()))
- }
- });
- match result {
- Ok(sz) => sz as c_int,
- Err(err) => err.to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
- }
- }
- #[no_mangle]
- pub extern fn ZRFU_BytesVec_Read(hptr: *const u64, ptr: *mut c_uchar, len: usize) -> c_int {
- get_handle_from_ptr!(hptr as h);
- if len == 0 {
- return 0
- }
- if ptr.is_null() {
- return ZRFUErrNo::BytesVec_NullPointerBufferPtr.get_return_code()
- }
- let result = BYTES_VEC_MM.get_mut_u64(h, |bv| {
- let res = bv.read_bytes(len);
- match res {
- Ok(dat) => {
- let clen = if dat.len() > len {
- len
- }else {
- dat.len()
- };
- unsafe {
- ptr.copy_from(dat.as_ptr(), clen);
- }
- Result::Ok(clen)
- },
- Err(err) => Result::Err(ZRFUErrBehavior::IOError
- .with_complex_error(format!("ByteVec IO Error: {}", err.to_string()).as_str()))
- }
- });
- match result {
- Ok(sz) => sz as c_int,
- Err(err) => err.to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
- }
- }
|