CmDeleteDeviceRegKeyWorker

NTSTATUS __stdcall CmDeleteDeviceRegKeyWorker(
        HPNPCTX *hPnpCtx,
        WCHAR *DeviceInstanceId,
        WCHAR *CmKeyTypeFlags,
        INT64 HardwareProfileId,
        CHAR DeleteTree,
        INT64 a6,
        INT64 a8,
        INT64 F8,
        INT64 a9){
  unsigned int v10; 
  _CM_KEY_TYPE v11; 
  WCHAR *v12; 
  PNPCTX_BASE_KEY_TYPE v13; 
  WCHAR *PoolWithTag; 
  unsigned __int64 v15; 
  int DeviceRegKeyPath; 
  WCHAR *v17; 
  HSYSCTX *v18; 
  unsigned __int16 Length; 
  INT64 v21; 
  int v22; 
  __int64 v23; 
  VOID *v24; 
  VOID *v25; 
  int v26; 
  WORD *v27; 
  WORD *v28; 
  __int64 v29; 
  VOID *v30; 
  _UNICODE_STRING *p_DestinationString; 
  unsigned int v32; 
  NTSTATUS v33; 
  __int64 v34; 
  VOID *v35; 
  __int64 v36; 
  VOID *v37; 
  UINT64 Create; 
  UINT64 Createa; 
  WCHAR *BaseKeyPath; 
  UINT64 BaseKeyLength; 
  UINT64 BaseKeyLengtha; 
  UINT64 *RequiredLength; 
  UINT64 *RequiredLengtha; 
  UINT64 v45; 
  INT64 v46; 
  WCHAR *DeviceInstanceIda; 
  VOID *phkResult; 
  VOID *Handle; 
  _UNICODE_STRING DestinationString; 

  DeviceInstanceIda = DeviceInstanceId;
  LODWORD(v45) = 0;
  v10 = HardwareProfileId;
  v46 = 0i64;
  v11 = (int)CmKeyTypeFlags;
  phkResult = 0i64;
  v12 = DeviceInstanceId;
  Handle = 0i64;
  v13 = PNPCTX_BASE_KEY_TYPE_CONTROLSET;
  PoolWithTag = 0i64;
  DestinationString = 0i64;
  if( !(_DWORD)CmKeyTypeFlags || ((unsigned int)CmKeyTypeFlags & 0xFFFFFCE8) != 0 )
    goto LABEL_85;
  LODWORD(v15) = ((unsigned __int16)CmKeyTypeFlags & 0x200) != 0 ? 360 : 240;
  PoolWithTag = (WCHAR *)ExAllocatePoolWithTag(1ui64, (unsigned int)v15, 1380994640i64);
  if( PoolWithTag )
  {
    while( 1 )
    {
      LODWORD(BaseKeyLength) = (unsigned int)v15 >> 1;
      DeviceRegKeyPath = CmGetDeviceRegKeyPath(hPnpCtx, v12, v11, v10, 0, PoolWithTag, BaseKeyLength, &v45);
      if( DeviceRegKeyPath != -1073741789 )
        break;
      ExFreePoolWithTag(PoolWithTag, 0);
      PoolWithTag = 0i64;
      v15 = 2i64 * (unsigned int)v45;
      if( v15 > 0xFFFFFFFF )
      {
        DeviceRegKeyPath = -1073741675;
        goto LABEL_10;
      }
      PoolWithTag = (WCHAR *)ExAllocatePoolWithTag(1ui64, (unsigned int)v15, 1380994640i64);
      if( !PoolWithTag )
        goto LABEL_60;
      v12 = DeviceInstanceIda;
    }
  }
  else
  {
LABEL_60:
    DeviceRegKeyPath = -1073741801;
  }
  if( DeviceRegKeyPath < 0 )
    goto LABEL_10;
  if( (v11 & 0x100) == 0 )
  {
    DeviceRegKeyPath = RtlInitUnicodeStringEx(&DestinationString, PoolWithTag);
    if( DeviceRegKeyPath < 0 )
      goto LABEL_10;
    Length = DestinationString.Length;
    if( DestinationString.Length < (unsigned int)v15
      && DestinationString.Length > 0x32u
      && (unsigned __int8)RtlPrefixUnicodeString(
                            &`_CmDeleteDeviceContainerRegKeyWorker'::`2'::ObjectPathRootPrefix,
                            &DestinationString,
                            1u) )
    {
      v17 = PoolWithTag + 25;
      DestinationString.MaximumLength -= 50;
      DestinationString.Buffer = PoolWithTag + 25;
      DestinationString.Length = Length - 50;
      if( (unsigned __int8)RtlPrefixUnicodeString(
                              &`_CmDeleteDeviceRegKeyWorker'::`2'::EnumKeyPrefix,
                              &DestinationString,
                              1u) )
      {
        v13 = PNPCTX_BASE_KEY_TYPE_ENUM;
        v17 = PoolWithTag + 30;
      }
      else if( (unsigned __int8)RtlPrefixUnicodeString(
                                   &`_CmDeleteDeviceInterfaceRegKeyWorker'::`2'::HardwareProfilesKeyPrefix,
                                   &DestinationString,
                                   1u) )
      {
        v13 = PNPCTX_BASE_KEY_TYPE_HARDWAREPROFILES|PNPCTX_BASE_KEY_TYPE_SOFTWARE;
        v17 = PoolWithTag + 43;
      }
      DeviceRegKeyPath = PnpCtxGetCachedContextBaseKey(hPnpCtx, v13, (VOID **)&v46);
      if( DeviceRegKeyPath < 0 )
        goto LABEL_10;
      v21 = v46;
      goto LABEL_26;
    }
LABEL_85:
    DeviceRegKeyPath = -1073741811;
    goto LABEL_10;
  }
  v17 = PoolWithTag;
  v18 = 0i64;
  if( hPnpCtx )
    v18 = (HSYSCTX *)*((_QWORD *)hPnpCtx + 28);
  DeviceRegKeyPath = SysCtxRegOpenCurrentUserKey(
                       v18,
                       0,
                       0x2000000,
                       &phkResult,
                       Create,
                       (INT64)BaseKeyPath,
                       BaseKeyLength,
                       (INT64)RequiredLength);
  if( DeviceRegKeyPath < 0 )
    goto LABEL_10;
  v21 = (INT64)phkResult;
LABEL_26:
  v46 = v21;
  LODWORD(Create) = 1;
  DeviceRegKeyPath = CmOpenDeviceRegKey(
                       hPnpCtx,
                       DeviceInstanceIda,
                       CM_KEY_TYPE_DEVICE_INSTANCE,
                       0i64,
                       Create,
                       0,
                       &Handle,
                       0i64);
  if( DeviceRegKeyPath < 0 )
    goto LABEL_10;
  v22 = (unsigned __int8)v11;
  LODWORD(v45) = (unsigned __int8)v11;
  if( (unsigned __int8)v11 == CM_KEY_TYPE_DEVICE_SOFTWARE && (v11 & 0xF00) == 0 )
  {
    *(_DWORD *)&DestinationString.Length = 786;
    p_DestinationString = &DestinationString;
    *(_DWORD *)(&DestinationString.MaximumLength + 1) = 530;
    v32 = 0;
    LODWORD(DestinationString.Buffer) = 274;
    while( 1 )
    {
      v33 = CmDeleteDeviceRegKey((INT64)hPnpCtx, DeviceInstanceIda, *(unsigned int *)&p_DestinationString->Length, 0i64);
      if( v33 )
      {
        if( v33 != -1073741637 && v33 != -1073741772 && v33 != -1073741811 )
          break;
      }
      ++v32;
      p_DestinationString = (_UNICODE_STRING *)((char *)p_DestinationString + 4);
      if( v32 >= 3 )
        goto LABEL_51;
    }
    DeviceRegKeyPath = v33;
LABEL_51:
    if( DeviceRegKeyPath < 0 )
      goto LABEL_10;
    CmDeleteDeviceMappedPropertyForAllDriverKeyRegValues(hPnpCtx, DeviceInstanceIda);
    LODWORD(RequiredLengtha) = 0;
    LODWORD(BaseKeyLengtha) = 0;
    LODWORD(Createa) = 1;
    v26 = CmSetDeviceRegProp(
            hPnpCtx,
            DeviceInstanceIda,
            0i64,
            CM_REG_PROP_DRIVER,
            Createa,
            0i64,
            BaseKeyLengtha,
            (UINT64)RequiredLengtha);
    if( v26 && v26 != -1073741275 && v26 != -1073741810 )
      goto LABEL_36;
    v22 = v45;
  }
  if( DeleteTree )
  {
    if( hPnpCtx && (v23 = *((_QWORD *)hPnpCtx + 28)) != 0 )
      v24 = *(VOID **)(v23 + 8);
    else
      v24 = 0i64;
    v25 = (VOID *)v46;
    v26 = RegRtlDeleteTreeInternal((VOID *)v46, v17, v24, 0);
  }
  else
  {
    if( hPnpCtx && (v34 = *((_QWORD *)hPnpCtx + 28)) != 0 )
      v35 = *(VOID **)(v34 + 8);
    else
      v35 = 0i64;
    v25 = (VOID *)v46;
    v26 = RegRtlDeleteKeyTransacted((VOID *)v46, v17, v35);
  }
  if( v26 != -1073741444 )
  {
    if( v26 < 0 )
      goto LABEL_36;
    if( v22 != 18 && (v22 != 17 || (v11 & 0xF00) != 0) )
    {
      v27 = wcsrchr(v17, 92);
      if( !v27 || (*v27 = 0, (v28 = wcsrchr(v17, 92)) == 0i64) )
      {
        DeviceRegKeyPath = -1073741595;
        goto LABEL_10;
      }
      if( hPnpCtx && (v29 = *((_QWORD *)hPnpCtx + 28)) != 0 )
        v30 = *(VOID **)(v29 + 8);
      else
        v30 = 0i64;
      v26 = RegRtlDeleteKeyTransacted(v25, v17, v30);
      if( v26 != -1073741535 )
      {
        if( v26 < 0
          || ((*v28 = 0, !hPnpCtx) || (v36 = *((_QWORD *)hPnpCtx + 28)) == 0 ? (v37 = 0i64) : (v37 = *(VOID **)(v36 + 8)),
              (v26 = RegRtlDeleteKeyTransacted(v25, v17, v37), (int)(v26 + 0x80000000) >= 0) && v26 != -1073741535) )
        {
LABEL_36:
          DeviceRegKeyPath = v26;
        }
      }
    }
  }
LABEL_10:
  if( Handle )
    ZwClose((_HANDLE)Handle);
  if( phkResult )
    ZwClose((_HANDLE)phkResult);
  if( PoolWithTag )
    ExFreePoolWithTag(PoolWithTag, 0);
  return DeviceRegKeyPath;
}

Referenced by:

CmDeleteDeviceRegKey