Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/clipboard/synthetic.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 <errno.h>
23
#include <winpr/crt.h>
24
#include <winpr/user.h>
25
#include <winpr/image.h>
26
27
#include "../utils/image.h"
28
#include "clipboard.h"
29
30
#include "../log.h"
31
#define TAG WINPR_TAG("clipboard.synthetic")
32
33
static const char mime_html[] = "text/html";
34
static const char mime_ms_html[] = "HTML Format";
35
static const char* mime_bitmap[] = { "image/bmp", "image/x-bmp", "image/x-MS-bmp",
36
                                   "image/x-win-bitmap" };
37
38
static const char mime_webp[] = "image/webp";
39
static const char mime_png[] = "image/png";
40
static const char mime_jpeg[] = "image/jpeg";
41
static const char mime_tiff[] = "image/tiff";
42
43
static const BYTE enc_base64url[] =
44
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45
46
WINPR_ATTR_NODISCARD static inline char* b64_encode(const BYTE* WINPR_RESTRICT data, size_t length,
47
                                                    size_t* plen)
48
240
{
49
240
  WINPR_ASSERT(plen);
50
240
  const BYTE* WINPR_RESTRICT alphabet = enc_base64url;
51
240
  int c = 0;
52
240
  size_t blocks = 0;
53
240
  size_t outLen = (length + 3) * 4 / 3;
54
240
  size_t extra = 0;
55
56
240
  const BYTE* q = data;
57
240
  const size_t alen = outLen + extra + 1ull;
58
240
  BYTE* p = malloc(alen);
59
240
  if (!p)
60
0
    return nullptr;
61
62
240
  BYTE* ret = p;
63
64
  /* b1, b2, b3 are input bytes
65
   *
66
   * 0         1         2
67
   * 012345678901234567890123
68
   * |  b1  |  b2   |  b3   |
69
   *
70
   * [ c1 ]     [  c3 ]
71
   *      [  c2 ]     [  c4 ]
72
   *
73
   * c1, c2, c3, c4 are output chars in base64
74
   */
75
76
  /* first treat complete blocks */
77
240
  blocks = length - (length % 3);
78
825k
  for (size_t i = 0; i < blocks; i += 3, q += 3)
79
824k
  {
80
824k
    c = (q[0] << 16) + (q[1] << 8) + q[2];
81
82
824k
    *p++ = alphabet[(c & 0x00FC0000) >> 18];
83
824k
    *p++ = alphabet[(c & 0x0003F000) >> 12];
84
824k
    *p++ = alphabet[(c & 0x00000FC0) >> 6];
85
824k
    *p++ = alphabet[c & 0x0000003F];
86
824k
  }
87
88
  /* then remainder */
89
240
  switch (length % 3)
90
240
  {
91
78
    case 0:
92
78
      break;
93
71
    case 1:
94
71
      c = (q[0] << 16);
95
71
      *p++ = alphabet[(c & 0x00FC0000) >> 18];
96
71
      *p++ = alphabet[(c & 0x0003F000) >> 12];
97
71
      break;
98
91
    case 2:
99
91
      c = (q[0] << 16) + (q[1] << 8);
100
91
      *p++ = alphabet[(c & 0x00FC0000) >> 18];
101
91
      *p++ = alphabet[(c & 0x0003F000) >> 12];
102
91
      *p++ = alphabet[(c & 0x00000FC0) >> 6];
103
91
      break;
104
0
    default:
105
0
      break;
106
240
  }
107
108
240
  *p = 0;
109
240
  *plen = WINPR_ASSERTING_INT_CAST(size_t, p - ret);
110
111
240
  return (char*)ret;
112
240
}
113
114
/**
115
 * Standard Clipboard Formats:
116
 * http://msdn.microsoft.com/en-us/library/windows/desktop/ff729168/
117
 */
118
119
/**
120
 * "CF_TEXT":
121
 *
122
 * Null-terminated ANSI text with CR/LF line endings.
123
 */
124
125
static void* clipboard_synthesize_cf_text(wClipboard* clipboard, UINT32 formatId, const void* data,
126
                                          UINT32* pSize)
127
618
{
128
618
  size_t size = 0;
129
618
  char* pDstData = nullptr;
130
131
618
  if (formatId == CF_UNICODETEXT)
132
362
  {
133
362
    char* str = ConvertWCharNToUtf8Alloc(data, *pSize / sizeof(WCHAR), &size);
134
135
362
    if (!str || (size > UINT32_MAX))
136
90
    {
137
90
      free(str);
138
90
      return nullptr;
139
90
    }
140
141
272
    pDstData = ConvertLineEndingToCRLF(str, &size);
142
272
    free(str);
143
272
    *pSize = (UINT32)size;
144
272
    return pDstData;
145
362
  }
146
256
  else if ((formatId == CF_TEXT) || (formatId == CF_OEMTEXT) ||
147
0
           (formatId == ClipboardGetFormatId(clipboard, mime_text_plain)))
148
256
  {
149
256
    size = *pSize;
150
256
    pDstData = ConvertLineEndingToCRLF(data, &size);
151
152
256
    if (!pDstData || (size > *pSize))
153
108
    {
154
108
      free(pDstData);
155
108
      return nullptr;
156
108
    }
157
158
148
    *pSize = (UINT32)size;
159
148
    return pDstData;
160
256
  }
161
162
0
  return nullptr;
163
618
}
164
165
/**
166
 * "CF_OEMTEXT":
167
 *
168
 * Null-terminated OEM text with CR/LF line endings.
169
 */
170
171
static void* clipboard_synthesize_cf_oemtext(wClipboard* clipboard, UINT32 formatId,
172
                                             const void* data, UINT32* pSize)
173
274
{
174
274
  return clipboard_synthesize_cf_text(clipboard, formatId, data, pSize);
175
274
}
176
177
/**
178
 * "CF_LOCALE":
179
 *
180
 * System locale identifier associated with CF_TEXT
181
 */
182
183
static void* clipboard_synthesize_cf_locale(WINPR_ATTR_UNUSED wClipboard* clipboard,
184
                                            WINPR_ATTR_UNUSED UINT32 formatId,
185
                                            WINPR_ATTR_UNUSED const void* data,
186
                                            WINPR_ATTR_UNUSED UINT32* pSize)
