summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibDragAndDrop.cpp
blob: 68ba038b624326626c53a6d7b75a86f02eafeada (plain)
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
/* $Id: VBoxGuestR3LibDragAndDrop.cpp $ */
/** @file
 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Drag & Drop.
 */

/*
 * Copyright (C) 2011-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>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/path.h>
#include <iprt/dir.h>
#include <iprt/file.h>
#include <iprt/uri.h>
#include <iprt/thread.h>

#include <iprt/cpp/list.h>
#include <iprt/cpp/ministring.h>

#ifdef LOG_GROUP
 #undef LOG_GROUP
#endif
#define LOG_GROUP LOG_GROUP_GUEST_DND
#include <VBox/log.h>

#include <VBox/VBoxGuestLib.h>
#include <VBox/GuestHost/DragAndDrop.h>
#include <VBox/HostServices/DragAndDropSvc.h>

using namespace DragAndDropSvc;

#include "VBoxGuestR3LibInternal.h"


/*********************************************************************************************************************************
*   Private internal functions                                                                                                   *
*********************************************************************************************************************************/

/**
 * Receives the next upcoming message for a given DnD context.
 *
 * @returns IPRT status code.
 *          Will return VERR_CANCELLED (implemented by the host service) if we need to bail out.
 * @param   pCtx                DnD context to use.
 * @param   puMsg               Where to store the message type.
 * @param   pcParms             Where to store the number of parameters required for receiving the message.
 * @param   fWait               Whether to wait (block) for a new message to arrive or not.
 */
static int vbglR3DnDGetNextMsgType(PVBGLR3GUESTDNDCMDCTX pCtx, uint32_t *puMsg, uint32_t *pcParms, bool fWait)
{
    AssertPtrReturn(pCtx,    VERR_INVALID_POINTER);
    AssertPtrReturn(puMsg,   VERR_INVALID_POINTER);
    AssertPtrReturn(pcParms, VERR_INVALID_POINTER);

    int rc;

    do
    {
        HGCMMsgGetNext Msg;
        VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_GET_NEXT_HOST_MSG, 3);
        Msg.uMsg.SetUInt32(0);
        Msg.cParms.SetUInt32(0);
        Msg.fBlock.SetUInt32(fWait ? 1 : 0);

        rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
        if (RT_SUCCESS(rc))
        {
            rc = Msg.uMsg.GetUInt32(puMsg);     AssertRC(rc);
            rc = Msg.cParms.GetUInt32(pcParms); AssertRC(rc);
        }

        LogRel(("DnD: Received message %s (%#x) from host\n", DnDHostMsgToStr(*puMsg), *puMsg));

    } while (rc == VERR_INTERRUPTED);

    return rc;
}


/**
 * Sends a DnD error back to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   rcErr               Error (IPRT-style) to send.
 */
VBGLR3DECL(int) VbglR3DnDSendError(PVBGLR3GUESTDNDCMDCTX pCtx, int rcErr)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);

    HGCMMsgGHError Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_EVT_ERROR, 2);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.rc.SetUInt32((uint32_t)rcErr); /* uint32_t vs. int. */

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));

    /*
     * Never return an error if the host did not accept the error at the current
     * time.  This can be due to the host not having any appropriate callbacks
     * set which would handle that error.
     *
     * bird: Looks like VERR_NOT_SUPPORTED is what the host will return if it
     *       doesn't an appropriate callback.  The code used to ignore ALL errors
     *       the host would return, also relevant ones.
     */
    if (RT_FAILURE(rc))
        LogFlowFunc(("Sending error %Rrc failed with rc=%Rrc\n", rcErr, rc));
    if (rc == VERR_NOT_SUPPORTED)
        rc = VINF_SUCCESS;

    return rc;
}

/**
 * Host -> Guest
 * Utility function to receive a so-called "action message" from the host.
 * Certain DnD messages use the same amount / sort of parameters and grouped as "action messages".
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   uMsg                Which kind of message to receive.
 * @param   puScreenID          Where to store the host screen ID the message is bound to. Optional.
 * @param   puX                 Where to store the absolute X coordinates. Optional.
 * @param   puY                 Where to store the absolute Y coordinates. Optional.
 * @param   puDefAction         Where to store the default action to perform. Optional.
 * @param   puAllActions        Where to store the available actions. Optional.
 * @param   ppszFormats         Where to store List of formats. Optional.
 * @param   pcbFormats          Size (in bytes) of where to store the list of formats. Optional.
 *
 * @todo r=andy Get rid of this function as soon as we resolved the protocol TODO #1.
 *              This was part of the initial protocol and needs to go.
 */
static int vbglR3DnDHGRecvAction(PVBGLR3GUESTDNDCMDCTX pCtx,
                                 uint32_t   uMsg,
                                 uint32_t  *puScreenID,
                                 uint32_t  *puX,
                                 uint32_t  *puY,
                                 uint32_t  *puDefAction,
                                 uint32_t  *puAllActions,
                                 char     **ppszFormats,
                                 uint32_t  *pcbFormats)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
    /* The rest is optional. */

    const uint32_t cbFormatsTmp = pCtx->cbMaxChunkSize;

    char *pszFormatsTmp = static_cast<char *>(RTMemAlloc(cbFormatsTmp));
    if (!pszFormatsTmp)
        return VERR_NO_MEMORY;

    HGCMMsgHGAction Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, uMsg, 8);
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.uScreenId.SetUInt32(0);
    Msg.u.v3.uX.SetUInt32(0);
    Msg.u.v3.uY.SetUInt32(0);
    Msg.u.v3.uDefAction.SetUInt32(0);
    Msg.u.v3.uAllActions.SetUInt32(0);
    Msg.u.v3.pvFormats.SetPtr(pszFormatsTmp, cbFormatsTmp);
    Msg.u.v3.cbFormats.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /** @todo Context ID not used yet. */
        if (RT_SUCCESS(rc) && puScreenID)
            rc = Msg.u.v3.uScreenId.GetUInt32(puScreenID);
        if (RT_SUCCESS(rc) && puX)
            rc = Msg.u.v3.uX.GetUInt32(puX);
        if (RT_SUCCESS(rc) && puY)
            rc = Msg.u.v3.uY.GetUInt32(puY);
        if (RT_SUCCESS(rc) && puDefAction)
            rc = Msg.u.v3.uDefAction.GetUInt32(puDefAction);
        if (RT_SUCCESS(rc) && puAllActions)
            rc = Msg.u.v3.uAllActions.GetUInt32(puAllActions);
        if (RT_SUCCESS(rc) && pcbFormats)
            rc = Msg.u.v3.cbFormats.GetUInt32(pcbFormats);

        if (RT_SUCCESS(rc))
        {
            if (ppszFormats)
            {
                *ppszFormats = RTStrDup(pszFormatsTmp);
                if (!*ppszFormats)
                    rc = VERR_NO_MEMORY;
            }
        }
    }

    RTStrFree(pszFormatsTmp);

    return rc;
}

/**
 * Host -> Guest
 * Utility function to receive a HOST_DND_FN_HG_EVT_LEAVE message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 */
static int vbglR3DnDHGRecvLeave(PVBGLR3GUESTDNDCMDCTX pCtx)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);

    HGCMMsgHGLeave Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_HG_EVT_LEAVE, 1);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

/**
 * Host -> Guest
 * Utility function to receive a HOST_DND_FN_HG_EVT_CANCEL message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 */
static int vbglR3DnDHGRecvCancel(PVBGLR3GUESTDNDCMDCTX pCtx)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);

    HGCMMsgHGCancel Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_CANCEL, 1);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

/**
 * Host -> Guest
 * Utility function to receive a HOST_DND_FN_HG_SND_DIR message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pszDirname          Where to store the directory name of the directory being created.
 * @param   cbDirname           Size (in bytes) of where to store the directory name of the directory being created.
 * @param   pcbDirnameRecv      Size (in bytes) of the actual directory name received.
 * @param   pfMode              Where to store the directory creation mode.
 */
static int vbglR3DnDHGRecvDir(PVBGLR3GUESTDNDCMDCTX pCtx,
                              char     *pszDirname,
                              uint32_t  cbDirname,
                              uint32_t *pcbDirnameRecv,
                              uint32_t *pfMode)
{
    AssertPtrReturn(pCtx,           VERR_INVALID_POINTER);
    AssertPtrReturn(pszDirname,     VERR_INVALID_POINTER);
    AssertReturn(cbDirname,         VERR_INVALID_PARAMETER);
    AssertPtrReturn(pcbDirnameRecv, VERR_INVALID_POINTER);
    AssertPtrReturn(pfMode,         VERR_INVALID_POINTER);

    HGCMMsgHGSendDir Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_HG_SND_DIR, 4);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.pvName.SetPtr(pszDirname, cbDirname);
    Msg.u.v3.cbName.SetUInt32(cbDirname);
    Msg.u.v3.fMode.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /** @todo Context ID not used yet. */
        rc = Msg.u.v3.cbName.GetUInt32(pcbDirnameRecv); AssertRC(rc);
        rc = Msg.u.v3.fMode.GetUInt32(pfMode);          AssertRC(rc);

        AssertReturn(cbDirname >= *pcbDirnameRecv, VERR_TOO_MUCH_DATA);
    }

    return rc;
}

