/src/FreeRDP/winpr/libwinpr/clipboard/clipboard.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Clipboard Functions |
4 | | * |
5 | | * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include <winpr/crt.h> |
23 | | #include <winpr/collections.h> |
24 | | #include <winpr/wlog.h> |
25 | | |
26 | | #include <winpr/clipboard.h> |
27 | | |
28 | | #include "clipboard.h" |
29 | | |
30 | | #include "synthetic_file.h" |
31 | | |
32 | | #include "../log.h" |
33 | | #define TAG WINPR_TAG("clipboard") |
34 | | |
35 | | const char* const mime_text_plain = "text/plain"; |
36 | | const char* const mime_text_utf8 = "text/plain;charset=utf-8"; |
37 | | const char* const mime_text_UTF8_STRING = "UTF8_STRING"; |
38 | | |
39 | | /** |
40 | | * Clipboard (Windows): |
41 | | * msdn.microsoft.com/en-us/library/windows/desktop/ms648709/ |
42 | | * |
43 | | * W3C Clipboard API and events: |
44 | | * http://www.w3.org/TR/clipboard-apis/ |
45 | | */ |
46 | | |
47 | | static const char* CF_STANDARD_STRINGS[] = { |
48 | | "CF_RAW", /* 0 */ |
49 | | "CF_TEXT", /* 1 */ |
50 | | "CF_BITMAP", /* 2 */ |
51 | | "CF_METAFILEPICT", /* 3 */ |
52 | | "CF_SYLK", /* 4 */ |
53 | | "CF_DIF", /* 5 */ |
54 | | "CF_TIFF", /* 6 */ |
55 | | "CF_OEMTEXT", /* 7 */ |
56 | | "CF_DIB", /* 8 */ |
57 | | "CF_PALETTE", /* 9 */ |
58 | | "CF_PENDATA", /* 10 */ |
59 | | "CF_RIFF", /* 11 */ |
60 | | "CF_WAVE", /* 12 */ |
61 | | "CF_UNICODETEXT", /* 13 */ |
62 | | "CF_ENHMETAFILE", /* 14 */ |
63 | | "CF_HDROP", /* 15 */ |
64 | | "CF_LOCALE", /* 16 */ |
65 | | "CF_DIBV5" /* 17 */ |
66 | | }; |
67 | | |
68 | | const char* ClipboardGetFormatIdString(UINT32 formatId) |
69 | 0 | { |
70 | 0 | if (formatId < ARRAYSIZE(CF_STANDARD_STRINGS)) |
71 | 0 | return CF_STANDARD_STRINGS[formatId]; |
72 | 0 | return "CF_REGISTERED_FORMAT"; |
73 | 0 | } |
74 | | |
75 | | static wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId, |
76 | | const char* name) |
77 | 0 | { |
78 | 0 | wClipboardFormat* format = nullptr; |
79 | |
|
80 | 0 | if (!clipboard) |
81 | 0 | return nullptr; |
82 | | |
83 | 0 | if (formatId) |
84 | 0 | { |
85 | 0 | for (UINT32 index = 0; index < clipboard->numFormats; index++) |
86 | 0 | { |
87 | 0 | wClipboardFormat* cformat = &clipboard->formats[index]; |
88 | 0 | if (formatId == cformat->formatId) |
89 | 0 | { |
90 | 0 | format = cformat; |
91 | 0 | break; |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | 0 | else if (name) |
96 | 0 | { |
97 | 0 | for (UINT32 index = 0; index < clipboard->numFormats; index++) |
98 | 0 | { |
99 | 0 | wClipboardFormat* cformat = &clipboard->formats[index]; |
100 | 0 | if (!cformat->formatName) |
101 | 0 | continue; |
102 | | |
103 | 0 | if (strcmp(name, cformat->formatName) == 0) |
104 | 0 | { |
105 | 0 | format = cformat; |
106 | 0 | break; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | 0 | else |
111 | 0 | { |
112 | | /* special "CF_RAW" case */ |
113 | 0 | if (clipboard->numFormats > 0) |
114 | 0 | { |
115 | 0 | format = &clipboard->formats[0]; |
116 | |
|
117 | 0 | if (format->formatId) |
118 | 0 | return nullptr; |
119 | | |
120 | 0 | if (!format->formatName || (strcmp(format->formatName, CF_STANDARD_STRINGS[0]) == 0)) |
121 | 0 | return format; |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | 0 | return format; |
126 | 0 | } |
127 | | |
128 | | static wClipboardSynthesizer* ClipboardFindSynthesizer(wClipboardFormat* format, UINT32 formatId) |
129 | 0 | { |
130 | 0 | if (!format) |
131 | 0 | return nullptr; |
132 | | |
133 | 0 | for (UINT32 index = 0; index < format->numSynthesizers; index++) |
134 | 0 | { |
135 | 0 | wClipboardSynthesizer* synthesizer = &(format->synthesizers[index]); |
136 | |
|
137 | 0 | if (formatId == synthesizer->syntheticId) |
138 | 0 | return synthesizer; |
139 | 0 | } |
140 | | |
141 | 0 | return nullptr; |
142 | 0 | } |
143 | | |
144 | | void ClipboardLock(wClipboard* clipboard) |
145 | 0 | { |
146 | 0 | if (!clipboard) |
147 | 0 | return; |
148 | | |
149 | 0 | EnterCriticalSection(&(clipboard->lock)); |
150 | 0 | } |
151 | | |
152 | | void ClipboardUnlock(wClipboard* clipboard) |
153 | 0 | { |
154 | 0 | if (!clipboard) |
155 | 0 | return; |
156 | | |
157 | 0 | LeaveCriticalSection(&(clipboard->lock)); |
158 | 0 | } |
159 | | |
160 | | BOOL ClipboardEmpty(wClipboard* clipboard) |
161 | 0 | { |
162 | 0 | if (!clipboard) |
163 | 0 | return FALSE; |
164 | | |
165 | 0 | if (clipboard->data) |
166 | 0 | { |
167 | 0 | free(clipboard->data); |
168 | 0 | clipboard->data = nullptr; |
169 | 0 | } |
170 | |
|
171 | 0 | clipboard->size = 0; |
172 | 0 | clipboard->formatId = 0; |
173 | 0 | clipboard->sequenceNumber++; |
174 | 0 | return TRUE; |
175 | 0 | } |
176 | | |
177 | | UINT32 ClipboardCountRegisteredFormats(wClipboard* clipboard) |
178 | 0 | { |
179 | 0 | if (!clipboard) |
180 | 0 | return 0; |
181 | | |
182 | 0 | return clipboard->numFormats; |
183 | 0 | } |
184 | | |
185 | | UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, UINT32** ppFormatIds) |
186 | 0 | { |
187 | 0 | UINT32* pFormatIds = nullptr; |
188 | 0 | wClipboardFormat* format = nullptr; |
189 | |
|
190 | 0 | if (!clipboard) |
191 | 0 | return 0; |
192 | | |
193 | 0 | if (!ppFormatIds) |
194 | 0 | return 0; |
195 | | |
196 | 0 | pFormatIds = *ppFormatIds; |
197 | |
|
198 | 0 | if (!pFormatIds) |
199 | 0 | { |
200 | 0 | pFormatIds = calloc(clipboard->numFormats, sizeof(UINT32)); |
201 | |
|
202 | 0 | if (!pFormatIds) |
203 | 0 | return 0; |
204 | | |
205 | 0 | *ppFormatIds = pFormatIds; |
206 | 0 | } |
207 | | |
208 | 0 | for (UINT32 index = 0; index < clipboard->numFormats; index++) |
209 | 0 | { |
210 | 0 | format = &(clipboard->formats[index]); |
211 | 0 | pFormatIds[index] = format->formatId; |
212 | 0 | } |
213 | |
|
214 | 0 | return clipboard->numFormats; |
215 | 0 | } |
216 | | |
217 | | UINT32 ClipboardRegisterFormat(wClipboard* clipboard, const char* name) |
218 | 0 | { |
219 | 0 | wClipboardFormat* format = nullptr; |
220 | |
|
221 | 0 | if (!clipboard) |
222 | 0 | return 0; |
223 | | |
224 | 0 | format = ClipboardFindFormat(clipboard, 0, name); |
225 | |
|
226 | 0 | if (format) |
227 | 0 | return format->formatId; |
228 | | |
229 | 0 | if ((clipboard->numFormats + 1) >= clipboard->maxFormats) |
230 | 0 | { |
231 | 0 | UINT32 numFormats = clipboard->maxFormats * 2; |
232 | 0 | wClipboardFormat* tmpFormat = nullptr; |
233 | 0 | tmpFormat = |
234 | 0 | (wClipboardFormat*)realloc(clipboard->formats, numFormats * sizeof(wClipboardFormat)); |
235 | |
|
236 | 0 | if (!tmpFormat) |
237 | 0 | return 0; |
238 | | |
239 | 0 | clipboard->formats = tmpFormat; |
240 | 0 | clipboard->maxFormats = numFormats; |
241 | 0 | } |
242 | | |
243 | 0 | format = &(clipboard->formats[clipboard->numFormats]); |
244 | 0 | ZeroMemory(format, sizeof(wClipboardFormat)); |
245 | |
|
246 | 0 | if (name) |
247 | 0 | { |
248 | 0 | format->formatName = _strdup(name); |
249 | |
|
250 | 0 | if (!format->formatName) |
251 | 0 | return 0; |
252 | 0 | } |
253 | | |
254 | 0 | format->formatId = clipboard->nextFormatId++; |
255 | 0 | clipboard->numFormats++; |
256 | 0 | return format->formatId; |
257 | 0 | } |
258 | | |
259 | | WINPR_ATTR_NODISCARD |
260 | | static wClipboardSynthesizer* registerSynthesizer(wClipboard* clipboard, UINT32 formatId, |
261 | | UINT32 syntheticId) |
262 | 0 | { |
263 | 0 | if (!clipboard) |
264 | 0 | { |
265 | 0 | WLog_WARN(TAG, |
266 | 0 | "Failed to register synthesizer for %s[0x%04" PRIx32 "] to %s [0x%04" PRIx32 |
267 | 0 | "]: clipboard=NULL", |
268 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, |
269 | 0 | ClipboardGetFormatName(clipboard, syntheticId), syntheticId); |
270 | 0 | return nullptr; |
271 | 0 | } |
272 | | |
273 | 0 | WLog_DBG(TAG, "Registering synthesizer for %s[0x%04" PRIx32 "] to %s [0x%04" PRIx32 "]", |
274 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, |
275 | 0 | ClipboardGetFormatName(clipboard, syntheticId), syntheticId); |
276 | |
|
277 | 0 | wClipboardFormat* format = ClipboardFindFormat(clipboard, formatId, nullptr); |
278 | |
|
279 | 0 | if (!format) |
280 | 0 | { |
281 | 0 | WLog_WARN(TAG, |
282 | 0 | "Failed to register synthesizer for %s[0x%04" PRIx32 "] to %s [0x%04" PRIx32 |
283 | 0 | "]: format=NULL", |
284 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, |
285 | 0 | ClipboardGetFormatName(clipboard, syntheticId), syntheticId); |
286 | 0 | return nullptr; |
287 | 0 | } |
288 | | |
289 | 0 | if (format->formatId == syntheticId) |
290 | 0 | { |
291 | 0 | WLog_WARN(TAG, |
292 | 0 | "Failed to register synthesizer for %s[0x%04" PRIx32 "] to %s [0x%04" PRIx32 |
293 | 0 | "]: formats equal", |
294 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, |
295 | 0 | ClipboardGetFormatName(clipboard, syntheticId), syntheticId); |
296 | 0 | return nullptr; |
297 | 0 | } |
298 | | |
299 | 0 | wClipboardSynthesizer* synthesizer = ClipboardFindSynthesizer(format, formatId); |
300 | |
|
301 | 0 | if (!synthesizer) |
302 | 0 | { |
303 | 0 | UINT32 numSynthesizers = format->numSynthesizers + 1; |
304 | 0 | wClipboardSynthesizer* tmpSynthesizer = (wClipboardSynthesizer*)realloc( |
305 | 0 | format->synthesizers, numSynthesizers * sizeof(wClipboardSynthesizer)); |
306 | |
|
307 | 0 | if (!tmpSynthesizer) |
308 | 0 | { |
309 | 0 | WLog_WARN(TAG, |
310 | 0 | "Failed to register synthesizer for %s[0x%04" PRIx32 "] to %s [0x%04" PRIx32 |
311 | 0 | "]: failed to allocate new synthesizer", |
312 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, |
313 | 0 | ClipboardGetFormatName(clipboard, syntheticId), syntheticId); |
314 | 0 | return nullptr; |
315 | 0 | } |
316 | | |
317 | 0 | format->synthesizers = tmpSynthesizer; |
318 | 0 | format->numSynthesizers = numSynthesizers; |
319 | 0 | const UINT32 index = numSynthesizers - 1; |
320 | 0 | synthesizer = &(format->synthesizers[index]); |
321 | 0 | } |
322 | 0 | return synthesizer; |
323 | 0 | } |
324 | | |
325 | | BOOL ClipboardRegisterSynthesizer(wClipboard* clipboard, UINT32 formatId, UINT32 syntheticId, |
326 | | CLIPBOARD_SYNTHESIZE_FN pfnSynthesize) |
327 | 0 | { |
328 | 0 | wClipboardSynthesizer* synthesizer = registerSynthesizer(clipboard, formatId, syntheticId); |
329 | 0 | if (!synthesizer) |
330 | 0 | return FALSE; |
331 | | |
332 | 0 | synthesizer->syntheticId = syntheticId; |
333 | 0 | synthesizer->pfnSynthesize = pfnSynthesize; |
334 | 0 | return TRUE; |
335 | 0 | } |
336 | | |
337 | | BOOL ClipboardRegisterSynthesizerEx(wClipboard* clipboard, UINT32 formatId, UINT32 syntheticId, |
338 | | CLIPBOARD_SYNTHESIZE_FN pfnSynthesize) |
339 | 0 | { |
340 | 0 | wClipboardSynthesizer* synthesizer = registerSynthesizer(clipboard, formatId, syntheticId); |
341 | 0 | if (!synthesizer) |
342 | 0 | return FALSE; |
343 | | |
344 | 0 | synthesizer->syntheticId = syntheticId; |
345 | 0 | synthesizer->pfnSynthesizeEx = pfnSynthesize; |
346 | 0 | return TRUE; |
347 | 0 | } |
348 | | |
349 | | UINT32 ClipboardCountFormats(wClipboard* clipboard) |
350 | 0 | { |
351 | 0 | UINT32 count = 0; |
352 | 0 | wClipboardFormat* format = nullptr; |
353 | |
|
354 | 0 | if (!clipboard) |
355 | 0 | return 0; |
356 | | |
357 | 0 | format = ClipboardFindFormat(clipboard, clipboard->formatId, nullptr); |
358 | |
|
359 | 0 | if (!format) |
360 | 0 | return 0; |
361 | | |
362 | 0 | count = 1 + format->numSynthesizers; |
363 | 0 | return count; |
364 | 0 | } |
365 | | |
366 | | UINT32 ClipboardGetFormatIds(wClipboard* clipboard, UINT32** ppFormatIds) |
367 | 0 | { |
368 | 0 | UINT32 count = 0; |
369 | 0 | UINT32* pFormatIds = nullptr; |
370 | 0 | wClipboardFormat* format = nullptr; |
371 | 0 | wClipboardSynthesizer* synthesizer = nullptr; |
372 | |
|
373 | 0 | if (!clipboard) |
374 | 0 | return 0; |
375 | | |
376 | 0 | format = ClipboardFindFormat(clipboard, clipboard->formatId, nullptr); |
377 | |
|
378 | 0 | if (!format) |
379 | 0 | return 0; |
380 | | |
381 | 0 | count = 1 + format->numSynthesizers; |
382 | |
|
383 | 0 | if (!ppFormatIds) |
384 | 0 | return 0; |
385 | | |
386 | 0 | pFormatIds = *ppFormatIds; |
387 | |
|
388 | 0 | if (!pFormatIds) |
389 | 0 | { |
390 | 0 | pFormatIds = calloc(count, sizeof(UINT32)); |
391 | |
|
392 | 0 | if (!pFormatIds) |
393 | 0 | return 0; |
394 | | |
395 | 0 | *ppFormatIds = pFormatIds; |
396 | 0 | } |
397 | | |
398 | 0 | pFormatIds[0] = format->formatId; |
399 | |
|
400 | 0 | for (UINT32 index = 1; index < count; index++) |
401 | 0 | { |
402 | 0 | synthesizer = &(format->synthesizers[index - 1]); |
403 | 0 | pFormatIds[index] = synthesizer->syntheticId; |
404 | 0 | } |
405 | |
|
406 | 0 | return count; |
407 | 0 | } |
408 | | |
409 | | static void ClipboardUninitFormats(wClipboard* clipboard) |
410 | 0 | { |
411 | 0 | WINPR_ASSERT(clipboard); |
412 | 0 | for (UINT32 formatId = 0; formatId < clipboard->numFormats; formatId++) |
413 | 0 | { |
414 | 0 | wClipboardFormat* format = &clipboard->formats[formatId]; |
415 | 0 | free(format->formatName); |
416 | 0 | free(format->synthesizers); |
417 | 0 | format->formatName = nullptr; |
418 | 0 | format->synthesizers = nullptr; |
419 | 0 | } |
420 | 0 | } |
421 | | |
422 | | static BOOL ClipboardInitFormats(wClipboard* clipboard) |
423 | 0 | { |
424 | 0 | UINT32 formatId = 0; |
425 | 0 | wClipboardFormat* format = nullptr; |
426 | |
|
427 | 0 | if (!clipboard) |
428 | 0 | return FALSE; |
429 | | |
430 | 0 | for (formatId = 0; formatId < CF_MAX; formatId++, clipboard->numFormats++) |
431 | 0 | { |
432 | 0 | format = &(clipboard->formats[clipboard->numFormats]); |
433 | 0 | ZeroMemory(format, sizeof(wClipboardFormat)); |
434 | 0 | format->formatId = formatId; |
435 | 0 | format->formatName = _strdup(CF_STANDARD_STRINGS[formatId]); |
436 | |
|
437 | 0 | if (!format->formatName) |
438 | 0 | goto error; |
439 | 0 | } |
440 | | |
441 | 0 | if (!ClipboardInitSynthesizers(clipboard)) |
442 | 0 | goto error; |
443 | | |
444 | 0 | return TRUE; |
445 | 0 | error: |
446 | |
|
447 | 0 | ClipboardUninitFormats(clipboard); |
448 | 0 | return FALSE; |
449 | 0 | } |
450 | | |
451 | | UINT32 ClipboardGetFormatId(wClipboard* clipboard, const char* name) |
452 | 0 | { |
453 | 0 | wClipboardFormat* format = nullptr; |
454 | |
|
455 | 0 | if (!clipboard) |
456 | 0 | return 0; |
457 | | |
458 | 0 | format = ClipboardFindFormat(clipboard, 0, name); |
459 | |
|
460 | 0 | if (!format) |
461 | 0 | return 0; |
462 | | |
463 | 0 | return format->formatId; |
464 | 0 | } |
465 | | |
466 | | const char* ClipboardGetFormatName(wClipboard* clipboard, UINT32 formatId) |
467 | 0 | { |
468 | 0 | wClipboardFormat* format = nullptr; |
469 | |
|
470 | 0 | if (!clipboard) |
471 | 0 | return nullptr; |
472 | | |
473 | 0 | format = ClipboardFindFormat(clipboard, formatId, nullptr); |
474 | |
|
475 | 0 | if (!format) |
476 | 0 | return nullptr; |
477 | | |
478 | 0 | return format->formatName; |
479 | 0 | } |
480 | | |
481 | | void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize) |
482 | 0 | { |
483 | 0 | void* pDstData = nullptr; |
484 | |
|
485 | 0 | if (!clipboard || !pSize) |
486 | 0 | { |
487 | 0 | WLog_ERR(TAG, "Invalid parameters clipboard=%p, pSize=%p", |
488 | 0 | WINPR_CXX_COMPAT_CAST(const void*, clipboard), |
489 | 0 | WINPR_CXX_COMPAT_CAST(const void*, pSize)); |
490 | 0 | return nullptr; |
491 | 0 | } |
492 | | |
493 | 0 | *pSize = 0; |
494 | 0 | wClipboardFormat* format = ClipboardFindFormat(clipboard, clipboard->formatId, nullptr); |
495 | |
|
496 | 0 | if (!format) |
497 | 0 | { |
498 | 0 | WLog_ERR(TAG, "Format [0x%08" PRIx32 "] not found", clipboard->formatId); |
499 | 0 | return nullptr; |
500 | 0 | } |
501 | | |
502 | 0 | const UINT32 SrcSize = clipboard->size; |
503 | 0 | void* pSrcData = clipboard->data; |
504 | |
|
505 | 0 | if (formatId == format->formatId) |
506 | 0 | { |
507 | 0 | UINT32 DstSize = SrcSize; |
508 | 0 | pDstData = malloc(DstSize); |
509 | |
|
510 | 0 | if (!pDstData) |
511 | 0 | return nullptr; |
512 | | |
513 | 0 | CopyMemory(pDstData, pSrcData, SrcSize); |
514 | 0 | *pSize = DstSize; |
515 | 0 | } |
516 | 0 | else |
517 | 0 | { |
518 | 0 | wClipboardSynthesizer* synthesizer = ClipboardFindSynthesizer(format, formatId); |
519 | |
|
520 | 0 | if (!synthesizer || !(synthesizer->pfnSynthesize || synthesizer->pfnSynthesizeEx)) |
521 | 0 | { |
522 | 0 | WLog_ERR(TAG, "No synthesizer for format %s [0x%08" PRIx32 "] --> %s [0x%08" PRIx32 "]", |
523 | 0 | ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId, |
524 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId); |
525 | 0 | return nullptr; |
526 | 0 | } |
527 | | |
528 | 0 | UINT32 DstSize = SrcSize; |
529 | 0 | if (synthesizer->pfnSynthesizeEx) |
530 | 0 | pDstData = synthesizer->pfnSynthesizeEx(clipboard, synthesizer->syntheticId, pSrcData, |
531 | 0 | &DstSize); |
532 | 0 | else |
533 | 0 | pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, &DstSize); |
534 | 0 | if (pDstData) |
535 | 0 | *pSize = DstSize; |
536 | 0 | } |
537 | | |
538 | 0 | WLog_DBG(TAG, "getting formatId=%s [0x%08" PRIx32 "] data=%p, size=%" PRIu32, |
539 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, pDstData, *pSize); |
540 | 0 | return pDstData; |
541 | 0 | } |
542 | | |
543 | | BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId, const void* data, UINT32 size) |
544 | 0 | { |
545 | 0 | wClipboardFormat* format = nullptr; |
546 | |
|
547 | 0 | WLog_DBG(TAG, "setting formatId=%s [0x%08" PRIx32 "], size=%" PRIu32, |
548 | 0 | ClipboardGetFormatName(clipboard, formatId), formatId, size); |
549 | 0 | if (!clipboard) |
550 | 0 | return FALSE; |
551 | | |
552 | 0 | format = ClipboardFindFormat(clipboard, formatId, nullptr); |
553 | |
|
554 | 0 | if (!format) |
555 | 0 | return FALSE; |
556 | | |
557 | 0 | free(clipboard->data); |
558 | | |
559 | | /* append 2 WCHAR zero bytes. Prevent problems with odd input data sizes. */ |
560 | 0 | clipboard->data = calloc(size + 2ull * sizeof(WCHAR), sizeof(char)); |
561 | |
|
562 | 0 | if (!clipboard->data) |
563 | 0 | return FALSE; |
564 | | |
565 | 0 | memcpy(clipboard->data, data, size); |
566 | | |
567 | | /* For string values we don“t know if they are '\0' terminated. |
568 | | * so set the size to the full length in bytes (e.g. string length + 1) |
569 | | */ |
570 | 0 | switch (formatId) |
571 | 0 | { |
572 | 0 | case CF_TEXT: |
573 | 0 | case CF_OEMTEXT: |
574 | 0 | clipboard->size = (UINT32)(strnlen(clipboard->data, size) + 1UL); |
575 | 0 | break; |
576 | 0 | case CF_UNICODETEXT: |
577 | 0 | clipboard->size = |
578 | 0 | (UINT32)((_wcsnlen(clipboard->data, size / sizeof(WCHAR)) + 1UL) * sizeof(WCHAR)); |
579 | 0 | break; |
580 | 0 | default: |
581 | 0 | clipboard->size = size; |
582 | 0 | break; |
583 | 0 | } |
584 | | |
585 | 0 | clipboard->formatId = formatId; |
586 | 0 | clipboard->sequenceNumber++; |
587 | 0 | return TRUE; |
588 | 0 | } |
589 | | |
590 | | UINT64 ClipboardGetOwner(wClipboard* clipboard) |
591 | 0 | { |
592 | 0 | if (!clipboard) |
593 | 0 | return 0; |
594 | | |
595 | 0 | return clipboard->ownerId; |
596 | 0 | } |
597 | | |
598 | | void ClipboardSetOwner(wClipboard* clipboard, UINT64 ownerId) |
599 | 0 | { |
600 | 0 | if (!clipboard) |
601 | 0 | return; |
602 | | |
603 | 0 | clipboard->ownerId = ownerId; |
604 | 0 | } |
605 | | |
606 | | wClipboardDelegate* ClipboardGetDelegate(wClipboard* clipboard) |
607 | 0 | { |
608 | 0 | if (!clipboard) |
609 | 0 | return nullptr; |
610 | | |
611 | 0 | return &clipboard->delegate; |
612 | 0 | } |
613 | | |
614 | | static void ClipboardInitLocalFileSubsystem(wClipboard* clipboard) |
615 | 0 | { |
616 | | /* |
617 | | * There can be only one local file subsystem active. |
618 | | * Return as soon as initialization succeeds. |
619 | | */ |
620 | 0 | if (ClipboardInitSyntheticFileSubsystem(clipboard)) |
621 | 0 | { |
622 | 0 | WLog_DBG(TAG, "initialized synthetic local file subsystem"); |
623 | 0 | return; |
624 | 0 | } |
625 | 0 | else |
626 | 0 | { |
627 | 0 | WLog_WARN(TAG, "failed to initialize synthetic local file subsystem"); |
628 | 0 | } |
629 | | |
630 | 0 | WLog_INFO(TAG, "failed to initialize local file subsystem, file transfer not available"); |
631 | 0 | } |
632 | | |
633 | | wClipboard* ClipboardCreate(void) |
634 | 0 | { |
635 | 0 | wClipboard* clipboard = (wClipboard*)calloc(1, sizeof(wClipboard)); |
636 | |
|
637 | 0 | if (!clipboard) |
638 | 0 | return nullptr; |
639 | | |
640 | 0 | clipboard->nextFormatId = 0xC000; |
641 | 0 | clipboard->sequenceNumber = 0; |
642 | |
|
643 | 0 | if (!InitializeCriticalSectionAndSpinCount(&(clipboard->lock), 4000)) |
644 | 0 | goto fail; |
645 | | |
646 | 0 | clipboard->numFormats = 0; |
647 | 0 | clipboard->maxFormats = 64; |
648 | 0 | clipboard->formats = (wClipboardFormat*)calloc(clipboard->maxFormats, sizeof(wClipboardFormat)); |
649 | |
|
650 | 0 | if (!clipboard->formats) |
651 | 0 | goto fail; |
652 | | |
653 | 0 | if (!ClipboardInitFormats(clipboard)) |
654 | 0 | goto fail; |
655 | | |
656 | 0 | clipboard->delegate.clipboard = clipboard; |
657 | 0 | ClipboardInitLocalFileSubsystem(clipboard); |
658 | 0 | return clipboard; |
659 | 0 | fail: |
660 | 0 | ClipboardDestroy(clipboard); |
661 | 0 | return nullptr; |
662 | 0 | } |
663 | | |
664 | | void ClipboardDestroy(wClipboard* clipboard) |
665 | 0 | { |
666 | 0 | if (!clipboard) |
667 | 0 | return; |
668 | | |
669 | 0 | ArrayList_Free(clipboard->localFiles); |
670 | 0 | clipboard->localFiles = nullptr; |
671 | |
|
672 | 0 | ClipboardUninitFormats(clipboard); |
673 | |
|
674 | 0 | free(clipboard->data); |
675 | 0 | clipboard->data = nullptr; |
676 | 0 | clipboard->size = 0; |
677 | 0 | clipboard->numFormats = 0; |
678 | 0 | free(clipboard->formats); |
679 | 0 | DeleteCriticalSection(&(clipboard->lock)); |
680 | 0 | free(clipboard); |
681 | 0 | } |
682 | | |
683 | | static BOOL is_dos_drive(const char* path, size_t len) |
684 | 0 | { |
685 | 0 | if (len < 2) |
686 | 0 | return FALSE; |
687 | | |
688 | 0 | WINPR_ASSERT(path); |
689 | 0 | if (path[1] == ':' || path[1] == '|') |
690 | 0 | { |
691 | 0 | if (((path[0] >= 'A') && (path[0] <= 'Z')) || ((path[0] >= 'a') && (path[0] <= 'z'))) |
692 | 0 | return TRUE; |
693 | 0 | } |
694 | 0 | return FALSE; |
695 | 0 | } |
696 | | |
697 | | char* parse_uri_to_local_file(const char* uri, size_t uri_len) |
698 | 0 | { |
699 | | // URI is specified by RFC 8089: https://datatracker.ietf.org/doc/html/rfc8089 |
700 | 0 | const char prefix[] = "file:"; |
701 | 0 | const char prefixTraditional[] = "file://"; |
702 | 0 | const char* localName = nullptr; |
703 | 0 | size_t localLen = 0; |
704 | 0 | char* buffer = nullptr; |
705 | 0 | const size_t prefixLen = strnlen(prefix, sizeof(prefix)); |
706 | 0 | const size_t prefixTraditionalLen = strnlen(prefixTraditional, sizeof(prefixTraditional)); |
707 | |
|
708 | 0 | WINPR_ASSERT(uri || (uri_len == 0)); |
709 | |
|
710 | 0 | WLog_VRB(TAG, "processing URI: %.*s", WINPR_ASSERTING_INT_CAST(int, uri_len), uri); |
711 | |
|
712 | 0 | if ((uri_len <= prefixLen) || strncmp(uri, prefix, prefixLen) != 0) |
713 | 0 | { |
714 | 0 | WLog_ERR(TAG, "non-'file:' URI schemes are not supported"); |
715 | 0 | return nullptr; |
716 | 0 | } |
717 | | |
718 | 0 | do |
719 | 0 | { |
720 | | /* https://datatracker.ietf.org/doc/html/rfc8089#appendix-F |
721 | | * - The minimal representation of a local file in a DOS- or Windows- |
722 | | * based environment with no authority field and an absolute path |
723 | | * that begins with a drive letter. |
724 | | * |
725 | | * "file:c:/path/to/file" |
726 | | * |
727 | | * - Regular DOS or Windows file URIs with vertical line characters in |
728 | | * the drive letter construct. |
729 | | * |
730 | | * "file:c|/path/to/file" |
731 | | * |
732 | | */ |
733 | 0 | if (uri[prefixLen] != '/') |
734 | 0 | { |
735 | |
|
736 | 0 | if (is_dos_drive(&uri[prefixLen], uri_len - prefixLen)) |
737 | 0 | { |
738 | | // Dos and Windows file URI |
739 | 0 | localName = &uri[prefixLen]; |
740 | 0 | localLen = uri_len - prefixLen; |
741 | 0 | break; |
742 | 0 | } |
743 | 0 | else |
744 | 0 | { |
745 | 0 | WLog_ERR(TAG, "URI format are not supported: %s", uri); |
746 | 0 | return nullptr; |
747 | 0 | } |
748 | 0 | } |
749 | | |
750 | | /* |
751 | | * - The minimal representation of a local file with no authority field |
752 | | * and an absolute path that begins with a slash "/". For example: |
753 | | * |
754 | | * "file:/path/to/file" |
755 | | * |
756 | | */ |
757 | 0 | else if ((uri_len > prefixLen + 1) && (uri[prefixLen + 1] != '/')) |
758 | 0 | { |
759 | 0 | if (is_dos_drive(&uri[prefixLen + 1], uri_len - prefixLen - 1)) |
760 | 0 | { |
761 | | // Dos and Windows file URI |
762 | 0 | localName = (uri + prefixLen + 1); |
763 | 0 | localLen = uri_len - prefixLen - 1; |
764 | 0 | } |
765 | 0 | else |
766 | 0 | { |
767 | 0 | localName = &uri[prefixLen]; |
768 | 0 | localLen = uri_len - prefixLen; |
769 | 0 | } |
770 | 0 | break; |
771 | 0 | } |
772 | | |
773 | | /* |
774 | | * - A traditional file URI for a local file with an empty authority. |
775 | | * |
776 | | * "file:///path/to/file" |
777 | | */ |
778 | 0 | if ((uri_len < prefixTraditionalLen) || |
779 | 0 | strncmp(uri, prefixTraditional, prefixTraditionalLen) != 0) |
780 | 0 | { |
781 | 0 | WLog_ERR(TAG, "non-'file:' URI schemes are not supported"); |
782 | 0 | return nullptr; |
783 | 0 | } |
784 | | |
785 | 0 | localName = &uri[prefixTraditionalLen]; |
786 | 0 | localLen = uri_len - prefixTraditionalLen; |
787 | |
|
788 | 0 | if (localLen < 1) |
789 | 0 | { |
790 | 0 | WLog_ERR(TAG, "empty 'file:' URI schemes are not supported"); |
791 | 0 | return nullptr; |
792 | 0 | } |
793 | | |
794 | | /* |
795 | | * "file:///c:/path/to/file" |
796 | | * "file:///c|/path/to/file" |
797 | | */ |
798 | 0 | if (localName[0] != '/') |
799 | 0 | { |
800 | 0 | WLog_ERR(TAG, "URI format are not supported: %s", uri); |
801 | 0 | return nullptr; |
802 | 0 | } |
803 | | |
804 | 0 | if (is_dos_drive(&localName[1], localLen - 1)) |
805 | 0 | { |
806 | 0 | localName++; |
807 | 0 | localLen--; |
808 | 0 | } |
809 | |
|
810 | 0 | } while (0); |
811 | | |
812 | 0 | buffer = winpr_str_url_decode(localName, localLen); |
813 | 0 | if (buffer) |
814 | 0 | { |
815 | 0 | if (buffer[1] == '|' && |
816 | 0 | ((buffer[0] >= 'A' && buffer[0] <= 'Z') || (buffer[0] >= 'a' && buffer[0] <= 'z'))) |
817 | 0 | buffer[1] = ':'; |
818 | 0 | return buffer; |
819 | 0 | } |
820 | | |
821 | 0 | return nullptr; |
822 | 0 | } |