187
437
{
188
437
  UINT32* pDstData = nullptr;
189
437
  pDstData = (UINT32*)malloc(sizeof(UINT32));
190
191
437
  if (!pDstData)
192
0
    return nullptr;
193
194
437
  *pDstData = 0x0409; /* English - United States */
195
437
  return (void*)pDstData;
196
437
}
197
198
/**
199
 * "CF_UNICODETEXT":
200
 *
201
 * Null-terminated UTF-16 text with CR/LF line endings.
202
 */
203
204
static void* clipboard_synthesize_cf_unicodetext(wClipboard* clipboard, UINT32 formatId,
205
                                                 const void* data, UINT32* pSize)
206
256
{
207
256
  size_t size = 0;
208
256
  char* crlfStr = nullptr;
209
256
  WCHAR* pDstData = nullptr;
210
211
256
  if ((formatId == CF_TEXT) || (formatId == CF_OEMTEXT) ||
212
0
      (formatId == ClipboardGetFormatId(clipboard, mime_text_plain)))
213
256
  {
214
256
    size_t len = 0;
215
256
    if (!pSize || (*pSize > INT32_MAX))
216
0
      return nullptr;
217
218
256
    size = *pSize;
219
256
    crlfStr = ConvertLineEndingToCRLF((const char*)data, &size);
220
221
256
    if (!crlfStr)
222
0
      return nullptr;
223
224
256
    pDstData = ConvertUtf8NToWCharAlloc(crlfStr, size, &len);
225
256
    free(crlfStr);
226
227
256
    if ((len < 1) || ((len + 1) > UINT32_MAX / sizeof(WCHAR)))
228
122
    {
229
122
      free(pDstData);
230
122
      return nullptr;
231
122
    }
232
233
134
    const size_t slen = (len + 1) * sizeof(WCHAR);
234
134
    *pSize = (UINT32)slen;
235
134
  }
236
237
134
  return (void*)pDstData;
238
256
}
239
240
/**
241
 * mime_utf8_string:
242
 *
243
 * Null-terminated UTF-8 string with LF line endings.
244
 */
245
246
static void* clipboard_synthesize_utf8_string(wClipboard* clipboard, UINT32 formatId,
247
                                              const void* data, UINT32* pSize)
248
437
{
249
437
  if (formatId == CF_UNICODETEXT)
250
181
  {
251
181
    size_t size = 0;
252
181
    char* pDstData = ConvertWCharNToUtf8Alloc(data, *pSize / sizeof(WCHAR), &size);
253
254
181
    if (!pDstData)
255
45
      return nullptr;
256
257
136
    const size_t rc = ConvertLineEndingToLF(pDstData, size);
258
136
    WINPR_ASSERT(rc <= UINT32_MAX);
259
136
    *pSize = (UINT32)rc;
260
136
    return pDstData;
261
181
  }
262
256
  else if ((formatId == CF_TEXT) || (formatId == CF_OEMTEXT) ||
263
0
           (formatId == ClipboardGetFormatId(clipboard, mime_text_plain)))
264
256
  {
265
256
    const size_t size = *pSize;
266
256
    char* pDstData = calloc(size + 1, sizeof(char));
267
268
256
    if (!pDstData)
269
0
      return nullptr;
270
271
256
    CopyMemory(pDstData, data, size);
272
256
    const size_t rc = ConvertLineEndingToLF(pDstData, size);
273
256
    WINPR_ASSERT(rc <= UINT32_MAX);
274
256
    *pSize = (UINT32)rc;
275
256
    return pDstData;
276
256
  }
277
278
0
  return nullptr;
279
437
}
280
281
static BOOL is_format_bitmap(wClipboard* clipboard, UINT32 formatId)
282
154
{
283
154
  for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
284
154
  {
285
154
    const char* mime = mime_bitmap[x];
286
154
    const UINT32 altFormatId = ClipboardGetFormatId(clipboard, mime);
287
154
    if (altFormatId == formatId)
288
154
      return TRUE;
289
154
  }
290
291
0
  return FALSE;
292
154
}
293
294
/**
295
 * "CF_DIB":
296
 *
297
 * BITMAPINFO structure followed by the bitmap bits.
298
 */
299
300
static void* clipboard_synthesize_cf_dib(wClipboard* clipboard, UINT32 formatId, const void* data,
301
                                         UINT32* pSize)
302
154
{
303
154
  UINT32 SrcSize = 0;
304
154
  UINT32 DstSize = 0;
305
154
  BYTE* pDstData = nullptr;
306
154
  SrcSize = *pSize;
307
308
#if defined(WINPR_UTILS_IMAGE_DIBv5)
309
  if (formatId == CF_DIBV5)
310
  {
311
    WLog_WARN(TAG, "[DIB] Unsupported destination format %s",
312
              ClipboardGetFormatName(clipboard, formatId));
313
  }
314
  else
315
#endif
316
154
      if (is_format_bitmap(clipboard, formatId))
317
154
  {
318
154
    WINPR_BITMAP_FILE_HEADER pFileHeader = WINPR_C_ARRAY_INIT;
319
154
    wStream sbuffer = WINPR_C_ARRAY_INIT;
320
154
    wStream* s = Stream_StaticConstInit(&sbuffer, data, SrcSize);
321
154
    if (!readBitmapFileHeader(s, &pFileHeader))
322
142
      return nullptr;
323
324
12
    DstSize = SrcSize - sizeof(BITMAPFILEHEADER);
325
12
    pDstData = (BYTE*)malloc(DstSize);
326
327
12
    if (!pDstData)
328
0
      return nullptr;
329
330
12
    data = (const void*)&((const BYTE*)data)[sizeof(BITMAPFILEHEADER)];
331
12
    CopyMemory(pDstData, data, DstSize);
332
12
    *pSize = DstSize;
333
12
    return pDstData;
334
12
  }
335
0
  else
336
0
  {
337
0
    WLog_WARN(TAG, "[DIB] Unsupported destination format %s",
338
0
              ClipboardGetFormatName(clipboard, formatId));
339
0
  }
340
341
0
  return nullptr;
342
154
}
343
344
/**
345
 * "CF_DIBV5":
346
 *
347
 * BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits.
348
 */
