1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/** $Id: VBoxClientClipboardHostToGuest.cpp $ */
/** @file
* VBoxClient - Shared Clipboard Host -> Guest copying, Darwin.
*/
/*
* Copyright (C) 2007-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <Carbon/Carbon.h>
#include <signal.h>
#include <stdlib.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/stream.h>
#include <iprt/thread.h>
#include <iprt/utf16.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/HostServices/VBoxClipboardSvc.h>
#include <VBox/GuestHost/clipboard-helper.h>
#include "VBoxClientInternal.h"
/**
* Allocate memory for host buffer and receive it.
*
* @param u32ClientId Host connection.
* @param fFormat Buffer data format.
* @param pData Where to store received data.
* @param cbDataSize The size of the received data.
* @param cbMemSize The actual size of memory occupied by *pData.
*
* @returns IPRT status code.
*/
static int vbclClipboardReadHostData(uint32_t u32ClientId, uint32_t fFormat, void **pData, uint32_t *cbDataSize, uint32_t *cbMemSize)
{
int rc;
AssertReturn(pData && cbDataSize && cbMemSize, VERR_INVALID_PARAMETER);
uint32_t cbDataSizeInternal = _4K;
uint32_t cbMemSizeInternal = cbDataSizeInternal;
void *pDataInternal = RTMemPageAllocZ(cbDataSizeInternal);
if (!pDataInternal)
return VERR_NO_MEMORY;
rc = VbglR3ClipboardReadData(u32ClientId, fFormat, pDataInternal, cbMemSizeInternal, &cbDataSizeInternal);
if (rc == VINF_BUFFER_OVERFLOW)
{
/* Reallocate bigger buffer and receive all the data */
RTMemPageFree(pDataInternal, cbMemSizeInternal);
cbDataSizeInternal = cbMemSizeInternal = RT_ALIGN_32(cbDataSizeInternal, PAGE_SIZE);
pDataInternal = RTMemPageAllocZ(cbMemSizeInternal);
if (!pDataInternal)
return VERR_NO_MEMORY;
rc = VbglR3ClipboardReadData(u32ClientId, fFormat, pDataInternal, cbMemSizeInternal, &cbDataSizeInternal);
}
/* Error occurred of zero-sized buffer */
if (RT_FAILURE(rc))
{
RTMemPageFree(pDataInternal, cbMemSizeInternal);
return VERR_NO_MEMORY;
}
*pData = pDataInternal;
*cbDataSize = cbDataSizeInternal;
*cbMemSize = cbMemSizeInternal;
return rc;
}
/**
* Release memory occupied by host buffer.
*
* @param pData Pointer to memory occupied by host buffer.
* @param cbMemSize The actual size of memory occupied by *pData.
*/
static void vbclClipboardReleaseHostData(void **pData, uint32_t cbMemSize)
{
AssertReturnVoid(pData && cbMemSize > 0);
RTMemPageFree(*pData, cbMemSize);
}
/**
* Paste buffer into guest clipboard.
*
* @param pPasteboard Guest PasteBoard reference.
* @param pData Data to be pasted.
* @param cbDataSize The size of *pData.
* @param fFormat Buffer data format.
* @param fClear Whether or not clear guest clipboard before insert data.
*
* @returns IPRT status code.
*/
static int vbclClipboardGuestPasteData(PasteboardRef pPasteboard, UInt8 *pData, CFIndex cbDataSize, CFStringRef sFormat, bool fClear)
{
PasteboardItemID itemId = (PasteboardItemID)1;
CFDataRef textData = NULL;
OSStatus rc;
/* Ignoring sunchronization flags here */
PasteboardSynchronize(pPasteboard);
if (fClear)
{
rc = PasteboardClear(pPasteboard);
AssertReturn(rc == noErr, VERR_NOT_SUPPORTED);
}
/* Create a CData object which we could pass to the pasteboard */
if ((textData = CFDataCreate(kCFAllocatorDefault, pData, cbDataSize)))
{
/* Put the Utf-8 version to the pasteboard */
rc = PasteboardPutItemFlavor(pPasteboard, itemId, sFormat, textData, 0);
CFRelease(textData);
if (rc != noErr)
{
VBoxClientVerbose(3, "unable to put data into guest's clipboard: %d\n", rc);
return VERR_GENERAL_FAILURE;
}
}
else
return VERR_NO_MEMORY;
/* Synchronize updated content */
PasteboardSynchronize(pPasteboard);
return VINF_SUCCESS;
}
/**
* Paste text data into guest clipboard.
*
* @param pPasteboard Guest PasteBoard reference.
* @param pData Data to be pasted.
* @param cbDataSize Size of *pData.
*/
static int vbclClipboardGuestPasteText(PasteboardRef pPasteboard, void *pData, uint32_t cbDataSize)
{
size_t cbActualLen;
int rc;
char *pszUtf8Buf;
RTUTF16 *pDataInternal;
AssertReturn(pData, VERR_INVALID_PARAMETER);
/* Skip zero-sized buffer */
AssertReturn(cbDataSize > 0, VINF_SUCCESS);
/* If buffer content is Unicode text, then deliver
it in both formats UTF16 (original) and UTF8. */
/* Convert END-OF-LINE */
rc = vboxClipboardUtf16GetLinSize((RTUTF16 *)pData, cbDataSize / 2, &cbActualLen);
AssertReturn(RT_SUCCESS(rc), rc);
pDataInternal = (RTUTF16 *)RTMemAlloc(cbActualLen * 2);
AssertReturn(pDataInternal, VERR_NO_MEMORY);
rc = vboxClipboardUtf16WinToLin((RTUTF16 *)pData, cbDataSize / 2, pDataInternal, cbActualLen);
/* Do actual paste */
if (RT_SUCCESS(rc))
{
/* Paste UTF16 */
rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pDataInternal, cbActualLen * 2, kUTTypeUTF16PlainText, true);
if (RT_SUCCESS(rc))
{
/* Paste UTF8 */
rc = RTUtf16ToUtf8((RTUTF16 *)pDataInternal, &pszUtf8Buf);
if (RT_SUCCESS(rc))
{
rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pszUtf8Buf, strlen(pszUtf8Buf), kUTTypeUTF8PlainText, false);
RTStrFree(pszUtf8Buf);
}
}
}
RTMemFree(pDataInternal);
return rc;
}
/**
* Paste picture data into guest clipboard.
*
* @param pPasteboard Guest PasteBoard reference.
* @param pData Data to be pasted.
* @param cbDataSize The size of *pData.
*
* @returns IPRT status code.
*/
static int vbclClipboardGuestPastePicture(PasteboardRef pPasteboard, void *pData, uint32_t cbDataSize)
{
int rc;
void *pBmp;
size_t cbBmpSize;
AssertReturn(pData, VERR_INVALID_PARAMETER);
/* Skip zero-sized buffer */
AssertReturn(cbDataSize > 0, VINF_SUCCESS);
rc = vboxClipboardDibToBmp(pData, cbDataSize, &pBmp, &cbBmpSize);
AssertReturn(RT_SUCCESS(rc), rc);
rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pBmp, cbBmpSize, kUTTypeBMP, true);
RTMemFree(pBmp);
return rc;
}
/**
* Read host's clipboard buffer and put its content to guest clipboard.
*
* @param u32ClientId Host connection.
* @param pPasteboard Guest PasteBoard reference.
* @param fFormats List of data formats (bit field) received from host.
*
* @returns IPRT status code.
*/
int vbclClipboardForwardToGuest(uint32_t u32ClientId, PasteboardRef pPasteboard, uint32_t fFormats)
{
int rc = VERR_INVALID_PARAMETER;
void *pData;
uint32_t cbDataSize, cbMemSize;
uint32_t fFormatsInternal = fFormats;
/* Walk across all item(s) formats */
while (fFormatsInternal)
{
if (fFormatsInternal & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
{
VBoxClientVerbose(3, "found VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT: %d\n", fFormatsInternal);
rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT, &pData, &cbDataSize, &cbMemSize);
if (RT_SUCCESS(rc))
{
/* Store data in guest buffer */
rc = vbclClipboardGuestPasteText(pPasteboard, pData, cbDataSize);
/* Release occupied resources */
vbclClipboardReleaseHostData(&pData, cbMemSize);
}
fFormatsInternal &= ~((uint32_t)VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
}
else if (fFormatsInternal & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
{
VBoxClientVerbose(3, "found VBOX_SHARED_CLIPBOARD_FMT_BITMAP: %d\n", fFormatsInternal);
rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_BITMAP, &pData, &cbDataSize, &cbMemSize);
if (RT_SUCCESS(rc))
{
/* Store data in guest buffer */
rc = vbclClipboardGuestPastePicture(pPasteboard, pData, cbDataSize);
/* Release occupied resources */
vbclClipboardReleaseHostData(&pData, cbMemSize);
}
fFormatsInternal &= ~((uint32_t)VBOX_SHARED_CLIPBOARD_FMT_BITMAP);
}
else if (fFormatsInternal & VBOX_SHARED_CLIPBOARD_FMT_HTML)
{
VBoxClientVerbose(3, "found VBOX_SHARED_CLIPBOARD_FMT_HTML: %d\n", fFormatsInternal);
rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_HTML, &pData, &cbDataSize, &cbMemSize);
if (RT_SUCCESS(rc))
{
/* Store data in guest buffer */
rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pData, cbDataSize, kUTTypeHTML, true);
/* Release occupied resources */
vbclClipboardReleaseHostData(&pData, cbMemSize);
}
fFormatsInternal &= ~((uint32_t)VBOX_SHARED_CLIPBOARD_FMT_HTML);
}
else
{
VBoxClientVerbose(3, "received data in unsupported format: %d\n", fFormats);
break;
}
}
return rc;
}
|