1 | /* $Id: clipboard-common.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard: Some helper function for converting between the various eol.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Includes contributions from François Revol
|
---|
8 | *
|
---|
9 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
28 | */
|
---|
29 |
|
---|
30 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
31 |
|
---|
32 | #include <iprt/alloc.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/semaphore.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/rand.h>
|
---|
37 | #include <iprt/utf16.h>
|
---|
38 |
|
---|
39 | #include <iprt/formats/bmp.h>
|
---|
40 |
|
---|
41 | #include <iprt/errcore.h>
|
---|
42 | #include <VBox/log.h>
|
---|
43 | #include <VBox/GuestHost/clipboard-helper.h>
|
---|
44 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Prototypes *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource);
|
---|
51 | static int shClEventSourceUnregisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT pEvent);
|
---|
52 |
|
---|
53 | static void shClEventDestroy(PSHCLEVENT pEvent);
|
---|
54 | DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent);
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Implementation *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Allocates a new event payload.
|
---|
63 | *
|
---|
64 | * @returns VBox status code.
|
---|
65 | * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
|
---|
66 | * @param pvData Data to associate to this payload.
|
---|
67 | * The payload owns the data then.
|
---|
68 | * @param cbData Size (in bytes) of data to associate.
|
---|
69 | * @param ppPayload Where to store the allocated event payload on success.
|
---|
70 | */
|
---|
71 | int ShClPayloadInit(uint32_t uID, void *pvData, uint32_t cbData,
|
---|
72 | PSHCLEVENTPAYLOAD *ppPayload)
|
---|
73 | {
|
---|
74 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
75 | AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
|
---|
76 |
|
---|
77 | PSHCLEVENTPAYLOAD pPayload = (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
|
---|
78 | if (pPayload)
|
---|
79 | {
|
---|
80 | pPayload->pvData = pvData;
|
---|
81 | pPayload->cbData = cbData;
|
---|
82 | pPayload->uID = uID;
|
---|
83 |
|
---|
84 | *ppPayload = pPayload;
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return VERR_NO_MEMORY;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Allocates a new event payload.
|
---|
93 | *
|
---|
94 | * @returns VBox status code.
|
---|
95 | * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
|
---|
96 | * @param pvData Data block to allocate (duplicate) to this payload.
|
---|
97 | * @param cbData Size (in bytes) of data block to allocate.
|
---|
98 | * @param ppPayload Where to store the allocated event payload on success.
|
---|
99 | */
|
---|
100 | int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData,
|
---|
101 | PSHCLEVENTPAYLOAD *ppPayload)
|
---|
102 | {
|
---|
103 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
104 | AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
|
---|
105 |
|
---|
106 | void *pvDataDup = RTMemDup(pvData, cbData);
|
---|
107 | if (pvDataDup)
|
---|
108 | return ShClPayloadInit(uID, pvDataDup, cbData, ppPayload);
|
---|
109 |
|
---|
110 | return VERR_NO_MEMORY;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Frees an event payload.
|
---|
115 | *
|
---|
116 | * @returns VBox status code.
|
---|
117 | * @param pPayload Event payload to free.
|
---|
118 | */
|
---|
119 | void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload)
|
---|
120 | {
|
---|
121 | if (!pPayload)
|
---|
122 | return;
|
---|
123 |
|
---|
124 | if (pPayload->pvData)
|
---|
125 | {
|
---|
126 | Assert(pPayload->cbData);
|
---|
127 | RTMemFree(pPayload->pvData);
|
---|
128 | pPayload->pvData = NULL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | pPayload->cbData = 0;
|
---|
132 | pPayload->uID = UINT32_MAX;
|
---|
133 |
|
---|
134 | RTMemFree(pPayload);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Creates a new event source.
|
---|
139 | *
|
---|
140 | * @returns VBox status code.
|
---|
141 | * @param pSource Event source to create.
|
---|
142 | * @param uID ID to use for event source.
|
---|
143 | */
|
---|
144 | int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
|
---|
145 | {
|
---|
146 | LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
|
---|
147 | AssertPtrReturn(pSource, VERR_INVALID_POINTER);
|
---|
148 |
|
---|
149 | int rc = RTCritSectInit(&pSource->CritSect);
|
---|
150 | AssertRCReturn(rc, rc);
|
---|
151 |
|
---|
152 | RTListInit(&pSource->lstEvents);
|
---|
153 |
|
---|
154 | pSource->uID = uID;
|
---|
155 | /* Choose a random event ID starting point. */
|
---|
156 | pSource->idNextEvent = RTRandU32Ex(1, VBOX_SHCL_MAX_EVENTS - 1);
|
---|
157 |
|
---|
158 | return VINF_SUCCESS;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Destroys an event source.
|
---|
163 | *
|
---|
164 | * @returns VBox status code.
|
---|
165 | * @param pSource Event source to destroy.
|
---|
166 | */
|
---|
167 | int ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource)
|
---|
168 | {
|
---|
169 | if (!pSource)
|
---|
170 | return VINF_SUCCESS;
|
---|
171 |
|
---|
172 | if (!RTCritSectIsInitialized(&pSource->CritSect)) /* Already destroyed? Bail out. */
|
---|
173 | return VINF_SUCCESS;
|
---|
174 |
|
---|
175 | LogFlowFunc(("ID=%RU32\n", pSource->uID));
|
---|
176 |
|
---|
177 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
178 | if (RT_SUCCESS(rc))
|
---|
179 | {
|
---|
180 | shClEventSourceResetInternal(pSource);
|
---|
181 |
|
---|
182 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
183 | AssertRC(rc);
|
---|
184 |
|
---|
185 | RTCritSectDelete(&pSource->CritSect);
|
---|
186 |
|
---|
187 | pSource->uID = UINT16_MAX;
|
---|
188 | pSource->idNextEvent = UINT32_MAX;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Resets an event source, internal version.
|
---|
196 | *
|
---|
197 | * @param pSource Event source to reset.
|
---|
198 | */
|
---|
199 | static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource)
|
---|
200 | {
|
---|
201 | LogFlowFunc(("ID=%RU32\n", pSource->uID));
|
---|
202 |
|
---|
203 | PSHCLEVENT pEvIt;
|
---|
204 | PSHCLEVENT pEvItNext;
|
---|
205 | RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
|
---|
206 | {
|
---|
207 | bool const fDealloc = ASMAtomicReadU32(&pEvIt->cRefs) == 0; /* Still any references left? Skip de-allocation. */
|
---|
208 | if (!fDealloc)
|
---|
209 | Log3Func(("Event %RU32 has %RU32 references left, skipping de-allocation\n", pEvIt->idEvent, pEvIt->cRefs));
|
---|
210 |
|
---|
211 | shClEventDestroy(pEvIt);
|
---|
212 |
|
---|
213 | int rc2 = shClEventSourceUnregisterEvent(pSource, pEvIt);
|
---|
214 | AssertRC(rc2);
|
---|
215 |
|
---|
216 | if (fDealloc)
|
---|
217 | {
|
---|
218 | RTMemFree(pEvIt);
|
---|
219 | pEvIt = NULL;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Resets an event source.
|
---|
226 | *
|
---|
227 | * @param pSource Event source to reset.
|
---|
228 | */
|
---|
229 | void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
|
---|
230 | {
|
---|
231 | int rc2 = RTCritSectEnter(&pSource->CritSect);
|
---|
232 | if (RT_SUCCESS(rc2))
|
---|
233 | {
|
---|
234 | shClEventSourceResetInternal(pSource);
|
---|
235 |
|
---|
236 | rc2 = RTCritSectLeave(&pSource->CritSect);
|
---|
237 | AssertRC(rc2);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Generates a new event ID for a specific event source and registers it.
|
---|
243 | *
|
---|
244 | * @returns VBox status code.
|
---|
245 | * @param pSource Event source to generate event for.
|
---|
246 | * @param ppEvent Where to return the new event generated on success.
|
---|
247 | */
|
---|
248 | int ShClEventSourceGenerateAndRegisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT *ppEvent)
|
---|
249 | {
|
---|
250 | AssertPtrReturn(pSource, VERR_INVALID_POINTER);
|
---|
251 | AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
|
---|
252 |
|
---|
253 | PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
|
---|
254 | AssertReturn(pEvent, VERR_NO_MEMORY);
|
---|
255 | int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
|
---|
256 | if (RT_SUCCESS(rc))
|
---|
257 | {
|
---|
258 | rc = RTCritSectEnter(&pSource->CritSect);
|
---|
259 | if (RT_SUCCESS(rc))
|
---|
260 | {
|
---|
261 | /*
|
---|
262 | * Allocate an unique event ID.
|
---|
263 | */
|
---|
264 | for (uint32_t cTries = 0;; cTries++)
|
---|
265 | {
|
---|
266 | SHCLEVENTID idEvent = ++pSource->idNextEvent;
|
---|
267 | if (idEvent < VBOX_SHCL_MAX_EVENTS)
|
---|
268 | { /* likely */ }
|
---|
269 | else
|
---|
270 | pSource->idNextEvent = idEvent = 1; /* zero == error, remember! */
|
---|
271 |
|
---|
272 | if (shclEventGet(pSource, idEvent) == NULL)
|
---|
273 | {
|
---|
274 | pEvent->pParent = pSource;
|
---|
275 | pEvent->idEvent = idEvent;
|
---|
276 | RTListAppend(&pSource->lstEvents, &pEvent->Node);
|
---|
277 |
|
---|
278 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
279 | AssertRC(rc);
|
---|
280 |
|
---|
281 | LogFlowFunc(("uSource=%RU16: New event: %#x\n", pSource->uID, idEvent));
|
---|
282 |
|
---|
283 | ShClEventRetain(pEvent);
|
---|
284 | *ppEvent = pEvent;
|
---|
285 |
|
---|
286 | return VINF_SUCCESS;
|
---|
287 | }
|
---|
288 |
|
---|
289 | AssertBreak(cTries < 4096);
|
---|
290 | }
|
---|
291 |
|
---|
292 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
293 | AssertRC(rc);
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | AssertMsgFailed(("Unable to register a new event ID for event source %RU16\n", pSource->uID));
|
---|
298 |
|
---|
299 | RTSemEventMultiDestroy(pEvent->hEvtMulSem);
|
---|
300 | pEvent->hEvtMulSem = NIL_RTSEMEVENTMULTI;
|
---|
301 | RTMemFree(pEvent);
|
---|
302 | return rc;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Destroys an event.
|
---|
307 | *
|
---|
308 | * @param pEvent Event to destroy.
|
---|
309 | */
|
---|
310 | static void shClEventDestroy(PSHCLEVENT pEvent)
|
---|
311 | {
|
---|
312 | if (!pEvent)
|
---|
313 | return;
|
---|
314 |
|
---|
315 | LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
|
---|
316 |
|
---|
317 | if (pEvent->hEvtMulSem != NIL_RTSEMEVENT)
|
---|
318 | {
|
---|
319 | RTSemEventMultiDestroy(pEvent->hEvtMulSem);
|
---|
320 | pEvent->hEvtMulSem = NIL_RTSEMEVENT;
|
---|
321 | }
|
---|
322 |
|
---|
323 | ShClPayloadFree(pEvent->pPayload);
|
---|
324 | pEvent->pPayload = NULL;
|
---|
325 |
|
---|
326 | pEvent->idEvent = NIL_SHCLEVENTID;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Unregisters an event.
|
---|
331 | *
|
---|
332 | * @returns VBox status code.
|
---|
333 | * @param pSource Event source to unregister event for.
|
---|
334 | * @param pEvent Event to unregister.
|
---|
335 | */
|
---|
336 | static int shClEventSourceUnregisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT pEvent)
|
---|
337 | {
|
---|
338 | RT_NOREF(pSource);
|
---|
339 |
|
---|
340 | LogFlowFunc(("idEvent=%RU32, cRefs=%RU32\n", pEvent->idEvent, pEvent->cRefs));
|
---|
341 |
|
---|
342 | RTListNodeRemove(&pEvent->Node);
|
---|
343 | pEvent->pParent = NULL;
|
---|
344 |
|
---|
345 | return VINF_SUCCESS;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Returns a specific event of a event source. Inlined version.
|
---|
350 | *
|
---|
351 | * @returns Pointer to event if found, or NULL if not found.
|
---|
352 | * @param pSource Event source to get event from.
|
---|
353 | * @param uID Event ID to get.
|
---|
354 | */
|
---|
355 | DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
|
---|
356 | {
|
---|
357 | PSHCLEVENT pEvent;
|
---|
358 | RTListForEach(&pSource->lstEvents, pEvent, SHCLEVENT, Node)
|
---|
359 | {
|
---|
360 | if (pEvent->idEvent == idEvent)
|
---|
361 | return pEvent;
|
---|
362 | }
|
---|
363 |
|
---|
364 | return NULL;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Returns a specific event of a event source.
|
---|
369 | *
|
---|
370 | * @returns Pointer to event if found, or NULL if not found.
|
---|
371 | * @param pSource Event source to get event from.
|
---|
372 | * @param idEvent ID of event to return.
|
---|
373 | */
|
---|
374 | PSHCLEVENT ShClEventSourceGetFromId(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
|
---|
375 | {
|
---|
376 | AssertPtrReturn(pSource, NULL);
|
---|
377 |
|
---|
378 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
379 | if (RT_SUCCESS(rc))
|
---|
380 | {
|
---|
381 | PSHCLEVENT pEvent = shclEventGet(pSource, idEvent);
|
---|
382 |
|
---|
383 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
384 | AssertRC(rc);
|
---|
385 |
|
---|
386 | return pEvent;
|
---|
387 | }
|
---|
388 |
|
---|
389 | return NULL;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Returns the last (newest) event ID which has been registered for an event source.
|
---|
394 | *
|
---|
395 | * @returns Pointer to last registered event, or NULL if not found.
|
---|
396 | * @param pSource Event source to get last registered event from.
|
---|
397 | */
|
---|
398 | PSHCLEVENT ShClEventSourceGetLast(PSHCLEVENTSOURCE pSource)
|
---|
399 | {
|
---|
400 | AssertPtrReturn(pSource, NULL);
|
---|
401 |
|
---|
402 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
403 | if (RT_SUCCESS(rc))
|
---|
404 | {
|
---|
405 | PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
|
---|
406 |
|
---|
407 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
408 | AssertRC(rc);
|
---|
409 |
|
---|
410 | return pEvent;
|
---|
411 | }
|
---|
412 |
|
---|
413 | return NULL;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Returns the current reference count for a specific event.
|
---|
418 | *
|
---|
419 | * @returns Reference count.
|
---|
420 | * @param pSource Event source the specific event is part of.
|
---|
421 | * @param idEvent Event ID to return reference count for.
|
---|
422 | */
|
---|
423 | uint32_t ShClEventGetRefs(PSHCLEVENT pEvent)
|
---|
424 | {
|
---|
425 | AssertPtrReturn(pEvent, 0);
|
---|
426 |
|
---|
427 | return ASMAtomicReadU32(&pEvent->cRefs);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Detaches a payload from an event, internal version.
|
---|
432 | *
|
---|
433 | * @returns Pointer to the detached payload. Can be NULL if the event has no payload.
|
---|
434 | * @param pEvent Event to detach payload for.
|
---|
435 | */
|
---|
436 | static PSHCLEVENTPAYLOAD shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
|
---|
437 | {
|
---|
438 | #ifdef VBOX_STRICT
|
---|
439 | AssertPtrReturn(pEvent, NULL);
|
---|
440 | #endif
|
---|
441 |
|
---|
442 | PSHCLEVENTPAYLOAD pPayload = pEvent->pPayload;
|
---|
443 |
|
---|
444 | pEvent->pPayload = NULL;
|
---|
445 |
|
---|
446 | return pPayload;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /**
|
---|
450 | * Waits for an event to get signalled.
|
---|
451 | *
|
---|
452 | * @returns VBox status code.
|
---|
453 | * @retval VERR_SHCLPB_EVENT_FAILED if the event has a set error code.
|
---|
454 | * @param pEvent Event to wait for.
|
---|
455 | * @param uTimeoutMs Timeout (in ms) to wait.
|
---|
456 | * @param pRc Where to return the event rc. Optional and can be NULL.
|
---|
457 | * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
|
---|
458 | * SharedClipboardPayloadFree(). Optional.
|
---|
459 | */
|
---|
460 | int ShClEventWaitEx(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, int *pRc, PSHCLEVENTPAYLOAD *ppPayload)
|
---|
461 | {
|
---|
462 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
463 | AssertPtrNullReturn(ppPayload, VERR_INVALID_POINTER);
|
---|
464 | LogFlowFuncEnter();
|
---|
465 |
|
---|
466 | int rc = RTSemEventMultiWait(pEvent->hEvtMulSem, uTimeoutMs);
|
---|
467 | if (RT_SUCCESS(rc))
|
---|
468 | {
|
---|
469 | if (RT_FAILURE(pEvent->rc))
|
---|
470 | rc = VERR_SHCLPB_EVENT_FAILED;
|
---|
471 |
|
---|
472 | if (pRc)
|
---|
473 | *pRc = pEvent->rc;
|
---|
474 |
|
---|
475 | if (ppPayload)
|
---|
476 | {
|
---|
477 | /* Make sure to detach payload here, as the caller now owns the data. */
|
---|
478 | *ppPayload = shclEventPayloadDetachInternal(pEvent);
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 | if (RT_FAILURE(rc))
|
---|
483 | LogRel2(("Shared Clipboard: Waiting for event %RU32 failed, rc=%Rrc\n", pEvent->idEvent, rc));
|
---|
484 |
|
---|
485 | LogFlowFuncLeaveRC(rc);
|
---|
486 | return rc;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Waits for an event to get signalled.
|
---|
491 | *
|
---|
492 | * @returns VBox status code.
|
---|
493 | * @retval VERR_SHCLPB_EVENT_FAILED if the event has a set error code.
|
---|
494 | * @param pEvent Event to wait for.
|
---|
495 | * @param uTimeoutMs Timeout (in ms) to wait.
|
---|
496 | * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
|
---|
497 | * SharedClipboardPayloadFree(). Optional.
|
---|
498 | */
|
---|
499 | int ShClEventWait(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, PSHCLEVENTPAYLOAD *ppPayload)
|
---|
500 | {
|
---|
501 | return ShClEventWaitEx(pEvent, uTimeoutMs, NULL /* pRc */, ppPayload);
|
---|
502 | }
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * Retains an event by increasing its reference count.
|
---|
506 | *
|
---|
507 | * @returns New reference count, or UINT32_MAX if failed.
|
---|
508 | * @param pEvent Event to retain.
|
---|
509 | */
|
---|
510 | uint32_t ShClEventRetain(PSHCLEVENT pEvent)
|
---|
511 | {
|
---|
512 | AssertPtrReturn(pEvent, UINT32_MAX);
|
---|
513 | AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) < 64, UINT32_MAX);
|
---|
514 | return ASMAtomicIncU32(&pEvent->cRefs);
|
---|
515 | }
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * Releases event by decreasing its reference count. Will be destroyed once the reference count reaches 0.
|
---|
519 | *
|
---|
520 | * @returns New reference count, or UINT32_MAX if failed.
|
---|
521 | * @param pEvent Event to release.
|
---|
522 | * If the reference count reaches 0, the event will
|
---|
523 | * be destroyed and \a pEvent will be invalid.
|
---|
524 | */
|
---|
525 | uint32_t ShClEventRelease(PSHCLEVENT pEvent)
|
---|
526 | {
|
---|
527 | if (!pEvent)
|
---|
528 | return 0;
|
---|
529 |
|
---|
530 | AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) > 0, UINT32_MAX);
|
---|
531 |
|
---|
532 | uint32_t const cRefs = ASMAtomicDecU32(&pEvent->cRefs);
|
---|
533 | if (cRefs == 0)
|
---|
534 | {
|
---|
535 | int rc;
|
---|
536 | PSHCLEVENTSOURCE pParent = pEvent->pParent;
|
---|
537 | if ( pParent
|
---|
538 | && RTCritSectIsInitialized(&pParent->CritSect))
|
---|
539 | {
|
---|
540 | rc = RTCritSectEnter(&pParent->CritSect);
|
---|
541 | if (RT_SUCCESS(rc))
|
---|
542 | {
|
---|
543 | rc = shClEventSourceUnregisterEvent(pParent, pEvent);
|
---|
544 |
|
---|
545 | int rc2 = RTCritSectLeave(&pParent->CritSect);
|
---|
546 | if (RT_SUCCESS(rc))
|
---|
547 | rc = rc2;
|
---|
548 | }
|
---|
549 | }
|
---|
550 | else
|
---|
551 | rc = VINF_SUCCESS;
|
---|
552 |
|
---|
553 | if (RT_SUCCESS(rc))
|
---|
554 | {
|
---|
555 | shClEventDestroy(pEvent);
|
---|
556 |
|
---|
557 | RTMemFree(pEvent);
|
---|
558 | pEvent = NULL;
|
---|
559 | }
|
---|
560 |
|
---|
561 | return RT_SUCCESS(rc) ? 0 : UINT32_MAX;
|
---|
562 | }
|
---|
563 |
|
---|
564 | return cRefs;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Signals an event, extended version.
|
---|
569 | *
|
---|
570 | * @returns VBox status code.
|
---|
571 | * @param pEvent Event to signal.
|
---|
572 | * @param rc Result code to set.
|
---|
573 | * @param pPayload Event payload to associate. Takes ownership on
|
---|
574 | * success. Optional.
|
---|
575 | */
|
---|
576 | int ShClEventSignalEx(PSHCLEVENT pEvent, int rc, PSHCLEVENTPAYLOAD pPayload)
|
---|
577 | {
|
---|
578 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
579 |
|
---|
580 | Assert(pEvent->pPayload == NULL);
|
---|
581 |
|
---|
582 | pEvent->rc = rc;
|
---|
583 | pEvent->pPayload = pPayload;
|
---|
584 |
|
---|
585 | int rc2 = RTSemEventMultiSignal(pEvent->hEvtMulSem);
|
---|
586 | if (RT_FAILURE(rc2))
|
---|
587 | pEvent->pPayload = NULL; /* (no race condition if consumer also enters the critical section) */
|
---|
588 |
|
---|
589 | LogFlowFuncLeaveRC(rc2);
|
---|
590 | return rc2;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Signals an event.
|
---|
595 | *
|
---|
596 | * @returns VBox status code.
|
---|
597 | * @param pEvent Event to signal.
|
---|
598 | * @param pPayload Event payload to associate. Takes ownership on
|
---|
599 | * success. Optional.
|
---|
600 | */
|
---|
601 | int ShClEventSignal(PSHCLEVENT pEvent, PSHCLEVENTPAYLOAD pPayload)
|
---|
602 | {
|
---|
603 | return ShClEventSignalEx(pEvent, VINF_SUCCESS, pPayload);
|
---|
604 | }
|
---|
605 |
|
---|
606 | int ShClUtf16LenUtf8(PCRTUTF16 pcwszSrc, size_t cwcSrc, size_t *pchLen)
|
---|
607 | {
|
---|
608 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
609 | AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
|
---|
610 |
|
---|
611 | size_t chLen = 0;
|
---|
612 | int rc = RTUtf16CalcUtf8LenEx(pcwszSrc, cwcSrc, &chLen);
|
---|
613 | if (RT_SUCCESS(rc))
|
---|
614 | *pchLen = chLen;
|
---|
615 | return rc;
|
---|
616 | }
|
---|
617 |
|
---|
618 | int ShClConvUtf16CRLFToUtf8LF(PCRTUTF16 pcwszSrc, size_t cwcSrc,
|
---|
619 | char *pszBuf, size_t cbBuf, size_t *pcbLen)
|
---|
620 | {
|
---|
621 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
622 | AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
|
---|
623 | AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
|
---|
624 | AssertPtrReturn(pcbLen, VERR_INVALID_POINTER);
|
---|
625 |
|
---|
626 | int rc;
|
---|
627 |
|
---|
628 | PRTUTF16 pwszTmp = NULL;
|
---|
629 | size_t cchTmp = 0;
|
---|
630 |
|
---|
631 | size_t cbLen = 0;
|
---|
632 |
|
---|
633 | /* How long will the converted text be? */
|
---|
634 | rc = ShClUtf16CRLFLenUtf8(pcwszSrc, cwcSrc, &cchTmp);
|
---|
635 | if (RT_SUCCESS(rc))
|
---|
636 | {
|
---|
637 | cchTmp++; /* Add space for terminator. */
|
---|
638 |
|
---|
639 | pwszTmp = (PRTUTF16)RTMemAlloc(cchTmp * sizeof(RTUTF16));
|
---|
640 | if (pwszTmp)
|
---|
641 | {
|
---|
642 | rc = ShClConvUtf16CRLFToLF(pcwszSrc, cwcSrc, pwszTmp, cchTmp);
|
---|
643 | if (RT_SUCCESS(rc))
|
---|
644 | rc = RTUtf16ToUtf8Ex(pwszTmp + 1, cchTmp - 1, &pszBuf, cbBuf, &cbLen);
|
---|
645 |
|
---|
646 | RTMemFree(reinterpret_cast<void *>(pwszTmp));
|
---|
647 | }
|
---|
648 | else
|
---|
649 | rc = VERR_NO_MEMORY;
|
---|
650 | }
|
---|
651 |
|
---|
652 | if (RT_SUCCESS(rc))
|
---|
653 | {
|
---|
654 | *pcbLen = cbLen;
|
---|
655 | }
|
---|
656 |
|
---|
657 | return rc;
|
---|
658 | }
|
---|
659 |
|
---|
660 | int ShClConvUtf16LFToCRLFA(PCRTUTF16 pcwszSrc, size_t cwcSrc,
|
---|
661 | PRTUTF16 *ppwszDst, size_t *pcwDst)
|
---|
662 | {
|
---|
663 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
664 | AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
|
---|
665 | AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
|
---|
666 |
|
---|
667 | PRTUTF16 pwszDst = NULL;
|
---|
668 | size_t cchDst;
|
---|
669 |
|
---|
670 | int rc = ShClUtf16CalcNormalizedEolToCRLFLength(pcwszSrc, cwcSrc, &cchDst);
|
---|
671 | if (RT_SUCCESS(rc))
|
---|
672 | {
|
---|
673 | pwszDst = (PRTUTF16)RTMemAlloc((cchDst + 1 /* Leave space for terminator */) * sizeof(RTUTF16));
|
---|
674 | if (pwszDst)
|
---|
675 | {
|
---|
676 | rc = ShClConvUtf16LFToCRLF(pcwszSrc, cwcSrc, pwszDst, cchDst + 1 /* Include terminator */);
|
---|
677 | }
|
---|
678 | else
|
---|
679 | rc = VERR_NO_MEMORY;
|
---|
680 | }
|
---|
681 |
|
---|
682 | if (RT_SUCCESS(rc))
|
---|
683 | {
|
---|
684 | *ppwszDst = pwszDst;
|
---|
685 | *pcwDst = cchDst;
|
---|
686 | }
|
---|
687 | else
|
---|
688 | RTMemFree(pwszDst);
|
---|
689 |
|
---|
690 | LogFlowFuncLeaveRC(rc);
|
---|
691 | return rc;
|
---|
692 | }
|
---|
693 |
|
---|
694 | int ShClConvUtf8LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
|
---|
695 | PRTUTF16 *ppwszDst, size_t *pcwDst)
|
---|
696 | {
|
---|
697 | AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
|
---|
698 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
699 | AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
|
---|
700 | AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
|
---|
701 |
|
---|
702 | /* Intermediate conversion to UTF-16. */
|
---|
703 | size_t cwcTmp;
|
---|
704 | PRTUTF16 pwcTmp = NULL;
|
---|
705 | int rc = RTStrToUtf16Ex(pcszSrc, cbSrc, &pwcTmp, 0, &cwcTmp);
|
---|
706 | if (RT_SUCCESS(rc))
|
---|
707 | {
|
---|
708 | rc = ShClConvUtf16LFToCRLFA(pwcTmp, cwcTmp, ppwszDst, pcwDst);
|
---|
709 | RTUtf16Free(pwcTmp);
|
---|
710 | }
|
---|
711 |
|
---|
712 | return rc;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Converts a Latin-1 string with LF line endings into an UTF-16 string with CRLF endings.
|
---|
717 | *
|
---|
718 | * @returns VBox status code.
|
---|
719 | * @param pcszSrc Latin-1 string to convert.
|
---|
720 | * @param cbSrc Size (in bytes) of Latin-1 string to convert.
|
---|
721 | * @param ppwszDst Where to return the converted UTF-16 string on success.
|
---|
722 | * @param pcwDst Where to return the length (in UTF-16 characters) on success.
|
---|
723 | *
|
---|
724 | * @note Only converts the source until the string terminator is found (or length limit is hit).
|
---|
725 | */
|
---|
726 | int ShClConvLatin1LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
|
---|
727 | PRTUTF16 *ppwszDst, size_t *pcwDst)
|
---|
728 | {
|
---|
729 | AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
|
---|
730 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
731 | AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
|
---|
732 | AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
|
---|
733 |
|
---|
734 | size_t chSrc = 0;
|
---|
735 |
|
---|
736 | PRTUTF16 pwszDst = NULL;
|
---|
737 |
|
---|
738 | /* Calculate the space needed. */
|
---|
739 | size_t cwDst = 0;
|
---|
740 | for (size_t i = 0; i < cbSrc && pcszSrc[i] != '\0'; ++i)
|
---|
741 | {
|
---|
742 | if (pcszSrc[i] == VBOX_SHCL_LINEFEED)
|
---|
743 | cwDst += 2; /* Space for VBOX_SHCL_CARRIAGERETURN + VBOX_SHCL_LINEFEED. */
|
---|
744 | else
|
---|
745 | ++cwDst;
|
---|
746 | chSrc++;
|
---|
747 | }
|
---|
748 |
|
---|
749 | pwszDst = (PRTUTF16)RTMemAlloc((cwDst + 1 /* Leave space for the terminator */) * sizeof(RTUTF16));
|
---|
750 | AssertPtrReturn(pwszDst, VERR_NO_MEMORY);
|
---|
751 |
|
---|
752 | /* Do the conversion, bearing in mind that Latin-1 expands "naturally" to UTF-16. */
|
---|
753 | for (size_t i = 0, j = 0; i < chSrc; ++i, ++j)
|
---|
754 | {
|
---|
755 | AssertMsg(j <= cwDst, ("cbSrc=%zu, j=%u vs. cwDst=%u\n", cbSrc, j, cwDst));
|
---|
756 | if (pcszSrc[i] != VBOX_SHCL_LINEFEED)
|
---|
757 | pwszDst[j] = pcszSrc[i];
|
---|
758 | else
|
---|
759 | {
|
---|
760 | pwszDst[j] = VBOX_SHCL_CARRIAGERETURN;
|
---|
761 | pwszDst[j + 1] = VBOX_SHCL_LINEFEED;
|
---|
762 | ++j;
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | pwszDst[cwDst] = '\0'; /* Make sure we are zero-terminated. */
|
---|
767 |
|
---|
768 | *ppwszDst = pwszDst;
|
---|
769 | *pcwDst = cwDst;
|
---|
770 |
|
---|
771 | return VINF_SUCCESS;
|
---|
772 | }
|
---|
773 |
|
---|
774 | int ShClConvUtf16ToUtf8HTML(PCRTUTF16 pcwszSrc, size_t cwcSrc, char **ppszDst, size_t *pcbDst)
|
---|
775 | {
|
---|
776 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
777 | AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
|
---|
778 | AssertPtrReturn(ppszDst, VERR_INVALID_POINTER);
|
---|
779 | AssertPtrReturn(pcbDst, VERR_INVALID_POINTER);
|
---|
780 |
|
---|
781 | int rc = VINF_SUCCESS;
|
---|
782 |
|
---|
783 | size_t cwTmp = cwcSrc;
|
---|
784 | PCRTUTF16 pwTmp = pcwszSrc;
|
---|
785 |
|
---|
786 | char *pchDst = NULL;
|
---|
787 | size_t cbDst = 0;
|
---|
788 |
|
---|
789 | size_t i = 0;
|
---|
790 | while (i < cwTmp)
|
---|
791 | {
|
---|
792 | /* Find zero symbol (end of string). */
|
---|
793 | for (; i < cwTmp && pcwszSrc[i] != 0; i++)
|
---|
794 | ;
|
---|
795 |
|
---|
796 | /* Convert found string. */
|
---|
797 | char *psz = NULL;
|
---|
798 | size_t cch = 0;
|
---|
799 | rc = RTUtf16ToUtf8Ex(pwTmp, cwTmp, &psz, pwTmp - pcwszSrc, &cch);
|
---|
800 | if (RT_FAILURE(rc))
|
---|
801 | break;
|
---|
802 |
|
---|
803 | /* Append new substring. */
|
---|
804 | char *pchNew = (char *)RTMemRealloc(pchDst, cbDst + cch + 1);
|
---|
805 | if (!pchNew)
|
---|
806 | {
|
---|
807 | RTStrFree(psz);
|
---|
808 | rc = VERR_NO_MEMORY;
|
---|
809 | break;
|
---|
810 | }
|
---|
811 |
|
---|
812 | pchDst = pchNew;
|
---|
813 | memcpy(pchDst + cbDst, psz, cch + 1);
|
---|
814 |
|
---|
815 | RTStrFree(psz);
|
---|
816 |
|
---|
817 | cbDst += cch + 1;
|
---|
818 |
|
---|
819 | /* Skip zero symbols. */
|
---|
820 | for (; i < cwTmp && pcwszSrc[i] == 0; i++)
|
---|
821 | ;
|
---|
822 |
|
---|
823 | /* Remember start of string. */
|
---|
824 | pwTmp += i;
|
---|
825 | }
|
---|
826 |
|
---|
827 | if (RT_SUCCESS(rc))
|
---|
828 | {
|
---|
829 | *ppszDst = pchDst;
|
---|
830 | *pcbDst = cbDst;
|
---|
831 |
|
---|
832 | return VINF_SUCCESS;
|
---|
833 | }
|
---|
834 |
|
---|
835 | RTMemFree(pchDst);
|
---|
836 |
|
---|
837 | return rc;
|
---|
838 | }
|
---|
839 |
|
---|
840 | int ShClUtf16CalcNormalizedEolToCRLFLength(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
|
---|
841 | {
|
---|
842 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
843 | AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
|
---|
844 |
|
---|
845 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
846 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
847 |
|
---|
848 | size_t cLen = 0;
|
---|
849 |
|
---|
850 | /* Don't copy the endian marker. */
|
---|
851 | size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
|
---|
852 |
|
---|
853 | /* Calculate the size of the destination text string. */
|
---|
854 | /* Is this Utf16 or Utf16-LE? */
|
---|
855 | for (; i < cwSrc; ++i, ++cLen)
|
---|
856 | {
|
---|
857 | /* Check for a single line feed */
|
---|
858 | if ( pcwszSrc[i] == VBOX_SHCL_LINEFEED
|
---|
859 | && (i == 0 || pcwszSrc[i - 1] != VBOX_SHCL_CARRIAGERETURN))
|
---|
860 | {
|
---|
861 | ++cLen;
|
---|
862 | }
|
---|
863 | #ifdef RT_OS_DARWIN
|
---|
864 | /* Check for a single carriage return (MacOS) */
|
---|
865 | if ( pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN
|
---|
866 | && (i + 1 >= cwSrc || pcwszSrc[i + 1] != VBOX_SHCL_LINEFEED))
|
---|
867 | {
|
---|
868 | ++cLen;
|
---|
869 | }
|
---|
870 | #endif
|
---|
871 | if (pcwszSrc[i] == 0)
|
---|
872 | {
|
---|
873 | /* Don't count this, as we do so below. */
|
---|
874 | break;
|
---|
875 | }
|
---|
876 | }
|
---|
877 |
|
---|
878 | *pchLen = cLen;
|
---|
879 |
|
---|
880 | return VINF_SUCCESS;
|
---|
881 | }
|
---|
882 |
|
---|
883 | int ShClUtf16CRLFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
|
---|
884 | {
|
---|
885 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
886 | AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
|
---|
887 | AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
|
---|
888 |
|
---|
889 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
890 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
891 |
|
---|
892 | size_t cLen = 0;
|
---|
893 |
|
---|
894 | /* Calculate the size of the destination text string. */
|
---|
895 | /* Is this Utf16 or Utf16-LE? */
|
---|
896 | if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
|
---|
897 | cLen = 0;
|
---|
898 | else
|
---|
899 | cLen = 1;
|
---|
900 |
|
---|
901 | for (size_t i = 0; i < cwSrc; ++i, ++cLen)
|
---|
902 | {
|
---|
903 | if ( (i + 1 < cwSrc)
|
---|
904 | && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
|
---|
905 | && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
|
---|
906 | {
|
---|
907 | ++i;
|
---|
908 | }
|
---|
909 | if (pcwszSrc[i] == 0)
|
---|
910 | break;
|
---|
911 | }
|
---|
912 |
|
---|
913 | *pchLen = cLen;
|
---|
914 |
|
---|
915 | return VINF_SUCCESS;
|
---|
916 | }
|
---|
917 |
|
---|
918 | int ShClConvUtf16LFToCRLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwcDst)
|
---|
919 | {
|
---|
920 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
921 | AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
|
---|
922 | AssertReturn(cwcDst, VERR_INVALID_PARAMETER);
|
---|
923 |
|
---|
924 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
925 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
926 |
|
---|
927 | /* Don't copy the endian marker. */
|
---|
928 | size_t offDst = 0;
|
---|
929 | for (size_t offSrc = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0; offSrc < cwcSrc; ++offSrc, ++offDst)
|
---|
930 | {
|
---|
931 | /* Ensure more output space: */
|
---|
932 | if (offDst < cwcDst) { /* likely */ }
|
---|
933 | else return VERR_BUFFER_OVERFLOW;
|
---|
934 |
|
---|
935 | /* Don't copy the null byte, as we add it below. */
|
---|
936 | if (pcwszSrc[offSrc] == 0)
|
---|
937 | break;
|
---|
938 |
|
---|
939 | /* Check for newlines not preceeded by carriage return: "\n" -> "\r\n"; but not "\r\n" to "\r\r\n"! */
|
---|
940 | if ( pcwszSrc[offSrc] == VBOX_SHCL_LINEFEED
|
---|
941 | && (offSrc == 0 || pcwszSrc[offSrc - 1] != VBOX_SHCL_CARRIAGERETURN))
|
---|
942 | {
|
---|
943 | pu16Dst[offDst++] = VBOX_SHCL_CARRIAGERETURN;
|
---|
944 |
|
---|
945 | /* Ensure sufficient output space: */
|
---|
946 | if (offDst < cwcDst) { /* likely */ }
|
---|
947 | else return VERR_BUFFER_OVERFLOW;
|
---|
948 | }
|
---|
949 | #ifdef RT_OS_DARWIN
|
---|
950 | /* Check for a carriage return not followed by newline (MacOS): "\r" -> "\n\r"; but not "\r\n" to "\r\n\n"! */
|
---|
951 | else if ( pcwszSrc[offSrc] == VBOX_SHCL_CARRIAGERETURN
|
---|
952 | && (offSrc + 1 >= cwcSrc || pcwszSrc[offSrc + 1] != VBOX_SHCL_LINEFEED))
|
---|
953 | {
|
---|
954 | pu16Dst[offDst++] = VBOX_SHCL_CARRIAGERETURN;
|
---|
955 |
|
---|
956 | /* Ensure more output space: */
|
---|
957 | if (offDst < cwcDst) { /* likely */ }
|
---|
958 | else return VERR_BUFFER_OVERFLOW;
|
---|
959 |
|
---|
960 | /* Add line feed. */
|
---|
961 | pu16Dst[offDst] = VBOX_SHCL_LINEFEED;
|
---|
962 | continue;
|
---|
963 | }
|
---|
964 | #endif
|
---|
965 | pu16Dst[offDst] = pcwszSrc[offSrc];
|
---|
966 | }
|
---|
967 |
|
---|
968 | /* Add terminator. */
|
---|
969 | if (offDst < cwcDst)
|
---|
970 | {
|
---|
971 | pu16Dst[offDst] = 0;
|
---|
972 | return VINF_SUCCESS;
|
---|
973 | }
|
---|
974 | return VERR_BUFFER_OVERFLOW;
|
---|
975 | }
|
---|
976 |
|
---|
977 | int ShClConvUtf16CRLFToLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
|
---|
978 | {
|
---|
979 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
980 | AssertReturn(cwcSrc, VERR_INVALID_PARAMETER);
|
---|
981 | AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
|
---|
982 | AssertReturn(cwDst, VERR_INVALID_PARAMETER);
|
---|
983 |
|
---|
984 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
985 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
986 |
|
---|
987 | /* Prepend the Utf16 byte order marker if it is missing. */
|
---|
988 | size_t cwDstPos;
|
---|
989 | if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
|
---|
990 | {
|
---|
991 | cwDstPos = 0;
|
---|
992 | }
|
---|
993 | else
|
---|
994 | {
|
---|
995 | pu16Dst[0] = VBOX_SHCL_UTF16LEMARKER;
|
---|
996 | cwDstPos = 1;
|
---|
997 | }
|
---|
998 |
|
---|
999 | for (size_t i = 0; i < cwcSrc; ++i, ++cwDstPos)
|
---|
1000 | {
|
---|
1001 | if (pcwszSrc[i] == 0)
|
---|
1002 | break;
|
---|
1003 |
|
---|
1004 | if (cwDstPos == cwDst)
|
---|
1005 | return VERR_BUFFER_OVERFLOW;
|
---|
1006 |
|
---|
1007 | if ( (i + 1 < cwcSrc)
|
---|
1008 | && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
|
---|
1009 | && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
|
---|
1010 | {
|
---|
1011 | ++i;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | pu16Dst[cwDstPos] = pcwszSrc[i];
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (cwDstPos == cwDst)
|
---|
1018 | return VERR_BUFFER_OVERFLOW;
|
---|
1019 |
|
---|
1020 | /* Add terminating zero. */
|
---|
1021 | pu16Dst[cwDstPos] = 0;
|
---|
1022 |
|
---|
1023 | return VINF_SUCCESS;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
|
---|
1027 | {
|
---|
1028 | AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
|
---|
1029 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
1030 | AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
|
---|
1031 | AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
|
---|
1032 |
|
---|
1033 | PBMPWIN3XINFOHDR coreHdr = (PBMPWIN3XINFOHDR)pvSrc;
|
---|
1034 | /** @todo Support all the many versions of the DIB headers. */
|
---|
1035 | if ( cbSrc < sizeof(BMPWIN3XINFOHDR)
|
---|
1036 | || RT_LE2H_U32(coreHdr->cbSize) < sizeof(BMPWIN3XINFOHDR)
|
---|
1037 | || RT_LE2H_U32(coreHdr->cbSize) != sizeof(BMPWIN3XINFOHDR))
|
---|
1038 | {
|
---|
1039 | return VERR_INVALID_PARAMETER;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | size_t offPixel = sizeof(BMPFILEHDR)
|
---|
1043 | + RT_LE2H_U32(coreHdr->cbSize)
|
---|
1044 | + RT_LE2H_U32(coreHdr->cClrUsed) * sizeof(uint32_t);
|
---|
1045 | if (cbSrc < offPixel)
|
---|
1046 | return VERR_INVALID_PARAMETER;
|
---|
1047 |
|
---|
1048 | size_t cbDst = sizeof(BMPFILEHDR) + cbSrc;
|
---|
1049 |
|
---|
1050 | void *pvDest = RTMemAlloc(cbDst);
|
---|
1051 | if (!pvDest)
|
---|
1052 | return VERR_NO_MEMORY;
|
---|
1053 |
|
---|
1054 | PBMPFILEHDR fileHdr = (PBMPFILEHDR)pvDest;
|
---|
1055 |
|
---|
1056 | fileHdr->uType = BMP_HDR_MAGIC;
|
---|
1057 | fileHdr->cbFileSize = (uint32_t)RT_H2LE_U32(cbDst);
|
---|
1058 | fileHdr->Reserved1 = 0;
|
---|
1059 | fileHdr->Reserved2 = 0;
|
---|
1060 | fileHdr->offBits = (uint32_t)RT_H2LE_U32(offPixel);
|
---|
1061 |
|
---|
1062 | memcpy((uint8_t *)pvDest + sizeof(BMPFILEHDR), pvSrc, cbSrc);
|
---|
1063 |
|
---|
1064 | *ppvDest = pvDest;
|
---|
1065 | *pcbDest = cbDst;
|
---|
1066 |
|
---|
1067 | return VINF_SUCCESS;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
|
---|
1071 | {
|
---|
1072 | AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
|
---|
1073 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
1074 | AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
|
---|
1075 | AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
|
---|
1076 |
|
---|
1077 | PBMPFILEHDR pBmpHdr = (PBMPFILEHDR)pvSrc;
|
---|
1078 | if ( cbSrc < sizeof(BMPFILEHDR)
|
---|
1079 | || pBmpHdr->uType != BMP_HDR_MAGIC
|
---|
1080 | || RT_LE2H_U32(pBmpHdr->cbFileSize) != cbSrc)
|
---|
1081 | {
|
---|
1082 | return VERR_INVALID_PARAMETER;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMPFILEHDR);
|
---|
1086 | *pcbDest = cbSrc - sizeof(BMPFILEHDR);
|
---|
1087 |
|
---|
1088 | return VINF_SUCCESS;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | #ifdef LOG_ENABLED
|
---|
1092 |
|
---|
1093 | int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
|
---|
1094 | {
|
---|
1095 | int rc = VINF_SUCCESS;
|
---|
1096 | char *pszBuf = (char *)RTMemTmpAllocZ(cbSrc + 1);
|
---|
1097 | if (pszBuf)
|
---|
1098 | {
|
---|
1099 | memcpy(pszBuf, pcszSrc, cbSrc);
|
---|
1100 | pszBuf[cbSrc] = '\0';
|
---|
1101 | for (size_t off = 0; off < cbSrc; ++off)
|
---|
1102 | if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
|
---|
1103 | pszBuf[off] = ' ';
|
---|
1104 | LogFunc(("Removed \\r\\n: %s\n", pszBuf));
|
---|
1105 | RTMemTmpFree(pszBuf);
|
---|
1106 | }
|
---|
1107 | else
|
---|
1108 | rc = VERR_NO_MEMORY;
|
---|
1109 | return rc;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
|
---|
1113 | {
|
---|
1114 | if (LogIsEnabled())
|
---|
1115 | {
|
---|
1116 | if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
|
---|
1117 | {
|
---|
1118 | LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
|
---|
1119 | if (pv && cb)
|
---|
1120 | LogFunc(("%ls\n", pv));
|
---|
1121 | else
|
---|
1122 | LogFunc(("%p %zu\n", pv, cb));
|
---|
1123 | }
|
---|
1124 | else if (uFormat & VBOX_SHCL_FMT_BITMAP)
|
---|
1125 | LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
|
---|
1126 | else if (uFormat & VBOX_SHCL_FMT_HTML)
|
---|
1127 | {
|
---|
1128 | LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
|
---|
1129 | if (pv && cb)
|
---|
1130 | {
|
---|
1131 | LogFunc(("%s\n", pv));
|
---|
1132 | ShClDbgDumpHtml((const char *)pv, cb);
|
---|
1133 | }
|
---|
1134 | else
|
---|
1135 | LogFunc(("%p %zu\n", pv, cb));
|
---|
1136 | }
|
---|
1137 | else
|
---|
1138 | LogFunc(("Invalid format %02X\n", uFormat));
|
---|
1139 | }
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | #endif /* LOG_ENABLED */
|
---|
1143 |
|
---|
1144 | /**
|
---|
1145 | * Translates a Shared Clipboard host function number to a string.
|
---|
1146 | *
|
---|
1147 | * @returns Function ID string name.
|
---|
1148 | * @param uFn The function to translate.
|
---|
1149 | */
|
---|
1150 | const char *ShClHostFunctionToStr(uint32_t uFn)
|
---|
1151 | {
|
---|
1152 | switch (uFn)
|
---|
1153 | {
|
---|
1154 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
|
---|
1155 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
|
---|
1156 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
|
---|
1157 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
|
---|
1158 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
|
---|
1159 | }
|
---|
1160 | return "Unknown";
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | /**
|
---|
1164 | * Translates a Shared Clipboard host message enum to a string.
|
---|
1165 | *
|
---|
1166 | * @returns Message ID string name.
|
---|
1167 | * @param uMsg The message to translate.
|
---|
1168 | */
|
---|
1169 | const char *ShClHostMsgToStr(uint32_t uMsg)
|
---|
1170 | {
|
---|
1171 | switch (uMsg)
|
---|
1172 | {
|
---|
1173 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
|
---|
1174 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
|
---|
1175 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
|
---|
1176 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_CANCELED);
|
---|
1177 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA_CID);
|
---|
1178 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
|
---|
1179 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
|
---|
1180 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
|
---|
1181 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
|
---|
1182 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
|
---|
1183 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
|
---|
1184 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
|
---|
1185 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
|
---|
1186 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
|
---|
1187 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
|
---|
1188 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
|
---|
1189 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
|
---|
1190 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
|
---|
1191 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
|
---|
1192 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
|
---|
1193 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
|
---|
1194 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
|
---|
1195 | }
|
---|
1196 | return "Unknown";
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | /**
|
---|
1200 | * Translates a Shared Clipboard guest message enum to a string.
|
---|
1201 | *
|
---|
1202 | * @returns Message ID string name.
|
---|
1203 | * @param uMsg The message to translate.
|
---|
1204 | */
|
---|
1205 | const char *ShClGuestMsgToStr(uint32_t uMsg)
|
---|
1206 | {
|
---|
1207 | switch (uMsg)
|
---|
1208 | {
|
---|
1209 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
|
---|
1210 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
|
---|
1211 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
|
---|
1212 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
|
---|
1213 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
|
---|
1214 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
|
---|
1215 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
|
---|
1216 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
|
---|
1217 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
|
---|
1218 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
|
---|
1219 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
|
---|
1220 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
|
---|
1221 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
|
---|
1222 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
|
---|
1223 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
|
---|
1224 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
|
---|
1225 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
|
---|
1226 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
|
---|
1227 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
|
---|
1228 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
|
---|
1229 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
|
---|
1230 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
|
---|
1231 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
|
---|
1232 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
|
---|
1233 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
|
---|
1234 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
|
---|
1235 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
|
---|
1236 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE);
|
---|
1237 | }
|
---|
1238 | return "Unknown";
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Converts Shared Clipboard formats to a string.
|
---|
1243 | *
|
---|
1244 | * @returns Stringified Shared Clipboard formats, or NULL on failure. Must be free'd with RTStrFree().
|
---|
1245 | * @param fFormats Shared Clipboard formats to convert.
|
---|
1246 | *
|
---|
1247 | */
|
---|
1248 | char *ShClFormatsToStrA(SHCLFORMATS fFormats)
|
---|
1249 | {
|
---|
1250 | #define APPEND_FMT_TO_STR(_aFmt) \
|
---|
1251 | if (fFormats & VBOX_SHCL_FMT_##_aFmt) \
|
---|
1252 | { \
|
---|
1253 | if (pszFmts) \
|
---|
1254 | { \
|
---|
1255 | rc2 = RTStrAAppend(&pszFmts, ", "); \
|
---|
1256 | if (RT_FAILURE(rc2)) \
|
---|
1257 | break; \
|
---|
1258 | } \
|
---|
1259 | \
|
---|
1260 | rc2 = RTStrAAppend(&pszFmts, #_aFmt); \
|
---|
1261 | if (RT_FAILURE(rc2)) \
|
---|
1262 | break; \
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | char *pszFmts = NULL;
|
---|
1266 | int rc2 = VINF_SUCCESS;
|
---|
1267 |
|
---|
1268 | do
|
---|
1269 | {
|
---|
1270 | APPEND_FMT_TO_STR(UNICODETEXT);
|
---|
1271 | APPEND_FMT_TO_STR(BITMAP);
|
---|
1272 | APPEND_FMT_TO_STR(HTML);
|
---|
1273 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1274 | APPEND_FMT_TO_STR(URI_LIST);
|
---|
1275 | # endif
|
---|
1276 |
|
---|
1277 | } while (0);
|
---|
1278 |
|
---|
1279 | if (!pszFmts)
|
---|
1280 | rc2 = RTStrAAppend(&pszFmts, "NONE");
|
---|
1281 |
|
---|
1282 | if ( RT_FAILURE(rc2)
|
---|
1283 | && pszFmts)
|
---|
1284 | {
|
---|
1285 | RTStrFree(pszFmts);
|
---|
1286 | pszFmts = NULL;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | #undef APPEND_FMT_TO_STR
|
---|
1290 |
|
---|
1291 | return pszFmts;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 |
|
---|
1295 | /*********************************************************************************************************************************
|
---|
1296 | * Shared Clipboard Cache *
|
---|
1297 | *********************************************************************************************************************************/
|
---|
1298 |
|
---|
1299 | /**
|
---|
1300 | * Returns (mutable) data of a cache entry.
|
---|
1301 | *
|
---|
1302 | * @param pCacheEntry Cache entry to return data for.
|
---|
1303 | * @param pvData Where to return the (mutable) data pointer.
|
---|
1304 | * @param pcbData Where to return the data size.
|
---|
1305 | */
|
---|
1306 | void ShClCacheEntryGet(PSHCLCACHEENTRY pCacheEntry, void **pvData, size_t *pcbData)
|
---|
1307 | {
|
---|
1308 | AssertPtrReturnVoid(pCacheEntry);
|
---|
1309 | AssertPtrReturnVoid(pvData);
|
---|
1310 | AssertReturnVoid(pcbData);
|
---|
1311 |
|
---|
1312 | *pvData = pCacheEntry->pvData;
|
---|
1313 | *pcbData = pCacheEntry->cbData;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | /**
|
---|
1317 | * Initializes a cache entry.
|
---|
1318 | *
|
---|
1319 | * @returns VBox status code.
|
---|
1320 | * @param pCacheEntry Cache entry to init.
|
---|
1321 | * @param pvData Data to copy to entry. Can be NULL to initialize an emptry entry.
|
---|
1322 | * @param cbData Size (in bytes) of \a pvData to copy to entry. Must be 0 if \a pvData is NULL.
|
---|
1323 | */
|
---|
1324 | static int shClCacheEntryInit(PSHCLCACHEENTRY pCacheEntry, const void *pvData, size_t cbData)
|
---|
1325 | {
|
---|
1326 | AssertReturn(RT_VALID_PTR(pvData) || cbData == 0, VERR_INVALID_PARAMETER);
|
---|
1327 |
|
---|
1328 | RT_BZERO(pCacheEntry, sizeof(SHCLCACHEENTRY));
|
---|
1329 |
|
---|
1330 | if (pvData)
|
---|
1331 | {
|
---|
1332 | pCacheEntry->pvData = RTMemDup(pvData, cbData);
|
---|
1333 | AssertPtrReturn(pCacheEntry->pvData, VERR_NO_MEMORY);
|
---|
1334 | pCacheEntry->cbData = cbData;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | return VINF_SUCCESS;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | /**
|
---|
1341 | * Returns whether a cache entry is valid (cache hit) or not.
|
---|
1342 | *
|
---|
1343 | * @returns \c true if valid, or \c false if not.
|
---|
1344 | * @param pCacheEntry Cache entry to check for.
|
---|
1345 | */
|
---|
1346 | DECLINLINE(bool) shClCacheEntryIsValid(PSHCLCACHEENTRY pCacheEntry)
|
---|
1347 | {
|
---|
1348 | return pCacheEntry->pvData != NULL;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * Destroys a cache entry.
|
---|
1353 | *
|
---|
1354 | * @param pCacheEntry Cache entry to destroy.
|
---|
1355 | */
|
---|
1356 | DECLINLINE(void) shClCacheEntryDestroy(PSHCLCACHEENTRY pCacheEntry)
|
---|
1357 | {
|
---|
1358 | if (pCacheEntry->pvData)
|
---|
1359 | {
|
---|
1360 | Assert(pCacheEntry->cbData);
|
---|
1361 | RTMemFree(pCacheEntry->pvData);
|
---|
1362 | pCacheEntry->pvData = NULL;
|
---|
1363 | pCacheEntry->cbData = 0;
|
---|
1364 | }
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | /**
|
---|
1368 | * Initializes a cache.
|
---|
1369 | *
|
---|
1370 | * @param pCache Cache to init.
|
---|
1371 | */
|
---|
1372 | void ShClCacheInit(PSHCLCACHE pCache)
|
---|
1373 | {
|
---|
1374 | AssertPtrReturnVoid(pCache);
|
---|
1375 |
|
---|
1376 | RT_BZERO(pCache, sizeof(SHCLCACHE));
|
---|
1377 |
|
---|
1378 | for (size_t i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
|
---|
1379 | shClCacheEntryInit(&pCache->aEntries[i], NULL, 0);
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /**
|
---|
1383 | * Destroys all entries of a cache.
|
---|
1384 | *
|
---|
1385 | * @param pCache Cache to destroy entries for.
|
---|
1386 | */
|
---|
1387 | DECLINLINE(void) shClCacheDestroyEntries(PSHCLCACHE pCache)
|
---|
1388 | {
|
---|
1389 | for (size_t i = 0; i < RT_ELEMENTS(pCache->aEntries); i++)
|
---|
1390 | shClCacheEntryDestroy(&pCache->aEntries[i]);
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | /**
|
---|
1394 | * Destroys a cache.
|
---|
1395 | *
|
---|
1396 | * @param pCache Cache to destroy.
|
---|
1397 | */
|
---|
1398 | void ShClCacheDestroy(PSHCLCACHE pCache)
|
---|
1399 | {
|
---|
1400 | AssertPtrReturnVoid(pCache);
|
---|
1401 |
|
---|
1402 | shClCacheDestroyEntries(pCache);
|
---|
1403 |
|
---|
1404 | RT_BZERO(pCache, sizeof(SHCLCACHE));
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Invalidates a cache.
|
---|
1409 | *
|
---|
1410 | * @param pCache Cache to invalidate.
|
---|
1411 | */
|
---|
1412 | void ShClCacheInvalidate(PSHCLCACHE pCache)
|
---|
1413 | {
|
---|
1414 | AssertPtrReturnVoid(pCache);
|
---|
1415 |
|
---|
1416 | shClCacheDestroyEntries(pCache);
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | /**
|
---|
1420 | * Invalidates a specific cache entry.
|
---|
1421 | *
|
---|
1422 | * @param pCache Cache to invalidate.
|
---|
1423 | * @param uFmt Format to invalidate entry for.
|
---|
1424 | */
|
---|
1425 | void ShClCacheInvalidateEntry(PSHCLCACHE pCache, SHCLFORMAT uFmt)
|
---|
1426 | {
|
---|
1427 | AssertPtrReturnVoid(pCache);
|
---|
1428 | AssertReturnVoid(uFmt < VBOX_SHCL_FMT_MAX);
|
---|
1429 | shClCacheEntryDestroy(&pCache->aEntries[uFmt]);
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | /**
|
---|
1433 | * Gets an entry for a Shared Clipboard format.
|
---|
1434 | *
|
---|
1435 | * @returns Pointer to entry if cached, or NULL if not in cache (cache miss).
|
---|
1436 | * @param pCache Cache to get entry for.
|
---|
1437 | * @param uFmt Format to get entry for.
|
---|
1438 | */
|
---|
1439 | PSHCLCACHEENTRY ShClCacheGet(PSHCLCACHE pCache, SHCLFORMAT uFmt)
|
---|
1440 | {
|
---|
1441 | AssertReturn(uFmt < VBOX_SHCL_FMT_MAX, NULL);
|
---|
1442 | return shClCacheEntryIsValid(&pCache->aEntries[uFmt]) ? &pCache->aEntries[uFmt] : NULL;
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | /**
|
---|
1446 | * Sets data to cache for a specific clipboard format, internal version.
|
---|
1447 | *
|
---|
1448 | * @returns VBox status code.
|
---|
1449 | * @param pCache Cache to set data for.
|
---|
1450 | * @param uFmt Clipboard format to set data for.
|
---|
1451 | * @param pvData Data to set.
|
---|
1452 | * @param cbData Size (in bytes) of data to set.
|
---|
1453 | */
|
---|
1454 | DECLINLINE(int) shClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData)
|
---|
1455 | {
|
---|
1456 | AssertReturn(uFmt < VBOX_SHCL_FMT_MAX, VERR_INVALID_PARAMETER);
|
---|
1457 | AssertReturn(shClCacheEntryIsValid(&pCache->aEntries[uFmt]) == false, VERR_ALREADY_EXISTS);
|
---|
1458 |
|
---|
1459 | return shClCacheEntryInit(&pCache->aEntries[uFmt], pvData, cbData);
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | /**
|
---|
1463 | * Sets data to cache for a specific clipboard format.
|
---|
1464 | *
|
---|
1465 | * @returns VBox status code.
|
---|
1466 | * @param pCache Cache to set data for.
|
---|
1467 | * @param uFmt Clipboard format to set data for.
|
---|
1468 | * @param pvData Data to set.
|
---|
1469 | * @param cbData Size (in bytes) of data to set.
|
---|
1470 | */
|
---|
1471 | int ShClCacheSet(PSHCLCACHE pCache, SHCLFORMAT uFmt, const void *pvData, size_t cbData)
|
---|
1472 | {
|
---|
1473 | AssertPtrReturn(pCache, VERR_INVALID_POINTER);
|
---|
1474 |
|
---|
1475 | if (!pvData) /* Nothing to cache? */
|
---|
1476 | return VINF_SUCCESS;
|
---|
1477 |
|
---|
1478 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
1479 |
|
---|
1480 | return shClCacheSet(pCache, uFmt, pvData, cbData);
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | /**
|
---|
1484 | * Sets data to cache for multiple clipboard formats.
|
---|
1485 | *
|
---|
1486 | * Will bail out if a given format cannot be handled with the data given.
|
---|
1487 | *
|
---|
1488 | * @returns VBox status code.
|
---|
1489 | * @param pCache Cache to set data for.
|
---|
1490 | * @param uFmt Clipboard format to set data for.
|
---|
1491 | * @param pvData Data to set.
|
---|
1492 | * @param cbData Size (in bytes) of data to set.
|
---|
1493 | */
|
---|
1494 | int ShClCacheSetMultiple(PSHCLCACHE pCache, SHCLFORMATS uFmts, const void *pvData, size_t cbData)
|
---|
1495 | {
|
---|
1496 | AssertPtrReturn(pCache, VERR_INVALID_POINTER);
|
---|
1497 |
|
---|
1498 | if (!pvData) /* Nothing to cache? */
|
---|
1499 | return VINF_SUCCESS;
|
---|
1500 |
|
---|
1501 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
1502 |
|
---|
1503 | int rc = VINF_SUCCESS;
|
---|
1504 |
|
---|
1505 | SHCLFORMATS uFmtsLeft = uFmts;
|
---|
1506 | while (uFmtsLeft)
|
---|
1507 | {
|
---|
1508 | SHCLFORMAT uFmt = VBOX_SHCL_FMT_NONE;
|
---|
1509 | void *pvConv = NULL;
|
---|
1510 | size_t cbConv = 0;
|
---|
1511 | if (uFmtsLeft & VBOX_SHCL_FMT_UNICODETEXT)
|
---|
1512 | {
|
---|
1513 | uFmt = VBOX_SHCL_FMT_UNICODETEXT;
|
---|
1514 |
|
---|
1515 | rc = RTStrValidateEncoding((const char *)pvData);
|
---|
1516 | if (RT_SUCCESS(rc))
|
---|
1517 | {
|
---|
1518 | rc = RTStrToUtf16((const char *)pvData, (PRTUTF16 *)&pvConv);
|
---|
1519 | if (RT_SUCCESS(rc))
|
---|
1520 | cbConv = (RTUtf16Len((const PRTUTF16)pvConv) + 1) * sizeof(RTUTF16);
|
---|
1521 | }
|
---|
1522 | else if (!RTUtf16ValidateEncoding((const PRTUTF16)pvData))
|
---|
1523 | {
|
---|
1524 | AssertFailedBreakStmt(rc = VERR_INVALID_PARAMETER);
|
---|
1525 | }
|
---|
1526 | }
|
---|
1527 | else if (uFmtsLeft & VBOX_SHCL_FMT_BITMAP)
|
---|
1528 | uFmt = VBOX_SHCL_FMT_BITMAP;
|
---|
1529 | else if (uFmtsLeft & VBOX_SHCL_FMT_HTML)
|
---|
1530 | uFmt = VBOX_SHCL_FMT_HTML;
|
---|
1531 | else if (uFmtsLeft & VBOX_SHCL_FMT_URI_LIST)
|
---|
1532 | uFmt = VBOX_SHCL_FMT_URI_LIST;
|
---|
1533 | else
|
---|
1534 | AssertFailedBreakStmt(rc = VERR_NOT_SUPPORTED);
|
---|
1535 |
|
---|
1536 | uFmtsLeft &= ~uFmt; /* Remove from list. */
|
---|
1537 | Assert(RT_VALID_PTR(pvConv) || cbConv == 0); /* Sanity. */
|
---|
1538 |
|
---|
1539 | if (RT_SUCCESS(rc))
|
---|
1540 | rc = shClCacheSet(pCache, uFmt,
|
---|
1541 | pvConv ? pvConv : pvData,
|
---|
1542 | cbConv ? cbConv : cbData);
|
---|
1543 | RTMemFree(pvConv);
|
---|
1544 | AssertRCBreak(rc);
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | return rc;
|
---|
1548 | }
|
---|
1549 |
|
---|