VirtualBox

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

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

Shared Clipboard: Renaming (SHAREDCLIPBOARD -> SHCL and VBOXCLIPBOARD -> SHCL).

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