VirtualBox

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

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

Consolidated all the different Bitmap (BMP) file format headers / definitions into new include/iprt/formats/bmp.h (part 2): Renaming + prefixing stuff.

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