/**
 * Host -> Guest
 * Utility function to receive a HOST_DND_FN_HG_SND_FILE_DATA message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pvData              Where to store the file data chunk.
 * @param   cbData              Size (in bytes) of where to store the data chunk.
 * @param   pcbDataRecv         Size (in bytes) of the actual data chunk size received.
 */
static int vbglR3DnDHGRecvFileData(PVBGLR3GUESTDNDCMDCTX pCtx,
                                   void                 *pvData,
                                   uint32_t              cbData,
                                   uint32_t             *pcbDataRecv)
{
    AssertPtrReturn(pCtx,            VERR_INVALID_POINTER);
    AssertPtrReturn(pvData,          VERR_INVALID_POINTER);
    AssertReturn(cbData,             VERR_INVALID_PARAMETER);
    AssertPtrReturn(pcbDataRecv,     VERR_INVALID_POINTER);

    HGCMMsgHGSendFileData Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_HG_SND_FILE_DATA, 5);
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.pvData.SetPtr(pvData, cbData);
    Msg.u.v3.cbData.SetUInt32(0);
    Msg.u.v3.pvChecksum.SetPtr(NULL, 0);
    Msg.u.v3.cbChecksum.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /** @todo Context ID not used yet. */
        rc = Msg.u.v3.cbData.GetUInt32(pcbDataRecv); AssertRC(rc);
        AssertReturn(cbData >= *pcbDataRecv, VERR_TOO_MUCH_DATA);
        /** @todo Add checksum support. */
    }

    return rc;
}

/**
 * Host -> Guest
 * Utility function to receive the HOST_DND_FN_HG_SND_FILE_HDR message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pszFilename         Where to store the file name of the file being transferred.
 * @param   cbFilename          Size (in bytes) of where to store the file name of the file being transferred.
 * @param   puFlags             File transfer flags. Currently not being used.
 * @param   pfMode              Where to store the file creation mode.
 * @param   pcbTotal            Where to store the file size (in bytes).
 */
static int vbglR3DnDHGRecvFileHdr(PVBGLR3GUESTDNDCMDCTX  pCtx,
                                  char                  *pszFilename,
                                  uint32_t               cbFilename,
                                  uint32_t              *puFlags,
                                  uint32_t              *pfMode,
                                  uint64_t              *pcbTotal)
{
    AssertPtrReturn(pCtx,        VERR_INVALID_POINTER);
    AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
    AssertReturn(cbFilename,     VERR_INVALID_PARAMETER);
    AssertPtrReturn(puFlags,     VERR_INVALID_POINTER);
    AssertPtrReturn(pfMode,      VERR_INVALID_POINTER);
    AssertReturn(pcbTotal,       VERR_INVALID_POINTER);

    HGCMMsgHGSendFileHdr Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_HG_SND_FILE_HDR, 6);
    Msg.uContext.SetUInt32(0); /** @todo Not used yet. */
    Msg.pvName.SetPtr(pszFilename, cbFilename);
    Msg.cbName.SetUInt32(cbFilename);
    Msg.uFlags.SetUInt32(0);
    Msg.fMode.SetUInt32(0);
    Msg.cbTotal.SetUInt64(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /** @todo Get context ID. */
        rc = Msg.uFlags.GetUInt32(puFlags);   AssertRC(rc);
        rc = Msg.fMode.GetUInt32(pfMode);     AssertRC(rc);
        rc = Msg.cbTotal.GetUInt64(pcbTotal); AssertRC(rc);
    }

    return rc;
}

/**
 * Host -> Guest
 * Helper function for receiving URI data from the host. Do not call directly.
 * This function also will take care of the file creation / locking on the guest.
 *
 * @returns IPRT status code.
 * @retval  VERR_CANCELLED if the transfer was cancelled by the host.
 * @param   pCtx                DnD context to use.
 * @param   pDataHdr            DnD data header to use. Needed for accounting.
 * @param   pDroppedFiles       Dropped files object to use for maintaining the file creation / locking.
 */
