Coverage Report

Created: 2026-11-10 06:39

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