349
#if defined(WINPR_UTILS_IMAGE_DIBv5)
350
static void* clipboard_synthesize_cf_dibv5(wClipboard* clipboard, UINT32 formatId,
351
                                           WINPR_ATTR_UNUSED const void* data,
352
                                           WINPR_ATTR_UNUSED UINT32* pSize)
353
{
354
  if (formatId == CF_DIB)
355
  {
356
    WLog_WARN(TAG, "[DIBv5] Unsupported destination format %s",
357
              ClipboardGetFormatName(clipboard, formatId));
358
  }
359
  else if (is_format_bitmap(clipboard, formatId))
360
  {
361
    WLog_WARN(TAG, "[DIBv5] Unsupported destination format %s",
362
              ClipboardGetFormatName(clipboard, formatId));
363
  }
364
  else
365
  {
366
    BOOL handled = FALSE;
367
#if defined(WINPR_UTILS_IMAGE_PNG)
368
    {
369
      const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_png);
370
      if (formatId == altFormatId)
371
      {
372
      }
373
    }
374
#endif
375
#if defined(WINPR_UTILS_IMAGE_JPEG)
376
    {
377
      const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_jpeg);
378
      if (formatId == altFormatId)
379
      {
380
      }
381
    }
382
#endif
383
    if (!handled)
384
    {
385
      WLog_WARN(TAG, "[DIBv5] Unsupported destination format %s",
386
                ClipboardGetFormatName(clipboard, formatId));
387
    }
388
  }
389
390
  return nullptr;
391
}
392
#endif
393
394
static void* clipboard_prepend_bmp_header(const WINPR_BITMAP_INFO_HEADER* pInfoHeader,
395
                                          const void* data, size_t size, UINT32* pSize)
396
430
{
397
430
  WINPR_ASSERT(pInfoHeader);
398
430
  WINPR_ASSERT(pSize);
399
400
430
  *pSize = 0;
401
430
  if ((pInfoHeader->biBitCount < 1) || (pInfoHeader->biBitCount > 32))
402
0
    return nullptr;
403
404
430
  const size_t DstSize = sizeof(WINPR_BITMAP_FILE_HEADER) + size;
405
430
  if (DstSize > UINT32_MAX)
406
0
    return nullptr;
407
408
430
  wStream* s = Stream_New(nullptr, DstSize);
409
430
  if (!s)
410
0
    return nullptr;
411
412
430
  WINPR_BITMAP_FILE_HEADER fileHeader = WINPR_C_ARRAY_INIT;
413
430
  fileHeader.bfType[0] = 'B';
414
430
  fileHeader.bfType[1] = 'M';
415
430
  fileHeader.bfSize = (UINT32)DstSize;
416
430
  fileHeader.bfOffBits = sizeof(WINPR_BITMAP_FILE_HEADER) + sizeof(WINPR_BITMAP_INFO_HEADER);
417
430
  if (!writeBitmapFileHeader(s, &fileHeader))
418
0
    goto fail;
419
420
430
  if (!Stream_EnsureRemainingCapacity(s, size))
421
0
    goto fail;
422
430
  Stream_Write(s, data, size);
423
424
430
  {
425
430
    const size_t len = Stream_GetPosition(s);
426
430
    if (len != DstSize)
427
0
      goto fail;
428
430
  }
429
430
430
  *pSize = (UINT32)DstSize;
431
432
430
  {
433
430
    BYTE* dst = Stream_Buffer(s);
434
430
    Stream_Free(s, FALSE);
435
430
    return dst;
436
430
  }
437
438
0
fail:
439
0
  Stream_Free(s, TRUE);
440
0
  return nullptr;
441
430
}
442
443
/**
444
 * "image/bmp":
445
 *
446
 * Bitmap file format.
447
 */
448
449
static void* clipboard_synthesize_image_bmp(WINPR_ATTR_UNUSED wClipboard* clipboard,
450
                                            UINT32 formatId, const void* data, UINT32* pSize)
451
1.36k
{
452
1.36k
  UINT32 SrcSize = *pSize;
453
454
1.36k
  if (formatId == CF_DIB)
455
1.36k
  {
456
1.36k
    if (SrcSize < sizeof(BITMAPINFOHEADER))
457
45
      return nullptr;
458
459
1.31k
    wStream sbuffer = WINPR_C_ARRAY_INIT;
460
1.31k
    size_t offset = 0;
461
1.31k
    WINPR_BITMAP_INFO_HEADER header = WINPR_C_ARRAY_INIT;
462
1.31k
    wStream* s = Stream_StaticConstInit(&sbuffer, data, SrcSize);
463
1.31k
    if (!readBitmapInfoHeader(s, &header, &offset))
464
885
      return nullptr;
465
466
430
    return clipboard_prepend_bmp_header(&header, data, SrcSize, pSize);
467
1.31k
  }
468
#if defined(WINPR_UTILS_IMAGE_DIBv5)
469
  else if (formatId == CF_DIBV5)
470
  {
471
    WLog_WARN(TAG, "[BMP] Unsupported destination format %s",
472
              ClipboardGetFormatName(clipboard, formatId));
473
  }
474
#endif
475
0
  else
476
0
  {
477
0
    WLog_WARN(TAG, "[BMP] Unsupported destination format %s",
478
0
              ClipboardGetFormatName(clipboard, formatId));
479
0
  }
480
481
0
  return nullptr;
482
1.36k
}
483
484
#if defined(WINPR_UTILS_IMAGE_PNG) || defined(WINPR_UTILS_IMAGE_WEBP) || \
485
    defined(WINPR_UTILS_IMAGE_JPEG)
486
static void* clipboard_synthesize_image_bmp_to_format(wClipboard* clipboard, UINT32 formatId,
487
                                                      UINT32 bmpFormat, const void* data,
488
                                                      UINT32* pSize)