static int vbglR3DnDHGRecvURIData(PVBGLR3GUESTDNDCMDCTX pCtx, PVBOXDNDSNDDATAHDR pDataHdr, PDNDDROPPEDFILES pDroppedFiles)
{
    AssertPtrReturn(pCtx,          VERR_INVALID_POINTER);
    AssertPtrReturn(pDataHdr,      VERR_INVALID_POINTER);
    AssertPtrReturn(pDroppedFiles, VERR_INVALID_POINTER);

    /* Only count the raw data minus the already received meta data. */
    Assert(pDataHdr->cbTotal >= pDataHdr->cbMeta);
    uint64_t cbToRecvBytes = pDataHdr->cbTotal - pDataHdr->cbMeta;
    uint64_t cToRecvObjs   = pDataHdr->cObjects;

    LogFlowFunc(("cbToRecvBytes=%RU64, cToRecvObjs=%RU64, (cbTotal=%RU64, cbMeta=%RU32)\n",
                 cbToRecvBytes, cToRecvObjs, pDataHdr->cbTotal, pDataHdr->cbMeta));

    /* Anything to do at all? */
    /* Note: Do not check for cbToRecvBytes == 0 here, as this might be just
     *       a bunch of 0-byte files to be transferred. */
    if (!cToRecvObjs)
        return VINF_SUCCESS;

    LogRel2(("DnD: Receiving URI data started\n"));

    /*
     * Allocate temporary chunk buffer.
     */
    uint32_t cbChunkMax = pCtx->cbMaxChunkSize;
    void *pvChunk = RTMemAlloc(cbChunkMax);
    if (!pvChunk)
        return VERR_NO_MEMORY;
    uint32_t cbChunkRead   = 0;

    uint64_t cbFileSize    = 0; /* Total file size (in bytes). */
    uint64_t cbFileWritten = 0; /* Written bytes. */

    const char *pszDropDir = DnDDroppedFilesGetDirAbs(pDroppedFiles);
    AssertPtr(pszDropDir);

    int rc;

    /*
     * Enter the main loop of retieving files + directories.
     */
    DNDTRANSFEROBJECT objCur;
    RT_ZERO(objCur);

    char szPathName[RTPATH_MAX] = { 0 };
    uint32_t cbPathName = 0;
    uint32_t fFlags     = 0;
    uint32_t fMode      = 0;

    do
    {
        LogFlowFunc(("Waiting for new message ...\n"));

        uint32_t uNextMsg;
        uint32_t cNextParms;
        rc = vbglR3DnDGetNextMsgType(pCtx, &uNextMsg, &cNextParms, true /* fWait */);
        if (RT_SUCCESS(rc))
        {
            LogFlowFunc(("uNextMsg=%RU32, cNextParms=%RU32\n", uNextMsg, cNextParms));

            switch (uNextMsg)
            {
                case HOST_DND_FN_HG_SND_DIR:
                {
                    rc = vbglR3DnDHGRecvDir(pCtx,
                                            szPathName,
                                            sizeof(szPathName),
                                            &cbPathName,
                                            &fMode);
                    LogFlowFunc(("HOST_DND_FN_HG_SND_DIR: "
                                 "pszPathName=%s, cbPathName=%RU32, fMode=0x%x, rc=%Rrc\n",
                                 szPathName, cbPathName, fMode, rc));

                    char *pszPathAbs = RTPathJoinA(pszDropDir, szPathName);
                    if (pszPathAbs)
                    {
#ifdef RT_OS_WINDOWS
                        uint32_t fCreationMode = (fMode & RTFS_DOS_MASK) | RTFS_DOS_NT_NORMAL;
#else
                        uint32_t fCreationMode = (fMode & RTFS_UNIX_MASK) | RTFS_UNIX_IRWXU;
#endif
                        rc = RTDirCreate(pszPathAbs, fCreationMode, 0);
                        if (RT_SUCCESS(rc))
                            rc = DnDDroppedFilesAddDir(pDroppedFiles, pszPathAbs);

                        if (RT_SUCCESS(rc))
                        {
                            Assert(cToRecvObjs);
                            cToRecvObjs--;
                        }

                        RTStrFree(pszPathAbs);
                    }
                    else
                        rc = VERR_NO_MEMORY;
                    break;
                }
                case HOST_DND_FN_HG_SND_FILE_HDR:
                    RT_FALL_THROUGH();
                case HOST_DND_FN_HG_SND_FILE_DATA:
                {
                    if (uNextMsg == HOST_DND_FN_HG_SND_FILE_HDR)
                    {
                        rc = vbglR3DnDHGRecvFileHdr(pCtx,
                                                    szPathName,
                                                    sizeof(szPathName),
                                                    &fFlags,
                                                    &fMode,
                                                    &cbFileSize);
                        LogFlowFunc(("HOST_DND_FN_HG_SND_FILE_HDR: "
                                     "szPathName=%s, fFlags=0x%x, fMode=0x%x, cbFileSize=%RU64, rc=%Rrc\n",
                                     szPathName, fFlags, fMode, cbFileSize, rc));
                    }
                    else
                    {
                        rc = vbglR3DnDHGRecvFileData(pCtx,
                                                     pvChunk,
                                                     cbChunkMax,
                                                     &cbChunkRead);
                        LogFlowFunc(("HOST_DND_FN_HG_SND_FILE_DATA: "
                                     "cbChunkRead=%RU32, rc=%Rrc\n", cbChunkRead, rc));
                    }

                    if (   RT_SUCCESS(rc)
                        && uNextMsg == HOST_DND_FN_HG_SND_FILE_HDR)
                    {
                        char *pszPathAbs = RTPathJoinA(pszDropDir, szPathName);
                        if (pszPathAbs)
                        {
                            LogFlowFunc(("Opening pszPathName=%s, cbPathName=%RU32, fMode=0x%x, cbFileSize=%zu\n",
                                         szPathName, cbPathName, fMode, cbFileSize));

                            uint64_t fOpen  =   RTFILE_O_WRITE | RTFILE_O_DENY_WRITE
                                              | RTFILE_O_CREATE_REPLACE;

                            /* Is there already a file open, e.g. in transfer? */
                            if (!DnDTransferObjectIsOpen(&objCur))
                            {
#ifdef RT_OS_WINDOWS
                                uint32_t fCreationMode = (fMode & RTFS_DOS_MASK) | RTFS_DOS_NT_NORMAL;
#else
                                uint32_t fCreationMode = (fMode & RTFS_UNIX_MASK) | RTFS_UNIX_IRUSR | RTFS_UNIX_IWUSR;
#endif
                                rc = DnDTransferObjectInitEx(&objCur, DNDTRANSFEROBJTYPE_FILE,
                                                             pszDropDir /* Source (base) path */, szPathName /* Destination path */);
                                if (RT_SUCCESS(rc))
                                {
                                    rc = DnDTransferObjectOpen(&objCur, fOpen, fCreationMode, DNDTRANSFEROBJECT_FLAGS_NONE);
                                    if (RT_SUCCESS(rc))
                                    {
                                        rc = DnDDroppedFilesAddFile(pDroppedFiles, pszPathAbs);
                                        if (RT_SUCCESS(rc))
                                        {
                                            cbFileWritten = 0;
                                            DnDTransferObjectSetSize(&objCur, cbFileSize);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                AssertMsgFailed(("ObjType=%RU32\n", DnDTransferObjectGetType(&objCur)));
                                rc = VERR_WRONG_ORDER;
                            }

                            RTStrFree(pszPathAbs);
                        }
                        else
                            rc = VERR_NO_MEMORY;
                    }

                    if (   RT_SUCCESS(rc)
                        && uNextMsg == HOST_DND_FN_HG_SND_FILE_DATA
                        && cbChunkRead)
                    {
                        uint32_t cbChunkWritten;
                        rc = DnDTransferObjectWrite(&objCur, pvChunk, cbChunkRead, &cbChunkWritten);
                        if (RT_SUCCESS(rc))
                        {
                            LogFlowFunc(("HOST_DND_FN_HG_SND_FILE_DATA: "
                                         "cbChunkRead=%RU32, cbChunkWritten=%RU32, cbFileWritten=%RU64 cbFileSize=%RU64\n",
                                         cbChunkRead, cbChunkWritten, cbFileWritten + cbChunkWritten, cbFileSize));

                            cbFileWritten += cbChunkWritten;

                            Assert(cbChunkRead <= cbToRecvBytes);
                            cbToRecvBytes -= cbChunkRead;
                        }
                    }

                    /* Data transfer complete? Close the file. */
                    bool fClose = DnDTransferObjectIsComplete(&objCur);
                    if (fClose)
                    {
                        Assert(cToRecvObjs);
                        cToRecvObjs--;
                    }

                    /* Only since protocol v2 we know the file size upfront. */
                    Assert(cbFileWritten <= cbFileSize);

                    if (fClose)
                    {
                        LogFlowFunc(("Closing file\n"));
                        DnDTransferObjectDestroy(&objCur);
                    }

                    break;
                }
                case HOST_DND_FN_CANCEL:
                {
                    rc = vbglR3DnDHGRecvCancel(pCtx);
                    if (RT_SUCCESS(rc))
                        rc = VERR_CANCELLED;
                    break;
                }
                default:
                {
                    LogRel(("DnD: Warning: Message %s (%#x) from host not supported or in wrong order\n", DnDHostMsgToStr(uNextMsg), uNextMsg));
                    rc = VERR_NOT_SUPPORTED;
                    break;
                }
            }
        }

        if (RT_FAILURE(rc))
            break;

        LogFlowFunc(("cbToRecvBytes=%RU64, cToRecvObjs=%RU64\n", cbToRecvBytes, cToRecvObjs));
        if (   !cbToRecvBytes
            && !cToRecvObjs)
        {
            break;
        }

    } while (RT_SUCCESS(rc));

    LogFlowFunc(("Loop ended with %Rrc\n", rc));

    /* All URI data processed? */
    if (rc == VERR_NO_DATA)
        rc = VINF_SUCCESS;

    /* Delete temp buffer again. */
    if (pvChunk)
        RTMemFree(pvChunk);

    /* Cleanup on failure or if the user has canceled the operation or
     * something else went wrong. */
    if (RT_FAILURE(rc))
    {
        if (rc == VERR_CANCELLED)
            LogRel2(("DnD: Receiving URI data was cancelled by the host\n"));
        else
            LogRel(("DnD: Receiving URI data failed with %Rrc\n", rc));

        DnDTransferObjectDestroy(&objCur);
        DnDDroppedFilesRollback(pDroppedFiles);
    }
    else
    {
        LogRel2(("DnD: Receiving URI data finished\n"));

        /** @todo Compare the transfer list with the dirs/files we really transferred. */
        /** @todo Implement checksum verification, if any. */
    }

    /*
     * Close the dropped files directory.
     * Don't try to remove it here, however, as the files are being needed
     * by the client's drag'n drop operation lateron.
     */
    int rc2 = DnDDroppedFilesReset(pDroppedFiles, false /* fRemoveDropDir */);
    if (RT_FAILURE(rc2)) /* Not fatal, don't report back to host. */
        LogFlowFunc(("Closing dropped files directory failed with %Rrc\n", rc2));

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Host -> Guest
 * Utility function to receive the HOST_DND_FN_HG_SND_DATA message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pDataHdr            DnD data header to use. Need for accounting and stuff.
 * @param   pvData              Where to store the received data from the host.
 * @param   cbData              Size (in bytes) of where to store the received data.
 * @param   pcbDataRecv         Where to store the received amount of data (in bytes).
 */
static int vbglR3DnDHGRecvDataRaw(PVBGLR3GUESTDNDCMDCTX pCtx, PVBOXDNDSNDDATAHDR pDataHdr,
                                  void *pvData, uint32_t cbData, uint32_t *pcbDataRecv)
{
    AssertPtrReturn(pCtx,            VERR_INVALID_POINTER);
    AssertPtrReturn(pDataHdr,        VERR_INVALID_POINTER);
    AssertPtrReturn(pvData,          VERR_INVALID_POINTER);
    AssertReturn(cbData,             VERR_INVALID_PARAMETER);
    AssertPtrNullReturn(pcbDataRecv, VERR_INVALID_POINTER);

    LogFlowFunc(("pvDate=%p, cbData=%RU32\n", pvData, cbData));

    HGCMMsgHGSendData Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_HG_SND_DATA, 5);
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.pvData.SetPtr(pvData, cbData);
    Msg.u.v3.cbData.SetUInt32(0);
    Msg.u.v3.pvChecksum.SetPtr(NULL, 0);
    Msg.u.v3.cbChecksum.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        uint32_t cbDataRecv;
        rc = Msg.u.v3.cbData.GetUInt32(&cbDataRecv);
        AssertRC(rc);
        if (RT_SUCCESS(rc))
        {
            /** @todo Use checksum for validating the received data. */
            if (pcbDataRecv)
                *pcbDataRecv = cbDataRecv;
            LogFlowFuncLeaveRC(rc);
            return rc;
        }
    }

    /* failure */
    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Host -> Guest
 * Utility function to receive the HOST_DND_FN_HG_SND_DATA_HDR message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pDataHdr            Where to store the receivd DnD data header.
 */
static int vbglR3DnDHGRecvDataHdr(PVBGLR3GUESTDNDCMDCTX pCtx, PVBOXDNDSNDDATAHDR pDataHdr)
{
    AssertPtrReturn(pCtx,     VERR_INVALID_POINTER);
    AssertPtrReturn(pDataHdr, VERR_INVALID_POINTER);

    Assert(pCtx->uProtocolDeprecated >= 3); /* Only for protocol v3 and up. */

    HGCMMsgHGSendDataHdr Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_HG_SND_DATA_HDR, 12);
    Msg.uContext.SetUInt32(0);
    Msg.uFlags.SetUInt32(0);
    Msg.uScreenId.SetUInt32(0);
    Msg.cbTotal.SetUInt64(0);
    Msg.cbMeta.SetUInt32(0);
    Msg.pvMetaFmt.SetPtr(pDataHdr->pvMetaFmt, pDataHdr->cbMetaFmt);
    Msg.cbMetaFmt.SetUInt32(0);
    Msg.cObjects.SetUInt64(0);
    Msg.enmCompression.SetUInt32(0);
    Msg.enmChecksumType.SetUInt32(0);
    Msg.pvChecksum.SetPtr(pDataHdr->pvChecksum, pDataHdr->cbChecksum);
    Msg.cbChecksum.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /* Msg.uContext not needed here. */
        Msg.uFlags.GetUInt32(&pDataHdr->uFlags);
        Msg.uScreenId.GetUInt32(&pDataHdr->uScreenId);
        Msg.cbTotal.GetUInt64(&pDataHdr->cbTotal);
        Msg.cbMeta.GetUInt32(&pDataHdr->cbMeta);
        Msg.cbMetaFmt.GetUInt32(&pDataHdr->cbMetaFmt);
        Msg.cObjects.GetUInt64(&pDataHdr->cObjects);
        Msg.enmCompression.GetUInt32(&pDataHdr->enmCompression);
        Msg.enmChecksumType.GetUInt32((uint32_t *)&pDataHdr->enmChecksumType);
        Msg.cbChecksum.GetUInt32(&pDataHdr->cbChecksum);
    }

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Host -> Guest
 * Helper function for receiving the actual DnD data from the host. Do not call directly.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pDataHdr            Where to store the data header data.
 * @param   ppvData             Returns the received meta data. Needs to be free'd by the caller.
 * @param   pcbData             Where to store the size (in bytes) of the received meta data.
 */
static int vbglR3DnDHGRecvDataLoop(PVBGLR3GUESTDNDCMDCTX pCtx, PVBOXDNDSNDDATAHDR pDataHdr,
                                   void **ppvData, uint64_t *pcbData)
{
    AssertPtrReturn(pCtx,     VERR_INVALID_POINTER);
    AssertPtrReturn(pDataHdr, VERR_INVALID_POINTER);
    AssertPtrReturn(ppvData,  VERR_INVALID_POINTER);
    AssertPtrReturn(pcbData,  VERR_INVALID_POINTER);

    int rc;
    uint32_t cbDataRecv;

    LogFlowFuncEnter();

    rc = vbglR3DnDHGRecvDataHdr(pCtx, pDataHdr);
    if (RT_FAILURE(rc))
        return rc;

    LogFlowFunc(("cbTotal=%RU64, cbMeta=%RU32, cObjects=%RU32\n", pDataHdr->cbTotal, pDataHdr->cbMeta, pDataHdr->cObjects));
    if (pDataHdr->cbMeta)
    {
        uint64_t cbDataTmp = 0;
        void    *pvDataTmp = RTMemAlloc(pDataHdr->cbMeta);
        if (!pvDataTmp)
            rc = VERR_NO_MEMORY;

        if (RT_SUCCESS(rc))
        {
            uint8_t *pvDataOff = (uint8_t *)pvDataTmp;
            while (cbDataTmp < pDataHdr->cbMeta)
            {
                rc = vbglR3DnDHGRecvDataRaw(pCtx, pDataHdr,
                                            pvDataOff, RT_MIN(pDataHdr->cbMeta - cbDataTmp, pCtx->cbMaxChunkSize),
                                            &cbDataRecv);
                if (RT_SUCCESS(rc))
                {
                    LogFlowFunc(("cbDataRecv=%RU32, cbDataTmp=%RU64\n", cbDataRecv, cbDataTmp));
                    Assert(cbDataTmp + cbDataRecv <= pDataHdr->cbMeta);
                    cbDataTmp += cbDataRecv;
                    pvDataOff += cbDataRecv;
                }
                else
                    break;
            }

            if (RT_SUCCESS(rc))
            {
                Assert(cbDataTmp == pDataHdr->cbMeta);

                LogFlowFunc(("Received %RU64 bytes of data\n", cbDataTmp));

                *ppvData = pvDataTmp;
                *pcbData = cbDataTmp;
            }
            else
                RTMemFree(pvDataTmp);
        }
    }
    else
    {
        *ppvData = NULL;
        *pcbData = 0;
    }

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Host -> Guest
 * Main function for receiving the actual DnD data from the host.
 *
 * @returns VBox status code.
 * @retval  VERR_CANCELLED if cancelled by the host.
 * @param   pCtx                DnD context to use.
 * @param   pMeta               Where to store the actual meta data received from the host.
 */
static int vbglR3DnDHGRecvDataMain(PVBGLR3GUESTDNDCMDCTX   pCtx,
                                   PVBGLR3GUESTDNDMETADATA pMeta)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
    AssertPtrReturn(pMeta, VERR_INVALID_POINTER);

    AssertMsgReturn(pCtx->cbMaxChunkSize, ("Maximum chunk size must not be 0\n"), VERR_INVALID_PARAMETER);

    VBOXDNDDATAHDR dataHdr;
    RT_ZERO(dataHdr);
    dataHdr.cbMetaFmt = pCtx->cbMaxChunkSize;
    dataHdr.pvMetaFmt = RTMemAlloc(dataHdr.cbMetaFmt);
    if (!dataHdr.pvMetaFmt)
        return VERR_NO_MEMORY;

    void    *pvData = NULL;
    uint64_t cbData = 0;
    int rc = vbglR3DnDHGRecvDataLoop(pCtx, &dataHdr, &pvData, &cbData);
    if (RT_SUCCESS(rc))
    {
        LogRel2(("DnD: Received %RU64 bytes meta data in format '%s'\n", cbData, (char *)dataHdr.pvMetaFmt));

        /**
         * Check if this is an URI event. If so, let VbglR3 do all the actual
         * data transfer + file/directory creation internally without letting
         * the caller know.
         *
         * This keeps the actual (guest OS-)dependent client (like VBoxClient /
         * VBoxTray) small by not having too much redundant code.
         */
        Assert(dataHdr.cbMetaFmt);
        AssertPtr(dataHdr.pvMetaFmt);
        if (DnDMIMEHasFileURLs((char *)dataHdr.pvMetaFmt, dataHdr.cbMetaFmt)) /* URI data. */
        {
            DNDDROPPEDFILES droppedFiles;
            RT_ZERO(droppedFiles);

            rc = DnDDroppedFilesInit(&droppedFiles);
            if (RT_SUCCESS(rc))
                rc = DnDDroppedFilesOpenTemp(&droppedFiles, DNDURIDROPPEDFILE_FLAGS_NONE);

            if (RT_FAILURE(rc))
            {
                LogRel(("DnD: Initializing dropped files directory failed with %Rrc\n", rc));
            }
            else
            {
                AssertPtr(pvData);
                Assert(cbData);

                /* Use the dropped files directory as the root directory for the current transfer. */
                rc = DnDTransferListInitEx(&pMeta->u.URI.Transfer, DnDDroppedFilesGetDirAbs(&droppedFiles),
                                           DNDTRANSFERLISTFMT_NATIVE);
                if (RT_SUCCESS(rc))
                {
                    rc = DnDTransferListAppendRootsFromBuffer(&pMeta->u.URI.Transfer, DNDTRANSFERLISTFMT_URI, (const char *)pvData, cbData,
                                                              DND_PATH_SEPARATOR_STR, 0 /* fFlags */);
                    if (RT_SUCCESS(rc))
                    {
                        rc = vbglR3DnDHGRecvURIData(pCtx, &dataHdr, &droppedFiles);
                        if (RT_SUCCESS(rc))
                        {
                            pMeta->enmType = VBGLR3GUESTDNDMETADATATYPE_URI_LIST;
                        }
                    }
                }
            }
        }
        else /* Raw data. */
        {
            pMeta->u.Raw.cbMeta = cbData;
            pMeta->u.Raw.pvMeta = pvData;

            pMeta->enmType = VBGLR3GUESTDNDMETADATATYPE_RAW;
        }

        if (pvData)
            RTMemFree(pvData);
    }

    if (dataHdr.pvMetaFmt)
        RTMemFree(dataHdr.pvMetaFmt);

    if (RT_FAILURE(rc))
    {
        if (rc != VERR_CANCELLED)
        {
            LogRel(("DnD: Receiving data failed with %Rrc\n", rc));

            int rc2 = VbglR3DnDHGSendProgress(pCtx, DND_PROGRESS_ERROR, 100 /* Percent */, rc);
            if (RT_FAILURE(rc2))
                LogRel(("DnD: Unable to send progress error %Rrc to host: %Rrc\n", rc, rc2));
        }
    }

    LogFlowFuncLeaveRC(rc);
    return rc;
}

#ifdef VBOX_WITH_DRAG_AND_DROP_GH
/**
 * Guest -> Host
 * Utility function to receive the HOST_DND_FN_GH_REQ_PENDING message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   puScreenID          For which screen on the host the request is for. Optional.
 */
static int vbglR3DnDGHRecvPending(PVBGLR3GUESTDNDCMDCTX pCtx, uint32_t *puScreenID)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
   /* pScreenID is optional. */

    HGCMMsgGHReqPending Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_GH_REQ_PENDING, 2);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.uScreenId.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /** @todo Context ID not used yet. */
        if (puScreenID)
            rc = Msg.u.v3.uContext.GetUInt32(puScreenID);
    }

    return rc;
}

