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