client/X11: correctly trim terminating null bytes from strings

Sometimes Windows sends strings with excess null terminating bytes.
For example, when one copies digits from calc.exe. At the same time,
some local applications freak out when they encounter null bytes
(at least LibreOffice is known to be replacing them with '#').

According to the specification of UTF8_STRING format [1], the string
data must not contain any trailing null bytes. So they all should be
trimmed, not only the last one.

Also, if the trailing null byte is not present, the length should not
be adjusted. For example, Firefox is actually sending "HTML Format"
without a null byte while Internet Explorer adds one. The spec for
text/html format [2] says nothing about the teminating null byte, so
we are free to remove it, but at least we should not mistakingly
delete '>' character of "</html>" tag when it is the last character.

[1] http://www.pps.univ-paris-diderot.fr/~jch/software/UTF8_STRING/UTF8_STRING.text

[2] https://www.ietf.org/rfc/rfc2854.txt
This commit is contained in:
ilammy 2015-08-19 17:25:17 +03:00
parent b9a297379b
commit d7c9a31b4b

View File

@ -1102,8 +1102,11 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context
DstSize = 0; DstSize = 0;
pDstData = (BYTE*) ClipboardGetData(clipboard->system, dstFormatId, &DstSize); pDstData = (BYTE*) ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
if ((DstSize > 1) && nullTerminated) if (nullTerminated)
DstSize--; {
while (DstSize > 0 && pDstData[DstSize - 1] == '\0')
DstSize--;
}
} }
clipboard->data = pDstData; clipboard->data = pDstData;