489
{
490
  WINPR_ASSERT(clipboard);
491
  WINPR_ASSERT(data);
492
  WINPR_ASSERT(pSize);
493
494
  size_t dsize = 0;
495
  void* result = nullptr;
496
497
  wImage* img = winpr_image_new();
498
  void* bmp = clipboard_synthesize_image_bmp(clipboard, formatId, data, pSize);
499
  const UINT32 SrcSize = *pSize;
500
  *pSize = 0;
501
502
  if (!bmp || !img)
503
    goto fail;
504
505
  if (winpr_image_read_buffer(img, bmp, SrcSize) <= 0)
506
    goto fail;
507
508
  result = winpr_image_write_buffer(img, bmpFormat, &dsize);
509
  if (result)
510
  {
511
    if (dsize <= UINT32_MAX)
512
      *pSize = (UINT32)dsize;
513
    else
514
    {
515
      free(result);
516
      result = nullptr;
517
    }
518
  }
519
520
fail:
521
  free(bmp);
522
  winpr_image_free(img, TRUE);
523
  return result;
524
}
525
#endif
526
527
#if defined(WINPR_UTILS_IMAGE_PNG)
528
static void* clipboard_synthesize_image_bmp_to_png(wClipboard* clipboard, UINT32 formatId,
529
                                                   const void* data, UINT32* pSize)
530
{
531
  return clipboard_synthesize_image_bmp_to_format(clipboard, formatId, WINPR_IMAGE_PNG, data,
532
                                                  pSize);
533
}
534
#endif
535
536
#if defined(WINPR_UTILS_IMAGE_PNG) || defined(WINPR_UTILS_IMAGE_WEBP) || \
537
    defined(WINPR_UTILS_IMAGE_JPEG)
538
static void* clipboard_synthesize_image_format_to_bmp(WINPR_ATTR_UNUSED wClipboard* clipboard,
539
                                                      WINPR_ATTR_UNUSED UINT32 srcFormatId,
540
                                                      const void* data, UINT32* pSize)
541
{
542
  WINPR_ASSERT(clipboard);
543
  WINPR_ASSERT(data);
544
  WINPR_ASSERT(pSize);
545
546
  BYTE* dst = nullptr;
547
  const UINT32 SrcSize = *pSize;
548
  size_t size = 0;
549
  wImage* image = winpr_image_new();
550
  if (!image)
551
    goto fail;
552
553
  const int res = winpr_image_read_buffer(image, data, SrcSize);
554
  if (res <= 0)
555
    goto fail;
556
557
  dst = winpr_image_write_buffer(image, WINPR_IMAGE_BITMAP, &size);
558
  if ((size < sizeof(WINPR_BITMAP_FILE_HEADER)) || (size > UINT32_MAX))
559
  {
560
    free(dst);
561
    dst = nullptr;
562
    goto fail;
563
  }
564
  *pSize = (UINT32)size;
565
566
fail:
567
  winpr_image_free(image, TRUE);
568
569
  if (dst)
570
    memmove(dst, &dst[sizeof(WINPR_BITMAP_FILE_HEADER)],
571
            size - sizeof(WINPR_BITMAP_FILE_HEADER));
572
  return dst;
573
}
574
#endif
575
576
#if defined(WINPR_UTILS_IMAGE_PNG)
577
static void* clipboard_synthesize_image_png_to_bmp(wClipboard* clipboard, UINT32 formatId,
578
                                                   const void* data, UINT32* pSize)
579
{
580
  return clipboard_synthesize_image_format_to_bmp(clipboard, formatId, data, pSize);
581
}
582
#endif
583
584
#if defined(WINPR_UTILS_IMAGE_WEBP)
585
static void* clipboard_synthesize_image_bmp_to_webp(wClipboard* clipboard, UINT32 formatId,
586
                                                    const void* data, UINT32* pSize)
587
{
588
  return clipboard_synthesize_image_bmp_to_format(clipboard, formatId, WINPR_IMAGE_WEBP, data,
589
                                                  pSize);
590
}
591
592
static void* clipboard_synthesize_image_webp_to_bmp(wClipboard* clipboard, UINT32 formatId,
593
                                                    const void* data, UINT32* pSize)
594
{
595
  return clipboard_synthesize_image_format_to_bmp(clipboard, formatId, data, pSize);
596
}
597
#endif
598
599
#if defined(WINPR_UTILS_IMAGE_JPEG)
600
static void* clipboard_synthesize_image_bmp_to_jpeg(wClipboard* clipboard, UINT32 formatId,
601
                                                    const void* data, UINT32* pSize)
602
{
603
  return clipboard_synthesize_image_bmp_to_format(clipboard, formatId, WINPR_IMAGE_JPEG, data,
604
                                                  pSize);
605
}
606
607
static void* clipboard_synthesize_image_jpeg_to_bmp(wClipboard* clipboard, UINT32 formatId,
608
                                                    const void* data, UINT32* pSize)
609
{
610
  return clipboard_synthesize_image_format_to_bmp(clipboard, formatId, data, pSize);
611
}
612
#endif
613
614
/**
615
 * "HTML Format":
616
 *
617
 * HTML clipboard format: msdn.microsoft.com/en-us/library/windows/desktop/ms649015/
618
 */
619
620
static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 formatId,
621
                                              const void* pData, UINT32* pSize)