/**
 * Guest -> Host
 * Utility function to receive the HOST_DND_FN_GH_EVT_DROPPED message from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   ppszFormat          Requested data format from the host. Optional.
 * @param   pcbFormat           Size of requested data format (in bytes). Optional.
 * @param   puAction            Requested action from the host. Optional.
 */
static int vbglR3DnDGHRecvDropped(PVBGLR3GUESTDNDCMDCTX pCtx,
                                  char     **ppszFormat,
                                  uint32_t  *pcbFormat,
                                  uint32_t  *puAction)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
    /* The rest is optional. */

    const uint32_t cbFormatTmp = pCtx->cbMaxChunkSize;

    char *pszFormatTmp = static_cast<char *>(RTMemAlloc(cbFormatTmp));
    if (!pszFormatTmp)
        return VERR_NO_MEMORY;

    HGCMMsgGHDropped Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, HOST_DND_FN_GH_EVT_DROPPED, 4);
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.pvFormat.SetPtr(pszFormatTmp, cbFormatTmp);
    Msg.u.v3.cbFormat.SetUInt32(0);
    Msg.u.v3.uAction.SetUInt32(0);

    int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /** @todo Context ID not used yet. */
        if (pcbFormat)
            rc = Msg.u.v3.cbFormat.GetUInt32(pcbFormat);
        if (RT_SUCCESS(rc) && puAction)
            rc = Msg.u.v3.uAction.GetUInt32(puAction);

        if (RT_SUCCESS(rc))
        {
            *ppszFormat = RTStrDup(pszFormatTmp);
            if (!*ppszFormat)
                rc = VERR_NO_MEMORY;
        }
    }

    RTMemFree(pszFormatTmp);

    return rc;
}
#endif /* VBOX_WITH_DRAG_AND_DROP_GH */


