VirtualBox

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

Last change on this file since 8083 was 7117, checked in by vboxsync, 17 years ago

r=bird: subversion attributes. some hungarian fixes. do not pass stuff by reference unless its normal C++ code and very convenient (because it makes it totally unreadable otherwise). A couple of questions. (hope it still compiles)

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