VirtualBox

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

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

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