622
93
{
623
93
  union
624
93
  {
625
93
    const void* cpv;
626
93
    const char* cpc;
627
93
    const BYTE* cpb;
628
93
    WCHAR* pv;
629
93
  } pSrcData;
630
93
  char* pDstData = nullptr;
631
632
93
  pSrcData.cpv = nullptr;
633
634
93
  WINPR_ASSERT(clipboard);
635
93
  WINPR_ASSERT(pSize);
636
637
93
  if (formatId == ClipboardGetFormatId(clipboard, mime_html))
638
93
  {
639
93
    const size_t SrcSize = (size_t)*pSize;
640
93
    const size_t DstSize = SrcSize + 200;
641
93
    char* body = nullptr;
642
93
    char num[20] = WINPR_C_ARRAY_INIT;
643
644
    /* Create a copy, we modify the input data */
645
93
    pSrcData.pv = calloc(1, SrcSize + 1);
646
93
    if (!pSrcData.pv)
647
0
      goto fail;
648
93
    memcpy(pSrcData.pv, pData, SrcSize);
649
650
93
    if (SrcSize > 2)
651
91
    {
652
91
      if (SrcSize > INT_MAX)
653
0
        goto fail;
654
655
      /* Check the BOM (Byte Order Mark) */
656
91
      if ((pSrcData.cpb[0] == 0xFE) && (pSrcData.cpb[1] == 0xFF))
657
42
      {
658
42
        if (!ByteSwapUnicode(pSrcData.pv, (SrcSize / 2)))
659
0
          goto fail;
660
42
      }
661
662
      /* Check if we have WCHAR, convert to UTF-8 */
663
91
      if ((pSrcData.cpb[0] == 0xFF) && (pSrcData.cpb[1] == 0xFE))
664
47
      {
665
47
        char* utfString = ConvertWCharNToUtf8Alloc(&pSrcData.pv[1],
666
47
                                                   (SrcSize / sizeof(WCHAR)) - 1, nullptr);
667
47
        free(pSrcData.pv);
668
47
        pSrcData.cpc = utfString;
669
47
        if (!utfString)
670
7
          goto fail;
671
47
      }
672
91
    }
673
674
86
    pDstData = (char*)calloc(1, DstSize);
675
676
86
    if (!pDstData)
677
0
      goto fail;
678
679
86
    (void)sprintf_s(pDstData, DstSize,
680
86
                    "Version:0.9\r\n"
681
86
                    "StartHTML:0000000000\r\n"
682
86
                    "EndHTML:0000000000\r\n"
683
86
                    "StartFragment:0000000000\r\n"
684
86
                    "EndFragment:0000000000\r\n");
685
86
    body = strstr(pSrcData.cpc, "<body");
686
687
86
    if (!body)
688
82
      body = strstr(pSrcData.cpc, "<BODY");
689
690
    /* StartHTML */
691
86
    (void)sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, DstSize));
692
86
    CopyMemory(&pDstData[23], num, 10);
693
694
86
    if (!body)
695
82
    {
696
82
      if (!winpr_str_append("<HTML><BODY>", pDstData, DstSize, nullptr))
697
0
        goto fail;
698
82
    }
699
700
86
    if (!winpr_str_append("<!--StartFragment-->", pDstData, DstSize, nullptr))
701
0
      goto fail;
702
703
    /* StartFragment */
704
86
    (void)sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
705
86
    CopyMemory(&pDstData[69], num, 10);
706
707
86
    if (!winpr_str_append(pSrcData.cpc, pDstData, DstSize, nullptr))
708
7
      goto fail;
709
710
    /* EndFragment */
711
79
    (void)sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
712
79
    CopyMemory(&pDstData[93], num, 10);
713
714
79
    if (!winpr_str_append("<!--EndFragment-->", pDstData, DstSize, nullptr))
715
2
      goto fail;
716
717
77
    if (!body)
718
73
    {
719
73
      if (!winpr_str_append("</BODY></HTML>", pDstData, DstSize, nullptr))
720
3
        goto fail;
721
73
    }
722
723
    /* EndHTML */
724
74
    (void)sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, DstSize));
725
74
    CopyMemory(&pDstData[43], num, 10);
726
74
    *pSize = (UINT32)strnlen(pDstData, DstSize) + 1;
727
74
  }
728
93
fail:
729
93
  free(pSrcData.pv);
730
93
  return pDstData;
731
93
}
732
733
static char* html_pre_write(wStream* s, const char* what)
734
960
{
735
960
  const size_t len = strlen(what);
736
960
  Stream_Write(s, what, len);
737
960
  char* startHTML = Stream_PointerAs(s, char);
738
10.5k
  for (size_t x = 0; x < 10; x++)
739
9.60k
    Stream_Write_INT8(s, '0');
740
960
  Stream_Write(s, "\r\n", 2);
741
960
  return startHTML;
742
960
}
743
744
static void html_fill_number(char* pos, size_t val)
745
960
{
746
960
  char str[11] = WINPR_C_ARRAY_INIT;
747
960
  (void)_snprintf(str, sizeof(str), "%010" PRIuz, val);
748
960
  memcpy(pos, str, 10);
749
960
}
750
751
static void* clipboard_wrap_html(const char* mime, const char* idata, size_t ilength,
752
                                 uint32_t* plen)
753
240
{
754
240
  WINPR_ASSERT(mime);
755
240
  WINPR_ASSERT(plen);
756
757
240
  *plen = 0;
758
759
240
  size_t b64len = 0;
760
240
  char* b64 = b64_encode((const BYTE*)idata, ilength, &b64len);
761
240
  if (!b64)
762
0
    return nullptr;
763
764
240
  const size_t mimelen = strlen(mime);
765
240
  wStream* s = Stream_New(nullptr, b64len + 225 + mimelen);
766
240
  if (!s)
767
0
  {
768
0
    free(b64);
769
0
    return nullptr;
770
0
  }
771
772
240
  char* startHTML = html_pre_write(s, "Version:0.9\r\nStartHTML:");
773
240
  char* endHTML = html_pre_write(s, "EndHTML:");
774
240
  char* startFragment = html_pre_write(s, "StartFragment:");
775
240
  char* endFragment = html_pre_write(s, "EndFragment:");
776
777
240
  html_fill_number(startHTML, Stream_GetPosition(s));
778
240
  const char html[] = "<html><!--StartFragment-->";
779
240
  Stream_Write(s, html, strnlen(html, sizeof(html)));
780
781
240
  html_fill_number(startFragment, Stream_GetPosition(s));
782
783
240
  const char body[] = "<body><img alt=\"FreeRDP clipboard image\" src=\"data:";
784
240
  Stream_Write(s, body, strnlen(body, sizeof(body)));
785
786
240
  Stream_Write(s, mime, mimelen);
787
788
240
  const char base64[] = ";base64,";
789
240
  Stream_Write(s, base64, strnlen(base64, sizeof(base64)));
790
240
  Stream_Write(s, b64, b64len);
791
792
240
  const char end[] = "\"/></body>";
793
240
  Stream_Write(s, end, strnlen(end, sizeof(end)));
794
795
240
  html_fill_number(endFragment, Stream_GetPosition(s));
796
797
240
  const char fragend[] = "<!--EndFragment--></html>";
798
240
  Stream_Write(s, fragend, strnlen(fragend, sizeof(fragend)));
799
240
  html_fill_number(endHTML, Stream_GetPosition(s));
800
801
240
  void* res = Stream_Buffer(s);
802
240
  const size_t pos = Stream_GetPosition(s);
803
240
  *plen = WINPR_ASSERTING_INT_CAST(uint32_t, pos);
804
240
  Stream_Free(s, FALSE);
805
240
  free(b64);
806
240
  return res;
807
240
}
808
809
static void* clipboard_wrap_format_to_html(uint32_t bmpFormat, const char* idata, size_t ilength,
810
                                           uint32_t* plen)
