PiCMGenerateDeviceInstance

VOID __fastcall PiCMGenerateDeviceInstance(
        WCHAR *PartialDeviceId,
        UINT8 GeneratePhantom,
        WCHAR *FullDeviceId,
        _BYTE *DeviceIdLength,
        INT64 a5,
        UINT64 a6){
  char v6; 
  HANDLE v10; 
  NTSTATUS v11; 
  WCHAR v12; 
  WCHAR *v13; 
  WCHAR **v14; 
  WCHAR **v15; 
  WCHAR *PoolWithTag; 
  int v17; 
  int v18; 
  _ETHREAD *CurrentThread; 
  __int64 v20; 
  UINT64 *pcchRemaining; 
  UINT64 *pcchRemaininga; 
  size_t *pcchRemainingb; 
  UINT64 dwFlags; 
  UINT64 dwFlagsa; 
  UINT64 dwFlagsb; 
  UINT64 dwFlagsc; 
  int v28; 
  int v29; 
  int v30; 
  HANDLE KeyHandle; 
  size_t pcchLength; 
  struct _UNICODE_STRING DestinationString; 
  int v34; 
  size_t v35; 
  LODWORD(v35) = (_DWORD)DeviceIdLength;
  v6 = 0;
  *FullDeviceId = 0;
  v30 = 0;
  v29 = 0;
  v28 = 0;
  v34 = 0;
  KeyHandle = 0i64;
  DestinationString = 0i64;
  v10 = 0i64;
  v11 = RtlStringCchLengthW(PartialDeviceId, 0xC8ui64, &pcchLength);
  if( v11 >= 0 )
  {
    v12 = *PartialDeviceId;
    v13 = PartialDeviceId;
    while( v12 )
    {
      if( (unsigned __int16)(v12 - 33) > 0x5Eu || v12 == 44 || v12 == 92 )
      {
        v11 = -1073741773;
        break;
      }
      v12 = *++v13;
    }
    if( v11 >= 0 )
    {
      LODWORD(dwFlags) = 2048;
      if( (int)RtlStringCchCopyExW(FullDeviceId, 0xC8ui64, (WCHAR *)L"Root", 0i64, 0i64, dwFlags) >= 0
        && (int)RtlInitUnicodeStringEx(&DestinationString, FullDeviceId) >= 0
        && RtlUpcaseUnicodeString(&DestinationString, &DestinationString, 0) >= 0
        && (int)RtlStringCchCatExW(FullDeviceId, 0xC8ui64, L"\\", v14, pcchRemaining, dwFlagsa) >= 0
        && (int)RtlStringCchCatExW(FullDeviceId, 0xC8ui64, PartialDeviceId, v15, pcchRemaininga, dwFlagsb) >= 0 )
      {
        PoolWithTag = (WCHAR *)ExAllocatePoolWithTag(PagedPool, 0x190ui64, 0x34706E50ui64);
        if( PoolWithTag )
        {
          v17 = 0;
          while( 1 )
          {
            if( v10 )
            {
              ZwClose(v10);
              v10 = 0i64;
              KeyHandle = 0i64;
            }
            LODWORD(pcchRemainingb) = 2048;
            RtlStringCchPrintfExW(PoolWithTag, 0xC8ui64, 0i64, 0i64, (UINT64)pcchRemainingb, L"%s\\%04u");
            if( v18 < 0 )
              break;
            if( (int)CmGetDeviceStatus(PiPnpRtlCtx, PoolWithTag, 0i64, &v30, &v29, &v28, (unsigned int)FullDeviceId) < 0 )
            {
              CurrentThread = (_ETHREAD *)KeGetCurrentThread();
              --*((_WORD *)CurrentThread + 242);
              ExAcquireResourceExclusiveLite(&PnpRegistryDeviceResource, 1u);
              v6 = 1;
              LOBYTE(v35) = 0;
              if( (int)CmCreateDevice(PiPnpRtlCtx, PoolWithTag, 131078, &KeyHandle, &v35, 0) < 0 )
                goto LABEL_35;
              if( (_BYTE)v35 )
              {
                v10 = KeyHandle;
                if( (int)CmValidateDeviceName(v20, PoolWithTag) < 0
                  || (LODWORD(dwFlagsc) = 2048,
                      (int)RtlStringCchCopyExW(FullDeviceId, 0xC8ui64, PoolWithTag, 0i64, 0i64, dwFlagsc) < 0) )
                {
                  CmDeleteDevice(PiPnpRtlCtx, PoolWithTag, 0);
                }
                else if( GeneratePhantom )
                {
                  v34 = 1;
                  RegRtlSetValue(v10, L"Phantom", 4u, &v34, 4u);
                }
LABEL_34:
                if( !v6 )
                  break;
LABEL_35:
                ExReleaseResourceLite(&PnpRegistryDeviceResource);
                KeLeaveCriticalRegionThread((__int64)KeGetCurrentThread());
                v10 = KeyHandle;
                break;
              }
              ExReleaseResourceLite(&PnpRegistryDeviceResource);
              KeLeaveCriticalRegionThread((__int64)KeGetCurrentThread());
              v10 = KeyHandle;
              v6 = 0;
            }
            if( (unsigned int)++v17 > 0x270F )
              goto LABEL_34;
            v6 = 0;
          }
          if( v10 )
            ZwClose(v10);
          ExFreePoolWithTag(PoolWithTag, 0x34706E50u);
        }
      }
    }
  }
}

Referenced by:

PiCMCreateDevice