1 | /* $Id*/
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard - Meta data handling functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
23 | #include <VBox/GuestHost/SharedClipboard-uri.h>
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Initializes a clipboard meta data struct.
|
---|
27 | *
|
---|
28 | * @returns VBox status code.
|
---|
29 | * @param pMeta Meta data struct to initialize.
|
---|
30 | */
|
---|
31 | int SharedClipboardMetaDataInit(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
32 | {
|
---|
33 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
34 |
|
---|
35 | pMeta->pvMeta = NULL;
|
---|
36 | pMeta->cbMeta = 0;
|
---|
37 | pMeta->cbUsed = 0;
|
---|
38 |
|
---|
39 | return VINF_SUCCESS;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Destroys a clipboard meta data struct by free'ing all its data.
|
---|
44 | *
|
---|
45 | * @param pMeta Meta data struct to destroy.
|
---|
46 | */
|
---|
47 | void SharedClipboardMetaDataDestroy(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
48 | {
|
---|
49 | AssertPtrReturnVoid(pMeta);
|
---|
50 |
|
---|
51 | if (!pMeta)
|
---|
52 | return;
|
---|
53 |
|
---|
54 | if (pMeta->pvMeta)
|
---|
55 | {
|
---|
56 | Assert(pMeta->cbMeta);
|
---|
57 |
|
---|
58 | RTMemFree(pMeta->pvMeta);
|
---|
59 |
|
---|
60 | pMeta->pvMeta = NULL;
|
---|
61 | pMeta->cbMeta = 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 | pMeta->cbUsed = 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Adds new meta data to a meta data struct.
|
---|
69 | *
|
---|
70 | * @returns VBox status code.
|
---|
71 | * @param pMeta Meta data struct to add data to.
|
---|
72 | */
|
---|
73 | int SharedClipboardMetaDataAdd(PSHAREDCLIPBOARDMETADATA pMeta, const void *pvDataAdd, uint32_t cbDataAdd)
|
---|
74 | {
|
---|
75 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
76 |
|
---|
77 | LogFlowFunc(("pvDataAdd=%p, cbDatadd=%RU32\n", pvDataAdd, cbDataAdd));
|
---|
78 |
|
---|
79 | if (!cbDataAdd)
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | AssertPtrReturn(pvDataAdd, VERR_INVALID_POINTER);
|
---|
82 |
|
---|
83 | int rc = SharedClipboardMetaDataResize(pMeta, pMeta->cbMeta + cbDataAdd);
|
---|
84 | if (RT_FAILURE(rc))
|
---|
85 | return rc;
|
---|
86 |
|
---|
87 | Assert(pMeta->cbMeta >= pMeta->cbUsed + cbDataAdd);
|
---|
88 | memcpy((uint8_t *)pMeta->pvMeta + pMeta->cbUsed, pvDataAdd, cbDataAdd);
|
---|
89 |
|
---|
90 | pMeta->cbUsed += cbDataAdd;
|
---|
91 |
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Resizes the data buffer of a meta data struct.
|
---|
97 | * Note: At the moment only supports growing the data buffer.
|
---|
98 | *
|
---|
99 | * @returns VBox status code.
|
---|
100 | * @param pMeta Meta data struct to resize.
|
---|
101 | * @param cbNewSize New size (in bytes) to use for resizing.
|
---|
102 | */
|
---|
103 | int SharedClipboardMetaDataResize(PSHAREDCLIPBOARDMETADATA pMeta, uint32_t cbNewSize)
|
---|
104 | {
|
---|
105 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
106 |
|
---|
107 | if (!cbNewSize)
|
---|
108 | {
|
---|
109 | SharedClipboardMetaDataDestroy(pMeta);
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (cbNewSize == pMeta->cbMeta)
|
---|
114 | return VINF_SUCCESS;
|
---|
115 |
|
---|
116 | if (cbNewSize > _32M) /* Meta data can be up to 32MB. */
|
---|
117 | return VERR_INVALID_PARAMETER;
|
---|
118 |
|
---|
119 | void *pvTmp = NULL;
|
---|
120 | if (!pMeta->cbMeta)
|
---|
121 | {
|
---|
122 | Assert(pMeta->cbUsed == 0);
|
---|
123 | pvTmp = RTMemAllocZ(cbNewSize);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | AssertPtr(pMeta->pvMeta);
|
---|
128 | pvTmp = RTMemRealloc(pMeta->pvMeta, cbNewSize);
|
---|
129 | RT_BZERO(pvTmp, cbNewSize);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (pvTmp)
|
---|
133 | {
|
---|
134 | pMeta->pvMeta = pvTmp;
|
---|
135 | pMeta->cbMeta = cbNewSize;
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | }
|
---|
138 |
|
---|
139 | return VERR_NO_MEMORY;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Returns the actual free bytes of a meta data struct.
|
---|
144 | *
|
---|
145 | * @returns Actual free bytes of a meta data struct.
|
---|
146 | * @param pMeta Meta data struct to return free bytes for.
|
---|
147 | */
|
---|
148 | size_t SharedClipboardMetaDataGetFree(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
149 | {
|
---|
150 | AssertPtrReturn(pMeta, 0);
|
---|
151 | return pMeta->cbMeta - pMeta->cbUsed;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Returns the actual used bytes of a meta data struct.
|
---|
156 | *
|
---|
157 | * @returns Actual used bytes of a meta data struct.
|
---|
158 | * @param pMeta Meta data struct to return used bytes for.
|
---|
159 | */
|
---|
160 | size_t SharedClipboardMetaDataGetUsed(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
161 | {
|
---|
162 | AssertPtrReturn(pMeta, 0);
|
---|
163 | return pMeta->cbUsed;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Returns the overall (allocated) size in bytes of a meta data struct.
|
---|
168 | *
|
---|
169 | * @returns Overall (allocated) size of a meta data struct.
|
---|
170 | * @param pMeta Meta data struct to return size for.
|
---|
171 | */
|
---|
172 | size_t SharedClipboardMetaDataGetSize(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
173 | {
|
---|
174 | AssertPtrReturn(pMeta, 0);
|
---|
175 | return pMeta->cbMeta;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Returns the a mutable raw pointer to the actual meta data.
|
---|
180 | *
|
---|
181 | * @returns Mutable raw pointer to the actual meta data.
|
---|
182 | * @param pMeta Meta data struct to return mutable data pointer for.
|
---|
183 | */
|
---|
184 | void *SharedClipboardMetaDataMutableRaw(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
185 | {
|
---|
186 | return pMeta->pvMeta;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Returns the a const'ed raw pointer to the actual meta data.
|
---|
191 | *
|
---|
192 | * @returns Const'ed raw pointer to the actual meta data.
|
---|
193 | * @param pMeta Meta data struct to return const'ed data pointer for.
|
---|
194 | */
|
---|
195 | const void *SharedClipboardMetaDataRaw(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
196 | {
|
---|
197 | return pMeta->pvMeta;
|
---|
198 | }
|
---|
199 |
|
---|