811
0
{
812
0
  void* res = nullptr;
813
0
  wImage* img = winpr_image_new();
814
0
  if (!img)
815
0
    goto fail;
816
817
0
  if (winpr_image_read_buffer(img, (const BYTE*)idata, ilength) <= 0)
818
0
    goto fail;
819
820
0
  {
821
0
    size_t bmpsize = 0;
822
0
    void* bmp = winpr_image_write_buffer(img, bmpFormat, &bmpsize);
823
0
    if (!bmp)
824
0
      goto fail;
825
826
0
    res = clipboard_wrap_html(winpr_image_format_mime(bmpFormat), bmp, bmpsize, plen);
827
0
    free(bmp);
828
0
  }
829
0
fail:
830
0
  winpr_image_free(img, TRUE);
831
0
  return res;
832
0
}
833
834
static void* clipboard_wrap_bmp_to_html(const char* idata, size_t ilength, uint32_t* plen)
835
240
{
836
240
  const uint32_t formats[] = { WINPR_IMAGE_WEBP, WINPR_IMAGE_PNG, WINPR_IMAGE_JPEG };
837
838
960
  for (size_t x = 0; x < ARRAYSIZE(formats); x++)
839
720
  {
840
720
    const uint32_t format = formats[x];
841
720
    if (winpr_image_format_is_supported(format))
842
0
    {
843
0
      return clipboard_wrap_format_to_html(format, idata, ilength, plen);
844
0
    }
845
720
  }
846
240
  const uint32_t bmpFormat = WINPR_IMAGE_BITMAP;
847
240
  return clipboard_wrap_html(winpr_image_format_mime(bmpFormat), idata, ilength, plen);
848
240
}
849
850
static void* clipboard_synthesize_image_html(WINPR_ATTR_UNUSED wClipboard* clipboard,
851
                                             UINT32 formatId, const void* data, UINT32* pSize)
852
426
{
853
426
  WINPR_ASSERT(pSize);
854
855
426
  const size_t datalen = *pSize;
856
857
426
  switch (formatId)
858
426
  {
859
0
    case CF_TIFF:
860
0
      return clipboard_wrap_html(mime_tiff, data, datalen, pSize);
861
272
    case CF_DIB:
862
272
    case CF_DIBV5:
863
272
    {
864
272
      uint32_t bmplen = *pSize;
865
272
      void* bmp = clipboard_synthesize_image_bmp(clipboard, formatId, data, &bmplen);
866
272
      if (!bmp)
867
186
      {
868
186
        WLog_WARN(TAG, "failed to convert formatId 0x%08" PRIx32 " [%s]", formatId,
869
186
                  ClipboardGetFormatName(clipboard, formatId));
870
186
        *pSize = 0;
871
186
        return nullptr;
872
186
      }
873
874
86
      void* res = clipboard_wrap_bmp_to_html(bmp, bmplen, pSize);
875
86
      free(bmp);
876
86
      return res;
877
272
    }
878
154
    default:
879
154
    {
880
154
      const uint32_t idWebp = ClipboardRegisterFormat(clipboard, mime_webp);
881
154
      const uint32_t idPng = ClipboardRegisterFormat(clipboard, mime_png);
882
154
      const uint32_t idJpeg = ClipboardRegisterFormat(clipboard, mime_jpeg);
883
154
      const uint32_t idTiff = ClipboardRegisterFormat(clipboard, mime_tiff);
884
154
      if (formatId == idWebp)
885
0
      {
886
0
        return clipboard_wrap_html(mime_webp, data, datalen, pSize);
887
0
      }
888
154
      else if (formatId == idPng)
889
0
      {
890
0
        return clipboard_wrap_html(mime_png, data, datalen, pSize);
891
0
      }
892
154
      else if (formatId == idJpeg)
893
0
      {
894
0
        return clipboard_wrap_html(mime_jpeg, data, datalen, pSize);
895
0
      }
896
154
      else if (formatId == idTiff)
897
0
      {
898
0
        return clipboard_wrap_html(mime_tiff, data, datalen, pSize);
899
0
      }
900
154
      else
901
154
      {
902
154
        for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
903
154
        {
904
154
          const char* mime = mime_bitmap[x];
905
154
          const uint32_t id = ClipboardRegisterFormat(clipboard, mime);
906
907
154
          if (formatId == id)
908
154
            return clipboard_wrap_bmp_to_html(data, datalen, pSize);
909
154
        }
910
154
      }
911
912
0
      WLog_WARN(TAG, "Unsupported image format id 0x%08" PRIx32 " [%s]", formatId,
913
0
                ClipboardGetFormatName(clipboard, formatId));
914
0
      *pSize = 0;
915
0
      return nullptr;
916
154
    }
917
426
  }
918
426
}
919
920
/**
921
 * "text/html":
922
 *
923
 * HTML text format.
924
 */
925
926
static void* clipboard_synthesize_text_html(wClipboard* clipboard, UINT32 formatId,
927
                                            const void* data, UINT32* pSize)
