VirtualBox

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

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

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