VirtualBox

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

Last change on this file since 78925 was 78897, 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: 5.0 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/**
26 * Initializes a clipboard meta data struct.
27 *
28 * @returns VBox status code.
29 * @param pMeta Meta data struct to initialize.
30 */
31int 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 */
47void 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 */
73int SharedClipboardMetaDataAdd(PSHAREDCLIPBOARDMETADATA pMeta, const void *pvDataAdd, uint32_t cbDataAdd)
74{
75 AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
76
77 if (!cbDataAdd)
78 return VINF_SUCCESS;
79 AssertPtrReturn(pvDataAdd, VERR_INVALID_POINTER);
80
81 int rc = SharedClipboardMetaDataResize(pMeta, pMeta->cbMeta + cbDataAdd);
82 if (RT_FAILURE(rc))
83 return rc;
84
85 Assert(pMeta->cbMeta >= pMeta->cbUsed + cbDataAdd);
86 memcpy((uint8_t *)pMeta->pvMeta + pMeta->cbUsed, pvDataAdd, cbDataAdd);
87
88 pMeta->cbUsed += cbDataAdd;
89
90 return cbDataAdd;
91}
92
93/**
94 * Resizes the data buffer of a meta data struct.
95 * Note: At the moment only supports growing the data buffer.
96 *
97 * @returns VBox status code.
98 * @param pMeta Meta data struct to resize.
99 * @param cbNewSize New size (in bytes) to use for resizing.
100 */
101int SharedClipboardMetaDataResize(PSHAREDCLIPBOARDMETADATA pMeta, size_t cbNewSize)
102{
103 AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
104
105 if (!cbNewSize)
106 {
107 SharedClipboardMetaDataDestroy(pMeta);
108 return VINF_SUCCESS;
109 }
110
111 if (cbNewSize == pMeta->cbMeta)
112 return VINF_SUCCESS;
113
114 if (cbNewSize > _32M) /* Meta data can be up to 32MB. */
115 return VERR_INVALID_PARAMETER;
116
117 void *pvTmp = NULL;
118 if (!pMeta->cbMeta)
119 {
120 Assert(pMeta->cbUsed == 0);
121 pvTmp = RTMemAllocZ(cbNewSize);
122 }
123 else
124 {
125 AssertPtr(pMeta->pvMeta);
126 pvTmp = RTMemRealloc(pMeta->pvMeta, cbNewSize);
127 RT_BZERO(pvTmp, cbNewSize);
128 }
129
130 if (pvTmp)
131 {
132 pMeta->pvMeta = pvTmp;
133 pMeta->cbMeta = cbNewSize;
134 return VINF_SUCCESS;
135 }
136
137 return VERR_NO_MEMORY;
138}
139
140/**
141 * Returns the actual used bytes of a meta data struct.
142 *
143 * @returns Actual used bytes of a meta data struct.
144 * @param pMeta Meta data struct to return used bytes for.
145 */
146size_t SharedClipboardMetaDataGetUsed(PSHAREDCLIPBOARDMETADATA pMeta)
147{
148 AssertPtrReturn(pMeta, 0);
149 return pMeta->cbUsed;
150}
151
152/**
153 * Returns the overall (allocated) size in bytes of a meta data struct.
154 *
155 * @returns Overall (allocated) size of a meta data struct.
156 * @param pMeta Meta data struct to return size for.
157 */
158size_t SharedClipboardMetaDataGetSize(PSHAREDCLIPBOARDMETADATA pMeta)
159{
160 AssertPtrReturn(pMeta, 0);
161 return pMeta->cbMeta;
162}
163
164/**
165 * Returns the a mutable raw pointer to the actual meta data.
166 *
167 * @returns Mutable raw pointer to the actual meta data.
168 * @param pMeta Meta data struct to return mutable data pointer for.
169 */
170void *SharedClipboardMetaDataMutableRaw(PSHAREDCLIPBOARDMETADATA pMeta)
171{
172 return pMeta->pvMeta;
173}
174
175/**
176 * Returns the a const'ed raw pointer to the actual meta data.
177 *
178 * @returns Const'ed raw pointer to the actual meta data.
179 * @param pMeta Meta data struct to return const'ed data pointer for.
180 */
181const void *SharedClipboardMetaDataRaw(PSHAREDCLIPBOARDMETADATA pMeta)
182{
183 return pMeta->pvMeta;
184}
185
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