mod.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. use core::slice;
  2. use std::sync::atomic::{Ordering};
  3. use std::{io};
  4. use std::io::Write;
  5. use ffi_support::{ConcurrentHandleMap, IntoFfi};
  6. use lazy_static::lazy_static;
  7. use libc::*;
  8. use crate::errno::{ZRFUErrBehavior, ZRFUErrNo, ZRFUErrPrefix};
  9. use crate::get_handle_from_ptr;
  10. use bytebuffer::ByteBuffer;
  11. pub struct ZRFUBytesVec {
  12. buf: ByteBuffer,
  13. }
  14. lazy_static! {
  15. static ref BYTES_VEC_MM: ConcurrentHandleMap<ZRFUBytesVec> = ConcurrentHandleMap::new();
  16. }
  17. impl ZRFUBytesVec {
  18. pub fn empty() -> ZRFUBytesVec {
  19. ZRFUBytesVec {
  20. buf: ByteBuffer::new(),
  21. }
  22. }
  23. pub fn push_mm(obj : ZRFUBytesVec) -> u64 {
  24. BYTES_VEC_MM.insert(obj).into_ffi_value()
  25. }
  26. pub fn do_with_object<F, R, E>(h: u64, callback: F) -> Result<R, ZRFUErrBehavior>
  27. where
  28. F: FnOnce(&ZRFUBytesVec) -> Result<R, ZRFUErrBehavior>,
  29. {
  30. BYTES_VEC_MM.get_u64(h, |bv| {
  31. callback(bv)
  32. })
  33. }
  34. pub fn do_with_mut_object<F, R>(h: u64, callback: F) -> Result<R, ZRFUErrBehavior>
  35. where
  36. F: FnOnce(&mut ZRFUBytesVec) -> Result<R, ZRFUErrBehavior>,
  37. {
  38. BYTES_VEC_MM.get_mut_u64(h, |bv| {
  39. callback(bv)
  40. })
  41. }
  42. pub fn get_usage() -> i32 {
  43. BYTES_VEC_MM.len() as i32
  44. }
  45. pub fn write_u8_slice(&mut self, dat: &[u8]) -> Result<usize, io::Error> {
  46. self.buf.write(dat)
  47. }
  48. pub fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>, io::Error> {
  49. let total_len = self.buf.len();
  50. let rpos = self.buf.get_rpos();
  51. let remain = total_len - rpos;
  52. if remain <= 0 {
  53. return Result::Ok(Vec::new());
  54. }
  55. let clen = if len > remain {
  56. remain
  57. }else{
  58. len
  59. };
  60. self.buf.read_bytes(clen)
  61. }
  62. pub fn reset_cursors(&mut self) -> () {
  63. self.buf.reset_cursors();
  64. }
  65. }
  66. impl ciborium_io::Read for &mut ZRFUBytesVec{
  67. type Error = std::io::Error;
  68. fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), std::io::Error> {
  69. let res = <ByteBuffer as io::Read>::read_exact(&mut self.buf, buf);
  70. res
  71. }
  72. }
  73. impl ciborium_io::Write for &mut ZRFUBytesVec{
  74. type Error = std::io::Error;
  75. fn write_all(&mut self, dat: &[u8]) -> Result<(), <Self as ciborium_io::Write>::Error> {
  76. let res = <ByteBuffer as io::Write>::write_all(&mut self.buf,dat);
  77. res
  78. }
  79. fn flush(&mut self) -> Result<(), <Self as ciborium_io::Write>::Error> {
  80. let res = <ByteBuffer as io::Write>::flush(&mut self.buf);
  81. res
  82. }
  83. }
  84. #[no_mangle]
  85. pub extern fn ZRFU_Debug_On() -> () {
  86. crate::errno::DEBUG_MODE.store(true, Ordering::Relaxed);
  87. }
  88. #[no_mangle]
  89. pub extern fn ZRFU_Debug_Off() -> () {
  90. crate::errno::DEBUG_MODE.store(false, Ordering::Relaxed);
  91. }
  92. #[no_mangle]
  93. pub extern fn ZRFU_BytesVec_New(hptr: *mut u64) -> c_int {
  94. if hptr.is_null() {
  95. return ZRFUErrNo::Generic_NullPointerBufferPtr.get_return_code()
  96. }
  97. let b = ZRFUBytesVec::empty();
  98. let hdl = BYTES_VEC_MM.insert(b);
  99. let h = hdl.into_ffi_value();
  100. unsafe { hptr.write(h) };
  101. 0
  102. }
  103. #[no_mangle]
  104. pub extern fn ZRFU_BytesVec_Del(hptr : *const u64) -> c_int {
  105. get_handle_from_ptr!(hptr as h);
  106. let result = BYTES_VEC_MM.delete_u64(h);
  107. match result {
  108. Ok(_) => 0,
  109. Err(e) => ZRFUErrBehavior::from(e).to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
  110. }
  111. }
  112. #[no_mangle]
  113. pub extern fn ZRFU_BytesVec_Len(hptr : *const u64) -> c_int {
  114. get_handle_from_ptr!(hptr as h);
  115. let result:Result<usize, ZRFUErrBehavior> = BYTES_VEC_MM.get_u64(h, |bv| {
  116. Result::Ok(bv.buf.len())
  117. });
  118. match result {
  119. Ok(sz) => sz as c_int,
  120. Err(err) => err.to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
  121. }
  122. }
  123. #[no_mangle]
  124. pub extern fn ZRFU_BytesVec_MMUsage() -> c_int {
  125. ZRFUBytesVec::get_usage()
  126. }
  127. #[no_mangle]
  128. pub extern fn ZRFU_BytesVec_Write(hptr: *const u64, ptr: *const c_uchar, len: usize) -> c_int {
  129. get_handle_from_ptr!(hptr as h);
  130. if len == 0 {
  131. return 0
  132. }
  133. if ptr.is_null() {
  134. return ZRFUErrNo::BytesVec_NullPointerBufferPtr.get_return_code()
  135. }
  136. let result = BYTES_VEC_MM.get_mut_u64(h, |bv| {
  137. let b = unsafe{ slice::from_raw_parts(ptr, len) };
  138. let res = bv.write_u8_slice(b);
  139. match res {
  140. Ok(sz) => Result::Ok(sz),
  141. Err(err) => Result::Err(ZRFUErrBehavior::IOError
  142. .with_complex_error(format!("ByteVec IO Error: {}", err.to_string()).as_str()))
  143. }
  144. });
  145. match result {
  146. Ok(sz) => sz as c_int,
  147. Err(err) => err.to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
  148. }
  149. }
  150. #[no_mangle]
  151. pub extern fn ZRFU_BytesVec_Read(hptr: *const u64, ptr: *mut c_uchar, len: usize) -> c_int {
  152. get_handle_from_ptr!(hptr as h);
  153. if len == 0 {
  154. return 0
  155. }
  156. if ptr.is_null() {
  157. return ZRFUErrNo::BytesVec_NullPointerBufferPtr.get_return_code()
  158. }
  159. let result = BYTES_VEC_MM.get_mut_u64(h, |bv| {
  160. let res = bv.read_bytes(len);
  161. match res {
  162. Ok(dat) => {
  163. let clen = if dat.len() > len {
  164. len
  165. }else {
  166. dat.len()
  167. };
  168. unsafe {
  169. ptr.copy_from(dat.as_ptr(), clen);
  170. }
  171. Result::Ok(clen)
  172. },
  173. Err(err) => Result::Err(ZRFUErrBehavior::IOError
  174. .with_complex_error(format!("ByteVec IO Error: {}", err.to_string()).as_str()))
  175. }
  176. });
  177. match result {
  178. Ok(sz) => sz as c_int,
  179. Err(err) => err.to_errno(ZRFUErrPrefix::BytesVec).get_return_code(),
  180. }
  181. }