Skip to content

Commit

Permalink
HexStringToBin wasn't properly handling non-null cbTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenegriffin committed Dec 1, 2016
1 parent a556cc6 commit aa9533e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 45 deletions.
46 changes: 15 additions & 31 deletions Dialogs/Editors/PropertyEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,68 +496,53 @@ void CPropertyEditor::WriteStringsToSPropValue()
m_lpsOutputValue->ulPropTag = m_ulPropTag;
m_lpsOutputValue->dwAlignPad = NULL;
vector<BYTE> bin;
wstring szTmpString;

switch (PROP_TYPE(m_ulPropTag))
{
case PT_I2: // treat as signed long
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.i = static_cast<short int>(wstringToLong(szTmpString, 10));
m_lpsOutputValue->Value.i = static_cast<short int>(wstringToLong(GetStringW(0), 10));
break;
case PT_LONG: // treat as unsigned long
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.l = static_cast<LONG>(wstringToUlong(szTmpString, 10));
m_lpsOutputValue->Value.l = static_cast<LONG>(wstringToUlong(GetStringW(0), 10));
break;
case PT_R4:
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.flt = static_cast<float>(wstringToDouble(szTmpString));
m_lpsOutputValue->Value.flt = static_cast<float>(wstringToDouble(GetStringW(0)));
break;
case PT_DOUBLE:
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.dbl = wstringToDouble(szTmpString);
m_lpsOutputValue->Value.dbl = wstringToDouble(GetStringW(0));
break;
case PT_CURRENCY:
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.cur.Hi = wstringToUlong(szTmpString, 16);
szTmpString = GetStringW(1);
m_lpsOutputValue->Value.cur.Lo = wstringToUlong(szTmpString, 16);
m_lpsOutputValue->Value.cur.Hi = wstringToUlong(GetStringW(0), 16);
m_lpsOutputValue->Value.cur.Lo = wstringToUlong(GetStringW(1), 16);
break;
case PT_APPTIME:
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.at = wstringToDouble(szTmpString);
m_lpsOutputValue->Value.at = wstringToDouble(GetStringW(0));
break;
case PT_ERROR: // unsigned
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.err = static_cast<SCODE>(wstringToUlong(szTmpString, 16));
m_lpsOutputValue->Value.err = static_cast<SCODE>(wstringToUlong(GetStringW(0), 16));
break;
case PT_BOOLEAN:
m_lpsOutputValue->Value.b = static_cast<unsigned short>(GetCheck(0));
break;
case PT_I8:
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.li.HighPart = static_cast<long>(wstringToUlong(szTmpString, 16));
szTmpString = GetStringW(1);
m_lpsOutputValue->Value.li.LowPart = static_cast<long>(wstringToUlong(szTmpString, 16));
m_lpsOutputValue->Value.li.HighPart = static_cast<long>(wstringToUlong(GetStringW(0), 16));
m_lpsOutputValue->Value.li.LowPart = static_cast<long>(wstringToUlong(GetStringW(1), 16));
break;
case PT_STRING8:
// We read strings out of the hex control in order to preserve any hex level tweaks the user
// may have done. The RichEdit control likes throwing them away.
szTmpString = GetStringW(1);
bin = HexStringToBin(szTmpString);
bin = HexStringToBin(GetStringW(1));
m_lpsOutputValue->Value.lpszA = reinterpret_cast<LPSTR>(ByteVectorToMAPI(bin, m_lpAllocParent));
break;
case PT_UNICODE:
// We read strings out of the hex control in order to preserve any hex level tweaks the user
// may have done. The RichEdit control likes throwing them away.
szTmpString = GetStringW(1);
bin = HexStringToBin(szTmpString);
bin = HexStringToBin(GetStringW(1));
m_lpsOutputValue->Value.lpszW = reinterpret_cast<LPWSTR>(ByteVectorToMAPI(bin, m_lpAllocParent));
break;
case PT_SYSTIME:
szTmpString = GetStringW(0);
m_lpsOutputValue->Value.ft.dwLowDateTime = wstringToUlong(szTmpString, 16);
szTmpString = GetStringW(1);
m_lpsOutputValue->Value.ft.dwHighDateTime = wstringToUlong(szTmpString, 16);
m_lpsOutputValue->Value.ft.dwLowDateTime = wstringToUlong(GetStringW(0), 16);
m_lpsOutputValue->Value.ft.dwHighDateTime = wstringToUlong(GetStringW(1), 16);
break;
case PT_CLSID:
EC_H(MAPIAllocateMore(
Expand All @@ -572,8 +557,7 @@ void CPropertyEditor::WriteStringsToSPropValue()
break;
case PT_BINARY:
// remember we already read szTmpString and ulStrLen and found ulStrLen was even
szTmpString = GetStringW(0);
bin = HexStringToBin(szTmpString);
bin = HexStringToBin(GetStringW(0));
m_lpsOutputValue->Value.bin.lpb = ByteVectorToMAPI(bin, m_lpAllocParent);
m_lpsOutputValue->Value.bin.cb = static_cast<ULONG>(bin.size());
break;
Expand Down
3 changes: 1 addition & 2 deletions MrMapi/MMSmartView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ void DoSmartView(_In_ MYOPTIONS ProgOpts)
}
else
{
auto str = LPCSTRToWstring(reinterpret_cast<LPCSTR>(lpbIn));
bin = HexStringToBin(str);
bin = HexStringToBin(LPCSTRToWstring(reinterpret_cast<LPCSTR>(lpbIn)));
Bin.cb = static_cast<ULONG>(bin.size());
Bin.lpb = bin.data();
}
Expand Down
21 changes: 10 additions & 11 deletions String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ wstring loadstring(DWORD dwID)
{
wstring fmtString;
LPWSTR buffer = nullptr;
size_t len = ::LoadStringW(nullptr, dwID, reinterpret_cast<PWCHAR>(&buffer), 0);
size_t len = LoadStringW(nullptr, dwID, reinterpret_cast<PWCHAR>(&buffer), 0);

if (len)
{
Expand Down Expand Up @@ -144,7 +144,7 @@ wstring LPCSTRToWstring(LPCSTR src)
wstring wstringToLower(const wstring& src)
{
auto dst = src;
transform(dst.begin(), dst.end(), dst.begin(), ::tolower);
transform(dst.begin(), dst.end(), dst.begin(), tolower);
return dst;
}

Expand Down Expand Up @@ -404,15 +404,17 @@ bool stripPrefix(wstring& str, const wstring& prefix)

// Converts hex string in lpsz to a binary buffer.
// If cbTarget != 0, caps the number of bytes converted at cbTarget
// TODO: rewrite with const ref parameter
vector<BYTE> HexStringToBin(_In_ wstring lpsz, size_t cbTarget)
vector<BYTE> HexStringToBin(_In_ const wstring& input, size_t cbTarget)
{
// If our target is odd, we can't convert
if (cbTarget % 2 != 0) return vector<BYTE>();

// remove junk
WCHAR szJunk[] = L"\r\n\t -.,\\/'{}`\""; // STRING_OK
for (unsigned int i = 0; i < _countof(szJunk); ++i)
wstring szJunk = L"\r\n\t -.,\\/'{}`\""; // STRING_OK
auto lpsz = strip(input, [szJunk](const WCHAR & chr)
{
lpsz.erase(std::remove(lpsz.begin(), lpsz.end(), szJunk[i]), lpsz.end());
}
return szJunk.find(chr) != wstring::npos;
});

// strip one (and only one) prefix
stripPrefix(lpsz, L"0x") ||
Expand All @@ -422,9 +424,6 @@ vector<BYTE> HexStringToBin(_In_ wstring lpsz, size_t cbTarget)

auto cchStrLen = lpsz.length();

// We have a clean string now. If it's of odd length, we're done.
if (cchStrLen % 2 != 0) return vector<BYTE>();

vector<BYTE> lpb;
WCHAR szTmp[3] = { 0 };
size_t iCur = 0;
Expand Down
2 changes: 1 addition & 1 deletion String.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ wstring BinToTextString(_In_ const LPSBinary lpBin, bool bMultiLine);
wstring BinToHexString(const vector<BYTE>& lpByte, bool bPrependCB);
wstring BinToHexString(_In_opt_count_(cb) const LPBYTE lpb, size_t cb, bool bPrependCB);
wstring BinToHexString(_In_opt_ const LPSBinary lpBin, bool bPrependCB);
vector<BYTE> HexStringToBin(_In_ wstring lpsz, size_t cbTarget = 0);
vector<BYTE> HexStringToBin(_In_ const wstring& input, size_t cbTarget = 0);
LPBYTE ByteVectorToLPBYTE(const vector<BYTE>& bin);

vector<wstring> split(const wstring& str, const wchar_t delim);
Expand Down

0 comments on commit aa9533e

Please sign in to comment.