Skip to content

Commit

Permalink
Okay so this will have it convert correctly, but the caller isn't all…
Browse files Browse the repository at this point in the history
…ocating enough memory to get this back now. So we have to look a layer up.
  • Loading branch information
miniksa committed Oct 20, 2020
1 parent 743283e commit c1ca8f3
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/host/dbcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,37 +176,43 @@ _Ret_range_(0, cbAnsi)
ULONG i, j;
for (i = 0, j = 0; i < cchUnicode && j < cbAnsi; i++, j++)
{
if (IsGlyphFullWidth(TmpUni[i]))
ULONG const NumBytes = sizeof(AsciiDbcs);

// Try to convert to OEM. On failure, use the default character as a replacement.
if (!ConvertToOem(gci.CP, &TmpUni[i], 1, (LPSTR)&AsciiDbcs[0], NumBytes))
{
ULONG const NumBytes = sizeof(AsciiDbcs);
ConvertToOem(gci.CP, &TmpUni[i], 1, (LPSTR)&AsciiDbcs[0], NumBytes);
if (IsDBCSLeadByteConsole(AsciiDbcs[0], &gci.CPInfo))
{
if (j < cbAnsi - 1)
{ // -1 is safe DBCS in buffer
pchAnsi[j] = AsciiDbcs[0];
j++;
pchAnsi[j] = AsciiDbcs[1];
AsciiDbcs[1] = 0;
}
else
{
pchAnsi[j] = AsciiDbcs[0];
break;
}
AsciiDbcs[0] = gci.CPInfo.DefaultChar[0];
AsciiDbcs[1] = gci.CPInfo.DefaultChar[1];
}

// If the first character is a lead byte, try to store them both.
if (IsDBCSLeadByteConsole(AsciiDbcs[0], &gci.CPInfo))
{
// If we have enough space to store them both, do so..
if (j < cbAnsi - 1)
{ // -1 off cbAnsi combined with less than means 2 bytes are left in the buffer
pchAnsi[j] = AsciiDbcs[0];
j++;
pchAnsi[j] = AsciiDbcs[1];
AsciiDbcs[1] = 0;
}
else
{
pchAnsi[j] = AsciiDbcs[0];
AsciiDbcs[1] = 0;
// We ran out of space to store AsciiDbcs[1].... fall down below and stuff it for later.
break;
}
}
else
{
ConvertToOem(gci.CP, &TmpUni[i], 1, &pchAnsi[j], 1);
// If it's not a lead byte, something strange is happening. So just store the 1 byte and discard the second.
pchAnsi[j] = AsciiDbcs[0];
AsciiDbcs[1] = 0;
}

}

// If we could not store the second byte of the double byte sequence because we ran out of space...
if (AsciiDbcs[1])
{
try
Expand Down

0 comments on commit c1ca8f3

Please sign in to comment.