PiCMGenerateDeviceInstance

NTSTATUS __stdcall PiCMGenerateDeviceInstance(WCHAR *PartialDeviceId, UINT8 GeneratePhantom, WCHAR *FullDeviceId){
  int v3; 
  char v4; 
  VOID *v8; 
  int inited; 
  WCHAR v10; 
  WCHAR *v11; 
  WCHAR **v12; 
  WCHAR **v13; 
  VOID **PoolWithTag; 
  int v15; 
  INT64 v16; 
  _ETHREAD *CurrentThread; 
  WCHAR *v18; 
  UINT64 *pcchRemaining; 
  UINT64 *pcchRemaininga; 
  size_t *pcchRemainingb; 
  size_t *pcchRemainingc; 
  UINT64 dwFlags; 
  UINT64 dwFlagsa; 
  UINT64 dwFlagsb; 
  UINT8 *dwFlagsc; 
  UINT64 dwFlagsd; 
  INT64 Flags; 
  INT64 a8; 
  INT64 ProblemStatus; 
  UINT64 DeviceStatus; 
  INT64 DeviceKey; 
  UINT64 pcchLength; 
  _UNICODE_STRING DestinationString; 
  int v36; 
  VOID *Created; 

  LODWORD(Created) = v3;
  v4 = 0;
  *FullDeviceId = 0;
  LODWORD(DeviceStatus) = 0;
  ProblemStatus = 0i64;
  v36 = 0;
  DeviceKey = 0i64;
  DestinationString = 0i64;
  v8 = 0i64;
  inited = RtlStringCchLengthW(PartialDeviceId, 0xC8ui64, &pcchLength);
  if( inited >= 0 )
  {
    v10 = *PartialDeviceId;
    v11 = PartialDeviceId;
    while( v10 )
    {
      if( (unsigned __int16)(v10 - 33) > 0x5Eu || v10 == 44 || v10 == 92 )
      {
        inited = -1073741773;
        break;
      }
      v10 = *++v11;
    }
    if( inited >= 0 )
    {
      LODWORD(dwFlags) = 2048;
      inited = RtlStringCchCopyExW(FullDeviceId, 0xC8ui64, (WCHAR *)L"Root", 0i64, 0i64, dwFlags);
      if( inited >= 0 )
      {
        inited = RtlInitUnicodeStringEx(&DestinationString, FullDeviceId);
        if( inited >= 0 )
        {
          inited = RtlUpcaseUnicodeString(&DestinationString, &DestinationString, 0);
          if( inited >= 0 )
          {
            inited = RtlStringCchCatExW(FullDeviceId, 0xC8ui64, (WCHAR *)L"\\", v12, pcchRemaining, dwFlagsa);
            if( inited >= 0 )
            {
              inited = RtlStringCchCatExW(FullDeviceId, 0xC8ui64, PartialDeviceId, v13, pcchRemaininga, dwFlagsb);
              if( inited >= 0 )
              {
                PoolWithTag = ExAllocatePoolWithTag(1ui64, 0x190ui64, 879783504i64);
                if( !PoolWithTag )
                  return -1073741670;
                v15 = 0;
                while( 1 )
                {
                  if( v8 )
                  {
                    ZwClose((_HANDLE)v8);
                    v8 = 0i64;
                    DeviceKey = 0i64;
                  }
                  LODWORD(a8) = v15;
                  LODWORD(pcchRemainingb) = 2048;
                  inited = RtlStringCchPrintfExW(
                             (WCHAR *)PoolWithTag,
                             0xC8ui64,
                             0i64,
                             0i64,
                             (UINT64)pcchRemainingb,
                             (WCHAR *)L"%s\\%04u");
                  if( inited < 0 )
                    break;
                  if( CmGetDeviceStatus(
                         *(&stru_140CF2E80 + 607),
                         (const WCHAR *)PoolWithTag,
                         0i64,
                         &DeviceStatus,
                         (UINT64 *)((char *)&ProblemStatus + 4),
                         &ProblemStatus,
                         (UINT64)FullDeviceId) < 0 )
                  {
                    CurrentThread = (_ETHREAD *)KeGetCurrentThread();
                    --CurrentThread->Tcb.KernelApcDisable;
                    ExAcquireResourceExclusiveLite((UINT64)&PnpReplaceEvent + 6688, 1, v16);
                    LODWORD(dwFlagsc) = 0;
                    v4 = 1;
                    LOBYTE(Created) = 0;
                    inited = CmCreateDevice(
                               *(&stru_140CF2E80 + 607),
                               (WCHAR *)PoolWithTag,
                               (WCHAR *)0x20006,
                               (INT64)&DeviceKey,
                               &Created,
                               dwFlagsc,
                               Flags,
                               a8,
                               ProblemStatus);
                    if( inited < 0 )
                      goto LABEL_36;
                    if( (_BYTE)Created )
                    {
                      v8 = (VOID *)DeviceKey;
                      inited = CmValidateDeviceName(v18);
                      if( inited < 0
                        || (LODWORD(dwFlagsd) = 2048,
                            inited = RtlStringCchCopyExW(
                                       FullDeviceId,
                                       0xC8ui64,
                                       (WCHAR *)PoolWithTag,
                                       0i64,
                                       0i64,
                                       dwFlagsd),
                            inited < 0) )
                      {
                        CmDeleteDevice(*(&stru_140CF2E80 + 607), (WCHAR *)PoolWithTag, 0i64);
                      }
                      else if( GeneratePhantom )
                      {
                        v36 = 1;
                        LODWORD(pcchRemainingc) = 4;
                        inited = RegRtlSetValue(v8, L"Phantom", 4u, &v36, (UINT64)pcchRemainingc);
                      }
LABEL_35:
                      if( !v4 )
                        break;
LABEL_36:
                      ExReleaseResourceLite((PERESOURCE)((char *)&PnpReplaceEvent + 6688));
                      KeLeaveCriticalRegionThread(KeGetCurrentThread());
                      v8 = (VOID *)DeviceKey;
                      break;
                    }
                    ExReleaseResourceLite((PERESOURCE)((char *)&PnpReplaceEvent + 6688));
                    KeLeaveCriticalRegionThread(KeGetCurrentThread());
                    v8 = (VOID *)DeviceKey;
                    v4 = 0;
                  }
                  if( (unsigned int)++v15 > 0x270F )
                  {
                    inited = -2147483622;
                    goto LABEL_35;
                  }
                  v4 = 0;
                }
                if( v8 )
                  ZwClose((_HANDLE)v8);
                ExFreePoolWithTag(PoolWithTag, 0x34706E50u);
              }
            }
          }
        }
      }
    }
  }
  if( inited == -2147483643 )
    return -1073741789;
  return inited;
}

Referenced by:

PiCMCreateDevice