VirtualBox

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

Last change on this file since 36300 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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