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