/*********************************************************************************************************************************
*   Public functions                                                                                                             *
*********************************************************************************************************************************/

/**
 * Connects a DnD context to the DnD host service.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to connect.
 */
VBGLR3DECL(int) VbglR3DnDConnect(PVBGLR3GUESTDNDCMDCTX pCtx)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);

    /* Initialize header */
    int rc = VbglR3HGCMConnect("VBoxDragAndDropSvc", &pCtx->uClientID);
    if (RT_FAILURE(rc))
        return rc;
    Assert(pCtx->uClientID);

    /* Set the default protocol version we would like to use.
     * Deprecated since VBox 6.1.x, but let this set to 3 to (hopefully) not break things. */
    pCtx->uProtocolDeprecated = 3;

    pCtx->fHostFeatures  = VBOX_DND_HF_NONE;
    pCtx->fGuestFeatures = VBOX_DND_GF_NONE;

    /*
     * Get the VM's session ID.
     * This is not fatal in case we're running with an ancient VBox version.
     */
    pCtx->uSessionID = 0;
    int rc2 = VbglR3GetSessionId(&pCtx->uSessionID); RT_NOREF(rc2);
    LogFlowFunc(("uSessionID=%RU64, rc=%Rrc\n", pCtx->uSessionID, rc2));

    /*
     * Try sending the connect message to tell the protocol version to use.
     * Note: This might fail when the Guest Additions run on an older VBox host (< VBox 5.0) which
     *       does not implement this command.
     */
    HGCMMsgConnect Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_CONNECT, 3);
    Msg.u.v3.uContext.SetUInt32(0);                /** @todo Context ID not used yet. */
    Msg.u.v3.uProtocol.SetUInt32(pCtx->uProtocolDeprecated); /* Deprecated since VBox 6.1.x. */
    Msg.u.v3.uFlags.SetUInt32(0);                  /* Unused at the moment. */

    rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
    if (RT_SUCCESS(rc))
    {
        /* Set the protocol version we're going to use as told by the host. */
        rc = Msg.u.v3.uProtocol.GetUInt32(&pCtx->uProtocolDeprecated); AssertRC(rc);

        /*
         * Next is reporting our features.  If this fails, assume older host.
         */
        rc2 = VbglR3DnDReportFeatures(pCtx->uClientID, pCtx->fGuestFeatures, &pCtx->fHostFeatures);
        if (RT_SUCCESS(rc2))
        {
            LogRel2(("DnD: Guest features: %#RX64 - Host features: %#RX64\n",
                     pCtx->fGuestFeatures, pCtx->fHostFeatures));
        }
        else /* Failing here is not fatal; might be running with an older host. */
        {
            AssertLogRelMsg(rc2 == VERR_NOT_SUPPORTED || rc2 == VERR_NOT_IMPLEMENTED,
                            ("Reporting features failed: %Rrc\n", rc2));
        }

        pCtx->cbMaxChunkSize = DND_DEFAULT_CHUNK_SIZE; /** @todo Use a scratch buffer on the heap? */
    }
    else
        pCtx->uProtocolDeprecated = 0; /*  We're using protocol v0 (initial draft) as a fallback. */

    LogFlowFunc(("uClient=%RU32, uProtocol=%RU32, rc=%Rrc\n", pCtx->uClientID, pCtx->uProtocolDeprecated, rc));
    return rc;
}

/**
 * Disconnects a given DnD context from the DnD host service.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to disconnect.
 *                              The context is invalid afterwards on successful disconnection.
 */
VBGLR3DECL(int) VbglR3DnDDisconnect(PVBGLR3GUESTDNDCMDCTX pCtx)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);

    if (!pCtx->uClientID) /* Already disconnected? Bail out early. */
        return VINF_SUCCESS;

    int rc = VbglR3HGCMDisconnect(pCtx->uClientID);
    if (RT_SUCCESS(rc))
        pCtx->uClientID = 0;

    return rc;
}

/**
 * Reports features to the host and retrieve host feature set.
 *
 * @returns VBox status code.
 * @param   idClient        The client ID returned by VbglR3DnDConnect().
 * @param   fGuestFeatures  Features to report, VBOX_DND_GF_XXX.
 * @param   pfHostFeatures  Where to store the features VBOX_DND_HF_XXX.
 */
VBGLR3DECL(int) VbglR3DnDReportFeatures(uint32_t idClient, uint64_t fGuestFeatures, uint64_t *pfHostFeatures)
{
    int rc;
    do
    {
        struct
        {
            VBGLIOCHGCMCALL         Hdr;
            HGCMFunctionParameter   f64Features0;
            HGCMFunctionParameter   f64Features1;
        } Msg;
        VBGL_HGCM_HDR_INIT(&Msg.Hdr, idClient, GUEST_DND_FN_REPORT_FEATURES, 2);
        VbglHGCMParmUInt64Set(&Msg.f64Features0, fGuestFeatures);
        VbglHGCMParmUInt64Set(&Msg.f64Features1, VBOX_DND_GF_1_MUST_BE_ONE);

        rc = VbglR3HGCMCall(&Msg.Hdr, sizeof(Msg));
        if (RT_SUCCESS(rc))
        {
            Assert(Msg.f64Features0.type == VMMDevHGCMParmType_64bit);
            Assert(Msg.f64Features1.type == VMMDevHGCMParmType_64bit);
            if (Msg.f64Features1.u.value64 & VBOX_DND_GF_1_MUST_BE_ONE)
                rc = VERR_NOT_SUPPORTED;
            else if (pfHostFeatures)
                *pfHostFeatures = Msg.f64Features0.u.value64;
            break;
        }
    } while (rc == VERR_INTERRUPTED);
    return rc;

}

/**
 * Receives the next upcoming DnD event.
 *
 * This is the main function DnD clients call in order to implement any DnD functionality.
 * The purpose of it is to abstract the actual DnD protocol handling as much as possible from
 * the clients -- those only need to react to certain events, regardless of how the underlying
 * protocol actually is working.
 *
 * @returns VBox status code.
 * @param   pCtx                DnD context to work with.
 * @param   ppEvent             Next DnD event received on success; needs to be free'd by the client calling
 *                              VbglR3DnDEventFree() when done.
 */
