VirtualBox

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

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

Shared Clipboard/common: Resolved 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 82871 2020-01-27 12:33:01Z 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 0 on error.
199 * @param pSource Event source to generate event for.
200 */
201SHCLEVENTID ShClEventIdGenerateAndRegister(PSHCLEVENTSOURCE pSource)
202{
203 AssertPtrReturn(pSource, 0);
204
205 /*
206 * Allocate an event.
207 */
208 PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
209 AssertReturn(pEvent, 0);
210 int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
211 AssertRCReturnStmt(rc, RTMemFree(pEvent), 0);
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 0;
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 * Signals an event.
353 *
354 * @returns VBox status code.
355 * @param pSource Event source of event to signal.
356 * @param uID Event ID to signal.
357 * @param pPayload Event payload to associate. Takes ownership. Optional.
358 *
359 * @note Caller must enter crit sect protecting the event source!
360 */
361int ShClEventSignal(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID,
362 PSHCLEVENTPAYLOAD pPayload)
363{
364 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
365
366 int rc;
367
368 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
369
370 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
371 if (pEvent)
372 {
373 Assert(pEvent->pPayload == NULL);
374
375 pEvent->pPayload = pPayload;
376
377 rc = RTSemEventMultiSignal(pEvent->hEvtMulSem);
378 }
379 else
380 rc = VERR_NOT_FOUND;
381
382 LogFlowFuncLeaveRC(rc);
383 return rc;
384}
385
386/**
387 * Detaches a payload from an event.
388 *
389 * @returns VBox status code.
390 * @param pSource Event source of event to detach payload for.
391 * @param uID Event ID to detach payload for.
392 */
393void ShClEventPayloadDetach(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
394{
395 /** @todo r=bird: This API is not needed, it either is a no-op as it
396 * replicates work done by ShClEventWait or it leaks the payload as
397 * ShClEventWait is the only way to get it as far as I can tell. */
398
399 AssertPtrReturnVoid(pSource);
400
401 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
402
403 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
404 if (pEvent)
405 {
406 shclEventPayloadDetachInternal(pEvent);
407 }
408#ifdef DEBUG_andy
409 else
410 AssertMsgFailed(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
411#endif
412}
413
414/** @todo Delinuxify the code (*Lin* -> *Host*); use AssertLogRel*. */
415
416int ShClUtf16GetWinSize(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pcwDest)
417{
418 size_t cwDest, i;
419
420 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pcwszSrc, cwSrc));
421 AssertLogRelMsgReturn(pcwszSrc != NULL, ("vboxClipboardUtf16GetWinSize: received a null Utf16 string, returning VERR_INVALID_PARAMETER\n"), VERR_INVALID_PARAMETER);
422 if (cwSrc == 0)
423 {
424 *pcwDest = 0;
425 LogFlowFunc(("empty source string, returning\n"));
426 return VINF_SUCCESS;
427 }
428/** @todo convert the remainder of the Assert stuff to AssertLogRel. */
429 /* We only take little endian Utf16 */
430 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
431 {
432 LogRel(("Shared Clipboard: vboxClipboardUtf16GetWinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
433 AssertReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER, VERR_INVALID_PARAMETER);
434 }
435 cwDest = 0;
436 /* Calculate the size of the destination text string. */
437 /* Is this Utf16 or Utf16-LE? */
438 for (i = (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0); i < cwSrc; ++i, ++cwDest)
439 {
440 /* Check for a single line feed */
441 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
442 ++cwDest;
443#ifdef RT_OS_DARWIN
444 /* Check for a single carriage return (MacOS) */
445 if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
446 ++cwDest;
447#endif
448 if (pcwszSrc[i] == 0)
449 {
450 /* Don't count this, as we do so below. */
451 break;
452 }
453 }
454 /* Count the terminating null byte. */
455 ++cwDest;
456 LogFlowFunc(("returning VINF_SUCCESS, %d 16bit words\n", cwDest));
457 *pcwDest = cwDest;
458 return VINF_SUCCESS;
459}
460
461int ShClUtf16LinToWin(PCRTUTF16 pcwszSrc, size_t cwSrc, PRTUTF16 pu16Dest, size_t cwDest)
462{
463 size_t i, j;
464 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pcwszSrc, cwSrc));
465 if (!VALID_PTR(pcwszSrc) || !VALID_PTR(pu16Dest))
466 {
467 LogRel(("Shared Clipboard: vboxClipboardUtf16LinToWin: received an invalid pointer, returning VERR_INVALID_PARAMETER\n"));
468 AssertReturn(VALID_PTR(pcwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
469 }
470 if (cwSrc == 0)
471 {
472 if (cwDest == 0)
473 {
474 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
475 return VERR_BUFFER_OVERFLOW;
476 }
477 pu16Dest[0] = 0;
478 LogFlowFunc(("empty source string, returning\n"));
479 return VINF_SUCCESS;
480 }
481 /* We only take little endian Utf16 */
482 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
483 {
484 LogRel(("Shared Clipboard: vboxClipboardUtf16LinToWin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
485 AssertReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER, VERR_INVALID_PARAMETER);
486 }
487 /* Don't copy the endian marker. */
488 for (i = (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0), j = 0; i < cwSrc; ++i, ++j)
489 {
490 /* Don't copy the null byte, as we add it below. */
491 if (pcwszSrc[i] == 0)
492 break;
493 if (j == cwDest)
494 {
495 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
496 return VERR_BUFFER_OVERFLOW;
497 }
498 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
499 {
500 pu16Dest[j] = VBOX_SHCL_CARRIAGERETURN;
501 ++j;
502 if (j == cwDest)
503 {
504 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
505 return VERR_BUFFER_OVERFLOW;
506 }
507 }
508#ifdef RT_OS_DARWIN
509 /* Check for a single carriage return (MacOS) */
510 else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
511 {
512 /* set cr */
513 pu16Dest[j] = VBOX_SHCL_CARRIAGERETURN;
514 ++j;
515 if (j == cwDest)
516 {
517 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
518 return VERR_BUFFER_OVERFLOW;
519 }
520 /* add the lf */
521 pu16Dest[j] = VBOX_SHCL_LINEFEED;
522 continue;
523 }
524#endif
525 pu16Dest[j] = pcwszSrc[i];
526 }
527 /* Add the trailing null. */
528 if (j == cwDest)
529 {
530 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
531 return VERR_BUFFER_OVERFLOW;
532 }
533 pu16Dest[j] = 0;
534 LogFlowFunc(("rc=VINF_SUCCESS, pu16Dest=%ls\n", pu16Dest));
535 return VINF_SUCCESS;
536}
537
538int ShClUtf16GetLinSize(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pcwDest)
539{
540 size_t cwDest;
541
542 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u\n", cwSrc, pcwszSrc, cwSrc));
543 if (!VALID_PTR(pcwszSrc))
544 {
545 LogRel(("Shared Clipboard: vboxClipboardUtf16GetLinSize: received an invalid Utf16 string, returning VERR_INVALID_PARAMETER\n"));
546 AssertReturn(VALID_PTR(pcwszSrc), VERR_INVALID_PARAMETER);
547 }
548 if (cwSrc == 0)
549 {
550 LogFlowFunc(("empty source string, returning VINF_SUCCESS\n"));
551 *pcwDest = 0;
552 return VINF_SUCCESS;
553 }
554 /* We only take little endian Utf16 */
555 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
556 {
557 LogRel(("Shared Clipboard: vboxClipboardUtf16GetLinSize: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
558 AssertReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER, VERR_INVALID_PARAMETER);
559 }
560 /* Calculate the size of the destination text string. */
561 /* Is this Utf16 or Utf16-LE? */
562 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
563 cwDest = 0;
564 else
565 cwDest = 1;
566 for (size_t i = 0; i < cwSrc; ++i, ++cwDest)
567 {
568 if ( (i + 1 < cwSrc)
569 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
570 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
571 {
572 ++i;
573 }
574 if (pcwszSrc[i] == 0)
575 {
576 break;
577 }
578 }
579 /* Terminating zero */
580 ++cwDest;
581 LogFlowFunc(("returning %d\n", cwDest));
582 *pcwDest = cwDest;
583 return VINF_SUCCESS;
584}
585
586int ShClUtf16WinToLin(PCRTUTF16 pcwszSrc, size_t cwSrc, PRTUTF16 pu16Dest, size_t cwDest)
587{
588 size_t cwDestPos;
589
590 LogFlowFunc(("pcwszSrc=%.*ls, cwSrc=%u, pu16Dest=%p, cwDest=%u\n",
591 cwSrc, pcwszSrc, cwSrc, pu16Dest, cwDest));
592 /* A buffer of size 0 may not be an error, but it is not a good idea either. */
593 Assert(cwDest > 0);
594 if (!VALID_PTR(pcwszSrc) || !VALID_PTR(pu16Dest))
595 {
596 LogRel(("Shared Clipboard: vboxClipboardUtf16WinToLin: received an invalid pointer, returning VERR_INVALID_PARAMETER\n"));
597 AssertReturn(VALID_PTR(pcwszSrc) && VALID_PTR(pu16Dest), VERR_INVALID_PARAMETER);
598 }
599 /* We only take little endian Utf16 */
600 if (pcwszSrc[0] == VBOX_SHCL_UTF16BEMARKER)
601 {
602 LogRel(("Shared Clipboard: vboxClipboardUtf16WinToLin: received a big endian Utf16 string, returning VERR_INVALID_PARAMETER\n"));
603 AssertMsgFailedReturn(("received a big endian string\n"), VERR_INVALID_PARAMETER);
604 }
605 if (cwDest == 0)
606 {
607 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
608 return VERR_BUFFER_OVERFLOW;
609 }
610 if (cwSrc == 0)
611 {
612 pu16Dest[0] = 0;
613 LogFlowFunc(("received empty string. Returning VINF_SUCCESS\n"));
614 return VINF_SUCCESS;
615 }
616 /* Prepend the Utf16 byte order marker if it is missing. */
617 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
618 {
619 cwDestPos = 0;
620 }
621 else
622 {
623 pu16Dest[0] = VBOX_SHCL_UTF16LEMARKER;
624 cwDestPos = 1;
625 }
626 for (size_t i = 0; i < cwSrc; ++i, ++cwDestPos)
627 {
628 if (pcwszSrc[i] == 0)
629 {
630 break;
631 }
632 if (cwDestPos == cwDest)
633 {
634 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
635 return VERR_BUFFER_OVERFLOW;
636 }
637 if ( (i + 1 < cwSrc)
638 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
639 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
640 {
641 ++i;
642 }
643 pu16Dest[cwDestPos] = pcwszSrc[i];
644 }
645 /* Terminating zero */
646 if (cwDestPos == cwDest)
647 {
648 LogFlowFunc(("returning VERR_BUFFER_OVERFLOW\n"));
649 return VERR_BUFFER_OVERFLOW;
650 }
651 pu16Dest[cwDestPos] = 0;
652 LogFlowFunc(("set string %ls. Returning\n", pu16Dest + 1));
653 return VINF_SUCCESS;
654}
655
656int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
657{
658 size_t cb = sizeof(BMFILEHEADER) + cbSrc;
659 PBMFILEHEADER pFileHeader = NULL;
660 void *pvDest = NULL;
661 size_t offPixel = 0;
662
663 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
664 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
665 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
666
667 PBMINFOHEADER pBitmapInfoHeader = (PBMINFOHEADER)pvSrc;
668 /** @todo Support all the many versions of the DIB headers. */
669 if ( cbSrc < sizeof(BMINFOHEADER)
670 || RT_LE2H_U32(pBitmapInfoHeader->uSize) < sizeof(BMINFOHEADER)
671 || RT_LE2H_U32(pBitmapInfoHeader->uSize) != sizeof(BMINFOHEADER))
672 {
673 Log(("vboxClipboardDibToBmp: invalid or unsupported bitmap data\n"));
674 return VERR_INVALID_PARAMETER;
675 }
676
677 offPixel = sizeof(BMFILEHEADER)
678 + RT_LE2H_U32(pBitmapInfoHeader->uSize)
679 + RT_LE2H_U32(pBitmapInfoHeader->uClrUsed) * sizeof(uint32_t);
680 if (cbSrc < offPixel)
681 {
682 Log(("vboxClipboardDibToBmp: invalid bitmap data\n"));
683 return VERR_INVALID_PARAMETER;
684 }
685
686 pvDest = RTMemAlloc(cb);
687 if (!pvDest)
688 {
689 Log(("writeToPasteboard: cannot allocate memory for bitmap\n"));
690 return VERR_NO_MEMORY;
691 }
692
693 pFileHeader = (PBMFILEHEADER)pvDest;
694 pFileHeader->uType = BITMAPHEADERMAGIC;
695 pFileHeader->uSize = (uint32_t)RT_H2LE_U32(cb);
696 pFileHeader->uReserved1 = pFileHeader->uReserved2 = 0;
697 pFileHeader->uOffBits = (uint32_t)RT_H2LE_U32(offPixel);
698 memcpy((uint8_t *)pvDest + sizeof(BMFILEHEADER), pvSrc, cbSrc);
699 *ppvDest = pvDest;
700 *pcbDest = cb;
701 return VINF_SUCCESS;
702}
703
704int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
705{
706 AssertPtrReturn(pvSrc, VERR_INVALID_PARAMETER);
707 AssertPtrReturn(ppvDest, VERR_INVALID_PARAMETER);
708 AssertPtrReturn(pcbDest, VERR_INVALID_PARAMETER);
709
710 PBMFILEHEADER pFileHeader = (PBMFILEHEADER)pvSrc;
711 if ( cbSrc < sizeof(BMFILEHEADER)
712 || pFileHeader->uType != BITMAPHEADERMAGIC
713 || RT_LE2H_U32(pFileHeader->uSize) != cbSrc)
714 {
715 Log(("vboxClipboardBmpGetDib: invalid bitmap data\n"));
716 return VERR_INVALID_PARAMETER;
717 }
718
719 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMFILEHEADER);
720 *pcbDest = cbSrc - sizeof(BMFILEHEADER);
721 return VINF_SUCCESS;
722}
723
724#ifdef LOG_ENABLED
725int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
726{
727 size_t cchIgnored = 0;
728 int rc = RTStrNLenEx(pcszSrc, cbSrc, &cchIgnored);
729 if (RT_SUCCESS(rc))
730 {
731 char *pszBuf = (char *)RTMemAllocZ(cbSrc + 1);
732 if (pszBuf)
733 {
734 rc = RTStrCopy(pszBuf, cbSrc + 1, (const char *)pcszSrc);
735 if (RT_SUCCESS(rc))
736 {
737 for (size_t i = 0; i < cbSrc; ++i)
738 if (pszBuf[i] == '\n' || pszBuf[i] == '\r')
739 pszBuf[i] = ' ';
740 }
741 else
742 LogFunc(("Error in copying string\n"));
743 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
744 RTMemFree(pszBuf);
745 }
746 else
747 rc = VERR_NO_MEMORY;
748 }
749
750 return rc;
751}
752
753void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
754{
755 if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
756 {
757 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
758 if (pv && cb)
759 LogFunc(("%ls\n", pv));
760 else
761 LogFunc(("%p %zu\n", pv, cb));
762 }
763 else if (uFormat & VBOX_SHCL_FMT_BITMAP)
764 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
765 else if (uFormat & VBOX_SHCL_FMT_HTML)
766 {
767 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
768 if (pv && cb)
769 {
770 LogFunc(("%s\n", pv));
771
772 //size_t cb = RTStrNLen(pv, );
773 char *pszBuf = (char *)RTMemAllocZ(cb + 1);
774 RTStrCopy(pszBuf, cb + 1, (const char *)pv);
775 for (size_t off = 0; off < cb; ++off)
776 {
777 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
778 pszBuf[off] = ' ';
779 }
780
781 LogFunc(("%s\n", pszBuf));
782 RTMemFree(pszBuf);
783 }
784 else
785 LogFunc(("%p %zu\n", pv, cb));
786 }
787 else
788 LogFunc(("Invalid format %02X\n", uFormat));
789}
790#endif /* LOG_ENABLED */
791
792/**
793 * Translates a Shared Clipboard host function number to a string.
794 *
795 * @returns Function ID string name.
796 * @param uFn The function to translate.
797 */
798const char *ShClHostFunctionToStr(uint32_t uFn)
799{
800 switch (uFn)
801 {
802 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
803 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
804 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
805 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
806 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
807 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_REGISTER);
808 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_UNREGISTER);
809 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_ATTACH);
810 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_DETACH);
811 }
812 return "Unknown";
813}
814
815/**
816 * Translates a Shared Clipboard host message enum to a string.
817 *
818 * @returns Message ID string name.
819 * @param uMsg The message to translate.
820 */
821const char *ShClHostMsgToStr(uint32_t uMsg)
822{
823 switch (uMsg)
824 {
825 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
826 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
827 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
828 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
829 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
830 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
831 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
832 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
833 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
834 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
835 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
836 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
837 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
838 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
839 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
840 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
841 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
842 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
843 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
844 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
845 }
846 return "Unknown";
847}
848
849/**
850 * Translates a Shared Clipboard guest message enum to a string.
851 *
852 * @returns Message ID string name.
853 * @param uMsg The message to translate.
854 */
855const char *ShClGuestMsgToStr(uint32_t uMsg)
856{
857 switch (uMsg)
858 {
859 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
860 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
861 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
862 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
863 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
864 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
865 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
866 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
867 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
868 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
869 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
870 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
871 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
872 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
873 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
874 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
875 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
876 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
877 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
878 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
879 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
880 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
881 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
882 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
883 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
884 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
885 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
886 }
887 return "Unknown";
888}
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