VirtualBox

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

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

Shared Clipboard/Transfers: Added IMachine::clipboardFileTransfersEnabled attribute (getter/setter) for enabling/disabling the feature. Disabled by default.

  • 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.6 KB
Line 
1/* $Id: clipboard-common.cpp 81286 2019-10-15 16:37:37Z 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 ShClPayloadAlloc(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 ShClPayloadFree(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 ShClEventCreate(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 ShClEventDestroy(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 ShClPayloadFree(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 ShClEventSourceCreate(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_SHCL_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 ShClEventSourceDestroy(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 ShClEventDestroy(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 ShClEventIDGenerate(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_SHCL_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 shclEventGet(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 ShClEventGetLast(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 shclEventPayloadDetachInternal(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 ShClEventRegister(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 (shclEventGet(pSource, uID) == NULL)
276 {
277 PSHCLEVENT pEvent
278 = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
279 if (pEvent)
280 {
281 rc = ShClEventCreate(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 ShClEventUnregister(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 = shclEventGet(pSource, uID);
319 if (pEvent)
320 {
321 LogFlowFunc(("Event %RU16\n", pEvent->uID));
322
323 RTListNodeRemove(&pEvent->Node);
324
325 ShClEventDestroy(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 ShClEventWait(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 = shclEventGet(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 shclEventPayloadDetachInternal(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 ShClEventSignal(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 = shclEventGet(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 ShClEventPayloadDetach(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
422{
423 AssertPtrReturnVoid(pSource);
424
425 LogFlowFunc(("uSource=%RU16, uEvent=%RU16\n", pSource->uID, uID));
426
427 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
428 if (pEvent)
429 {
430 shclEventPayloadDetachInternal(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 ShClUtf16GetWinSize(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 ShClUtf16LinToWin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest, size_t cwDest)
486{
487 size_t i, j;
488 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
489 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
490 {
491 LogRel(("vboxClipboardUtf16LinToWin: received an invalid pointer, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
492 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
493 }
494 if (cwSrc == 0)
495 {
496 if (cwDest == 0)
497 {
498 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
499 return VERR_BUFFER_OVERFLOW;
500 }
501 pu16Dest[0] = 0;
502 LogFlowFunc(("empty source string, returning\n"));
503 return VINF_SUCCESS;
504 }
505 /* We only take little endian Utf16 */
506 if (pwszSrc[0] == UTF16BEMARKER)
507 {
508 LogRel(("vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
509 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
510 }
511 /* Don't copy the endian marker. */
512 for (i = (pwszSrc[0] == UTF16LEMARKER ? 1 : 0), j = 0; i < cwSrc; ++i, ++j)
513 {
514 /* Don't copy the null byte, as we add it below. */
515 if (pwszSrc[i] == 0)
516 break;
517 if (j == cwDest)
518 {
519 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
520 return VERR_BUFFER_OVERFLOW;
521 }
522 if (pwszSrc[i] == LINEFEED)
523 {
524 pu16Dest[j] = CARRIAGERETURN;
525 ++j;
526 if (j == cwDest)
527 {
528 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
529 return VERR_BUFFER_OVERFLOW;
530 }
531 }
532#ifdef RT_OS_DARWIN
533 /* Check for a single carriage return (MacOS) */
534 else if (pwszSrc[i] == CARRIAGERETURN)
535 {
536 /* set cr */
537 pu16Dest[j] = CARRIAGERETURN;
538 ++j;
539 if (j == cwDest)
540 {
541 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
542 return VERR_BUFFER_OVERFLOW;
543 }
544 /* add the lf */
545 pu16Dest[j] = LINEFEED;
546 continue;
547 }
548#endif
549 pu16Dest[j] = pwszSrc[i];
550 }
551 /* Add the trailing null. */
552 if (j == cwDest)
553 {
554 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
555 return VERR_BUFFER_OVERFLOW;
556 }
557 pu16Dest[j] = 0;
558 LogFlowFunc(("rc=VINF_SUCCESS, pu16Dest=%ls\n", pu16Dest));
559 return VINF_SUCCESS;
560}
561
562int ShClUtf16GetLinSize(PRTUTF16 pwszSrc, size_t cwSrc, size_t *pcwDest)
563{
564 size_t cwDest;
565
566 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pwszSrc, cwSrc));
567 if (!VALID_PTR(pwszSrc))
568 {
569 LogRel(("vboxClipboardUtf16GetLinSize: received an invalid Utf16 string %p. Returning VERR_INVALID_PARAMETER.\n", pwszSrc));
570 AssertReturn(VALID_PTR(pwszSrc), VERR_INVALID_PARAMETER);
571 }
572 if (cwSrc == 0)
573 {
574 LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
575 *pcwDest = 0;
576 return VINF_SUCCESS;
577 }
578 /* We only take little endian Utf16 */
579 if (pwszSrc[0] == UTF16BEMARKER)
580 {
581 LogRel(("vboxClipboardUtf16GetLinSize: received a big endian Utf16 string. Returning VERR_INVALID_PARAMETER.\n"));
582 AssertReturn(pwszSrc[0] != UTF16BEMARKER, VERR_INVALID_PARAMETER);
583 }
584 /* Calculate the size of the destination text string. */
585 /* Is this Utf16 or Utf16-LE? */
586 if (pwszSrc[0] == UTF16LEMARKER)
587 cwDest = 0;
588 else
589 cwDest = 1;
590 for (size_t i = 0; i < cwSrc; ++i, ++cwDest)
591 {
592 if ( (i + 1 < cwSrc)
593 && (pwszSrc[i] == CARRIAGERETURN)
594 && (pwszSrc[i + 1] == LINEFEED))
595 {
596 ++i;
597 }
598 if (pwszSrc[i] == 0)
599 {
600 break;
601 }
602 }
603 /* Terminating zero */
604 ++cwDest;
605 LogFlowFunc(("returning %d\n", cwDest));
606 *pcwDest = cwDest;
607 return VINF_SUCCESS;
608}
609
610int ShClUtf16WinToLin(PRTUTF16 pwszSrc, size_t cwSrc, PRTUTF16 pu16Dest, size_t cwDest)
611{
612 size_t cwDestPos;
613
614 LogFlowFunc(("pwszSrc=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
615 cwSrc, pwszSrc, cwSrc, pu16Dest, cwDest));
616 /* A buffer of size 0 may not be an error, but it is not a good idea either. */
617 Assert(cwDest > 0);
618 if (!VALID_PTR(pwszSrc) || !VALID_PTR(pu16Dest))
619 {
620 LogRel(("vboxClipboardUtf16WinToLin: received an invalid ptr, pwszSrc=%p, pu16Dest=%p, returning VERR_INVALID_PARAMETER\n", pwszSrc, pu16Dest));
621 AssertReturn(VALID_PTR(pwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
622 }
623 /* We only take little endian Utf16 */
624 if (pwszSrc[0] == UTF16BEMARKER)
625 {
626 LogRel(("vboxClipboardUtf16WinToLin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
627 AssertMsgFailedReturn(("received a big endian string\n"), VERR_INVALID_PARAMETER);
628 }
629 if (cwDest == 0)
630 {
631 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
632 return VERR_BUFFER_OVERFLOW;
633 }
634 if (cwSrc == 0)
635 {
636 pu16Dest[0] = 0;
637 LogFlowFunc(("received empty string. Returning VINF_SUCCESS\n"));
638 return VINF_SUCCESS;
639 }
640 /* Prepend the Utf16 byte order marker if it is missing. */
641 if (pwszSrc[0] == UTF16LEMARKER)
642 {
643 cwDestPos = 0;
644 }
645 else
646 {
647 pu16Dest[0] = UTF16LEMARKER;
648 cwDestPos = 1;
649 }
650 for (size_t i = 0; i < cwSrc; ++i, ++cwDestPos)
651 {
652 if (pwszSrc[i] == 0)
653 {
654 break;
655 }
656 if (cwDestPos == cwDest)
657 {
658 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
659 return VERR_BUFFER_OVERFLOW;
660 }
661 if ( (i + 1 < cwSrc)
662 && (pwszSrc[i] == CARRIAGERETURN)
663 && (pwszSrc[i + 1] == LINEFEED))
664 {
665 ++i;
666 }
667 pu16Dest[cwDestPos] = pwszSrc[i];
668 }
669 /* Terminating zero */
670 if (cwDestPos == cwDest)
671 {
672 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
673 return VERR_BUFFER_OVERFLOW;
674 }
675 pu16Dest[cwDestPos] = 0;
676 LogFlowFunc(("set string %ls. Returning\n", pu16Dest + 1));
677 return VINF_SUCCESS;
678}
679
680int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
681{
682 size_t cb = sizeof(BMFILEHEADER) + cbSrc;
683 PBMFILEHEADER pFileHeader = NULL;
684 void *pvDest = NULL;
685 size_t offPixel = 0;
686
687 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
688 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
689 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
690
691 PBMINFOHEADER pBitmapInfoHeader = (PBMINFOHEADER)pvSrc;
692 /** @todo Support all the many versions of the DIB headers. */
693 if ( cbSrc < sizeof(BMINFOHEADER)
694 || RT_LE2H_U32(pBitmapInfoHeader->u32Size) < sizeof(BMINFOHEADER)
695 || RT_LE2H_U32(pBitmapInfoHeader->u32Size) != sizeof(BMINFOHEADER))
696 {
697 Log(("vboxClipboardDibToBmp: invalid or unsupported bitmap data.\n"));
698 return VERR_INVALID_PARAMETER;
699 }
700
701 offPixel = sizeof(BMFILEHEADER)
702 + RT_LE2H_U32(pBitmapInfoHeader->u32Size)
703 + RT_LE2H_U32(pBitmapInfoHeader->u32ClrUsed) * sizeof(uint32_t);
704 if (cbSrc < offPixel)
705 {
706 Log(("vboxClipboardDibToBmp: invalid bitmap data.\n"));
707 return VERR_INVALID_PARAMETER;
708 }
709
710 pvDest = RTMemAlloc(cb);
711 if (!pvDest)
712 {
713 Log(("writeToPasteboard: cannot allocate memory for bitmap.\n"));
714 return VERR_NO_MEMORY;
715 }
716
717 pFileHeader = (PBMFILEHEADER)pvDest;
718 pFileHeader->u16Type = BITMAPHEADERMAGIC;
719 pFileHeader->u32Size = (uint32_t)RT_H2LE_U32(cb);
720 pFileHeader->u16Reserved1 = pFileHeader->u16Reserved2 = 0;
721 pFileHeader->u32OffBits = (uint32_t)RT_H2LE_U32(offPixel);
722 memcpy((uint8_t *)pvDest + sizeof(BMFILEHEADER), pvSrc, cbSrc);
723 *ppvDest = pvDest;
724 *pcbDest = cb;
725 return VINF_SUCCESS;
726}
727
728int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
729{
730 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
731 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
732 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
733
734 PBMFILEHEADER pFileHeader = (PBMFILEHEADER)pvSrc;
735 if ( cbSrc < sizeof(BMFILEHEADER)
736 || pFileHeader->u16Type != BITMAPHEADERMAGIC
737 || RT_LE2H_U32(pFileHeader->u32Size) != cbSrc)
738 {
739 Log(("vboxClipboardBmpGetDib: invalid bitmap data.\n"));
740 return VERR_INVALID_PARAMETER;
741 }
742
743 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMFILEHEADER);
744 *pcbDest = cbSrc - sizeof(BMFILEHEADER);
745 return VINF_SUCCESS;
746}
747
748#ifdef LOG_ENABLED
749int ShClDbgDumpHtml(const char *pszSrc, size_t cbSrc)
750{
751 size_t cchIgnored = 0;
752 int rc = RTStrNLenEx(pszSrc, cbSrc, &cchIgnored);
753 if (RT_SUCCESS(rc))
754 {
755 char *pszBuf = (char *)RTMemAllocZ(cbSrc + 1);
756 if (pszBuf)
757 {
758 rc = RTStrCopy(pszBuf, cbSrc + 1, (const char *)pszSrc);
759 if (RT_SUCCESS(rc))
760 {
761 for (size_t i = 0; i < cbSrc; ++i)
762 if (pszBuf[i] == '\n' || pszBuf[i] == '\r')
763 pszBuf[i] = ' ';
764 }
765 else
766 LogFunc(("Error in copying string\n"));
767 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
768 RTMemFree(pszBuf);
769 }
770 else
771 rc = VERR_NO_MEMORY;
772 }
773
774 return rc;
775}
776
777void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT u32Format)
778{
779 if (u32Format & VBOX_SHCL_FMT_UNICODETEXT)
780 {
781 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
782 if (pv && cb)
783 LogFunc(("%ls\n", pv));
784 else
785 LogFunc(("%p %zu\n", pv, cb));
786 }
787 else if (u32Format & VBOX_SHCL_FMT_BITMAP)
788 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
789 else if (u32Format & VBOX_SHCL_FMT_HTML)
790 {
791 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
792 if (pv && cb)
793 {
794 LogFunc(("%s\n", pv));
795
796 //size_t cb = RTStrNLen(pv, );
797 char *pszBuf = (char *)RTMemAllocZ(cb + 1);
798 RTStrCopy(pszBuf, cb + 1, (const char *)pv);
799 for (size_t off = 0; off < cb; ++off)
800 {
801 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
802 pszBuf[off] = ' ';
803 }
804
805 LogFunc(("%s\n", pszBuf));
806 RTMemFree(pszBuf);
807 }
808 else
809 LogFunc(("%p %zu\n", pv, cb));
810 }
811 else
812 LogFunc(("Invalid format %02X\n", u32Format));
813}
814#endif /* LOG_ENABLED */
815
816/**
817 * Translates a Shared Clipboard host function number to a string.
818 *
819 * @returns Function ID string name.
820 * @param uFn The function to translate.
821 */
822const char *ShClHostFunctionToStr(uint32_t uFn)
823{
824 switch (uFn)
825 {
826 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
827 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
828 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
829 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
830 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
831 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_REGISTER);
832 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_UNREGISTER);
833 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_ATTACH);
834 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_DETACH);
835 }
836 return "Unknown";
837}
838
839/**
840 * Translates a Shared Clipboard host message enum to a string.
841 *
842 * @returns Message ID string name.
843 * @param uMsg The message to translate.
844 */
845const char *ShClHostMsgToStr(uint32_t uMsg)
846{
847 switch (uMsg)
848 {
849 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
850 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
851 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
852 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
853 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
854 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
855 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
856 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
857 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
858 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
859 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
860 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
861 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
862 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
863 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
864 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
865 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
866 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
867 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
868 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
869 }
870 return "Unknown";
871}
872
873/**
874 * Translates a Shared Clipboard guest message enum to a string.
875 *
876 * @returns Message ID string name.
877 * @param uMsg The message to translate.
878 */
879const char *ShClGuestMsgToStr(uint32_t uMsg)
880{
881 switch (uMsg)
882 {
883 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD);
884 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_FORMATS_REPORT);
885 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
886 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
887 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
888 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
889 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
890 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
891 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
892 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
893 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
894 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
895 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
896 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
897 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
898 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
899 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
900 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
901 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
902 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
903 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
904 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
905 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
906 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CANCEL);
907 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
908 }
909 return "Unknown";
910}
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