VBGLR3DECL(int) VbglR3DnDEventGetNext(PVBGLR3GUESTDNDCMDCTX pCtx, PVBGLR3DNDEVENT *ppEvent)
{
    AssertPtrReturn(pCtx,    VERR_INVALID_POINTER);
    AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);

    PVBGLR3DNDEVENT pEvent = (PVBGLR3DNDEVENT)RTMemAllocZ(sizeof(VBGLR3DNDEVENT));
    if (!pEvent)
        return VERR_NO_MEMORY;

    uint32_t uMsg   = 0;
    uint32_t cParms = 0;
    int rc = vbglR3DnDGetNextMsgType(pCtx, &uMsg, &cParms, true /* fWait */);
    if (RT_SUCCESS(rc))
    {
        /* Check for VM session change. */
        uint64_t uSessionID;
        int rc2 = VbglR3GetSessionId(&uSessionID);
        if (   RT_SUCCESS(rc2)
            && (uSessionID != pCtx->uSessionID))
        {
            LogRel2(("DnD: VM session ID changed to %RU64\n", uSessionID));
            rc = VbglR3DnDDisconnect(pCtx);
            if (RT_SUCCESS(rc))
                rc = VbglR3DnDConnect(pCtx);
        }
    }

    if (rc == VERR_CANCELLED) /* Host service told us that we have to bail out. */
    {
        LogRel2(("DnD: Host service requested termination\n"));

        pEvent->enmType = VBGLR3DNDEVENTTYPE_QUIT;
        *ppEvent = pEvent;

        return VINF_SUCCESS;
    }

    if (RT_SUCCESS(rc))
    {
        LogFunc(("Handling uMsg=%RU32\n", uMsg));

        switch(uMsg)
        {
            case HOST_DND_FN_HG_EVT_ENTER:
            {
                rc = vbglR3DnDHGRecvAction(pCtx,
                                           uMsg,
                                           &pEvent->u.HG_Enter.uScreenID,
                                           NULL /* puXPos */,
                                           NULL /* puYPos */,
                                           NULL /* uDefAction */,
                                           &pEvent->u.HG_Enter.dndLstActionsAllowed,
                                           &pEvent->u.HG_Enter.pszFormats,
                                           &pEvent->u.HG_Enter.cbFormats);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_HG_ENTER;
                break;
            }
            case HOST_DND_FN_HG_EVT_MOVE:
            {
                rc = vbglR3DnDHGRecvAction(pCtx,
                                           uMsg,
                                           NULL /* puScreenId */,
                                           &pEvent->u.HG_Move.uXpos,
                                           &pEvent->u.HG_Move.uYpos,
                                           &pEvent->u.HG_Move.dndActionDefault,
                                           NULL /* puAllActions */,
                                           NULL /* pszFormats */,
                                           NULL /* pcbFormats */);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_HG_MOVE;
                break;
            }
            case HOST_DND_FN_HG_EVT_DROPPED:
            {
                rc = vbglR3DnDHGRecvAction(pCtx,
                                           uMsg,
                                           NULL /* puScreenId */,
                                           &pEvent->u.HG_Drop.uXpos,
                                           &pEvent->u.HG_Drop.uYpos,
                                           &pEvent->u.HG_Drop.dndActionDefault,
                                           NULL /* puAllActions */,
                                           NULL /* pszFormats */,
                                           NULL /* pcbFormats */);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_HG_DROP;
                break;
            }
            case HOST_DND_FN_HG_EVT_LEAVE:
            {
                rc = vbglR3DnDHGRecvLeave(pCtx);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_HG_LEAVE;
                break;
            }
            case HOST_DND_FN_HG_SND_DATA_HDR:
            {
                rc = vbglR3DnDHGRecvDataMain(pCtx, &pEvent->u.HG_Received.Meta);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_HG_RECEIVE;
                break;
            }
            case HOST_DND_FN_HG_SND_DIR:
                RT_FALL_THROUGH();
            case HOST_DND_FN_HG_SND_FILE_HDR:
                RT_FALL_THROUGH();
            case HOST_DND_FN_HG_SND_FILE_DATA:
            {
                /*
                 * All messages for this block are handled internally
                 * by vbglR3DnDHGRecvDataMain(), see above.
                 *
                 * So if we land here our code is buggy.
                 */
                rc = VERR_WRONG_ORDER;
                break;
            }
            case HOST_DND_FN_CANCEL:
            {
                rc = vbglR3DnDHGRecvCancel(pCtx);
                if (RT_SUCCESS(rc))
                    rc = VERR_CANCELLED; /* Will emit a cancel event below. */
                break;
            }
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
            case HOST_DND_FN_GH_REQ_PENDING:
            {
                rc = vbglR3DnDGHRecvPending(pCtx, &pEvent->u.GH_IsPending.uScreenID);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_GH_REQ_PENDING;
                break;
            }
            case HOST_DND_FN_GH_EVT_DROPPED:
            {
                rc = vbglR3DnDGHRecvDropped(pCtx,
                                            &pEvent->u.GH_Drop.pszFormat,
                                            &pEvent->u.GH_Drop.cbFormat,
                                            &pEvent->u.GH_Drop.dndActionRequested);
                if (RT_SUCCESS(rc))
                    pEvent->enmType = VBGLR3DNDEVENTTYPE_GH_DROP;
                break;
            }
#endif
            default:
            {
                rc = VERR_NOT_SUPPORTED;
                break;
            }
        }
    }

    if (RT_FAILURE(rc))
    {
        /* Current operation cancelled? Set / overwrite event type and tell the caller. */
        if (rc == VERR_CANCELLED)
        {
            pEvent->enmType = VBGLR3DNDEVENTTYPE_CANCEL;
            rc              = VINF_SUCCESS; /* Deliver the event to the caller. */
        }
        else
        {
            VbglR3DnDEventFree(pEvent);
            LogRel(("DnD: Handling message %s (%#x) failed with %Rrc\n", DnDHostMsgToStr(uMsg), uMsg, rc));
        }
    }

    if (RT_SUCCESS(rc))
        *ppEvent = pEvent;

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Frees (destroys) a formerly allocated DnD event.
 *
 * @returns IPRT status code.
 * @param   pEvent              Event to free (destroy).
 */
VBGLR3DECL(void) VbglR3DnDEventFree(PVBGLR3DNDEVENT pEvent)
{
    if (!pEvent)
        return;

    /* Some messages require additional cleanup. */
    switch (pEvent->enmType)
    {
        case VBGLR3DNDEVENTTYPE_HG_ENTER:
        {
            if (pEvent->u.HG_Enter.pszFormats)
                RTStrFree(pEvent->u.HG_Enter.pszFormats);
            break;
        }

#ifdef VBOX_WITH_DRAG_AND_DROP_GH
        case VBGLR3DNDEVENTTYPE_GH_DROP:
        {
            if (pEvent->u.GH_Drop.pszFormat)
                RTStrFree(pEvent->u.GH_Drop.pszFormat);
            break;
        }
#endif
        case VBGLR3DNDEVENTTYPE_HG_RECEIVE:
        {
            PVBGLR3GUESTDNDMETADATA pMeta = &pEvent->u.HG_Received.Meta;
            switch (pMeta->enmType)
            {
                case VBGLR3GUESTDNDMETADATATYPE_RAW:
                {
                    if (pMeta->u.Raw.pvMeta)
                    {
                        Assert(pMeta->u.Raw.cbMeta);
                        RTMemFree(pMeta->u.Raw.pvMeta);
                        pMeta->u.Raw.cbMeta = 0;
                    }
                    break;
                }

                case VBGLR3GUESTDNDMETADATATYPE_URI_LIST:
                {
                    DnDTransferListDestroy(&pMeta->u.URI.Transfer);
                    break;
                }

                default:
                    break;
            }
            break;
        }

        default:
            break;
    }

    RTMemFree(pEvent);
    pEvent = NULL;
}

VBGLR3DECL(int) VbglR3DnDHGSendAckOp(PVBGLR3GUESTDNDCMDCTX pCtx, VBOXDNDACTION dndAction)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);

    HGCMMsgHGAck Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_HG_ACK_OP, 2);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.uAction.SetUInt32(dndAction);

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

/**
 * Host -> Guest
 * Requests the actual DnD data to be sent from the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pcszFormat          Format to request the data from the host in.
 */
VBGLR3DECL(int) VbglR3DnDHGSendReqData(PVBGLR3GUESTDNDCMDCTX pCtx, const char* pcszFormat)
{
    AssertPtrReturn(pCtx,       VERR_INVALID_POINTER);
    AssertPtrReturn(pcszFormat, VERR_INVALID_POINTER);
    if (!RTStrIsValidEncoding(pcszFormat))
        return VERR_INVALID_PARAMETER;

    const uint32_t cbFormat = (uint32_t)strlen(pcszFormat) + 1; /* Include termination */

    HGCMMsgHGReqData Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_HG_REQ_DATA, 3);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.pvFormat.SetPtr((void*)pcszFormat, cbFormat);
    Msg.u.v3.cbFormat.SetUInt32(cbFormat);

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

/**
 * Host -> Guest
 * Reports back its progress back to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   uStatus             DnD status to report.
 * @param   uPercent            Overall progress (in percent) to report.
 * @param   rcErr               Error code (IPRT-style) to report.
 */
VBGLR3DECL(int) VbglR3DnDHGSendProgress(PVBGLR3GUESTDNDCMDCTX pCtx, uint32_t uStatus, uint8_t uPercent, int rcErr)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
    AssertReturn(uStatus > DND_PROGRESS_UNKNOWN, VERR_INVALID_PARAMETER);

    HGCMMsgHGProgress Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_HG_EVT_PROGRESS, 4);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.uStatus.SetUInt32(uStatus);
    Msg.u.v3.uPercent.SetUInt32(uPercent);
    Msg.u.v3.rc.SetUInt32((uint32_t)rcErr); /* uint32_t vs. int. */

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

#ifdef VBOX_WITH_DRAG_AND_DROP_GH
/**
 * Guest -> Host
 * Acknowledges that there currently is a drag'n drop operation in progress on the guest,
 * which eventually could be dragged over to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                 DnD context to use.
 * @param   dndActionDefault     Default action for the operation to report.
 * @param   dndLstActionsAllowed All available actions for the operation to report.
 * @param   pcszFormats          Available formats for the operation to report.
 * @param   cbFormats            Size (in bytes) of formats to report.
 */
