VirtualBox

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

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

SharedClipboardSvc,Vbgl: Started looking over the host message handling, adding missing locking and fixing docs. 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: 27.7 KB
Line 
1/* $Id: clipboard-common.cpp 82526 2019-12-09 21:48:53Z 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 > 0, VERR_INVALID_PARAMETER);
48
49 PSHCLEVENTPAYLOAD pPayload = (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
50 if (pPayload)
51 {
52 pPayload->pvData = RTMemDup(pvData, cbData);
53 if (pPayload->pvData)
54 {
55 pPayload->cbData = cbData;
56 pPayload->uID = uID;
57
58 *ppPayload = pPayload;
59 return VINF_SUCCESS;
60 }
61
62 RTMemFree(pPayload);
63 }
64 return VERR_NO_MEMORY;
65}
66
67/**
68 * Frees an event payload.
69 *
70 * @returns VBox status code.
71 * @param pPayload Event payload to free.
72 */
73void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload)
74{
75 if (!pPayload)
76 return;
77
78 if (pPayload->pvData)
79 {
80 Assert(pPayload->cbData);
81 RTMemFree(pPayload->pvData);
82 pPayload->pvData = NULL;
83 }
84
85 pPayload->cbData = 0;
86 pPayload->uID = UINT32_MAX;
87
88 RTMemFree(pPayload);
89}
90
91/**
92 * Creates (initializes) an event.
93 *
94 * @returns VBox status code.
95 * @param pEvent Event to initialize.
96 * @param uID Event ID to use.
97 */
98int ShClEventCreate(PSHCLEVENT pEvent, SHCLEVENTID uID)
99{
100 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
101
102 LogFlowFunc(("Event %RU32\n", uID));
103
104 int rc = RTSemEventCreate(&pEvent->hEventSem);
105 if (RT_SUCCESS(rc))
106 {
107 pEvent->uID = uID;
108 pEvent->pPayload = NULL;
109 }
110
111 return rc;
112}
113
114/**
115 * Destroys an event.
116 *
117 * @param pEvent Event to destroy.
118 */
119void ShClEventDestroy(PSHCLEVENT pEvent)
120{
121 if (!pEvent)
122 return;
123
124 LogFlowFunc(("Event %RU32\n", pEvent->uID));
125
126 if (pEvent->hEventSem != NIL_RTSEMEVENT)
127 {
128 RTSemEventDestroy(pEvent->hEventSem);
129 pEvent->hEventSem = NIL_RTSEMEVENT;
130 }
131
132 ShClPayloadFree(pEvent->pPayload);
133
134 pEvent->uID = 0;
135}
136
137/**
138 * Creates a new event source.
139 *
140 * @returns VBox status code.
141 * @param pSource Event source to create.
142 * @param uID ID to use for event source.
143 */
144int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
145{
146 LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
147 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
148
149 RTListInit(&pSource->lstEvents);
150
151 pSource->uID = uID;
152 /* Choose a random event ID starting point. */
153 pSource->uEventIDNext = RTRandU32() % VBOX_SHCL_MAX_EVENTS;
154
155 LogFlowFuncLeaveRC(VINF_SUCCESS);
156 return VINF_SUCCESS;
157}
158
159/**
160 * Destroys an event source.
161 *
162 * @param pSource Event source to destroy.
163 */
164void ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource)
165{
166 if (!pSource)
167 return;
168
169 LogFlowFunc(("ID=%RU32\n", pSource->uID));
170
171 ShClEventSourceReset(pSource);
172
173 pSource->uID = UINT16_MAX;
174 pSource->uEventIDNext = UINT32_MAX;
175}
176
177/**
178 * Resets an event source.
179 *
180 * @param pSource Event source to reset.
181 */
182void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
183{
184 LogFlowFunc(("ID=%RU32\n", pSource->uID));
185
186 PSHCLEVENT pEvIt;
187 PSHCLEVENT pEvItNext;
188 RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
189 {
190 RTListNodeRemove(&pEvIt->Node);
191
192 ShClEventDestroy(pEvIt);
193
194 RTMemFree(pEvIt);
195 pEvIt = NULL;
196 }
197}
198
199/**
200 * Generates a new event ID for a specific event source.
201 *
202 * @returns New event ID generated, or 0 on error.
203 * @param pSource Event source to generate event for.
204 */
205SHCLEVENTID ShClEventIDGenerate(PSHCLEVENTSOURCE pSource)
206{
207 AssertPtrReturn(pSource, 0);
208
209 LogFlowFunc(("uSource=%RU16: New event: %RU32\n", pSource->uID, pSource->uEventIDNext));
210
211 pSource->uEventIDNext++;
212 if (pSource->uEventIDNext == VBOX_SHCL_MAX_EVENTS)
213 pSource->uEventIDNext = 0;
214
215 return pSource->uEventIDNext;
216}
217
218/**
219 * Returns a specific event of a event source.
220 *
221 * @returns Pointer to event if found, or NULL if not found.
222 * @param pSource Event source to get event from.
223 * @param uID Event ID to get.
224 */
225inline PSHCLEVENT shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
226{
227 PSHCLEVENT pEvIt;
228 RTListForEach(&pSource->lstEvents, pEvIt, SHCLEVENT, Node)
229 {
230 if (pEvIt->uID == uID)
231 return pEvIt;
232 }
233
234 return NULL;
235}
236
237/**
238 * Returns the last (newest) event ID which has been registered for an event source.
239 *
240 * @returns Last registered event ID, or 0 if not found.
241 * @param pSource Event source to get last registered event from.
242 */
243SHCLEVENTID ShClEventGetLast(PSHCLEVENTSOURCE pSource)
244{
245 AssertPtrReturn(pSource, 0);
246 PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
247 if (pEvent)
248 return pEvent->uID;
249
250 return 0;
251}
252
253/**
254 * Detaches a payload from an event, internal version.
255 *
256 * @param pEvent Event to detach payload for.
257 */
258static void shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
259{
260 /** @todo r=bird: This should return pPayload. It should also not need
261 * assert the validity of pEvent in non-strict builds, given that this
262 * is an static + internal function, that's a complete waste of time. */
263 AssertPtrReturnVoid(pEvent);
264
265 pEvent->pPayload = NULL;
266}
267
268/**
269 * Registers an event.
270 *
271 * @returns VBox status code.
272 * @param pSource Event source to register event for.
273 * @param uID Event ID to register.
274 */
275int ShClEventRegister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
276{
277 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
278
279 int rc;
280
281 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
282
283 if (shclEventGet(pSource, uID) == NULL)
284 {
285 PSHCLEVENT pEvent
286 = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
287 if (pEvent)
288 {
289 rc = ShClEventCreate(pEvent, uID);
290 if (RT_SUCCESS(rc))
291 {
292 RTListAppend(&pSource->lstEvents, &pEvent->Node);
293
294 LogFlowFunc(("Event %RU32\n", uID));
295 }
296 }
297 else
298 rc = VERR_NO_MEMORY;
299 }
300 else
301 rc = VERR_ALREADY_EXISTS;
302
303#ifdef DEBUG_andy
304 AssertRC(rc);
305#endif
306
307 LogFlowFuncLeaveRC(rc);
308 return rc;
309}
310
311/**
312 * Unregisters an event.
313 *
314 * @returns VBox status code.
315 * @param pSource Event source to unregister event for.
316 * @param uID Event ID to unregister.
317 */
318int ShClEventUnregister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
319{
320 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
321
322 int rc;
323
324 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
325
326 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
327 if (pEvent)
328 {
329 LogFlowFunc(("Event %RU32\n", pEvent->uID));
330
331 RTListNodeRemove(&pEvent->Node);
332
333 ShClEventDestroy(pEvent);
334
335 RTMemFree(pEvent);
336 pEvent = NULL;
337
338 rc = VINF_SUCCESS;
339 }
340 else
341 rc = VERR_NOT_FOUND;
342
343 LogFlowFuncLeaveRC(rc);
344 return rc;
345}
346
347/**
348 * Waits for an event to get signalled.
349 *
350 * @returns VBox status code.
351 * @param pSource Event source that contains the event to wait for.
352 * @param uID Event ID to wait for.
353 * @param uTimeoutMs Timeout (in ms) to wait.
354 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
355 * SharedClipboardPayloadFree(). Optional.
356 */
357int ShClEventWait(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID, RTMSINTERVAL uTimeoutMs,
358 PSHCLEVENTPAYLOAD* ppPayload)
359{
360 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
361 /** ppPayload is optional. */
362
363 LogFlowFuncEnter();
364
365 int rc;
366
367 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
368 if (pEvent)
369 {
370 rc = RTSemEventWait(pEvent->hEventSem, uTimeoutMs);
371 if (RT_SUCCESS(rc))
372 {
373 if (ppPayload)
374 {
375 *ppPayload = pEvent->pPayload;
376
377 /* Make sure to detach payload here, as the caller now owns the data. */
378 shclEventPayloadDetachInternal(pEvent);
379 }
380 }
381 }
382 else
383 rc = VERR_NOT_FOUND;
384
385 LogFlowFuncLeaveRC(rc);
386 return rc;
387}
388
389/**
390 * Signals an event.
391 *
392 * @returns VBox status code.
393 * @param pSource Event source of event to signal.
394 * @param uID Event ID to signal.
395 * @param pPayload Event payload to associate. Takes ownership. Optional.
396 */
397int ShClEventSignal(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID,
398 PSHCLEVENTPAYLOAD pPayload)
399{
400 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
401
402 int rc;
403
404 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
405
406 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
407 if (pEvent)
408 {
409 Assert(pEvent->pPayload == NULL);
410
411 pEvent->pPayload = pPayload;
412
413 rc = RTSemEventSignal(pEvent->hEventSem);
414 }
415 else
416 rc = VERR_NOT_FOUND;
417
418 LogFlowFuncLeaveRC(rc);
419 return rc;
420}
421
422/**
423 * Detaches a payload from an event.
424 *
425 * @returns VBox status code.
426 * @param pSource Event source of event to detach payload for.
427 * @param uID Event ID to detach payload for.
428 */
429void ShClEventPayloadDetach(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
430{
431 /** @todo r=bird: This API is not needed, it either is a no-op as it
432 * replicates work done by ShClEventWait or it leaks the payload as
433 * ShClEventWait is the only way to get it as far as I can tell. */
434
435 AssertPtrReturnVoid(pSource);
436
437 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
438
439 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
440 if (pEvent)
441 {
442 shclEventPayloadDetachInternal(pEvent);
443 }
444#ifdef DEBUG_andy
445 else
446 AssertMsgFailed(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
447#endif
448}
449
450/** @todo Delinuxify the code (*Lin* -> *Host*); use AssertLogRel*. */
451
452int ShClUtf16GetWinSize(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pcwDest)
453{
454 size_t cwDest, i;
455
456 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pcwszSrc, cwSrc));
457 AssertLogRelMsgReturn(pcwszSrc != NULL, ("vboxClipboardUtf16GetWinSize: received a null Utf16 string, returning VERR_INVALID_PARAMETER\n"), VERR_INVALID_PARAMETER);
458 if (cwSrc == 0)
459 {
460 *pcwDest = 0;
461 LogFlowFunc(("empty source string, returning\n"));
462 return VINF_SUCCESS;
463 }
464/** @todo convert the remainder of the Assert stuff to AssertLogRel. */
465 /* We only take little endian Utf16 */
466 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
467 {
468 LogRel(("Shared Clipboard: vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
469 AssertReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER, VERR_INVALID_PARAMETER);
470 }
471 cwDest = 0;
472 /* Calculate the size of the destination text string. */
473 /* Is this Utf16 or Utf16-LE? */
474 for (i = (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0); i < cwSrc; ++i, ++cwDest)
475 {
476 /* Check for a single line feed */
477 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
478 ++cwDest;
479#ifdef RT_OS_DARWIN
480 /* Check for a single carriage return (MacOS) */
481 if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
482 ++cwDest;
483#endif
484 if (pcwszSrc[i] == 0)
485 {
486 /* Don't count this, as we do so below. */
487 break;
488 }
489 }
490 /* Count the terminating null byte. */
491 ++cwDest;
492 LogFlowFunc(("returning VINF_SUCCESS, %d 16bit words\n", cwDest));
493 *pcwDest = cwDest;
494 return VINF_SUCCESS;
495}
496
497int ShClUtf16LinToWin(PCRTUTF16 pcwszSrc, size_t cwSrc, PRTUTF16 pu16Dest, size_t cwDest)
498{
499 size_t i, j;
500 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pcwszSrc, cwSrc));
501 if (!VALID_PTR(pcwszSrc) || !VALID_PTR(pu16Dest))
502 {
503 LogRel(("Shared Clipboard: vboxClipboardUtf16LinToWin: received an invalid pointer, returning VERR_INVALID_PARAMETER\n"));
504 AssertReturn(VALID_PTR(pcwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
505 }
506 if (cwSrc == 0)
507 {
508 if (cwDest == 0)
509 {
510 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
511 return VERR_BUFFER_OVERFLOW;
512 }
513 pu16Dest[0] = 0;
514 LogFlowFunc(("empty source string, returning\n"));
515 return VINF_SUCCESS;
516 }
517 /* We only take little endian Utf16 */
518 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
519 {
520 LogRel(("Shared Clipboard: vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
521 AssertReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER, VERR_INVALID_PARAMETER);
522 }
523 /* Don't copy the endian marker. */
524 for (i = (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0), j = 0; i < cwSrc; ++i, ++j)
525 {
526 /* Don't copy the null byte, as we add it below. */
527 if (pcwszSrc[i] == 0)
528 break;
529 if (j == cwDest)
530 {
531 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
532 return VERR_BUFFER_OVERFLOW;
533 }
534 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
535 {
536 pu16Dest[j] = VBOX_SHCL_CARRIAGERETURN;
537 ++j;
538 if (j == cwDest)
539 {
540 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
541 return VERR_BUFFER_OVERFLOW;
542 }
543 }
544#ifdef RT_OS_DARWIN
545 /* Check for a single carriage return (MacOS) */
546 else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
547 {
548 /* set cr */
549 pu16Dest[j] = VBOX_SHCL_CARRIAGERETURN;
550 ++j;
551 if (j == cwDest)
552 {
553 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
554 return VERR_BUFFER_OVERFLOW;
555 }
556 /* add the lf */
557 pu16Dest[j] = VBOX_SHCL_LINEFEED;
558 continue;
559 }
560#endif
561 pu16Dest[j] = pcwszSrc[i];
562 }
563 /* Add the trailing null. */
564 if (j == cwDest)
565 {
566 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
567 return VERR_BUFFER_OVERFLOW;
568 }
569 pu16Dest[j] = 0;
570 LogFlowFunc(("rc=VINF_SUCCESS, pu16Dest=%ls\n", pu16Dest));
571 return VINF_SUCCESS;
572}
573
574int ShClUtf16GetLinSize(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pcwDest)
575{
576 size_t cwDest;
577
578 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pcwszSrc, cwSrc));
579 if (!VALID_PTR(pcwszSrc))
580 {
581 LogRel(("Shared Clipboard: vboxClipboardUtf16GetLinSize: received an invalid Utf16 string, returning VERR_INVALID_PARAMETER\n"));
582 AssertReturn(VALID_PTR(pcwszSrc), VERR_INVALID_PARAMETER);
583 }
584 if (cwSrc == 0)
585 {
586 LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
587 *pcwDest = 0;
588 return VINF_SUCCESS;
589 }
590 /* We only take little endian Utf16 */
591 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
592 {
593 LogRel(("Shared Clipboard: vboxClipboardUtf16GetLinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
594 AssertReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER, VERR_INVALID_PARAMETER);
595 }
596 /* Calculate the size of the destination text string. */
597 /* Is this Utf16 or Utf16-LE? */
598 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
599 cwDest = 0;
600 else
601 cwDest = 1;
602 for (size_t i = 0; i < cwSrc; ++i, ++cwDest)
603 {
604 if ( (i + 1 < cwSrc)
605 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
606 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
607 {
608 ++i;
609 }
610 if (pcwszSrc[i] == 0)
611 {
612 break;
613 }
614 }
615 /* Terminating zero */
616 ++cwDest;
617 LogFlowFunc(("returning %d\n", cwDest));
618 *pcwDest = cwDest;
619 return VINF_SUCCESS;
620}
621
622int ShClUtf16WinToLin(PCRTUTF16 pcwszSrc, size_t cwSrc, PRTUTF16 pu16Dest, size_t cwDest)
623{
624 size_t cwDestPos;
625
626 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
627 cwSrc, pcwszSrc, cwSrc, pu16Dest, cwDest));
628 /* A buffer of size 0 may not be an error, but it is not a good idea either. */
629 Assert(cwDest > 0);
630 if (!VALID_PTR(pcwszSrc) || !VALID_PTR(pu16Dest))
631 {
632 LogRel(("Shared Clipboard: vboxClipboardUtf16WinToLin: received an invalid pointer, returning VERR_INVALID_PARAMETER\n"));
633 AssertReturn(VALID_PTR(pcwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
634 }
635 /* We only take little endian Utf16 */
636 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
637 {
638 LogRel(("Shared Clipboard: vboxClipboardUtf16WinToLin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
639 AssertMsgFailedReturn(("received a big endian string\n"), VERR_INVALID_PARAMETER);
640 }
641 if (cwDest == 0)
642 {
643 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
644 return VERR_BUFFER_OVERFLOW;
645 }
646 if (cwSrc == 0)
647 {
648 pu16Dest[0] = 0;
649 LogFlowFunc(("received empty string. Returning VINF_SUCCESS\n"));
650 return VINF_SUCCESS;
651 }
652 /* Prepend the Utf16 byte order marker if it is missing. */
653 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
654 {
655 cwDestPos = 0;
656 }
657 else
658 {
659 pu16Dest[0] = VBOX_SHCL_UTF16LEMARKER;
660 cwDestPos = 1;
661 }
662 for (size_t i = 0; i < cwSrc; ++i, ++cwDestPos)
663 {
664 if (pcwszSrc[i] == 0)
665 {
666 break;
667 }
668 if (cwDestPos == cwDest)
669 {
670 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
671 return VERR_BUFFER_OVERFLOW;
672 }
673 if ( (i + 1 < cwSrc)
674 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
675 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
676 {
677 ++i;
678 }
679 pu16Dest[cwDestPos] = pcwszSrc[i];
680 }
681 /* Terminating zero */
682 if (cwDestPos == cwDest)
683 {
684 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
685 return VERR_BUFFER_OVERFLOW;
686 }
687 pu16Dest[cwDestPos] = 0;
688 LogFlowFunc(("set string %ls. Returning\n", pu16Dest + 1));
689 return VINF_SUCCESS;
690}
691
692int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
693{
694 size_t cb = sizeof(BMFILEHEADER) + cbSrc;
695 PBMFILEHEADER pFileHeader = NULL;
696 void *pvDest = NULL;
697 size_t offPixel = 0;
698
699 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
700 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
701 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
702
703 PBMINFOHEADER pBitmapInfoHeader = (PBMINFOHEADER)pvSrc;
704 /** @todo Support all the many versions of the DIB headers. */
705 if ( cbSrc < sizeof(BMINFOHEADER)
706 || RT_LE2H_U32(pBitmapInfoHeader->uSize) < sizeof(BMINFOHEADER)
707 || RT_LE2H_U32(pBitmapInfoHeader->uSize) != sizeof(BMINFOHEADER))
708 {
709 Log(("vboxClipboardDibToBmp: invalid or unsupported bitmap data\n"));
710 return VERR_INVALID_PARAMETER;
711 }
712
713 offPixel = sizeof(BMFILEHEADER)
714 + RT_LE2H_U32(pBitmapInfoHeader->uSize)
715 + RT_LE2H_U32(pBitmapInfoHeader->uClrUsed) * sizeof(uint32_t);
716 if (cbSrc < offPixel)
717 {
718 Log(("vboxClipboardDibToBmp: invalid bitmap data\n"));
719 return VERR_INVALID_PARAMETER;
720 }
721
722 pvDest = RTMemAlloc(cb);
723 if (!pvDest)
724 {
725 Log(("writeToPasteboard: cannot allocate memory for bitmap\n"));
726 return VERR_NO_MEMORY;
727 }
728
729 pFileHeader = (PBMFILEHEADER)pvDest;
730 pFileHeader->uType = BITMAPHEADERMAGIC;
731 pFileHeader->uSize = (uint32_t)RT_H2LE_U32(cb);
732 pFileHeader->uReserved1 = pFileHeader->uReserved2 = 0;
733 pFileHeader->uOffBits = (uint32_t)RT_H2LE_U32(offPixel);
734 memcpy((uint8_t *)pvDest + sizeof(BMFILEHEADER), pvSrc, cbSrc);
735 *ppvDest = pvDest;
736 *pcbDest = cb;
737 return VINF_SUCCESS;
738}
739
740int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
741{
742 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
743 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
744 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
745
746 PBMFILEHEADER pFileHeader = (PBMFILEHEADER)pvSrc;
747 if ( cbSrc < sizeof(BMFILEHEADER)
748 || pFileHeader->uType != BITMAPHEADERMAGIC
749 || RT_LE2H_U32(pFileHeader->uSize) != cbSrc)
750 {
751 Log(("vboxClipboardBmpGetDib: invalid bitmap data\n"));
752 return VERR_INVALID_PARAMETER;
753 }
754
755 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMFILEHEADER);
756 *pcbDest = cbSrc - sizeof(BMFILEHEADER);
757 return VINF_SUCCESS;
758}
759
760#ifdef LOG_ENABLED
761int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
762{
763 size_t cchIgnored = 0;
764 int rc = RTStrNLenEx(pcszSrc, cbSrc, &cchIgnored);
765 if (RT_SUCCESS(rc))
766 {
767 char *pszBuf = (char *)RTMemAllocZ(cbSrc + 1);
768 if (pszBuf)
769 {
770 rc = RTStrCopy(pszBuf, cbSrc + 1, (const char *)pcszSrc);
771 if (RT_SUCCESS(rc))
772 {
773 for (size_t i = 0; i < cbSrc; ++i)
774 if (pszBuf[i] == '\n' || pszBuf[i] == '\r')
775 pszBuf[i] = ' ';
776 }
777 else
778 LogFunc(("Error in copying string\n"));
779 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
780 RTMemFree(pszBuf);
781 }
782 else
783 rc = VERR_NO_MEMORY;
784 }
785
786 return rc;
787}
788
789void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
790{
791 if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
792 {
793 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
794 if (pv && cb)
795 LogFunc(("%ls\n", pv));
796 else
797 LogFunc(("%p %zu\n", pv, cb));
798 }
799 else if (uFormat & VBOX_SHCL_FMT_BITMAP)
800 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
801 else if (uFormat & VBOX_SHCL_FMT_HTML)
802 {
803 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
804 if (pv && cb)
805 {
806 LogFunc(("%s\n", pv));
807
808 //size_t cb = RTStrNLen(pv, );
809 char *pszBuf = (char *)RTMemAllocZ(cb + 1);
810 RTStrCopy(pszBuf, cb + 1, (const char *)pv);
811 for (size_t off = 0; off < cb; ++off)
812 {
813 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
814 pszBuf[off] = ' ';
815 }
816
817 LogFunc(("%s\n", pszBuf));
818 RTMemFree(pszBuf);
819 }
820 else
821 LogFunc(("%p %zu\n", pv, cb));
822 }
823 else
824 LogFunc(("Invalid format %02X\n", uFormat));
825}
826#endif /* LOG_ENABLED */
827
828/**
829 * Translates a Shared Clipboard host function number to a string.
830 *
831 * @returns Function ID string name.
832 * @param uFn The function to translate.
833 */
834const char *ShClHostFunctionToStr(uint32_t uFn)
835{
836 switch (uFn)
837 {
838 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
839 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
840 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
841 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
842 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
843 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_REGISTER);
844 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_UNREGISTER);
845 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_ATTACH);
846 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_DETACH);
847 }
848 return "Unknown";
849}
850
851/**
852 * Translates a Shared Clipboard host message enum to a string.
853 *
854 * @returns Message ID string name.
855 * @param uMsg The message to translate.
856 */
857const char *ShClHostMsgToStr(uint32_t uMsg)
858{
859 switch (uMsg)
860 {
861 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
862 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
863 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
864 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
865 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
866 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
867 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
868 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
869 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
870 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
871 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
872 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
873 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
874 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
875 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
876 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
877 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
878 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
879 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
880 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
881 }
882 return "Unknown";
883}
884
885/**
886 * Translates a Shared Clipboard guest message enum to a string.
887 *
888 * @returns Message ID string name.
889 * @param uMsg The message to translate.
890 */
891const char *ShClGuestMsgToStr(uint32_t uMsg)
892{
893 switch (uMsg)
894 {
895 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
896 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
897 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
898 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
899 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
900 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
901 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
902 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
903 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
904 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
905 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
906 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
907 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
908 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
909 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
910 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
911 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
912 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
913 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
914 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
915 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
916 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
917 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
918 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
919 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
920 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
921 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
922 }
923 return "Unknown";
924}
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