192f3ab15Sopenharmony_ciuse foreign_types::ForeignTypeRef;
292f3ab15Sopenharmony_ciuse libc::{c_char, c_void};
392f3ab15Sopenharmony_ciuse std::convert::AsRef;
492f3ab15Sopenharmony_ciuse std::ffi::CStr;
592f3ab15Sopenharmony_ciuse std::fmt;
692f3ab15Sopenharmony_ciuse std::ops::Deref;
792f3ab15Sopenharmony_ciuse std::str;
892f3ab15Sopenharmony_ci
992f3ab15Sopenharmony_ciuse crate::stack::Stackable;
1092f3ab15Sopenharmony_ci
1192f3ab15Sopenharmony_ciforeign_type_and_impl_send_sync! {
1292f3ab15Sopenharmony_ci    type CType = c_char;
1392f3ab15Sopenharmony_ci    fn drop = free;
1492f3ab15Sopenharmony_ci
1592f3ab15Sopenharmony_ci    pub struct OpensslString;
1692f3ab15Sopenharmony_ci    pub struct OpensslStringRef;
1792f3ab15Sopenharmony_ci}
1892f3ab15Sopenharmony_ci
1992f3ab15Sopenharmony_ciimpl fmt::Display for OpensslString {
2092f3ab15Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2192f3ab15Sopenharmony_ci        fmt::Display::fmt(&**self, f)
2292f3ab15Sopenharmony_ci    }
2392f3ab15Sopenharmony_ci}
2492f3ab15Sopenharmony_ci
2592f3ab15Sopenharmony_ciimpl fmt::Debug for OpensslString {
2692f3ab15Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2792f3ab15Sopenharmony_ci        fmt::Debug::fmt(&**self, f)
2892f3ab15Sopenharmony_ci    }
2992f3ab15Sopenharmony_ci}
3092f3ab15Sopenharmony_ci
3192f3ab15Sopenharmony_ciimpl Stackable for OpensslString {
3292f3ab15Sopenharmony_ci    type StackType = ffi::stack_st_OPENSSL_STRING;
3392f3ab15Sopenharmony_ci}
3492f3ab15Sopenharmony_ci
3592f3ab15Sopenharmony_ciimpl AsRef<str> for OpensslString {
3692f3ab15Sopenharmony_ci    fn as_ref(&self) -> &str {
3792f3ab15Sopenharmony_ci        self
3892f3ab15Sopenharmony_ci    }
3992f3ab15Sopenharmony_ci}
4092f3ab15Sopenharmony_ci
4192f3ab15Sopenharmony_ciimpl AsRef<[u8]> for OpensslString {
4292f3ab15Sopenharmony_ci    fn as_ref(&self) -> &[u8] {
4392f3ab15Sopenharmony_ci        self.as_bytes()
4492f3ab15Sopenharmony_ci    }
4592f3ab15Sopenharmony_ci}
4692f3ab15Sopenharmony_ci
4792f3ab15Sopenharmony_ciimpl Deref for OpensslStringRef {
4892f3ab15Sopenharmony_ci    type Target = str;
4992f3ab15Sopenharmony_ci
5092f3ab15Sopenharmony_ci    fn deref(&self) -> &str {
5192f3ab15Sopenharmony_ci        unsafe {
5292f3ab15Sopenharmony_ci            let slice = CStr::from_ptr(self.as_ptr()).to_bytes();
5392f3ab15Sopenharmony_ci            str::from_utf8_unchecked(slice)
5492f3ab15Sopenharmony_ci        }
5592f3ab15Sopenharmony_ci    }
5692f3ab15Sopenharmony_ci}
5792f3ab15Sopenharmony_ci
5892f3ab15Sopenharmony_ciimpl AsRef<str> for OpensslStringRef {
5992f3ab15Sopenharmony_ci    fn as_ref(&self) -> &str {
6092f3ab15Sopenharmony_ci        self
6192f3ab15Sopenharmony_ci    }
6292f3ab15Sopenharmony_ci}
6392f3ab15Sopenharmony_ci
6492f3ab15Sopenharmony_ciimpl AsRef<[u8]> for OpensslStringRef {
6592f3ab15Sopenharmony_ci    fn as_ref(&self) -> &[u8] {
6692f3ab15Sopenharmony_ci        self.as_bytes()
6792f3ab15Sopenharmony_ci    }
6892f3ab15Sopenharmony_ci}
6992f3ab15Sopenharmony_ci
7092f3ab15Sopenharmony_ciimpl fmt::Display for OpensslStringRef {
7192f3ab15Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7292f3ab15Sopenharmony_ci        fmt::Display::fmt(&**self, f)
7392f3ab15Sopenharmony_ci    }
7492f3ab15Sopenharmony_ci}
7592f3ab15Sopenharmony_ci
7692f3ab15Sopenharmony_ciimpl fmt::Debug for OpensslStringRef {
7792f3ab15Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7892f3ab15Sopenharmony_ci        fmt::Debug::fmt(&**self, f)
7992f3ab15Sopenharmony_ci    }
8092f3ab15Sopenharmony_ci}
8192f3ab15Sopenharmony_ci
8292f3ab15Sopenharmony_ci#[inline]
8392f3ab15Sopenharmony_ci#[cfg(not(boringssl))]
8492f3ab15Sopenharmony_ciunsafe fn free(buf: *mut c_char) {
8592f3ab15Sopenharmony_ci    ffi::OPENSSL_free(buf as *mut c_void);
8692f3ab15Sopenharmony_ci}
8792f3ab15Sopenharmony_ci
8892f3ab15Sopenharmony_ci#[inline]
8992f3ab15Sopenharmony_ci#[cfg(boringssl)]
9092f3ab15Sopenharmony_ciunsafe fn free(buf: *mut c_char) {
9192f3ab15Sopenharmony_ci    ffi::CRYPTO_free(
9292f3ab15Sopenharmony_ci        buf as *mut c_void,
9392f3ab15Sopenharmony_ci        concat!(file!(), "\0").as_ptr() as *const c_char,
9492f3ab15Sopenharmony_ci        line!() as ::libc::c_int,
9592f3ab15Sopenharmony_ci    );
9692f3ab15Sopenharmony_ci}
97