VirtualBox

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

Last change on this file since 80845 was 80845, checked in by vboxsync, 5 years ago

Shared Clipboard/URI: More code for transfer channel handling.

  • Property eol-style set to native
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 27.0 KB
Line 
1/* $Id: clipboard-common.cpp 80845 2019-09-17 09:05:21Z 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-2019 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
28#include <iprt/errcore.h>
29#include <VBox/log.h>
30#include <VBox/GuestHost/clipboard-helper.h>
31#include <VBox/HostServices/VBoxClipboardSvc.h>
32
33
34/**
35 * Allocates a new event payload.
36 *
37 * @returns VBox status code.
38 * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
39 * @param pvData Data block to associate to this payload.
40 * @param cbData Size (in bytes) of data block to associate.
41 * @param ppPayload Where to store the allocated event payload on success.
42 */
43int SharedClipboardPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData,
44 PSHCLEVENTPAYLOAD *ppPayload)
45{
46 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
47 AssertReturn (cbData, VERR_INVALID_PARAMETER);
48
49 PSHCLEVENTPAYLOAD pPayload =
50 (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
51 if (!pPayload)
52 return VERR_NO_MEMORY;
53
54 pPayload->pvData = RTMemAlloc(cbData);
55 if (pPayload->pvData)
56 {
57 memcpy(pPayload->pvData, pvData, cbData);
58
59 pPayload->cbData = cbData;
60 pPayload->uID = uID;
61
62 *ppPayload = pPayload;
63
64 return VINF_SUCCESS;
65 }
66
67 RTMemFree(pPayload);
68 return VERR_NO_MEMORY;
69}
70
71/**
72 * Frees an event payload.
73 *
74 * @returns VBox status code.
75 * @param pPayload Event payload to free.
76 */
77void SharedClipboardPayloadFree(PSHCLEVENTPAYLOAD pPayload)
78{
79 if (!pPayload)
80 return;
81
82 if (pPayload->pvData)
83 {
84 Assert(pPayload->cbData);
85 RTMemFree(pPayload->pvData);
86 pPayload->pvData = NULL;
87 }
88
89 pPayload->cbData = 0;
90
91 RTMemFree(pPayload);
92 pPayload = NULL;
93}
94
95/**
96 * Creates (initializes) an event.
97 *
98 * @returns VBox status code.
99 * @param pEvent Event to initialize.
100 * @param uID Event ID to use.
101 */
102int SharedClipboardEventCreate(PSHCLEVENT pEvent, SHCLEVENTID uID)
103{
104 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
105
106 LogFlowFunc(("Event %RU16\n", uID));
107
108 int rc = RTSemEventCreate(&pEvent->hEventSem);
109 if (RT_SUCCESS(rc))
110 {
111 pEvent->uID = uID;
112 pEvent->pPayload = NULL;
113 }
114
115 return rc;
116}
117
118/**
119 * Destroys an event.
120 *
121 * @param pEvent Event to destroy.
122 */
123void SharedClipboardEventDestroy(PSHCLEVENT pEvent)
124{
125 if (!pEvent)
126 return;
127
128 LogFlowFunc(("Event %RU16\n", pEvent->uID));
129
130 if (pEvent->hEventSem != NIL_RTSEMEVENT)
131 {
132 RTSemEventDestroy(pEvent->hEventSem);
133 pEvent->hEventSem = NIL_RTSEMEVENT;
134 }
135
136 SharedClipboardPayloadFree(pEvent->pPayload);
137
138 pEvent->uID = 0;
139}
140
141/**
142 * Creates a new event source.
143 *
144 * @returns VBox status code.
145 * @param pSource Event source to create.
146 * @param uID ID to use for event source.
147 */
148int SharedClipboardEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
149{
150 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
151
152 LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
153
154 int rc = VINF_SUCCESS;
155
156 RTListInit(&pSource->lstEvents);
157
158 pSource->uID = uID;
159 /* Choose a random event ID starting point. */
160 pSource->uEventIDNext = RTRandU32() % VBOX_SHARED_CLIPBOARD_MAX_EVENTS;
161
162 LogFlowFuncLeaveRC(rc);
163 return rc;
164}
165
166/**
167 * Destroys an event source.
168 *
169 * @param pSource Event source to destroy.
170 */
171void SharedClipboardEventSourceDestroy(PSHCLEVENTSOURCE pSource)
172{
173 if (!pSource)
174 return;
175
176 LogFlowFunc(("ID=%RU16\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 SharedClipboardEventDestroy(pEvIt);
185
186 RTMemFree(pEvIt);
187 pEvIt = NULL;
188 }
189
190 pSource->uID = 0;
191 pSource->uEventIDNext = 0;
192}
193
194/**
195 * Generates a new event ID for a specific event source.
196 *
197 * @returns New event ID generated, or 0 on error.
198 * @param pSource Event source to generate event for.
199 */
200SHCLEVENTID SharedClipboardEventIDGenerate(PSHCLEVENTSOURCE pSource)
201{
202 AssertPtrReturn(pSource, 0);
203
204 LogFlowFunc(("uSource=%RU16: New event: %RU16\n", pSource->uID, pSource->uEventIDNext));
205
206 pSource->uEventIDNext++;
207 if (pSource->uEventIDNext == VBOX_SHARED_CLIPBOARD_MAX_EVENTS)
208 pSource->uEventIDNext = 0;
209
210 return pSource->uEventIDNext;
211}
212
213/**
214 * Returns a specific event of a event source.
215 *
216 * @returns Pointer to event if found, or NULL if not found.
217 * @param pSource Event source to get event from.
218 * @param uID Event ID to get.
219 */
220inline PSHCLEVENT sharedClipboardEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
221{
222 PSHCLEVENT pEvIt;
223 RTListForEach(&pSource->lstEvents, pEvIt, SHCLEVENT, Node)
224 {
225 if (pEvIt->uID == uID)
226 return pEvIt;
227 }
228
229 return NULL;
230}
231
232/**
233 * Returns the last (newest) event ID which has been registered for an event source.
234 *
235 * @returns Last registered event ID, or 0 if not found.
236 * @param pSource Event source to get last registered event from.
237 */
238SHCLEVENTID SharedClipboardEventGetLast(PSHCLEVENTSOURCE pSource)
239{
240 AssertPtrReturn(pSource, 0);
241 PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
242 if (pEvent)
243 return pEvent->uID;
244
245 return 0;
246}
247
248/**
249 * Detaches a payload from an event, internal version.
250 *
251 * @param pEvent Event to detach payload for.
252 */
253static void sharedClipboardEventPayloadDetachInternal(PSHCLEVENT pEvent)
254{
255 AssertPtrReturnVoid(pEvent);
256
257 pEvent->pPayload = NULL;
258}
259
260/**
261 * Registers an event.
262 *
263 * @returns VBox status code.
264 * @param pSource Event source to register event for.
265 * @param uID Event ID to register.
266 */
267int SharedClipboardEventRegister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
268{
269 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
270
271 int rc;
272
273 LogFlowFunc(("uSource=%RU16, uEvent=%RU16\n", pSource->uID, uID));
274
275 if (sharedClipboardEventGet(pSource, uID) == NULL)
276 {
277 PSHCLEVENT pEvent
278 = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
279 if (pEvent)
280 {
281 rc = SharedClipboardEventCreate(pEvent, uID);
282 if (RT_SUCCESS(rc))
283 {
284 RTListAppend(&pSource->lstEvents, &pEvent->Node);
285
286 LogFlowFunc(("Event %RU16\n", uID));
287 }
288 }
289 else
290 rc = VERR_NO_MEMORY;
291 }
292 else
293 rc = VERR_ALREADY_EXISTS;
294
295#ifdef DEBUG_andy
296 AssertRC(rc);
297#endif
298
299 LogFlowFuncLeaveRC(rc);
300 return rc;
301}
302
303/**
304 * Unregisters an event.
305 *
306 * @returns VBox status code.
307 * @param pSource Event source to unregister event for.
308 * @param uID Event ID to unregister.
309 */
310int SharedClipboardEventUnregister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
311{
312 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
313
314 int rc;
315
316 LogFlowFunc(("uSource=%RU16, uEvent=%RU16\n", pSource->uID, uID));
317
318 PSHCLEVENT pEvent = sharedClipboardEventGet(pSource, uID);
319 if (pEvent)
320 {
321 LogFlowFunc(("Event %RU16\n", pEvent->uID));
322
323 RTListNodeRemove(&pEvent->Node);
324
325 SharedClipboardEventDestroy(pEvent);
326
327 RTMemFree(pEvent);
328 pEvent = NULL;
329
330 rc = VINF_SUCCESS;
331 }
332 else
333 rc = VERR_NOT_FOUND;
334
335 LogFlowFuncLeaveRC(rc);
336 return rc;
337}
338
339/**
340 * Waits for an event to get signalled.
341 *
342 * @returns VBox status code.
343 * @param pSource Event source that contains the event to wait for.
344 * @param uID Event ID to wait for.
345 * @param uTimeoutMs Timeout (in ms) to wait.
346 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
347 * SharedClipboardPayloadFree(). Optional.
348 */
349int SharedClipboardEventWait(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID, RTMSINTERVAL uTimeoutMs,
350 PSHCLEVENTPAYLOAD* ppPayload)
351{
352 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
353 /** ppPayload is optional. */
354
355 LogFlowFuncEnter();
356
357 int rc;
358
359 PSHCLEVENT pEvent = sharedClipboardEventGet(pSource, uID);
360 if (pEvent)
361 {
362 rc = RTSemEventWait(pEvent->hEventSem, uTimeoutMs);
363 if (RT_SUCCESS(rc))
364 {
365 if (ppPayload)
366 {
367 *ppPayload = pEvent->pPayload;
368
369 /* Make sure to detach payload here, as the caller now owns the data. */
370 sharedClipboardEventPayloadDetachInternal(pEvent);
371 }
372 }
373 }
374 else
375 rc = VERR_NOT_FOUND;
376
377 LogFlowFuncLeaveRC(rc);
378 return rc;
379}
380
381/**
382 * Signals an event.
383 *
384 * @returns VBox status code.
385 * @param pSource Event source of event to signal.
386 * @param uID Event ID to signal.
387 * @param pPayload Event payload to associate. Takes ownership. Optional.
388 */
389int SharedClipboardEventSignal(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID,
390 PSHCLEVENTPAYLOAD pPayload)
391{
392 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
393
394 int rc;
395
396 LogFlowFunc(("uSource=%RU16, uEvent=%RU16\n", pSource->uID, uID));
397
398 PSHCLEVENT pEvent = sharedClipboardEventGet(pSource, uID);
399 if (pEvent)
400 {
401 Assert(pEvent->pPayload == NULL);
402
403 pEvent->pPayload = pPayload;
404
405 rc = RTSemEventSignal(pEvent->hEventSem);
406 }
407 else
408 rc = VERR_NOT_FOUND;
409
410 LogFlowFuncLeaveRC(rc);
411 return rc;
412}
413
414/**
415 * Detaches a payload from an event.
416 *
417 * @returns VBox status code.
418 * @param pSource Event source of event to detach payload for.
419 * @param uID Event ID to detach payload for.
420 */
421void SharedClipboardEventPayloadDetach(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
422{
423 AssertPtrReturnVoid(pSource);
424
425 LogFlowFunc(("uSource=%RU16, uEvent=%RU16\n", pSource->uID, uID));
426
427 PSHCLEVENT pEvent = sharedClipboardEventGet(pSource, uID);
428 if (pEvent)
429 {
430 sharedClipboardEventPayloadDetachInternal(pEvent);
431 }
432#ifdef DEBUG_andy
433 else
434 AssertMsgFailed(("uSource=%RU16, uEvent=%RU16\n", pSource->uID, uID));
435#endif
436}
437
438/** @todo use const where appropriate; delinuxify the code (*Lin* -> *Host*); use AssertLogRel*. */
439
440int vboxClipboardUtf16GetWinSize(PRTUTF16 pwszSrc, size_t cwSrc, size_t *pcwDest)
441{
442 size_t cwDest, i;
443
444 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
445 AssertLogRelMsgReturn(pwszSrc != NULL, ("vboxClipboardUtf16GetWinSize: received a null Utf16 string, returning VERR_INVALID_PARAMETER\n"), VERR_INVALID_PARAMETER);
446 if (cwSrc == 0)
447 {
448 *pcwDest = 0;
449 LogFlowFunc(("empty source string, returning\n"));
450 return VINF_SUCCESS;
451 }
452/** @todo convert the remainder of the Assert stuff to AssertLogRel. */
453 /* We only take little endian Utf16 */
454 if (pwszSrc[0] == UTF16BEMARKER)
455 {
456 LogRel(("vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
457 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
458 }
459 cwDest = 0;
460 /* Calculate the size of the destination text string. */
461 /* Is this Utf16 or Utf16-LE? */
462 for (i = (pwszSrc[0] == UTF16LEMARKER ? 1 : 0); i < cwSrc; ++i, ++cwDest)
463 {
464 /* Check for a single line feed */
465 if (pwszSrc[i] == LINEFEED)
466 ++cwDest;
467#ifdef RT_OS_DARWIN
468 /* Check for a single carriage return (MacOS) */
469 if (pwszSrc[i] == CARRIAGERETURN)
470 ++cwDest;
471#endif
472 if (pwszSrc[i] == 0)
473 {
474 /* Don't count this, as we do so below. */
475 break;
476 }
477 }
478 /* Count the terminating null byte. */
479 ++cwDest;
480 LogFlowFunc(("returning VINF_SUCCESS, %d 16bit words\n", cwDest));
481 *pcwDest = cwDest;
482 return VINF_SUCCESS;
483}
484
485int vboxClipboardUtf16LinToWin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest,
486 size_t cwDest)
487{
488 size_t i, j;
489 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
490 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
491 {
492 LogRel(("vboxClipboardUtf16LinToWin: received an invalid pointer, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
493 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
494 }
495 if (cwSrc == 0)
496 {
497 if (cwDest == 0)
498 {
499 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
500 return VERR_BUFFER_OVERFLOW;
501 }
502 pu16Dest[0] = 0;
503 LogFlowFunc(("empty source string, returning\n"));
504 return VINF_SUCCESS;
505 }
506 /* We only take little endian Utf16 */
507 if (pwszSrc[0] == UTF16BEMARKER)
508 {
509 LogRel(("vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
510 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
511 }
512 /* Don't copy the endian marker. */
513 for (i = (pwszSrc[0] == UTF16LEMARKER ? 1 : 0), j = 0; i < cwSrc; ++i, ++j)
514 {
515 /* Don't copy the null byte, as we add it below. */
516 if (pwszSrc[i] == 0)
517 break;
518 if (j == cwDest)
519 {
520 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
521 return VERR_BUFFER_OVERFLOW;
522 }
523 if (pwszSrc[i] == LINEFEED)
524 {
525 pu16Dest[j] = CARRIAGERETURN;
526 ++j;
527 if (j == cwDest)
528 {
529 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
530 return VERR_BUFFER_OVERFLOW;
531 }
532 }
533#ifdef RT_OS_DARWIN
534 /* Check for a single carriage return (MacOS) */
535 else if (pwszSrc[i] == CARRIAGERETURN)
536 {
537 /* set cr */
538 pu16Dest[j] = CARRIAGERETURN;
539 ++j;
540 if (j == cwDest)
541 {
542 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
543 return VERR_BUFFER_OVERFLOW;
544 }
545 /* add the lf */
546 pu16Dest[j] = LINEFEED;
547 continue;
548 }
549#endif
550 pu16Dest[j] = pwszSrc[i];
551 }
552 /* Add the trailing null. */
553 if (j == cwDest)
554 {
555 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
556 return VERR_BUFFER_OVERFLOW;
557 }
558 pu16Dest[j] = 0;
559 LogFlowFunc(("rc=VINF_SUCCESS, pu16Dest=%ls\n", pu16Dest));
560 return VINF_SUCCESS;
561}
562
563int vboxClipboardUtf16GetLinSize(PRTUTF16 pwszSrc, size_t cwSrc, size_t *pcwDest)
564{
565 size_t cwDest;
566
567 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
568 if (!VALID_PTR(pwszSrc))
569 {
570 LogRel(("vboxClipboardUtf16GetLinSize: received an invalid Utf16 string %p. Returning VERR_INVALID_PARAMETER.\n", pwszSrc));
571 AssertReturn(VALID_PTR(pwszSrc), VERR_INVALID_PARAMETER);
572 }
573 if (cwSrc == 0)
574 {
575 LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
576 *pcwDest = 0;
577 return VINF_SUCCESS;
578 }
579 /* We only take little endian Utf16 */
580 if (pwszSrc[0] == UTF16BEMARKER)
581 {
582 LogRel(("vboxClipboardUtf16GetLinSize: received a big endian Utf16 string. Returning VERR_INVALID_PARAMETER.\n"));
583 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
584 }
585 /* Calculate the size of the destination text string. */
586 /* Is this Utf16 or Utf16-LE? */
587 if (pwszSrc[0] == UTF16LEMARKER)
588 cwDest = 0;
589 else
590 cwDest = 1;
591 for (size_t i = 0; i < cwSrc; ++i, ++cwDest)
592 {
593 if ( (i + 1 < cwSrc)
594 && (pwszSrc[i] == CARRIAGERETURN)
595 && (pwszSrc[i + 1] == LINEFEED))
596 {
597 ++i;
598 }
599 if (pwszSrc[i] == 0)
600 {
601 break;
602 }
603 }
604 /* Terminating zero */
605 ++cwDest;
606 LogFlowFunc(("returning %d\n", cwDest));
607 *pcwDest = cwDest;
608 return VINF_SUCCESS;
609}
610
611int vboxClipboardUtf16WinToLin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest,
612 size_t cwDest)
613{
614 size_t cwDestPos;
615
616 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
617 cwSrc, pwszSrc, cwSrc, pu16Dest, cwDest));
618 /* A buffer of size 0 may not be an error, but it is not a good idea either. */
619 Assert(cwDest > 0);
620 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
621 {
622 LogRel(("vboxClipboardUtf16WinToLin: received an invalid ptr, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
623 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
624 }
625 /* We only take little endian Utf16 */
626 if (pwszSrc[0] == UTF16BEMARKER)
627 {
628 LogRel(("vboxClipboardUtf16WinToLin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
629 AssertMsgFailedReturn(("received a big endian string\n"), VERR_INVALID_PARAMETER);
630 }
631 if (cwDest == 0)
632 {
633 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
634 return VERR_BUFFER_OVERFLOW;
635 }
636 if (cwSrc == 0)
637 {
638 pu16Dest[0] = 0;
639 LogFlowFunc(("received empty string. Returning VINF_SUCCESS\n"));
640 return VINF_SUCCESS;
641 }
642 /* Prepend the Utf16 byte order marker if it is missing. */
643 if (pwszSrc[0] == UTF16LEMARKER)
644 {
645 cwDestPos = 0;
646 }
647 else
648 {
649 pu16Dest[0] = UTF16LEMARKER;
650 cwDestPos = 1;
651 }
652 for (size_t i = 0; i < cwSrc; ++i, ++cwDestPos)
653 {
654 if (pwszSrc[i] == 0)
655 {
656 break;
657 }
658 if (cwDestPos == cwDest)
659 {
660 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
661 return VERR_BUFFER_OVERFLOW;
662 }
663 if ( (i + 1 < cwSrc)
664 && (pwszSrc[i] == CARRIAGERETURN)
665 && (pwszSrc[i + 1] == LINEFEED))
666 {
667 ++i;
668 }
669 pu16Dest[cwDestPos] = pwszSrc[i];
670 }
671 /* Terminating zero */
672 if (cwDestPos == cwDest)
673 {
674 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
675 return VERR_BUFFER_OVERFLOW;
676 }
677 pu16Dest[cwDestPos] = 0;
678 LogFlowFunc(("set string %ls. Returning\n", pu16Dest + 1));
679 return VINF_SUCCESS;
680}
681
682int vboxClipboardDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
683{
684 size_t cb = sizeof(BMFILEHEADER) + cbSrc;
685 PBMFILEHEADER pFileHeader = NULL;
686 void *pvDest = NULL;
687 size_t offPixel = 0;
688
689 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
690 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
691 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
692
693 PBMINFOHEADER pBitmapInfoHeader = (PBMINFOHEADER)pvSrc;
694 /** @todo Support all the many versions of the DIB headers. */
695 if ( cbSrc < sizeof(BMINFOHEADER)
696 || RT_LE2H_U32(pBitmapInfoHeader->u32Size) < sizeof(BMINFOHEADER)
697 || RT_LE2H_U32(pBitmapInfoHeader->u32Size) != sizeof(BMINFOHEADER))
698 {
699 Log(("vboxClipboardDibToBmp: invalid or unsupported bitmap data.\n"));
700 return VERR_INVALID_PARAMETER;
701 }
702
703 offPixel = sizeof(BMFILEHEADER)
704 + RT_LE2H_U32(pBitmapInfoHeader->u32Size)
705 + RT_LE2H_U32(pBitmapInfoHeader->u32ClrUsed) * sizeof(uint32_t);
706 if (cbSrc < offPixel)
707 {
708 Log(("vboxClipboardDibToBmp: invalid bitmap data.\n"));
709 return VERR_INVALID_PARAMETER;
710 }
711
712 pvDest = RTMemAlloc(cb);
713 if (!pvDest)
714 {
715 Log(("writeToPasteboard: cannot allocate memory for bitmap.\n"));
716 return VERR_NO_MEMORY;
717 }
718
719 pFileHeader = (PBMFILEHEADER)pvDest;
720 pFileHeader->u16Type = BITMAPHEADERMAGIC;
721 pFileHeader->u32Size = (uint32_t)RT_H2LE_U32(cb);
722 pFileHeader->u16Reserved1 = pFileHeader->u16Reserved2 = 0;
723 pFileHeader->u32OffBits = (uint32_t)RT_H2LE_U32(offPixel);
724 memcpy((uint8_t *)pvDest + sizeof(BMFILEHEADER), pvSrc, cbSrc);
725 *ppvDest = pvDest;
726 *pcbDest = cb;
727 return VINF_SUCCESS;
728}
729
730int vboxClipboardBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
731{
732 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
733 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
734 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
735
736 PBMFILEHEADER pFileHeader = (PBMFILEHEADER)pvSrc;
737 if ( cbSrc < sizeof(BMFILEHEADER)
738 || pFileHeader->u16Type != BITMAPHEADERMAGIC
739 || RT_LE2H_U32(pFileHeader->u32Size) != cbSrc)
740 {
741 Log(("vboxClipboardBmpGetDib: invalid bitmap data.\n"));
742 return VERR_INVALID_PARAMETER;
743 }
744
745 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMFILEHEADER);
746 *pcbDest = cbSrc - sizeof(BMFILEHEADER);
747 return VINF_SUCCESS;
748}
749
750#ifdef LOG_ENABLED
751int VBoxClipboardDbgDumpHtml(const char *pszSrc, size_t cbSrc)
752{
753 size_t cchIgnored = 0;
754 int rc = RTStrNLenEx(pszSrc, cbSrc, &cchIgnored);
755 if (RT_SUCCESS(rc))
756 {
757 char *pszBuf = (char *)RTMemAllocZ(cbSrc + 1);
758 if (pszBuf)
759 {
760 rc = RTStrCopy(pszBuf, cbSrc + 1, (const char *)pszSrc);
761 if (RT_SUCCESS(rc))
762 {
763 for (size_t i = 0; i < cbSrc; ++i)
764 if (pszBuf[i] == '\n' || pszBuf[i] == '\r')
765 pszBuf[i] = ' ';
766 }
767 else
768 LogFunc(("Error in copying string\n"));
769 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
770 RTMemFree(pszBuf);
771 }
772 else
773 rc = VERR_NO_MEMORY;
774 }
775
776 return rc;
777}
778
779void VBoxClipboardDbgDumpData(const void *pv, size_t cb, SHCLFORMAT u32Format)
780{
781 if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
782 {
783 LogFunc(("VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT:\n"));
784 if (pv && cb)
785 LogFunc(("%ls\n", pv));
786 else
787 LogFunc(("%p %zu\n", pv, cb));
788 }
789 else if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
790 LogFunc(("VBOX_SHARED_CLIPBOARD_FMT_BITMAP\n"));
791 else if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_HTML)
792 {
793 LogFunc(("VBOX_SHARED_CLIPBOARD_FMT_HTML:\n"));
794 if (pv && cb)
795 {
796 LogFunc(("%s\n", pv));
797
798 //size_t cb = RTStrNLen(pv, );
799 char *pszBuf = (char *)RTMemAllocZ(cb + 1);
800 RTStrCopy(pszBuf, cb + 1, (const char *)pv);
801 for (size_t off = 0; off < cb; ++off)
802 {
803 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
804 pszBuf[off] = ' ';
805 }
806
807 LogFunc(("%s\n", pszBuf));
808 RTMemFree(pszBuf);
809 }
810 else
811 LogFunc(("%p %zu\n", pv, cb));
812 }
813 else
814 LogFunc(("Invalid format %02X\n", u32Format));
815}
816#endif /* LOG_ENABLED */
817
818/**
819 * Translates a Shared Clipboard host message enum to a string.
820 *
821 * @returns Message ID string name.
822 * @param uMsg The message to translate.
823 */
824const char *VBoxClipboardHostMsgToStr(uint32_t uMsg)
825{
826 switch (uMsg)
827 {
828 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT);
829 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA);
830 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS_REPORT);
831 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_TRANSFER_STATUS);
832 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_ROOT_LIST_HDR_READ);
833 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_ROOT_LIST_HDR_WRITE);
834 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_ROOT_LIST_ENTRY_READ);
835 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_ROOT_LIST_ENTRY_WRITE);
836 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_LIST_OPEN);
837 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_LIST_CLOSE);
838 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_LIST_HDR_READ);
839 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_LIST_HDR_WRITE);
840 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_LIST_ENTRY_READ);
841 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_LIST_ENTRY_WRITE);
842 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_OBJ_OPEN);
843 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_OBJ_CLOSE);
844 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_OBJ_READ);
845 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_OBJ_WRITE);
846 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_CANCEL);
847 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_HOST_MSG_URI_ERROR);
848 }
849 return "Unknown";
850}
851
852/**
853 * Translates a Shared Clipboard guest message enum to a string.
854 *
855 * @returns Message ID string name.
856 * @param uMsg The message to translate.
857 */
858const char *VBoxClipboardGuestMsgToStr(uint32_t uMsg)
859{
860 switch (uMsg)
861 {
862 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_GET_HOST_MSG_OLD);
863 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_FORMATS_REPORT);
864 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_DATA_READ);
865 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_DATA_WRITE);
866 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_CONNECT);
867 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_MSG_PEEK_NOWAIT);
868 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_MSG_PEEK_WAIT);
869 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_MSG_GET);
870 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_STATUS);
871 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_REPLY);
872 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_ROOT_LIST_HDR_READ);
873 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_ROOT_LIST_HDR_WRITE);
874 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_ROOT_LIST_ENTRY_READ);
875 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
876 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_LIST_OPEN);
877 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_LIST_CLOSE);
878 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_LIST_HDR_READ);
879 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_LIST_HDR_WRITE);
880 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_LIST_ENTRY_READ);
881 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_LIST_ENTRY_WRITE);
882 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_OBJ_OPEN);
883 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_OBJ_CLOSE);
884 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_OBJ_READ);
885 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_OBJ_WRITE);
886 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_CANCEL);
887 RT_CASE_RET_STR(VBOX_SHARED_CLIPBOARD_GUEST_FN_ERROR);
888 }
889 return "Unknown";
890}
891
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