VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/clipboard-helper.cpp@ 9580

Last change on this file since 9580 was 8280, checked in by vboxsync, 17 years ago

else if on the same line.

  • Property eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 8.7 KB
Line 
1/* $Id: clipboard-helper.cpp 8280 2008-04-22 12:28:31Z 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 "clipboard-helper.h"
23#include "VBox/log.h"
24#include <iprt/assert.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/** @todo convert the remainder of the Assert stuff to AssertLogRel. */
35 /* We only take little endian Utf16 */
36 if (pwszSrc[0] == UTF16BEMARKER)
37 {
38 LogRel(("vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
39 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
40 }
41 if (cwSrc == 0)
42 {
43 *pcwDest = 0;
44 LogFlowFunc(("empty source string, returning\n"));
45 return VINF_SUCCESS;
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 /* Check for a single carriage return (MacOS) */
56 if (pwszSrc[i] == CARRIAGERETURN)
57 ++cwDest;
58 if (pwszSrc[i] == 0)
59 {
60 /* Don't count this, as we do so below. */
61 break;
62 }
63 }
64 /* Count the terminating null byte. */
65 ++cwDest;
66 LogFlowFunc(("returning VINF_SUCCESS, %d 16bit words\n", cwDest));
67 *pcwDest = cwDest;
68 return VINF_SUCCESS;
69}
70
71int vboxClipboardUtf16LinToWin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest,
72 size_t cwDest)
73{
74 size_t i, j;
75 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
76 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
77 {
78 LogRel(("vboxClipboardUtf16LinToWin: received an invalid pointer, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
79 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
80 }
81 /* We only take little endian Utf16 */
82 if (pwszSrc[0] == UTF16BEMARKER)
83 {
84 LogRel(("vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
85 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
86 }
87 if (cwSrc == 0)
88 {
89 if (cwDest == 0)
90 {
91 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
92 return VERR_BUFFER_OVERFLOW;
93 }
94 pu16Dest[0] = 0;
95 LogFlowFunc(("empty source string, returning\n"));
96 return VINF_SUCCESS;
97 }
98 /* Don't copy the endian marker. */
99 for (i = (pwszSrc[0] == UTF16LEMARKER ? 1 : 0), j = 0; i < cwSrc; ++i, ++j)
100 {
101 /* Don't copy the null byte, as we add it below. */
102 if (pwszSrc[i] == 0)
103 break;
104 if (j == cwDest)
105 {
106 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
107 return VERR_BUFFER_OVERFLOW;
108 }
109 if (pwszSrc[i] == LINEFEED)
110 {
111 pu16Dest[j] = CARRIAGERETURN;
112 ++j;
113 if (j == cwDest)
114 {
115 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
116 return VERR_BUFFER_OVERFLOW;
117 }
118 }
119 /* Check for a single carriage return (MacOS) */
120 else if (pwszSrc[i] == CARRIAGERETURN)
121 {
122 /* set cr */
123 pu16Dest[j] = CARRIAGERETURN;
124 ++j;
125 if (j == cwDest)
126 {
127 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
128 return VERR_BUFFER_OVERFLOW;
129 }
130 /* add the lf */
131 pu16Dest[j] = LINEFEED;
132 continue;
133 }
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 /* We only take little endian Utf16 */
158 if (pwszSrc[0] == UTF16BEMARKER)
159 {
160 LogRel(("vboxClipboardUtf16GetLinSize: received a big endian Utf16 string. Returning VERR_INVALID_PARAMETER.\n"));
161 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
162 }
163 if (cwSrc == 0)
164 {
165 LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
166 *pcwDest = 0;
167 return VINF_SUCCESS;
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