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