928
96
{
929
96
  char* pDstData = nullptr;
930
931
96
  if (formatId == ClipboardGetFormatId(clipboard, mime_ms_html))
932
96
  {
933
96
    const char* str = (const char*)data;
934
96
    const size_t SrcSize = *pSize;
935
96
    const char* begStr = strstr(str, "StartHTML:");
936
96
    const char* endStr = strstr(str, "EndHTML:");
937
938
96
    if (!begStr || !endStr)
939
2
      return nullptr;
940
941
96
    errno = 0;
942
94
    const long beg = strtol(&begStr[10], nullptr, 10);
943
944
94
    if (errno != 0)
945
1
      return nullptr;
946
947
93
    const long end = strtol(&endStr[8], nullptr, 10);
948
949
93
    if ((beg < 0) || (end < 0) || ((size_t)beg > SrcSize) || ((size_t)end > SrcSize) ||
950
6
        (beg >= end) || (errno != 0))
951
88
      return nullptr;
952
953
5
    const size_t DstSize = (size_t)(end - beg);
954
5
    pDstData = calloc(DstSize + 1, sizeof(char));
955
956
5
    if (!pDstData)
957
0
      return nullptr;
958
959
5
    CopyMemory(pDstData, &str[beg], DstSize);
960
5
    const size_t rc = ConvertLineEndingToLF(pDstData, DstSize);
961
5
    WINPR_ASSERT(rc <= UINT32_MAX);
962
5
    *pSize = (UINT32)rc;
963
5
  }
964
965
5
  return pDstData;
966
96
}
967
968
BOOL ClipboardInitSynthesizers(wClipboard* clipboard)
969
1.69k
{
970
  /**
971
   * CF_TEXT
972
   */
973
1.69k
  {
974
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_TEXT, CF_OEMTEXT,
975
1.69k
                                      clipboard_synthesize_cf_oemtext))
976
0
      return FALSE;
977
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_TEXT, CF_UNICODETEXT,
978
1.69k
                                      clipboard_synthesize_cf_unicodetext))
979
0
      return FALSE;
980
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_TEXT, CF_LOCALE,
981
1.69k
                                      clipboard_synthesize_cf_locale))
982
0
      return FALSE;
983
984
1.69k
    UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_text_plain);
985
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_TEXT, altFormatId,
986
1.69k
                                      clipboard_synthesize_utf8_string))
987
0
      return FALSE;
988
1.69k
  }
989
  /**
990
   * CF_OEMTEXT
991
   */
992
1.69k
  {
993
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_OEMTEXT, CF_TEXT,
994
1.69k
                                      clipboard_synthesize_cf_text))
995
0
      return FALSE;
996
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_OEMTEXT, CF_UNICODETEXT,
997
1.69k
                                      clipboard_synthesize_cf_unicodetext))
998
0
      return FALSE;
999
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_OEMTEXT, CF_LOCALE,
1000
1.69k
                                      clipboard_synthesize_cf_locale))
1001
0
      return FALSE;
1002
1.69k
    UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_text_plain);
1003
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_OEMTEXT, altFormatId,
1004
1.69k
                                      clipboard_synthesize_utf8_string))
1005
0
      return FALSE;
1006
1.69k
  }
1007
  /**
1008
   * CF_UNICODETEXT
1009
   */
1010
1.69k
  {
1011
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_UNICODETEXT, CF_TEXT,
1012
1.69k
                                      clipboard_synthesize_cf_text))
1013
0
      return FALSE;
1014
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_UNICODETEXT, CF_OEMTEXT,
1015
1.69k
                                      clipboard_synthesize_cf_oemtext))
1016
0
      return FALSE;
1017
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_UNICODETEXT, CF_LOCALE,
1018
1.69k
                                      clipboard_synthesize_cf_locale))
1019
0
      return FALSE;
1020
1.69k
    UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_text_plain);
1021
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_UNICODETEXT, altFormatId,
1022
1.69k
                                      clipboard_synthesize_utf8_string))
1023
0
      return FALSE;
1024
1.69k
  }
1025
  /**
1026
   * UTF8_STRING
1027
   */
1028
1.69k
  {
1029
1.69k
    UINT32 formatId = ClipboardRegisterFormat(clipboard, mime_text_plain);
1030
1031
1.69k
    if (formatId)
1032
1.69k
    {
1033
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_TEXT,
1034
1.69k
                                        clipboard_synthesize_cf_text))
1035
0
        return FALSE;
1036
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_OEMTEXT,
1037
1.69k
                                        clipboard_synthesize_cf_oemtext))
1038
0
        return FALSE;
1039
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_UNICODETEXT,
1040
1.69k
                                        clipboard_synthesize_cf_unicodetext))
1041
0
        return FALSE;
1042
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_LOCALE,
1043
1.69k
                                        clipboard_synthesize_cf_locale))
1044
0
        return FALSE;
1045
1.69k
    }
1046
1.69k
  }
1047
  /**
1048
   * text/plain
1049
   */
1050
1.69k
  {
1051
1.69k
    UINT32 formatId = ClipboardRegisterFormat(clipboard, mime_text_plain);
1052
1053
1.69k
    if (formatId)
1054
1.69k
    {
1055
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_TEXT,
1056
1.69k
                                        clipboard_synthesize_cf_text))
1057
0
        return FALSE;
1058
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_OEMTEXT,
1059
1.69k
                                        clipboard_synthesize_cf_oemtext))
1060
0
        return FALSE;
1061
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_UNICODETEXT,
1062
1.69k
                                        clipboard_synthesize_cf_unicodetext))
1063
0
        return FALSE;
1064
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, CF_LOCALE,
1065
1.69k
                                        clipboard_synthesize_cf_locale))
1066
0
        return FALSE;
1067
1.69k
    }
1068
1.69k
  }
1069
1070
1.69k
  const uint32_t htmlFormat = ClipboardRegisterFormat(clipboard, mime_ms_html);
1071
1.69k
  const uint32_t tiffFormat = ClipboardRegisterFormat(clipboard, mime_tiff);
1072
1073
  /**
1074
   * CF_TIFF
1075
   */
1076
1.69k
  if (!ClipboardRegisterSynthesizer(clipboard, CF_TIFF, htmlFormat,
1077
1.69k
                                    clipboard_synthesize_image_html))
1078
0
    return FALSE;
1079
1.69k
  if (!ClipboardRegisterSynthesizer(clipboard, tiffFormat, htmlFormat,
1080
1.69k
                                    clipboard_synthesize_image_html))
1081
0
    return FALSE;
1082
1083
  /**
1084
   * CF_DIB
1085
   */
1086
1.69k
  {
1087
#if defined(WINPR_UTILS_IMAGE_DIBv5)
1088
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIB, CF_DIBV5,
1089
                                      clipboard_synthesize_cf_dibv5))
1090
      return FALSE;
1091
#endif
1092
8.47k
    for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
1093
6.78k
    {
1094
6.78k
      const char* mime = mime_bitmap[x];
1095
6.78k
      const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime);
1096
6.78k
      if (altFormatId == 0)
