VirtualBox

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

Last change on this file since 88556 was 87687, checked in by vboxsync, 4 years ago

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