VirtualBox

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

Last change on this file since 92735 was 92735, checked in by vboxsync, 3 years ago

Shared Clipboard/Transfers: Resolved @todos: Revamped event [source] interface to now use the event objects directly instead of the event ID. 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: 34.2 KB
Line 
1/* $Id: clipboard-common.cpp 92735 2021-12-03 16:03:24Z 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-2021 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
21
22#include <iprt/alloc.h>
23#include <iprt/assert.h>
24#include <iprt/semaphore.h>
25#include <iprt/path.h>
26#include <iprt/rand.h>
27#include <iprt/utf16.h>
28
29#include <iprt/formats/bmp.h>
30
31#include <iprt/errcore.h>
32#include <VBox/log.h>
33#include <VBox/GuestHost/clipboard-helper.h>
34#include <VBox/HostServices/VBoxClipboardSvc.h>
35
36
37/*********************************************************************************************************************************
38* Prototypes *
39*********************************************************************************************************************************/
40static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource);
41
42static void shClEventDestroy(PSHCLEVENT pEvent);
43DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent);
44
45
46/*********************************************************************************************************************************
47* Implementation *
48*********************************************************************************************************************************/
49
50/**
51 * Allocates a new event payload.
52 *
53 * @returns VBox status code.
54 * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
55 * @param pvData Data block to associate to this payload.
56 * @param cbData Size (in bytes) of data block to associate.
57 * @param ppPayload Where to store the allocated event payload on success.
58 */
59int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData,
60 PSHCLEVENTPAYLOAD *ppPayload)
61{
62 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
63 AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
64
65 PSHCLEVENTPAYLOAD pPayload = (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
66 if (pPayload)
67 {
68 pPayload->pvData = RTMemDup(pvData, cbData);
69 if (pPayload->pvData)
70 {
71 pPayload->cbData = cbData;
72 pPayload->uID = uID;
73
74 *ppPayload = pPayload;
75 return VINF_SUCCESS;
76 }
77
78 RTMemFree(pPayload);
79 }
80 return VERR_NO_MEMORY;
81}
82
83/**
84 * Frees an event payload.
85 *
86 * @returns VBox status code.
87 * @param pPayload Event payload to free.
88 */
89void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload)
90{
91 if (!pPayload)
92 return;
93
94 if (pPayload->pvData)
95 {
96 Assert(pPayload->cbData);
97 RTMemFree(pPayload->pvData);
98 pPayload->pvData = NULL;
99 }
100
101 pPayload->cbData = 0;
102 pPayload->uID = UINT32_MAX;
103
104 RTMemFree(pPayload);
105}
106
107/**
108 * Creates a new event source.
109 *
110 * @returns VBox status code.
111 * @param pSource Event source to create.
112 * @param uID ID to use for event source.
113 */
114int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
115{
116 LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
117 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
118
119 int rc = RTCritSectInit(&pSource->CritSect);
120 AssertRCReturn(rc, rc);
121
122 RTListInit(&pSource->lstEvents);
123
124 pSource->uID = uID;
125 /* Choose a random event ID starting point. */
126 pSource->idNextEvent = RTRandU32Ex(1, VBOX_SHCL_MAX_EVENTS - 1);
127
128 return VINF_SUCCESS;
129}
130
131/**
132 * Destroys an event source.
133 *
134 * @returns VBox status code.
135 * @param pSource Event source to destroy.
136 */
137int ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource)
138{
139 if (!pSource)
140 return VINF_SUCCESS;
141
142 LogFlowFunc(("ID=%RU32\n", pSource->uID));
143
144 int rc = RTCritSectEnter(&pSource->CritSect);
145 if (RT_SUCCESS(rc))
146 {
147 shClEventSourceResetInternal(pSource);
148
149 rc = RTCritSectLeave(&pSource->CritSect);
150 AssertRC(rc);
151
152 RTCritSectDelete(&pSource->CritSect);
153
154 pSource->uID = UINT16_MAX;
155 pSource->idNextEvent = UINT32_MAX;
156 }
157
158 return rc;
159}
160
161/**
162 * Resets an event source, internal version.
163 *
164 * @param pSource Event source to reset.
165 */
166static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource)
167{
168 LogFlowFunc(("ID=%RU32\n", pSource->uID));
169
170 PSHCLEVENT pEvIt;
171 PSHCLEVENT pEvItNext;
172 RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
173 {
174 RTListNodeRemove(&pEvIt->Node);
175
176 shClEventDestroy(pEvIt);
177
178 RTMemFree(pEvIt);
179 pEvIt = NULL;
180 }
181}
182
183/**
184 * Resets an event source.
185 *
186 * @param pSource Event source to reset.
187 */
188void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
189{
190 int rc2 = RTCritSectEnter(&pSource->CritSect);
191 if (RT_SUCCESS(rc2))
192 {
193 shClEventSourceResetInternal(pSource);
194
195 rc2 = RTCritSectLeave(&pSource->CritSect);
196 AssertRC(rc2);
197 }
198}
199
200/**
201 * Generates a new event ID for a specific event source and registers it.
202 *
203 * @returns VBox status code.
204 * @param pSource Event source to generate event for.
205 * @param ppEvent Where to return the new event generated on success.
206 */
207int ShClEventSourceGenerateAndRegisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT *ppEvent)
208{
209 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
210 AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
211
212 PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
213 AssertReturn(pEvent, VERR_NO_MEMORY);
214 int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
215 if (RT_SUCCESS(rc))
216 {
217 rc = RTCritSectEnter(&pSource->CritSect);
218 if (RT_SUCCESS(rc))
219 {
220 /*
221 * Allocate an unique event ID.
222 */
223 for (uint32_t cTries = 0;; cTries++)
224 {
225 SHCLEVENTID idEvent = ++pSource->idNextEvent;
226 if (idEvent < VBOX_SHCL_MAX_EVENTS)
227 { /* likely */ }
228 else
229 pSource->idNextEvent = idEvent = 1; /* zero == error, remember! */
230
231 if (shclEventGet(pSource, idEvent) == NULL)
232 {
233 pEvent->pParent = pSource;
234 pEvent->idEvent = idEvent;
235 RTListAppend(&pSource->lstEvents, &pEvent->Node);
236
237 rc = RTCritSectLeave(&pSource->CritSect);
238 AssertRC(rc);
239
240 LogFlowFunc(("uSource=%RU16: New event: %#x\n", pSource->uID, idEvent));
241
242 *ppEvent = pEvent;
243
244 return VINF_SUCCESS;
245 }
246
247 AssertBreak(cTries < 4096);
248 }
249
250 rc = RTCritSectLeave(&pSource->CritSect);
251 AssertRC(rc);
252 }
253 }
254
255 AssertMsgFailed(("Unable to register a new event ID for event source %RU16\n", pSource->uID));
256
257 RTSemEventMultiDestroy(pEvent->hEvtMulSem);
258 pEvent->hEvtMulSem = NIL_RTSEMEVENTMULTI;
259 RTMemFree(pEvent);
260 return rc;
261}
262
263/**
264 * Destroys an event.
265 *
266 * @param pEvent Event to destroy.
267 */
268static void shClEventDestroy(PSHCLEVENT pEvent)
269{
270 if (!pEvent)
271 return;
272
273 AssertMsgReturnVoid(pEvent->cRefs == 0, ("Event %RU32 still has %RU32 references\n",
274 pEvent->idEvent, pEvent->cRefs));
275
276 LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
277
278 if (pEvent->hEvtMulSem != NIL_RTSEMEVENT)
279 {
280 RTSemEventMultiDestroy(pEvent->hEvtMulSem);
281 pEvent->hEvtMulSem = NIL_RTSEMEVENT;
282 }
283
284 ShClPayloadFree(pEvent->pPayload);
285
286 pEvent->idEvent = NIL_SHCLEVENTID;
287}
288
289/**
290 * Unregisters an event.
291 *
292 * @returns VBox status code.
293 * @param pSource Event source to unregister event for.
294 * @param pEvent Event to unregister. On success the pointer will be invalid.
295 */
296static int shClEventSourceUnregisterEventInternal(PSHCLEVENTSOURCE pSource, PSHCLEVENT pEvent)
297{
298 LogFlowFunc(("idEvent=%RU32, cRefs=%RU32\n", pEvent->idEvent, pEvent->cRefs));
299
300 AssertReturn(pEvent->cRefs == 0, VERR_WRONG_ORDER);
301
302 int rc = RTCritSectEnter(&pSource->CritSect);
303 if (RT_SUCCESS(rc))
304 {
305 RTListNodeRemove(&pEvent->Node);
306
307 shClEventDestroy(pEvent);
308
309 rc = RTCritSectLeave(&pSource->CritSect);
310 if (RT_SUCCESS(rc))
311 {
312 RTMemFree(pEvent);
313 pEvent = NULL;
314 }
315 }
316
317 return rc;
318}
319
320/**
321 * Returns a specific event of a event source. Inlined version.
322 *
323 * @returns Pointer to event if found, or NULL if not found.
324 * @param pSource Event source to get event from.
325 * @param uID Event ID to get.
326 */
327DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
328{
329 PSHCLEVENT pEvent;
330 RTListForEach(&pSource->lstEvents, pEvent, SHCLEVENT, Node)
331 {
332 if (pEvent->idEvent == idEvent)
333 return pEvent;
334 }
335
336 return NULL;
337}
338
339/**
340 * Returns a specific event of a event source.
341 *
342 * @returns Pointer to event if found, or NULL if not found.
343 * @param pSource Event source to get event from.
344 * @param idEvent ID of event to return.
345 */
346PSHCLEVENT ShClEventSourceGetFromId(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
347{
348 AssertPtrReturn(pSource, NULL);
349
350 int rc = RTCritSectEnter(&pSource->CritSect);
351 if (RT_SUCCESS(rc))
352 {
353 PSHCLEVENT pEvent = shclEventGet(pSource, idEvent);
354
355 rc = RTCritSectLeave(&pSource->CritSect);
356 AssertRC(rc);
357
358 return pEvent;
359 }
360
361 return NULL;
362}
363
364/**
365 * Returns the last (newest) event ID which has been registered for an event source.
366 *
367 * @returns Pointer to last registered event, or NULL if not found.
368 * @param pSource Event source to get last registered event from.
369 */
370PSHCLEVENT ShClEventSourceGetLast(PSHCLEVENTSOURCE pSource)
371{
372 AssertPtrReturn(pSource, NULL);
373
374 int rc = RTCritSectEnter(&pSource->CritSect);
375 if (RT_SUCCESS(rc))
376 {
377 PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
378
379 rc = RTCritSectLeave(&pSource->CritSect);
380 AssertRC(rc);
381
382 return pEvent;
383 }
384
385 return NULL;
386}
387
388/**
389 * Returns the current reference count for a specific event.
390 *
391 * @returns Reference count.
392 * @param pSource Event source the specific event is part of.
393 * @param idEvent Event ID to return reference count for.
394 */
395uint32_t ShClEventGetRefs(PSHCLEVENT pEvent)
396{
397 AssertPtrReturn(pEvent, 0);
398
399 return ASMAtomicReadU32(&pEvent->cRefs);
400}
401
402/**
403 * Detaches a payload from an event, internal version.
404 *
405 * @returns Pointer to the detached payload. Can be NULL if the payload has no payload.
406 * @param pEvent Event to detach payload for.
407 */
408static PSHCLEVENTPAYLOAD shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
409{
410#ifdef VBOX_STRICT
411 AssertPtrReturn(pEvent, NULL);
412#endif
413
414 PSHCLEVENTPAYLOAD pPayload = pEvent->pPayload;
415
416 pEvent->pPayload = NULL;
417
418 return pPayload;
419}
420
421/**
422 * Waits for an event to get signalled.
423 *
424 * @returns VBox status code.
425 * @param pEvent Event to wait for.
426 * @param uTimeoutMs Timeout (in ms) to wait.
427 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
428 * SharedClipboardPayloadFree(). Optional.
429 */
430int ShClEventWait(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, PSHCLEVENTPAYLOAD *ppPayload)
431{
432 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
433 AssertPtrNullReturn(ppPayload, VERR_INVALID_POINTER);
434 LogFlowFuncEnter();
435
436 int rc = RTSemEventMultiWait(pEvent->hEvtMulSem, uTimeoutMs);
437 if (RT_SUCCESS(rc))
438 {
439 if (ppPayload)
440 {
441 /* Make sure to detach payload here, as the caller now owns the data. */
442 *ppPayload = shclEventPayloadDetachInternal(pEvent);
443 }
444 }
445
446 if (RT_FAILURE(rc))
447 LogRel2(("Shared Clipboard: Waiting for event %RU32 failed, rc=%Rrc\n", pEvent->idEvent, rc));
448
449 LogFlowFuncLeaveRC(rc);
450 return rc;
451}
452
453/**
454 * Retains an event by increasing its reference count.
455 *
456 * @returns New reference count, or UINT32_MAX if failed.
457 * @param pEvent Event to retain.
458 */
459uint32_t ShClEventRetain(PSHCLEVENT pEvent)
460{
461 AssertPtrReturn(pEvent, UINT32_MAX);
462 AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) < 64, UINT32_MAX);
463 return ASMAtomicIncU32(&pEvent->cRefs);
464}
465
466/**
467 * Releases an event by decreasing its reference count.
468 *
469 * @returns New reference count, or UINT32_MAX if failed.
470 * @param pEvent Event to release.
471 * If the reference count reaches 0, the event will
472 * be destroyed and \a pEvent will be invalid.
473 */
474uint32_t ShClEventRelease(PSHCLEVENT pEvent)
475{
476 if (!pEvent)
477 return 0;
478
479 AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) > 0, UINT32_MAX);
480
481 uint32_t const cRefs = ASMAtomicDecU32(&pEvent->cRefs);
482 if (cRefs == 0)
483 {
484 AssertPtr(pEvent->pParent);
485 int rc2 = shClEventSourceUnregisterEventInternal(pEvent->pParent, pEvent);
486 AssertRC(rc2);
487
488 return RT_SUCCESS(rc2) ? 0 : UINT32_MAX;
489 }
490
491 return cRefs;
492}
493
494/**
495 * Signals an event.
496 *
497 * @returns VBox status code.
498 * @param pEvent Event to signal.
499 * @param pPayload Event payload to associate. Takes ownership on
500 * success. Optional.
501 */
502int ShClEventSignal(PSHCLEVENT pEvent, PSHCLEVENTPAYLOAD pPayload)
503{
504 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
505
506 Assert(pEvent->pPayload == NULL);
507
508 pEvent->pPayload = pPayload;
509
510 int rc = RTSemEventMultiSignal(pEvent->hEvtMulSem);
511 if (RT_FAILURE(rc))
512 pEvent->pPayload = NULL; /* (no race condition if consumer also enters the critical section) */
513
514 LogFlowFuncLeaveRC(rc);
515 return rc;
516}
517
518int ShClUtf16LenUtf8(PCRTUTF16 pcwszSrc, size_t cwcSrc, size_t *pchLen)
519{
520 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
521 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
522
523 size_t chLen = 0;
524 int rc = RTUtf16CalcUtf8LenEx(pcwszSrc, cwcSrc, &chLen);
525 if (RT_SUCCESS(rc))
526 *pchLen = chLen;
527 return rc;
528}
529
530int ShClConvUtf16CRLFToUtf8LF(PCRTUTF16 pcwszSrc, size_t cwcSrc,
531 char *pszBuf, size_t cbBuf, size_t *pcbLen)
532{
533 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
534 AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
535 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
536 AssertPtrReturn(pcbLen, VERR_INVALID_POINTER);
537
538 int rc;
539
540 PRTUTF16 pwszTmp = NULL;
541 size_t cchTmp = 0;
542
543 size_t cbLen = 0;
544
545 /* How long will the converted text be? */
546 rc = ShClUtf16CRLFLenUtf8(pcwszSrc, cwcSrc, &cchTmp);
547 if (RT_SUCCESS(rc))
548 {
549 cchTmp++; /* Add space for terminator. */
550
551 pwszTmp = (PRTUTF16)RTMemAlloc(cchTmp * sizeof(RTUTF16));
552 if (pwszTmp)
553 {
554 rc = ShClConvUtf16CRLFToLF(pcwszSrc, cwcSrc, pwszTmp, cchTmp);
555 if (RT_SUCCESS(rc))
556 rc = RTUtf16ToUtf8Ex(pwszTmp + 1, cchTmp - 1, &pszBuf, cbBuf, &cbLen);
557
558 RTMemFree(reinterpret_cast<void *>(pwszTmp));
559 }
560 else
561 rc = VERR_NO_MEMORY;
562 }
563
564 if (RT_SUCCESS(rc))
565 {
566 *pcbLen = cbLen;
567 }
568
569 return rc;
570}
571
572int ShClConvUtf16LFToCRLFA(PCRTUTF16 pcwszSrc, size_t cwcSrc,
573 PRTUTF16 *ppwszDst, size_t *pcwDst)
574{
575 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
576 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
577 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
578
579 PRTUTF16 pwszDst = NULL;
580 size_t cchDst;
581
582 int rc = ShClUtf16LFLenUtf8(pcwszSrc, cwcSrc, &cchDst);
583 if (RT_SUCCESS(rc))
584 {
585 pwszDst = (PRTUTF16)RTMemAlloc((cchDst + 1 /* Leave space for terminator */) * sizeof(RTUTF16));
586 if (pwszDst)
587 {
588 rc = ShClConvUtf16LFToCRLF(pcwszSrc, cwcSrc, pwszDst, cchDst + 1 /* Include terminator */);
589 }
590 else
591 rc = VERR_NO_MEMORY;
592 }
593
594 if (RT_SUCCESS(rc))
595 {
596 *ppwszDst = pwszDst;
597 *pcwDst = cchDst;
598 }
599 else
600 RTMemFree(pwszDst);
601
602 LogFlowFuncLeaveRC(rc);
603 return rc;
604}
605
606int ShClConvUtf8LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
607 PRTUTF16 *ppwszDst, size_t *pcwDst)
608{
609 AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
610 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
611 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
612 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
613
614 /* Intermediate conversion to UTF-16. */
615 size_t cwcTmp;
616 PRTUTF16 pwcTmp = NULL;
617 int rc = RTStrToUtf16Ex(pcszSrc, cbSrc, &pwcTmp, 0, &cwcTmp);
618 if (RT_SUCCESS(rc))
619 {
620 rc = ShClConvUtf16LFToCRLFA(pwcTmp, cwcTmp, ppwszDst, pcwDst);
621 RTUtf16Free(pwcTmp);
622 }
623
624 return rc;
625}
626
627int ShClConvLatin1LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
628 PRTUTF16 *ppwszDst, size_t *pcwDst)
629{
630 AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
631 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
632 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
633 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
634
635 int rc = VINF_SUCCESS;
636
637 PRTUTF16 pwszDst = NULL;
638
639 /* Calculate the space needed. */
640 unsigned cwDst = 0;
641 for (unsigned i = 0; i < cbSrc && pcszSrc[i] != '\0'; ++i)
642 {
643 if (pcszSrc[i] == VBOX_SHCL_LINEFEED)
644 cwDst += 2; /* Space for VBOX_SHCL_CARRIAGERETURN + VBOX_SHCL_LINEFEED. */
645 else
646 ++cwDst;
647 }
648
649 pwszDst = (PRTUTF16)RTMemAlloc((cwDst + 1 /* Leave space for the terminator */) * sizeof(RTUTF16));
650 if (!pwszDst)
651 rc = VERR_NO_MEMORY;
652
653 /* Do the conversion, bearing in mind that Latin-1 expands "naturally" to UTF-16. */
654 if (RT_SUCCESS(rc))
655 {
656 for (unsigned i = 0, j = 0; i < cbSrc; ++i, ++j)
657 {
658 if (pcszSrc[i] != VBOX_SHCL_LINEFEED)
659 pwszDst[j] = pcszSrc[i];
660 else
661 {
662 pwszDst[j] = VBOX_SHCL_CARRIAGERETURN;
663 pwszDst[j + 1] = VBOX_SHCL_LINEFEED;
664 ++j;
665 }
666 }
667
668 pwszDst[cwDst] = '\0'; /* Make sure we are zero-terminated. */
669 }
670
671 if (RT_SUCCESS(rc))
672 {
673 *ppwszDst = pwszDst;
674 *pcwDst = cwDst;
675 }
676 else
677 RTMemFree(pwszDst);
678
679 return rc;
680}
681
682int ShClConvUtf16ToUtf8HTML(PCRTUTF16 pcwszSrc, size_t cwcSrc, char **ppszDst, size_t *pcbDst)
683{
684 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
685 AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
686 AssertPtrReturn(ppszDst, VERR_INVALID_POINTER);
687 AssertPtrReturn(pcbDst, VERR_INVALID_POINTER);
688
689 int rc = VINF_SUCCESS;
690
691 size_t cwTmp = cwcSrc;
692 PCRTUTF16 pwTmp = pcwszSrc;
693
694 char *pchDst = NULL;
695 size_t cbDst = 0;
696
697 size_t i = 0;
698 while (i < cwTmp)
699 {
700 /* Find zero symbol (end of string). */
701 for (; i < cwTmp && pcwszSrc[i] != 0; i++)
702 ;
703
704 /* Convert found string. */
705 char *psz = NULL;
706 size_t cch = 0;
707 rc = RTUtf16ToUtf8Ex(pwTmp, cwTmp, &psz, pwTmp - pcwszSrc, &cch);
708 if (RT_FAILURE(rc))
709 break;
710
711 /* Append new substring. */
712 char *pchNew = (char *)RTMemRealloc(pchDst, cbDst + cch + 1);
713 if (!pchNew)
714 {
715 RTStrFree(psz);
716 rc = VERR_NO_MEMORY;
717 break;
718 }
719
720 pchDst = pchNew;
721 memcpy(pchDst + cbDst, psz, cch + 1);
722
723 RTStrFree(psz);
724
725 cbDst += cch + 1;
726
727 /* Skip zero symbols. */
728 for (; i < cwTmp && pcwszSrc[i] == 0; i++)
729 ;
730
731 /* Remember start of string. */
732 pwTmp += i;
733 }
734
735 if (RT_SUCCESS(rc))
736 {
737 *ppszDst = pchDst;
738 *pcbDst = cbDst;
739
740 return VINF_SUCCESS;
741 }
742
743 RTMemFree(pchDst);
744
745 return rc;
746}
747
748int ShClUtf16LFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
749{
750 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
751 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
752
753 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
754 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
755
756 size_t cLen = 0;
757
758 /* Don't copy the endian marker. */
759 size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
760
761 /* Calculate the size of the destination text string. */
762 /* Is this Utf16 or Utf16-LE? */
763 for (; i < cwSrc; ++i, ++cLen)
764 {
765 /* Check for a single line feed */
766 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
767 ++cLen;
768#ifdef RT_OS_DARWIN
769 /* Check for a single carriage return (MacOS) */
770 if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
771 ++cLen;
772#endif
773 if (pcwszSrc[i] == 0)
774 {
775 /* Don't count this, as we do so below. */
776 break;
777 }
778 }
779
780 *pchLen = cLen;
781
782 return VINF_SUCCESS;
783}
784
785int ShClUtf16CRLFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
786{
787 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
788 AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
789 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
790
791 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
792 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
793
794 size_t cLen = 0;
795
796 /* Calculate the size of the destination text string. */
797 /* Is this Utf16 or Utf16-LE? */
798 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
799 cLen = 0;
800 else
801 cLen = 1;
802
803 for (size_t i = 0; i < cwSrc; ++i, ++cLen)
804 {
805 if ( (i + 1 < cwSrc)
806 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
807 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
808 {
809 ++i;
810 }
811 if (pcwszSrc[i] == 0)
812 break;
813 }
814
815 *pchLen = cLen;
816
817 return VINF_SUCCESS;
818}
819
820int ShClConvUtf16LFToCRLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
821{
822 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
823 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
824 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
825
826 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
827 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
828
829 int rc = VINF_SUCCESS;
830
831 /* Don't copy the endian marker. */
832 size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
833 size_t j = 0;
834
835 for (; i < cwcSrc; ++i, ++j)
836 {
837 /* Don't copy the null byte, as we add it below. */
838 if (pcwszSrc[i] == 0)
839 break;
840
841 /* Not enough space in destination? */
842 if (j == cwDst)
843 {
844 rc = VERR_BUFFER_OVERFLOW;
845 break;
846 }
847
848 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
849 {
850 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
851 ++j;
852
853 /* Not enough space in destination? */
854 if (j == cwDst)
855 {
856 rc = VERR_BUFFER_OVERFLOW;
857 break;
858 }
859 }
860#ifdef RT_OS_DARWIN
861 /* Check for a single carriage return (MacOS) */
862 else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
863 {
864 /* Set CR.r */
865 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
866 ++j;
867
868 /* Not enough space in destination? */
869 if (j == cwDst)
870 {
871 rc = VERR_BUFFER_OVERFLOW;
872 break;
873 }
874
875 /* Add line feed. */
876 pu16Dst[j] = VBOX_SHCL_LINEFEED;
877 continue;
878 }
879#endif
880 pu16Dst[j] = pcwszSrc[i];
881 }
882
883 if (j == cwDst)
884 rc = VERR_BUFFER_OVERFLOW;
885
886 if (RT_SUCCESS(rc))
887 {
888 /* Add terminator. */
889 pu16Dst[j] = 0;
890 }
891
892 return rc;
893}
894
895int ShClConvUtf16CRLFToLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
896{
897 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
898 AssertReturn(cwcSrc, VERR_INVALID_PARAMETER);
899 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
900 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
901
902 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
903 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
904
905 /* Prepend the Utf16 byte order marker if it is missing. */
906 size_t cwDstPos;
907 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
908 {
909 cwDstPos = 0;
910 }
911 else
912 {
913 pu16Dst[0] = VBOX_SHCL_UTF16LEMARKER;
914 cwDstPos = 1;
915 }
916
917 for (size_t i = 0; i < cwcSrc; ++i, ++cwDstPos)
918 {
919 if (pcwszSrc[i] == 0)
920 break;
921
922 if (cwDstPos == cwDst)
923 return VERR_BUFFER_OVERFLOW;
924
925 if ( (i + 1 < cwcSrc)
926 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
927 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
928 {
929 ++i;
930 }
931
932 pu16Dst[cwDstPos] = pcwszSrc[i];
933 }
934
935 if (cwDstPos == cwDst)
936 return VERR_BUFFER_OVERFLOW;
937
938 /* Add terminating zero. */
939 pu16Dst[cwDstPos] = 0;
940
941 return VINF_SUCCESS;
942}
943
944int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
945{
946 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
947 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
948 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
949 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
950
951 PBMPWIN3XINFOHDR coreHdr = (PBMPWIN3XINFOHDR)pvSrc;
952 /** @todo Support all the many versions of the DIB headers. */
953 if ( cbSrc < sizeof(BMPWIN3XINFOHDR)
954 || RT_LE2H_U32(coreHdr->cbSize) < sizeof(BMPWIN3XINFOHDR)
955 || RT_LE2H_U32(coreHdr->cbSize) != sizeof(BMPWIN3XINFOHDR))
956 {
957 return VERR_INVALID_PARAMETER;
958 }
959
960 size_t offPixel = sizeof(BMPFILEHDR)
961 + RT_LE2H_U32(coreHdr->cbSize)
962 + RT_LE2H_U32(coreHdr->cClrUsed) * sizeof(uint32_t);
963 if (cbSrc < offPixel)
964 return VERR_INVALID_PARAMETER;
965
966 size_t cbDst = sizeof(BMPFILEHDR) + cbSrc;
967
968 void *pvDest = RTMemAlloc(cbDst);
969 if (!pvDest)
970 return VERR_NO_MEMORY;
971
972 PBMPFILEHDR fileHdr = (PBMPFILEHDR)pvDest;
973
974 fileHdr->uType = BMP_HDR_MAGIC;
975 fileHdr->cbFileSize = (uint32_t)RT_H2LE_U32(cbDst);
976 fileHdr->Reserved1 = 0;
977 fileHdr->Reserved2 = 0;
978 fileHdr->offBits = (uint32_t)RT_H2LE_U32(offPixel);
979
980 memcpy((uint8_t *)pvDest + sizeof(BMPFILEHDR), pvSrc, cbSrc);
981
982 *ppvDest = pvDest;
983 *pcbDest = cbDst;
984
985 return VINF_SUCCESS;
986}
987
988int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
989{
990 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
991 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
992 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
993 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
994
995 PBMPFILEHDR pBmpHdr = (PBMPFILEHDR)pvSrc;
996 if ( cbSrc < sizeof(BMPFILEHDR)
997 || pBmpHdr->uType != BMP_HDR_MAGIC
998 || RT_LE2H_U32(pBmpHdr->cbFileSize) != cbSrc)
999 {
1000 return VERR_INVALID_PARAMETER;
1001 }
1002
1003 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMPFILEHDR);
1004 *pcbDest = cbSrc - sizeof(BMPFILEHDR);
1005
1006 return VINF_SUCCESS;
1007}
1008
1009#ifdef LOG_ENABLED
1010
1011int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
1012{
1013 int rc = VINF_SUCCESS;
1014 char *pszBuf = (char *)RTMemTmpAllocZ(cbSrc + 1);
1015 if (pszBuf)
1016 {
1017 memcpy(pszBuf, pcszSrc, cbSrc);
1018 pszBuf[cbSrc] = '\0';
1019 for (size_t off = 0; off < cbSrc; ++off)
1020 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
1021 pszBuf[off] = ' ';
1022 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
1023 RTMemTmpFree(pszBuf);
1024 }
1025 else
1026 rc = VERR_NO_MEMORY;
1027 return rc;
1028}
1029
1030void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
1031{
1032 if (LogIsEnabled())
1033 {
1034 if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
1035 {
1036 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
1037 if (pv && cb)
1038 LogFunc(("%ls\n", pv));
1039 else
1040 LogFunc(("%p %zu\n", pv, cb));
1041 }
1042 else if (uFormat & VBOX_SHCL_FMT_BITMAP)
1043 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
1044 else if (uFormat & VBOX_SHCL_FMT_HTML)
1045 {
1046 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
1047 if (pv && cb)
1048 {
1049 LogFunc(("%s\n", pv));
1050 ShClDbgDumpHtml((const char *)pv, cb);
1051 }
1052 else
1053 LogFunc(("%p %zu\n", pv, cb));
1054 }
1055 else
1056 LogFunc(("Invalid format %02X\n", uFormat));
1057 }
1058}
1059
1060#endif /* LOG_ENABLED */
1061
1062/**
1063 * Translates a Shared Clipboard host function number to a string.
1064 *
1065 * @returns Function ID string name.
1066 * @param uFn The function to translate.
1067 */
1068const char *ShClHostFunctionToStr(uint32_t uFn)
1069{
1070 switch (uFn)
1071 {
1072 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
1073 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
1074 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
1075 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
1076 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
1077 }
1078 return "Unknown";
1079}
1080
1081/**
1082 * Translates a Shared Clipboard host message enum to a string.
1083 *
1084 * @returns Message ID string name.
1085 * @param uMsg The message to translate.
1086 */
1087const char *ShClHostMsgToStr(uint32_t uMsg)
1088{
1089 switch (uMsg)
1090 {
1091 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
1092 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
1093 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
1094 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_CANCELED);
1095 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA_CID);
1096 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
1097 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
1098 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
1099 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
1100 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
1101 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
1102 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
1103 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
1104 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
1105 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
1106 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
1107 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
1108 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
1109 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
1110 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
1111 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
1112 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
1113 }
1114 return "Unknown";
1115}
1116
1117/**
1118 * Translates a Shared Clipboard guest message enum to a string.
1119 *
1120 * @returns Message ID string name.
1121 * @param uMsg The message to translate.
1122 */
1123const char *ShClGuestMsgToStr(uint32_t uMsg)
1124{
1125 switch (uMsg)
1126 {
1127 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
1128 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
1129 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
1130 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
1131 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
1132 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
1133 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
1134 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
1135 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
1136 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
1137 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
1138 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
1139 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
1140 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
1141 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
1142 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
1143 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
1144 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
1145 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
1146 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
1147 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
1148 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
1149 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
1150 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
1151 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
1152 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
1153 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
1154 }
1155 return "Unknown";
1156}
1157
1158/**
1159 * Converts Shared Clipboard formats to a string.
1160 *
1161 * @returns Stringified Shared Clipboard formats, or NULL on failure. Must be free'd with RTStrFree().
1162 * @param fFormats Shared Clipboard formats to convert.
1163 *
1164 */
1165char *ShClFormatsToStrA(SHCLFORMATS fFormats)
1166{
1167#define APPEND_FMT_TO_STR(_aFmt) \
1168 if (fFormats & VBOX_SHCL_FMT_##_aFmt) \
1169 { \
1170 if (pszFmts) \
1171 { \
1172 rc2 = RTStrAAppend(&pszFmts, ", "); \
1173 if (RT_FAILURE(rc2)) \
1174 break; \
1175 } \
1176 \
1177 rc2 = RTStrAAppend(&pszFmts, #_aFmt); \
1178 if (RT_FAILURE(rc2)) \
1179 break; \
1180 }
1181
1182 char *pszFmts = NULL;
1183 int rc2 = VINF_SUCCESS;
1184
1185 do
1186 {
1187 APPEND_FMT_TO_STR(UNICODETEXT);
1188 APPEND_FMT_TO_STR(BITMAP);
1189 APPEND_FMT_TO_STR(HTML);
1190# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1191 APPEND_FMT_TO_STR(URI_LIST);
1192# endif
1193
1194 } while (0);
1195
1196 if (!pszFmts)
1197 rc2 = RTStrAAppend(&pszFmts, "NONE");
1198
1199 if ( RT_FAILURE(rc2)
1200 && pszFmts)
1201 {
1202 RTStrFree(pszFmts);
1203 pszFmts = NULL;
1204 }
1205
1206#undef APPEND_FMT_TO_STR
1207
1208 return pszFmts;
1209}
1210
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