Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

bugfix: deviceName garbled in Windows #34 #86

Merged
merged 1 commit into from
Oct 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/detection_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ _SetupDiGetDeviceRegistryProperty DllSetupDiGetDeviceRegistryProperty;
* Local Helper Functions protoypes
**********************************/
void UpdateDevice(PDEV_BROADCAST_DEVICEINTERFACE pDevInf, WPARAM wParam, DeviceState_t state);
std::string Utf8Encode(const std::string &str);
Copy link
Owner

@MadLittleMods MadLittleMods Jun 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is std::string still available after #84 is merged?

Also any of the string stuff we are doing below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, 'std::string' is the c++ standard,mean string with namespace std,
#84 problem is the 'ATL' is not the standard library.

DWORD WINAPI ListenerThread(LPVOID lpParam);

void BuildInitialDeviceList();
Expand Down Expand Up @@ -421,14 +422,14 @@ void ExtractDeviceInfo(HDEVINFO hDevInfo, SP_DEVINFO_DATA* pspDevInfoData, TCHAR

// device found
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, buffSize, &nSize)) {
resultItem->deviceName = buf;
resultItem->deviceName = Utf8Encode(buf);
}
else if ( DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, buffSize, &nSize))
{
resultItem->deviceName = buf;
resultItem->deviceName = Utf8Encode(buf);
}
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_MFG, &DataT, (PBYTE)buf, buffSize, &nSize)) {
resultItem->manufacturer = buf;
resultItem->manufacturer = Utf8Encode(buf);
}
if (DllSetupDiGetDeviceRegistryProperty(hDevInfo, pspDevInfoData, SPDRP_HARDWAREID, &DataT, (PBYTE)buf, buffSize, &nSize)) {
// Use this to extract VID / PID
Expand Down Expand Up @@ -550,3 +551,22 @@ void UpdateDevice(PDEV_BROADCAST_DEVICEINTERFACE pDevInf, WPARAM wParam, DeviceS

SetEvent(deviceChangedRegisteredEvent);
}

std::string Utf8Encode(const std::string &str)
{
if (str.empty()) {
return std::string();
}

//System default code page to wide character
int wstr_size = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
std::wstring wstr_tmp(wstr_size, 0);
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr_tmp[0], wstr_size);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is pretty similar to https://stackoverflow.com/a/6693107/796832 so seems good enough to me


//Wide character to Utf8
int str_size = WideCharToMultiByte(CP_UTF8, 0, &wstr_tmp[0], (int)wstr_tmp.size(), NULL, 0, NULL, NULL);
std::string str_utf8(str_size, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr_tmp[0], (int)wstr_tmp.size(), &str_utf8[0], str_size, NULL, NULL);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty similar to WideCharToMultiByte so seems good enough to me


return str_utf8;
}
Copy link
Owner

@MadLittleMods MadLittleMods Jun 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you get this code from somewhere like StackOverflow? Let's link it for reference like the following at the top of this function

// via https://stackoverflow.com/q/25410284/796832

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, i wrote it myself, it easy for me, i wrote is before. sometimes i need change the code page from 'gbk' to 'utf8'.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bit too much magic here for me to be comfortable. Just seems like something could be exploitable here with all of this reference and byte copying stuff.

Would definitely prefer a standard approach to tackle this

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.