1
0
mirror of https://github.com/solemnwarning/directplay-lite synced 2024-12-30 16:45:37 +01:00

DirectPlay8Address: Convert ANSI strings to Unicode.

This commit is contained in:
Daniel Collins 2018-09-27 22:05:38 +01:00
parent f19678217f
commit ca6184b1c9
3 changed files with 39 additions and 50 deletions

View File

@ -269,9 +269,27 @@ HRESULT DirectPlay8Address::AddComponent(CONST WCHAR* CONST pwszName, CONST void
break; break;
case DPNA_DATATYPE_STRING_ANSI: case DPNA_DATATYPE_STRING_ANSI:
new_component = new StringComponentA(pwszName, (const char*)(lpvData), dwDataSize); {
break; /* DirectX converts ANSI strings to wide on input. */
int wide_char_count = MultiByteToWideChar(CP_ACP, 0, (const char*)(lpvData), dwDataSize, NULL, 0);
if(wide_char_count == 0)
{
return DPNERR_INVALIDPARAM;
}
std::vector<wchar_t> wide_char_buf(wide_char_count);
wide_char_count = MultiByteToWideChar(CP_ACP, 0, (const char*)(lpvData), dwDataSize, wide_char_buf.data(), wide_char_count);
if(wide_char_count == 0)
{
return DPNERR_INVALIDPARAM;
}
new_component = new StringComponentW(pwszName, wide_char_buf.data(), wide_char_count * sizeof(wchar_t));
break;
}
case DPNA_DATATYPE_DWORD: case DPNA_DATATYPE_DWORD:
if(dwDataSize != sizeof(DWORD)) if(dwDataSize != sizeof(DWORD))
{ {
@ -375,30 +393,6 @@ HRESULT DirectPlay8Address::StringComponentW::get_component(LPVOID pvBuffer, PDW
} }
} }
DirectPlay8Address::StringComponentA::StringComponentA(const std::wstring &name, const char *lpvData, DWORD dwDataSize):
Component(name, DPNA_DATATYPE_STRING_ANSI), value(lpvData, dwDataSize) {}
HRESULT DirectPlay8Address::StringComponentA::get_component(LPVOID pvBuffer, PDWORD pdwBufferSize, PDWORD pdwDataType)
{
*pdwDataType = DPNA_DATATYPE_STRING_ANSI;
if(*pdwBufferSize >= value.length() && pvBuffer != NULL)
{
memcpy(pvBuffer, value.data(), value.length());
*pdwBufferSize = value.length();
return S_OK;
}
else{
*pdwBufferSize = value.length();
return DPNERR_BUFFERTOOSMALL;
}
}
DirectPlay8Address::Component *DirectPlay8Address::StringComponentA::clone()
{
return new DirectPlay8Address::StringComponentA(name, value.data(), value.length());
}
DirectPlay8Address::DWORDComponent::DWORDComponent(const std::wstring &name, DWORD value): DirectPlay8Address::DWORDComponent::DWORDComponent(const std::wstring &name, DWORD value):
Component(name, DPNA_DATATYPE_DWORD), value(value) {} Component(name, DPNA_DATATYPE_DWORD), value(value) {}

View File