VBGLR3DECL(int) VbglR3DnDGHSendAckPending(PVBGLR3GUESTDNDCMDCTX pCtx,
                                          VBOXDNDACTION dndActionDefault, VBOXDNDACTIONLIST dndLstActionsAllowed,
                                          const char* pcszFormats, uint32_t cbFormats)
{
    AssertPtrReturn(pCtx,        VERR_INVALID_POINTER);
    AssertPtrReturn(pcszFormats, VERR_INVALID_POINTER);
    AssertReturn(cbFormats,      VERR_INVALID_PARAMETER);

    if (!RTStrIsValidEncoding(pcszFormats))
        return VERR_INVALID_UTF8_ENCODING;

    HGCMMsgGHAckPending Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_GH_ACK_PENDING, 5);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.uDefAction.SetUInt32(dndActionDefault);
    Msg.u.v3.uAllActions.SetUInt32(dndLstActionsAllowed);
    Msg.u.v3.pvFormats.SetPtr((void*)pcszFormats, cbFormats);
    Msg.u.v3.cbFormats.SetUInt32(cbFormats);

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

/**
 * Guest -> Host
 * Utility function to send DnD data from guest to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pvData              Data block to send.
 * @param   cbData              Size (in bytes) of data block to send.
 * @param   pDataHdr            Data header to use -- needed for accounting.
 */
static int vbglR3DnDGHSendDataInternal(PVBGLR3GUESTDNDCMDCTX pCtx,
                                       void *pvData, uint64_t cbData, PVBOXDNDSNDDATAHDR pDataHdr)
{
    AssertPtrReturn(pCtx,     VERR_INVALID_POINTER);
    AssertPtrReturn(pvData,   VERR_INVALID_POINTER);
    AssertReturn(cbData,      VERR_INVALID_PARAMETER);
    AssertPtrReturn(pDataHdr, VERR_INVALID_POINTER);

    HGCMMsgGHSendDataHdr MsgHdr;
    VBGL_HGCM_HDR_INIT(&MsgHdr.hdr, pCtx->uClientID, GUEST_DND_FN_GH_SND_DATA_HDR, 12);
    MsgHdr.uContext.SetUInt32(0);                           /** @todo Not used yet. */
    MsgHdr.uFlags.SetUInt32(0);                             /** @todo Not used yet. */
    MsgHdr.uScreenId.SetUInt32(0);                          /** @todo Not used for guest->host (yet). */
    MsgHdr.cbTotal.SetUInt64(pDataHdr->cbTotal);
    MsgHdr.cbMeta.SetUInt32(pDataHdr->cbMeta);
    MsgHdr.pvMetaFmt.SetPtr(pDataHdr->pvMetaFmt, pDataHdr->cbMetaFmt);
    MsgHdr.cbMetaFmt.SetUInt32(pDataHdr->cbMetaFmt);
    MsgHdr.cObjects.SetUInt64(pDataHdr->cObjects);
    MsgHdr.enmCompression.SetUInt32(0);                     /** @todo Not used yet. */
    MsgHdr.enmChecksumType.SetUInt32(RTDIGESTTYPE_INVALID); /** @todo Not used yet. */
    MsgHdr.pvChecksum.SetPtr(NULL, 0);                      /** @todo Not used yet. */
    MsgHdr.cbChecksum.SetUInt32(0);                         /** @todo Not used yet. */

    int rc = VbglR3HGCMCall(&MsgHdr.hdr, sizeof(MsgHdr));

    LogFlowFunc(("cbTotal=%RU64, cbMeta=%RU32, cObjects=%RU64, rc=%Rrc\n",
                 pDataHdr->cbTotal, pDataHdr->cbMeta, pDataHdr->cObjects, rc));

    if (RT_SUCCESS(rc))
    {
        HGCMMsgGHSendData MsgData;
        VBGL_HGCM_HDR_INIT(&MsgData.hdr, pCtx->uClientID, GUEST_DND_FN_GH_SND_DATA, 5);
        MsgData.u.v3.uContext.SetUInt32(0);      /** @todo Not used yet. */
        MsgData.u.v3.pvChecksum.SetPtr(NULL, 0); /** @todo Not used yet. */
        MsgData.u.v3.cbChecksum.SetUInt32(0);    /** @todo Not used yet. */

        uint32_t       cbCurChunk;
        const uint32_t cbMaxChunk = pCtx->cbMaxChunkSize;
        uint32_t       cbSent     = 0;

        while (cbSent < cbData)
        {
            cbCurChunk = RT_MIN(cbData - cbSent, cbMaxChunk);
            MsgData.u.v3.pvData.SetPtr(static_cast<uint8_t *>(pvData) + cbSent, cbCurChunk);
            MsgData.u.v3.cbData.SetUInt32(cbCurChunk);

            rc = VbglR3HGCMCall(&MsgData.hdr, sizeof(MsgData));
            if (RT_FAILURE(rc))
                break;

            cbSent += cbCurChunk;
        }

        LogFlowFunc(("cbMaxChunk=%RU32, cbData=%RU64, cbSent=%RU32, rc=%Rrc\n",
                     cbMaxChunk, cbData, cbSent, rc));

        if (RT_SUCCESS(rc))
            Assert(cbSent == cbData);
    }

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Guest -> Host
 * Utility function to send a guest directory to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pObj                transfer object containing the directory to send.
 */
static int vbglR3DnDGHSendDir(PVBGLR3GUESTDNDCMDCTX pCtx, DNDTRANSFEROBJECT *pObj)
{
    AssertPtrReturn(pObj,                                         VERR_INVALID_POINTER);
    AssertPtrReturn(pCtx,                                         VERR_INVALID_POINTER);
    AssertReturn(DnDTransferObjectGetType(pObj) == DNDTRANSFEROBJTYPE_DIRECTORY, VERR_INVALID_PARAMETER);

    const char *pcszPath = DnDTransferObjectGetDestPath(pObj);
    const size_t cbPath  = RTStrNLen(pcszPath, RTPATH_MAX) + 1 /* Include termination. */;
    const RTFMODE fMode  = DnDTransferObjectGetMode(pObj);

    LogFlowFunc(("strDir=%s (%zu bytes), fMode=0x%x\n", pcszPath, cbPath, fMode));

    if (cbPath > RTPATH_MAX + 1) /* Can't happen, but check anyway. */
        return VERR_INVALID_PARAMETER;

    HGCMMsgGHSendDir Msg;
    VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_GH_SND_DIR, 4);
    /** @todo Context ID not used yet. */
    Msg.u.v3.uContext.SetUInt32(0);
    Msg.u.v3.pvName.SetPtr((void *)pcszPath, (uint32_t)cbPath);
    Msg.u.v3.cbName.SetUInt32((uint32_t)cbPath);
    Msg.u.v3.fMode.SetUInt32(fMode);

    return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
}

/**
 * Guest -> Host
 * Utility function to send a file from the guest to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pObj                Transfer object containing the file to send.
 */
static int vbglR3DnDGHSendFile(PVBGLR3GUESTDNDCMDCTX pCtx, PDNDTRANSFEROBJECT pObj)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
    AssertPtrReturn(pObj, VERR_INVALID_POINTER);
    AssertReturn(DnDTransferObjectIsOpen(pObj) == false, VERR_INVALID_STATE);
    AssertReturn(DnDTransferObjectGetType(pObj) == DNDTRANSFEROBJTYPE_FILE, VERR_INVALID_PARAMETER);

    uint64_t fOpen = RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE;

    int rc = DnDTransferObjectOpen(pObj, fOpen, 0 /* fMode */, DNDTRANSFEROBJECT_FLAGS_NONE);
    if (RT_FAILURE(rc))
        return rc;

    uint32_t cbBuf = pCtx->cbMaxChunkSize;
    AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
    void *pvBuf = RTMemAlloc(cbBuf); /** @todo Make this buffer part of PVBGLR3GUESTDNDCMDCTX? */
    if (!pvBuf)
    {
        int rc2 = DnDTransferObjectClose(pObj);
        AssertRC(rc2);
        return VERR_NO_MEMORY;
    }

    const char *pcszPath  = DnDTransferObjectGetDestPath(pObj);
    const size_t cchPath  = RTStrNLen(pcszPath, RTPATH_MAX);
    const uint64_t cbSize = DnDTransferObjectGetSize(pObj);
    const RTFMODE fMode   = DnDTransferObjectGetMode(pObj);

    LogFlowFunc(("strFile=%s (%zu), cbSize=%RU64, fMode=0x%x\n", pcszPath, cchPath, cbSize, fMode));

    HGCMMsgGHSendFileHdr MsgHdr;
    VBGL_HGCM_HDR_INIT(&MsgHdr.hdr, pCtx->uClientID, GUEST_DND_FN_GH_SND_FILE_HDR, 6);
    MsgHdr.uContext.SetUInt32(0);                                                    /* Context ID; unused at the moment. */
    MsgHdr.pvName.SetPtr((void *)pcszPath, (uint32_t)(cchPath + 1));                 /* Include termination. */
    MsgHdr.cbName.SetUInt32((uint32_t)(cchPath + 1));                                /* Ditto. */
    MsgHdr.uFlags.SetUInt32(0);                                                      /* Flags; unused at the moment. */
    MsgHdr.fMode.SetUInt32(fMode);                                                   /* File mode */
    MsgHdr.cbTotal.SetUInt64(cbSize);                                                /* File size (in bytes). */

    rc = VbglR3HGCMCall(&MsgHdr.hdr, sizeof(MsgHdr));

    LogFlowFunc(("Sending file header resulted in %Rrc\n", rc));

    if (RT_SUCCESS(rc))
    {
        /*
         * Send the actual file data, chunk by chunk.
         */
        HGCMMsgGHSendFileData Msg;
        VBGL_HGCM_HDR_INIT(&Msg.hdr, pCtx->uClientID, GUEST_DND_FN_GH_SND_FILE_DATA, 5);
        Msg.u.v3.uContext.SetUInt32(0);
        Msg.u.v3.pvChecksum.SetPtr(NULL, 0);
        Msg.u.v3.cbChecksum.SetUInt32(0);

        uint64_t cbToReadTotal  = cbSize;
        uint64_t cbWrittenTotal = 0;
        while (cbToReadTotal)
        {
            uint32_t cbToRead = RT_MIN(cbToReadTotal, cbBuf);
            uint32_t cbRead   = 0;
            if (cbToRead)
                rc = DnDTransferObjectRead(pObj, pvBuf, cbToRead, &cbRead);

            LogFlowFunc(("cbToReadTotal=%RU64, cbToRead=%RU32, cbRead=%RU32, rc=%Rrc\n",
                         cbToReadTotal, cbToRead, cbRead, rc));

            if (   RT_SUCCESS(rc)
                && cbRead)
            {
                Msg.u.v3.pvData.SetPtr(pvBuf, cbRead);
                Msg.u.v3.cbData.SetUInt32(cbRead);
                /** @todo Calculate + set checksums. */

                rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
            }

            if (RT_FAILURE(rc))
            {
                LogFlowFunc(("Reading failed with rc=%Rrc\n", rc));
                break;
            }

            Assert(cbRead  <= cbToReadTotal);
            cbToReadTotal  -= cbRead;
            cbWrittenTotal += cbRead;

            LogFlowFunc(("%RU64/%RU64 -- %RU8%%\n", cbWrittenTotal, cbSize, cbWrittenTotal * 100 / cbSize));
        };
    }

    RTMemFree(pvBuf);
    int rc2 = DnDTransferObjectClose(pObj);
    if (RT_SUCCESS(rc))
        rc = rc2;

    LogFlowFuncLeaveRC(rc);
    return rc;
}

