PiDevCfgResolveVariableKeyHandle

NTSTATUS __stdcall PiDevCfgResolveVariableKeyHandle(
        PI_DEVCFG_RESOLVE_CONTEXT *ResolveContext,
        VOID *VariableKeyHandle,
        VOID **KeyHandleResult){
  unsigned int v5; 
  NTSTATUS RegistryValue; 
  _KEY_VALUE_FULL_INFORMATION *v7; 
  int DeviceRegProp; 
  __int64 v9; 
  const wchar_t *v10; 
  const wchar_t **v11; 
  INT64 v12; 
  INT64 v13; 
  UINT8 *v14; 
  wchar_t *v15; 
  wchar_t **v16; 
  INT64 v17; 
  __int64 v18; 
  NTSTATUS v19; 
  INT64 v20; 
  INT64 v21; 
  NTSTATUS v22; 
  WORD *v23; 
  WORD *v24; 
  VOID **PoolWithTag; 
  WORD *v26; 
  unsigned int v27; 
  WORD *v28; 
  WORD *v29; 
  __int64 v30; 
  int v31; 
  int v32; 
  _PI_DEVCFG_VARIABLE_ENTRY **v33; 
  _PI_DEVCFG_VARIABLE_ENTRY *v34; 
  unsigned int v35; 
  __int64 v36; 
  WORD *v37; 
  __int64 v38; 
  unsigned int MaximumLength; 
  WORD *v40; 
  wchar_t *Buffer; 
  unsigned int v42; 
  WORD *v43; 
  WORD *v44; 
  WORD *v45; 
  __int64 v46; 
  size_t v47; 
  __int64 v48; 
  unsigned int v49; 
  __int64 v50; 
  WORD *v51; 
  __int64 v52; 
  _UNICODE_STRING v53; 
  unsigned int DataLength; 
  _WORD *v55; 
  void *v56; 
  UINT64 DesiredAccess; 
  INT64 DesiredAccessa; 
  INT64 Create; 
  VOID **DeviceKey; 
  UINT64 *Disposition; 
  _KEY_VALUE_FULL_INFORMATION *Information; 
  _HANDLE Handle[2]; 
  _UNICODE_STRING DestinationString; 
  unsigned int v66; 
  _OBJECT_ATTRIBUTES ObjectAttributes; 
  _PI_DEVCFG_RESOLVE_CONTEXT *ResolveContexta; 
  UINT64 PropertySize; 
  WORD *v70; 
  _UNICODE_STRING UnicodeString; 
  PVOID P; 
  VOID *v73; 
  VOID *KeyHandle; 
  VOID **v75; 
  UINT8 PropertyBuffer[76]; 
  __int16 v77; 

  v75 = KeyHandleResult;
  *KeyHandleResult = 0i64;
  ResolveContexta = ResolveContext;
  Information = 0i64;
  *(_QWORD *)Handle = 0i64;
  KeyHandle = 0i64;
  v73 = 0i64;
  PropertySize = 0i64;
  DestinationString = 0i64;
  v5 = 0;
  P = 0i64;
  *(_QWORD *)&UnicodeString.Length = 0i64;
  UnicodeString.Buffer = 0i64;
  RegistryValue = IopGetRegistryValue(VariableKeyHandle, (PWCHAR)L"KeyRoot", 0i64, &Information);
  v7 = Information;
  DeviceRegProp = RegistryValue;
  if( RegistryValue < 0 )
    goto LABEL_105;
  if( !PnpValidateRegistryString(Information) )
  {
LABEL_3:
    DeviceRegProp = -1073741823;
    goto LABEL_105;
  }
  v9 = 0i64;
  v10 = (const wchar_t *)((char *)v7 + v7->DataOffset);
  v11 = (const wchar_t **)&off_140985B90;
  while( wcsicmp(*v11, v10) )
  {
    v9 = (unsigned int)(v9 + 1);
    v11 += 4;
    if( (unsigned int)v9 >= 7 )
      goto LABEL_7;
  }
  v16 = &(&off_140985B90)[4 * v9];
  if( v16 )
  {
    v17 = *((unsigned int *)v16 + 4);
    v14 = 0i64;
    v18 = *(_QWORD *)ResolveContext;
    if( !(_DWORD)v17 )
    {
      v20 = *((unsigned int *)v16 + 3);
      if( !(_DWORD)v20 )
      {
        DeviceRegProp = -1073741595;
        goto LABEL_105;
      }
      if( (_DWORD)v20 == 7 )
      {
        LODWORD(Disposition) = 0;
        LODWORD(PropertySize) = 78;
        DeviceRegProp = CmGetDeviceRegProp(
                          *(&stru_140CF2E80 + 607),
                          *(const WCHAR **)(v18 + 48),
                          0i64,
                          9i64,
                          (UINT64 *)((char *)&PropertySize + 4),
                          PropertyBuffer,
                          &PropertySize,
                          (UINT64)Disposition);
        if( DeviceRegProp < 0 )
          goto LABEL_105;
        if( HIDWORD(PropertySize) != 1 )
          goto LABEL_3;
        v20 = *((unsigned int *)v16 + 3);
        v77 = 0;
        v14 = PropertyBuffer;
      }
      else
      {
        v14 = (UINT8 *)v16[3];
      }
      DeviceRegProp = PnpCtxOpenContextBaseKey(v12, v20, v17, 0x20019ui64, Handle);
      if( DeviceRegProp < 0 )
        goto LABEL_105;
      if( v14 )
      {
        RtlInitUnicodeString(&DestinationString, (PCWSTR)v14);
        ObjectAttributes.RootDirectory = *(void **)Handle;
        *(_QWORD *)&ObjectAttributes.Length = 48i64;
        ObjectAttributes.ObjectName = &DestinationString;
        *(_QWORD *)&ObjectAttributes.Attributes = 576i64;
        v14 = 0i64;
        *(_OWORD *)&ObjectAttributes.SecurityDescriptor = 0i64;
        DeviceRegProp = ZwOpenKey(
                          &KeyHandle,
                          0x20019ui64,
                          &ObjectAttributes,
                          v21,
                          DesiredAccessa,
                          Create,
                          (INT64)DeviceKey,
                          (INT64)Disposition);
        if( DeviceRegProp < 0 )
          goto LABEL_105;
        ZwClose(Handle[0]);
        *(_QWORD *)Handle = KeyHandle;
      }
      goto LABEL_31;
    }
    LODWORD(DesiredAccess) = 131097;
    v19 = CmOpenDeviceRegKey(
            *(&stru_140CF2E80 + 607),
            *(WCHAR **)(v18 + 48),
            (_CM_KEY_TYPE)v17,
            0i64,
            DesiredAccess,
            0,
            (VOID **)Handle,
            0i64);
  }
  else
  {
LABEL_7:
    v14 = 0i64;
    if( wcsicmp(v10, L"SYSTEM") )
    {
      if( wcsicmp(v10, L"SOFTWARE") )
      {
        if( wcsicmp(v10, L"HARDWARE") )
        {
          DeviceRegProp = -1073741772;
          goto LABEL_105;
        }
        v15 = L"\\Registry\\Machine\\HARDWARE";
      }
      else
      {
        v15 = L"\\Registry\\Machine\\SOFTWARE";
      }
      *(_DWORD *)&DestinationString.Length = 3538996;
    }
    else
    {
      *(_DWORD *)&DestinationString.Length = 3276848;
      v15 = L"\\Registry\\Machine\\SYSTEM";
    }
    DestinationString.Buffer = v15;
    *(_QWORD *)&ObjectAttributes.Length = 48i64;
    ObjectAttributes.ObjectName = &DestinationString;
    *(_QWORD *)&ObjectAttributes.Attributes = 576i64;
    ObjectAttributes.RootDirectory = 0i64;
    *(_OWORD *)&ObjectAttributes.SecurityDescriptor = 0i64;
    v19 = ZwOpenKey(
            (VOID **)Handle,
            0x20019ui64,
            &ObjectAttributes,
            v13,
            DesiredAccess,
            Create,
            (INT64)DeviceKey,
            (INT64)Disposition);
  }
  DeviceRegProp = v19;
  if( v19 < 0 )
    goto LABEL_105;
LABEL_31:
  ExFreePoolWithTag(v7, 0);
  Information = (_KEY_VALUE_FULL_INFORMATION *)v14;
  v22 = IopGetRegistryValue(VariableKeyHandle, (PWCHAR)L"KeyPath", 0i64, &Information);
  v7 = Information;
  DeviceRegProp = v22;
  if( v22 < 0 )
  {
    if( v22 != -1073741772 )
      goto LABEL_105;
    v56 = *(void **)Handle;
    DeviceRegProp = (int)v14;
    v73 = *(VOID **)Handle;
    *(_QWORD *)Handle = v14;
    goto LABEL_103;
  }
  if( !PnpValidateRegistryString(Information) )
    goto LABEL_3;
  v70 = (WORD *)((char *)v7 + v7->DataOffset);
  v23 = v70;
  v24 = wcschr(v70, 36);
  if( !v24 )
    goto LABEL_98;
  do
  {
    ++v5;
    v24 = wcschr(v24 + 1, 36);
  }
  while( v24 );
  LODWORD(Information) = v5;
  if( !v5 )
  {
LABEL_98:
    DataLength = v7->DataLength;
    v55 = (_WORD *)((char *)v7 + v7->DataOffset);
    LODWORD(Information) = 0;
    PnpRegSzToString(v55, DataLength, (int *)&Information);
    DestinationString.Length = (unsigned __int16)Information;
    DestinationString.MaximumLength = v7->DataLength;
    DestinationString.Buffer = (wchar_t *)((char *)v7 + v7->DataOffset);
LABEL_99:
    ObjectAttributes.RootDirectory = *(void **)Handle;
    *(_QWORD *)&ObjectAttributes.Length = 48i64;
    ObjectAttributes.ObjectName = &DestinationString;
    *(_QWORD *)&ObjectAttributes.Attributes = 576i64;
    *(_OWORD *)&ObjectAttributes.SecurityDescriptor = 0i64;
    DeviceRegProp = ZwOpenKey(
                      &v73,
                      0x20019ui64,
                      &ObjectAttributes,
                      (INT64)v40,
                      DesiredAccessa,
                      Create,
                      (INT64)DeviceKey,
                      (INT64)Disposition);
    if( DeviceRegProp < 0 )
      goto LABEL_105;
    v56 = v73;
LABEL_103:
    *v75 = v56;
    goto LABEL_105;
  }
  PoolWithTag = ExAllocatePoolWithTag(1ui64, 8i64 * v5, 1667526736i64);
  P = PoolWithTag;
  if( !PoolWithTag )
  {
LABEL_37:
    DeviceRegProp = -1073741670;
    goto LABEL_105;
  }
  memset(PoolWithTag, 0i64, 8 * v5);
  v26 = v23;
  v27 = 0;
  if( *v23 )
  {
    while( v27 < (unsigned int)Information )
    {
      v28 = wcschr(v26, 92);
      v29 = v28;
      if( v28 )
      {
        *v28 = 0;
        v30 = v28 - v26;
      }
      else
      {
        v30 = -1i64;
        do
          ++v30;
        while( v26[v30] );
      }
      v31 = 2 * v30;
      v32 = 2 * v30;
      if( *v26 == 36 )
      {
        v33 = (_PI_DEVCFG_VARIABLE_ENTRY **)((char *)P + 8 * v27);
        DeviceRegProp = PiDevCfgResolveVariable(ResolveContexta, v26 + 1, v33);
        if( DeviceRegProp < 0 )
        {
          if( DeviceRegProp != -1073741772 )
            break;
          v32 = v31;
          DeviceRegProp = 0;
        }
        else
        {
          v34 = *v33;
          v32 = v31;
          v35 = (*v33)[8];
          if( v35 )
          {
            if( v35 <= 2 )
            {
              v32 = v34[9] - 2;
            }
            else
            {
              v32 = v31;
              if( v35 == 7 )
              {
                v36 = -1i64;
                do
                  ++v36;
                while( *(_WORD *)(*((_QWORD *)v34 + 5) + 2 * v36) );
                v32 = 2 * v36;
              }
            }
          }
        }
        ++v27;
      }
      if( (unsigned __int64)(v32 + (unsigned int)UnicodeString.MaximumLength) + 2 >= 0xFFFE )
      {
        DeviceRegProp = -2147483643;
        break;
      }
      UnicodeString.MaximumLength += v32 + 2;
      if( v29 )
      {
        *v29 = 92;
        v37 = v29 + 1;
      }
      else
      {
        v38 = -1i64;
        do
          ++v38;
        while( v26[v38] );
        v37 = &v26[v38];
      }
      v26 = v37;
      if( !*v37 )
        break;
    }
    v23 = v70;
  }
  if( DeviceRegProp >= 0 )
  {
    MaximumLength = UnicodeString.MaximumLength;
    UnicodeString.Length = UnicodeString.MaximumLength - 2;
    UnicodeString.Buffer = (wchar_t *)ExAllocatePoolWithTag(1ui64, UnicodeString.MaximumLength, 1667526736i64);
    Buffer = UnicodeString.Buffer;
    if( !UnicodeString.Buffer )
      goto LABEL_37;
    v42 = 0;
    v43 = v23;
    LODWORD(ResolveContexta) = 0;
    v66 = MaximumLength;
    if( *v23 )
    {
      while( v42 < (unsigned int)Information )
      {
        v44 = wcschr(v43, 92);
        v45 = v44;
        if( v44 )
        {
          *v44 = 0;
          v46 = v44 - v43;
        }
        else
        {
          v46 = -1i64;
          do
            ++v46;
          while( v43[v46] );
        }
        v40 = v43;
        if( v43 != v23 )
        {
          if( MaximumLength <= 2 )
            goto LABEL_3;
          *Buffer++ = 92;
          MaximumLength -= 2;
          v66 = MaximumLength;
        }
        v47 = 2 * v46;
        if( *v43 == 36 )
        {
          v48 = *((_QWORD *)P + (unsigned int)ResolveContexta);
          LODWORD(ResolveContexta) = (_DWORD)ResolveContexta + 1;
          if( v48 )
          {
            v49 = *(_DWORD *)(v48 + 32);
            if( v49 )
            {
              if( v49 <= 2 )
              {
                v40 = *(WORD **)(v48 + 40);
                v47 = *(_DWORD *)(v48 + 36) - 2;
              }
              else if( v49 == 7 )
              {
                v40 = *(WORD **)(v48 + 40);
                v50 = -1i64;
                do
                  ++v50;
                while( v40[v50] );
                v47 = 2 * v50;
              }
            }
          }
        }
        if( MaximumLength <= v47 )
        {
          DeviceRegProp = -1073741823;
          break;
        }
        memmove(Buffer, v40, v47);
        Buffer += (unsigned __int64)v47 >> 1;
        MaximumLength = v66 - v47;
        v66 -= v47;
        if( v45 )
        {
          *v45 = 92;
          v51 = v45 + 1;
        }
        else
        {
          v52 = -1i64;
          do
            ++v52;
          while( v43[v52] );
          v51 = &v43[v52];
        }
        v43 = v51;
        if( !*v51 )
          break;
        v42 = (unsigned int)ResolveContexta;
        v23 = v70;
      }
    }
    if( DeviceRegProp >= 0 )
    {
      if( MaximumLength != 2 )
        goto LABEL_3;
      v53 = UnicodeString;
      *Buffer = 0;
      DestinationString = v53;
      goto LABEL_99;
    }
  }
LABEL_105:
  RtlFreeAnsiString(&UnicodeString);
  if( P )
    ExFreePoolWithTag(P, 0);
  if( v7 )
    ExFreePoolWithTag(v7, 0);
  if( *(_QWORD *)Handle )
    ZwClose(Handle[0]);
  return DeviceRegProp;
}

Referenced by:

PiDevCfgResolveVariableKeyCopy
PiDevCfgResolveVariableKeyValue