@ -35,16 +35,6 @@ class DirectPlay8Address: public IDirectPlay8Address
virtual HRESULT get_component(LPVOID pvBuffer, PDWORD pdwBufferSize, PDWORD pdwDataType); virtual HRESULT get_component(LPVOID pvBuffer, PDWORD pdwBufferSize, PDWORD pdwDataType);
}; };
class StringComponentA: public Component
{
public:
const std::string value;
StringComponentA(const std::wstring &name, const char *lpvData, DWORD dwDataSize);
virtual Component *clone();
virtual HRESULT get_component(LPVOID pvBuffer, PDWORD pdwBufferSize, PDWORD pdwDataType);
};
class DWORDComponent: public Component class DWORDComponent: public Component
{ {
public: public:

View File

@ -310,8 +310,11 @@ class DirectPlay8AddressWithAStringComponent: public DirectPlay8AddressInitial
protected: protected:
const wchar_t *REFKEY = L"key"; const wchar_t *REFKEY = L"key";
const char *REFVAL = "ASCII string value"; const char *AREFVAL = "ASCII string value";
const DWORD REFVSIZE = 19; const DWORD AREFVSIZE = 19;
const wchar_t *WREFVAL = L"ASCII string value";
const DWORD WREFVSIZE = 38;
unsigned char vbuf[256]; unsigned char vbuf[256];
@ -319,7 +322,7 @@ class DirectPlay8AddressWithAStringComponent: public DirectPlay8AddressInitial
{ {
memset(vbuf, 0xFF, sizeof(vbuf)); memset(vbuf, 0xFF, sizeof(vbuf));
ASSERT_EQ(idp8->AddComponent(REFKEY, REFVAL, REFVSIZE, DPNA_DATATYPE_STRING_ANSI), S_OK); ASSERT_EQ(idp8->AddComponent(REFKEY, AREFVAL, AREFVSIZE, DPNA_DATATYPE_STRING_ANSI), S_OK);
} }
}; };
@ -337,43 +340,45 @@ TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeZero)
ASSERT_EQ(idp8->GetComponentByName(REFKEY, NULL, &vsize, &type), DPNERR_BUFFERTOOSMALL); ASSERT_EQ(idp8->GetComponentByName(REFKEY, NULL, &vsize, &type), DPNERR_BUFFERTOOSMALL);
EXPECT_EQ(vsize, REFVSIZE); EXPECT_EQ(vsize, WREFVSIZE);
} }
TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeSmall) TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeSmall)
{ {
DWORD vsize = REFVSIZE - 1; DWORD vsize = WREFVSIZE - 1;
DWORD type; DWORD type;
ASSERT_EQ(idp8->GetComponentByName(REFKEY, (void*)(vbuf), &vsize, &type), DPNERR_BUFFERTOOSMALL); ASSERT_EQ(idp8->GetComponentByName(REFKEY, (void*)(vbuf), &vsize, &type), DPNERR_BUFFERTOOSMALL);
EXPECT_EQ(vsize, REFVSIZE); EXPECT_EQ(vsize, WREFVSIZE);
} }
TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeExact) TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeExact)
{ {
DWORD vsize = REFVSIZE; DWORD vsize = WREFVSIZE;
DWORD type; DWORD type;
ASSERT_EQ(idp8->GetComponentByName(REFKEY, (void*)(vbuf), &vsize, &type), S_OK); ASSERT_EQ(idp8->GetComponentByName(REFKEY, (void*)(vbuf), &vsize, &type), S_OK);
EXPECT_EQ(vsize, REFVSIZE); EXPECT_EQ(vsize, WREFVSIZE);
EXPECT_EQ(type, (DWORD)(DPNA_DATATYPE_STRING_ANSI)); EXPECT_EQ(type, (DWORD)(DPNA_DATATYPE_STRING));
EXPECT_EQ(std::string((const char*)(vbuf), vsize), std::string(REFVAL, REFVSIZE)); EXPECT_EQ(std::wstring((const wchar_t*)(vbuf), (vsize / sizeof(wchar_t))),
std::wstring(WREFVAL, (WREFVSIZE / sizeof(wchar_t))));
} }
TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeBig) TEST_F(DirectPlay8AddressWithAStringComponent, ComponentByNameBufferSizeBig)
{ {
DWORD vsize = REFVSIZE * 2; DWORD vsize = WREFVSIZE * 2;
DWORD type; DWORD type;
ASSERT_EQ(idp8->GetComponentByName(REFKEY, (void*)(vbuf), &vsize, &type), S_OK); ASSERT_EQ(idp8->GetComponentByName(REFKEY, (void*)(vbuf), &vsize, &type), S_OK);
EXPECT_EQ(vsize, REFVSIZE); EXPECT_EQ(vsize, WREFVSIZE);
EXPECT_EQ(type, (DWORD)(DPNA_DATATYPE_STRING_ANSI)); EXPECT_EQ(type, (DWORD)(DPNA_DATATYPE_STRING));
EXPECT_EQ(std::string((const char*)(vbuf), vsize), std::string(REFVAL, REFVSIZE)); EXPECT_EQ(std::wstring((const wchar_t*)(vbuf), (vsize / sizeof(wchar_t))),
std::wstring(WREFVAL, (WREFVSIZE / sizeof(wchar_t))));
} }
// TEST_F(DirectPlay8AddressWithAStringComponent, GetURLW) // TEST_F(DirectPlay8AddressWithAStringComponent, GetURLW)