CmOpenCommonClassRegKeyWorker

NTSTATUS __stdcall CmOpenCommonClassRegKeyWorker(
        HPNPCTX *hPnpCtx,
        WCHAR *ClassGuid,
        _CM_KEY_TYPE CmKeyTypeFlags,
        UINT64 HardwareProfileId,
        UINT64 DesiredAccess,
        UINT8 Create,
        VOID **ClassKey,
        UINT64 *Disposition){
  WCHAR *v8; 
  INT16 v9; 
  PNPCTX_BASE_KEY_TYPE v11; 
  unsigned int v12; 
  unsigned __int64 v13; 
  wchar_t *PoolWithTag; 
  int v15; 
  int inited; 
  unsigned __int16 Length; 
  WCHAR *v18; 
  VOID *v19; 
  __int64 v20; 
  VOID **v21; 
  NTSTATUS Tree; 
  UINT64 *v23; 
  HSYSCTX *v25; 
  INT64 v26; 
  INT64 pszDest; 
  INT64 cchDest; 
  INT64 v29; 
  INT64 v30; 
  VOID *Handle; 
  _UNICODE_STRING DestinationString; 
  INT64 v34; 

  v8 = ClassGuid;
  LODWORD(v34) = 0;
  v9 = CmKeyTypeFlags;
  v30 = 0i64;
  Handle = 0i64;
  v11 = PNPCTX_BASE_KEY_TYPE_CONTROLSET;
  v12 = HardwareProfileId;
  DestinationString = 0i64;
  LODWORD(v13) = (CmKeyTypeFlags & 0x200) != 0 ? 320 : 200;
  PoolWithTag = (wchar_t *)ExAllocatePoolWithTag(1ui64, (unsigned int)v13, 1380994640i64);
  if( PoolWithTag )
  {
    while( 1 )
    {
      CmGetCommonClassRegKeyPath(
        (unsigned int)v13 >> 1,
        v8,
        v9,
        v12,
        v26,
        (_NTSTRSAFE_PWSTR)PoolWithTag,
        (unsigned int)v13 >> 1,
        (INT64)&v34);
      inited = v15;
      if( v15 != -1073741789 )
        break;
      ExFreePoolWithTag(PoolWithTag, 0);
      PoolWithTag = 0i64;
      v13 = 2i64 * (unsigned int)v34;
      if( v13 > 0xFFFFFFFF )
      {
        inited = -1073741675;
        goto LABEL_21;
      }
      PoolWithTag = (wchar_t *)ExAllocatePoolWithTag(1ui64, (unsigned int)v13, 1380994640i64);
      if( !PoolWithTag )
        goto LABEL_36;
      v8 = ClassGuid;
    }
  }
  else
  {
LABEL_36:
    inited = -1073741801;
  }
  if( inited < 0 )
    goto LABEL_21;
  if( (v9 & 0x100) != 0 )
  {
    v18 = PoolWithTag;
    if( hPnpCtx )
      v25 = (HSYSCTX *)*((_QWORD *)hPnpCtx + 28);
    else
      v25 = 0i64;
    inited = SysCtxRegOpenCurrentUserKey(v25, 0, 0x2000000, &Handle, v26, pszDest, cchDest, v29);
    if( inited < 0 )
      goto LABEL_21;
    v19 = Handle;
    v30 = (INT64)Handle;
  }
  else
  {
    inited = RtlInitUnicodeStringEx(&DestinationString, PoolWithTag);
    if( inited < 0 )
      goto LABEL_21;
    Length = DestinationString.Length;
    if( DestinationString.Length >= (unsigned int)v13
      || DestinationString.Length <= 0x32u
      || !(unsigned __int8)RtlPrefixUnicodeString(
                             &`_CmDeleteDeviceContainerRegKeyWorker'::`2'::ObjectPathRootPrefix,
                             &DestinationString,
                             1u) )
    {
      inited = -1073741811;
      goto LABEL_21;
    }
    v18 = PoolWithTag + 25;
    DestinationString.MaximumLength -= 50;
    DestinationString.Buffer = PoolWithTag + 25;
    DestinationString.Length = Length - 50;
    if( (unsigned __int8)RtlPrefixUnicodeString(
                            &`_CmOpenCommonClassRegKeyWorker'::`2'::ClassKeyPrefix,
                            &DestinationString,
                            1u) )
    {
      v11 = PNPCTX_BASE_KEY_TYPE_CLASS;
      v18 = PoolWithTag + 39;
    }
    else if( (unsigned __int8)RtlPrefixUnicodeString(
                                 &`_CmDeleteDeviceInterfaceRegKeyWorker'::`2'::DeviceClassesKeyPrefix,
                                 &DestinationString,
                                 1u) )
    {
      v11 = PNPCTX_BASE_KEY_TYPE_DEVICECLASSES;
      v18 = PoolWithTag + 47;
    }
    else if( (unsigned __int8)RtlPrefixUnicodeString(
                                 &`_CmOpenDeviceInterfaceRegKeyWorker'::`2'::HardwareProfilesKeyPrefix,
                                 &DestinationString,
                                 1u) )
    {
      v11 = PNPCTX_BASE_KEY_TYPE_HARDWAREPROFILES|PNPCTX_BASE_KEY_TYPE_SOFTWARE;
      v18 = PoolWithTag + 43;
    }
    inited = PnpCtxGetCachedContextBaseKey(hPnpCtx, v11, (VOID **)&v30);
    if( inited < 0 )
      goto LABEL_21;
    v19 = (VOID *)v30;
  }
  if( Create )
  {
    LODWORD(v26) = DesiredAccess;
    Tree = PnpCtxRegCreateTree(hPnpCtx, v19, v18, 0i64, v26, 0i64, ClassKey, Disposition);
LABEL_19:
    if( Tree != -1073741444 )
    {
      if( Tree >= 0 )
        goto LABEL_21;
      goto LABEL_30;
    }
    inited = -1073741595;
    goto LABEL_21;
  }
  if( hPnpCtx )
    v20 = *((_QWORD *)hPnpCtx + 28);
  else
    v20 = 0i64;
  v21 = ClassKey;
  Tree = SysCtxRegOpenKey(v20, v19, v18, 0, DesiredAccess, ClassKey);
  v23 = Disposition;
  if( Tree >= 0 )
    *(_DWORD *)Disposition = 2;
  if( Tree != -1073741772 )
    goto LABEL_19;
  if( v11 != PNPCTX_BASE_KEY_TYPE_DEVICECLASSES || !*((_BYTE *)hPnpCtx + 4) )
  {
LABEL_30:
    inited = Tree;
    goto LABEL_21;
  }
  inited = PnpCtxGetCachedContextBaseKey(hPnpCtx, PNPCTX_BASE_KEY_TYPE_DEVICECONTAINERS, (VOID **)&v30);
  if( inited >= 0 )
  {
    Tree = SysCtxRegOpenKey(*((_QWORD *)hPnpCtx + 28), (VOID *)v30, v18, 0, DesiredAccess, v21);
    if( Tree >= 0 )
      *(_DWORD *)v23 = 2;
    goto LABEL_19;
  }
LABEL_21:
  if( Handle )
    ZwClose((_HANDLE)Handle);
  if( PoolWithTag )
    ExFreePoolWithTag(PoolWithTag, 0);
  return inited;
}

Referenced by:

CmOpenCommonClassRegKey