PiNormalizeDeviceText

NTSTATUS __stdcall PiNormalizeDeviceText(WCHAR *DriverText, WCHAR **NormalizedText){
  void *v3; 
  unsigned __int16 *v4; 
  WCHAR *v5; 
  int inited; 
  VOID **PoolWithTag; 
  WORD *v9; 
  WORD *v10; 
  const wchar_t *v11; 
  WORD *v12; 
  WORD *v13; 
  INT64 v14; 
  __int64 v15; 
  __int64 v16; 
  __int64 v17; 
  __int64 v18; 
  UINT64 v19; 
  VOID **v20; 
  UINT64 *ResultLength; 
  WCHAR *pszFormat; 
  INT64 v23; 
  INT64 v24; 
  _UNICODE_STRING String2; 
  wchar_t *EndPtr; 
  INT64 v27; 
  _UNICODE_STRING DestinationString; 
  UINT64 pcbRemaining; 
  INT64 v30; 
  wchar_t *v31; 
  PVOID P; 
  _OBJECT_ATTRIBUTES ObjectAttributes; 
  UINT64 NumberOfBytes; 
  VOID *KeyHandle; 

  *NormalizedText = 0i64;
  EndPtr = 0i64;
  LODWORD(NumberOfBytes) = 0;
  DestinationString = 0i64;
  pcbRemaining = 0i64;
  v3 = 0i64;
  memset(&ObjectAttributes, 0, sizeof(ObjectAttributes));
  v27 = 0i64;
  v4 = 0i64;
  v5 = 0i64;
  KeyHandle = 0i64;
  String2 = 0i64;
  RtlInitUnicodeString(&DestinationString, 0i64);
  inited = RtlInitUnicodeStringEx(&String2, DriverText);
  if( inited < 0 )
    goto LABEL_4;
  if( !(unsigned __int8)RtlPrefixUnicodeString(&CmRegistryMachineName, &String2, 1u) )
  {
    inited = 0;
    goto LABEL_4;
  }
  PoolWithTag = ExAllocatePoolWithTag(1ui64, String2.Length + 2i64, 538996816i64);
  P = PoolWithTag;
  v9 = (WORD *)PoolWithTag;
  if( !PoolWithTag )
  {
    inited = -1073741670;
    goto LABEL_4;
  }
  memmove(PoolWithTag, DriverText, String2.Length);
  v9[(unsigned __int64)String2.Length >> 1] = 0;
  v10 = wcschr(v9, 44);
  if( !v10 )
    goto LABEL_34;
  v11 = v10 + 1;
  *v10 = 0;
  v12 = wcschr(v10 + 1, 44);
  v13 = v12;
  if( v12 )
  {
    *v12 = 0;
    v13 = v12 + 1;
  }
  v30 = wcstoi64(v11, &EndPtr, 10);
  if( *EndPtr )
    goto LABEL_34;
  RtlInitUnicodeString(&String2, v9);
  ObjectAttributes.Length = 48;
  ObjectAttributes.ObjectName = &String2;
  ObjectAttributes.RootDirectory = 0i64;
  ObjectAttributes.Attributes = 576;
  *(_OWORD *)&ObjectAttributes.SecurityDescriptor = 0i64;
  inited = ZwOpenKey(&KeyHandle, 0x20019ui64, &ObjectAttributes, v14, (INT64)ResultLength, (INT64)pszFormat, v23, v24);
  if( inited >= 0 )
  {
    inited = ZwQueryKey(KeyHandle, KeyBasicInformation, 0i64, 0i64, &NumberOfBytes);
    if( inited == -1073741789 )
    {
      v4 = (unsigned __int16 *)ExAllocatePoolWithTag(1ui64, (unsigned int)NumberOfBytes, 538996816i64);
      if( !v4 )
        goto LABEL_38;
      inited = ZwQueryKey(KeyHandle, KeyBasicInformation, v4, (unsigned int)NumberOfBytes, &NumberOfBytes);
      if( inited < 0 )
        goto LABEL_35;
      String2.Length = v4[6];
      String2.MaximumLength = v4[6];
      String2.Buffer = v4 + 8;
      inited = IopBuildFullDriverPath(&String2, KeyHandle, &DestinationString);
      if( inited < 0 )
        goto LABEL_35;
      *(_DWORD *)&String2.Length = 1703960;
      String2.Buffer = L"\\SystemRoot\\";
      if( !(unsigned __int8)RtlPrefixUnicodeString(&String2, &DestinationString, 1u) )
        goto LABEL_35;
      v15 = (unsigned __int16)(DestinationString.Length - 24);
      v31 = DestinationString.Buffer + 12;
      inited = PiGetDefaultMessageString(KeyHandle, (unsigned int)v30, (WCHAR **)&v27);
      if( inited < 0 )
      {
        v3 = (void *)v27;
        goto LABEL_35;
      }
      v16 = -1i64;
      v17 = -1i64;
      do
        ++v17;
      while( v11[v17] );
      v3 = (void *)v27;
      v18 = -1i64;
      do
        ++v18;
      while( *(_WORD *)(v27 + 2 * v18) );
      v19 = (unsigned __int16)v15 + 2 * (v17 + v18 + 5);
      if( v13 )
      {
        do
          ++v16;
        while( v13[v16] );
        v19 += 2 * v16 + 6;
      }
      v20 = ExAllocatePoolWithTag(1ui64, v19, 538996816i64);
      v5 = (WCHAR *)v20;
      if( !v20 )
      {
LABEL_38:
        inited = -1073741670;
        goto LABEL_35;
      }
      if( inited >= 0 )
      {
        if( !v13 || (inited = RtlStringCbPrintfW(EndPtr, pcbRemaining, (WCHAR *)L";(%s)"), inited >= 0) )
        {
          *NormalizedText = v5;
LABEL_34:
          inited = 0;
        }
      }
    }
  }
LABEL_35:
  ExFreePoolWithTag(P, 0);
  if( v4 )
    ExFreePoolWithTag(v4, 0);
LABEL_4:
  RtlFreeAnsiString(&DestinationString);
  if( v3 )
    ExFreePoolWithTag(v3, 0);
  if( KeyHandle )
    ZwClose((_HANDLE)KeyHandle);
  if( inited < 0 && v5 )
    ExFreePoolWithTag(v5, 0);
  return inited;
}

Referenced by:

PnpQueryDeviceText