VirtualBox

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

Last change on this file since 96757 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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