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