SepGetLowBoxNumberEntry

NTSTATUS __stdcall SepGetLowBoxNumberEntry(
        _SEP_LOWBOX_NUMBER_MAPPING *LowBoxMapping,
        VOID *Sid,
        _SEP_LOWBOX_NUMBER_ENTRY **MatchingEntry){
  _RTL_DYNAMIC_HASH_TABLE *HashTable; 
  _RTL_AVL_TABLE *p_Bitmap; 
  NTSTATUS v6; 
  unsigned int v9; 
  VOID **PoolWithTag; 
  _SEP_LOWBOX_NUMBER_ENTRY *v11; 
  UINT8 *v12; 
  UINT32 ClearBitsAndSet; 
  __int64 v14; 
  __int64 v15; 
  UINT64 v16; 
  unsigned int v17; 
  VOID **v18; 
  UINT8 *v19; 
  VOID *v20; 
  VOID *v21; 
  _TABLE_SEARCH_RESULT v22; 
  _TABLE_SEARCH_RESULT v23; 
  INT64 a3; 
  _RTL_DYNAMIC_HASH_TABLE *v25; 

  HashTable = LowBoxMapping->HashTable;
  p_Bitmap = (_RTL_AVL_TABLE *)&LowBoxMapping->Bitmap;
  v25 = HashTable;
  v6 = 0;
  a3 = 0i64;
  SepFindMatchingLowBoxNumberEntry(HashTable, (UINT8 *)Sid, &a3);
  if( !a3 )
  {
    v9 = (4 * *((unsigned __int8 *)Sid + 1) + 67) & 0xFFFFFFFC;
    PoolWithTag = ExAllocatePoolWithTag(1ui64, v9, 1934386515i64);
    v11 = (_SEP_LOWBOX_NUMBER_ENTRY *)PoolWithTag;
    if( !PoolWithTag )
      return -1073741670;
    PoolWithTag[4] = PoolWithTag + 7;
    RtlCopySid(v9 - 56, PoolWithTag + 7, Sid);
    ClearBitsAndSet = (unsigned int)RtlFindClearBitsAndSet(p_Bitmap, (VOID *)1, 0i64, v12, v20, v22);
    if( ClearBitsAndSet == -1 )
    {
      v17 = 2 * RtlNumberOfSetBits((UINT64 *)p_Bitmap);
      if( v17 > 0x10000 )
        goto LABEL_19;
      v18 = ExAllocatePoolWithTag(1ui64, (unsigned __int64)v17 >> 3, 1934386515i64);
      if( !v18 )
        goto LABEL_19;
      ExFreePoolWithTag(p_Bitmap->BalancedRoot.LeftChild, 0);
      LODWORD(p_Bitmap->BalancedRoot.Parent) = v17;
      p_Bitmap->BalancedRoot.LeftChild = (_RTL_BALANCED_LINKS *)v18;
      RtlClearAllBits((PRTL_BITMAP)p_Bitmap);
      RtlSetBits((_RTL_BITMAP *)p_Bitmap, 0i64, v17 >> 1);
      ClearBitsAndSet = (unsigned int)RtlFindClearBitsAndSet(p_Bitmap, (VOID *)1, 0i64, v19, v21, v23);
      if( ClearBitsAndSet == -1 )
        goto LABEL_19;
      HashTable = v25;
    }
    if( ClearBitsAndSet != 0xFFFF )
    {
      v11->AtomTable = 0i64;
      v11->LowboxNumber = ClearBitsAndSet + 1;
      v11->ReferenceCount = 1i64;
      v14 = (unsigned int)*((unsigned __int8 *)Sid + 1) - 1;
      v15 = *((unsigned int *)Sid + v14 + 2);
      v16 = v15 + 1;
      if( (_DWORD)v15 )
        v16 = *((unsigned int *)Sid + v14 + 2);
      if( RtlInsertEntryHashTable(HashTable, &v11->HashEntry, v16, 0i64) )
      {
        *MatchingEntry = v11;
        return v6;
      }
      ExFreePoolWithTag(v11, 0);
      RtlClearBits((PRTL_BITMAP)p_Bitmap, 1u, ClearBitsAndSet);
      return -1073741670;
    }
LABEL_19:
    ExFreePoolWithTag(v11, 0);
    return -1073741670;
  }
  if( _InterlockedIncrement64((volatile signed __int64 *)(a3 + 24)) <= 1 )
    __fastfail(0xEu);
  *MatchingEntry = (_SEP_LOWBOX_NUMBER_ENTRY *)a3;
  return 0;
}

Referenced by:

SepSetTokenLowboxNumber