VirtualBox

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

Last change on this file since 85829 was 85829, checked in by vboxsync, 4 years ago

Shared Clipboard/X11: Major cleanup for string conversion functions. Not fully tested yet [Windows build fix].

  • Property eol-style set to native
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 31.8 KB
Line 
1/* $Id: clipboard-common.cpp 85829 2020-08-19 09:36:24Z 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-2020 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#include <iprt/utf16.h>
28
29#include <iprt/errcore.h>
30#include <VBox/log.h>
31#include <VBox/GuestHost/clipboard-helper.h>
32#include <VBox/HostServices/VBoxClipboardSvc.h>
33
34
35/**
36 * Allocates a new event payload.
37 *
38 * @returns VBox status code.
39 * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
40 * @param pvData Data block to associate to this payload.
41 * @param cbData Size (in bytes) of data block to associate.
42 * @param ppPayload Where to store the allocated event payload on success.
43 */
44int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData,
45 PSHCLEVENTPAYLOAD *ppPayload)
46{
47 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
48 AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
49
50 PSHCLEVENTPAYLOAD pPayload = (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
51 if (pPayload)
52 {
53 pPayload->pvData = RTMemDup(pvData, cbData);
54 if (pPayload->pvData)
55 {
56 pPayload->cbData = cbData;
57 pPayload->uID = uID;
58
59 *ppPayload = pPayload;
60 return VINF_SUCCESS;
61 }
62
63 RTMemFree(pPayload);
64 }
65 return VERR_NO_MEMORY;
66}
67
68/**
69 * Frees an event payload.
70 *
71 * @returns VBox status code.
72 * @param pPayload Event payload to free.
73 */
74void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload)
75{
76 if (!pPayload)
77 return;
78
79 if (pPayload->pvData)
80 {
81 Assert(pPayload->cbData);
82 RTMemFree(pPayload->pvData);
83 pPayload->pvData = NULL;
84 }
85
86 pPayload->cbData = 0;
87 pPayload->uID = UINT32_MAX;
88
89 RTMemFree(pPayload);
90}
91
92/**
93 * Destroys an event, but doesn't free the memory.
94 *
95 * @param pEvent Event to destroy.
96 */
97static void shClEventTerm(PSHCLEVENT pEvent)
98{
99 if (!pEvent)
100 return;
101
102 AssertMsgReturnVoid(pEvent->cRefs == 0, ("Event %RU32 still has %RU32 references\n",
103 pEvent->idEvent, pEvent->cRefs));
104
105 LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
106
107 if (pEvent->hEvtMulSem != NIL_RTSEMEVENT)
108 {
109 RTSemEventMultiDestroy(pEvent->hEvtMulSem);
110 pEvent->hEvtMulSem = NIL_RTSEMEVENT;
111 }
112
113 ShClPayloadFree(pEvent->pPayload);
114
115 pEvent->idEvent = 0;
116}
117
118/**
119 * Creates a new event source.
120 *
121 * @returns VBox status code.
122 * @param pSource Event source to create.
123 * @param uID ID to use for event source.
124 */
125int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
126{
127 LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
128 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
129
130 RTListInit(&pSource->lstEvents);
131
132 pSource->uID = uID;
133 /* Choose a random event ID starting point. */
134 pSource->idNextEvent = RTRandU32Ex(1, VBOX_SHCL_MAX_EVENTS - 1);
135
136 LogFlowFuncLeaveRC(VINF_SUCCESS);
137 return VINF_SUCCESS;
138}
139
140/**
141 * Destroys an event source.
142 *
143 * @param pSource Event source to destroy.
144 */
145void ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource)
146{
147 if (!pSource)
148 return;
149
150 LogFlowFunc(("ID=%RU32\n", pSource->uID));
151
152 ShClEventSourceReset(pSource);
153
154 pSource->uID = UINT16_MAX;
155 pSource->idNextEvent = UINT32_MAX;
156}
157
158/**
159 * Resets an event source.
160 *
161 * @param pSource Event source to reset.
162 */
163void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
164{
165 LogFlowFunc(("ID=%RU32\n", pSource->uID));
166
167 PSHCLEVENT pEvIt;
168 PSHCLEVENT pEvItNext;
169 RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
170 {
171 RTListNodeRemove(&pEvIt->Node);
172
173 shClEventTerm(pEvIt);
174
175 RTMemFree(pEvIt);
176 pEvIt = NULL;
177 }
178}
179
180/**
181 * Returns a specific event of a event source.
182 *
183 * @returns Pointer to event if found, or NULL if not found.
184 * @param pSource Event source to get event from.
185 * @param uID Event ID to get.
186 */
187DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
188{
189 PSHCLEVENT pEvent;
190 RTListForEach(&pSource->lstEvents, pEvent, SHCLEVENT, Node)
191 {
192 if (pEvent->idEvent == idEvent)
193 return pEvent;
194 }
195
196 return NULL;
197}
198
199/**
200 * Generates a new event ID for a specific event source and registers it.
201 *
202 * @returns New event ID generated, or NIL_SHCLEVENTID on error.
203 * @param pSource Event source to generate event for.
204 */
205SHCLEVENTID ShClEventIdGenerateAndRegister(PSHCLEVENTSOURCE pSource)
206{
207 AssertPtrReturn(pSource, NIL_SHCLEVENTID);
208
209 /*
210 * Allocate an event.
211 */
212 PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
213 AssertReturn(pEvent, NIL_SHCLEVENTID);
214 int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
215 AssertRCReturnStmt(rc, RTMemFree(pEvent), NIL_SHCLEVENTID);
216
217 /*
218 * Allocate an unique event ID.
219 */
220 for (uint32_t cTries = 0;; cTries++)
221 {
222 SHCLEVENTID idEvent = ++pSource->idNextEvent;
223 if (idEvent < VBOX_SHCL_MAX_EVENTS)
224 { /* likely */ }
225 else
226 pSource->idNextEvent = idEvent = 1; /* zero == error, remember! */
227
228 if (shclEventGet(pSource, idEvent) == NULL)
229 {
230 pEvent->idEvent = idEvent;
231 RTListAppend(&pSource->lstEvents, &pEvent->Node);
232
233 LogFlowFunc(("uSource=%RU16: New event: %#x\n", pSource->uID, idEvent));
234 return idEvent;
235 }
236
237 AssertBreak(cTries < 4096);
238 }
239
240 AssertMsgFailed(("Unable to register a new event ID for event source %RU16\n", pSource->uID));
241
242 RTMemFree(pEvent);
243 return NIL_SHCLEVENTID;
244}
245
246/**
247 * Returns the last (newest) event ID which has been registered for an event source.
248 *
249 * @returns Last registered event ID, or 0 if not found.
250 * @param pSource Event source to get last registered event from.
251 */
252SHCLEVENTID ShClEventGetLast(PSHCLEVENTSOURCE pSource)
253{
254 AssertPtrReturn(pSource, 0);
255 PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
256 if (pEvent)
257 return pEvent->idEvent;
258
259 return 0;
260}
261
262/**
263 * Detaches a payload from an event, internal version.
264 *
265 * @param pEvent Event to detach payload for.
266 */
267static void shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
268{
269 /** @todo r=bird: This should return pPayload. It should also not need
270 * assert the validity of pEvent in non-strict builds, given that this
271 * is an static + internal function, that's a complete waste of time. */
272 AssertPtrReturnVoid(pEvent);
273
274 pEvent->pPayload = NULL;
275}
276
277/**
278 * Unregisters an event.
279 *
280 * @returns VBox status code.
281 * @param pSource Event source to unregister event for.
282 * @param uID Event ID to unregister.
283 */
284int ShClEventUnregister(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
285{
286 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
287
288 int rc;
289
290 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
291
292 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
293 if (pEvent)
294 {
295 LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
296
297 RTListNodeRemove(&pEvent->Node);
298
299 shClEventTerm(pEvent);
300
301 RTMemFree(pEvent);
302 pEvent = NULL;
303
304 rc = VINF_SUCCESS;
305 }
306 else
307 rc = VERR_NOT_FOUND;
308
309 LogFlowFuncLeaveRC(rc);
310 return rc;
311}
312
313/**
314 * Waits for an event to get signalled.
315 *
316 * @returns VBox status code.
317 * @param pSource Event source that contains the event to wait for.
318 * @param uID Event ID to wait for.
319 * @param uTimeoutMs Timeout (in ms) to wait.
320 * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
321 * SharedClipboardPayloadFree(). Optional.
322 */
323int ShClEventWait(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID, RTMSINTERVAL uTimeoutMs,
324 PSHCLEVENTPAYLOAD* ppPayload)
325{
326 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
327 /** ppPayload is optional. */
328
329 LogFlowFuncEnter();
330
331 int rc;
332
333 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
334 if (pEvent)
335 {
336 rc = RTSemEventMultiWait(pEvent->hEvtMulSem, uTimeoutMs);
337 if (RT_SUCCESS(rc))
338 {
339 if (ppPayload)
340 {
341 *ppPayload = pEvent->pPayload;
342
343 /* Make sure to detach payload here, as the caller now owns the data. */
344 shclEventPayloadDetachInternal(pEvent);
345 }
346 }
347 }
348 else
349 rc = VERR_NOT_FOUND;
350
351 LogFlowFuncLeaveRC(rc);
352 return rc;
353}
354
355/**
356 * Retains an event by increasing its reference count.
357 *
358 * @returns New reference count, or UINT32_MAX if failed.
359 * @param pSource Event source of event to retain.
360 * @param idEvent ID of event to retain.
361 */
362uint32_t ShClEventRetain(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
363{
364 PSHCLEVENT pEvent = shclEventGet(pSource, idEvent);
365 if (!pEvent)
366 {
367 AssertFailed();
368 return UINT32_MAX;
369 }
370
371 AssertReturn(pEvent->cRefs < 64, UINT32_MAX); /* Sanity. Yeah, not atomic. */
372
373 return ASMAtomicIncU32(&pEvent->cRefs);
374}
375
376/**
377 * Releases an event by decreasing its reference count.
378 *
379 * @returns New reference count, or UINT32_MAX if failed.
380 * @param pSource Event source of event to release.
381 * @param idEvent ID of event to release.
382 */
383uint32_t ShClEventRelease(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
384{
385 PSHCLEVENT pEvent = shclEventGet(pSource, idEvent);
386 if (!pEvent)
387 {
388 AssertFailed();
389 return UINT32_MAX;
390 }
391
392 AssertReturn(pEvent->cRefs, UINT32_MAX); /* Sanity. Yeah, not atomic. */
393
394 return ASMAtomicDecU32(&pEvent->cRefs);
395}
396
397/**
398 * Signals an event.
399 *
400 * @returns VBox status code.
401 * @param pSource Event source of event to signal.
402 * @param uID Event ID to signal.
403 * @param pPayload Event payload to associate. Takes ownership. Optional.
404 *
405 * @note Caller must enter crit sect protecting the event source!
406 */
407int ShClEventSignal(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID,
408 PSHCLEVENTPAYLOAD pPayload)
409{
410 AssertPtrReturn(pSource, VERR_INVALID_POINTER);
411
412 int rc;
413
414 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
415
416 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
417 if (pEvent)
418 {
419 Assert(pEvent->pPayload == NULL);
420
421 pEvent->pPayload = pPayload;
422
423 rc = RTSemEventMultiSignal(pEvent->hEvtMulSem);
424 }
425 else
426 rc = VERR_NOT_FOUND;
427
428 LogFlowFuncLeaveRC(rc);
429 return rc;
430}
431
432/**
433 * Detaches a payload from an event.
434 *
435 * @returns VBox status code.
436 * @param pSource Event source of event to detach payload for.
437 * @param uID Event ID to detach payload for.
438 */
439void ShClEventPayloadDetach(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
440{
441 /** @todo r=bird: This API is not needed, it either is a no-op as it
442 * replicates work done by ShClEventWait or it leaks the payload as
443 * ShClEventWait is the only way to get it as far as I can tell. */
444
445 AssertPtrReturnVoid(pSource);
446
447 LogFlowFunc(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
448
449 PSHCLEVENT pEvent = shclEventGet(pSource, uID);
450 if (pEvent)
451 {
452 shclEventPayloadDetachInternal(pEvent);
453 }
454#ifdef DEBUG_andy
455 else
456 AssertMsgFailed(("uSource=%RU16, uEvent=%RU32\n", pSource->uID, uID));
457#endif
458}
459
460int ShClUtf16LenUtf8(PCRTUTF16 pcwszSrc, size_t cwcSrc, size_t *pchLen)
461{
462 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
463 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
464
465 size_t chLen = 0;
466 int rc = RTUtf16CalcUtf8LenEx(pcwszSrc, cwcSrc, &chLen);
467 if (RT_SUCCESS(rc))
468 *pchLen = chLen + 1; /* Include terminator. */
469 return rc;
470}
471
472int ShClConvUtf16CRLFToUtf8LF(PRTUTF16 pwszSrc, size_t cwcSrc,
473 char *pszBuf, size_t cbBuf, size_t *pcbLen)
474{
475 AssertPtrReturn(pwszSrc, VERR_INVALID_POINTER);
476 AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
477 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
478 AssertPtrReturn(pcbLen, VERR_INVALID_POINTER);
479
480 int rc;
481
482 PRTUTF16 pwszTmp = NULL;
483 size_t cchTmp = 0;
484
485 size_t cbLen = 0;
486
487 /* How long will the converted text be? */
488 rc = ShClUtf16CRLFLenUtf8(pwszSrc, cwcSrc, &cchTmp);
489 if (RT_SUCCESS(rc))
490 {
491 if (cchTmp)
492 {
493 pwszTmp = (PRTUTF16)RTMemAlloc(cchTmp * sizeof(RTUTF16));
494 if (pwszTmp)
495 {
496 rc = ShClConvUtf16CRLFToLF(pwszSrc, cwcSrc, pwszTmp, cchTmp);
497 if (RT_SUCCESS(rc))
498 rc = RTUtf16ToUtf8Ex(pwszTmp + 1, cchTmp - 1, &pszBuf, cbBuf, &cbLen);
499
500 RTMemFree(reinterpret_cast<void *>(pwszTmp));
501 }
502 else
503 rc = VERR_NO_MEMORY;
504 }
505 else
506 rc = VERR_NO_DATA;
507 }
508
509 if (RT_SUCCESS(rc))
510 {
511 *pcbLen = cbLen + 1 /* Include terminator */;
512 }
513
514 LogFlowFuncLeaveRC(rc);
515 return rc;
516}
517
518int ShClConvUtf16LFToCRLFA(RTUTF16 *pwcSrc, size_t cwcSrc,
519 PRTUTF16 *ppwszDst, size_t *pcwDst)
520{
521 AssertPtrReturn(pwcSrc, VERR_INVALID_POINTER);
522 AssertReturn(cwcSrc, VERR_INVALID_PARAMETER);
523 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
524 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
525
526 PRTUTF16 pwszDst = NULL;
527 size_t cchDst;
528
529 int rc = ShClUtf16LFLenUtf8(pwcSrc, cwcSrc, &cchDst);
530 if (RT_SUCCESS(rc))
531 {
532 Assert(cchDst);
533 pwszDst = (PRTUTF16)RTMemAlloc(cchDst * sizeof(RTUTF16));
534 if (pwszDst)
535 {
536 rc = ShClConvUtf16LFToCRLF(pwcSrc, cwcSrc, pwszDst, cchDst);
537 }
538 else
539 rc = VERR_NO_MEMORY;
540 }
541
542 if (RT_SUCCESS(rc))
543 {
544 *ppwszDst = pwszDst;
545 *pcwDst = cchDst;
546 }
547 else
548 RTMemFree(pwszDst);
549
550 LogFlowFuncLeaveRC(rc);
551 return rc;
552}
553
554int ShClConvUtf8LFToUtf16CRLF(const char *pcSrc, unsigned cbSrc,
555 PRTUTF16 *ppwszDst, size_t *pcwDst)
556{
557 AssertPtrReturn(pcSrc, VERR_INVALID_POINTER);
558 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
559 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
560 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
561
562 /* Intermediate conversion to UTF-16. */
563 size_t cwcTmp;
564 PRTUTF16 pwcTmp = NULL;
565 int rc = RTStrToUtf16Ex(pcSrc, cbSrc, &pwcTmp, 0, &cwcTmp);
566 if (RT_SUCCESS(rc))
567 {
568 rc = ShClConvUtf16LFToCRLFA(pwcTmp, cwcTmp, ppwszDst, pcwDst);
569 RTUtf16Free(pwcTmp);
570 }
571
572 LogFlowFuncLeaveRC(rc);
573 return rc;
574}
575
576int ShClConvLatin1LFToUtf16CRLF(const char *pcSrc, unsigned cbSrc,
577 PRTUTF16 *ppwszDst, size_t *pcwDst)
578{
579 AssertPtrReturn(pcSrc, VERR_INVALID_POINTER);
580 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
581 AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
582 AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
583
584 int rc = VINF_SUCCESS;
585
586 PRTUTF16 pwszDst = NULL;
587
588 /* Calculate the space needed. */
589 unsigned cbDst = 0;
590 for (unsigned i = 0; i < cbSrc && pcSrc[i] != '\0'; ++i)
591 {
592 if (pcSrc[i] == VBOX_SHCL_LINEFEED)
593 cbDst += sizeof(RTUTF16);
594 else
595 ++cbDst;
596 }
597
598 ++cbDst; /* Leave space for the terminator. */
599
600 pwszDst = (PRTUTF16)RTMemAlloc(cbDst * sizeof(RTUTF16));
601 if (!pwszDst)
602 rc = VERR_NO_MEMORY;
603
604 /* Do the conversion, bearing in mind that Latin-1 expands "naturally" to UTF-16. */
605 if (RT_SUCCESS(rc))
606 {
607 for (unsigned i = 0, j = 0; i < cbSrc; ++i, ++j)
608 {
609 if (pcSrc[i] != VBOX_SHCL_LINEFEED)
610 pwszDst[j] = pcSrc[i];
611 else
612 {
613 pwszDst[j] = VBOX_SHCL_CARRIAGERETURN;
614 pwszDst[j + 1] = VBOX_SHCL_LINEFEED;
615 ++j;
616 }
617 }
618
619 pwszDst[cbDst - 1] = '\0'; /* Make sure we are zero-terminated. */
620 }
621
622 if (RT_SUCCESS(rc))
623 {
624 *ppwszDst = pwszDst;
625 *pcwDst = cbDst * sizeof(RTUTF16);
626 }
627 else
628 RTMemFree(pwszDst);
629
630 LogFlowFuncLeaveRC(rc);
631 return rc;
632}
633
634int ShClConvUtf16ToUtf8HTML(RTUTF16 *pwcSrc, size_t cwSrc, char **ppszDst, size_t *pcbDst)
635{
636 AssertPtrReturn(pwcSrc, VERR_INVALID_POINTER);
637 AssertReturn (cwSrc, VERR_INVALID_PARAMETER);
638 AssertPtrReturn(ppszDst, VERR_INVALID_POINTER);
639 AssertPtrReturn(pcbDst, VERR_INVALID_POINTER);
640
641 int rc = VINF_SUCCESS;
642
643 size_t cwTmp = cwSrc;
644 RTUTF16 *pwTmp = pwcSrc;
645
646 char *pchDst = NULL;
647 size_t cbDst = 0;
648
649 size_t i = 0;
650 while (i < cwTmp)
651 {
652 /* Find zero symbol (end of string). */
653 for (; i < cwTmp && pwcSrc[i] != 0; i++)
654 ;
655
656 /* Convert found string. */
657 char *psz = NULL;
658 size_t cch = 0;
659 rc = RTUtf16ToUtf8Ex(pwTmp, cwTmp, &psz, pwTmp - pwcSrc, &cch);
660 if (RT_FAILURE(rc))
661 break;
662
663 /* Append new substring. */
664 char *pchNew = (char *)RTMemRealloc(pchDst, cbDst + cch + 1);
665 if (!pchNew)
666 {
667 RTStrFree(psz);
668 rc = VERR_NO_MEMORY;
669 break;
670 }
671
672 pchDst = pchNew;
673 memcpy(pchDst + cbDst, psz, cch + 1);
674
675 RTStrFree(psz);
676
677 cbDst += cch + 1;
678
679 /* Skip zero symbols. */
680 for (; i < cwTmp && pwcSrc[i] == 0; i++)
681 ;
682
683 /* Remember start of string. */
684 pwTmp += i;
685 }
686
687 if (RT_SUCCESS(rc))
688 {
689 *ppszDst = pchDst;
690 *pcbDst = cbDst;
691
692 return VINF_SUCCESS;
693 }
694
695 RTMemFree(pchDst);
696
697 LogFlowFuncLeaveRC(rc);
698 return rc;
699}
700
701int ShClUtf16LFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
702{
703 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
704 AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
705 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
706
707 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
708 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
709
710 size_t cLen = 0;
711
712 /* Don't copy the endian marker. */
713 size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
714
715 /* Calculate the size of the destination text string. */
716 /* Is this Utf16 or Utf16-LE? */
717 for (; i < cwSrc; ++i, ++cLen)
718 {
719 /* Check for a single line feed */
720 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
721 ++cLen;
722#ifdef RT_OS_DARWIN
723 /* Check for a single carriage return (MacOS) */
724 if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
725 ++cLen;
726#endif
727 if (pcwszSrc[i] == 0)
728 {
729 /* Don't count this, as we do so below. */
730 break;
731 }
732 }
733
734 /* Add terminator. */
735 ++cLen;
736
737 *pchLen = cLen;
738
739 LogFlowFuncLeaveRC(VINF_SUCCESS);
740 return VINF_SUCCESS;
741}
742
743int ShClUtf16CRLFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
744{
745 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
746 AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
747 AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
748
749 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
750 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
751
752 size_t cLen = 0;
753
754 /* Calculate the size of the destination text string. */
755 /* Is this Utf16 or Utf16-LE? */
756 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
757 cLen = 0;
758 else
759 cLen = 1;
760
761 for (size_t i = 0; i < cwSrc; ++i, ++cLen)
762 {
763 if ( (i + 1 < cwSrc)
764 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
765 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
766 {
767 ++i;
768 }
769 if (pcwszSrc[i] == 0)
770 break;
771 }
772
773 /* Add terminating zero. */
774 ++cLen;
775
776 *pchLen = cLen;
777
778 LogFlowFuncLeaveRC(VINF_SUCCESS);
779 return VINF_SUCCESS;
780}
781
782int ShClConvUtf16LFToCRLF(PCRTUTF16 pcwszSrc, size_t cwSrc, PRTUTF16 pu16Dst, size_t cwDst)
783{
784 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
785 AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
786 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
787 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
788
789 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
790 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
791
792 int rc = VINF_SUCCESS;
793
794 /* Don't copy the endian marker. */
795 size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
796 size_t j = 0;
797
798 for (; i < cwSrc; ++i, ++j)
799 {
800 /* Don't copy the null byte, as we add it below. */
801 if (pcwszSrc[i] == 0)
802 break;
803
804 /* Not enough space in destination? */
805 if (j == cwDst)
806 {
807 rc = VERR_BUFFER_OVERFLOW;
808 break;
809 }
810
811 if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
812 {
813 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
814 ++j;
815
816 /* Not enough space in destination? */
817 if (j == cwDst)
818 {
819 rc = VERR_BUFFER_OVERFLOW;
820 break;
821 }
822 }
823#ifdef RT_OS_DARWIN
824 /* Check for a single carriage return (MacOS) */
825 else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
826 {
827 /* Set CR.r */
828 pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
829 ++j;
830
831 /* Not enough space in destination? */
832 if (j == cwDst)
833 {
834 rc = VERR_BUFFER_OVERFLOW;
835 break;
836 }
837
838 /* Add line feed. */
839 pu16Dst[j] = VBOX_SHCL_LINEFEED;
840 continue;
841 }
842#endif
843 pu16Dst[j] = pcwszSrc[i];
844 }
845
846 if (j == cwDst)
847 rc = VERR_BUFFER_OVERFLOW;
848
849 if (RT_SUCCESS(rc))
850 {
851 /* Add terminator. */
852 pu16Dst[j] = 0;
853 }
854
855 LogFlowFuncLeaveRC(rc);
856 return rc;
857}
858
859int ShClConvUtf16CRLFToLF(PCRTUTF16 pcwszSrc, size_t cwSrc, PRTUTF16 pu16Dst, size_t cwDst)
860{
861 AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
862 AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
863 AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
864 AssertReturn(cwDst, VERR_INVALID_PARAMETER);
865
866 AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
867 ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
868
869 /* Prepend the Utf16 byte order marker if it is missing. */
870 size_t cwDstPos;
871 if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
872 {
873 cwDstPos = 0;
874 }
875 else
876 {
877 pu16Dst[0] = VBOX_SHCL_UTF16LEMARKER;
878 cwDstPos = 1;
879 }
880
881 for (size_t i = 0; i < cwSrc; ++i, ++cwDstPos)
882 {
883 if (pcwszSrc[i] == 0)
884 break;
885
886 if (cwDstPos == cwDst)
887 return VERR_BUFFER_OVERFLOW;
888
889 if ( (i + 1 < cwSrc)
890 && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
891 && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
892 {
893 ++i;
894 }
895
896 pu16Dst[cwDstPos] = pcwszSrc[i];
897 }
898
899 if (cwDstPos == cwDst)
900 return VERR_BUFFER_OVERFLOW;
901
902 /* Add terminating zero. */
903 pu16Dst[cwDstPos] = 0;
904
905 LogFlowFuncLeaveRC(VINF_SUCCESS);
906 return VINF_SUCCESS;
907}
908
909int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
910{
911 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
912 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
913 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
914 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
915
916 PBMINFOHEADER pBitmapInfoHeader = (PBMINFOHEADER)pvSrc;
917 /** @todo Support all the many versions of the DIB headers. */
918 if ( cbSrc < sizeof(BMINFOHEADER)
919 || RT_LE2H_U32(pBitmapInfoHeader->uSize) < sizeof(BMINFOHEADER)
920 || RT_LE2H_U32(pBitmapInfoHeader->uSize) != sizeof(BMINFOHEADER))
921 {
922 return VERR_INVALID_PARAMETER;
923 }
924
925 size_t offPixel = sizeof(BMFILEHEADER)
926 + RT_LE2H_U32(pBitmapInfoHeader->uSize)
927 + RT_LE2H_U32(pBitmapInfoHeader->uClrUsed) * sizeof(uint32_t);
928 if (cbSrc < offPixel)
929 return VERR_INVALID_PARAMETER;
930
931 size_t cbDst = sizeof(BMFILEHEADER) + cbSrc;
932
933 void *pvDest = RTMemAlloc(cbDst);
934 if (!pvDest)
935 return VERR_NO_MEMORY;
936
937 PBMFILEHEADER pFileHeader = (PBMFILEHEADER)pvDest;
938
939 pFileHeader->uType = BITMAPHEADERMAGIC;
940 pFileHeader->uSize = (uint32_t)RT_H2LE_U32(cbDst);
941 pFileHeader->uReserved1 = pFileHeader->uReserved2 = 0;
942 pFileHeader->uOffBits = (uint32_t)RT_H2LE_U32(offPixel);
943
944 memcpy((uint8_t *)pvDest + sizeof(BMFILEHEADER), pvSrc, cbSrc);
945
946 *ppvDest = pvDest;
947 *pcbDest = cbDst;
948
949 LogFlowFuncLeaveRC(VINF_SUCCESS);
950 return VINF_SUCCESS;
951}
952
953int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
954{
955 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
956 AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
957 AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
958 AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
959
960 PBMFILEHEADER pFileHeader = (PBMFILEHEADER)pvSrc;
961 if ( cbSrc < sizeof(BMFILEHEADER)
962 || pFileHeader->uType != BITMAPHEADERMAGIC
963 || RT_LE2H_U32(pFileHeader->uSize) != cbSrc)
964 {
965 return VERR_INVALID_PARAMETER;
966 }
967
968 *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMFILEHEADER);
969 *pcbDest = cbSrc - sizeof(BMFILEHEADER);
970
971 return VINF_SUCCESS;
972}
973
974#ifdef LOG_ENABLED
975int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
976{
977 size_t cchIgnored = 0;
978 int rc = RTStrNLenEx(pcszSrc, cbSrc, &cchIgnored);
979 if (RT_SUCCESS(rc))
980 {
981 char *pszBuf = (char *)RTMemAllocZ(cbSrc + 1);
982 if (pszBuf)
983 {
984 rc = RTStrCopy(pszBuf, cbSrc + 1, (const char *)pcszSrc);
985 if (RT_SUCCESS(rc))
986 {
987 for (size_t i = 0; i < cbSrc; ++i)
988 if (pszBuf[i] == '\n' || pszBuf[i] == '\r')
989 pszBuf[i] = ' ';
990 }
991 else
992 LogFunc(("Error in copying string\n"));
993 LogFunc(("Removed \\r\\n: %s\n", pszBuf));
994 RTMemFree(pszBuf);
995 }
996 else
997 rc = VERR_NO_MEMORY;
998 }
999
1000 return rc;
1001}
1002
1003void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
1004{
1005 if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
1006 {
1007 LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
1008 if (pv && cb)
1009 LogFunc(("%ls\n", pv));
1010 else
1011 LogFunc(("%p %zu\n", pv, cb));
1012 }
1013 else if (uFormat & VBOX_SHCL_FMT_BITMAP)
1014 LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
1015 else if (uFormat & VBOX_SHCL_FMT_HTML)
1016 {
1017 LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
1018 if (pv && cb)
1019 {
1020 LogFunc(("%s\n", pv));
1021
1022 //size_t cb = RTStrNLen(pv, );
1023 char *pszBuf = (char *)RTMemAllocZ(cb + 1);
1024 RTStrCopy(pszBuf, cb + 1, (const char *)pv);
1025 for (size_t off = 0; off < cb; ++off)
1026 {
1027 if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
1028 pszBuf[off] = ' ';
1029 }
1030
1031 LogFunc(("%s\n", pszBuf));
1032 RTMemFree(pszBuf);
1033 }
1034 else
1035 LogFunc(("%p %zu\n", pv, cb));
1036 }
1037 else
1038 LogFunc(("Invalid format %02X\n", uFormat));
1039}
1040#endif /* LOG_ENABLED */
1041
1042/**
1043 * Translates a Shared Clipboard host function number to a string.
1044 *
1045 * @returns Function ID string name.
1046 * @param uFn The function to translate.
1047 */
1048const char *ShClHostFunctionToStr(uint32_t uFn)
1049{
1050 switch (uFn)
1051 {
1052 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
1053 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
1054 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
1055 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
1056 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
1057 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_REGISTER);
1058 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_UNREGISTER);
1059 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_ATTACH);
1060 RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_AREA_DETACH);
1061 }
1062 return "Unknown";
1063}
1064
1065/**
1066 * Translates a Shared Clipboard host message enum to a string.
1067 *
1068 * @returns Message ID string name.
1069 * @param uMsg The message to translate.
1070 */
1071const char *ShClHostMsgToStr(uint32_t uMsg)
1072{
1073 switch (uMsg)
1074 {
1075 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
1076 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
1077 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
1078 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_CANCELED);
1079 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA_CID);
1080 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
1081 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
1082 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
1083 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
1084 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
1085 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
1086 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
1087 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
1088 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
1089 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
1090 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
1091 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
1092 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
1093 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
1094 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
1095 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
1096 RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
1097 }
1098 return "Unknown";
1099}
1100
1101/**
1102 * Translates a Shared Clipboard guest message enum to a string.
1103 *
1104 * @returns Message ID string name.
1105 * @param uMsg The message to translate.
1106 */
1107const char *ShClGuestMsgToStr(uint32_t uMsg)
1108{
1109 switch (uMsg)
1110 {
1111 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
1112 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
1113 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
1114 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
1115 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
1116 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
1117 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
1118 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
1119 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
1120 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
1121 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
1122 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
1123 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
1124 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
1125 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
1126 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
1127 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
1128 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
1129 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
1130 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
1131 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
1132 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
1133 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
1134 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
1135 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
1136 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
1137 RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
1138 }
1139 return "Unknown";
1140}
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