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 | #include <iprt/path.h>
|
---|
26 | #include <iprt/uri.h>
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Initializes a clipboard meta data struct.
|
---|
30 | *
|
---|
31 | * @returns VBox status code.
|
---|
32 | * @param pMeta Meta data struct to initialize.
|
---|
33 | * @param enmFmt Meta data format to use.
|
---|
34 | */
|
---|
35 | int SharedClipboardMetaDataInit(PSHAREDCLIPBOARDMETADATA pMeta, SHAREDCLIPBOARDMETADATAFMT enmFmt)
|
---|
36 | {
|
---|
37 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
38 |
|
---|
39 | pMeta->enmFmt = enmFmt;
|
---|
40 | pMeta->pvMeta = NULL;
|
---|
41 | pMeta->cbMeta = 0;
|
---|
42 | pMeta->cbUsed = 0;
|
---|
43 | pMeta->cbOffRead = 0;
|
---|
44 |
|
---|
45 | return VINF_SUCCESS;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Destroys a clipboard meta data struct by free'ing all its data, internal version.
|
---|
50 | *
|
---|
51 | * @param pMeta Meta data struct to destroy.
|
---|
52 | */
|
---|
53 | static void sharedClipboardMetaDataDestroyInternal(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
54 | {
|
---|
55 | if (!pMeta)
|
---|
56 | return;
|
---|
57 |
|
---|
58 | if (pMeta->pvMeta)
|
---|
59 | {
|
---|
60 | Assert(pMeta->cbMeta);
|
---|
61 |
|
---|
62 | RTMemFree(pMeta->pvMeta);
|
---|
63 |
|
---|
64 | pMeta->pvMeta = NULL;
|
---|
65 | pMeta->cbMeta = 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | pMeta->cbUsed = 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Destroys a clipboard meta data struct by free'ing all its data.
|
---|
73 | *
|
---|
74 | * @param pMeta Meta data struct to destroy.
|
---|
75 | */
|
---|
76 | void SharedClipboardMetaDataDestroy(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
77 | {
|
---|
78 | sharedClipboardMetaDataDestroyInternal(pMeta);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Frees a clipboard meta data struct.
|
---|
83 | *
|
---|
84 | * @param pMeta Meta data struct to free.
|
---|
85 | */
|
---|
86 | void SharedClipboardMetaDataFree(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
87 | {
|
---|
88 | if (pMeta)
|
---|
89 | {
|
---|
90 | sharedClipboardMetaDataDestroyInternal(pMeta);
|
---|
91 |
|
---|
92 | RTMemFree(pMeta);
|
---|
93 | pMeta = NULL;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Adds new meta data to a meta data struct.
|
---|
99 | *
|
---|
100 | * @returns VBox status code.
|
---|
101 | * @param pMeta Meta data struct to add data to.
|
---|
102 | */
|
---|
103 | int SharedClipboardMetaDataAdd(PSHAREDCLIPBOARDMETADATA pMeta, const void *pvDataAdd, uint32_t cbDataAdd)
|
---|
104 | {
|
---|
105 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
106 |
|
---|
107 | LogFlowFunc(("pvDataAdd=%p, cbDatadd=%RU32\n", pvDataAdd, cbDataAdd));
|
---|
108 |
|
---|
109 | if (!cbDataAdd)
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | AssertPtrReturn(pvDataAdd, VERR_INVALID_POINTER);
|
---|
112 |
|
---|
113 | int rc = SharedClipboardMetaDataResize(pMeta, pMeta->cbMeta + cbDataAdd);
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | return rc;
|
---|
116 |
|
---|
117 | Assert(pMeta->cbMeta >= pMeta->cbUsed + cbDataAdd);
|
---|
118 | memcpy((uint8_t *)pMeta->pvMeta + pMeta->cbUsed, pvDataAdd, cbDataAdd);
|
---|
119 |
|
---|
120 | pMeta->cbUsed += cbDataAdd;
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Converts data to a specific meta data format.
|
---|
127 | *
|
---|
128 | * @returns VBox status code. VERR_NOT_SUPPORTED if the format is unknown.
|
---|
129 | * @param pvData Pointer to data to convert.
|
---|
130 | * @param cbData Size (in bytes) of data to convert.
|
---|
131 | * @param enmFmt Meta data format to convert data to.
|
---|
132 | * @param ppvData Where to store the converted data on success. Caller must free the data accordingly.
|
---|
133 | * @param pcbData Where to store the size (in bytes) of the converted data. Optional.
|
---|
134 | */
|
---|
135 | int SharedClipboardMetaDataConvertToFormat(const void *pvData, size_t cbData, SHAREDCLIPBOARDMETADATAFMT enmFmt,
|
---|
136 | void **ppvData, uint32_t *pcbData)
|
---|
137 | {
|
---|
138 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
139 | AssertReturn (cbData, VERR_INVALID_PARAMETER);
|
---|
140 | AssertPtrReturn(ppvData, VERR_INVALID_POINTER);
|
---|
141 |
|
---|
142 | /* pcbData is optional. */
|
---|
143 |
|
---|
144 | RT_NOREF(cbData);
|
---|
145 |
|
---|
146 | int rc = VERR_INVALID_PARAMETER;
|
---|
147 |
|
---|
148 | switch (enmFmt)
|
---|
149 | {
|
---|
150 | case SHAREDCLIPBOARDMETADATAFMT_URI_LIST:
|
---|
151 | {
|
---|
152 | char *pszTmp = RTStrDup((char *)pvData);
|
---|
153 | if (!pszTmp)
|
---|
154 | {
|
---|
155 | rc = VERR_NO_MEMORY;
|
---|
156 | break;
|
---|
157 | }
|
---|
158 |
|
---|
159 | RTPathChangeToUnixSlashes(pszTmp, true /* fForce */);
|
---|
160 |
|
---|
161 | char *pszURI;
|
---|
162 | rc = RTUriFileCreateEx(pszTmp, RTPATH_STR_F_STYLE_UNIX, &pszURI, 0 /*cbUri*/, NULL /* pcchUri */);
|
---|
163 | if (RT_SUCCESS(rc))
|
---|
164 | {
|
---|
165 | *ppvData = pszURI;
|
---|
166 | if (pcbData)
|
---|
167 | *pcbData = (uint32_t)strlen(pszURI);
|
---|
168 |
|
---|
169 | rc = VINF_SUCCESS;
|
---|
170 | }
|
---|
171 |
|
---|
172 | RTStrFree(pszTmp);
|
---|
173 | break;
|
---|
174 | }
|
---|
175 |
|
---|
176 | default:
|
---|
177 | rc = VERR_NOT_SUPPORTED;
|
---|
178 | AssertFailed();
|
---|
179 | break;
|
---|
180 | }
|
---|
181 |
|
---|
182 | LogFlowFuncLeaveRC(rc);
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Reads meta data.
|
---|
188 | *
|
---|
189 | * @returns VBox status code.
|
---|
190 | * @param pMeta Meta data struct to read from.
|
---|
191 | * @param pvBuf Buffer where to store the read data.
|
---|
192 | * @param cbBuf Size of buffer (in bytes) where to store the data.
|
---|
193 | * @param pcbRead Where to return the read amount in bytes. Optional.
|
---|
194 | */
|
---|
195 | int SharedClipboardMetaDataRead(PSHAREDCLIPBOARDMETADATA pMeta, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
196 | {
|
---|
197 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
198 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
199 | AssertPtrReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
200 | /* pcRead is optional. */
|
---|
201 |
|
---|
202 | int rc = VINF_SUCCESS;
|
---|
203 |
|
---|
204 | uint32_t cbRead = 0;
|
---|
205 | uint32_t cbToRead = pMeta->cbUsed - pMeta->cbOffRead;
|
---|
206 |
|
---|
207 | if (cbBuf > cbToRead)
|
---|
208 | cbBuf = cbToRead;
|
---|
209 |
|
---|
210 | if (cbToRead)
|
---|
211 | {
|
---|
212 | memcpy(pvBuf, (uint8_t *)pMeta->pvMeta + pMeta->cbOffRead, cbToRead);
|
---|
213 |
|
---|
214 | Assert(pMeta->cbUsed >= cbToRead);
|
---|
215 | pMeta->cbUsed -= cbToRead;
|
---|
216 |
|
---|
217 | pMeta->cbOffRead += cbToRead;
|
---|
218 | Assert(pMeta->cbOffRead <= pMeta->cbMeta);
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (pcbRead)
|
---|
222 | *pcbRead = cbRead;
|
---|
223 |
|
---|
224 | LogFlowFuncLeaveRC(rc);
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Resizes the data buffer of a meta data struct.
|
---|
230 | * Note: At the moment only supports growing the data buffer.
|
---|
231 | *
|
---|
232 | * @returns VBox status code.
|
---|
233 | * @param pMeta Meta data struct to resize.
|
---|
234 | * @param cbNewSize New size (in bytes) to use for resizing.
|
---|
235 | */
|
---|
236 | int SharedClipboardMetaDataResize(PSHAREDCLIPBOARDMETADATA pMeta, uint32_t cbNewSize)
|
---|
237 | {
|
---|
238 | AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
|
---|
239 |
|
---|
240 | if (!cbNewSize)
|
---|
241 | {
|
---|
242 | SharedClipboardMetaDataDestroy(pMeta);
|
---|
243 | return VINF_SUCCESS;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (cbNewSize == pMeta->cbMeta)
|
---|
247 | return VINF_SUCCESS;
|
---|
248 |
|
---|
249 | if (cbNewSize > _32M) /* Meta data can be up to 32MB. */
|
---|
250 | return VERR_INVALID_PARAMETER;
|
---|
251 |
|
---|
252 | void *pvTmp = NULL;
|
---|
253 | if (!pMeta->cbMeta)
|
---|
254 | {
|
---|
255 | Assert(pMeta->cbUsed == 0);
|
---|
256 | pvTmp = RTMemAllocZ(cbNewSize);
|
---|
257 | }
|
---|
258 | else
|
---|
259 | {
|
---|
260 | AssertPtr(pMeta->pvMeta);
|
---|
261 | pvTmp = RTMemRealloc(pMeta->pvMeta, cbNewSize);
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (pvTmp)
|
---|
265 | {
|
---|
266 | pMeta->pvMeta = pvTmp;
|
---|
267 | pMeta->cbMeta = cbNewSize;
|
---|
268 | return VINF_SUCCESS;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return VERR_NO_MEMORY;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Returns the actual free bytes of a meta data struct.
|
---|
276 | *
|
---|
277 | * @returns Actual free bytes of a meta data struct.
|
---|
278 | * @param pMeta Meta data struct to return free bytes for.
|
---|
279 | */
|
---|
280 | size_t SharedClipboardMetaDataGetFree(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
281 | {
|
---|
282 | AssertPtrReturn(pMeta, 0);
|
---|
283 | return pMeta->cbMeta - pMeta->cbUsed;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Returns the actual used bytes of a meta data struct.
|
---|
288 | *
|
---|
289 | * @returns Actual used bytes of a meta data struct.
|
---|
290 | * @param pMeta Meta data struct to return used bytes for.
|
---|
291 | */
|
---|
292 | size_t SharedClipboardMetaDataGetUsed(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
293 | {
|
---|
294 | AssertPtrReturn(pMeta, 0);
|
---|
295 | return pMeta->cbUsed;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Returns the overall (allocated) size in bytes of a meta data struct.
|
---|
300 | *
|
---|
301 | * @returns Overall (allocated) size of a meta data struct.
|
---|
302 | * @param pMeta Meta data struct to return size for.
|
---|
303 | */
|
---|
304 | size_t SharedClipboardMetaDataGetSize(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
305 | {
|
---|
306 | AssertPtrReturn(pMeta, 0);
|
---|
307 | return pMeta->cbMeta;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Returns the a mutable raw pointer to the actual meta data.
|
---|
312 | *
|
---|
313 | * @returns Mutable raw pointer to the actual meta data.
|
---|
314 | * @param pMeta Meta data struct to return mutable data pointer for.
|
---|
315 | */
|
---|
316 | void *SharedClipboardMetaDataMutableRaw(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
317 | {
|
---|
318 | return pMeta->pvMeta;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Returns the a const'ed raw pointer to the actual meta data.
|
---|
323 | *
|
---|
324 | * @returns Const'ed raw pointer to the actual meta data.
|
---|
325 | * @param pMeta Meta data struct to return const'ed data pointer for.
|
---|
326 | */
|
---|
327 | const void *SharedClipboardMetaDataRaw(PSHAREDCLIPBOARDMETADATA pMeta)
|
---|
328 | {
|
---|
329 | return pMeta->pvMeta;
|
---|
330 | }
|
---|
331 |
|
---|