VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/clipboard-helper.cpp@ 28381

Last change on this file since 28381 was 26163, checked in by vboxsync, 15 years ago

PDM: s/pUsbReg/pReg/g (2nd try, backed out r57176)

  • Property eol-style set to native
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 8.8 KB
Line 
1/* $Id: clipboard-helper.cpp 26163 2010-02-02 18:58:33Z vboxsync $ */
2/** @file
3 * Shared Clipboard: Some helper function for converting between the various eol.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include <iprt/assert.h>
23#include <VBox/log.h>
24#include <VBox/GuestHost/clipboard-helper.h>
25
26/** @todo use const where appropriate; delinuxifiy the code (*Lin* -> *Host*); use AssertLogRel*. */
27
28int vboxClipboardUtf16GetWinSize(PRTUTF16 pwszSrc, size_t cwSrc, size_t *pcwDest)
29{
30 size_t cwDest, i;
31
32 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
33 AssertLogRelMsgReturn(pwszSrc != NULL, ("vboxClipboardUtf16GetWinSize: received a null Utf16 string, returning VERR_INVALID_PARAMETER\n"), VERR_INVALID_PARAMETER);
34 if (cwSrc == 0)
35 {
36 *pcwDest = 0;
37 LogFlowFunc(("empty source string, returning\n"));
38 return VINF_SUCCESS;
39 }
40/** @todo convert the remainder of the Assert stuff to AssertLogRel. */
41 /* We only take little endian Utf16 */
42 if (pwszSrc[0] == UTF16BEMARKER)
43 {
44 LogRel(("vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
45 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
46 }
47 cwDest = 0;
48 /* Calculate the size of the destination text string. */
49 /* Is this Utf16 or Utf16-LE? */
50 for (i = (pwszSrc[0] == UTF16LEMARKER ? 1 : 0); i < cwSrc; ++i, ++cwDest)
51 {
52 /* Check for a single line feed */
53 if (pwszSrc[i] == LINEFEED)
54 ++cwDest;
55#ifdef RT_OS_DARWIN
56 /* Check for a single carriage return (MacOS) */
57 if (pwszSrc[i] == CARRIAGERETURN)
58 ++cwDest;
59#endif
60 if (pwszSrc[i] == 0)
61 {
62 /* Don't count this, as we do so below. */
63 break;
64 }
65 }
66 /* Count the terminating null byte. */
67 ++cwDest;
68 LogFlowFunc(("returning VINF_SUCCESS, %d 16bit words\n", cwDest));
69 *pcwDest = cwDest;
70 return VINF_SUCCESS;
71}
72
73int vboxClipboardUtf16LinToWin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest,
74 size_t cwDest)
75{
76 size_t i, j;
77 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
78 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
79 {
80 LogRel(("vboxClipboardUtf16LinToWin: received an invalid pointer, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
81 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
82 }
83 if (cwSrc == 0)
84 {
85 if (cwDest == 0)
86 {
87 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
88 return VERR_BUFFER_OVERFLOW;
89 }
90 pu16Dest[0] = 0;
91 LogFlowFunc(("empty source string, returning\n"));
92 return VINF_SUCCESS;
93 }
94 /* We only take little endian Utf16 */
95 if (pwszSrc[0] == UTF16BEMARKER)
96 {
97 LogRel(("vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
98 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
99 }
100 /* Don't copy the endian marker. */
101 for (i = (pwszSrc[0] == UTF16LEMARKER ? 1 : 0), j = 0; i < cwSrc; ++i, ++j)
102 {
103 /* Don't copy the null byte, as we add it below. */
104 if (pwszSrc[i] == 0)
105 break;
106 if (j == cwDest)
107 {
108 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
109 return VERR_BUFFER_OVERFLOW;
110 }
111 if (pwszSrc[i] == LINEFEED)
112 {
113 pu16Dest[j] = CARRIAGERETURN;
114 ++j;
115 if (j == cwDest)
116 {
117 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
118 return VERR_BUFFER_OVERFLOW;
119 }
120 }
121#ifdef RT_OS_DARWIN
122 /* Check for a single carriage return (MacOS) */
123 else if (pwszSrc[i] == CARRIAGERETURN)
124 {
125 /* set cr */
126 pu16Dest[j] = CARRIAGERETURN;
127 ++j;
128 if (j == cwDest)
129 {
130 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
131 return VERR_BUFFER_OVERFLOW;
132 }
133 /* add the lf */
134 pu16Dest[j] = LINEFEED;
135 continue;
136 }
137#endif
138 pu16Dest[j] = pwszSrc[i];
139 }
140 /* Add the trailing null. */
141 if (j == cwDest)
142 {
143 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
144 return VERR_BUFFER_OVERFLOW;
145 }
146 pu16Dest[j] = 0;
147 LogFlowFunc(("rc=VINF_SUCCESS, pu16Dest=%ls\n", pu16Dest));
148 return VINF_SUCCESS;
149}
150
151int vboxClipboardUtf16GetLinSize(PRTUTF16 pwszSrc, size_t cwSrc, size_t *pcwDest)
152{
153 size_t cwDest;
154
155 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
156 if (!VALID_PTR(pwszSrc))
157 {
158 LogRel(("vboxClipboardUtf16GetLinSize: received an invalid Utf16 string %p. Returning VERR_INVALID_PARAMETER.\n", pwszSrc));
159 AssertReturn(VALID_PTR(pwszSrc), VERR_INVALID_PARAMETER);
160 }
161 if (cwSrc == 0)
162 {
163 LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
164 *pcwDest = 0;
165 return VINF_SUCCESS;
166 }
167 /* We only take little endian Utf16 */
168 if (pwszSrc[0] == UTF16BEMARKER)
169 {
170 LogRel(("vboxClipboardUtf16GetLinSize: received a big endian Utf16 string. Returning VERR_INVALID_PARAMETER.\n"));
171 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
172 }
173 /* Calculate the size of the destination text string. */
174 /* Is this Utf16 or Utf16-LE? */
175 if (pwszSrc[0] == UTF16LEMARKER)
176 cwDest = 0;
177 else
178 cwDest = 1;
179 for (size_t i = 0; i < cwSrc; ++i, ++cwDest)
180 {
181 if ( (i + 1 < cwSrc)
182 && (pwszSrc[i] == CARRIAGERETURN)
183 && (pwszSrc[i + 1] == LINEFEED))
184 {
185 ++i;
186 }
187 if (pwszSrc[i] == 0)
188 {
189 break;
190 }
191 }
192 /* Terminating zero */
193 ++cwDest;
194 LogFlowFunc(("returning %d\n", cwDest));
195 *pcwDest = cwDest;
196 return VINF_SUCCESS;
197}
198
199int vboxClipboardUtf16WinToLin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest,
200 size_t cwDest)
201{
202 size_t cwDestPos;
203
204 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
205 cwSrc, pwszSrc, cwSrc, pu16Dest, cwDest));
206 /* A buffer of size 0 may not be an error, but it is not a good idea either. */
207 Assert(cwDest > 0);
208 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
209 {
210 LogRel(("vboxClipboardUtf16WinToLin: received an invalid ptr, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
211 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
212 }
213 /* We only take little endian Utf16 */
214 if (pwszSrc[0] == UTF16BEMARKER)
215 {
216 LogRel(("vboxClipboardUtf16WinToLin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
217 AssertMsgFailedReturn(("received a big endian string\n"), VERR_INVALID_PARAMETER);
218 }
219 if (cwDest == 0)
220 {
221 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
222 return VERR_BUFFER_OVERFLOW;
223 }
224 if (cwSrc == 0)
225 {
226 pu16Dest[0] = 0;
227 LogFlowFunc(("received empty string. Returning VINF_SUCCESS\n"));
228 return VINF_SUCCESS;
229 }
230 /* Prepend the Utf16 byte order marker if it is missing. */
231 if (pwszSrc[0] == UTF16LEMARKER)
232 {
233 cwDestPos = 0;
234 }
235 else
236 {
237 pu16Dest[0] = UTF16LEMARKER;
238 cwDestPos = 1;
239 }
240 for (size_t i = 0; i < cwSrc; ++i, ++cwDestPos)
241 {
242 if (pwszSrc[i] == 0)
243 {
244 break;
245 }
246 if (cwDestPos == cwDest)
247 {
248 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
249 return VERR_BUFFER_OVERFLOW;
250 }
251 if ( (i + 1 < cwSrc)
252 && (pwszSrc[i] == CARRIAGERETURN)
253 && (pwszSrc[i + 1] == LINEFEED))
254 {
255 ++i;
256 }
257 pu16Dest[cwDestPos] = pwszSrc[i];
258 }
259 /* Terminating zero */
260 if (cwDestPos == cwDest)
261 {
262 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
263 return VERR_BUFFER_OVERFLOW;
264 }
265 pu16Dest[cwDestPos] = 0;
266 LogFlowFunc(("set string %ls. Returning\n", pu16Dest + 1));
267 return VINF_SUCCESS;
268}
269
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette