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