Coverage Report

Created: 2026-01-27 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/openssl-0.10.62/src/bio.rs
Line
Count
Source
1
use cfg_if::cfg_if;
2
use libc::c_int;
3
use std::marker::PhantomData;
4
use std::ptr;
5
use std::slice;
6
7
use crate::cvt_p;
8
use crate::error::ErrorStack;
9
10
pub struct MemBioSlice<'a>(*mut ffi::BIO, PhantomData<&'a [u8]>);
11
12
impl<'a> Drop for MemBioSlice<'a> {
13
0
    fn drop(&mut self) {
14
0
        unsafe {
15
0
            ffi::BIO_free_all(self.0);
16
0
        }
17
0
    }
18
}
19
20
impl<'a> MemBioSlice<'a> {
21
0
    pub fn new(buf: &'a [u8]) -> Result<MemBioSlice<'a>, ErrorStack> {
22
0
        ffi::init();
23
24
0
        assert!(buf.len() <= c_int::max_value() as usize);
25
0
        let bio = unsafe {
26
0
            cvt_p(BIO_new_mem_buf(
27
0
                buf.as_ptr() as *const _,
28
0
                buf.len() as crate::SLenType,
29
0
            ))?
30
        };
31
32
0
        Ok(MemBioSlice(bio, PhantomData))
33
0
    }
34
35
0
    pub fn as_ptr(&self) -> *mut ffi::BIO {
36
0
        self.0
37
0
    }
38
}
39
40
pub struct MemBio(*mut ffi::BIO);
41
42
impl Drop for MemBio {
43
0
    fn drop(&mut self) {
44
0
        unsafe {
45
0
            ffi::BIO_free_all(self.0);
46
0
        }
47
0
    }
48
}
49
50
impl MemBio {
51
0
    pub fn new() -> Result<MemBio, ErrorStack> {
52
0
        ffi::init();
53
54
0
        let bio = unsafe { cvt_p(ffi::BIO_new(ffi::BIO_s_mem()))? };
55
0
        Ok(MemBio(bio))
56
0
    }
57
58
0
    pub fn as_ptr(&self) -> *mut ffi::BIO {
59
0
        self.0
60
0
    }
61
62
0
    pub fn get_buf(&self) -> &[u8] {
63
        unsafe {
64
0
            let mut ptr = ptr::null_mut();
65
0
            let len = ffi::BIO_get_mem_data(self.0, &mut ptr);
66
0
            slice::from_raw_parts(ptr as *const _ as *const _, len as usize)
67
        }
68
0
    }
69
70
    #[cfg(not(boringssl))]
71
0
    pub unsafe fn from_ptr(bio: *mut ffi::BIO) -> MemBio {
72
0
        MemBio(bio)
73
0
    }
74
}
75
76
cfg_if! {
77
    if #[cfg(any(ossl102, boringssl))] {
78
        use ffi::BIO_new_mem_buf;
79
    } else {
80
        #[allow(bad_style)]
81
        unsafe fn BIO_new_mem_buf(buf: *const ::libc::c_void, len: ::libc::c_int) -> *mut ffi::BIO {
82
            ffi::BIO_new_mem_buf(buf as *mut _, len)
83
        }
84
    }
85
}