VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/ClipboardMetaData.cpp@ 79268

Last change on this file since 79268 was 79267, checked in by vboxsync, 6 years ago

Shared Clipboard/URI: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.3 KB
Line 
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 */
35int 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
44 return VINF_SUCCESS;
45}
46
47/**
48 * Destroys a clipboard meta data struct by free'ing all its data.
49 *
50 * @param pMeta Meta data struct to destroy.
51 */
52void SharedClipboardMetaDataDestroy(PSHAREDCLIPBOARDMETADATA pMeta)
53{
54 if (!pMeta)
55 return;
56
57 if (pMeta->pvMeta)
58 {
59 Assert(pMeta->cbMeta);
60
61 RTMemFree(pMeta->pvMeta);
62
63 pMeta->pvMeta = NULL;
64 pMeta->cbMeta = 0;
65 }
66
67 pMeta->cbUsed = 0;
68}
69
70/**
71 * Adds new meta data to a meta data struct.
72 *
73 * @returns VBox status code.
74 * @param pMeta Meta data struct to add data to.
75 */
76int SharedClipboardMetaDataAdd(PSHAREDCLIPBOARDMETADATA pMeta, const void *pvDataAdd, uint32_t cbDataAdd)
77{
78 AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
79
80 LogFlowFunc(("pvDataAdd=%p, cbDatadd=%RU32\n", pvDataAdd, cbDataAdd));
81
82 if (!cbDataAdd)
83 return VINF_SUCCESS;
84 AssertPtrReturn(pvDataAdd, VERR_INVALID_POINTER);
85
86 int rc = SharedClipboardMetaDataResize(pMeta, pMeta->cbMeta + cbDataAdd);
87 if (RT_FAILURE(rc))
88 return rc;
89
90 Assert(pMeta->cbMeta >= pMeta->cbUsed + cbDataAdd);
91 memcpy((uint8_t *)pMeta->pvMeta + pMeta->cbUsed, pvDataAdd, cbDataAdd);
92
93 pMeta->cbUsed += cbDataAdd;
94
95 return rc;
96}
97
98/**
99 * Converts data to a specific meta data format.
100 *
101 * @returns VBox status code. VERR_NOT_SUPPORTED if the format is unknown.
102 * @param pvData Pointer to data to convert.
103 * @param cbData Size (in bytes) of data to convert.
104 * @param enmFmt Meta data format to convert data to.
105 * @param ppvData Where to store the converted data on success. Caller must free the data accordingly.
106 * @param pcbData Where to store the size (in bytes) of the converted data. Optional.
107 */
108int SharedClipboardMetaDataConvertToFormat(const void *pvData, size_t cbData, SHAREDCLIPBOARDMETADATAFMT enmFmt,
109 void **ppvData, uint32_t *pcbData)
110{
111 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
112 AssertReturn (cbData, VERR_INVALID_PARAMETER);
113 AssertPtrReturn(ppvData, VERR_INVALID_POINTER);
114
115 /* pcbData is optional. */
116
117 RT_NOREF(cbData);
118
119 int rc = VERR_INVALID_PARAMETER;
120
121 switch (enmFmt)
122 {
123 case SHAREDCLIPBOARDMETADATAFMT_URI_LIST:
124 {
125 char *pszTmp = RTStrDup((char *)pvData);
126 if (!pszTmp)
127 {
128 rc = VERR_NO_MEMORY;
129 break;
130 }
131
132 RTPathChangeToUnixSlashes(pszTmp, true /* fForce */);
133
134 char *pszURI;
135 rc = RTUriFileCreateEx(pszTmp, RTPATH_STR_F_STYLE_UNIX, &pszURI, 0 /*cbUri*/, NULL /* pcchUri */);
136 if (RT_SUCCESS(rc))
137 {
138 *ppvData = pszURI;
139 if (pcbData)
140 *pcbData = (uint32_t)strlen(pszURI);
141
142 rc = VINF_SUCCESS;
143 }
144
145 RTStrFree(pszTmp);
146 break;
147 }
148
149 default:
150 rc = VERR_NOT_SUPPORTED;
151 AssertFailed();
152 break;
153 }
154
155 LogFlowFuncLeaveRC(rc);
156 return rc;
157}
158
159/**
160 * Resizes the data buffer of a meta data struct.
161 * Note: At the moment only supports growing the data buffer.
162 *
163 * @returns VBox status code.
164 * @param pMeta Meta data struct to resize.
165 * @param cbNewSize New size (in bytes) to use for resizing.
166 */
167int SharedClipboardMetaDataResize(PSHAREDCLIPBOARDMETADATA pMeta, uint32_t cbNewSize)
168{
169 AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
170
171 if (!cbNewSize)
172 {
173 SharedClipboardMetaDataDestroy(pMeta);
174 return VINF_SUCCESS;
175 }
176
177 if (cbNewSize == pMeta->cbMeta)
178 return VINF_SUCCESS;
179
180 if (cbNewSize > _32M) /* Meta data can be up to 32MB. */
181 return VERR_INVALID_PARAMETER;
182
183 void *pvTmp = NULL;
184 if (!pMeta->cbMeta)
185 {
186 Assert(pMeta->cbUsed == 0);
187 pvTmp = RTMemAllocZ(cbNewSize);
188 }
189 else
190 {
191 AssertPtr(pMeta->pvMeta);
192 pvTmp = RTMemRealloc(pMeta->pvMeta, cbNewSize);
193 }
194
195 if (pvTmp)
196 {
197 pMeta->pvMeta = pvTmp;
198 pMeta->cbMeta = cbNewSize;
199 return VINF_SUCCESS;
200 }
201
202 return VERR_NO_MEMORY;
203}
204
205/**
206 * Returns the actual free bytes of a meta data struct.
207 *
208 * @returns Actual free bytes of a meta data struct.
209 * @param pMeta Meta data struct to return free bytes for.
210 */
211size_t SharedClipboardMetaDataGetFree(PSHAREDCLIPBOARDMETADATA pMeta)
212{
213 AssertPtrReturn(pMeta, 0);
214 return pMeta->cbMeta - pMeta->cbUsed;
215}
216
217/**
218 * Returns the actual used bytes of a meta data struct.
219 *
220 * @returns Actual used bytes of a meta data struct.
221 * @param pMeta Meta data struct to return used bytes for.
222 */
223size_t SharedClipboardMetaDataGetUsed(PSHAREDCLIPBOARDMETADATA pMeta)
224{
225 AssertPtrReturn(pMeta, 0);
226 return pMeta->cbUsed;
227}
228
229/**
230 * Returns the overall (allocated) size in bytes of a meta data struct.
231 *
232 * @returns Overall (allocated) size of a meta data struct.
233 * @param pMeta Meta data struct to return size for.
234 */
235size_t SharedClipboardMetaDataGetSize(PSHAREDCLIPBOARDMETADATA pMeta)
236{
237 AssertPtrReturn(pMeta, 0);
238 return pMeta->cbMeta;
239}
240
241/**
242 * Returns the a mutable raw pointer to the actual meta data.
243 *
244 * @returns Mutable raw pointer to the actual meta data.
245 * @param pMeta Meta data struct to return mutable data pointer for.
246 */
247void *SharedClipboardMetaDataMutableRaw(PSHAREDCLIPBOARDMETADATA pMeta)
248{
249 return pMeta->pvMeta;
250}
251
252/**
253 * Returns the a const'ed raw pointer to the actual meta data.
254 *
255 * @returns Const'ed raw pointer to the actual meta data.
256 * @param pMeta Meta data struct to return const'ed data pointer for.
257 */
258const void *SharedClipboardMetaDataRaw(PSHAREDCLIPBOARDMETADATA pMeta)
259{
260 return pMeta->pvMeta;
261}
262
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette