VirtualBox

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

Last change on this file since 103795 was 102815, checked in by vboxsync, 10 months ago

Shared Clipboard: Added ShClCacheSetMultiple(). bugref:9437

  • 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.5 KB
Line 
1/* $Id: clipboard-common.cpp 102815 2024-01-10 13:44: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 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
943 ++j;
944
945 /* Not enough space in destination? */
946 if (j == cwDst)
947 {
948 rc = VERR_BUFFER_OVERFLOW;
949 break;
950 }
951 }
952#ifdef RT_OS_DARWIN
953 /* Check for a single carriage return (MacOS) */
954 else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
955 {
956 /* Set CR.r */
957 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
958 ++j;
959
960 /* Not enough space in destination? */
961 if (j == cwDst)
962 {
963 rc = VERR_BUFFER_OVERFLOW;
964 break;
965 }
966
967 /* Add line feed. */
968 pu16Dst[j] = VBOX_SHCL_LINEFEED;
969 continue;
970 }
971#endif
972 pu16Dst[j] = pcwszSrc[i];
973 }
974
975 if (j == cwDst)
976 rc = VERR_BUFFER_OVERFLOW;
977
978 if (RT_SUCCESS(rc))
979 {
980 /* Add terminator. */
981 pu16Dst[j] = 0;
982 }
983
984 return rc;
985}
986
987int ShClConvUtf16CRLFToLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
988{
989 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
990 AssertReturn(cwcSrc, VERR_INVALID_PARAMETER);
991 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
992 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
993
994 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
995 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
996
997 /* Prepend the Utf16 byte order marker if it is missing. */
998 size_t cwDstPos;
999 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
1000 {
1001 cwDstPos = 0;
1002 }
1003 else
1004 {
1005 pu16Dst[0] = VBOX_SHCL_UTF16LEMARKER;
1006 cwDstPos = 1;
1007 }
1008
1009 for (size_t i = 0; i < cwcSrc; ++i, ++cwDstPos)
1010 {
1011 if (pcwszSrc[i] == 0)
1012 break;
1013
1014 if (cwDstPos == cwDst)
1015 return VERR_BUFFER_OVERFLOW;
1016
1017 if ( (i + 1 < cwcSrc)
1018 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
1019 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
1020 {
1021 ++i;
1022 }
1023
1024 pu16Dst[cwDstPos] = pcwszSrc[i];
1025 }
1026
1027 if (cwDstPos == cwDst)
1028 return VERR_BUFFER_OVERFLOW;
1029
1030 /* Add terminating zero. */
1031 pu16Dst[cwDstPos] = 0;
1032
1033 return VINF_SUCCESS;
1034}
1035
1036int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
1037{
1038 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
1039 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
1040 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
1041 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
1042
1043 PBMPWIN3XINFOHDR coreHdr = (PBMPWIN3XINFOHDR)pvSrc;
1044 /** @todo Support all the many versions of the DIB headers. */
1045 if ( cbSrc < sizeof(BMPWIN3XINFOHDR)
1046 || RT_LE2H_U32(coreHdr->cbSize) < sizeof(BMPWIN3XINFOHDR)
1047 || RT_LE2H_U32(coreHdr->cbSize) != sizeof(BMPWIN3XINFOHDR))
1048 {
1049 return VERR_INVALID_PARAMETER;
1050 }
1051
1052 size_t offPixel = sizeof(BMPFILEHDR)
1053 + RT_LE2H_U32(coreHdr->cbSize)
1054 + RT_LE2H_U32(coreHdr->cClrUsed) * sizeof(uint32_t);
1055 if (cbSrc < offPixel)
1056 return VERR_INVALID_PARAMETER;
1057
1058 size_t cbDst = sizeof(BMPFILEHDR) + cbSrc;
1059
1060 void *pvDest = RTMemAlloc(cbDst);
1061 if (!pvDest)
1062 return VERR_NO_MEMORY;
1063
1064 PBMPFILEHDR fileHdr = (PBMPFILEHDR)pvDest;
1065
1066 fileHdr->uType = BMP_HDR_MAGIC;
1067 fileHdr->cbFileSize = (uint32_t)RT_H2LE_U32(cbDst);
1068 fileHdr->Reserved1 = 0;
1069 fileHdr->Reserved2 = 0;
1070 fileHdr->offBits = (uint32_t)RT_H2LE_U32(offPixel);
1071
1072 memcpy((uint8_t *)pvDest + sizeof(BMPFILEHDR), pvSrc, cbSrc);
1073
1074 *ppvDest = pvDest;
1075 *pcbDest = cbDst;
1076
1077 return VINF_SUCCESS;
1078}
1079
1080int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
1081{
1082 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
1083 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
1084 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
1085 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
1086
1087 PBMPFILEHDR pBmpHdr = (PBMPFILEHDR)pvSrc;
1088 if ( cbSrc < sizeof(BMPFILEHDR)
1089 || pBmpHdr->uType != BMP_HDR_MAGIC
1090 || RT_LE2H_U32(pBmpHdr->cbFileSize) != cbSrc)
1091 {
1092 return VERR_INVALID_PARAMETER;
1093 }
1094
1095 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMPFILEHDR);
1096 *pcbDest = cbSrc - sizeof(BMPFILEHDR);
1097
1098 return VINF_SUCCESS;
1099}
1100
1101#ifdef LOG_ENABLED
1102
1103int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
1104{
1105 int rc = VINF_SUCCESS;
1106 char *pszBuf = (char *)RTMemTmpAllocZ(cbSrc + 1);
1107 if (pszBuf)
1108 {
1109 memcpy(pszBuf, pcszSrc, cbSrc);
1110 pszBuf[cbSrc] = '\0';
1111 for (size_t off = 0; off < cbSrc; ++off)
1112 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
1113 pszBuf[off] = ' ';
1114 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
1115 RTMemTmpFree(pszBuf);
1116 }
1117 else
1118 rc = VERR_NO_MEMORY;
1119 return rc;
1120}
1121
1122void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
1123{
1124 if (LogIsEnabled())
1125 {
1126 if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
1127 {
1128 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
1129 if (pv && cb)
1130 LogFunc(("%ls\n", pv));
1131 else
1132 LogFunc(("%p %zu\n", pv, cb));
1133 }
1134 else if (uFormat & VBOX_SHCL_FMT_BITMAP)
1135 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
1136 else if (uFormat & VBOX_SHCL_FMT_HTML)
1137 {
1138 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
1139 if (pv && cb)
1140 {
1141 LogFunc(("%s\n", pv));
1142 ShClDbgDumpHtml((const char *)pv, cb);
1143 }
1144 else
1145 LogFunc(("%p %zu\n", pv, cb));
1146 }
1147 else
1148 LogFunc(("Invalid format %02X\n", uFormat));
1149 }
1150}
1151
1152#endif /* LOG_ENABLED */
1153
1154/**
1155 * Translates a Shared Clipboard host function number to a string.
1156 *
1157 * @returns Function ID string name.
1158 * @param uFn The function to translate.
1159 */
1160const char *ShClHostFunctionToStr(uint32_t uFn)
1161{
1162 switch (uFn)
1163 {
1164 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
1165 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
1166 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
1167 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
1168 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
1169 }
1170 return "Unknown";
1171}
1172
1173/**
1174 * Translates a Shared Clipboard host message enum to a string.
1175 *
1176 * @returns Message ID string name.
1177 * @param uMsg The message to translate.
1178 */
1179const char *ShClHostMsgToStr(uint32_t uMsg)
1180{
1181 switch (uMsg)
1182 {
1183 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
1184 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
1185 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
1186 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_CANCELED);
1187 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA_CID);
1188 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
1189 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
1190 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
1191 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
1192 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
1193 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
1194 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
1195 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
1196 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
1197 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
1198 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
1199 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
1200 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
1201 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
1202 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
1203 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
1204 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
1205 }
1206 return "Unknown";
1207}
1208
1209/**
1210 * Translates a Shared Clipboard guest message enum to a string.
1211 *
1212 * @returns Message ID string name.
1213 * @param uMsg The message to translate.
1214 */
1215const char *ShClGuestMsgToStr(uint32_t uMsg)
1216{
1217 switch (uMsg)
1218 {
1219 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
1220 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
1221 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
1222 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
1223 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
1224 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
1225 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
1226 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
1227 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
1228 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
1229 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
1230 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
1231 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
1232 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
1233 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
1234 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
1235 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
1236 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
1237 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
1238 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
1239 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
1240 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
1241 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
1242 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
1243 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
1244 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
1245 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
1246 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE);
1247 }
1248 return "Unknown";
1249}
1250
1251/**
1252 * Converts Shared Clipboard formats to a string.
1253 *
1254 * @returns Stringified Shared Clipboard formats, or NULL on failure. Must be free'd with RTStrFree().
1255 * @param fFormats Shared Clipboard formats to convert.
1256 *
1257 */
1258char *ShClFormatsToStrA(SHCLFORMATS fFormats)
1259{
1260#define APPEND_FMT_TO_STR(_aFmt) \
1261 if (fFormats & VBOX_SHCL_FMT_##_aFmt) \
1262 { \
1263 if (pszFmts) \
1264 { \
1265 rc2 = RTStrAAppend(&pszFmts, ", "); \
1266 if (RT_FAILURE(rc2)) \
1267 break; \
1268 } \
1269 \
1270 rc2 = RTStrAAppend(&pszFmts, #_aFmt); \
1271 if (RT_FAILURE(rc2)) \
1272 break; \
1273 }
1274
1275 char *pszFmts = NULL;
1276 int rc2 = VINF_SUCCESS;
1277
1278 do
1279 {
1280 APPEND_FMT_TO_STR(UNICODETEXT);
1281 APPEND_FMT_TO_STR(BITMAP);
1282 APPEND_FMT_TO_STR(HTML);
1283# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1284 APPEND_FMT_TO_STR(URI_LIST);
1285# endif
1286
1287 } while (0);
1288
1289 if (!pszFmts)
1290 rc2 = RTStrAAppend(&pszFmts, "NONE");
1291
1292 if ( RT_FAILURE(rc2)
1293 && pszFmts)
1294 {
1295 RTStrFree(pszFmts);
1296 pszFmts = NULL;
1297 }
1298
1299#undef APPEND_FMT_TO_STR
1300
1301 return pszFmts;
1302}
1303
1304
1305/*********************************************************************************************************************************
1306* Shared Clipboard Cache *
1307*********************************************************************************************************************************/
1308
1309/**
1310 * Returns (mutable) data of a cache entry.
1311 *
1312 * @param pCacheEntry Cache entry to return data for.
1313 * @param pvData Where to return the (mutable) data pointer.
1314 * @param pcbData Where to return the data size.
1315 */
1316void ShClCacheEntryGet(PSHCLCACHEENTRY pCacheEntry, void **pvData, size_t *pcbData)
1317{
1318 AssertPtrReturnVoid(pCacheEntry);
1319 AssertPtrReturnVoid(pvData);
1320 AssertReturnVoid(pcbData);
1321
1322 *pvData = pCacheEntry->pvData;
1323 *pcbData = pCacheEntry->cbData;
1324}
1325
1326/**
1327 * Initializes a cache entry.
1328 *
1329 * @returns VBox status code.
1330 * @param pCacheEntry Cache entry to init.
1331 * @param pvData Data to copy to entry. Can be NULL to initialize an emptry entry.
1332 * @param cbData Size (in bytes) of \a pvData to copy to entry. Must be 0 if \a pvData is NULL.
1333 */
1334static int shClCacheEntryInit(PSHCLCACHEENTRY pCacheEntry, const void *pvData, size_t cbData)
1335{
1336 AssertReturn(RT_VALID_PTR(pvData) || cbData == 0, VERR_INVALID_PARAMETER);
1337
1338 RT_BZERO(pCacheEntry, sizeof(SHCLCACHEENTRY));
1339
1340 if (pvData)
1341 {
1342 pCacheEntry->pvData = RTMemDup(pvData, cbData);
1343 AssertPtrReturn(pCacheEntry->pvData, VERR_NO_MEMORY);
1344 pCacheEntry->cbData = cbData;
1345 }
1346
1347 return VINF_SUCCESS;
1348}
1349
1350/**
1351 * Returns whether a cache entry is valid (cache hit) or not.
1352 *
1353 * @returns \c true if valid, or \c false if not.
1354 * @param pCacheEntry Cache entry to check for.
1355 */
1356DECLINLINE(bool) shClCacheEntryIsValid(PSHCLCACHEENTRY pCacheEntry)
1357{
1358 return pCacheEntry->pvData != NULL;
1359}
1360
1361/**
1362 * Destroys a cache entry.
1363 *
1364 * @param pCacheEntry Cache entry to destroy.
1365 */
1366DECLINLINE(void) shClCacheEntryDestroy(PSHCLCACHEENTRY pCacheEntry)
1367{
1368 if (pCacheEntry->pvData)
1369 {
1370 Assert(pCacheEntry->cbData);
1371 RTMemFree(pCacheEntry->pvData);
1372 pCacheEntry->pvData = NULL;
1373 pCacheEntry->cbData = 0;
1374 }
1375}
1376
1377/**
1378 * Initializes a cache.
1379 *
1380 * @param pCache Cache to init.
1381 */
1382void ShClCacheInit(PSHCLCACHE pCache)
1383{
1384 AssertPtrReturnVoid(pCache);
1385
1386 RT_BZERO(pCache, sizeof(SHCLCACHE));
1387
1388 for (size_t i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
1389 shClCacheEntryInit(&pCache->aEntries[i], NULL, 0);
1390}
1391
1392/**
1393 * Destroys all entries of a cache.
1394 *
1395 * @param pCache Cache to destroy entries for.
1396 */
1397DECLINLINE(void) shClCacheDestroyEntries(PSHCLCACHE pCache)
1398{
1399 for (size_t i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
1400 shClCacheEntryDestroy(&pCache->aEntries[i]);
1401}
1402
1403/**
1404 * Destroys a cache.
1405 *
1406 * @param pCache Cache to destroy.
1407 */
1408void ShClCacheDestroy(PSHCLCACHE pCache)
1409{
1410 AssertPtrReturnVoid(pCache);
1411
1412 shClCacheDestroyEntries(pCache);
1413
1414 RT_BZERO(pCache, sizeof(SHCLCACHE));
1415}
1416
1417/**
1418 * Invalidates a cache.
1419 *
1420 * @param pCache Cache to invalidate.
1421 */
1422void ShClCacheInvalidate(PSHCLCACHE pCache)
1423{
1424 AssertPtrReturnVoid(pCache);
1425
1426 shClCacheDestroyEntries(pCache);
1427}
1428
1429/**
1430 * Invalidates a specific cache entry.
1431 *
1432 * @param pCache Cache to invalidate.
1433 * @param uFmt Format to invalidate entry for.
1434 */
1435void ShClCacheInvalidateEntry(PSHCLCACHE pCache, SHCLFORMAT uFmt)
1436{
1437 AssertPtrReturnVoid(pCache);
1438 AssertReturnVoid(uFmt < VBOX_SHCL_FMT_MAX);
1439 shClCacheEntryDestroy(&pCache->aEntries[uFmt]);
1440}
1441
1442/**
1443 * Gets an entry for a Shared Clipboard format.
1444 *
1445 * @returns Pointer to entry if cached, or NULL if not in cache (cache miss).
1446 * @param pCache Cache to get entry for.
1447 * @param uFmt Format to get entry for.
1448 */
1449PSHCLCACHEENTRY ShClCacheGet(PSHCLCACHE pCache, SHCLFORMAT uFmt)
1450{
1451 AssertReturn(uFmt < VBOX_SHCL_FMT_MAX, NULL);
1452 return shClCacheEntryIsValid(&pCache->aEntries[uFmt]) ? &pCache->aEntries[uFmt] : NULL;
1453}
1454
1455/**
1456 * Sets data to cache for a specific clipboard format, internal version.
1457 *
1458 * @returns VBox status code.
1459 * @param pCache Cache to set data for.
1460 * @param uFmt Clipboard format to set data for.
1461 * @param pvData Data to set.
1462 * @param cbData Size (in bytes) of data to set.
1463 */
1464DECLINLINE(int) shClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData)
1465{
1466 AssertReturn(uFmt < VBOX_SHCL_FMT_MAX, VERR_INVALID_PARAMETER);
1467 AssertReturn(shClCacheEntryIsValid(&pCache->aEntries[uFmt]) == false, VERR_ALREADY_EXISTS);
1468
1469 return shClCacheEntryInit(&pCache->aEntries[uFmt], pvData, cbData);
1470}
1471
1472/**
1473 * Sets data to cache for a specific clipboard format.
1474 *
1475 * @returns VBox status code.
1476 * @param pCache Cache to set data for.
1477 * @param uFmt Clipboard format to set data for.
1478 * @param pvData Data to set.
1479 * @param cbData Size (in bytes) of data to set.
1480 */
1481int ShClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData)
1482{
1483 AssertPtrReturn(pCache, VERR_INVALID_POINTER);
1484
1485 if (!pvData) /* Nothing to cache? */
1486 return VINF_SUCCESS;
1487
1488 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1489
1490 return shClCacheSet(pCache, uFmt, pvData, cbData);
1491}
1492
1493/**
1494 * Sets data to cache for multiple clipboard formats.
1495 *
1496 * Will bail out if a given format cannot be handled with the data given.
1497 *
1498 * @returns VBox status code.
1499 * @param pCache Cache to set data for.
1500 * @param uFmt Clipboard format to set data for.
1501 * @param pvData Data to set.
1502 * @param cbData Size (in bytes) of data to set.
1503 */
1504int ShClCacheSetMultiple(PSHCLCACHE pCache, SHCLFORMATS uFmts, const void *pvData, size_t cbData)
1505{
1506 AssertPtrReturn(pCache, VERR_INVALID_POINTER);
1507
1508 if (!pvData) /* Nothing to cache? */
1509 return VINF_SUCCESS;
1510
1511 AssertReturn(cbData, VERR_INVALID_PARAMETER);
1512
1513 int rc = VINF_SUCCESS;
1514
1515 SHCLFORMATS uFmtsLeft = uFmts;
1516 while (uFmtsLeft)
1517 {
1518 SHCLFORMAT uFmt = VBOX_SHCL_FMT_NONE;
1519 void *pvConv = NULL;
1520 size_t cbConv = 0;
1521 if (uFmtsLeft & VBOX_SHCL_FMT_UNICODETEXT)
1522 {
1523 uFmt = VBOX_SHCL_FMT_UNICODETEXT;
1524
1525 rc = RTStrValidateEncoding((const char *)pvData);
1526 if (RT_SUCCESS(rc))
1527 {
1528 rc = RTStrToUtf16((const char *)pvData, (PRTUTF16 *)&pvConv);
1529 if (RT_SUCCESS(rc))
1530 cbConv = (RTUtf16Len((const PRTUTF16)pvConv) + 1) * sizeof(RTUTF16);
1531 }
1532 else if (!RTUtf16ValidateEncoding((const PRTUTF16)pvData))
1533 {
1534 AssertFailedBreakStmt(rc = VERR_INVALID_PARAMETER);
1535 }
1536 }
1537 else if (uFmtsLeft & VBOX_SHCL_FMT_BITMAP)
1538 uFmt = VBOX_SHCL_FMT_BITMAP;
1539 else if (uFmtsLeft & VBOX_SHCL_FMT_HTML)
1540 uFmt = VBOX_SHCL_FMT_HTML;
1541 else if (uFmtsLeft & VBOX_SHCL_FMT_URI_LIST)
1542 uFmt = VBOX_SHCL_FMT_URI_LIST;
1543 else
1544 AssertFailedBreakStmt(rc = VERR_NOT_SUPPORTED);
1545
1546 uFmtsLeft &= ~uFmt; /* Remove from list. */
1547 Assert(RT_VALID_PTR(pvConv) || cbConv == 0); /* Sanity. */
1548
1549 if (RT_SUCCESS(rc))
1550 rc = shClCacheSet(pCache, uFmt,
1551 pvConv ? pvConv : pvData,
1552 cbConv ? cbConv : cbData);
1553 RTMemFree(pvConv);
1554 AssertRCBreak(rc);
1555 }
1556
1557 return rc;
1558}
1559
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