1097
0
        continue;
1098
6.78k
      if (!ClipboardRegisterSynthesizer(clipboard, CF_DIB, altFormatId,
1099
6.78k
                                        clipboard_synthesize_image_bmp))
1100
0
        return FALSE;
1101
6.78k
    }
1102
1.69k
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIB, htmlFormat,
1103
1.69k
                                      clipboard_synthesize_image_html))
1104
0
      return FALSE;
1105
1.69k
  }
1106
1107
  /**
1108
   * CF_DIBV5
1109
   */
1110
#if defined(WINPR_UTILS_IMAGE_DIBv5)
1111
  {
1112
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIBV5, CF_DIB, clipboard_synthesize_cf_dib))
1113
      return FALSE;
1114
1115
    for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
1116
    {
1117
      const char* mime = mime_bitmap[x];
1118
      const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime);
1119
      if (altFormatId == 0)
1120
        continue;
1121
      if (!ClipboardRegisterSynthesizer(clipboard, CF_DIBV5, altFormatId,
1122
                                        clipboard_synthesize_image_bmp))
1123
        return FALSE;
1124
    }
1125
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIBV5, htmlFormat,
1126
                                      clipboard_synthesize_image_html))
1127
      return FALSE;
1128
  }
1129
#endif
1130
1131
  /**
1132
   * image/bmp
1133
   */
1134
8.47k
  for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
1135
6.78k
  {
1136
6.78k
    const char* mime = mime_bitmap[x];
1137
6.78k
    const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime);
1138
6.78k
    if (altFormatId == 0)
1139
0
      continue;
1140
6.78k
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIB,
1141
6.78k
                                      clipboard_synthesize_cf_dib))
1142
0
      return FALSE;
1143
#if defined(WINPR_UTILS_IMAGE_DIBv5)
1144
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIBV5,
1145
                                      clipboard_synthesize_cf_dibv5))
1146
      return FALSE;
1147
#endif
1148
6.78k
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, htmlFormat,
1149
6.78k
                                      clipboard_synthesize_image_html))
1150
0
      return FALSE;
1151
6.78k
  }
1152
1153
  /**
1154
   * image/png
1155
   */
1156
#if defined(WINPR_UTILS_IMAGE_PNG)
1157
  {
1158
    const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_png);
1159
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIB, altFormatId,
1160
                                      clipboard_synthesize_image_bmp_to_png))
1161
      return FALSE;
1162
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIB,
1163
                                      clipboard_synthesize_image_png_to_bmp))
1164
      return FALSE;
1165
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, htmlFormat,
1166
                                      clipboard_synthesize_image_html))
1167
      return FALSE;
1168
#if defined(WINPR_UTILS_IMAGE_DIBv5)
1169
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIBV5, altFormatId,
1170
                                      clipboard_synthesize_image_bmp_to_png))
1171
      return FALSE;
1172
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIBV5,
1173
                                      clipboard_synthesize_image_png_to_bmp))
1174
      return FALSE;
1175
#endif
1176
  }
1177
#endif
1178
1179
  /**
1180
   * image/webp
1181
   */
1182
#if defined(WINPR_UTILS_IMAGE_WEBP)
1183
  {
1184
    const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_webp);
1185
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIB, altFormatId,
1186
                                      clipboard_synthesize_image_bmp_to_webp))
1187
      return FALSE;
1188
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIB,
1189
                                      clipboard_synthesize_image_webp_to_bmp))
1190
      return FALSE;
1191
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, htmlFormat,
1192
                                      clipboard_synthesize_image_html))
1193
      return FALSE;
1194
#if defined(WINPR_UTILS_IMAGE_DIBv5)
1195
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIBV5, altFormatId,
1196
                                      clipboard_synthesize_image_bmp_to_webp))
1197
      return FALSE;
1198
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIBV5,
1199
                                      clipboard_synthesize_image_webp_to_bmp))
1200
      return FALSE;
1201
#endif
1202
  }
1203
#endif
1204
1205
  /**
1206
   * image/jpeg
1207
   */
1208
#if defined(WINPR_UTILS_IMAGE_JPEG)
1209
  {
1210
    const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_jpeg);
1211
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIB, altFormatId,
1212
                                      clipboard_synthesize_image_bmp_to_jpeg))
1213
      return FALSE;
1214
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIB,
1215
                                      clipboard_synthesize_image_jpeg_to_bmp))
1216
      return FALSE;
1217
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, htmlFormat,
1218
                                      clipboard_synthesize_image_html))
1219
      return FALSE;
1220
#if defined(WINPR_UTILS_IMAGE_DIBv5)
1221
    if (!ClipboardRegisterSynthesizer(clipboard, altFormatId, CF_DIBV5,
1222
                                      clipboard_synthesize_image_jpeg_to_bmp))
1223
      return FALSE;
1224
    if (!ClipboardRegisterSynthesizer(clipboard, CF_DIBV5, altFormatId,
1225
                                      clipboard_synthesize_image_bmp_to_jpeg))
1226
      return FALSE;
1227
#endif
1228
  }
1229
#endif
1230
1231
  /**
1232
   * HTML Format
1233
   */
1234
1.69k
  {
1235
1.69k
    UINT32 formatId = ClipboardRegisterFormat(clipboard, mime_ms_html);
1236
1237
1.69k
    if (formatId)
1238
1.69k
    {
1239
1.69k
      const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_html);
1240
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, altFormatId,
1241
1.69k
                                        clipboard_synthesize_text_html))
1242
0
        return FALSE;
1243
1.69k
    }
1244
1.69k
  }
1245
1246
  /**
1247
   * text/html
1248
   */
1249
1.69k
  {
1250
1.69k
    UINT32 formatId = ClipboardRegisterFormat(clipboard, mime_html);
1251
1252
1.69k
    if (formatId)
1253
1.69k
    {
1254
1.69k
      const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_ms_html);
1255
1.69k
      if (!ClipboardRegisterSynthesizer(clipboard, formatId, altFormatId,
1256
1.69k
                                        clipboard_synthesize_html_format))
1257
0
        return FALSE;
1258
1.69k
    }
1259
1.69k
  }
1260
1261
1.69k
  return TRUE;
1262
1.69k
}