VirtualBox

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

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

Shared Clipboard: Use NIL_SHCLEVENTID everywhere.

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