VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp@ 104759

Last change on this file since 104759 was 104759, checked in by vboxsync, 7 months ago

Shared Clipboard: Attempt to prevent extra new lines when copying text from Linux to Windows, bugref:10694.

  • Property eol-style set to native
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 45.9 KB
Line 
1/* $Id: clipboard-common.cpp 104759 2024-05-22 13:59:29Z vboxsync $ */
2/** @file
3 * Shared Clipboard: Some helper function for converting between the various eol.
4 */
5
6/*
7 * Includes contributions from François Revol
8 *
9 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * SPDX-License-Identifier: GPL-3.0-only
28 */
29
30#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
31
32#include <iprt/alloc.h>
33#include <iprt/assert.h>
34#include <iprt/semaphore.h>
35#include <iprt/path.h>
36#include <iprt/rand.h>
37#include <iprt/utf16.h>
38
39#include <iprt/formats/bmp.h>
40
41#include <iprt/errcore.h>
42#include <VBox/log.h>
43#include <VBox/GuestHost/clipboard-helper.h>
44#include <VBox/HostServices/VBoxClipboardSvc.h>
45
46
47/*********************************************************************************************************************************
48* Prototypes *
49*********************************************************************************************************************************/
50static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource);
51static int shClEventSourceUnregisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT pEvent);
52
53static void shClEventDestroy(PSHCLEVENT pEvent);
54DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent);
55
56
57/*********************************************************************************************************************************
58* Implementation *
59*********************************************************************************************************************************/
60
61/**
62 * Allocates a new event payload.
63 *
64 * @returns VBox status code.
65 * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
66 * @param pvData Data to associate to this payload.
67 * The payload owns the data then.
68 * @param cbData Size (in bytes) of data to associate.
69 * @param ppPayload Where to store the allocated event payload on success.
70 */
71int ShClPayloadInit(uint32_t uID, void *pvData, uint32_t cbData,
72 PSHCLEVENTPAYLOAD *ppPayload)
73{
74 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
75 AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
76
77 PSHCLEVENTPAYLOAD pPayload = (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
78 if (pPayload)
79 {
80 pPayload->pvData = pvData;
81 pPayload->cbData = cbData;
82 pPayload->uID = uID;
83
84 *ppPayload = pPayload;
85 return VINF_SUCCESS;
86 }
87
88 return VERR_NO_MEMORY;
89}
90
91/**
92 * Allocates a new event payload.
93 *
94 * @returns VBox status code.
95 * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
96 * @param pvData Data block to allocate (duplicate) to this payload.
97 * @param cbData Size (in bytes) of data block to allocate.
98 * @param ppPayload Where to store the allocated event payload on success.
99 */
100int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData,
101 PSHCLEVENTPAYLOAD *ppPayload)
102{
103 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
104 AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
105
106 void *pvDataDup = RTMemDup(pvData, cbData);
107 if (pvDataDup)
108 return ShClPayloadInit(uID, pvDataDup, cbData, ppPayload);
109
110 return VERR_NO_MEMORY;
111}
112
113/**
114 * Frees an event payload.
115 *
116 * @returns VBox status code.
117 * @param pPayload Event payload to free.
118 */
119void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload)
120{
121 if (!pPayload)
122 return;
123
124 if (pPayload->pvData)
125 {
126 Assert(pPayload->cbData);
127 RTMemFree(pPayload->pvData);
128 pPayload->pvData = NULL;
129 }
130
131 pPayload->cbData = 0;
132 pPayload->uID = UINT32_MAX;
133
134 RTMemFree(pPayload);
135}
136
137/**
138 * Creates a new event source.
139 *
140 * @returns VBox status code.
141 * @param pSource Event source to create.
142 * @param uID ID to use for event source.
143 */
144int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
145{
146 LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
147 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
148
149 int rc = RTCritSectInit(&pSource->CritSect);
150 AssertRCReturn(rc, rc);
151
152 RTListInit(&pSource->lstEvents);
153
154 pSource->uID = uID;
155 /* Choose a random event ID starting point. */
156 pSource->idNextEvent = RTRandU32Ex(1, VBOX_SHCL_MAX_EVENTS - 1);
157
158 return VINF_SUCCESS;
159}
160
161/**
162 * Destroys an event source.
163 *
164 * @returns VBox status code.
165 * @param pSource Event source to destroy.
166 */
167int ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource)
168{
169 if (!pSource)
170 return VINF_SUCCESS;
171
172 if (!RTCritSectIsInitialized(&pSource->CritSect)) /* Already destroyed? Bail out. */
173 return VINF_SUCCESS;
174
175 LogFlowFunc(("ID=%RU32\n", pSource->uID));
176
177 int rc = RTCritSectEnter(&pSource->CritSect);
178 if (RT_SUCCESS(rc))
179 {
180 shClEventSourceResetInternal(pSource);
181
182 rc = RTCritSectLeave(&pSource->CritSect);
183 AssertRC(rc);
184
185 RTCritSectDelete(&pSource->CritSect);
186
187 pSource->uID = UINT16_MAX;
188 pSource->idNextEvent = UINT32_MAX;
189 }
190
191 return rc;
192}
193
194/**
195 * Resets an event source, internal version.
196 *
197 * @param pSource Event source to reset.
198 */
199static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource)
200{
201 LogFlowFunc(("ID=%RU32\n", pSource->uID));
202
203 PSHCLEVENT pEvIt;
204 PSHCLEVENT pEvItNext;
205 RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
206 {
207 bool const fDealloc = ASMAtomicReadU32(&pEvIt->cRefs) == 0; /* Still any references left? Skip de-allocation. */
208 if (!fDealloc)
209 Log3Func(("Event %RU32 has %RU32 references left, skipping de-allocation\n", pEvIt->idEvent, pEvIt->cRefs));
210
211 shClEventDestroy(pEvIt);
212
213 int rc2 = shClEventSourceUnregisterEvent(pSource, pEvIt);
214 AssertRC(rc2);
215
216 if (fDealloc)
217 {
218 RTMemFree(pEvIt);
219 pEvIt = NULL;
220 }
221 }
222}
223
224/**
225 * Resets an event source.
226 *
227 * @param pSource Event source to reset.
228 */
229void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
230{
231 int rc2 = RTCritSectEnter(&pSource->CritSect);
232 if (RT_SUCCESS(rc2))
233 {
234 shClEventSourceResetInternal(pSource);
235
236 rc2 = RTCritSectLeave(&pSource->CritSect);
237 AssertRC(rc2);
238 }
239}
240
241/**
242 * Generates a new event ID for a specific event source and registers it.
243 *
244 * @returns VBox status code.
245 * @param pSource Event source to generate event for.
246 * @param ppEvent Where to return the new event generated on success.
247 */
248int ShClEventSourceGenerateAndRegisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT *ppEvent)
249{
250 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
251 AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
252
253 PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
254 AssertReturn(pEvent, VERR_NO_MEMORY);
255 int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
256 if (RT_SUCCESS(rc))
257 {
258 rc = RTCritSectEnter(&pSource->CritSect);
259 if (RT_SUCCESS(rc))
260 {
261 /*
262 * Allocate an unique event ID.
263 */
264 for (uint32_t cTries = 0;; cTries++)
265 {
266 SHCLEVENTID idEvent = ++pSource->idNextEvent;
267 if (idEvent < VBOX_SHCL_MAX_EVENTS)
268 { /* likely */ }
269 else
270 pSource->idNextEvent = idEvent = 1; /* zero == error, remember! */
271
272 if (shclEventGet(pSource, idEvent) == NULL)
273 {
274 pEvent->pParent = pSource;
275 pEvent->idEvent = idEvent;
276 RTListAppend(&pSource->lstEvents, &pEvent->Node);
277
278 rc = RTCritSectLeave(&pSource->CritSect);
279 AssertRC(rc);
280
281 LogFlowFunc(("uSource=%RU16: New event: %#x\n", pSource->uID, idEvent));
282
283 ShClEventRetain(pEvent);
284 *ppEvent = pEvent;
285
286 return VINF_SUCCESS;
287 }
288
289 AssertBreak(cTries < 4096);
290 }
291
292 rc = RTCritSectLeave(&pSource->CritSect);
293 AssertRC(rc);
294 }
295 }
296
297 AssertMsgFailed(("Unable to register a new event ID for event source %RU16\n", pSource->uID));
298
299 RTSemEventMultiDestroy(pEvent->hEvtMulSem);
300 pEvent->hEvtMulSem = NIL_RTSEMEVENTMULTI;
301 RTMemFree(pEvent);
302 return rc;
303}
304
305/**
306 * Destroys an event.
307 *
308 * @param pEvent Event to destroy.
309 */
310static void shClEventDestroy(PSHCLEVENT pEvent)
311{
312 if (!pEvent)
313 return;
314
315 LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
316
317 if (pEvent->hEvtMulSem != NIL_RTSEMEVENT)
318 {
319 RTSemEventMultiDestroy(pEvent->hEvtMulSem);
320 pEvent->hEvtMulSem = NIL_RTSEMEVENT;
321 }
322
323 ShClPayloadFree(pEvent->pPayload);
324 pEvent->pPayload = NULL;
325
326 pEvent->idEvent = NIL_SHCLEVENTID;
327}
328
329/**
330 * Unregisters an event.
331 *
332 * @returns VBox status code.
333 * @param pSource Event source to unregister event for.
334 * @param pEvent Event to unregister.
335 */
336static int shClEventSourceUnregisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT pEvent)
337{
338 RT_NOREF(pSource);
339
340 LogFlowFunc(("idEvent=%RU32, cRefs=%RU32\n", pEvent->idEvent, pEvent->cRefs));
341
342 RTListNodeRemove(&pEvent->Node);
343 pEvent->pParent = NULL;
344
345 return VINF_SUCCESS;
346}
347
348/**
349 * Returns a specific event of a event source. Inlined version.
350 *
351 * @returns Pointer to event if found, or NULL if not found.
352 * @param pSource Event source to get event from.
353 * @param uID Event ID to get.
354 */
355DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
356{
357 PSHCLEVENT pEvent;
358 RTListForEach(&pSource->lstEvents, pEvent, SHCLEVENT, Node)
359 {
360 if (pEvent->idEvent == idEvent)
361 return pEvent;
362 }
363
364 return NULL;
365}
366
367/**
368 * Returns a specific event of a event source.
369 *
370 * @returns Pointer to event if found, or NULL if not found.
371 * @param pSource Event source to get event from.
372 * @param idEvent ID of event to return.
373 */
374PSHCLEVENT ShClEventSourceGetFromId(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
375{
376 AssertPtrReturn(pSource, NULL);
377
378 int rc = RTCritSectEnter(&pSource->CritSect);
379 if (RT_SUCCESS(rc))
380 {
381 PSHCLEVENT pEvent = shclEventGet(pSource, idEvent);
382
383 rc = RTCritSectLeave(&pSource->CritSect);
384 AssertRC(rc);
385
386 return pEvent;
387 }
388
389 return NULL;
390}
391
392/**
393 * Returns the last (newest) event ID which has been registered for an event source.
394 *
395 * @returns Pointer to last registered event, or NULL if not found.
396 * @param pSource Event source to get last registered event from.
397 */
398PSHCLEVENT ShClEventSourceGetLast(PSHCLEVENTSOURCE pSource)
399{
400 AssertPtrReturn(pSource, NULL);
401
402 int rc = RTCritSectEnter(&pSource->CritSect);
403 if (RT_SUCCESS(rc))
404 {
405 PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
406
407 rc = RTCritSectLeave(&pSource->CritSect);
408 AssertRC(rc);
409
410 return pEvent;
411 }
412
413 return NULL;
414}
415
416/**
417 * Returns the current reference count for a specific event.
418 *
419 * @returns Reference count.
420 * @param pSource Event source the specific event is part of.
421 * @param idEvent Event ID to return reference count for.
422 */
423uint32_t ShClEventGetRefs(PSHCLEVENT pEvent)
424{
425 AssertPtrReturn(pEvent, 0);
426
427 return ASMAtomicReadU32(&pEvent->cRefs);
428}
429
430/**
431 * Detaches a payload from an event, internal version.
432 *
433 * @returns Pointer to the detached payload. Can be NULL if the event has no payload.
434 * @param pEvent Event to detach payload for.
435 */
436static PSHCLEVENTPAYLOAD shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
437{
438#ifdef VBOX_STRICT
439 AssertPtrReturn(pEvent, NULL);
440#endif
441
442 PSHCLEVENTPAYLOAD pPayload = pEvent->pPayload;
443
444 pEvent->pPayload = NULL;
445
446 return pPayload;
447}
448
449/**
450 * Waits for an event to get signalled.
451 *
452 * @returns VBox status code.
453 * @retval VERR_SHCLPB_EVENT_FAILED if the event has a set error code.
454 * @param pEvent Event to wait for.
455 * @param uTimeoutMs Timeout (in ms) to wait.
456 * @param pRc Where to return the event rc. Optional and can be NULL.
457 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
458 * SharedClipboardPayloadFree(). Optional.
459 */
460int ShClEventWaitEx(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, int *pRc, PSHCLEVENTPAYLOAD *ppPayload)
461{
462 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
463 AssertPtrNullReturn(ppPayload, VERR_INVALID_POINTER);
464 LogFlowFuncEnter();
465
466 int rc = RTSemEventMultiWait(pEvent->hEvtMulSem, uTimeoutMs);
467 if (RT_SUCCESS(rc))
468 {
469 if (RT_FAILURE(pEvent->rc))
470 rc = VERR_SHCLPB_EVENT_FAILED;
471
472 if (pRc)
473 *pRc = pEvent->rc;
474
475 if (ppPayload)
476 {
477 /* Make sure to detach payload here, as the caller now owns the data. */
478 *ppPayload = shclEventPayloadDetachInternal(pEvent);
479 }
480 }
481
482 if (RT_FAILURE(rc))
483 LogRel2(("Shared Clipboard: Waiting for event %RU32 failed, rc=%Rrc\n", pEvent->idEvent, rc));
484
485 LogFlowFuncLeaveRC(rc);
486 return rc;
487}
488
489/**
490 * Waits for an event to get signalled.
491 *
492 * @returns VBox status code.
493 * @retval VERR_SHCLPB_EVENT_FAILED if the event has a set error code.
494 * @param pEvent Event to wait for.
495 * @param uTimeoutMs Timeout (in ms) to wait.
496 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
497 * SharedClipboardPayloadFree(). Optional.
498 */
499int ShClEventWait(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, PSHCLEVENTPAYLOAD *ppPayload)
500{
501 return ShClEventWaitEx(pEvent, uTimeoutMs, NULL /* pRc */, ppPayload);
502}
503
504/**
505 * Retains an event by increasing its reference count.
506 *
507 * @returns New reference count, or UINT32_MAX if failed.
508 * @param pEvent Event to retain.
509 */
510uint32_t ShClEventRetain(PSHCLEVENT pEvent)
511{
512 AssertPtrReturn(pEvent, UINT32_MAX);
513 AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) < 64, UINT32_MAX);
514 return ASMAtomicIncU32(&pEvent->cRefs);
515}
516
517/**
518 * Releases event by decreasing its reference count. Will be destroyed once the reference count reaches 0.
519 *
520 * @returns New reference count, or UINT32_MAX if failed.
521 * @param pEvent Event to release.
522 * If the reference count reaches 0, the event will
523 * be destroyed and \a pEvent will be invalid.
524 */
525uint32_t ShClEventRelease(PSHCLEVENT pEvent)
526{
527 if (!pEvent)
528 return 0;
529
530 AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) > 0, UINT32_MAX);
531
532 uint32_t const cRefs = ASMAtomicDecU32(&pEvent->cRefs);
533 if (cRefs == 0)
534 {
535 int rc;
536 PSHCLEVENTSOURCE pParent = pEvent->pParent;
537 if ( pParent
538 && RTCritSectIsInitialized(&pParent->CritSect))
539 {
540 rc = RTCritSectEnter(&pParent->CritSect);
541 if (RT_SUCCESS(rc))
542 {
543 rc = shClEventSourceUnregisterEvent(pParent, pEvent);
544
545 int rc2 = RTCritSectLeave(&pParent->CritSect);
546 if (RT_SUCCESS(rc))
547 rc = rc2;
548 }
549 }
550 else
551 rc = VINF_SUCCESS;
552
553 if (RT_SUCCESS(rc))
554 {
555 shClEventDestroy(pEvent);
556
557 RTMemFree(pEvent);
558 pEvent = NULL;
559 }
560
561 return RT_SUCCESS(rc) ? 0 : UINT32_MAX;
562 }
563
564 return cRefs;
565}
566
567/**
568 * Signals an event, extended version.
569 *
570 * @returns VBox status code.
571 * @param pEvent Event to signal.
572 * @param rc Result code to set.
573 * @param pPayload Event payload to associate. Takes ownership on
574 * success. Optional.
575 */
576int ShClEventSignalEx(PSHCLEVENT pEvent, int rc, PSHCLEVENTPAYLOAD pPayload)
577{
578 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
579
580 Assert(pEvent->pPayload == NULL);
581
582 pEvent->rc = rc;
583 pEvent->pPayload = pPayload;
584
585 int rc2 = RTSemEventMultiSignal(pEvent->hEvtMulSem);
586 if (RT_FAILURE(rc2))
587 pEvent->pPayload = NULL; /* (no race condition if consumer also enters the critical section) */
588
589 LogFlowFuncLeaveRC(rc2);
590 return rc2;
591}
592
593/**
594 * Signals an event.
595 *
596 * @returns VBox status code.
597 * @param pEvent Event to signal.
598 * @param pPayload Event payload to associate. Takes ownership on
599 * success. Optional.
600 */
601int ShClEventSignal(PSHCLEVENT pEvent, PSHCLEVENTPAYLOAD pPayload)
602{
603 return ShClEventSignalEx(pEvent, VINF_SUCCESS, pPayload);
604}
605
606int ShClUtf16LenUtf8(PCRTUTF16 pcwszSrc, size_t cwcSrc, size_t *pchLen)
607{
608 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
609 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
610
611 size_t chLen = 0;
612 int rc = RTUtf16CalcUtf8LenEx(pcwszSrc, cwcSrc, &chLen);
613 if (RT_SUCCESS(rc))
614 *pchLen = chLen;
615 return rc;
616}
617
618int ShClConvUtf16CRLFToUtf8LF(PCRTUTF16 pcwszSrc, size_t cwcSrc,
619 char *pszBuf, size_t cbBuf, size_t *pcbLen)
620{
621 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
622 AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
623 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
624 AssertPtrReturn(pcbLen, VERR_INVALID_POINTER);
625
626 int rc;
627
628 PRTUTF16 pwszTmp = NULL;
629 size_t cchTmp = 0;
630
631 size_t cbLen = 0;
632
633 /* How long will the converted text be? */
634 rc = ShClUtf16CRLFLenUtf8(pcwszSrc, cwcSrc, &cchTmp);
635 if (RT_SUCCESS(rc))
636 {
637 cchTmp++; /* Add space for terminator. */
638
639 pwszTmp = (PRTUTF16)RTMemAlloc(cchTmp * sizeof(RTUTF16));
640 if (pwszTmp)
641 {
642 rc = ShClConvUtf16CRLFToLF(pcwszSrc, cwcSrc, pwszTmp, cchTmp);
643 if (RT_SUCCESS(rc))
644 rc = RTUtf16ToUtf8Ex(pwszTmp + 1, cchTmp - 1, &pszBuf, cbBuf, &cbLen);
645
646 RTMemFree(reinterpret_cast<void *>(pwszTmp));
647 }
648 else
649 rc = VERR_NO_MEMORY;
650 }
651
652 if (RT_SUCCESS(rc))
653 {
654 *pcbLen = cbLen;
655 }
656
657 return rc;
658}
659
660int ShClConvUtf16LFToCRLFA(PCRTUTF16 pcwszSrc, size_t cwcSrc,
661 PRTUTF16 *ppwszDst, size_t *pcwDst)
662{
663 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
664 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
665 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
666
667 PRTUTF16 pwszDst = NULL;
668 size_t cchDst;
669
670 int rc = ShClUtf16LFLenUtf8(pcwszSrc, cwcSrc, &cchDst);
671 if (RT_SUCCESS(rc))
672 {
673 pwszDst = (PRTUTF16)RTMemAlloc((cchDst + 1 /* Leave space for terminator */) * sizeof(RTUTF16));
674 if (pwszDst)
675 {
676 rc = ShClConvUtf16LFToCRLF(pcwszSrc, cwcSrc, pwszDst, cchDst + 1 /* Include terminator */);
677 }
678 else
679 rc = VERR_NO_MEMORY;
680 }
681
682 if (RT_SUCCESS(rc))
683 {
684 *ppwszDst = pwszDst;
685 *pcwDst = cchDst;
686 }
687 else
688 RTMemFree(pwszDst);
689
690 LogFlowFuncLeaveRC(rc);
691 return rc;
692}
693
694int ShClConvUtf8LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
695 PRTUTF16 *ppwszDst, size_t *pcwDst)
696{
697 AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
698 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
699 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
700 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
701
702 /* Intermediate conversion to UTF-16. */
703 size_t cwcTmp;
704 PRTUTF16 pwcTmp = NULL;
705 int rc = RTStrToUtf16Ex(pcszSrc, cbSrc, &pwcTmp, 0, &cwcTmp);
706 if (RT_SUCCESS(rc))
707 {
708 rc = ShClConvUtf16LFToCRLFA(pwcTmp, cwcTmp, ppwszDst, pcwDst);
709 RTUtf16Free(pwcTmp);
710 }
711
712 return rc;
713}
714
715/**
716 * Converts a Latin-1 string with LF line endings into an UTF-16 string with CRLF endings.
717 *
718 * @returns VBox status code.
719 * @param pcszSrc Latin-1 string to convert.
720 * @param cbSrc Size (in bytes) of Latin-1 string to convert.
721 * @param ppwszDst Where to return the converted UTF-16 string on success.
722 * @param pcwDst Where to return the length (in UTF-16 characters) on success.
723 *
724 * @note Only converts the source until the string terminator is found (or length limit is hit).
725 */
726int ShClConvLatin1LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
727 PRTUTF16 *ppwszDst, size_t *pcwDst)
728{
729 AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
730 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
731 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
732 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
733
734 size_t chSrc = 0;
735
736 PRTUTF16 pwszDst = NULL;
737
738 /* Calculate the space needed. */
739 size_t cwDst = 0;
740 for (size_t i = 0; i < cbSrc && pcszSrc[i] != '\0'; ++i)
741 {
742 if (pcszSrc[i] == VBOX_SHCL_LINEFEED)
743 cwDst += 2; /* Space for VBOX_SHCL_CARRIAGERETURN + VBOX_SHCL_LINEFEED. */
744 else
745 ++cwDst;
746 chSrc++;
747 }
748
749 pwszDst = (PRTUTF16)RTMemAlloc((cwDst + 1 /* Leave space for the terminator */) * sizeof(RTUTF16));
750 AssertPtrReturn(pwszDst, VERR_NO_MEMORY);
751
752 /* Do the conversion, bearing in mind that Latin-1 expands "naturally" to UTF-16. */
753 for (size_t i = 0, j = 0; i < chSrc; ++i, ++j)
754 {
755 AssertMsg(j <= cwDst, ("cbSrc=%zu, j=%u vs. cwDst=%u\n", cbSrc, j, cwDst));
756 if (pcszSrc[i] != VBOX_SHCL_LINEFEED)
757 pwszDst[j] = pcszSrc[i];
758 else
759 {
760 pwszDst[j] = VBOX_SHCL_CARRIAGERETURN;
761 pwszDst[j + 1] = VBOX_SHCL_LINEFEED;
762 ++j;
763 }
764 }
765
766 pwszDst[cwDst] = '\0'; /* Make sure we are zero-terminated. */
767
768 *ppwszDst = pwszDst;
769 *pcwDst = cwDst;
770
771 return VINF_SUCCESS;
772}
773
774int ShClConvUtf16ToUtf8HTML(PCRTUTF16 pcwszSrc, size_t cwcSrc, char **ppszDst, size_t *pcbDst)
775{
776 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
777 AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
778 AssertPtrReturn(ppszDst, VERR_INVALID_POINTER);
779 AssertPtrReturn(pcbDst, VERR_INVALID_POINTER);
780
781 int rc = VINF_SUCCESS;
782
783 size_t cwTmp = cwcSrc;
784 PCRTUTF16 pwTmp = pcwszSrc;
785
786 char *pchDst = NULL;
787 size_t cbDst = 0;
788
789 size_t i = 0;
790 while (i < cwTmp)
791 {
792 /* Find zero symbol (end of string). */
793 for (; i < cwTmp && pcwszSrc[i] != 0; i++)
794 ;
795
796 /* Convert found string. */
797 char *psz = NULL;
798 size_t cch = 0;
799 rc = RTUtf16ToUtf8Ex(pwTmp, cwTmp, &psz, pwTmp - pcwszSrc, &cch);
800 if (RT_FAILURE(rc))
801 break;
802
803 /* Append new substring. */
804 char *pchNew = (char *)RTMemRealloc(pchDst, cbDst + cch + 1);
805 if (!pchNew)
806 {
807 RTStrFree(psz);
808 rc = VERR_NO_MEMORY;
809 break;
810 }
811
812 pchDst = pchNew;
813 memcpy(pchDst + cbDst, psz, cch + 1);
814
815 RTStrFree(psz);
816
817 cbDst += cch + 1;
818
819 /* Skip zero symbols. */
820 for (; i < cwTmp && pcwszSrc[i] == 0; i++)
821 ;
822
823 /* Remember start of string. */
824 pwTmp += i;
825 }
826
827 if (RT_SUCCESS(rc))
828 {
829 *ppszDst = pchDst;
830 *pcbDst = cbDst;
831
832 return VINF_SUCCESS;
833 }
834
835 RTMemFree(pchDst);
836
837 return rc;
838}
839
840int ShClUtf16LFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
841{
842 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
843 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
844
845 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
846 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
847
848 size_t cLen = 0;
849
850 /* Don't copy the endian marker. */
851 size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
852
853 /* Calculate the size of the destination text string. */
854 /* Is this Utf16 or Utf16-LE? */
855 for (; i < cwSrc; ++i, ++cLen)
856 {
857 /* Check for a single line feed */
858 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
859 ++cLen;
860#ifdef RT_OS_DARWIN
861 /* Check for a single carriage return (MacOS) */
862 if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
863 ++cLen;
864#endif
865 if (pcwszSrc[i] == 0)
866 {
867 /* Don't count this, as we do so below. */
868 break;
869 }
870 }
871
872 *pchLen = cLen;
873
874 return VINF_SUCCESS;
875}
876
877int ShClUtf16CRLFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
878{
879 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
880 AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
881 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
882
883 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
884 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
885
886 size_t cLen = 0;
887
888 /* Calculate the size of the destination text string. */
889 /* Is this Utf16 or Utf16-LE? */
890 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
891 cLen = 0;
892 else
893 cLen = 1;
894
895 for (size_t i = 0; i < cwSrc; ++i, ++cLen)
896 {
897 if ( (i + 1 < cwSrc)
898 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
899 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
900 {
901 ++i;
902 }
903 if (pcwszSrc[i] == 0)
904 break;
905 }
906
907 *pchLen = cLen;
908
909 return VINF_SUCCESS;
910}
911
912int ShClConvUtf16LFToCRLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
913{
914 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
915 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
916 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
917
918 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
919 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
920
921 int rc = VINF_SUCCESS;
922
923 /* Don't copy the endian marker. */
924 size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
925 size_t j = 0;
926
927 for (; i < cwcSrc; ++i, ++j)
928 {
929 /* Don't copy the null byte, as we add it below. */
930 if (pcwszSrc[i] == 0)
931 break;
932
933 /* Not enough space in destination? */
934 if (j == cwDst)
935 {
936 rc = VERR_BUFFER_OVERFLOW;
937 break;
938 }
939
940 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
941 {
942 /* Insert '\r' in front of '\n', but avoid '\r\r\n' situations
943 because it will result in extra empty lines on the other side. */
944 if ( i > 1
945 && pcwszSrc[i - 1] != VBOX_SHCL_CARRIAGERETURN)
946 {
947 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
948 ++j;
949 }
950
951 /* Not enough space in destination? */
952 if (j == cwDst)
953 {
954 rc = VERR_BUFFER_OVERFLOW;
955 break;
956 }
957 }
958#ifdef RT_OS_DARWIN
959 /* Check for a single carriage return (MacOS) */
960 else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
961 {
962 /* Set CR.r, but avoid '\r\r'. */
963 if ( i > 1
964 && pcwszSrc[i - 1] != VBOX_SHCL_CARRIAGERETURN)
965 {
966 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
967 ++j;
968 }
969
970 /* Not enough space in destination? */
971 if (j == cwDst)
972 {
973 rc = VERR_BUFFER_OVERFLOW;
974 break;
975 }
976
977 /* Add line feed. */
978 pu16Dst[j] = VBOX_SHCL_LINEFEED;
979 continue;
980 }
981#endif
982 pu16Dst[j] = pcwszSrc[i];
983 }
984
985 if (j == cwDst)
986 rc = VERR_BUFFER_OVERFLOW;
987
988 if (RT_SUCCESS(rc))
989 {
990 /* Add terminator. */
991 pu16Dst[j] = 0;
992 }
993
994 return rc;
995}
996
997int ShClConvUtf16CRLFToLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
998{
999 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
1000 AssertReturn(cwcSrc, VERR_INVALID_PARAMETER);
1001 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
1002 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
1003
1004 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
1005 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
1006
1007 /* Prepend the Utf16 byte order marker if it is missing. */
1008 size_t cwDstPos;
1009 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
1010 {
1011 cwDstPos = 0;
1012 }
1013 else
1014 {
1015 pu16Dst[0] = VBOX_SHCL_UTF16LEMARKER;
1016 cwDstPos = 1;
1017 }
1018
1019 for (size_t i = 0; i < cwcSrc; ++i, ++cwDstPos)
1020 {
1021 if (pcwszSrc[i] == 0)
1022 break;
1023
1024 if (cwDstPos == cwDst)
1025 return VERR_BUFFER_OVERFLOW;
1026
1027 if ( (i + 1 < cwcSrc)
1028 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
1029 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
1030 {
1031 ++i;
1032 }
1033
1034 pu16Dst[cwDstPos] = pcwszSrc[i];
1035 }
1036
1037 if (cwDstPos == cwDst)
1038 return VERR_BUFFER_OVERFLOW;
1039
1040 /* Add terminating zero. */
1041 pu16Dst[cwDstPos] = 0;
1042
1043 return VINF_SUCCESS;
1044}
1045
1046int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
1047{
1048 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
1049 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
1050 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
1051 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
1052
1053 PBMPWIN3XINFOHDR coreHdr = (PBMPWIN3XINFOHDR)pvSrc;
1054 /** @todo Support all the many versions of the DIB headers. */
1055 if ( cbSrc < sizeof(BMPWIN3XINFOHDR)
1056 || RT_LE2H_U32(coreHdr->cbSize) < sizeof(BMPWIN3XINFOHDR)
1057 || RT_LE2H_U32(coreHdr->cbSize) != sizeof(BMPWIN3XINFOHDR))
1058 {
1059 return VERR_INVALID_PARAMETER;
1060 }
1061
1062 size_t offPixel = sizeof(BMPFILEHDR)
1063 + RT_LE2H_U32(coreHdr->cbSize)
1064 + RT_LE2H_U32(coreHdr->cClrUsed) * sizeof(uint32_t);
1065 if (cbSrc < offPixel)
1066 return VERR_INVALID_PARAMETER;
1067
1068 size_t cbDst = sizeof(BMPFILEHDR) + cbSrc;
1069
1070 void *pvDest = RTMemAlloc(cbDst);
1071 if (!pvDest)
1072 return VERR_NO_MEMORY;
1073
1074 PBMPFILEHDR fileHdr = (PBMPFILEHDR)pvDest;
1075
1076 fileHdr->uType = BMP_HDR_MAGIC;
1077 fileHdr->cbFileSize = (uint32_t)RT_H2LE_U32(cbDst);
1078 fileHdr->Reserved1 = 0;
1079 fileHdr->Reserved2 = 0;
1080 fileHdr->offBits = (uint32_t)RT_H2LE_U32(offPixel);
1081
1082 memcpy((uint8_t *)pvDest + sizeof(BMPFILEHDR), pvSrc, cbSrc);
1083
1084 *ppvDest = pvDest;
1085 *pcbDest = cbDst;
1086
1087 return VINF_SUCCESS;
1088}
1089
1090int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
1091{
1092 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
1093 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
1094 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
1095 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
1096
1097 PBMPFILEHDR pBmpHdr = (PBMPFILEHDR)pvSrc;
1098 if ( cbSrc < sizeof(BMPFILEHDR)
1099 || pBmpHdr->uType != BMP_HDR_MAGIC
1100 || RT_LE2H_U32(pBmpHdr->cbFileSize) != cbSrc)
1101 {
1102 return VERR_INVALID_PARAMETER;
1103 }
1104
1105 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMPFILEHDR);
1106 *pcbDest = cbSrc - sizeof(BMPFILEHDR);
1107
1108 return VINF_SUCCESS;
1109}
1110
1111#ifdef LOG_ENABLED
1112
1113int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
1114{
1115 int rc = VINF_SUCCESS;
1116 char *pszBuf = (char *)RTMemTmpAllocZ(cbSrc + 1);
1117 if (pszBuf)
1118 {
1119 memcpy(pszBuf, pcszSrc, cbSrc);
1120 pszBuf[cbSrc] = '\0';
1121 for (size_t off = 0; off < cbSrc; ++off)
1122 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
1123 pszBuf[off] = ' ';
1124 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
1125 RTMemTmpFree(pszBuf);
1126 }
1127 else
1128 rc = VERR_NO_MEMORY;
1129 return rc;
1130}
1131
1132void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
1133{
1134 if (LogIsEnabled())
1135 {
1136 if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
1137 {
1138 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
1139 if (pv && cb)
1140 LogFunc(("%ls\n", pv));
1141 else
1142 LogFunc(("%p %zu\n", pv, cb));
1143 }
1144 else if (uFormat & VBOX_SHCL_FMT_BITMAP)
1145 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
1146 else if (uFormat & VBOX_SHCL_FMT_HTML)
1147 {
1148 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
1149 if (pv && cb)
1150 {
1151 LogFunc(("%s\n", pv));
1152 ShClDbgDumpHtml((const char *)pv, cb);
1153 }
1154 else
1155 LogFunc(("%p %zu\n", pv, cb));
1156 }
1157 else
1158 LogFunc(("Invalid format %02X\n", uFormat));
1159 }
1160}
1161
1162#endif /* LOG_ENABLED */
1163
1164/**
1165 * Translates a Shared Clipboard host function number to a string.
1166 *
1167 * @returns Function ID string name.
1168 * @param uFn The function to translate.
1169 */
1170const char *ShClHostFunctionToStr(uint32_t uFn)
1171{
1172 switch (uFn)
1173 {
1174 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
1175 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
1176 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
1177 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
1178 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
1179 }
1180 return "Unknown";
1181}
1182
1183/**
1184 * Translates a Shared Clipboard host message enum to a string.
1185 *
1186 * @returns Message ID string name.
1187 * @param uMsg The message to translate.
1188 */
1189const char *ShClHostMsgToStr(uint32_t uMsg)
1190{
1191 switch (uMsg)
1192 {
1193 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
1194 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
1195 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
1196 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_CANCELED);
1197 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA_CID);
1198 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
1199 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
1200 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
1201 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
1202 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
1203 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
1204 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
1205 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
1206 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
1207 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
1208 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
1209 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
1210 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
1211 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
1212 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
1213 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
1214 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
1215 }
1216 return "Unknown";
1217}
1218
1219/**
1220 * Translates a Shared Clipboard guest message enum to a string.
1221 *
1222 * @returns Message ID string name.
1223 * @param uMsg The message to translate.
1224 */
1225const char *ShClGuestMsgToStr(uint32_t uMsg)
1226{
1227 switch (uMsg)
1228 {
1229 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
1230 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
1231 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
1232 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
1233 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
1234 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
1235 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
1236 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
1237 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
1238 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
1239 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
1240 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
1241 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
1242 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
1243 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
1244 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
1245 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
1246 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
1247 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
1248 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
1249 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
1250 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
1251 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
1252 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
1253 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
1254 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
1255 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
1256 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE);
1257 }
1258 return "Unknown";
1259}
1260
1261/**
1262 * Converts Shared Clipboard formats to a string.
1263 *
1264 * @returns Stringified Shared Clipboard formats, or NULL on failure. Must be free'd with RTStrFree().
1265 * @param fFormats Shared Clipboard formats to convert.
1266 *
1267 */
1268char *ShClFormatsToStrA(SHCLFORMATS fFormats)
1269{
1270#define APPEND_FMT_TO_STR(_aFmt) \
1271 if (fFormats & VBOX_SHCL_FMT_##_aFmt) \
1272 { \
1273 if (pszFmts) \
1274 { \
1275 rc2 = RTStrAAppend(&pszFmts, ", "); \
1276 if (RT_FAILURE(rc2)) \
1277 break; \
1278 } \
1279 \
1280 rc2 = RTStrAAppend(&pszFmts, #_aFmt); \
1281 if (RT_FAILURE(rc2)) \
1282 break; \
1283 }
1284
1285 char *pszFmts = NULL;
1286 int rc2 = VINF_SUCCESS;
1287
1288 do
1289 {
1290 APPEND_FMT_TO_STR(UNICODETEXT);
1291 APPEND_FMT_TO_STR(BITMAP);
1292 APPEND_FMT_TO_STR(HTML);
1293# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1294 APPEND_FMT_TO_STR(URI_LIST);
1295# endif
1296
1297 } while (0);
1298
1299 if (!pszFmts)
1300 rc2 = RTStrAAppend(&pszFmts, "NONE");
1301
1302 if ( RT_FAILURE(rc2)
1303 && pszFmts)
1304 {
1305 RTStrFree(pszFmts);
1306 pszFmts = NULL;
1307 }
1308
1309#undef APPEND_FMT_TO_STR
1310
1311 return pszFmts;
1312}
1313
1314
1315/*********************************************************************************************************************************
1316* Shared Clipboard Cache *
1317*********************************************************************************************************************************/
1318
1319/**
1320 * Returns (mutable) data of a cache entry.
1321 *
1322 * @param pCacheEntry Cache entry to return data for.
1323 * @param pvData Where to return the (mutable) data pointer.
1324 * @param pcbData Where to return the data size.
1325 */
1326void ShClCacheEntryGet(PSHCLCACHEENTRY pCacheEntry, void **pvData, size_t *pcbData)
1327{
1328 AssertPtrReturnVoid(pCacheEntry);
1329 AssertPtrReturnVoid(pvData);
1330 AssertReturnVoid(pcbData);
1331
1332 *pvData = pCacheEntry->pvData;
1333 *pcbData = pCacheEntry->cbData;
1334}
1335
1336/**
1337 * Initializes a cache entry.
1338 *
1339 * @returns VBox status code.
1340 * @param pCacheEntry Cache entry to init.
1341 * @param pvData Data to copy to entry. Can be NULL to initialize an emptry entry.
1342 * @param cbData Size (in bytes) of \a pvData to copy to entry. Must be 0 if \a pvData is NULL.
1343 */
1344static int shClCacheEntryInit(PSHCLCACHEENTRY pCacheEntry, const void *pvData, size_t cbData)
1345{
1346 AssertReturn(RT_VALID_PTR(pvData) || cbData == 0, VERR_INVALID_PARAMETER);
1347
1348 RT_BZERO(pCacheEntry, sizeof(SHCLCACHEENTRY));
1349
1350 if (pvData)
1351 {
1352 pCacheEntry->pvData = RTMemDup(pvData, cbData);
1353 AssertPtrReturn(pCacheEntry->pvData, VERR_NO_MEMORY);
1354 pCacheEntry->cbData = cbData;
1355 }
1356
1357 return VINF_SUCCESS;
1358}
1359
1360/**
1361 * Returns whether a cache entry is valid (cache hit) or not.
1362 *
1363 * @returns \c true if valid, or \c false if not.
1364 * @param pCacheEntry Cache entry to check for.
1365 */
1366DECLINLINE(bool) shClCacheEntryIsValid(PSHCLCACHEENTRY pCacheEntry)
1367{
1368 return pCacheEntry->pvData != NULL;
1369}
1370
1371/**
1372 * Destroys a cache entry.
1373 *
1374 * @param pCacheEntry Cache entry to destroy.
1375 */
1376DECLINLINE(void) shClCacheEntryDestroy(PSHCLCACHEENTRY pCacheEntry)
1377{
1378 if (pCacheEntry->pvData)
1379 {
1380 Assert(pCacheEntry->cbData);
1381 RTMemFree(pCacheEntry->pvData);
1382 pCacheEntry->pvData = NULL;
1383 pCacheEntry->cbData = 0;
1384 }
1385}
1386
1387/**
1388 * Initializes a cache.
1389 *
1390 * @param pCache Cache to init.
1391 */
1392void ShClCacheInit(PSHCLCACHE pCache)
1393{
1394 AssertPtrReturnVoid(pCache);
1395
1396 RT_BZERO(pCache, sizeof(SHCLCACHE));
1397
1398 for (size_t i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
1399 shClCacheEntryInit(&pCache->aEntries[i], NULL, 0);
1400}
1401
1402/**
1403 * Destroys all entries of a cache.
1404 *
1405 * @param pCache Cache to destroy entries for.
1406 */
1407DECLINLINE(void) shClCacheDestroyEntries(PSHCLCACHE pCache)
1408{
1409 for (size_t i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
1410 shClCacheEntryDestroy(&pCache->aEntries[i]);
1411}
1412
1413/**
1414 * Destroys a cache.
1415 *
1416 * @param pCache Cache to destroy.
1417 */
1418void ShClCacheDestroy(PSHCLCACHE pCache)
1419{
1420 AssertPtrReturnVoid(pCache);
1421
1422 shClCacheDestroyEntries(pCache);
1423
1424 RT_BZERO(pCache, sizeof(SHCLCACHE));
1425}
1426
1427/**
1428 * Invalidates a cache.
1429 *
1430 * @param pCache Cache to invalidate.
1431 */
1432void ShClCacheInvalidate(PSHCLCACHE pCache)
1433{
1434 AssertPtrReturnVoid(pCache);
1435
1436 shClCacheDestroyEntries(pCache);
1437}
1438
1439/**
1440 * Invalidates a specific cache entry.
1441 *
1442 * @param pCache Cache to invalidate.
1443 * @param uFmt Format to invalidate entry for.
1444 */
1445void ShClCacheInvalidateEntry(PSHCLCACHE pCache, SHCLFORMAT uFmt)
1446{
1447 AssertPtrReturnVoid(pCache);
1448 AssertReturnVoid(uFmt < VBOX_SHCL_FMT_MAX);
1449 shClCacheEntryDestroy(&pCache->aEntries[uFmt]);
1450}
1451
1452/**
1453 * Gets an entry for a Shared Clipboard format.
1454 *
1455 * @returns Pointer to entry if cached, or NULL if not in cache (cache miss).
1456 * @param pCache Cache to get entry for.
1457 * @param uFmt Format to get entry for.
1458 */
1459PSHCLCACHEENTRY ShClCacheGet(PSHCLCACHE pCache, SHCLFORMAT uFmt)
1460{
1461 AssertReturn(uFmt < VBOX_SHCL_FMT_MAX, NULL);
1462 return shClCacheEntryIsValid(&pCache->aEntries[uFmt]) ? &pCache->aEntries[uFmt] : NULL;
1463}
1464
1465/**
1466 * Sets data to cache for a specific clipboard format, internal version.
1467 *
1468 * @returns VBox status code.
1469 * @param pCache Cache to set data for.
1470 * @param uFmt Clipboard format to set data for.
1471 * @param pvData Data to set.
1472 * @param cbData Size (in bytes) of data to set.
1473 */
1474DECLINLINE(int) shClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData)
1475{
1476 AssertReturn(uFmt < VBOX_SHCL_FMT_MAX, VERR_INVALID_PARAMETER);
1477 AssertReturn(shClCacheEntryIsValid(&pCache->aEntries[uFmt]) == false, VERR_ALREADY_EXISTS);
1478
1479 return shClCacheEntryInit(&pCache->aEntries[uFmt], pvData, cbData);
1480}
1481
1482/**
1483 * Sets data to cache for a specific clipboard format.
1484 *
1485 * @returns VBox status code.
1486 * @param pCache Cache to set data for.
1487 * @param uFmt Clipboard format to set data for.
1488 * @param pvData Data to set.
1489 * @param cbData Size (in bytes) of data to set.
1490 */
1491int ShClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData)
1492{
1493 AssertPtrReturn(pCache, VERR_INVALID_POINTER);
1494
1495 if (!pvData) /* Nothing to cache? */
1496 return VINF_SUCCESS;
1497
1498 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1499
1500 return shClCacheSet(pCache, uFmt, pvData, cbData);
1501}
1502
1503/**
1504 * Sets data to cache for multiple clipboard formats.
1505 *
1506 * Will bail out if a given format cannot be handled with the data given.
1507 *
1508 * @returns VBox status code.
1509 * @param pCache Cache to set data for.
1510 * @param uFmt Clipboard format to set data for.
1511 * @param pvData Data to set.
1512 * @param cbData Size (in bytes) of data to set.
1513 */
1514int ShClCacheSetMultiple(PSHCLCACHE pCache, SHCLFORMATS uFmts, const void *pvData, size_t cbData)
1515{
1516 AssertPtrReturn(pCache, VERR_INVALID_POINTER);
1517
1518 if (!pvData) /* Nothing to cache? */
1519 return VINF_SUCCESS;
1520
1521 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1522
1523 int rc = VINF_SUCCESS;
1524
1525 SHCLFORMATS uFmtsLeft = uFmts;
1526 while (uFmtsLeft)
1527 {
1528 SHCLFORMAT uFmt = VBOX_SHCL_FMT_NONE;
1529 void *pvConv = NULL;
1530 size_t cbConv = 0;
1531 if (uFmtsLeft & VBOX_SHCL_FMT_UNICODETEXT)
1532 {
1533 uFmt = VBOX_SHCL_FMT_UNICODETEXT;
1534
1535 rc = RTStrValidateEncoding((const char *)pvData);
1536 if (RT_SUCCESS(rc))
1537 {
1538 rc = RTStrToUtf16((const char *)pvData, (PRTUTF16 *)&pvConv);
1539 if (RT_SUCCESS(rc))
1540 cbConv = (RTUtf16Len((const PRTUTF16)pvConv) + 1) * sizeof(RTUTF16);
1541 }
1542 else if (!RTUtf16ValidateEncoding((const PRTUTF16)pvData))
1543 {
1544 AssertFailedBreakStmt(rc = VERR_INVALID_PARAMETER);
1545 }
1546 }
1547 else if (uFmtsLeft & VBOX_SHCL_FMT_BITMAP)
1548 uFmt = VBOX_SHCL_FMT_BITMAP;
1549 else if (uFmtsLeft & VBOX_SHCL_FMT_HTML)
1550 uFmt = VBOX_SHCL_FMT_HTML;
1551 else if (uFmtsLeft & VBOX_SHCL_FMT_URI_LIST)
1552 uFmt = VBOX_SHCL_FMT_URI_LIST;
1553 else
1554 AssertFailedBreakStmt(rc = VERR_NOT_SUPPORTED);
1555
1556 uFmtsLeft &= ~uFmt; /* Remove from list. */
1557 Assert(RT_VALID_PTR(pvConv) || cbConv == 0); /* Sanity. */
1558
1559 if (RT_SUCCESS(rc))
1560 rc = shClCacheSet(pCache, uFmt,
1561 pvConv ? pvConv : pvData,
1562 cbConv ? cbConv : cbData);
1563 RTMemFree(pvConv);
1564 AssertRCBreak(rc);
1565 }
1566
1567 return rc;
1568}
1569
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