SepAddLuidToIndexEntry

INT64 __fastcall SepAddLuidToIndexEntry(UINT64 *Luid, UINT64 *Index, _SEP_LUID_TO_INDEX_MAP_ENTRY **RefEntry){
  UINT64 *v4; 
  UINT64 v5; 
  _SEP_LUID_TO_INDEX_MAP_ENTRY *PoolWithTag; 
  _ETHREAD *CurrentThread; 
  struct _RTL_BITMAP *v8; 
  unsigned int ClearBitsAndSet; 
  int v10; 
  UINT64 IndexIntoGlobalSingletonTable; 
  volatile INT64 *v12; 
  char v13; 
  ULONG v15; 
  unsigned int *v16; 
  RTL_DYNAMIC_HASH_TABLE *HashTable; 
  *RefEntry = 0i64;
  v4 = Luid;
  v5 = 1i64;
  PoolWithTag = (_SEP_LUID_TO_INDEX_MAP_ENTRY *)ExAllocatePoolWithTag(PagedPool, 0x38ui64, 0x74446553ui64);
  if( !PoolWithTag )
    return(unsigned int)-1073741801;
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  --*((_WORD *)CurrentThread + 242);
  ExAcquirePushLockExclusiveEx(SeLuidToIndexMapping, 0i64);
  v8 = (struct _RTL_BITMAP *)(SeLuidToIndexMapping + 16);
  HashTable = *(RTL_DYNAMIC_HASH_TABLE **)(SeLuidToIndexMapping + 8);
  ClearBitsAndSet = RtlFindClearBitsAndSet((PRTL_BITMAP)(SeLuidToIndexMapping + 16), 1u, 0);
  if( ClearBitsAndSet == -1 )
  {
    SepCleanupMarkedForDeletionEntries();
    ClearBitsAndSet = RtlFindClearBitsAndSet(v8, 1u, 0);
    if( ClearBitsAndSet == -1 )
    {
      v15 = RtlNumberOfSetBits(v8) + 64;
      v16 = (unsigned int *)ExAllocatePoolWithTag(PagedPool, (unsigned __int64)v15 >> 3, 0x74446553ui64);
      if( !v16 )
      {
        v10 = -1073741801;
        goto LABEL_17;
      }
      v10 = SepExpandSingletonArrays();
      if( v10 < 0 )
        goto LABEL_17;
      ExFreePoolWithTag(v8->Buffer, 0);
      v8->SizeOfBitMap = v15;
      v8->Buffer = v16;
      RtlClearAllBits(v8);
      RtlSetBits(v8, 0i64, v15 - 64);
      v4 = Luid;
      ClearBitsAndSet = RtlFindClearBitsAndSet(v8, 1u, 0);
    }
  }
  PoolWithTag->ReferenceCount = 1i64;
  PoolWithTag->IndexIntoGlobalSingletonTable = ClearBitsAndSet;
  PoolWithTag->Luid = *v4;
  PoolWithTag->MarkedForDeletion = 0;
  if( *v4 )
    v5 = *v4;
  if( !RtlInsertEntryHashTable(HashTable, &PoolWithTag->HashEntry, v5, 0i64) )
  {
    v10 = -1073741801;
    _bittestandreset((signed __int32 *)v8->Buffer, ClearBitsAndSet);
    v16 = 0i64;
LABEL_17:
    ExFreePoolWithTag(PoolWithTag, 0);
    if( v16 )
      ExFreePoolWithTag(v16, 0);
    goto LABEL_7;
  }
  v10 = 0;
  SepInitSingletonEntry(ClearBitsAndSet, *v4);
  IndexIntoGlobalSingletonTable = PoolWithTag->IndexIntoGlobalSingletonTable;
  *RefEntry = PoolWithTag;
  *Index = IndexIntoGlobalSingletonTable;
LABEL_7:
  v12 = (volatile INT64 *)SeLuidToIndexMapping;
  v13 = _InterlockedExchangeAdd64((volatile signed __int64 *)SeLuidToIndexMapping, 0xFFFFFFFFFFFFFFFFui64);
  if( (v13 & 2) != 0 && (v13 & 4) == 0 )
    ExfTryToWakePushLock(v12);
  KeAbPostRelease((PVOID)v12);
  KeLeaveCriticalRegionThread((__int64)KeGetCurrentThread());
  return(unsigned int)v10;
}

Referenced by:

SepSetProcessUniqueAttribute