RtlpSetSecurityObject

INT64 __fastcall RtlpSetSecurityObject(
        VOID *Object,
        UINT64 SecurityInformation,
        VOID *ModificationDescriptor,
        VOID **ObjectsSecurityDescriptor,
        UINT64 AutoInheritFlags,
        UINT64 PoolType,
        _GENERIC_MAPPING *GenericMapping,
        VOID *Token){
  __int64 v8; 
  VOID **v9; 
  unsigned int v10; 
  unsigned int *v11; 
  unsigned __int16 *v12; 
  SECURITY_SUBJECT_CONTEXT *p_SubjectContext; 
  __int16 v14; 
  char *v15; 
  unsigned int *v16; 
  __int16 v17; 
  __int64 v18; 
  ACL *v19; 
  __int16 v20; 
  int v21; 
  bool v22; 
  __int64 v23; 
  UINT8 *v24; 
  bool v25; 
  unsigned int *v26; 
  __int64 v27; 
  unsigned __int8 *v28; 
  PVOID v29; 
  __int16 v30; 
  _ACL *v31; 
  __int16 v32; 
  unsigned int v33; 
  int v34; 
  unsigned int v35; 
  unsigned int v36; 
  char *PoolWithTag; 
  char *v38; 
  _ACL *v39; 
  __int16 v40; 
  UINT64 v41; 
  __int64 v42; 
  __int16 v43; 
  __int64 v44; 
  VOID **v45; 
  __int64 AclSize; 
  UINT8 *v47; 
  int valid; 
  void *v49; 
  void *v50; 
  void *v51; 
  __int64 v53; 
  __int64 v54; 
  PVOID v55; 
  void *v56; 
  void *v57; 
  int v58; 
  unsigned __int16 *v59; 
  _ACL *v60; 
  _ACL *v61; 
  __int16 v62; 
  _ACL *pTrustAcl; 
  _ACL **ppNewAcl; 
  unsigned __int16 v65; 
  __int16 v66; 
  __int64 v67; 
  __int64 v68; 
  unsigned int *v69; 
  __int16 v70; 
  __int64 v71; 
  __int16 v72; 
  __int64 v73; 
  ACL *v74; 
  __int64 v75; 
  UINT8 v76; 
  unsigned int v77; 
  unsigned int v78; 
  int v79; 
  __int64 v80; 
  _DWORD *v81; 
  INT64 *v82; 
  unsigned int v83; 
  __int64 v84; 
  int v85; 
  unsigned int v86; 
  unsigned int v87; 
  unsigned int v88; 
  unsigned __int8 *v89; 
  __int64 v90; 
  _QWORD *v91; 
  __int16 v92; 
  _BYTE *v93; 
  __int64 v94; 
  char v95; 
  __int16 v96; 
  UINT64 pIndex; 
  UINT8 v98; 
  bool v99; 
  bool v100; 
  char v101; 
  char v102; 
  char v103; 
  char v104; 
  char v105; 
  char v106; 
  char v107; 
  char v108; 
  char v109; 
  UINT8 ServerAclAllocated[8]; 
  void *Src; 
  PVOID v112; 
  PVOID v113; 
  PVOID v114; 
  SID_IDENTIFIER_AUTHORITY PoolTypea; 
  PVOID v116; 
  PVOID v117; 
  VOID **v118; 
  unsigned __int16 *v119; 
  _GENERIC_MAPPING *v120; 
  PVOID P; 
  size_t Size; 
  BOOL v123; 
  int v124; 
  UINT8 *v125; 
  _SECURITY_SUBJECT_CONTEXT *v126; 
  VOID *Group; 
  VOID *ServerGroup; 
  VOID *IntegrityLabel; 
  VOID *TrustLabel; 
  _ACL *Dacl; 
  PVOID Label; 
  int v133; 
  PVOID v134; 
  PVOID v135; 
  struct _SECURITY_SUBJECT_CONTEXT SubjectContext; 
  __int16 Sid; 
  int v138; 
  __int64 v139; 
  v9 = ObjectsSecurityDescriptor;
  v10 = SecurityInformation;
  v139 = v8;
  LOBYTE(v8) = 0;
  *(_DWORD *)PoolTypea.Value = PoolType;
  v11 = (unsigned int *)ModificationDescriptor;
  v12 = 0i64;
  p_SubjectContext = (SECURITY_SUBJECT_CONTEXT *)Token;
  v120 = GenericMapping;
  v14 = *((_WORD *)ModificationDescriptor + 1);
  v118 = ObjectsSecurityDescriptor;
  v119 = (unsigned __int16 *)ModificationDescriptor;
  v126 = (_SECURITY_SUBJECT_CONTEXT *)Token;
  v102 = 0;
  v101 = 0;
  ServerAclAllocated[0] = 0;
  v103 = 0;
  v109 = 0;
  v104 = 0;
  v105 = 0;
  v106 = 0;
  v107 = 0;
  v108 = 0;
  v133 = 0;
  v134 = 0i64;
  P = 0i64;
  v113 = 0i64;
  Group = 0i64;
  v114 = 0i64;
  IntegrityLabel = 0i64;
  v116 = 0i64;
  ServerGroup = 0i64;
  v112 = 0i64;
  TrustLabel = 0i64;
  v117 = 0i64;
  Dacl = 0i64;
  Label = 0i64;
  v98 = 0;
  LOBYTE(v124) = 0;
  v123 = v8;
  HIDWORD(pIndex) = 0;
  v96 = 0x8000;
  v135 = 0i64;
  memset(&SubjectContext, 0, sizeof(SubjectContext));
  if( (v14 & 0x10) != 0 )
  {
    if( v14 >= 0 )
    {
      v15 = (char *)*((_QWORD *)ModificationDescriptor + 3);
    }
    else
    {
      v67 = *((unsigned int *)ModificationDescriptor + 3);
      if( (_DWORD)v67 )
        v15 = (char *)ModificationDescriptor + v67;
      else
        v15 = 0i64;
    }
  }
  else
  {
    v15 = 0i64;
  }
  v16 = (unsigned int *)*ObjectsSecurityDescriptor;
  Src = v15;
  v17 = *((_WORD *)v16 + 1);
  if( (v17 & 0x10) != 0 )
  {
    if( v17 >= 0 )
    {
      v19 = (ACL *)*((_QWORD *)v16 + 3);
    }
    else
    {
      v18 = v16[3];
      if( (_DWORD)v18 )
        v19 = (ACL *)((char *)v16 + v18);
      else
        v19 = 0i64;
    }
  }
  else
  {
    v19 = 0i64;
  }
  if( !Token )
  {
    SeCaptureSubjectContext(&SubjectContext);
    v16 = (unsigned int *)*v9;
    p_SubjectContext = &SubjectContext;
    v126 = &SubjectContext;
  }
  if( *((__int16 *)v16 + 1) >= 0 )
  {
    valid = -1073741593;
    goto LABEL_76;
  }
  v20 = *((_WORD *)v11 + 1);
  v99 = (v20 & 0x80u) != 0;
  v100 = (v20 & 0x40) != 0;
  v21 = v10 & 0x80;
  v22 = (v10 & 0x100) != 0;
  if( (v10 & 0x10000) != 0 )
  {
    v85 = v10 | 0x1FF;
    v86 = v85 & 0xFFFFFF7F;
    if( v21 )
      v86 = v85;
    v10 = v86 & 0xFFFFFEFF;
    if( v22 )
      v10 = v86;
    if( !v19 && !v15 )
    {
      v10 &= 0xFFFFFE07;
      if( (v20 & 0x10) != 0 )
        v10 |= 8u;
      else
        v123 = (v20 & 0x800) != 0;
    }
  }
  if( (v10 & 1) == 0 )
  {
    if( *((__int16 *)v16 + 1) >= 0 )
    {
      v24 = (UINT8 *)*((_QWORD *)v16 + 1);
    }
    else
    {
      v23 = v16[1];
      if( !(_DWORD)v23 )
        goto LABEL_247;
      v24 = (UINT8 *)v16 + v23;
    }
    v125 = v24;
    v25 = v24 == 0i64;
LABEL_16:
    if( !v25 )
      goto LABEL_17;
LABEL_247:
    valid = -1073741734;
    goto LABEL_76;
  }
  v79 = v10 >> 2;
  LOBYTE(v79) = (v10 & 4) == 0;
  v124 = v79;
  if( v20 >= 0 )
  {
    v24 = (UINT8 *)*((_QWORD *)v11 + 1);
  }
  else
  {
    v80 = v11[1];
    if( (_DWORD)v80 )
      v24 = (UINT8 *)v11 + v80;
    else
      v24 = 0i64;
  }
  v125 = v24;
  v101 = 1;
  if( (AutoInheritFlags & 8) == 0 )
  {
    v25 = SepValidOwnerSubjectContext(p_SubjectContext, v24, (v20 & 0x80u) != 0) == 0;
    goto LABEL_16;
  }
LABEL_17:
  if( !RtlValidSid(v24) )
    goto LABEL_247;
  if( (v10 & 2) != 0 )
  {
    if( *((__int16 *)v11 + 1) >= 0 )
    {
      v28 = (unsigned __int8 *)*((_QWORD *)v11 + 2);
    }
    else
    {
      v84 = v11[2];
      if( (_DWORD)v84 )
        v28 = (unsigned __int8 *)v11 + v84;
      else
        v28 = 0i64;
    }
    v102 = 1;
  }
  else
  {
    v26 = (unsigned int *)*v9;
    if( *((__int16 *)*v9 + 1) >= 0 )
    {
      v28 = (unsigned __int8 *)*((_QWORD *)v26 + 2);
    }
    else
    {
      v27 = v26[2];
      if( !(_DWORD)v27 )
      {
LABEL_191:
        valid = -1073741733;
        goto LABEL_76;
      }
      v28 = (unsigned __int8 *)v26 + v27;
    }
  }
  if( !v28 || !RtlValidSid(v28) )
    goto LABEL_191;
  if( (v10 & 0x1F8) == 0 )
  {
    v12 = (unsigned __int16 *)v19;
    Src = v19;
    v29 = v116;
    goto LABEL_26;
  }
  LODWORD(Size) = v10 & 0x10;
  if( (v10 & 0x10) != 0 )
  {
    LODWORD(pIndex) = 0;
    do
    {
      LODWORD(v53) = RtlFindAceByType((ACL *)Src, 0x11ui64, &pIndex);
      v54 = v53;
      if( v53 )
      {
        v76 = *(_BYTE *)(v53 + 1);
        v55 = (PVOID)(v53 + 8);
        v25 = (*(_DWORD *)(v54 + 4) & 0xFFFFFFF8) == 0;
        Label = v55;
        v98 = v76;
        if( !v25 )
        {
LABEL_192:
          valid = -1073740730;
          goto LABEL_76;
        }
      }
      else
      {
        v55 = Label;
      }
      if( !SepValidLabelSubjectContext(v126, v55, v98) )
        goto LABEL_192;
      LODWORD(pIndex) = pIndex + 1;
    }
    while( v54 );
  }
  LODWORD(v56) = SepLocateTokenTrustLevel(v126);
  v57 = v56;
  LODWORD(Label) = v10 & 0x80;
  if( (v10 & 0x80) == 0 )
  {
LABEL_86:
    v58 = v10 & 0x100;
    LODWORD(pIndex) = v58;
    if( (v10 & 0x100) != 0 )
    {
      valid = RtlpValidFilterAclSubjectContext((ACL *)Src, v57);
      if( valid < 0 )
        goto LABEL_76;
      if( (AutoInheritFlags & 2) == 0 )
      {
        valid = RtlpValidFilterAclSubjectContext(v19, v57);
        if( valid < 0 )
          goto LABEL_76;
      }
      v58 = pIndex;
      v9 = v118;
    }
    v59 = v119;
    if( (v10 & 8) != 0 )
    {
      v78 = v119[1];
      if( (AutoInheritFlags & 2) == 0 )
      {
        v61 = (_ACL *)Src;
        v113 = Src;
        v96 = v78 & 0x2000 | 0x8010;
        v60 = (_ACL *)Src;
        if( (v78 & 0xA00) == 2560 )
          v96 = v78 & 0x2000 | 0x8810;
LABEL_90:
        if( (v10 & 0x20) != 0 )
        {
          v77 = v59[1];
          if( (AutoInheritFlags & 2) != 0 )
          {
            valid = RtlpComputeMergedAcl(
                      v19,
                      (*((_WORD *)*v9 + 1) & 0x800 | (*((unsigned __int16 *)*v9 + 1) >> 1) & 0x18u) >> 1,
                      (unsigned __int8 *)v61,
                      (v77 & 0x800 | (v77 >> 1) & 0x18) >> 1,
                      (__int64)v24,
                      (__int64)v28,
                      (__int64)v120,
                      2,
                      (__int64)&ServerGroup,
                      (__int64)&pIndex + 4);
            if( valid < 0 )
            {
              v29 = ServerGroup;
              goto LABEL_63;
            }
            v9 = v118;
            v59 = v119;
            v61 = (_ACL *)Src;
            v60 = (_ACL *)v113;
            v62 = (2 * (WORD2(pIndex) & 0x1400 | (2 * (BYTE4(pIndex) & 8 | 4)))) | v96;
            v105 = 1;
            v58 = pIndex;
            v96 = v62;
            v116 = ServerGroup;
          }
          else
          {
            v116 = v61;
            v62 = v77 & 0x2000 | 0x10 | v96;
            v96 = v62;
            if( (v77 & 0xA00) == 2560 )
            {
              v62 |= 0x800u;
              v96 = v62;
            }
          }
        }
        else
        {
          v62 = v96;
          v116 = v19;
        }
        if( (v10 & 0x40) != 0 )
        {
          v87 = v59[1];
          if( (AutoInheritFlags & 2) != 0 )
          {
            valid = RtlpComputeMergedAcl(
                      v19,
                      (*((_WORD *)*v9 + 1) & 0x800 | (*((unsigned __int16 *)*v9 + 1) >> 1) & 0x18u) >> 1,
                      (unsigned __int8 *)v61,
                      (v87 & 0x800 | (v87 >> 1) & 0x18) >> 1,
                      (__int64)v24,
                      (__int64)v28,
                      (__int64)v120,
                      2,
                      (__int64)&IntegrityLabel,
                      (__int64)&pIndex + 4);
            if( valid < 0 )
            {
              v49 = IntegrityLabel;
              v29 = v116;
              goto LABEL_64;
            }
            v9 = v118;
            v59 = v119;
            v61 = (_ACL *)Src;
            v60 = (_ACL *)v113;
            v62 = (2 * (WORD2(pIndex) & 0x1400 | (2 * (BYTE4(pIndex) & 8 | 4)))) | v96;
            v106 = 1;
            v58 = pIndex;
            v96 = v62;
            v114 = IntegrityLabel;
          }
          else
          {
            v114 = v61;
            v62 |= v87 & 0x2000 | 0x10;
            v96 = v62;
            if( (v87 & 0xA00) == 2560 )
            {
              v62 |= 0x800u;
              v96 = v62;
            }
          }
        }
        else
        {
          v114 = v19;
        }
        if( (_DWORD)Label )
        {
          v83 = v59[1];
          if( (AutoInheritFlags & 2) == 0 )
          {
            v112 = v61;
            v62 |= v83 & 0x2000 | 0x10;
            v96 = v62;
            v25 = (v83 & 0xA00) == 2560;
            pTrustAcl = v61;
            if( v25 )
            {
              v62 |= 0x800u;
              v96 = v62;
            }
            goto LABEL_97;
          }
          valid = RtlpComputeMergedAcl(
                    v19,
                    (*((_WORD *)*v9 + 1) & 0x800 | (*((unsigned __int16 *)*v9 + 1) >> 1) & 0x18u) >> 1,
                    (unsigned __int8 *)v61,
                    (v83 & 0x800 | (v83 >> 1) & 0x18) >> 1,
                    (__int64)v24,
                    (__int64)v28,
                    (__int64)v120,
                    2,
                    (__int64)&TrustLabel,
                    (__int64)&pIndex + 4);
          if( valid < 0 )
          {
            v50 = TrustLabel;
            v29 = v116;
            v49 = v114;
            goto LABEL_65;
          }
          pTrustAcl = (_ACL *)TrustLabel;
          v9 = v118;
          v59 = v119;
          v61 = (_ACL *)Src;
          v60 = (_ACL *)v113;
          v62 = (2 * (WORD2(pIndex) & 0x1400 | (2 * (BYTE4(pIndex) & 8 | 4)))) | v96;
          v107 = 1;
          v58 = pIndex;
          v96 = v62;
        }
        else
        {
          pTrustAcl = v19;
        }
        v112 = pTrustAcl;
LABEL_97:
        if( v58 )
        {
          v88 = v59[1];
          ppNewAcl = (_ACL **)v61;
          if( (AutoInheritFlags & 2) != 0 )
          {
            valid = RtlpComputeMergedAcl(
                      v19,
                      (*((_WORD *)*v9 + 1) & 0x800 | (*((unsigned __int16 *)*v9 + 1) >> 1) & 0x18u) >> 1,
                      (unsigned __int8 *)v61,
                      (v88 & 0x800 | (v88 >> 1) & 0x18) >> 1,
                      (__int64)v24,
                      (__int64)v28,
                      (__int64)v120,
                      2,
                      (__int64)&Dacl,
                      (__int64)&pIndex + 4);
            if( valid < 0 )
            {
              v51 = Dacl;
              v50 = v112;
              v29 = v116;
              v49 = v114;
LABEL_66:
              if( v113 && v104 )
                ExFreePoolWithTag(v113, 0);
              if( v29 && v105 )
                ExFreePoolWithTag(v29, 0);
              if( v49 && v106 )
                ExFreePoolWithTag(v49, 0);
              if( v50 && v107 )
                ExFreePoolWithTag(v50, 0);
              if( v51 && v108 )
                ExFreePoolWithTag(v51, 0);
              if( v12 && v109 )
                ExFreePoolWithTag(v12, 0);
              if( ServerAclAllocated[0] )
                ExFreePoolWithTag(v135, 0);
              goto LABEL_76;
            }
            ppNewAcl = (_ACL **)Dacl;
            v61 = (_ACL *)Src;
            v60 = (_ACL *)v113;
            v108 = 1;
            v117 = Dacl;
            v62 = (2 * (WORD2(pIndex) & 0x1400 | (2 * (BYTE4(pIndex) & 8 | 4)))) | v96;
            v96 = v62;
          }
          else
          {
            v117 = v61;
            v62 |= v88 & 0x2000 | 0x10;
            v96 = v62;
            if( (v88 & 0xA00) == 2560 )
            {
              v62 |= 0x800u;
              v96 = v62;
            }
          }
          pTrustAcl = (_ACL *)v112;
        }
        else
        {
          ppNewAcl = (_ACL **)v19;
          v117 = v19;
        }
        v11 = (unsigned int *)v119;
        if( (_DWORD)Size )
        {
          v65 = v119[1];
          v66 = v65 & 0x2000 | 0x10 | v62;
          v96 = v66;
          if( (v65 & 0xA00) == 2560 )
            v96 = v66 | 0x800;
        }
        else
        {
          v61 = v19;
        }
        v29 = v116;
        valid = RtlpCombineAcls(v60, v61, (_ACL *)v116, (_ACL *)v114, pTrustAcl, ppNewAcl, (UINT64 *)&P);
        if( valid < 0 )
          goto LABEL_63;
        v12 = (unsigned __int16 *)P;
        Src = P;
        if( !v113 && P && !*((_WORD *)P + 2) )
        {
          ExFreePoolWithTag(P, 0);
          v12 = 0i64;
          Src = 0i64;
        }
        v9 = v118;
        v109 = 1;
LABEL_26:
        if( (v10 & 4) != 0 )
        {
          v30 = *((_WORD *)v11 + 1);
          if( (AutoInheritFlags & 1) != 0 )
          {
            if( (v30 & 4) != 0 )
            {
              if( v30 >= 0 )
              {
                v89 = (unsigned __int8 *)*((_QWORD *)v11 + 4);
              }
              else
              {
                v90 = v11[4];
                if( (_DWORD)v90 )
                  v89 = (unsigned __int8 *)v11 + v90;
                else
                  v89 = 0i64;
              }
            }
            else
            {
              v89 = 0i64;
            }
            v91 = *v9;
            v92 = *((_WORD *)*v9 + 1);
            if( (v92 & 4) != 0 )
            {
              if( v92 >= 0 )
              {
                v93 = (_BYTE *)v91[4];
              }
              else
              {
                v94 = *((unsigned int *)v91 + 4);
                if( (_DWORD)v94 )
                  v93 = (char *)v91 + v94;
                else
                  v93 = 0i64;
              }
            }
            else
            {
              v93 = 0i64;
            }
            valid = RtlpComputeMergedAcl(
                      v93,
                      v92 & 0x140C,
                      v89,
                      *((_WORD *)v11 + 1) & 0x140C,
                      (__int64)v24,
                      (__int64)v28,
                      (__int64)v120,
                      1,
                      (__int64)&v134,
                      (__int64)&pIndex + 4);
            if( valid < 0 )
              goto LABEL_63;
            v31 = (_ACL *)v134;
            v103 = 1;
            v96 |= WORD2(pIndex) & 0x1408 | 4;
          }
          else
          {
            if( (v30 & 4) != 0 )
            {
              if( v30 < 0 )
              {
                v68 = v11[4];
                if( (_DWORD)v68 )
                  v31 = (_ACL *)((char *)v11 + v68);
                else
                  v31 = 0i64;
              }
              else
              {
                v31 = (_ACL *)*((_QWORD *)v11 + 4);
              }
            }
            else
            {
              v31 = 0i64;
            }
            v32 = v30 & 0x1000 | 4 | v96;
            v96 = v32;
            if( (v30 & 0x500) == 1280 )
              v96 = v32 | 0x400;
          }
          if( v99 )
          {
            Size = 0i64;
            Group = 0i64;
            P = 0i64;
            ServerGroup = 0i64;
            IntegrityLabel = 0i64;
            TrustLabel = 0i64;
            Dacl = 0i64;
            SepGetDefaultsSubjectContext(
              v126,
              (VOID **)&Size,
              &Group,
              &P,
              &ServerGroup,
              &IntegrityLabel,
              &TrustLabel,
              &Dacl);
            valid = RtlpCreateServerAcl(v31, v100, P, (_ACL **)&v135, ServerAclAllocated);
            if( valid < 0 )
            {
LABEL_60:
              if( v103 )
                ExFreePoolWithTag(v134, 0);
              goto LABEL_62;
            }
            v31 = (_ACL *)v135;
          }
        }
        else
        {
          v69 = (unsigned int *)*v9;
          v70 = *((_WORD *)*v9 + 1);
          if( (v70 & 4) != 0 )
          {
            if( v70 >= 0 )
            {
              v31 = (_ACL *)*((_QWORD *)v69 + 4);
            }
            else
            {
              v71 = v69[4];
              if( (_DWORD)v71 )
                v31 = (_ACL *)((char *)v69 + v71);
              else
                v31 = 0i64;
            }
          }
          else
          {
            v31 = 0i64;
          }
        }
        v33 = 4 * v24[1] + 8;
        v34 = 4 * v28[1] + 8;
        LODWORD(Size) = v34;
        if( v12 )
          v35 = (v12[1] + 3) & 0xFFFFFFFC;
        else
          v35 = 0;
        if( v31 )
          v36 = (v31->AclSize + 3) & 0xFFFFFFFC;
        else
          v36 = 0;
        PoolWithTag = (char *)ExAllocatePoolWithTag(
                                *(_POOL_TYPE *)PoolTypea.Value,
                                v33 + v34 + 20 + v36 + v35,
                                0x64536553ui64);
        v38 = PoolWithTag;
        if( PoolWithTag )
        {
          v39 = (_ACL *)(PoolWithTag + 20);
          v25 = !v123;
          *(_OWORD *)PoolWithTag = 0i64;
          *((_DWORD *)PoolWithTag + 4) = 0;
          v40 = v96;
          *v38 = 1;
          if( !v25 )
            v40 = v96 | 0x800;
          v41 = 0x4000i64;
          v42 = (__int64)v119;
          v43 = *((_WORD *)v38 + 1) | v40;
          *((_WORD *)v38 + 1) = v43;
          if( (*(_WORD *)(v42 + 2) & 0x4000) != 0 )
          {
            v38[1] = *(_BYTE *)(v42 + 1);
            *((_WORD *)v38 + 1) = v43 | 0x4000;
          }
          if( Src )
          {
            memmove((UINT8 *)v38 + 20, (UINT8 *)Src, *((unsigned __int16 *)Src + 1));
            RtlpApplyAclToObject((_ACL *)(v38 + 20), v120);
            *((_DWORD *)v38 + 3) = (_DWORD)v39 - (_DWORD)v38;
            v44 = *((unsigned __int16 *)Src + 1);
            if( v35 > (unsigned int)v44 )
              memset((INT64)v39 + v44, 0i64);
            v39 = (_ACL *)((char *)v39 + v35);
          }
          else
          {
            *((_DWORD *)v38 + 3) = 0;
          }
          v45 = v118;
          if( (v96 & 0x10) == 0 )
            *((_WORD *)v38 + 1) |= *((_WORD *)*v118 + 1) & 0x2830;
          if( v31 )
          {
            memmove((UINT8 *)v39, (UINT8 *)v31, v31->AclSize);
            RtlpApplyAclToObject(v39, v120);
            *((_DWORD *)v38 + 4) = (_DWORD)v39 - (_DWORD)v38;
            AclSize = v31->AclSize;
            if( v36 > (unsigned int)AclSize )
              memset((INT64)v39 + AclSize, 0i64);
            v39 = (_ACL *)((char *)v39 + v36);
          }
          else
          {
            *((_DWORD *)v38 + 4) = 0;
          }
          if( (v96 & 4) != 0 )
            goto LABEL_54;
          *((_WORD *)v38 + 1) |= *((_WORD *)*v45 + 1) & 0x140C;
          if( !(_BYTE)v124 )
            goto LABEL_54;
          LOBYTE(v41) = 1;
          *(_DWORD *)PoolTypea.Value = 0;
          *(_WORD *)&PoolTypea.Value[4] = 768;
          valid = RtlInitializeSid(&Sid, &PoolTypea, v41);
          if( valid >= 0 )
          {
            v138 = 4;
            LODWORD(pIndex) = 0;
            while( 1 )
            {
              v72 = *((_WORD *)v38 + 1);
              if( (v72 & 4) != 0 )
              {
                if( v72 >= 0 )
                {
                  v74 = (ACL *)*((_QWORD *)v38 + 4);
                }
                else
                {
                  v73 = *((unsigned int *)v38 + 4);
                  v74 = (_DWORD)v73 ? (ACL *)&v38[v73] : 0i64;
                }
              }
              else
              {
                v74 = 0i64;
              }
              LODWORD(v75) = RtlFindAceBySid(v74, &Sid, &pIndex);
              if( !v75 )
                break;
              v95 = *(_BYTE *)(v75 + 1) & 0xF4 | 8;
              LODWORD(pIndex) = pIndex + 1;
              *(_BYTE *)(v75 + 1) = v95;
            }
            v45 = v118;
LABEL_54:
            memmove((UINT8 *)v39, v125, v33);
            *((_DWORD *)v38 + 1) = (_DWORD)v39 - (_DWORD)v38;
            v47 = (UINT8 *)v39 + v33;
            if( !v101 )
              *((_WORD *)v38 + 1) |= *((_WORD *)*v45 + 1) & 1;
            memmove(v47, v28, (unsigned int)Size);
            v25 = v102 == 0;
            *((_DWORD *)v38 + 2) = (_DWORD)v47 - (_DWORD)v38;
            if( v25 )
              *((_WORD *)v38 + 1) |= *((_WORD *)*v45 + 1) & 2;
            *v45 = v38;
            valid = 0;
          }
        }
        else
        {
          valid = -1073741801;
        }
        v12 = (unsigned __int16 *)Src;
        goto LABEL_60;
      }
      valid = RtlpComputeMergedAcl(
                v19,
                (*((_WORD *)*v9 + 1) & 0x2800 | (*((unsigned __int16 *)*v9 + 1) >> 1) & 0x18u) >> 1,
                (unsigned __int8 *)Src,
                (v78 & 0x2800 | (v78 >> 1) & 0x18) >> 1,
                (__int64)v24,
                (__int64)v28,
                (__int64)v120,
                2,
                (__int64)&Group,
                (__int64)&pIndex + 4);
      if( valid < 0 )
      {
        v113 = Group;
LABEL_62:
        v29 = v116;
LABEL_63:
        v49 = v114;
LABEL_64:
        v50 = v112;
LABEL_65:
        v51 = v117;
        goto LABEL_66;
      }
      v60 = (_ACL *)Group;
      v9 = v118;
      v59 = v119;
      v104 = 1;
      v113 = Group;
      v96 = 2 * (WORD2(pIndex) & 0x1400 | (2 * (BYTE4(pIndex) & 8 | 0x2004)));
      v58 = pIndex;
    }
    else
    {
      v60 = v19;
      v113 = v19;
    }
    v61 = (_ACL *)Src;
    goto LABEL_90;
  }
  if( v56 )
  {
    LODWORD(pIndex) = 0;
    while( 1 )
    {
      LODWORD(v81) = RtlFindAceByType((ACL *)Src, 0x14ui64, &pIndex);
      v113 = v81;
      if( v81 )
      {
        if( (v81[1] & 0xFF000000) != 0 )
          goto LABEL_192;
        if( !RtlpValidTrustSubjectContext(v57, v81 + 2, v82) )
          break;
        v81 = v113;
      }
      LODWORD(pIndex) = pIndex + 1;
      if( !v81 )
        goto LABEL_86;
    }
  }
  valid = -1073741790;
LABEL_76:
  if( v126 == &SubjectContext )
    SeReleaseSubjectContext(&SubjectContext);
  return(unsigned int)valid;
}

Referenced by:

CmpSetSecurityDescriptorInfo
SeSetSecurityDescriptorInfo
SeSetSecurityDescriptorInfoEx