VirtualBox

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

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

Shared Clipboard: Resolved some more @todos.

  • 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.2 KB
Line 
1/* $Id: clipboard-common.cpp 82280 2019-11-29 11:48:59Z 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 %RU32\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 %RU32\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=%RU32\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=%RU32\n", pSource->uID));
177
178 ShClEventSourceReset(pSource);
179
180 pSource->uID = 0;
181 pSource->uEventIDNext = 0;
182}
183
184/**
185 * Resets an event source.
186 *
187 * @param pSource Event source to reset.
188 */
189void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
190{
191 LogFlowFunc(("ID=%RU32\n", pSource->uID));
192
193 PSHCLEVENT pEvIt;
194 PSHCLEVENT pEvItNext;
195 RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
196 {
197 RTListNodeRemove(&pEvIt->Node);
198
199 ShClEventDestroy(pEvIt);
200
201 RTMemFree(pEvIt);
202 pEvIt = NULL;
203 }
204}
205
206/**
207 * Generates a new event ID for a specific event source.
208 *
209 * @returns New event ID generated, or 0 on error.
210 * @param pSource Event source to generate event for.
211 */
212SHCLEVENTID ShClEventIDGenerate(PSHCLEVENTSOURCE pSource)
213{
214 AssertPtrReturn(pSource, 0);
215
216 LogFlowFunc(("uSource=%RU16: New event: %RU32\n", pSource->uID, pSource->uEventIDNext));
217
218 pSource->uEventIDNext++;
219 if (pSource->uEventIDNext == VBOX_SHCL_MAX_EVENTS)
220 pSource->uEventIDNext = 0;
221
222 return pSource->uEventIDNext;
223}
224
225/**
226 * Returns a specific event of a event source.
227 *
228 * @returns Pointer to event if found, or NULL if not found.
229 * @param pSource Event source to get event from.
230 * @param uID Event ID to get.
231 */
232inline PSHCLEVENT shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
233{
234 PSHCLEVENT pEvIt;
235 RTListForEach(&pSource->lstEvents, pEvIt, SHCLEVENT, Node)
236 {
237 if (pEvIt->uID == uID)
238 return pEvIt;
239 }
240
241 return NULL;
242}
243
244/**
245 * Returns the last (newest) event ID which has been registered for an event source.
246 *
247 * @returns Last registered event ID, or 0 if not found.
248 * @param pSource Event source to get last registered event from.
249 */
250SHCLEVENTID ShClEventGetLast(PSHCLEVENTSOURCE pSource)
251{
252 AssertPtrReturn(pSource, 0);
253 PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
254 if (pEvent)
255 return pEvent->uID;
256
257 return 0;
258}
259
260/**
261 * Detaches a payload from an event, internal version.
262 *
263 * @param pEvent Event to detach payload for.
264 */
265static void shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
266{
267 AssertPtrReturnVoid(pEvent);
268
269 pEvent->pPayload = NULL;
270}
271
272/**
273 * Registers an event.
274 *
275 * @returns VBox status code.
276 * @param pSource Event source to register event for.
277 * @param uID Event ID to register.
278 */
279int ShClEventRegister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
280{
281 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
282
283 int rc;
284
285 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
286
287 if (shclEventGet(pSource, uID) == NULL)
288 {
289 PSHCLEVENT pEvent
290 = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
291 if (pEvent)
292 {
293 rc = ShClEventCreate(pEvent, uID);
294 if (RT_SUCCESS(rc))
295 {
296 RTListAppend(&pSource->lstEvents, &pEvent->Node);
297
298 LogFlowFunc(("Event %RU32\n", uID));
299 }
300 }
301 else
302 rc = VERR_NO_MEMORY;
303 }
304 else
305 rc = VERR_ALREADY_EXISTS;
306
307#ifdef DEBUG_andy
308 AssertRC(rc);
309#endif
310
311 LogFlowFuncLeaveRC(rc);
312 return rc;
313}
314
315/**
316 * Unregisters an event.
317 *
318 * @returns VBox status code.
319 * @param pSource Event source to unregister event for.
320 * @param uID Event ID to unregister.
321 */
322int ShClEventUnregister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
323{
324 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
325
326 int rc;
327
328 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
329
330 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
331 if (pEvent)
332 {
333 LogFlowFunc(("Event %RU32\n", pEvent->uID));
334
335 RTListNodeRemove(&pEvent->Node);
336
337 ShClEventDestroy(pEvent);
338
339 RTMemFree(pEvent);
340 pEvent = NULL;
341
342 rc = VINF_SUCCESS;
343 }
344 else
345 rc = VERR_NOT_FOUND;
346
347 LogFlowFuncLeaveRC(rc);
348 return rc;
349}
350
351/**
352 * Waits for an event to get signalled.
353 *
354 * @returns VBox status code.
355 * @param pSource Event source that contains the event to wait for.
356 * @param uID Event ID to wait for.
357 * @param uTimeoutMs Timeout (in ms) to wait.
358 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
359 * SharedClipboardPayloadFree(). Optional.
360 */
361int ShClEventWait(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID, RTMSINTERVAL uTimeoutMs,
362 PSHCLEVENTPAYLOAD* ppPayload)
363{
364 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
365 /** ppPayload is optional. */
366
367 LogFlowFuncEnter();
368
369 int rc;
370
371 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
372 if (pEvent)
373 {
374 rc = RTSemEventWait(pEvent->hEventSem, uTimeoutMs);
375 if (RT_SUCCESS(rc))
376 {
377 if (ppPayload)
378 {
379 *ppPayload = pEvent->pPayload;
380
381 /* Make sure to detach payload here, as the caller now owns the data. */
382 shclEventPayloadDetachInternal(pEvent);
383 }
384 }
385 }
386 else
387 rc = VERR_NOT_FOUND;
388
389 LogFlowFuncLeaveRC(rc);
390 return rc;
391}
392
393/**
394 * Signals an event.
395 *
396 * @returns VBox status code.
397 * @param pSource Event source of event to signal.
398 * @param uID Event ID to signal.
399 * @param pPayload Event payload to associate. Takes ownership. Optional.
400 */
401int ShClEventSignal(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID,
402 PSHCLEVENTPAYLOAD pPayload)
403{
404 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
405
406 int rc;
407
408 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
409
410 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
411 if (pEvent)
412 {
413 Assert(pEvent->pPayload == NULL);
414
415 pEvent->pPayload = pPayload;
416
417 rc = RTSemEventSignal(pEvent->hEventSem);
418 }
419 else
420 rc = VERR_NOT_FOUND;
421
422 LogFlowFuncLeaveRC(rc);
423 return rc;
424}
425
426/**
427 * Detaches a payload from an event.
428 *
429 * @returns VBox status code.
430 * @param pSource Event source of event to detach payload for.
431 * @param uID Event ID to detach payload for.
432 */
433void ShClEventPayloadDetach(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
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_GET_HOST_MSG_OLD);
896 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_FORMATS_REPORT);
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_REPLY);
906 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
907 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
908 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
909 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
910 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
911 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
912 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
913 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
914 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
915 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
916 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
917 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
918 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
919 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
920 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CANCEL);
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