PiGetRelatedDevice

NTSTATUS __stdcall PiGetRelatedDevice(
        UNICODE_STRING *TargetDeviceInstance,
        PWCHAR RelatedDeviceInstance,
        UINT64 *RelatedDeviceInstanceLength,
        UINT64 Relation){
  NTSTATUS v5; 
  int v6; 
  _QWORD *v9; 
  int v10; 
  void *v11; 
  __int64 *v12; 
  __int64 v13; 
  int v15; 
  _ETHREAD *CurrentThread; 
  _QWORD *v17; 
  int v18; 
  v5 = 0;
  v6 = Relation;
  PpDevNodeLockTree(0);
  v9 = PnpDeviceObjectFromDeviceInstanceWithTag((INT64)TargetDeviceInstance, 0x43706E50ui64);
  v11 = v9;
  if( !v9 || (v12 = *(__int64 **)(v9[39] + 40i64)) == 0i64 || (unsigned int)(*((_DWORD *)v12 + 75) - 787) <= 1 )
  {
LABEL_14:
    v5 = -1073741810;
    goto LABEL_11;
  }
  if( v6 == 1 )
  {
    v12 = (__int64 *)v12[2];
    goto LABEL_6;
  }
  v10 = 4096;
  if( v6 == 2 )
  {
    v12 = (__int64 *)v12[1];
    if( !v12
      || (v15 = *((_DWORD *)v12 + 99), (v15 & 0x2000) == 0)
      || *((_DWORD *)v12 + 101) != 24
      || (v15 & 0x1000) == 0 )
    {
LABEL_18:
      CurrentThread = (_ETHREAD *)KeGetCurrentThread();
      --*((_WORD *)CurrentThread + 242);
      LOBYTE(v10) = 1;
      ExAcquireResourceSharedLite(&PnpRegistryDeviceResource, v10);
      if( v12 )
      {
        while( 1 )
        {
          if( v12[6] )
          {
            v17 = PnpDeviceObjectFromDeviceInstanceWithTag((INT64)(v12 + 5), 0x43706E50ui64);
            if( v17 )
              break;
          }
          v12 = (__int64 *)*v12;
          if( !v12 )
            goto LABEL_22;
        }
        ObfDereferenceObjectWithTag(v17, 0x43706E50ui64);
      }
LABEL_22:
      ExReleaseResourceLite(&PnpRegistryDeviceResource);
      KeLeaveCriticalRegion();
LABEL_7:
      if( v12 )
      {
        v13 = *((unsigned __int16 *)v12 + 20);
        if( *(_DWORD *)RelatedDeviceInstanceLength <= (unsigned int)v13 )
        {
          v5 = -1073741789;
          LODWORD(v13) = v13 + 2;
        }
        else
        {
          memmove((UINT8 *)RelatedDeviceInstance, (UINT8 *)v12[6], *((unsigned __int16 *)v12 + 20));
          *(PWCHAR)((char *)RelatedDeviceInstance + v13) = 0;
        }
        *(_DWORD *)RelatedDeviceInstanceLength = v13;
        goto LABEL_11;
      }
      goto LABEL_14;
    }
    goto LABEL_24;
  }
  if( v6 == 3 )
  {
    do
    {
LABEL_24:
      v12 = (__int64 *)*v12;
      if( !v12 )
        break;
      v18 = *((_DWORD *)v12 + 99);
      if( (v18 & 0x2000) == 0 )
        break;
    }
    while( *((_DWORD *)v12 + 101) == 24 && (v18 & 0x1000) != 0 );
LABEL_6:
    if( v6 == 1 )
      goto LABEL_7;
    goto LABEL_18;
  }
  v5 = -1073741811;
LABEL_11:
  PpDevNodeUnlockTree(0);
  if( v11 )
    ObfDereferenceObjectWithTag(v11, 0x43706E50ui64);
  return v5;
}

Referenced by:

PiCMGetRelatedDeviceInstance
PiControlGetRelatedDevice