1 | /* $Id: clipboard-common.cpp 99974 2023-05-25 11:10:14Z 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-2023 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 |
|
---|
52 | static void shClEventDestroy(PSHCLEVENT pEvent);
|
---|
53 | DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent);
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Implementation *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Allocates a new event payload.
|
---|
62 | *
|
---|
63 | * @returns VBox status code.
|
---|
64 | * @param uID Payload ID to set for this payload. Useful for consequtive payloads.
|
---|
65 | * @param pvData Data block to associate to this payload.
|
---|
66 | * @param cbData Size (in bytes) of data block to associate.
|
---|
67 | * @param ppPayload Where to store the allocated event payload on success.
|
---|
68 | */
|
---|
69 | int ShClPayloadAlloc(uint32_t uID, const void *pvData, uint32_t cbData,
|
---|
70 | PSHCLEVENTPAYLOAD *ppPayload)
|
---|
71 | {
|
---|
72 | AssertPtrReturn(pvData, VERR_INVALID_POINTER);
|
---|
73 | AssertReturn(cbData > 0, VERR_INVALID_PARAMETER);
|
---|
74 |
|
---|
75 | PSHCLEVENTPAYLOAD pPayload = (PSHCLEVENTPAYLOAD)RTMemAlloc(sizeof(SHCLEVENTPAYLOAD));
|
---|
76 | if (pPayload)
|
---|
77 | {
|
---|
78 | pPayload->pvData = RTMemDup(pvData, cbData);
|
---|
79 | if (pPayload->pvData)
|
---|
80 | {
|
---|
81 | pPayload->cbData = cbData;
|
---|
82 | pPayload->uID = uID;
|
---|
83 |
|
---|
84 | *ppPayload = pPayload;
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | }
|
---|
87 |
|
---|
88 | RTMemFree(pPayload);
|
---|
89 | }
|
---|
90 | return VERR_NO_MEMORY;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Frees an event payload.
|
---|
95 | *
|
---|
96 | * @returns VBox status code.
|
---|
97 | * @param pPayload Event payload to free.
|
---|
98 | */
|
---|
99 | void ShClPayloadFree(PSHCLEVENTPAYLOAD pPayload)
|
---|
100 | {
|
---|
101 | if (!pPayload)
|
---|
102 | return;
|
---|
103 |
|
---|
104 | if (pPayload->pvData)
|
---|
105 | {
|
---|
106 | Assert(pPayload->cbData);
|
---|
107 | RTMemFree(pPayload->pvData);
|
---|
108 | pPayload->pvData = NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | pPayload->cbData = 0;
|
---|
112 | pPayload->uID = UINT32_MAX;
|
---|
113 |
|
---|
114 | RTMemFree(pPayload);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Creates a new event source.
|
---|
119 | *
|
---|
120 | * @returns VBox status code.
|
---|
121 | * @param pSource Event source to create.
|
---|
122 | * @param uID ID to use for event source.
|
---|
123 | */
|
---|
124 | int ShClEventSourceCreate(PSHCLEVENTSOURCE pSource, SHCLEVENTSOURCEID uID)
|
---|
125 | {
|
---|
126 | LogFlowFunc(("pSource=%p, uID=%RU16\n", pSource, uID));
|
---|
127 | AssertPtrReturn(pSource, VERR_INVALID_POINTER);
|
---|
128 |
|
---|
129 | int rc = RTCritSectInit(&pSource->CritSect);
|
---|
130 | AssertRCReturn(rc, rc);
|
---|
131 |
|
---|
132 | RTListInit(&pSource->lstEvents);
|
---|
133 |
|
---|
134 | pSource->uID = uID;
|
---|
135 | /* Choose a random event ID starting point. */
|
---|
136 | pSource->idNextEvent = RTRandU32Ex(1, VBOX_SHCL_MAX_EVENTS - 1);
|
---|
137 |
|
---|
138 | return VINF_SUCCESS;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Destroys an event source.
|
---|
143 | *
|
---|
144 | * @returns VBox status code.
|
---|
145 | * @param pSource Event source to destroy.
|
---|
146 | */
|
---|
147 | int ShClEventSourceDestroy(PSHCLEVENTSOURCE pSource)
|
---|
148 | {
|
---|
149 | if (!pSource)
|
---|
150 | return VINF_SUCCESS;
|
---|
151 |
|
---|
152 | if (!RTCritSectIsInitialized(&pSource->CritSect)) /* Already destroyed? Bail out. */
|
---|
153 | return VINF_SUCCESS;
|
---|
154 |
|
---|
155 | LogFlowFunc(("ID=%RU32\n", pSource->uID));
|
---|
156 |
|
---|
157 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | {
|
---|
160 | shClEventSourceResetInternal(pSource);
|
---|
161 |
|
---|
162 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
163 | AssertRC(rc);
|
---|
164 |
|
---|
165 | RTCritSectDelete(&pSource->CritSect);
|
---|
166 |
|
---|
167 | pSource->uID = UINT16_MAX;
|
---|
168 | pSource->idNextEvent = UINT32_MAX;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Resets an event source, internal version.
|
---|
176 | *
|
---|
177 | * @param pSource Event source to reset.
|
---|
178 | */
|
---|
179 | static void shClEventSourceResetInternal(PSHCLEVENTSOURCE pSource)
|
---|
180 | {
|
---|
181 | LogFlowFunc(("ID=%RU32\n", pSource->uID));
|
---|
182 |
|
---|
183 | PSHCLEVENT pEvIt;
|
---|
184 | PSHCLEVENT pEvItNext;
|
---|
185 | RTListForEachSafe(&pSource->lstEvents, pEvIt, pEvItNext, SHCLEVENT, Node)
|
---|
186 | {
|
---|
187 | RTListNodeRemove(&pEvIt->Node);
|
---|
188 |
|
---|
189 | shClEventDestroy(pEvIt);
|
---|
190 |
|
---|
191 | RTMemFree(pEvIt);
|
---|
192 | pEvIt = NULL;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Resets an event source.
|
---|
198 | *
|
---|
199 | * @param pSource Event source to reset.
|
---|
200 | */
|
---|
201 | void ShClEventSourceReset(PSHCLEVENTSOURCE pSource)
|
---|
202 | {
|
---|
203 | int rc2 = RTCritSectEnter(&pSource->CritSect);
|
---|
204 | if (RT_SUCCESS(rc2))
|
---|
205 | {
|
---|
206 | shClEventSourceResetInternal(pSource);
|
---|
207 |
|
---|
208 | rc2 = RTCritSectLeave(&pSource->CritSect);
|
---|
209 | AssertRC(rc2);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Generates a new event ID for a specific event source and registers it.
|
---|
215 | *
|
---|
216 | * @returns VBox status code.
|
---|
217 | * @param pSource Event source to generate event for.
|
---|
218 | * @param ppEvent Where to return the new event generated on success.
|
---|
219 | */
|
---|
220 | int ShClEventSourceGenerateAndRegisterEvent(PSHCLEVENTSOURCE pSource, PSHCLEVENT *ppEvent)
|
---|
221 | {
|
---|
222 | AssertPtrReturn(pSource, VERR_INVALID_POINTER);
|
---|
223 | AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
|
---|
224 |
|
---|
225 | PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
|
---|
226 | AssertReturn(pEvent, VERR_NO_MEMORY);
|
---|
227 | int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
|
---|
228 | if (RT_SUCCESS(rc))
|
---|
229 | {
|
---|
230 | rc = RTCritSectEnter(&pSource->CritSect);
|
---|
231 | if (RT_SUCCESS(rc))
|
---|
232 | {
|
---|
233 | /*
|
---|
234 | * Allocate an unique event ID.
|
---|
235 | */
|
---|
236 | for (uint32_t cTries = 0;; cTries++)
|
---|
237 | {
|
---|
238 | SHCLEVENTID idEvent = ++pSource->idNextEvent;
|
---|
239 | if (idEvent < VBOX_SHCL_MAX_EVENTS)
|
---|
240 | { /* likely */ }
|
---|
241 | else
|
---|
242 | pSource->idNextEvent = idEvent = 1; /* zero == error, remember! */
|
---|
243 |
|
---|
244 | if (shclEventGet(pSource, idEvent) == NULL)
|
---|
245 | {
|
---|
246 | pEvent->pParent = pSource;
|
---|
247 | pEvent->idEvent = idEvent;
|
---|
248 | RTListAppend(&pSource->lstEvents, &pEvent->Node);
|
---|
249 |
|
---|
250 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
251 | AssertRC(rc);
|
---|
252 |
|
---|
253 | LogFlowFunc(("uSource=%RU16: New event: %#x\n", pSource->uID, idEvent));
|
---|
254 |
|
---|
255 | ShClEventRetain(pEvent);
|
---|
256 | *ppEvent = pEvent;
|
---|
257 |
|
---|
258 | return VINF_SUCCESS;
|
---|
259 | }
|
---|
260 |
|
---|
261 | AssertBreak(cTries < 4096);
|
---|
262 | }
|
---|
263 |
|
---|
264 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
265 | AssertRC(rc);
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | AssertMsgFailed(("Unable to register a new event ID for event source %RU16\n", pSource->uID));
|
---|
270 |
|
---|
271 | RTSemEventMultiDestroy(pEvent->hEvtMulSem);
|
---|
272 | pEvent->hEvtMulSem = NIL_RTSEMEVENTMULTI;
|
---|
273 | RTMemFree(pEvent);
|
---|
274 | return rc;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Destroys an event.
|
---|
279 | *
|
---|
280 | * @param pEvent Event to destroy.
|
---|
281 | */
|
---|
282 | static void shClEventDestroy(PSHCLEVENT pEvent)
|
---|
283 | {
|
---|
284 | if (!pEvent)
|
---|
285 | return;
|
---|
286 |
|
---|
287 | AssertMsgReturnVoid(pEvent->cRefs == 0, ("Event %RU32 still has %RU32 references\n",
|
---|
288 | pEvent->idEvent, pEvent->cRefs));
|
---|
289 |
|
---|
290 | LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
|
---|
291 |
|
---|
292 | if (pEvent->hEvtMulSem != NIL_RTSEMEVENT)
|
---|
293 | {
|
---|
294 | RTSemEventMultiDestroy(pEvent->hEvtMulSem);
|
---|
295 | pEvent->hEvtMulSem = NIL_RTSEMEVENT;
|
---|
296 | }
|
---|
297 |
|
---|
298 | ShClPayloadFree(pEvent->pPayload);
|
---|
299 | pEvent->pPayload = NULL;
|
---|
300 |
|
---|
301 | pEvent->idEvent = NIL_SHCLEVENTID;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Unregisters an event.
|
---|
306 | *
|
---|
307 | * @returns VBox status code.
|
---|
308 | * @param pSource Event source to unregister event for.
|
---|
309 | * @param pEvent Event to unregister. On success the pointer will be invalid.
|
---|
310 | */
|
---|
311 | static int shClEventSourceUnregisterEventInternal(PSHCLEVENTSOURCE pSource, PSHCLEVENT pEvent)
|
---|
312 | {
|
---|
313 | LogFlowFunc(("idEvent=%RU32, cRefs=%RU32\n", pEvent->idEvent, pEvent->cRefs));
|
---|
314 |
|
---|
315 | AssertReturn(pEvent->cRefs == 0, VERR_WRONG_ORDER);
|
---|
316 |
|
---|
317 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
318 | if (RT_SUCCESS(rc))
|
---|
319 | {
|
---|
320 | RTListNodeRemove(&pEvent->Node);
|
---|
321 |
|
---|
322 | shClEventDestroy(pEvent);
|
---|
323 |
|
---|
324 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
325 | if (RT_SUCCESS(rc))
|
---|
326 | {
|
---|
327 | RTMemFree(pEvent);
|
---|
328 | pEvent = NULL;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | return rc;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Returns a specific event of a event source. Inlined version.
|
---|
337 | *
|
---|
338 | * @returns Pointer to event if found, or NULL if not found.
|
---|
339 | * @param pSource Event source to get event from.
|
---|
340 | * @param uID Event ID to get.
|
---|
341 | */
|
---|
342 | DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
|
---|
343 | {
|
---|
344 | PSHCLEVENT pEvent;
|
---|
345 | RTListForEach(&pSource->lstEvents, pEvent, SHCLEVENT, Node)
|
---|
346 | {
|
---|
347 | if (pEvent->idEvent == idEvent)
|
---|
348 | return pEvent;
|
---|
349 | }
|
---|
350 |
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Returns a specific event of a event source.
|
---|
356 | *
|
---|
357 | * @returns Pointer to event if found, or NULL if not found.
|
---|
358 | * @param pSource Event source to get event from.
|
---|
359 | * @param idEvent ID of event to return.
|
---|
360 | */
|
---|
361 | PSHCLEVENT ShClEventSourceGetFromId(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
|
---|
362 | {
|
---|
363 | AssertPtrReturn(pSource, NULL);
|
---|
364 |
|
---|
365 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
366 | if (RT_SUCCESS(rc))
|
---|
367 | {
|
---|
368 | PSHCLEVENT pEvent = shclEventGet(pSource, idEvent);
|
---|
369 |
|
---|
370 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
371 | AssertRC(rc);
|
---|
372 |
|
---|
373 | return pEvent;
|
---|
374 | }
|
---|
375 |
|
---|
376 | return NULL;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Returns the last (newest) event ID which has been registered for an event source.
|
---|
381 | *
|
---|
382 | * @returns Pointer to last registered event, or NULL if not found.
|
---|
383 | * @param pSource Event source to get last registered event from.
|
---|
384 | */
|
---|
385 | PSHCLEVENT ShClEventSourceGetLast(PSHCLEVENTSOURCE pSource)
|
---|
386 | {
|
---|
387 | AssertPtrReturn(pSource, NULL);
|
---|
388 |
|
---|
389 | int rc = RTCritSectEnter(&pSource->CritSect);
|
---|
390 | if (RT_SUCCESS(rc))
|
---|
391 | {
|
---|
392 | PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
|
---|
393 |
|
---|
394 | rc = RTCritSectLeave(&pSource->CritSect);
|
---|
395 | AssertRC(rc);
|
---|
396 |
|
---|
397 | return pEvent;
|
---|
398 | }
|
---|
399 |
|
---|
400 | return NULL;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Returns the current reference count for a specific event.
|
---|
405 | *
|
---|
406 | * @returns Reference count.
|
---|
407 | * @param pSource Event source the specific event is part of.
|
---|
408 | * @param idEvent Event ID to return reference count for.
|
---|
409 | */
|
---|
410 | uint32_t ShClEventGetRefs(PSHCLEVENT pEvent)
|
---|
411 | {
|
---|
412 | AssertPtrReturn(pEvent, 0);
|
---|
413 |
|
---|
414 | return ASMAtomicReadU32(&pEvent->cRefs);
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Detaches a payload from an event, internal version.
|
---|
419 | *
|
---|
420 | * @returns Pointer to the detached payload. Can be NULL if the payload has no payload.
|
---|
421 | * @param pEvent Event to detach payload for.
|
---|
422 | */
|
---|
423 | static PSHCLEVENTPAYLOAD shclEventPayloadDetachInternal(PSHCLEVENT pEvent)
|
---|
424 | {
|
---|
425 | #ifdef VBOX_STRICT
|
---|
426 | AssertPtrReturn(pEvent, NULL);
|
---|
427 | #endif
|
---|
428 |
|
---|
429 | PSHCLEVENTPAYLOAD pPayload = pEvent->pPayload;
|
---|
430 |
|
---|
431 | pEvent->pPayload = NULL;
|
---|
432 |
|
---|
433 | return pPayload;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Waits for an event to get signalled.
|
---|
438 | *
|
---|
439 | * @returns VBox status code.
|
---|
440 | * @param pEvent Event to wait for.
|
---|
441 | * @param uTimeoutMs Timeout (in ms) to wait.
|
---|
442 | * @param ppPayload Where to store the (allocated) event payload on success. Needs to be free'd with
|
---|
443 | * SharedClipboardPayloadFree(). Optional.
|
---|
444 | */
|
---|
445 | int ShClEventWait(PSHCLEVENT pEvent, RTMSINTERVAL uTimeoutMs, PSHCLEVENTPAYLOAD *ppPayload)
|
---|
446 | {
|
---|
447 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
448 | AssertPtrNullReturn(ppPayload, VERR_INVALID_POINTER);
|
---|
449 | LogFlowFuncEnter();
|
---|
450 |
|
---|
451 | int rc = RTSemEventMultiWait(pEvent->hEvtMulSem, uTimeoutMs);
|
---|
452 | if (RT_SUCCESS(rc))
|
---|
453 | {
|
---|
454 | if (ppPayload)
|
---|
455 | {
|
---|
456 | /* Make sure to detach payload here, as the caller now owns the data. */
|
---|
457 | *ppPayload = shclEventPayloadDetachInternal(pEvent);
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | if (RT_FAILURE(rc))
|
---|
462 | LogRel2(("Shared Clipboard: Waiting for event %RU32 failed, rc=%Rrc\n", pEvent->idEvent, rc));
|
---|
463 |
|
---|
464 | LogFlowFuncLeaveRC(rc);
|
---|
465 | return rc;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Retains an event by increasing its reference count.
|
---|
470 | *
|
---|
471 | * @returns New reference count, or UINT32_MAX if failed.
|
---|
472 | * @param pEvent Event to retain.
|
---|
473 | */
|
---|
474 | uint32_t ShClEventRetain(PSHCLEVENT pEvent)
|
---|
475 | {
|
---|
476 | AssertPtrReturn(pEvent, UINT32_MAX);
|
---|
477 | AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) < 64, UINT32_MAX);
|
---|
478 | return ASMAtomicIncU32(&pEvent->cRefs);
|
---|
479 | }
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * Releases an event by decreasing its reference count.
|
---|
483 | *
|
---|
484 | * @returns New reference count, or UINT32_MAX if failed.
|
---|
485 | * @param pEvent Event to release.
|
---|
486 | * If the reference count reaches 0, the event will
|
---|
487 | * be destroyed and \a pEvent will be invalid.
|
---|
488 | */
|
---|
489 | uint32_t ShClEventRelease(PSHCLEVENT pEvent)
|
---|
490 | {
|
---|
491 | if (!pEvent)
|
---|
492 | return 0;
|
---|
493 |
|
---|
494 | AssertReturn(ASMAtomicReadU32(&pEvent->cRefs) > 0, UINT32_MAX);
|
---|
495 |
|
---|
496 | uint32_t const cRefs = ASMAtomicDecU32(&pEvent->cRefs);
|
---|
497 | if (cRefs == 0)
|
---|
498 | {
|
---|
499 | AssertPtr(pEvent->pParent);
|
---|
500 | int rc2 = shClEventSourceUnregisterEventInternal(pEvent->pParent, pEvent);
|
---|
501 | AssertRC(rc2);
|
---|
502 |
|
---|
503 | return RT_SUCCESS(rc2) ? 0 : UINT32_MAX;
|
---|
504 | }
|
---|
505 |
|
---|
506 | return cRefs;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Signals an event.
|
---|
511 | *
|
---|
512 | * @returns VBox status code.
|
---|
513 | * @param pEvent Event to signal.
|
---|
514 | * @param pPayload Event payload to associate. Takes ownership on
|
---|
515 | * success. Optional.
|
---|
516 | */
|
---|
517 | int ShClEventSignal(PSHCLEVENT pEvent, PSHCLEVENTPAYLOAD pPayload)
|
---|
518 | {
|
---|
519 | AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
|
---|
520 |
|
---|
521 | Assert(pEvent->pPayload == NULL);
|
---|
522 |
|
---|
523 | pEvent->pPayload = pPayload;
|
---|
524 |
|
---|
525 | int rc = RTSemEventMultiSignal(pEvent->hEvtMulSem);
|
---|
526 | if (RT_FAILURE(rc))
|
---|
527 | pEvent->pPayload = NULL; /* (no race condition if consumer also enters the critical section) */
|
---|
528 |
|
---|
529 | LogFlowFuncLeaveRC(rc);
|
---|
530 | return rc;
|
---|
531 | }
|
---|
532 |
|
---|
533 | int ShClUtf16LenUtf8(PCRTUTF16 pcwszSrc, size_t cwcSrc, size_t *pchLen)
|
---|
534 | {
|
---|
535 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
536 | AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
|
---|
537 |
|
---|
538 | size_t chLen = 0;
|
---|
539 | int rc = RTUtf16CalcUtf8LenEx(pcwszSrc, cwcSrc, &chLen);
|
---|
540 | if (RT_SUCCESS(rc))
|
---|
541 | *pchLen = chLen;
|
---|
542 | return rc;
|
---|
543 | }
|
---|
544 |
|
---|
545 | int ShClConvUtf16CRLFToUtf8LF(PCRTUTF16 pcwszSrc, size_t cwcSrc,
|
---|
546 | char *pszBuf, size_t cbBuf, size_t *pcbLen)
|
---|
547 | {
|
---|
548 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
549 | AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
|
---|
550 | AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
|
---|
551 | AssertPtrReturn(pcbLen, VERR_INVALID_POINTER);
|
---|
552 |
|
---|
553 | int rc;
|
---|
554 |
|
---|
555 | PRTUTF16 pwszTmp = NULL;
|
---|
556 | size_t cchTmp = 0;
|
---|
557 |
|
---|
558 | size_t cbLen = 0;
|
---|
559 |
|
---|
560 | /* How long will the converted text be? */
|
---|
561 | rc = ShClUtf16CRLFLenUtf8(pcwszSrc, cwcSrc, &cchTmp);
|
---|
562 | if (RT_SUCCESS(rc))
|
---|
563 | {
|
---|
564 | cchTmp++; /* Add space for terminator. */
|
---|
565 |
|
---|
566 | pwszTmp = (PRTUTF16)RTMemAlloc(cchTmp * sizeof(RTUTF16));
|
---|
567 | if (pwszTmp)
|
---|
568 | {
|
---|
569 | rc = ShClConvUtf16CRLFToLF(pcwszSrc, cwcSrc, pwszTmp, cchTmp);
|
---|
570 | if (RT_SUCCESS(rc))
|
---|
571 | rc = RTUtf16ToUtf8Ex(pwszTmp + 1, cchTmp - 1, &pszBuf, cbBuf, &cbLen);
|
---|
572 |
|
---|
573 | RTMemFree(reinterpret_cast<void *>(pwszTmp));
|
---|
574 | }
|
---|
575 | else
|
---|
576 | rc = VERR_NO_MEMORY;
|
---|
577 | }
|
---|
578 |
|
---|
579 | if (RT_SUCCESS(rc))
|
---|
580 | {
|
---|
581 | *pcbLen = cbLen;
|
---|
582 | }
|
---|
583 |
|
---|
584 | return rc;
|
---|
585 | }
|
---|
586 |
|
---|
587 | int ShClConvUtf16LFToCRLFA(PCRTUTF16 pcwszSrc, size_t cwcSrc,
|
---|
588 | PRTUTF16 *ppwszDst, size_t *pcwDst)
|
---|
589 | {
|
---|
590 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
591 | AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
|
---|
592 | AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
|
---|
593 |
|
---|
594 | PRTUTF16 pwszDst = NULL;
|
---|
595 | size_t cchDst;
|
---|
596 |
|
---|
597 | int rc = ShClUtf16LFLenUtf8(pcwszSrc, cwcSrc, &cchDst);
|
---|
598 | if (RT_SUCCESS(rc))
|
---|
599 | {
|
---|
600 | pwszDst = (PRTUTF16)RTMemAlloc((cchDst + 1 /* Leave space for terminator */) * sizeof(RTUTF16));
|
---|
601 | if (pwszDst)
|
---|
602 | {
|
---|
603 | rc = ShClConvUtf16LFToCRLF(pcwszSrc, cwcSrc, pwszDst, cchDst + 1 /* Include terminator */);
|
---|
604 | }
|
---|
605 | else
|
---|
606 | rc = VERR_NO_MEMORY;
|
---|
607 | }
|
---|
608 |
|
---|
609 | if (RT_SUCCESS(rc))
|
---|
610 | {
|
---|
611 | *ppwszDst = pwszDst;
|
---|
612 | *pcwDst = cchDst;
|
---|
613 | }
|
---|
614 | else
|
---|
615 | RTMemFree(pwszDst);
|
---|
616 |
|
---|
617 | LogFlowFuncLeaveRC(rc);
|
---|
618 | return rc;
|
---|
619 | }
|
---|
620 |
|
---|
621 | int ShClConvUtf8LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
|
---|
622 | PRTUTF16 *ppwszDst, size_t *pcwDst)
|
---|
623 | {
|
---|
624 | AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
|
---|
625 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
626 | AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
|
---|
627 | AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
|
---|
628 |
|
---|
629 | /* Intermediate conversion to UTF-16. */
|
---|
630 | size_t cwcTmp;
|
---|
631 | PRTUTF16 pwcTmp = NULL;
|
---|
632 | int rc = RTStrToUtf16Ex(pcszSrc, cbSrc, &pwcTmp, 0, &cwcTmp);
|
---|
633 | if (RT_SUCCESS(rc))
|
---|
634 | {
|
---|
635 | rc = ShClConvUtf16LFToCRLFA(pwcTmp, cwcTmp, ppwszDst, pcwDst);
|
---|
636 | RTUtf16Free(pwcTmp);
|
---|
637 | }
|
---|
638 |
|
---|
639 | return rc;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /**
|
---|
643 | * Converts a Latin-1 string with LF line endings into an UTF-16 string with CRLF endings.
|
---|
644 | *
|
---|
645 | * @returns VBox status code.
|
---|
646 | * @param pcszSrc Latin-1 string to convert.
|
---|
647 | * @param cbSrc Size (in bytes) of Latin-1 string to convert.
|
---|
648 | * @param ppwszDst Where to return the converted UTF-16 string on success.
|
---|
649 | * @param pcwDst Where to return the length (in UTF-16 characters) on success.
|
---|
650 | *
|
---|
651 | * @note Only converts the source until the string terminator is found (or length limit is hit).
|
---|
652 | */
|
---|
653 | int ShClConvLatin1LFToUtf16CRLF(const char *pcszSrc, size_t cbSrc,
|
---|
654 | PRTUTF16 *ppwszDst, size_t *pcwDst)
|
---|
655 | {
|
---|
656 | AssertPtrReturn(pcszSrc, VERR_INVALID_POINTER);
|
---|
657 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
658 | AssertPtrReturn(ppwszDst, VERR_INVALID_POINTER);
|
---|
659 | AssertPtrReturn(pcwDst, VERR_INVALID_POINTER);
|
---|
660 |
|
---|
661 | size_t chSrc = 0;
|
---|
662 |
|
---|
663 | PRTUTF16 pwszDst = NULL;
|
---|
664 |
|
---|
665 | /* Calculate the space needed. */
|
---|
666 | size_t cwDst = 0;
|
---|
667 | for (size_t i = 0; i < cbSrc && pcszSrc[i] != '\0'; ++i)
|
---|
668 | {
|
---|
669 | if (pcszSrc[i] == VBOX_SHCL_LINEFEED)
|
---|
670 | cwDst += 2; /* Space for VBOX_SHCL_CARRIAGERETURN + VBOX_SHCL_LINEFEED. */
|
---|
671 | else
|
---|
672 | ++cwDst;
|
---|
673 | chSrc++;
|
---|
674 | }
|
---|
675 |
|
---|
676 | pwszDst = (PRTUTF16)RTMemAlloc((cwDst + 1 /* Leave space for the terminator */) * sizeof(RTUTF16));
|
---|
677 | AssertPtrReturn(pwszDst, VERR_NO_MEMORY);
|
---|
678 |
|
---|
679 | /* Do the conversion, bearing in mind that Latin-1 expands "naturally" to UTF-16. */
|
---|
680 | for (size_t i = 0, j = 0; i < chSrc; ++i, ++j)
|
---|
681 | {
|
---|
682 | AssertMsg(j <= cwDst, ("cbSrc=%zu, j=%u vs. cwDst=%u\n", cbSrc, j, cwDst));
|
---|
683 | if (pcszSrc[i] != VBOX_SHCL_LINEFEED)
|
---|
684 | pwszDst[j] = pcszSrc[i];
|
---|
685 | else
|
---|
686 | {
|
---|
687 | pwszDst[j] = VBOX_SHCL_CARRIAGERETURN;
|
---|
688 | pwszDst[j + 1] = VBOX_SHCL_LINEFEED;
|
---|
689 | ++j;
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | pwszDst[cwDst] = '\0'; /* Make sure we are zero-terminated. */
|
---|
694 |
|
---|
695 | *ppwszDst = pwszDst;
|
---|
696 | *pcwDst = cwDst;
|
---|
697 |
|
---|
698 | return VINF_SUCCESS;
|
---|
699 | }
|
---|
700 |
|
---|
701 | int ShClConvUtf16ToUtf8HTML(PCRTUTF16 pcwszSrc, size_t cwcSrc, char **ppszDst, size_t *pcbDst)
|
---|
702 | {
|
---|
703 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
704 | AssertReturn (cwcSrc, VERR_INVALID_PARAMETER);
|
---|
705 | AssertPtrReturn(ppszDst, VERR_INVALID_POINTER);
|
---|
706 | AssertPtrReturn(pcbDst, VERR_INVALID_POINTER);
|
---|
707 |
|
---|
708 | int rc = VINF_SUCCESS;
|
---|
709 |
|
---|
710 | size_t cwTmp = cwcSrc;
|
---|
711 | PCRTUTF16 pwTmp = pcwszSrc;
|
---|
712 |
|
---|
713 | char *pchDst = NULL;
|
---|
714 | size_t cbDst = 0;
|
---|
715 |
|
---|
716 | size_t i = 0;
|
---|
717 | while (i < cwTmp)
|
---|
718 | {
|
---|
719 | /* Find zero symbol (end of string). */
|
---|
720 | for (; i < cwTmp && pcwszSrc[i] != 0; i++)
|
---|
721 | ;
|
---|
722 |
|
---|
723 | /* Convert found string. */
|
---|
724 | char *psz = NULL;
|
---|
725 | size_t cch = 0;
|
---|
726 | rc = RTUtf16ToUtf8Ex(pwTmp, cwTmp, &psz, pwTmp - pcwszSrc, &cch);
|
---|
727 | if (RT_FAILURE(rc))
|
---|
728 | break;
|
---|
729 |
|
---|
730 | /* Append new substring. */
|
---|
731 | char *pchNew = (char *)RTMemRealloc(pchDst, cbDst + cch + 1);
|
---|
732 | if (!pchNew)
|
---|
733 | {
|
---|
734 | RTStrFree(psz);
|
---|
735 | rc = VERR_NO_MEMORY;
|
---|
736 | break;
|
---|
737 | }
|
---|
738 |
|
---|
739 | pchDst = pchNew;
|
---|
740 | memcpy(pchDst + cbDst, psz, cch + 1);
|
---|
741 |
|
---|
742 | RTStrFree(psz);
|
---|
743 |
|
---|
744 | cbDst += cch + 1;
|
---|
745 |
|
---|
746 | /* Skip zero symbols. */
|
---|
747 | for (; i < cwTmp && pcwszSrc[i] == 0; i++)
|
---|
748 | ;
|
---|
749 |
|
---|
750 | /* Remember start of string. */
|
---|
751 | pwTmp += i;
|
---|
752 | }
|
---|
753 |
|
---|
754 | if (RT_SUCCESS(rc))
|
---|
755 | {
|
---|
756 | *ppszDst = pchDst;
|
---|
757 | *pcbDst = cbDst;
|
---|
758 |
|
---|
759 | return VINF_SUCCESS;
|
---|
760 | }
|
---|
761 |
|
---|
762 | RTMemFree(pchDst);
|
---|
763 |
|
---|
764 | return rc;
|
---|
765 | }
|
---|
766 |
|
---|
767 | int ShClUtf16LFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
|
---|
768 | {
|
---|
769 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
770 | AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
|
---|
771 |
|
---|
772 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
773 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
774 |
|
---|
775 | size_t cLen = 0;
|
---|
776 |
|
---|
777 | /* Don't copy the endian marker. */
|
---|
778 | size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
|
---|
779 |
|
---|
780 | /* Calculate the size of the destination text string. */
|
---|
781 | /* Is this Utf16 or Utf16-LE? */
|
---|
782 | for (; i < cwSrc; ++i, ++cLen)
|
---|
783 | {
|
---|
784 | /* Check for a single line feed */
|
---|
785 | if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
|
---|
786 | ++cLen;
|
---|
787 | #ifdef RT_OS_DARWIN
|
---|
788 | /* Check for a single carriage return (MacOS) */
|
---|
789 | if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
|
---|
790 | ++cLen;
|
---|
791 | #endif
|
---|
792 | if (pcwszSrc[i] == 0)
|
---|
793 | {
|
---|
794 | /* Don't count this, as we do so below. */
|
---|
795 | break;
|
---|
796 | }
|
---|
797 | }
|
---|
798 |
|
---|
799 | *pchLen = cLen;
|
---|
800 |
|
---|
801 | return VINF_SUCCESS;
|
---|
802 | }
|
---|
803 |
|
---|
804 | int ShClUtf16CRLFLenUtf8(PCRTUTF16 pcwszSrc, size_t cwSrc, size_t *pchLen)
|
---|
805 | {
|
---|
806 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
807 | AssertReturn(cwSrc, VERR_INVALID_PARAMETER);
|
---|
808 | AssertPtrReturn(pchLen, VERR_INVALID_POINTER);
|
---|
809 |
|
---|
810 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
811 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
812 |
|
---|
813 | size_t cLen = 0;
|
---|
814 |
|
---|
815 | /* Calculate the size of the destination text string. */
|
---|
816 | /* Is this Utf16 or Utf16-LE? */
|
---|
817 | if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
|
---|
818 | cLen = 0;
|
---|
819 | else
|
---|
820 | cLen = 1;
|
---|
821 |
|
---|
822 | for (size_t i = 0; i < cwSrc; ++i, ++cLen)
|
---|
823 | {
|
---|
824 | if ( (i + 1 < cwSrc)
|
---|
825 | && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
|
---|
826 | && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
|
---|
827 | {
|
---|
828 | ++i;
|
---|
829 | }
|
---|
830 | if (pcwszSrc[i] == 0)
|
---|
831 | break;
|
---|
832 | }
|
---|
833 |
|
---|
834 | *pchLen = cLen;
|
---|
835 |
|
---|
836 | return VINF_SUCCESS;
|
---|
837 | }
|
---|
838 |
|
---|
839 | int ShClConvUtf16LFToCRLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
|
---|
840 | {
|
---|
841 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
842 | AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
|
---|
843 | AssertReturn(cwDst, VERR_INVALID_PARAMETER);
|
---|
844 |
|
---|
845 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
846 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
847 |
|
---|
848 | int rc = VINF_SUCCESS;
|
---|
849 |
|
---|
850 | /* Don't copy the endian marker. */
|
---|
851 | size_t i = pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER ? 1 : 0;
|
---|
852 | size_t j = 0;
|
---|
853 |
|
---|
854 | for (; i < cwcSrc; ++i, ++j)
|
---|
855 | {
|
---|
856 | /* Don't copy the null byte, as we add it below. */
|
---|
857 | if (pcwszSrc[i] == 0)
|
---|
858 | break;
|
---|
859 |
|
---|
860 | /* Not enough space in destination? */
|
---|
861 | if (j == cwDst)
|
---|
862 | {
|
---|
863 | rc = VERR_BUFFER_OVERFLOW;
|
---|
864 | break;
|
---|
865 | }
|
---|
866 |
|
---|
867 | if (pcwszSrc[i] == VBOX_SHCL_LINEFEED)
|
---|
868 | {
|
---|
869 | pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
|
---|
870 | ++j;
|
---|
871 |
|
---|
872 | /* Not enough space in destination? */
|
---|
873 | if (j == cwDst)
|
---|
874 | {
|
---|
875 | rc = VERR_BUFFER_OVERFLOW;
|
---|
876 | break;
|
---|
877 | }
|
---|
878 | }
|
---|
879 | #ifdef RT_OS_DARWIN
|
---|
880 | /* Check for a single carriage return (MacOS) */
|
---|
881 | else if (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
|
---|
882 | {
|
---|
883 | /* Set CR.r */
|
---|
884 | pu16Dst[j] = VBOX_SHCL_CARRIAGERETURN;
|
---|
885 | ++j;
|
---|
886 |
|
---|
887 | /* Not enough space in destination? */
|
---|
888 | if (j == cwDst)
|
---|
889 | {
|
---|
890 | rc = VERR_BUFFER_OVERFLOW;
|
---|
891 | break;
|
---|
892 | }
|
---|
893 |
|
---|
894 | /* Add line feed. */
|
---|
895 | pu16Dst[j] = VBOX_SHCL_LINEFEED;
|
---|
896 | continue;
|
---|
897 | }
|
---|
898 | #endif
|
---|
899 | pu16Dst[j] = pcwszSrc[i];
|
---|
900 | }
|
---|
901 |
|
---|
902 | if (j == cwDst)
|
---|
903 | rc = VERR_BUFFER_OVERFLOW;
|
---|
904 |
|
---|
905 | if (RT_SUCCESS(rc))
|
---|
906 | {
|
---|
907 | /* Add terminator. */
|
---|
908 | pu16Dst[j] = 0;
|
---|
909 | }
|
---|
910 |
|
---|
911 | return rc;
|
---|
912 | }
|
---|
913 |
|
---|
914 | int ShClConvUtf16CRLFToLF(PCRTUTF16 pcwszSrc, size_t cwcSrc, PRTUTF16 pu16Dst, size_t cwDst)
|
---|
915 | {
|
---|
916 | AssertPtrReturn(pcwszSrc, VERR_INVALID_POINTER);
|
---|
917 | AssertReturn(cwcSrc, VERR_INVALID_PARAMETER);
|
---|
918 | AssertPtrReturn(pu16Dst, VERR_INVALID_POINTER);
|
---|
919 | AssertReturn(cwDst, VERR_INVALID_PARAMETER);
|
---|
920 |
|
---|
921 | AssertMsgReturn(pcwszSrc[0] != VBOX_SHCL_UTF16BEMARKER,
|
---|
922 | ("Big endian UTF-16 not supported yet\n"), VERR_NOT_SUPPORTED);
|
---|
923 |
|
---|
924 | /* Prepend the Utf16 byte order marker if it is missing. */
|
---|
925 | size_t cwDstPos;
|
---|
926 | if (pcwszSrc[0] == VBOX_SHCL_UTF16LEMARKER)
|
---|
927 | {
|
---|
928 | cwDstPos = 0;
|
---|
929 | }
|
---|
930 | else
|
---|
931 | {
|
---|
932 | pu16Dst[0] = VBOX_SHCL_UTF16LEMARKER;
|
---|
933 | cwDstPos = 1;
|
---|
934 | }
|
---|
935 |
|
---|
936 | for (size_t i = 0; i < cwcSrc; ++i, ++cwDstPos)
|
---|
937 | {
|
---|
938 | if (pcwszSrc[i] == 0)
|
---|
939 | break;
|
---|
940 |
|
---|
941 | if (cwDstPos == cwDst)
|
---|
942 | return VERR_BUFFER_OVERFLOW;
|
---|
943 |
|
---|
944 | if ( (i + 1 < cwcSrc)
|
---|
945 | && (pcwszSrc[i] == VBOX_SHCL_CARRIAGERETURN)
|
---|
946 | && (pcwszSrc[i + 1] == VBOX_SHCL_LINEFEED))
|
---|
947 | {
|
---|
948 | ++i;
|
---|
949 | }
|
---|
950 |
|
---|
951 | pu16Dst[cwDstPos] = pcwszSrc[i];
|
---|
952 | }
|
---|
953 |
|
---|
954 | if (cwDstPos == cwDst)
|
---|
955 | return VERR_BUFFER_OVERFLOW;
|
---|
956 |
|
---|
957 | /* Add terminating zero. */
|
---|
958 | pu16Dst[cwDstPos] = 0;
|
---|
959 |
|
---|
960 | return VINF_SUCCESS;
|
---|
961 | }
|
---|
962 |
|
---|
963 | int ShClDibToBmp(const void *pvSrc, size_t cbSrc, void **ppvDest, size_t *pcbDest)
|
---|
964 | {
|
---|
965 | AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
|
---|
966 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
967 | AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
|
---|
968 | AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
|
---|
969 |
|
---|
970 | PBMPWIN3XINFOHDR coreHdr = (PBMPWIN3XINFOHDR)pvSrc;
|
---|
971 | /** @todo Support all the many versions of the DIB headers. */
|
---|
972 | if ( cbSrc < sizeof(BMPWIN3XINFOHDR)
|
---|
973 | || RT_LE2H_U32(coreHdr->cbSize) < sizeof(BMPWIN3XINFOHDR)
|
---|
974 | || RT_LE2H_U32(coreHdr->cbSize) != sizeof(BMPWIN3XINFOHDR))
|
---|
975 | {
|
---|
976 | return VERR_INVALID_PARAMETER;
|
---|
977 | }
|
---|
978 |
|
---|
979 | size_t offPixel = sizeof(BMPFILEHDR)
|
---|
980 | + RT_LE2H_U32(coreHdr->cbSize)
|
---|
981 | + RT_LE2H_U32(coreHdr->cClrUsed) * sizeof(uint32_t);
|
---|
982 | if (cbSrc < offPixel)
|
---|
983 | return VERR_INVALID_PARAMETER;
|
---|
984 |
|
---|
985 | size_t cbDst = sizeof(BMPFILEHDR) + cbSrc;
|
---|
986 |
|
---|
987 | void *pvDest = RTMemAlloc(cbDst);
|
---|
988 | if (!pvDest)
|
---|
989 | return VERR_NO_MEMORY;
|
---|
990 |
|
---|
991 | PBMPFILEHDR fileHdr = (PBMPFILEHDR)pvDest;
|
---|
992 |
|
---|
993 | fileHdr->uType = BMP_HDR_MAGIC;
|
---|
994 | fileHdr->cbFileSize = (uint32_t)RT_H2LE_U32(cbDst);
|
---|
995 | fileHdr->Reserved1 = 0;
|
---|
996 | fileHdr->Reserved2 = 0;
|
---|
997 | fileHdr->offBits = (uint32_t)RT_H2LE_U32(offPixel);
|
---|
998 |
|
---|
999 | memcpy((uint8_t *)pvDest + sizeof(BMPFILEHDR), pvSrc, cbSrc);
|
---|
1000 |
|
---|
1001 | *ppvDest = pvDest;
|
---|
1002 | *pcbDest = cbDst;
|
---|
1003 |
|
---|
1004 | return VINF_SUCCESS;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | int ShClBmpGetDib(const void *pvSrc, size_t cbSrc, const void **ppvDest, size_t *pcbDest)
|
---|
1008 | {
|
---|
1009 | AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
|
---|
1010 | AssertReturn(cbSrc, VERR_INVALID_PARAMETER);
|
---|
1011 | AssertPtrReturn(ppvDest, VERR_INVALID_POINTER);
|
---|
1012 | AssertPtrReturn(pcbDest, VERR_INVALID_POINTER);
|
---|
1013 |
|
---|
1014 | PBMPFILEHDR pBmpHdr = (PBMPFILEHDR)pvSrc;
|
---|
1015 | if ( cbSrc < sizeof(BMPFILEHDR)
|
---|
1016 | || pBmpHdr->uType != BMP_HDR_MAGIC
|
---|
1017 | || RT_LE2H_U32(pBmpHdr->cbFileSize) != cbSrc)
|
---|
1018 | {
|
---|
1019 | return VERR_INVALID_PARAMETER;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | *ppvDest = ((uint8_t *)pvSrc) + sizeof(BMPFILEHDR);
|
---|
1023 | *pcbDest = cbSrc - sizeof(BMPFILEHDR);
|
---|
1024 |
|
---|
1025 | return VINF_SUCCESS;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | #ifdef LOG_ENABLED
|
---|
1029 |
|
---|
1030 | int ShClDbgDumpHtml(const char *pcszSrc, size_t cbSrc)
|
---|
1031 | {
|
---|
1032 | int rc = VINF_SUCCESS;
|
---|
1033 | char *pszBuf = (char *)RTMemTmpAllocZ(cbSrc + 1);
|
---|
1034 | if (pszBuf)
|
---|
1035 | {
|
---|
1036 | memcpy(pszBuf, pcszSrc, cbSrc);
|
---|
1037 | pszBuf[cbSrc] = '\0';
|
---|
1038 | for (size_t off = 0; off < cbSrc; ++off)
|
---|
1039 | if (pszBuf[off] == '\n' || pszBuf[off] == '\r')
|
---|
1040 | pszBuf[off] = ' ';
|
---|
1041 | LogFunc(("Removed \\r\\n: %s\n", pszBuf));
|
---|
1042 | RTMemTmpFree(pszBuf);
|
---|
1043 | }
|
---|
1044 | else
|
---|
1045 | rc = VERR_NO_MEMORY;
|
---|
1046 | return rc;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | void ShClDbgDumpData(const void *pv, size_t cb, SHCLFORMAT uFormat)
|
---|
1050 | {
|
---|
1051 | if (LogIsEnabled())
|
---|
1052 | {
|
---|
1053 | if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
|
---|
1054 | {
|
---|
1055 | LogFunc(("VBOX_SHCL_FMT_UNICODETEXT:\n"));
|
---|
1056 | if (pv && cb)
|
---|
1057 | LogFunc(("%ls\n", pv));
|
---|
1058 | else
|
---|
1059 | LogFunc(("%p %zu\n", pv, cb));
|
---|
1060 | }
|
---|
1061 | else if (uFormat & VBOX_SHCL_FMT_BITMAP)
|
---|
1062 | LogFunc(("VBOX_SHCL_FMT_BITMAP\n"));
|
---|
1063 | else if (uFormat & VBOX_SHCL_FMT_HTML)
|
---|
1064 | {
|
---|
1065 | LogFunc(("VBOX_SHCL_FMT_HTML:\n"));
|
---|
1066 | if (pv && cb)
|
---|
1067 | {
|
---|
1068 | LogFunc(("%s\n", pv));
|
---|
1069 | ShClDbgDumpHtml((const char *)pv, cb);
|
---|
1070 | }
|
---|
1071 | else
|
---|
1072 | LogFunc(("%p %zu\n", pv, cb));
|
---|
1073 | }
|
---|
1074 | else
|
---|
1075 | LogFunc(("Invalid format %02X\n", uFormat));
|
---|
1076 | }
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | #endif /* LOG_ENABLED */
|
---|
1080 |
|
---|
1081 | /**
|
---|
1082 | * Translates a Shared Clipboard host function number to a string.
|
---|
1083 | *
|
---|
1084 | * @returns Function ID string name.
|
---|
1085 | * @param uFn The function to translate.
|
---|
1086 | */
|
---|
1087 | const char *ShClHostFunctionToStr(uint32_t uFn)
|
---|
1088 | {
|
---|
1089 | switch (uFn)
|
---|
1090 | {
|
---|
1091 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_MODE);
|
---|
1092 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE);
|
---|
1093 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_SET_HEADLESS);
|
---|
1094 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_CANCEL);
|
---|
1095 | RT_CASE_RET_STR(VBOX_SHCL_HOST_FN_ERROR);
|
---|
1096 | }
|
---|
1097 | return "Unknown";
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | /**
|
---|
1101 | * Translates a Shared Clipboard host message enum to a string.
|
---|
1102 | *
|
---|
1103 | * @returns Message ID string name.
|
---|
1104 | * @param uMsg The message to translate.
|
---|
1105 | */
|
---|
1106 | const char *ShClHostMsgToStr(uint32_t uMsg)
|
---|
1107 | {
|
---|
1108 | switch (uMsg)
|
---|
1109 | {
|
---|
1110 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_QUIT);
|
---|
1111 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA);
|
---|
1112 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
|
---|
1113 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_CANCELED);
|
---|
1114 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_READ_DATA_CID);
|
---|
1115 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_STATUS);
|
---|
1116 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_READ);
|
---|
1117 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_HDR_WRITE);
|
---|
1118 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_READ);
|
---|
1119 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ROOT_LIST_ENTRY_WRITE);
|
---|
1120 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_OPEN);
|
---|
1121 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_CLOSE);
|
---|
1122 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_READ);
|
---|
1123 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_HDR_WRITE);
|
---|
1124 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_READ);
|
---|
1125 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_LIST_ENTRY_WRITE);
|
---|
1126 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_OPEN);
|
---|
1127 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_CLOSE);
|
---|
1128 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_READ);
|
---|
1129 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_OBJ_WRITE);
|
---|
1130 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_CANCEL);
|
---|
1131 | RT_CASE_RET_STR(VBOX_SHCL_HOST_MSG_TRANSFER_ERROR);
|
---|
1132 | }
|
---|
1133 | return "Unknown";
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | /**
|
---|
1137 | * Translates a Shared Clipboard guest message enum to a string.
|
---|
1138 | *
|
---|
1139 | * @returns Message ID string name.
|
---|
1140 | * @param uMsg The message to translate.
|
---|
1141 | */
|
---|
1142 | const char *ShClGuestMsgToStr(uint32_t uMsg)
|
---|
1143 | {
|
---|
1144 | switch (uMsg)
|
---|
1145 | {
|
---|
1146 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_OLD_GET_WAIT);
|
---|
1147 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FORMATS);
|
---|
1148 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_READ);
|
---|
1149 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_DATA_WRITE);
|
---|
1150 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_CONNECT);
|
---|
1151 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPORT_FEATURES);
|
---|
1152 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_QUERY_FEATURES);
|
---|
1153 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT);
|
---|
1154 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT);
|
---|
1155 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_GET);
|
---|
1156 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_MSG_CANCEL);
|
---|
1157 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_REPLY);
|
---|
1158 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_READ);
|
---|
1159 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_HDR_WRITE);
|
---|
1160 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_READ);
|
---|
1161 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ROOT_LIST_ENTRY_WRITE);
|
---|
1162 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_OPEN);
|
---|
1163 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_CLOSE);
|
---|
1164 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_READ);
|
---|
1165 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_HDR_WRITE);
|
---|
1166 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_READ);
|
---|
1167 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_LIST_ENTRY_WRITE);
|
---|
1168 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_OPEN);
|
---|
1169 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_CLOSE);
|
---|
1170 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_READ);
|
---|
1171 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_OBJ_WRITE);
|
---|
1172 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_ERROR);
|
---|
1173 | RT_CASE_RET_STR(VBOX_SHCL_GUEST_FN_NEGOTIATE_CHUNK_SIZE);
|
---|
1174 | }
|
---|
1175 | return "Unknown";
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | /**
|
---|
1179 | * Converts Shared Clipboard formats to a string.
|
---|
1180 | *
|
---|
1181 | * @returns Stringified Shared Clipboard formats, or NULL on failure. Must be free'd with RTStrFree().
|
---|
1182 | * @param fFormats Shared Clipboard formats to convert.
|
---|
1183 | *
|
---|
1184 | */
|
---|
1185 | char *ShClFormatsToStrA(SHCLFORMATS fFormats)
|
---|
1186 | {
|
---|
1187 | #define APPEND_FMT_TO_STR(_aFmt) \
|
---|
1188 | if (fFormats & VBOX_SHCL_FMT_##_aFmt) \
|
---|
1189 | { \
|
---|
1190 | if (pszFmts) \
|
---|
1191 | { \
|
---|
1192 | rc2 = RTStrAAppend(&pszFmts, ", "); \
|
---|
1193 | if (RT_FAILURE(rc2)) \
|
---|
1194 | break; \
|
---|
1195 | } \
|
---|
1196 | \
|
---|
1197 | rc2 = RTStrAAppend(&pszFmts, #_aFmt); \
|
---|
1198 | if (RT_FAILURE(rc2)) \
|
---|
1199 | break; \
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | char *pszFmts = NULL;
|
---|
1203 | int rc2 = VINF_SUCCESS;
|
---|
1204 |
|
---|
1205 | do
|
---|
1206 | {
|
---|
1207 | APPEND_FMT_TO_STR(UNICODETEXT);
|
---|
1208 | APPEND_FMT_TO_STR(BITMAP);
|
---|
1209 | APPEND_FMT_TO_STR(HTML);
|
---|
1210 | # ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
1211 | APPEND_FMT_TO_STR(URI_LIST);
|
---|
1212 | # endif
|
---|
1213 |
|
---|
1214 | } while (0);
|
---|
1215 |
|
---|
1216 | if (!pszFmts)
|
---|
1217 | rc2 = RTStrAAppend(&pszFmts, "NONE");
|
---|
1218 |
|
---|
1219 | if ( RT_FAILURE(rc2)
|
---|
1220 | && pszFmts)
|
---|
1221 | {
|
---|
1222 | RTStrFree(pszFmts);
|
---|
1223 | pszFmts = NULL;
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | #undef APPEND_FMT_TO_STR
|
---|
1227 |
|
---|
1228 | return pszFmts;
|
---|
1229 | }
|
---|
1230 |
|
---|