RtlpGenerateInheritedAce

NTSTATUS __stdcall RtlpGenerateInheritedAce(
        VOID *OldAce,
        UINT8 IsDirectoryObject,
        UINT8 AutoInherit,
        VOID *ClientOwnerSid,
        VOID *ClientGroupSid,
        VOID *ServerOwnerSid,
        VOID *ServerGroupSid,
        _GENERIC_MAPPING *GenericMapping,
        _GUID **pNewObjectType,
        UINT64 GuidCount,
        UINT64 *NewAceLength,
        _ACL *NewAcl,
        UINT64 *NewAceExtraLength,
        UINT8 *ObjectAceInherited){
  UINT8 *v14; 
  NTSTATUS v15; 
  char v16; 
  unsigned int v18; 
  unsigned int v19; 
  unsigned int v20; 
  UINT8 v21; 
  _ACL *v22; 
  unsigned int v23; 
  _ACL *v24; 
  char v25; 
  char v27; 
  size_t v28; 
  unsigned int v29; 
  __int64 v30; 
  unsigned __int8 v31; 
  UINT64 v32; 
  char EffectiveAceMapped[4]; 
  char AcePosition[12]; 
  _ACL *v35; 
  UINT8 AclOverflowed; 
  UINT8 v37; 
  VOID *ClientOwnerSida; 

  ClientOwnerSida = ClientOwnerSid;
  v37 = AutoInherit;
  v14 = ObjectAceInherited;
  v15 = 0;
  v16 = 0;
  v35 = 0i64;
  *(_DWORD *)AcePosition = 0;
  EffectiveAceMapped[0] = 0;
  *ObjectAceInherited = 0;
  v18 = 0;
  AclOverflowed = 0;
  v19 = 0;
  v20 = 0;
  v21 = 0;
  if( IsDirectoryObject )
  {
    v27 = *((_BYTE *)OldAce + 1);
    if( (v27 & 4) == 0 && ((v27 & 1) != 0 || (v27 & 2) != 0) )
      v21 = 1;
  }
  v22 = NewAcl;
  v23 = 0;
  *(_QWORD *)&AcePosition[4] = 0i64;
  v24 = NewAcl + 1;
  if( NewAcl->AceCount )
  {
    while( v24 < (_ACL *)((char *)NewAcl + NewAcl->AclSize) )
    {
      ++v23;
      v24 = (_ACL *)((char *)v24 + v24->AclSize);
      if( v23 >= NewAcl->AceCount )
      {
        v14 = ObjectAceInherited;
        goto LABEL_7;
      }
    }
    return -1073741699;
  }
LABEL_7:
  if( v24 > (_ACL *)((char *)NewAcl + NewAcl->AclSize) )
    return -1073741699;
  *(_QWORD *)&AcePosition[4] = v24;
  if( !v24 )
    return -1073741699;
  v25 = *((_BYTE *)OldAce + 1);
  if( IsDirectoryObject )
  {
    if( (v25 & 2) == 0 )
      goto LABEL_11;
  }
  else if( (v25 & 1) == 0 )
  {
    goto LABEL_11;
  }
  LODWORD(v32) = GuidCount;
  v35 = v24;
  if( !RtlpCopyEffectiveAce(
          (_ACE_HEADER *)OldAce,
          v37,
          v21,
          ClientOwnerSida,
          ClientGroupSid,
          ServerOwnerSid,
          ServerGroupSid,
          GenericMapping,
          pNewObjectType,
          v32,
          (VOID **)&AcePosition[4],
          (UINT64 *)AcePosition,
          NewAcl,
          v14,
          (UINT8 *)EffectiveAceMapped,
          &AclOverflowed) )
    return -1073741699;
  v20 = *(_DWORD *)AcePosition;
  if( !AclOverflowed && *(_DWORD *)AcePosition )
  {
    if( RtlpIsDuplicateAce((__int64)v22, (unsigned __int8 *)v24) )
    {
      v19 = *(_DWORD *)AcePosition;
      v20 = 0;
      --v22->AceCount;
      v18 = 0;
      v16 = EffectiveAceMapped[0];
      *(_DWORD *)AcePosition = 0;
      *(_QWORD *)&AcePosition[4] = v24;
      goto LABEL_11;
    }
    v20 = *(_DWORD *)AcePosition;
  }
  v16 = EffectiveAceMapped[0];
  v18 = v20;
LABEL_11:
  if( v21 )
  {
    if( v20 && !v16 )
    {
      if( !AclOverflowed )
      {
        v30 = (__int64)v35;
        v31 = v35->Sbz1 | *((_BYTE *)OldAce + 1) & 3;
        v35->Sbz1 = v31;
        if( v37 )
          *(_BYTE *)(v30 + 1) = v31 | 0x10;
      }
      goto LABEL_12;
    }
    if( *(_BYTE *)OldAce > 8u || *((_DWORD *)OldAce + 1) )
    {
      v28 = *((unsigned __int16 *)OldAce + 1);
      v18 += v28;
      if( v18 <= 0xFFFF )
      {
        if( *((unsigned __int16 *)OldAce + 1) > (__int64)v22 + v22->AclSize - *(_QWORD *)&AcePosition[4] )
        {
          AclOverflowed = 1;
        }
        else if( !AclOverflowed )
        {
          memmove(*(VOID **)&AcePosition[4], OldAce, v28);
          *(_BYTE *)(*(_QWORD *)&AcePosition[4] + 1i64) |= 8u;
          ++v22->AceCount;
          if( v37 )
          {
            *(_BYTE *)(*(_QWORD *)&AcePosition[4] + 1i64) |= 0x10u;
            if( RtlpIsDuplicateAce((__int64)v22, *(unsigned __int8 **)&AcePosition[4]) )
            {
              --v22->AceCount;
              v29 = *((unsigned __int16 *)OldAce + 1);
              v18 -= v29;
              if( v19 > v29 )
                v29 = v19;
              v19 = v29;
            }
          }
        }
        goto LABEL_12;
      }
      return -1073741699;
    }
  }
LABEL_12:
  *(_DWORD *)NewAceLength = v18;
  *(_DWORD *)NewAceExtraLength = v19;
  if( AclOverflowed )
    return -1073741789;
  return v15;
}

Referenced by:

RtlpGenerateInheritAcl