diff options
| author | Steven Fackler <[email protected]> | 2016-08-11 21:01:27 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-08-11 21:01:27 -0700 |
| commit | 652326003cefe215dbfc838051e6114515cc5190 (patch) | |
| tree | 22dc99a726321cd8228004c34c40ca4a0648c594 /openssl/src/bio.rs | |
| parent | Merge branch 'release-v0.7.14' into release (diff) | |
| parent | Release openssl-sys v0.7.15, openssl v0.8.0 (diff) | |
| download | rust-openssl-openssl-sys-v0.7.15.tar.xz rust-openssl-openssl-sys-v0.7.15.zip | |
Merge branch 'release-v0.7.15-sys-v0.8.0' into releaseopenssl-v0.8.0openssl-sys-v0.7.15
Diffstat (limited to 'openssl/src/bio.rs')
| -rw-r--r-- | openssl/src/bio.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/openssl/src/bio.rs b/openssl/src/bio.rs new file mode 100644 index 00000000..0d82a6c3 --- /dev/null +++ b/openssl/src/bio.rs @@ -0,0 +1,67 @@ +use std::marker::PhantomData; +use std::ptr; +use std::slice; +use libc::c_int; +use ffi; + +use error::ErrorStack; + +pub struct MemBioSlice<'a>(*mut ffi::BIO, PhantomData<&'a [u8]>); + +impl<'a> Drop for MemBioSlice<'a> { + fn drop(&mut self) { + unsafe { + ffi::BIO_free_all(self.0); + } + } +} + +impl<'a> MemBioSlice<'a> { + pub fn new(buf: &'a [u8]) -> Result<MemBioSlice<'a>, ErrorStack> { + ffi::init(); + + assert!(buf.len() <= c_int::max_value() as usize); + let bio = unsafe { + try_ssl_null!(ffi::BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int)) + }; + + Ok(MemBioSlice(bio, PhantomData)) + } + + pub fn as_ptr(&self) -> *mut ffi::BIO { + self.0 + } +} + +pub struct MemBio(*mut ffi::BIO); + +impl Drop for MemBio { + fn drop(&mut self) { + unsafe { + ffi::BIO_free_all(self.0); + } + } +} + +impl MemBio { + pub fn new() -> Result<MemBio, ErrorStack> { + ffi::init(); + + let bio = unsafe { + try_ssl_null!(ffi::BIO_new(ffi::BIO_s_mem())) + }; + Ok(MemBio(bio)) + } + + pub fn as_ptr(&self) -> *mut ffi::BIO { + self.0 + } + + pub fn get_buf(&self) -> &[u8] { + unsafe { + let mut ptr = ptr::null_mut(); + let len = ffi::BIO_get_mem_data(self.0, &mut ptr); + slice::from_raw_parts(ptr as *const _ as *const _, len as usize) + } + } +} |