summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardHostToGuest.cpp
blob: 1b2d729f980943cbc68cb23025378ce04c720a39 (plain)
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
306
307
308
309
310
311
312
313
314
315
/** $Id: VBoxClientClipboardHostToGuest.cpp $ */
/** @file
 * VBoxClient - Shared Clipboard Host -> Guest copying, Darwin.
 */

/*
 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   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/GuestHost/SharedClipboard.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)
{
    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 */
    size_t cwcDst;
    int rc = ShClUtf16CRLFLenUtf8((RTUTF16 *)pData, cbDataSize / sizeof(RTUTF16), &cwcDst);
    AssertRCReturn(rc, rc);

    cwcDst++; /* Add space for terminator. */

    PRTUTF16 pwszDst = (RTUTF16 *)RTMemAlloc(cwcDst * sizeof(RTUTF16));
    AssertPtrReturn(pwszDst, VERR_NO_MEMORY);

    rc = ShClConvUtf16CRLFToLF((RTUTF16 *)pData, cbDataSize / sizeof(RTUTF16), pwszDst, cwcDst);
    if (RT_SUCCESS(rc))
    {
        /* Paste UTF16 */
        rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pwszDst, cwcDst * sizeof(RTUTF16), kUTTypeUTF16PlainText, true);
        if (RT_SUCCESS(rc))
        {
            /* Paste UTF8 */
            char *pszDst;
            rc = RTUtf16ToUtf8((PRTUTF16)pwszDst, &pszDst);
            if (RT_SUCCESS(rc))
            {
                rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pszDst, strlen(pszDst), kUTTypeUTF8PlainText, false);
                RTStrFree(pszDst);
            }
        }

    }

    RTMemFree(pwszDst);

    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 = ShClDibToBmp(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_SHCL_FMT_UNICODETEXT)
        {
            VBoxClientVerbose(3, "found VBOX_SHCL_FMT_UNICODETEXT: %d\n", fFormatsInternal);

            rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHCL_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_SHCL_FMT_UNICODETEXT);
        }

        else if (fFormatsInternal & VBOX_SHCL_FMT_BITMAP)
        {
            VBoxClientVerbose(3, "found VBOX_SHCL_FMT_BITMAP: %d\n", fFormatsInternal);

            rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHCL_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_SHCL_FMT_BITMAP);
        }

        else if (fFormatsInternal & VBOX_SHCL_FMT_HTML)
        {
            VBoxClientVerbose(3, "found VBOX_SHCL_FMT_HTML: %d\n", fFormatsInternal);

            rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHCL_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_SHCL_FMT_HTML);
        }

        else
        {
            VBoxClientVerbose(3, "received data in unsupported format: %d\n", fFormats);
            break;
        }
    }

    return rc;
}