VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.cpp@ 83630

Last change on this file since 83630 was 83630, checked in by vboxsync, 5 years ago

SharedClipboard: Fixed leaks in sloppy error handling in writeToPasteboard. bugref:9620

  • Property eol-style set to native
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 KB
Line 
1/* $Id: darwin-pasteboard.cpp 83630 2020-04-08 19:13:56Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Mac OS X host implementation.
4 */
5
6/*
7 * Includes contributions from François Revol
8 *
9 * Copyright (C) 2008-2020 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
25#include <Carbon/Carbon.h>
26
27#include <iprt/assert.h>
28#include <iprt/mem.h>
29#include <iprt/errcore.h>
30#include <iprt/utf16.h>
31
32#include "VBox/log.h"
33#include "VBox/HostServices/VBoxClipboardSvc.h"
34#include "VBox/GuestHost/SharedClipboard.h"
35#include "VBox/GuestHost/clipboard-helper.h"
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41/* For debugging */
42//#define SHOW_CLIPBOARD_CONTENT
43
44
45/**
46 * Initialize the global pasteboard and return a reference to it.
47 *
48 * @param pPasteboardRef Reference to the global pasteboard.
49 *
50 * @returns IPRT status code.
51 */
52int initPasteboard(PasteboardRef *pPasteboardRef)
53{
54 int rc = VINF_SUCCESS;
55
56 if (PasteboardCreate(kPasteboardClipboard, pPasteboardRef))
57 rc = VERR_NOT_SUPPORTED;
58
59 return rc;
60}
61
62/**
63 * Release the reference to the global pasteboard.
64 *
65 * @param pPasteboardRef Reference to the global pasteboard.
66 */
67void destroyPasteboard(PasteboardRef *pPasteboardRef)
68{
69 CFRelease(*pPasteboardRef);
70 *pPasteboardRef = NULL;
71}
72
73/**
74 * Inspect the global pasteboard for new content. Check if there is some type
75 * that is supported by vbox and return it.
76 *
77 * @param pPasteboard Reference to the global pasteboard.
78 * @param pfFormats Pointer for the bit combination of the
79 * supported types.
80 * @param pfChanged True if something has changed after the
81 * last call.
82 *
83 * @returns IPRT status code. (Always VINF_SUCCESS atm.)
84 */
85int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged)
86{
87 OSStatus err = noErr;
88 *pfFormats = 0;
89 *pfChanged = true;
90
91 PasteboardSyncFlags syncFlags;
92 /* Make sure all is in sync */
93 syncFlags = PasteboardSynchronize(pPasteboard);
94 /* If nothing changed return */
95 if (!(syncFlags & kPasteboardModified))
96 {
97 *pfChanged = false;
98 Log2(("queryNewPasteboardFormats: no change\n"));
99 return VINF_SUCCESS;
100 }
101
102 /* Are some items in the pasteboard? */
103 ItemCount itemCount;
104 err = PasteboardGetItemCount(pPasteboard, &itemCount);
105 if (itemCount < 1)
106 {
107 Log(("queryNewPasteboardFormats: changed: No items on the pasteboard\n"));
108 return VINF_SUCCESS;
109 }
110
111 /* The id of the first element in the pasteboard */
112 int rc = VINF_SUCCESS;
113 PasteboardItemID itemID;
114 if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID)))
115 {
116 /* Retrieve all flavors in the pasteboard, maybe there
117 * is something we can use. */
118 CFArrayRef flavorTypeArray;
119 if (!(err = PasteboardCopyItemFlavors(pPasteboard, itemID, &flavorTypeArray)))
120 {
121 CFIndex flavorCount;
122 flavorCount = CFArrayGetCount(flavorTypeArray);
123 for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++)
124 {
125 CFStringRef flavorType;
126 flavorType = static_cast <CFStringRef>(CFArrayGetValueAtIndex(flavorTypeArray, flavorIndex));
127 /* Currently only unicode supported */
128 if ( UTTypeConformsTo(flavorType, kUTTypeUTF8PlainText)
129 || UTTypeConformsTo(flavorType, kUTTypeUTF16PlainText))
130 {
131 Log(("queryNewPasteboardFormats: Unicode flavor detected.\n"));
132 *pfFormats |= VBOX_SHCL_FMT_UNICODETEXT;
133 }
134 else if (UTTypeConformsTo(flavorType, kUTTypeBMP))
135 {
136 Log(("queryNewPasteboardFormats: BMP flavor detected.\n"));
137 *pfFormats |= VBOX_SHCL_FMT_BITMAP;
138 }
139 }
140 CFRelease(flavorTypeArray);
141 }
142 }
143
144 Log(("queryNewPasteboardFormats: changed: *pfFormats=%#x\n", *pfFormats));
145 return rc;
146}
147
148/**
149 * Read content from the host clipboard and write it to the internal clipboard
150 * structure for further processing.
151 *
152 * @param pPasteboard Reference to the global pasteboard.
153 * @param fFormat The format type which should be read.
154 * @param pv The destination buffer.
155 * @param cb The size of the destination buffer.
156 * @param pcbActual The size which is needed to transfer the content.
157 *
158 * @returns IPRT status code.
159 */
160int readFromPasteboard(PasteboardRef pPasteboard, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcbActual)
161{
162 Log(("readFromPasteboard: fFormat = %02X\n", fFormat));
163
164 OSStatus err = noErr;
165
166 /* Make sure all is in sync */
167 PasteboardSynchronize(pPasteboard);
168
169 /* Are some items in the pasteboard? */
170 ItemCount itemCount;
171 err = PasteboardGetItemCount(pPasteboard, &itemCount);
172 if (itemCount < 1)
173 return VINF_SUCCESS;
174
175 /* The id of the first element in the pasteboard */
176 int rc = VERR_NOT_SUPPORTED;
177 PasteboardItemID itemID;
178 if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID)))
179 {
180 /* The guest request unicode */
181 if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
182 {
183 CFDataRef outData;
184 PRTUTF16 pwszTmp = NULL;
185 /* Try utf-16 first */
186 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeUTF16PlainText, &outData)))
187 {
188 Log(("Clipboard content is utf-16\n"));
189
190 PRTUTF16 pwszString = (PRTUTF16)CFDataGetBytePtr(outData);
191 if (pwszString)
192 rc = RTUtf16DupEx(&pwszTmp, pwszString, 0);
193 else
194 rc = VERR_INVALID_PARAMETER;
195 }
196 /* Second try is utf-8 */
197 else
198 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeUTF8PlainText, &outData)))
199 {
200 Log(("readFromPasteboard: clipboard content is utf-8\n"));
201 const char *pszString = (const char *)CFDataGetBytePtr(outData);
202 if (pszString)
203 rc = RTStrToUtf16(pszString, &pwszTmp);
204 else
205 rc = VERR_INVALID_PARAMETER;
206 }
207 if (pwszTmp)
208 {
209 /* Check how much longer will the converted text will be. */
210 size_t cwSrc = RTUtf16Len(pwszTmp);
211 size_t cwDest;
212 rc = ShClUtf16GetWinSize(pwszTmp, cwSrc, &cwDest);
213 if (RT_FAILURE(rc))
214 {
215 RTUtf16Free(pwszTmp);
216 Log(("readFromPasteboard: clipboard conversion failed. vboxClipboardUtf16GetWinSize returned %Rrc. Abandoning.\n", rc));
217 AssertRCReturn(rc, rc);
218 }
219 /* Set the actually needed data size */
220 *pcbActual = cwDest * 2;
221 /* Return success state */
222 rc = VINF_SUCCESS;
223 /* Do not copy data if the dst buffer is not big enough. */
224 if (*pcbActual <= cb)
225 {
226 rc = ShClUtf16LinToWin(pwszTmp, RTUtf16Len(pwszTmp), static_cast <PRTUTF16>(pv), cb / 2);
227 if (RT_FAILURE(rc))
228 {
229 RTUtf16Free(pwszTmp);
230 Log(("readFromPasteboard: clipboard conversion failed. vboxClipboardUtf16LinToWin() returned %Rrc. Abandoning.\n", rc));
231 AssertRCReturn(rc, rc);
232 }
233#ifdef SHOW_CLIPBOARD_CONTENT
234 Log(("readFromPasteboard: clipboard content: %ls\n", static_cast <PRTUTF16>(pv)));
235#endif
236 }
237 /* Free the temp string */
238 RTUtf16Free(pwszTmp);
239 }
240 }
241 /* The guest request BITMAP */
242 else if (fFormat & VBOX_SHCL_FMT_BITMAP)
243 {
244 CFDataRef outData;
245 const void *pTmp = NULL;
246 size_t cbTmpSize;
247 /* Get the data from the pasteboard */
248 if (!(err = PasteboardCopyItemFlavorData(pPasteboard, itemID, kUTTypeBMP, &outData)))
249 {
250 Log(("Clipboard content is BMP\n"));
251 pTmp = CFDataGetBytePtr(outData);
252 cbTmpSize = CFDataGetLength(outData);
253 }
254 if (pTmp)
255 {
256 const void *pDib;
257 size_t cbDibSize;
258 rc = ShClBmpGetDib(pTmp, cbTmpSize, &pDib, &cbDibSize);
259 if (RT_FAILURE(rc))
260 {
261 rc = VERR_NOT_SUPPORTED;
262 Log(("readFromPasteboard: unknown bitmap format. vboxClipboardBmpGetDib returned %Rrc. Abandoning.\n", rc));
263 AssertRCReturn(rc, rc);
264 }
265
266 *pcbActual = cbDibSize;
267 /* Return success state */
268 rc = VINF_SUCCESS;
269 /* Do not copy data if the dst buffer is not big enough. */
270 if (*pcbActual <= cb)
271 {
272 memcpy(pv, pDib, cbDibSize);
273#ifdef SHOW_CLIPBOARD_CONTENT
274 Log(("readFromPasteboard: clipboard content bitmap %d bytes\n", cbDibSize));
275#endif
276 }
277 }
278 }
279 }
280
281 Log(("readFromPasteboard: rc = %02X\n", rc));
282 return rc;
283}
284
285/**
286 * Takes the ownership of the pasteboard.
287 *
288 * This is called when the other end reports available formats.
289 *
290 * @returns VBox status code.
291 * @param hPasteboard The pastboard handle (reference).
292 * @param idOwnership The ownership ID to use now.
293 * @param pszOwnershipFlavor The ownership indicator flavor
294 * @param pszOwnershipValue The ownership value (stringified format mask).
295 *
296 * @todo Add fFormats so we can make promises about available formats at once
297 * without needing to request any data first. That might help on
298 * flavor priority.
299 */
300int takePasteboardOwnership(PasteboardRef hPasteboard, uint64_t idOwnership,
301 const char *pszOwnershipFlavor, const char *pszOwnershipValue)
302{
303 /*
304 * Clear the pasteboard and take ownership over it.
305 */
306 OSStatus orc = PasteboardClear(hPasteboard);
307 if (orc == 0)
308 {
309 /* For good measure. */
310 PasteboardSynchronize(hPasteboard);
311
312 /*
313 * Put the ownership flavor and value onto the clipboard.
314 */
315 CFDataRef hData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)pszOwnershipValue, strlen(pszOwnershipValue));
316 if (hData)
317 {
318 CFStringRef hFlavor = CFStringCreateWithCString(kCFAllocatorDefault, pszOwnershipFlavor, kCFStringEncodingUTF8);
319 if (hFlavor)
320 {
321 orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
322 hFlavor, hData, kPasteboardFlavorNoFlags);
323 if (orc == 0)
324 Log(("takePasteboardOwnership: idOwnership=%RX64 flavor=%s value=%s\n",
325 idOwnership, pszOwnershipFlavor, pszOwnershipValue));
326 else
327 Log(("takePasteboardOwnership: PasteboardPutItemFlavor -> %d (%#x)!\n", orc, orc));
328 CFRelease(hFlavor);
329 }
330 else
331 Log(("takePasteboardOwnership: CFStringCreateWithCString failed!\n"));
332 CFRelease(hData);
333 }
334 else
335 Log(("takePasteboardOwnership: CFDataCreate failed!\n"));
336 }
337 else
338 Log(("takePasteboardOwnership: PasteboardClear failed -> %d (%#x)\n", orc, orc));
339 return orc == 0 ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
340}
341
342/**
343 * Write clipboard content to the host clipboard from the internal clipboard
344 * structure.
345 *
346 * @param hPasteboard Reference to the global pasteboard.
347 * @param idOwnership The ownership ID.
348 * @param pv The source buffer.
349 * @param cb The size of the source buffer.
350 * @param fFormat The format type which should be written.
351 *
352 * @returns IPRT status code.
353 */
354int writeToPasteboard(PasteboardRef hPasteboard, uint64_t idOwnership, const void *pv, uint32_t cb, uint32_t fFormat)
355{
356 int rc;
357 OSStatus orc;
358 Log(("writeToPasteboard: fFormat = %02X\n", fFormat));
359
360 /* Make sure all is in sync */
361 PasteboardSynchronize(hPasteboard);
362
363 /*
364 * Handle the unicode text
365 */
366 if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
367 {
368 PCRTUTF16 const pwszSrc = (PCRTUTF16)pv;
369 size_t const cwcSrc = cb / sizeof(RTUTF16);
370
371 /*
372 * If the other side is windows or OS/2, we may have to convert
373 * '\r\n' -> '\n' and the drop ending marker.
374 */
375
376 /* How long will the converted text be? */
377 size_t cwcDst = 0;
378 rc = ShClUtf16GetLinSize(pwszSrc, cwcSrc, &cwcDst);
379 AssertMsgRCReturn(rc, ("ShClUtf16GetLinSize failed: %Rrc\n", rc), rc);
380
381 /* Ignore empty strings? */
382 if (cwcDst == 0)
383 {
384 Log(("writeToPasteboard: received empty string from the guest; ignoreing it.\n"));
385 return VINF_SUCCESS;
386 }
387
388 /* Allocate the necessary memory and do the conversion. */
389 PRTUTF16 pwszDst = (PRTUTF16)RTMemAlloc(cwcDst * sizeof(RTUTF16));
390 AssertMsgReturn(pwszDst, ("cwcDst=%#zx\n", cwcDst), VERR_NO_UTF16_MEMORY);
391
392 rc = ShClUtf16WinToLin(pwszSrc, cwcSrc, pwszDst, cwcDst);
393 if (RT_SUCCESS(rc))
394 {
395 /*
396 * Create an immutable CFData object that we can place on the clipboard.
397 */
398 rc = VINF_SUCCESS;
399 CFDataRef hData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)pwszDst, cwcDst * sizeof(RTUTF16));
400 if (hData)
401 {
402 orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
403 kUTTypeUTF16PlainText, hData, kPasteboardFlavorNoFlags);
404 if (orc != 0)
405 {
406 Log(("writeToPasteboard: PasteboardPutItemFlavor/kUTTypeUTF16PlainText failed: %d (%#x)\n", orc, orc));
407 rc = VERR_GENERAL_FAILURE;
408 }
409 else
410 {
411 Log(("writeToPasteboard: CFDataCreate/UTF16 failed!\n"));
412 rc = VERR_NO_MEMORY;
413 }
414 CFRelease(hData);
415 }
416
417 /*
418 * Now for the UTF-8 version.
419 */
420 char *pszDst;
421 int rc2 = RTUtf16ToUtf8(pwszDst, &pszDst);
422 if (RT_SUCCESS(rc2))
423 {
424 hData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)pszDst, strlen(pszDst));
425 if (hData)
426 {
427 orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
428 kUTTypeUTF8PlainText, hData, kPasteboardFlavorNoFlags);
429 if (orc != 0)
430 {
431 Log(("writeToPasteboard: PasteboardPutItemFlavor/kUTTypeUTF8PlainText failed: %d (%#x)\n", orc, orc));
432 rc = VERR_GENERAL_FAILURE;
433 }
434 CFRelease(hData);
435 }
436 else
437 {
438 Log(("writeToPasteboard: CFDataCreate/UTF8 failed!\n"));
439 rc = VERR_NO_MEMORY;
440 }
441 RTStrFree(pszDst);
442 }
443 else
444 rc = rc2;
445 }
446 else
447 Log(("writeToPasteboard: clipboard conversion failed. vboxClipboardUtf16WinToLin() returned %Rrc. Abandoning.\n", rc));
448
449 RTMemFree(pwszDst);
450 }
451 /*
452 * Handle the bitmap. We convert the DIB to a bitmap and put it on
453 * the pasteboard using the BMP flavor.
454 */
455 else if (fFormat & VBOX_SHCL_FMT_BITMAP)
456 {
457 /* Create a full BMP from it */
458 void *pvBmp;
459 size_t cbBmp;
460 rc = ShClDibToBmp(pv, cb, &pvBmp, &cbBmp);
461 if (RT_SUCCESS(rc))
462 {
463 CFDataRef hData = CFDataCreate(kCFAllocatorDefault, (UInt8 const *)pvBmp, cbBmp);
464 if (hData)
465 {
466 orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
467 kUTTypeBMP, hData, kPasteboardFlavorNoFlags);
468 if (orc != 0)
469 {
470 Log(("writeToPasteboard: PasteboardPutItemFlavor/kUTTypeBMP failed: %d (%#x)\n", orc, orc));
471 rc = VERR_GENERAL_FAILURE;
472 }
473 CFRelease(hData);
474 }
475 else
476 {
477 Log(("writeToPasteboard: CFDataCreate/UTF8 failed!\n"));
478 rc = VERR_NO_MEMORY;
479 }
480 RTMemFree(pvBmp);
481 }
482 }
483 else
484 rc = VERR_NOT_IMPLEMENTED;
485
486 Log(("writeToPasteboard: rc=%Rrc\n", rc));
487 return rc;
488}
489
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