clipboard-helper.cpp revision 40e287c60f6d55660960a3556e40218812d29973
/* $Id$ */
/** @file
* Shared Clipboard: Some helper function for converting between the various eol.
*/
/*
* Includes contributions from François Revol
*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/** @todo use const where appropriate; delinuxify the code (*Lin* -> *Host*); use AssertLogRel*. */
{
AssertLogRelMsgReturn(pwszSrc != NULL, ("vboxClipboardUtf16GetWinSize: received a null Utf16 string, returning VERR_INVALID_PARAMETER\n"), VERR_INVALID_PARAMETER);
if (cwSrc == 0)
{
*pcwDest = 0;
LogFlowFunc(("empty source string, returning\n"));
return VINF_SUCCESS;
}
/** @todo convert the remainder of the Assert stuff to AssertLogRel. */
/* We only take little endian Utf16 */
if (pwszSrc[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
}
cwDest = 0;
/* Calculate the size of the destination text string. */
/* Is this Utf16 or Utf16-LE? */
{
/* Check for a single line feed */
++cwDest;
#ifdef RT_OS_DARWIN
/* Check for a single carriage return (MacOS) */
if (pwszSrc[i] == CARRIAGERETURN)
++cwDest;
#endif
if (pwszSrc[i] == 0)
{
/* Don't count this, as we do so below. */
break;
}
}
/* Count the terminating null byte. */
++cwDest;
return VINF_SUCCESS;
}
{
size_t i, j;
{
LogRel(("vboxClipboardUtf16LinToWin: received an invalid pointer, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
}
if (cwSrc == 0)
{
if (cwDest == 0)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
pu16Dest[0] = 0;
LogFlowFunc(("empty source string, returning\n"));
return VINF_SUCCESS;
}
/* We only take little endian Utf16 */
if (pwszSrc[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
}
/* Don't copy the endian marker. */
{
/* Don't copy the null byte, as we add it below. */
if (pwszSrc[i] == 0)
break;
if (j == cwDest)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
{
pu16Dest[j] = CARRIAGERETURN;
++j;
if (j == cwDest)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
}
#ifdef RT_OS_DARWIN
/* Check for a single carriage return (MacOS) */
else if (pwszSrc[i] == CARRIAGERETURN)
{
/* set cr */
pu16Dest[j] = CARRIAGERETURN;
++j;
if (j == cwDest)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
/* add the lf */
continue;
}
#endif
}
/* Add the trailing null. */
if (j == cwDest)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
pu16Dest[j] = 0;
return VINF_SUCCESS;
}
{
{
LogRel(("vboxClipboardUtf16GetLinSize: received an invalid Utf16 string %p. Returning VERR_INVALID_PARAMETER.\n", pwszSrc));
}
if (cwSrc == 0)
{
LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
*pcwDest = 0;
return VINF_SUCCESS;
}
/* We only take little endian Utf16 */
if (pwszSrc[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16GetLinSize: received a big endian Utf16 string. Returning VERR_INVALID_PARAMETER.\n"));
}
/* Calculate the size of the destination text string. */
/* Is this Utf16 or Utf16-LE? */
if (pwszSrc[0] == UTF16LEMARKER)
cwDest = 0;
else
cwDest = 1;
{
if ( (i + 1 < cwSrc)
&& (pwszSrc[i] == CARRIAGERETURN)
{
++i;
}
if (pwszSrc[i] == 0)
{
break;
}
}
/* Terminating zero */
++cwDest;
return VINF_SUCCESS;
}
{
LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
/* A buffer of size 0 may not be an error, but it is not a good idea either. */
{
LogRel(("vboxClipboardUtf16WinToLin: received an invalid ptr, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
}
/* We only take little endian Utf16 */
if (pwszSrc[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16WinToLin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
}
if (cwDest == 0)
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
if (cwSrc == 0)
{
pu16Dest[0] = 0;
LogFlowFunc(("received empty string. Returning VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
/* Prepend the Utf16 byte order marker if it is missing. */
if (pwszSrc[0] == UTF16LEMARKER)
{
cwDestPos = 0;
}
else
{
pu16Dest[0] = UTF16LEMARKER;
cwDestPos = 1;
}
{
if (pwszSrc[i] == 0)
{
break;
}
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
if ( (i + 1 < cwSrc)
&& (pwszSrc[i] == CARRIAGERETURN)
{
++i;
}
}
/* Terminating zero */
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
return VINF_SUCCESS;
}
{
/** @todo Support all the many versions of the DIB headers. */
if ( cbSrc < sizeof(BMINFOHEADER)
{
Log(("vboxClipboardDibToBmp: invalid or unsupported bitmap data.\n"));
return VERR_INVALID_PARAMETER;
}
offPixel = sizeof(BMFILEHEADER)
{
Log(("vboxClipboardDibToBmp: invalid bitmap data.\n"));
return VERR_INVALID_PARAMETER;
}
if (!pvDest)
{
Log(("writeToPasteboard: cannot allocate memory for bitmap.\n"));
return VERR_NO_MEMORY;
}
return VINF_SUCCESS;
}
{
if ( cbSrc < sizeof(BMFILEHEADER)
{
Log(("vboxClipboardBmpGetDib: invalid bitmap data.\n"));
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}