/**
 * Guest -> Host
 * Utility function to send a transfer object from guest to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pObj                Transfer object to send from guest to the host.
 */
static int vbglR3DnDGHSendURIObject(PVBGLR3GUESTDNDCMDCTX pCtx, PDNDTRANSFEROBJECT pObj)
{
    AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
    AssertPtrReturn(pObj, VERR_INVALID_POINTER);

    int rc;

    const DNDTRANSFEROBJTYPE enmType = DnDTransferObjectGetType(pObj);

    switch (enmType)
    {
        case DNDTRANSFEROBJTYPE_DIRECTORY:
            rc = vbglR3DnDGHSendDir(pCtx, pObj);
            break;

        case DNDTRANSFEROBJTYPE_FILE:
            rc = vbglR3DnDGHSendFile(pCtx, pObj);
            break;

        default:
            AssertMsgFailed(("Object type %ld not implemented\n", enmType));
            rc = VERR_NOT_IMPLEMENTED;
            break;
    }

    return rc;
}

/**
 * Guest -> Host
 * Utility function to send raw data from guest to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pvData              Block to raw data to send.
 * @param   cbData              Size (in bytes) of raw data to send.
 */
static int vbglR3DnDGHSendRawData(PVBGLR3GUESTDNDCMDCTX pCtx, void *pvData, size_t cbData)
{
    AssertPtrReturn(pCtx,   VERR_INVALID_POINTER);
    AssertPtrReturn(pvData, VERR_INVALID_POINTER);
    /* cbData can be 0. */

    VBOXDNDDATAHDR dataHdr;
    RT_ZERO(dataHdr);

    dataHdr.cbMeta  = (uint32_t)cbData;
    dataHdr.cbTotal = cbData;

    return vbglR3DnDGHSendDataInternal(pCtx, pvData, cbData, &dataHdr);
}

/**
 * Guest -> Host
 * Utility function to send transfer data from guest to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pTransferList       Dnd transfer list to send.
 */
static int vbglR3DnDGHSendTransferData(PVBGLR3GUESTDNDCMDCTX pCtx, PDNDTRANSFERLIST pTransferList)
{
    AssertPtrReturn(pCtx,VERR_INVALID_POINTER);
    AssertPtrReturn(pTransferList, VERR_INVALID_POINTER);

    /*
     * Send the (meta) data; in case of URIs it's the root entries of a
     * transfer list the host needs to know upfront to set up the drag'n drop operation.
     */
    char *pszList = NULL;
    size_t cbList;
    int rc = DnDTransferListGetRoots(pTransferList, DNDTRANSFERLISTFMT_URI, &pszList, &cbList);
    if (RT_FAILURE(rc))
        return rc;

    void    *pvURIList = (void *)pszList;
    uint32_t cbURLIist = (uint32_t)cbList;

    /* The total size also contains the size of the meta data. */
    uint64_t cbTotal  = cbURLIist;
             cbTotal += DnDTransferListObjTotalBytes(pTransferList);

    /* We're going to send a transfer list in text format. */
    const char     szMetaFmt[] = "text/uri-list";
    const uint32_t cbMetaFmt   = (uint32_t)strlen(szMetaFmt) + 1; /* Include termination. */

    VBOXDNDDATAHDR dataHdr;
    dataHdr.uFlags    = 0; /* Flags not used yet. */
    dataHdr.cbTotal   = cbTotal;
    dataHdr.cbMeta    = cbURLIist;
    dataHdr.pvMetaFmt = (void *)szMetaFmt;
    dataHdr.cbMetaFmt = cbMetaFmt;
    dataHdr.cObjects  = DnDTransferListObjCount(pTransferList);

    rc = vbglR3DnDGHSendDataInternal(pCtx, pvURIList, cbURLIist, &dataHdr);

    if (RT_SUCCESS(rc))
    {
        while (DnDTransferListObjCount(pTransferList))
        {
            PDNDTRANSFEROBJECT pObj = DnDTransferListObjGetFirst(pTransferList);

            rc = vbglR3DnDGHSendURIObject(pCtx, pObj);
            if (RT_FAILURE(rc))
                break;

            DnDTransferListObjRemoveFirst(pTransferList);
        }

        Assert(DnDTransferListObjCount(pTransferList) == 0);
    }

    return rc;
}

/**
 * Guest -> Host
 * Sends data, which either can be raw or URI data, from guest to the host. This function
 * initiates the actual data transfer from guest to the host.
 *
 * @returns IPRT status code.
 * @param   pCtx                DnD context to use.
 * @param   pszFormat           In which format the data will be sent.
 * @param   pvData              Data block to send.
 *                              For URI data this must contain the absolute local URI paths, separated by DND_PATH_SEPARATOR_STR.
 * @param   cbData              Size (in bytes) of data block to send.
 */
VBGLR3DECL(int) VbglR3DnDGHSendData(PVBGLR3GUESTDNDCMDCTX pCtx, const char *pszFormat, void *pvData, uint32_t cbData)
{
    AssertPtrReturn(pCtx,      VERR_INVALID_POINTER);
    AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
    AssertPtrReturn(pvData,    VERR_INVALID_POINTER);
    AssertReturn(cbData,       VERR_INVALID_PARAMETER);

    LogFlowFunc(("pszFormat=%s, pvData=%p, cbData=%RU32\n", pszFormat, pvData, cbData));

    LogRel2(("DnD: Sending %RU32 bytes meta data in format '%s'\n", cbData, pszFormat));

    int rc;
    if (DnDMIMEHasFileURLs(pszFormat, strlen(pszFormat)))
    {
        DNDTRANSFERLIST lstTransfer;
        RT_ZERO(lstTransfer);

        rc = DnDTransferListInit(&lstTransfer);
        if (RT_SUCCESS(rc))
        {
            /** @todo Add symlink support (DNDTRANSFERLIST_FLAGS_RESOLVE_SYMLINKS) here. */
            /** @todo Add lazy loading (DNDTRANSFERLIST_FLAGS_LAZY) here. */
            const DNDTRANSFERLISTFLAGS fFlags = DNDTRANSFERLIST_FLAGS_RECURSIVE;

            rc = DnDTransferListAppendPathsFromBuffer(&lstTransfer, DNDTRANSFERLISTFMT_URI, (const char *)pvData, cbData,
                                                      DND_PATH_SEPARATOR_STR, fFlags);
            if (RT_SUCCESS(rc))
                rc = vbglR3DnDGHSendTransferData(pCtx, &lstTransfer);
            DnDTransferListDestroy(&lstTransfer);
        }
    }
    else
        rc = vbglR3DnDGHSendRawData(pCtx, pvData, cbData);

    if (RT_FAILURE(rc))
    {
        LogRel(("DnD: Sending data failed with rc=%Rrc\n", rc));

        if (rc != VERR_CANCELLED)
        {
            int rc2 = VbglR3DnDSendError(pCtx, rc);
            if (RT_FAILURE(rc2))
                LogFlowFunc(("Unable to send error (%Rrc) to host, rc=%Rrc\n", rc, rc2));
        }
    }

    return rc;
}
#endif /* VBOX_WITH_DRAG_AND_DROP_GH */