20#include <winpr/config.h>
24#include <winpr/user.h>
25#include <winpr/image.h>
27#include "../utils/image.h"
29#include "../crt/unicode.h"
32#define TAG WINPR_TAG("clipboard.synthetic")
34static const char mime_html[] =
"text/html";
35static const char mime_ms_html[] =
"HTML Format";
37#define BITMAP_MIME_TYPES "image/bmp", "image/x-bmp", "image/x-MS-bmp", "image/x-win-bitmap"
38static const char* mime_bitmap[] = { BITMAP_MIME_TYPES };
40#if defined(WINPR_UTILS_IMAGE_WEBP)
41static const char mime_webp[] =
"image/webp";
43#if defined(WINPR_UTILS_IMAGE_PNG)
44static const char mime_png[] =
"image/png";
46#if defined(WINPR_UTILS_IMAGE_JPEG)
47static const char mime_jpeg[] =
"image/jpeg";
49static const char mime_tiff[] =
"image/tiff";
51static const char* mime_images[] = {
52#if defined(WINPR_UTILS_IMAGE_WEBP)
55#if defined(WINPR_UTILS_IMAGE_PNG)
58#if defined(WINPR_UTILS_IMAGE_JPEG)
61 mime_tiff, BITMAP_MIME_TYPES
64static const BYTE enc_base64url[] =
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
68static BOOL format_is_utf8(wClipboard* clipboard, UINT32 formatId);
71static inline char* b64_encode(
const BYTE* WINPR_RESTRICT data,
size_t length,
size_t* plen)
74 const BYTE* WINPR_RESTRICT alphabet = enc_base64url;
77 size_t outLen = (length + 3) * 4 / 3;
81 const size_t alen = outLen + extra + 1ull;
82 BYTE* p = malloc(alen);
101 blocks = length - (length % 3);
102 for (
size_t i = 0; i < blocks; i += 3, q += 3)
104 c = (q[0] << 16) + (q[1] << 8) + q[2];
106 *p++ = alphabet[(c & 0x00FC0000) >> 18];
107 *p++ = alphabet[(c & 0x0003F000) >> 12];
108 *p++ = alphabet[(c & 0x00000FC0) >> 6];
109 *p++ = alphabet[c & 0x0000003F];
119 *p++ = alphabet[(c & 0x00FC0000) >> 18];
120 *p++ = alphabet[(c & 0x0003F000) >> 12];
123 c = (q[0] << 16) + (q[1] << 8);
124 *p++ = alphabet[(c & 0x00FC0000) >> 18];
125 *p++ = alphabet[(c & 0x0003F000) >> 12];
126 *p++ = alphabet[(c & 0x00000FC0) >> 6];
133 *plen = WINPR_ASSERTING_INT_CAST(
size_t, p - ret);
148WINPR_ATTR_MALLOC(free, 1)
149static
void* clipboard_synthesize_cf_locale(WINPR_ATTR_UNUSED wClipboard* clipboard,
150 UINT32 dstFormatId, WINPR_ATTR_UNUSED const
void* data,
151 WINPR_ATTR_UNUSED UINT32* pSize)
153 if (dstFormatId != CF_LOCALE)
156 "Unuspported destination format %s [0x%04" PRIx32
157 "], trying to convert from %s [0x%04" PRIx32
"]",
158 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
159 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
163 switch (clipboard->formatId)
171 const UINT32 formatIdText = ClipboardRegisterFormat(clipboard, mime_text_plain);
172 if ((clipboard->formatId != formatIdText) &&
173 !format_is_utf8(clipboard, clipboard->formatId))
176 "Unuspported source format %s [0x%04" PRIx32
177 "], trying to convert to %s [0x%04" PRIx32
"]",
178 ClipboardGetFormatName(clipboard, clipboard->formatId),
179 clipboard->formatId, ClipboardGetFormatName(clipboard, dstFormatId),
187 UINT32* pDstData = (UINT32*)calloc(1,
sizeof(UINT32));
193 *pSize =
sizeof(UINT32);
194 return (
void*)pDstData;
199BOOL format_is_utf8(wClipboard* clipboard, UINT32 formatId)
202 if (formatId == ClipboardGetFormatId(clipboard, mime_text_plain))
205 return (formatId == ClipboardGetFormatId(clipboard, mime_text_utf8)) ||
206 (formatId == ClipboardGetFormatId(clipboard, mime_text_UTF8_STRING));
214WINPR_ATTR_MALLOC(free, 1)
215static
void* clipboard_synthesize_string(wClipboard* clipboard, UINT32 dstFormatId,
216 const
void* data, UINT32* pSize)
218 WINPR_ASSERT(clipboard);
221 char* utf8 =
nullptr;
223 if (clipboard->formatId == CF_UNICODETEXT)
226 utf8 = ConvertWCharNToUtf8Alloc(data, *pSize /
sizeof(WCHAR), &size);
231 const size_t rc = ConvertLineEndingToLF(utf8, size);
234 else if (format_is_utf8(clipboard, clipboard->formatId))
236 const size_t size = *pSize;
237 utf8 = strndup(data, size);
240 utf8len = strlen(utf8);
242 else if ((clipboard->formatId == CF_TEXT) || (clipboard->formatId == CF_OEMTEXT) ||
243 (clipboard->formatId == ClipboardGetFormatId(clipboard, mime_text_plain)))
245 const size_t size = *pSize;
246 utf8 = calloc(size + 1,
sizeof(
char));
251 CopyMemory(utf8, data, size);
252 const size_t rc = ConvertLineEndingToLF(utf8, size);
253 const SSIZE_T res = winpr_utfEscapedStringToUtf8(utf8, rc);
256 winpr_znfree(utf8, size);
259 utf8len = (size_t)res;
264 "Unuspported source format %s [0x%04" PRIx32
265 "], trying to convert to %s [0x%04" PRIx32
"]",
267 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
268 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
276 size_t crlfLen = utf8len;
277 char* crlf = ConvertLineEndingToCRLF(utf8, &crlfLen);
278 winpr_znfree(utf8, utf8len);
283 WCHAR* wstr = ConvertUtf8NToWCharAlloc(crlf, crlfLen, &wlen);
284 winpr_znfree(crlf, crlfLen);
285 if (!wstr || (wlen == 0) || (wlen >= UINT32_MAX /
sizeof(WCHAR)))
287 winpr_znfree(wstr, wlen *
sizeof(WCHAR));
291 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, (wlen + 1) *
sizeof(WCHAR));
298 char* escaped = winpr_utf8ToUtfEscapedString(utf8, utf8len, &esclen);
299 winpr_znfree(utf8, utf8len);
300 if (!escaped || (esclen == 0) || (esclen >= UINT32_MAX))
302 winpr_znfree(escaped, esclen);
305 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, esclen + 1);
309 if (format_is_utf8(clipboard, dstFormatId))
311 if (utf8len >= UINT32_MAX)
313 winpr_znfree(utf8, utf8len);
316 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, utf8len + 1);
320 if (dstFormatId == ClipboardGetFormatId(clipboard, mime_text_plain))
323 char* escaped = winpr_utf8ToUtfEscapedString(utf8, utf8len, &esclen);
324 winpr_znfree(utf8, utf8len);
325 if (!escaped || (esclen == 0) || (esclen >= UINT32_MAX))
327 winpr_znfree(escaped, esclen);
330 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, esclen + 1);
335 "Unuspported destination format %s [0x%04" PRIx32
336 "], trying to convert from %s [0x%04" PRIx32
"]",
337 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
338 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
339 winpr_znfree(utf8, utf8len);
344WINPR_ATTR_MALLOC(free, 1)
346 size_t offset, const
void* data,
size_t size,
349 WINPR_ASSERT(pInfoHeader);
353 if ((pInfoHeader->biBitCount < 1) || (pInfoHeader->biBitCount > 32))
359 if ((pInfoHeader->biSize > size) || (offset > (size - pInfoHeader->biSize)))
363 if (bitmapOffset > DstSize)
366 wStream* s = Stream_New(
nullptr, DstSize);
371 fileHeader.bfType[0] =
'B';
372 fileHeader.bfType[1] =
'M';
373 fileHeader.bfSize = (UINT32)DstSize;
374 fileHeader.bfOffBits = (UINT32)bitmapOffset;
375 if (!writeBitmapFileHeader(s, &fileHeader))
378 if (!Stream_EnsureRemainingCapacity(s, size))
380 Stream_Write(s, data, size);
383 const size_t len = Stream_GetPosition(s);
388 *pSize = (UINT32)DstSize;
391 BYTE* dst = Stream_Buffer(s);
392 Stream_Free(s, FALSE);
397 Stream_Free(s, TRUE);
406WINPR_ATTR_MALLOC(free, 1)
407static
void* clipboard_synthesize_image_bmp(WINPR_ATTR_UNUSED wClipboard* clipboard,
408 UINT32 dstFormatId, const
void* data, UINT32* pSize)
410 UINT32 SrcSize = *pSize;
412 if (dstFormatId == CF_DIB)
417 wStream sbuffer = WINPR_C_ARRAY_INIT;
420 wStream* s = Stream_StaticConstInit(&sbuffer, data, SrcSize);
421 if (!readBitmapInfoHeader(s, &header, &offset))
424 return clipboard_prepend_bmp_header(&header, offset, data, SrcSize, pSize);
426#if defined(WINPR_UTILS_IMAGE_DIBv5)
427 else if (dstFormatId == CF_DIBV5)
430 "Unuspported destination format %s [0x%04" PRIx32
431 "], trying to convert from %s [0x%04" PRIx32
"]",
432 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
433 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
439 "Unuspported destination format %s [0x%04" PRIx32
440 "], trying to convert from %s [0x%04" PRIx32
"]",
441 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
442 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
449static BOOL format_is_image(wClipboard* clipboard, UINT32 formatId)
459 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
461 const char* mime = mime_images[x];
462 const UINT32
id = ClipboardRegisterFormat(clipboard, mime);
471static UINT32 image_format(WINPR_ATTR_UNUSED wClipboard* clipboard,
472 WINPR_ATTR_UNUSED UINT32 formatId)
474#if defined(WINPR_UTILS_IMAGE_PNG)
475 if (formatId == ClipboardRegisterFormat(clipboard, mime_png))
476 return WINPR_IMAGE_PNG;
478#if defined(WINPR_UTILS_IMAGE_JPEG)
479 if (formatId == ClipboardRegisterFormat(clipboard, mime_jpeg))
480 return WINPR_IMAGE_JPEG;
482#if defined(WINPR_UTILS_IMAGE_WEBP)
483 if (formatId == ClipboardRegisterFormat(clipboard, mime_webp))
484 return WINPR_IMAGE_WEBP;
486 return WINPR_IMAGE_BITMAP;
489WINPR_ATTR_MALLOC(free, 1)
490static
void* clipboard_synthesize_image_dib_to_format(wClipboard* clipboard, UINT32 dstFormatId,
491 const
void* data, UINT32* pSize)
493 WINPR_ASSERT(clipboard);
497 if ((clipboard->formatId != CF_DIB) && (clipboard->formatId != CF_DIBV5))
500 "Unuspported source format %s [0x%04" PRIx32
501 "], trying to convert to %s [0x%04" PRIx32
"]",
503 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
504 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
507 if (!format_is_image(clipboard, dstFormatId))
510 "Unuspported destination format %s [0x%04" PRIx32
511 "], trying to convert from %s [0x%04" PRIx32
"]",
513 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
514 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
518 const UINT32 format = image_format(clipboard, dstFormatId);
520 void* result =
nullptr;
522 wImage* img = winpr_image_new();
523 void* bmp = clipboard_synthesize_image_bmp(clipboard, clipboard->formatId, data, pSize);
524 const UINT32 SrcSize = *pSize;
530 if (winpr_image_read_buffer(img, bmp, SrcSize) <= 0)
533 result = winpr_image_write_buffer(img, format, &dsize);
536 if (dsize <= UINT32_MAX)
537 *pSize = (UINT32)dsize;
547 winpr_image_free(img, TRUE);
556WINPR_ATTR_MALLOC(free, 1)
557static
void* clipboard_synthesize_cf_dib(const
void* pvdata,
size_t dataLen, UINT32* pSize)
560 const BYTE* data = pvdata;
563 wStream sbuffer = WINPR_C_ARRAY_INIT;
564 wStream* s = Stream_StaticConstInit(&sbuffer, data, dataLen);
565 if (!readBitmapFileHeader(s, &pFileHeader))
569 BYTE* pDstData = (BYTE*)calloc(DstSize, 1);
575 memcpy(pDstData, src, DstSize);
576 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, DstSize);
580WINPR_ATTR_MALLOC(free, 1)
581static
void* clipboard_synthesize_image_format_to_cf_dib(wClipboard* clipboard, UINT32 dstFormatId,
582 const
void* data, UINT32* pSize)
584 WINPR_ASSERT(clipboard);
588 if ((dstFormatId != CF_DIB) && (dstFormatId != CF_DIBV5))
591 "Unuspported destination format %s [0x%04" PRIx32
592 "], trying to convert from %s [0x%04" PRIx32
"]",
593 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
594 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
598 if (!format_is_image(clipboard, dstFormatId))
601 "Unuspported source format %s [0x%04" PRIx32
602 "], trying to convert to %s [0x%04" PRIx32
"]",
603 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
604 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
608 void* result =
nullptr;
610 const UINT32 SrcSize = *pSize;
612 wImage* image = winpr_image_new();
616 const int res = winpr_image_read_buffer(image, data, SrcSize);
620 dst = winpr_image_write_buffer(image, WINPR_IMAGE_BITMAP, &size);
624 result = clipboard_synthesize_cf_dib(dst, size, pSize);
627 winpr_image_free(image, TRUE);
637WINPR_ATTR_MALLOC(free, 1)
638static
void* clipboard_synthesize_image_format_to_format(wClipboard* clipboard, UINT32 dstFormatId,
639 const
void* data, UINT32* pSize)
641 WINPR_ASSERT(clipboard);
645 if (!format_is_image(clipboard, clipboard->formatId) ||
646 !format_is_image(clipboard, dstFormatId))
648 WLog_ERR(TAG,
"Unsupported conversion from %s [0x%04" PRIx32
"] to %s [0x%04" PRIx32
"]",
649 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
650 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
654 void* result =
nullptr;
656 const UINT32 SrcSize = *pSize;
659 wImage* image = winpr_image_new();
663 if (winpr_image_read_buffer(image, data, SrcSize) <= 0)
666 result = winpr_image_write_buffer(image, image_format(clipboard, dstFormatId), &size);
669 if (size <= UINT32_MAX)
670 *pSize = (UINT32)size;
679 winpr_image_free(image, TRUE);
688WINPR_ATTR_MALLOC(free, 1)
689static
void* clipboard_synthesize_ms_html_format(wClipboard* clipboard, UINT32 formatId,
690 const
void* pData, UINT32* pSize)
692 if (formatId != ClipboardGetFormatId(clipboard, mime_ms_html))
695 "Unuspported destination format %s [0x%04" PRIx32
696 "], trying to convert from %s [0x%04" PRIx32
"]",
697 ClipboardGetFormatName(clipboard, formatId), formatId,
698 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
701 if (clipboard->formatId != ClipboardGetFormatId(clipboard, mime_html))
704 "Unuspported destination format %s [0x%04" PRIx32
705 "], trying to convert from %s [0x%04" PRIx32
"]",
706 ClipboardGetFormatName(clipboard, formatId), formatId,
707 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
717 char* pDstData =
nullptr;
719 pSrcData.cpv =
nullptr;
721 WINPR_ASSERT(clipboard);
724 if (formatId == ClipboardGetFormatId(clipboard, mime_ms_html))
726 const size_t SrcSize = (size_t)*pSize;
727 const size_t DstSize = SrcSize + 200;
728 char* body =
nullptr;
729 char num[20] = WINPR_C_ARRAY_INIT;
732 pSrcData.pv = calloc(1, SrcSize + 1);
735 memcpy(pSrcData.pv, pData, SrcSize);
739 if (SrcSize > INT_MAX)
743 if ((pSrcData.cpb[0] == 0xFE) && (pSrcData.cpb[1] == 0xFF))
745 if (!ByteSwapUnicode(pSrcData.pv, (SrcSize / 2)))
750 if ((pSrcData.cpb[0] == 0xFF) && (pSrcData.cpb[1] == 0xFE))
752 char* utfString = ConvertWCharNToUtf8Alloc(&pSrcData.pv[1],
753 (SrcSize /
sizeof(WCHAR)) - 1,
nullptr);
755 pSrcData.cpc = utfString;
761 pDstData = (
char*)calloc(1, DstSize);
766 (void)sprintf_s(pDstData, DstSize,
768 "StartHTML:0000000000\r\n"
769 "EndHTML:0000000000\r\n"
770 "StartFragment:0000000000\r\n"
771 "EndFragment:0000000000\r\n");
772 body = strstr(pSrcData.cpc,
"<body");
775 body = strstr(pSrcData.cpc,
"<BODY");
778 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, DstSize));
779 CopyMemory(&pDstData[23], num, 10);
783 if (!winpr_str_append(
"<HTML><BODY>", pDstData, DstSize,
nullptr))
787 if (!winpr_str_append(
"<!--StartFragment-->", pDstData, DstSize,
nullptr))
791 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, SrcSize + 200));
792 CopyMemory(&pDstData[69], num, 10);
794 if (!winpr_str_append(pSrcData.cpc, pDstData, DstSize,
nullptr))
798 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, SrcSize + 200));
799 CopyMemory(&pDstData[93], num, 10);
801 if (!winpr_str_append(
"<!--EndFragment-->", pDstData, DstSize,
nullptr))
806 if (!winpr_str_append(
"</BODY></HTML>", pDstData, DstSize,
nullptr))
811 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, DstSize));
812 CopyMemory(&pDstData[43], num, 10);
813 *pSize = (UINT32)strnlen(pDstData, DstSize) + 1;
820WINPR_ATTR_MALLOC(free, 1)
821static
char* html_pre_write(
wStream* s, const
char* what)
823 const size_t len = strlen(what);
824 Stream_Write(s, what, len);
825 char* startHTML = Stream_PointerAs(s,
char);
826 for (
size_t x = 0; x < 10; x++)
827 Stream_Write_INT8(s,
'0');
828 Stream_Write(s,
"\r\n", 2);
832static void html_fill_number(
char* pos,
size_t val)
834 char str[11] = WINPR_C_ARRAY_INIT;
835 (void)_snprintf(str,
sizeof(str),
"%010" PRIuz, val);
836 memcpy(pos, str, 10);
839WINPR_ATTR_MALLOC(free, 1)
840static
void* clipboard_wrap_html(const
char* mime, const
char* idata,
size_t ilength,
849 char* b64 = b64_encode((
const BYTE*)idata, ilength, &b64len);
853 const size_t mimelen = strlen(mime);
854 wStream* s = Stream_New(
nullptr, b64len + 225 + mimelen);
861 char* startHTML = html_pre_write(s,
"Version:0.9\r\nStartHTML:");
862 char* endHTML = html_pre_write(s,
"EndHTML:");
863 char* startFragment = html_pre_write(s,
"StartFragment:");
864 char* endFragment = html_pre_write(s,
"EndFragment:");
866 html_fill_number(startHTML, Stream_GetPosition(s));
867 const char html[] =
"<html><!--StartFragment-->";
868 Stream_Write(s, html, strnlen(html,
sizeof(html)));
870 html_fill_number(startFragment, Stream_GetPosition(s));
872 const char body[] =
"<body><img alt=\"FreeRDP clipboard image\" src=\"data:";
873 Stream_Write(s, body, strnlen(body,
sizeof(body)));
875 Stream_Write(s, mime, mimelen);
877 const char base64[] =
";base64,";
878 Stream_Write(s, base64, strnlen(base64,
sizeof(base64)));
879 Stream_Write(s, b64, b64len);
881 const char end[] =
"\"/></body>";
882 Stream_Write(s, end, strnlen(end,
sizeof(end)));
884 html_fill_number(endFragment, Stream_GetPosition(s));
886 const char fragend[] =
"<!--EndFragment--></html>";
887 Stream_Write(s, fragend, strnlen(fragend,
sizeof(fragend)));
888 html_fill_number(endHTML, Stream_GetPosition(s));
890 void* res = Stream_Buffer(s);
891 const size_t pos = Stream_GetPosition(s);
892 *plen = WINPR_ASSERTING_INT_CAST(uint32_t, pos);
893 Stream_Free(s, FALSE);
898WINPR_ATTR_MALLOC(free, 1)
899static
void* clipboard_wrap_format_to_html(uint32_t bmpFormat, const
char* idata,
size_t ilength,
903 wImage* img = winpr_image_new();
907 if (winpr_image_read_buffer(img, (
const BYTE*)idata, ilength) <= 0)
912 void* bmp = winpr_image_write_buffer(img, bmpFormat, &bmpsize);
916 res = clipboard_wrap_html(winpr_image_format_mime(bmpFormat), bmp, bmpsize, plen);
920 winpr_image_free(img, TRUE);
924WINPR_ATTR_MALLOC(free, 1)
925static
void* clipboard_wrap_bmp_to_ms_html(const
char* idata,
size_t ilength, uint32_t* plen)
927 const uint32_t formats[] = { WINPR_IMAGE_WEBP, WINPR_IMAGE_PNG, WINPR_IMAGE_JPEG };
929 for (
size_t x = 0; x < ARRAYSIZE(formats); x++)
931 const uint32_t format = formats[x];
932 if (winpr_image_format_is_supported(format))
934 return clipboard_wrap_format_to_html(format, idata, ilength, plen);
937 const uint32_t bmpFormat = WINPR_IMAGE_BITMAP;
938 return clipboard_wrap_html(winpr_image_format_mime(bmpFormat), idata, ilength, plen);
941WINPR_ATTR_MALLOC(free, 1)
942static
void* clipboard_synthesize_image_ms_html(WINPR_ATTR_UNUSED wClipboard* clipboard,
943 UINT32 formatId, const
void* data, UINT32* pSize)
945 if (formatId != ClipboardGetFormatId(clipboard, mime_ms_html))
948 "Unuspported destination format %s [0x%04" PRIx32
949 "], trying to convert from %s [0x%04" PRIx32
"]",
950 ClipboardGetFormatName(clipboard, formatId), formatId,
951 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
957 const size_t datalen = *pSize;
959 switch (clipboard->formatId)
962 return clipboard_wrap_html(mime_tiff, data, datalen, pSize);
966 uint32_t bmplen = *pSize;
968 clipboard_synthesize_image_bmp(clipboard, clipboard->formatId, data, &bmplen);
971 WLog_WARN(TAG,
"failed to convert formatId 0x%08" PRIx32
" [%s]",
973 ClipboardGetFormatName(clipboard, clipboard->formatId));
978 void* res = clipboard_wrap_bmp_to_ms_html(bmp, bmplen, pSize);
984#if defined(WINPR_UTILS_IMAGE_WEBP)
985 const uint32_t idWebp = ClipboardRegisterFormat(clipboard, mime_webp);
986 if (clipboard->formatId == idWebp)
987 return clipboard_wrap_html(mime_webp, data, datalen, pSize);
990#if defined(WINPR_UTILS_IMAGE_PNG)
991 const uint32_t idPng = ClipboardRegisterFormat(clipboard, mime_png);
992 if (clipboard->formatId == idPng)
993 return clipboard_wrap_html(mime_png, data, datalen, pSize);
995#if defined(WINPR_UTILS_IMAGE_JPEG)
996 const uint32_t idJpeg = ClipboardRegisterFormat(clipboard, mime_jpeg);
997 if (clipboard->formatId == idJpeg)
998 return clipboard_wrap_html(mime_jpeg, data, datalen, pSize);
1001 const uint32_t idTiff = ClipboardRegisterFormat(clipboard, mime_tiff);
1002 if (clipboard->formatId == idTiff)
1003 return clipboard_wrap_html(mime_tiff, data, datalen, pSize);
1005 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
1007 const char* mime = mime_bitmap[x];
1008 const uint32_t
id = ClipboardRegisterFormat(clipboard, mime);
1011 return clipboard_wrap_bmp_to_ms_html(data, datalen, pSize);
1014 WLog_WARN(TAG,
"Unsupported image format id 0x%08" PRIx32
" [%s]", clipboard->formatId,
1015 ClipboardGetFormatName(clipboard, clipboard->formatId));
1027WINPR_ATTR_MALLOC(free, 1)
1028static
void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 formatId,
1029 const
void* data, UINT32* pSize)
1031 if (formatId != ClipboardGetFormatId(clipboard, mime_html))
1034 "Unuspported destination format %s [0x%04" PRIx32
1035 "], trying to convert from %s [0x%04" PRIx32
"]",
1036 ClipboardGetFormatName(clipboard, formatId), formatId,
1037 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
1040 if (clipboard->formatId != ClipboardGetFormatId(clipboard, mime_ms_html))
1043 "Unuspported destination format %s [0x%04" PRIx32
1044 "], trying to convert from %s [0x%04" PRIx32
"]",
1045 ClipboardGetFormatName(clipboard, formatId), formatId,
1046 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
1050 char* pDstData =
nullptr;
1052 if (formatId == ClipboardGetFormatId(clipboard, mime_html))
1054 const char* str = (
const char*)data;
1055 const size_t SrcSize = *pSize;
1056 const char* begStr = strstr(str,
"StartHTML:");
1057 const char* endStr = strstr(str,
"EndHTML:");
1059 if (!begStr || !endStr)
1063 const long beg = strtol(&begStr[10],
nullptr, 10);
1068 const long end = strtol(&endStr[8],
nullptr, 10);
1070 if ((beg < 0) || (end < 0) || ((
size_t)beg > SrcSize) || ((
size_t)end > SrcSize) ||
1071 (beg >= end) || (errno != 0))
1074 const size_t DstSize = (size_t)(end - beg);
1075 pDstData = calloc(DstSize + 1,
sizeof(
char));
1080 CopyMemory(pDstData, &str[beg], DstSize);
1081 const size_t rc = ConvertLineEndingToLF(pDstData, DstSize);
1082 WINPR_ASSERT(rc <= UINT32_MAX);
1083 *pSize = (UINT32)rc;
1089BOOL ClipboardInitSynthesizers(wClipboard* clipboard)
1091 WINPR_ASSERT(clipboard);
1093 const UINT32 formatIdUtf = ClipboardRegisterFormat(clipboard, mime_text_utf8);
1094 const UINT32 formatIdUtf8String = ClipboardRegisterFormat(clipboard, mime_text_UTF8_STRING);
1095 const UINT32 formatIdPlain = ClipboardRegisterFormat(clipboard, mime_text_plain);
1096 const UINT32 textFormatIds[] = { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT,
1097 formatIdUtf, formatIdUtf8String, formatIdPlain };
1101 for (
size_t x = 0; x < ARRAYSIZE(textFormatIds); x++)
1103 const UINT32 formatId = textFormatIds[x];
1105 for (
size_t y = 0; y < ARRAYSIZE(textFormatIds); y++)
1107 const UINT32 dstFormatId = textFormatIds[y];
1108 if (formatId == dstFormatId)
1111 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, dstFormatId,
1112 clipboard_synthesize_string))
1116 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, CF_LOCALE,
1117 clipboard_synthesize_cf_locale))
1121 const uint32_t msHtmlFormat = ClipboardRegisterFormat(clipboard, mime_ms_html);
1126 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_TIFF, msHtmlFormat,
1127 clipboard_synthesize_image_ms_html))
1133 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, CF_DIB,
1134 clipboard_synthesize_image_dib_to_format))
1136 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIB, msHtmlFormat,
1137 clipboard_synthesize_image_ms_html))
1140#if defined(WINPR_UTILS_IMAGE_DIBv5)
1141 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, CF_DIB,
1142 clipboard_synthesize_image_dib_to_format))
1144 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, msHtmlFormat,
1145 clipboard_synthesize_image_ms_html))
1152 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
1154 const char* mime = mime_images[x];
1155 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime);
1156 if (altFormatId == 0)
1158 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIB, altFormatId,
1159 clipboard_synthesize_image_dib_to_format))
1161 if (!ClipboardRegisterSynthesizerEx(clipboard, altFormatId, CF_DIB,
1162 clipboard_synthesize_image_format_to_cf_dib))
1164 if (!ClipboardRegisterSynthesizerEx(clipboard, altFormatId, msHtmlFormat,
1165 clipboard_synthesize_image_ms_html))
1167#if defined(WINPR_UTILS_IMAGE_DIBv5)
1168 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, altFormatId,
1169 clipboard_synthesize_image_dib_to_format))
1171 if (!ClipboardRegisterSynthesizerEx(clipboard, altFormatId, CF_DIBV5,
1172 clipboard_synthesize_image_format_to_cf_dib))
1177 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
1179 const char* mime = mime_images[x];
1180 if (strcmp(mime, mime_tiff) == 0)
1182 const UINT32 formatId = ClipboardRegisterFormat(clipboard, mime);
1183 for (
size_t y = 0; y < ARRAYSIZE(mime_images); y++)
1185 const char* altMime = mime_images[y];
1186 if ((x == y) || (strcmp(altMime, mime_tiff) == 0))
1188 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, altMime);
1189 if ((formatId == 0) || (altFormatId == 0))
1191 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, altFormatId,
1192 clipboard_synthesize_image_format_to_format))
1202 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_html);
1203 if (!ClipboardRegisterSynthesizerEx(clipboard, msHtmlFormat, altFormatId,
1204 clipboard_synthesize_html_format))
1212 UINT32 formatId = ClipboardRegisterFormat(clipboard, mime_html);
1216 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_ms_html);
1217 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, altFormatId,
1218 clipboard_synthesize_ms_html_format))