use crate::*;
use winapi::shared::minwindef::{BYTE, DWORD};
use winapi::shared::winerror::*;
use winapi::um::winnt::{SID, SID_IDENTIFIER_AUTHORITY};
use core::fmt::{self, Debug, Formatter};
use core::marker::PhantomData;
use core::mem::size_of;
use core::ops::Deref;
#[derive(Clone, Copy)] #[repr(transparent)] pub struct Ptr<'a>(*mut SID, PhantomData<&'a SID>);
impl Debug for Ptr<'_> { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { Debug::fmt(&**self, fmt) } }
impl Deref for Ptr<'_> { type Target = sid::Value; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } } }
impl Ptr<'_> {
pub const unsafe fn from_raw_unchecked(sid: *mut SID) -> Self { Self(sid, PhantomData) }
pub unsafe fn from_raw(sid: *mut SID, bytes: usize) -> Result<Self, Error> {
if sid.is_null() { return Ok(Self(sid, PhantomData)) }
if bytes < size_of::<Header>() { return Err(Error(ERROR_INVALID_SID)) }
let header : Header = unsafe { core::ptr::read(sid.cast()) };
if header.SubAuthorityCount > 15 { return Err(Error(ERROR_INVALID_SUB_AUTHORITY)) }
let expected_size = size_of::<Header>() + size_of::<DWORD>() * usize::from(header.SubAuthorityCount);
if bytes < expected_size { return Err(Error(ERROR_INVALID_SID)) }
Ok(Self(sid, PhantomData))
}
}
#[allow(non_snake_case)]
#[derive(Clone, Copy)]
#[repr(C)] struct Header {
Revision: BYTE,
SubAuthorityCount: BYTE,
IdentifierAuthority: SID_IDENTIFIER_AUTHORITY,
}