clipboard-helper.cpp revision dc23707aec4dc2ce2c6b6f51af21eef72bb8bf2e
/** @file
*
* Shared Clipboard:
* Some helper function for converting between the various eol.
*/
/*
* Copyright (C) 2006-2008 innotek GmbH
*
* 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.
*/
#include "clipboard-helper.h"
{
if (pu16Src == 0)
{
LogRel(("vboxClipboardUtf16GetWinSize: received a null Utf16 string, returning VERR_INVALID_PARAMETER\n"));
}
/* We only take little endian Utf16 */
if (pu16Src[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
}
if (cwSrc == 0)
{
*pcwDest = 0;
LogFlowFunc(("empty source string, returning\n"));
return VINF_SUCCESS;
}
cwDest = 0;
/* Calculate the size of the destination text string. */
/* Is this Utf16 or Utf16-LE? */
{
++cwDest;
if (pu16Src[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, pu16Src=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pu16Src, pu16Dest));
}
/* We only take little endian Utf16 */
if (pu16Src[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
}
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;
}
/* Don't copy the endian marker. */
{
/* Don't copy the null byte, as we add it below. */
if (pu16Src[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;
}
}
}
/* 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", pu16Src));
}
/* We only take little endian Utf16 */
if (pu16Src[0] == UTF16BEMARKER)
{
LogRel(("vboxClipboardUtf16GetLinSize: received a big endian Utf16 string. Returning VERR_INVALID_PARAMETER.\n"));
}
if (cwSrc == 0)
{
LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
*pcwDest = 0;
return VINF_SUCCESS;
}
/* Calculate the size of the destination text string. */
/* Is this Utf16 or Utf16-LE? */
if (pu16Src[0] == UTF16LEMARKER)
cwDest = 0;
else
cwDest = 1;
{
if ( (i + 1 < cwSrc)
&& (pu16Src[i] == CARRIAGERETURN)
{
++i;
}
if (pu16Src[i] == 0)
{
break;
}
}
/* Terminating zero */
++cwDest;
return VINF_SUCCESS;
}
{
LogFlowFunc(("pu16Src=%.*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, pu16Src=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pu16Src, pu16Dest));
}
/* We only take little endian Utf16 */
if (pu16Src[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 (pu16Src[0] == UTF16LEMARKER)
{
cwDestPos = 0;
}
else
{
pu16Dest[0] = UTF16LEMARKER;
cwDestPos = 1;
}
{
if (pu16Src[i] == 0)
{
break;
}
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
if ( (i + 1 < cwSrc)
&& (pu16Src[i] == CARRIAGERETURN)
{
++i;
}
}
/* Terminating zero */
{
LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
return VERR_BUFFER_OVERFLOW;
}
return VINF_SUCCESS;
}