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 inline char* b64_encode(
const BYTE* WINPR_RESTRICT data,
size_t length,
size_t* plen)
71 const BYTE* WINPR_RESTRICT alphabet = enc_base64url;
74 size_t outLen = (length + 3) * 4 / 3;
78 const size_t alen = outLen + extra + 1ull;
79 BYTE* p = malloc(alen);
98 blocks = length - (length % 3);
99 for (
size_t i = 0; i < blocks; i += 3, q += 3)
101 c = (q[0] << 16) + (q[1] << 8) + q[2];
103 *p++ = alphabet[(c & 0x00FC0000) >> 18];
104 *p++ = alphabet[(c & 0x0003F000) >> 12];
105 *p++ = alphabet[(c & 0x00000FC0) >> 6];
106 *p++ = alphabet[c & 0x0000003F];
116 *p++ = alphabet[(c & 0x00FC0000) >> 18];
117 *p++ = alphabet[(c & 0x0003F000) >> 12];
120 c = (q[0] << 16) + (q[1] << 8);
121 *p++ = alphabet[(c & 0x00FC0000) >> 18];
122 *p++ = alphabet[(c & 0x0003F000) >> 12];
123 *p++ = alphabet[(c & 0x00000FC0) >> 6];
130 *plen = WINPR_ASSERTING_INT_CAST(
size_t, p - ret);
145WINPR_ATTR_MALLOC(free, 1)
146static
void* clipboard_synthesize_cf_locale(WINPR_ATTR_UNUSED wClipboard* clipboard,
147 UINT32 dstFormatId, WINPR_ATTR_UNUSED const
void* data,
148 WINPR_ATTR_UNUSED UINT32* pSize)
150 if (dstFormatId != CF_LOCALE)
153 "Unuspported destination format %s [0x%04" PRIx32
154 "], trying to convert from %s [0x%04" PRIx32
"]",
155 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
156 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
160 switch (clipboard->formatId)
168 const UINT32 formatIdText = ClipboardRegisterFormat(clipboard, mime_text_plain);
169 const UINT32 formatIdUtf = ClipboardRegisterFormat(clipboard, mime_text_plain);
170 if ((clipboard->formatId != formatIdText) && (clipboard->formatId != formatIdUtf))
173 "Unuspported source format %s [0x%04" PRIx32
174 "], trying to convert to %s [0x%04" PRIx32
"]",
175 ClipboardGetFormatName(clipboard, clipboard->formatId),
176 clipboard->formatId, ClipboardGetFormatName(clipboard, dstFormatId),
184 UINT32* pDstData = (UINT32*)calloc(1,
sizeof(UINT32));
190 return (
void*)pDstData;
196static BOOL format_is_utf8(wClipboard* clipboard, UINT32 formatId)
199 if (formatId == ClipboardGetFormatId(clipboard, mime_text_plain))
202 return (formatId == ClipboardGetFormatId(clipboard, mime_text_utf8)) ||
203 (formatId == ClipboardGetFormatId(clipboard, mime_text_UTF8_STRING));
211WINPR_ATTR_MALLOC(free, 1)
212static
void* clipboard_synthesize_string(wClipboard* clipboard, UINT32 dstFormatId,
213 const
void* data, UINT32* pSize)
215 WINPR_ASSERT(clipboard);
218 char* utf8 =
nullptr;
220 if (clipboard->formatId == CF_UNICODETEXT)
223 utf8 = ConvertWCharNToUtf8Alloc(data, *pSize /
sizeof(WCHAR), &size);
228 const size_t rc = ConvertLineEndingToLF(utf8, size);
231 else if (format_is_utf8(clipboard, clipboard->formatId))
233 const size_t size = *pSize;
234 utf8 = strndup(data, size);
237 utf8len = strlen(utf8);
239 else if ((clipboard->formatId == CF_TEXT) || (clipboard->formatId == CF_OEMTEXT) ||
240 (clipboard->formatId == ClipboardGetFormatId(clipboard, mime_text_plain)))
242 const size_t size = *pSize;
243 utf8 = calloc(size + 1,
sizeof(
char));
248 CopyMemory(utf8, data, size);
249 const size_t rc = ConvertLineEndingToLF(utf8, size);
250 const SSIZE_T res = winpr_utfEscapedStringToUtf8(utf8, rc);
253 winpr_znfree(utf8, size);
256 utf8len = (size_t)res;
261 "Unuspported source format %s [0x%04" PRIx32
262 "], trying to convert to %s [0x%04" PRIx32
"]",
264 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
265 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
273 size_t crlfLen = utf8len;
274 char* crlf = ConvertLineEndingToCRLF(utf8, &crlfLen);
275 winpr_znfree(utf8, utf8len);
280 WCHAR* wstr = ConvertUtf8NToWCharAlloc(crlf, crlfLen, &wlen);
281 winpr_znfree(crlf, crlfLen);
282 if (!wstr || (wlen == 0) || (wlen >= UINT32_MAX /
sizeof(WCHAR)))
284 winpr_znfree(wstr, wlen *
sizeof(WCHAR));
288 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, (wlen + 1) *
sizeof(WCHAR));
295 char* escaped = winpr_utf8ToUtfEscapedString(utf8, utf8len, &esclen);
296 winpr_znfree(utf8, utf8len);
297 if (!escaped || (esclen == 0) || (esclen >= UINT32_MAX))
299 winpr_znfree(escaped, esclen);
302 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, esclen + 1);
306 if (format_is_utf8(clipboard, dstFormatId))
308 if (utf8len >= UINT32_MAX)
310 winpr_znfree(utf8, utf8len);
313 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, utf8len + 1);
317 if (dstFormatId == ClipboardGetFormatId(clipboard, mime_text_plain))
320 char* escaped = winpr_utf8ToUtfEscapedString(utf8, utf8len, &esclen);
321 winpr_znfree(utf8, utf8len);
322 if (!escaped || (esclen == 0) || (esclen >= UINT32_MAX))
324 winpr_znfree(escaped, esclen);
327 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, esclen + 1);
332 "Unuspported destination format %s [0x%04" PRIx32
333 "], trying to convert from %s [0x%04" PRIx32
"]",
334 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
335 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
336 winpr_znfree(utf8, utf8len);
341WINPR_ATTR_MALLOC(free, 1)
343 size_t offset, const
void* data,
size_t size,
346 WINPR_ASSERT(pInfoHeader);
350 if ((pInfoHeader->biBitCount < 1) || (pInfoHeader->biBitCount > 32))
356 if ((pInfoHeader->biSize > size) || (offset > (size - pInfoHeader->biSize)))
360 if (bitmapOffset > DstSize)
363 wStream* s = Stream_New(
nullptr, DstSize);
368 fileHeader.bfType[0] =
'B';
369 fileHeader.bfType[1] =
'M';
370 fileHeader.bfSize = (UINT32)DstSize;
371 fileHeader.bfOffBits = (UINT32)bitmapOffset;
372 if (!writeBitmapFileHeader(s, &fileHeader))
375 if (!Stream_EnsureRemainingCapacity(s, size))
377 Stream_Write(s, data, size);
380 const size_t len = Stream_GetPosition(s);
385 *pSize = (UINT32)DstSize;
388 BYTE* dst = Stream_Buffer(s);
389 Stream_Free(s, FALSE);
394 Stream_Free(s, TRUE);
403WINPR_ATTR_MALLOC(free, 1)
404static
void* clipboard_synthesize_image_bmp(WINPR_ATTR_UNUSED wClipboard* clipboard,
405 UINT32 dstFormatId, const
void* data, UINT32* pSize)
407 UINT32 SrcSize = *pSize;
409 if (dstFormatId == CF_DIB)
414 wStream sbuffer = WINPR_C_ARRAY_INIT;
417 wStream* s = Stream_StaticConstInit(&sbuffer, data, SrcSize);
418 if (!readBitmapInfoHeader(s, &header, &offset))
421 return clipboard_prepend_bmp_header(&header, offset, data, SrcSize, pSize);
423#if defined(WINPR_UTILS_IMAGE_DIBv5)
424 else if (dstFormatId == CF_DIBV5)
427 "Unuspported destination format %s [0x%04" PRIx32
428 "], trying to convert from %s [0x%04" PRIx32
"]",
429 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
430 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
436 "Unuspported destination format %s [0x%04" PRIx32
437 "], trying to convert from %s [0x%04" PRIx32
"]",
438 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
439 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
446static BOOL format_is_image(wClipboard* clipboard, UINT32 formatId)
456 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
458 const char* mime = mime_images[x];
459 const UINT32
id = ClipboardRegisterFormat(clipboard, mime);
468static UINT32 image_format(WINPR_ATTR_UNUSED wClipboard* clipboard,
469 WINPR_ATTR_UNUSED UINT32 formatId)
471#if defined(WINPR_UTILS_IMAGE_PNG)
472 if (formatId == ClipboardRegisterFormat(clipboard, mime_png))
473 return WINPR_IMAGE_PNG;
475#if defined(WINPR_UTILS_IMAGE_JPEG)
476 if (formatId == ClipboardRegisterFormat(clipboard, mime_jpeg))
477 return WINPR_IMAGE_JPEG;
479#if defined(WINPR_UTILS_IMAGE_WEBP)
480 if (formatId == ClipboardRegisterFormat(clipboard, mime_webp))
481 return WINPR_IMAGE_WEBP;
483 return WINPR_IMAGE_BITMAP;
486WINPR_ATTR_MALLOC(free, 1)
487static
void* clipboard_synthesize_image_dib_to_format(wClipboard* clipboard, UINT32 dstFormatId,
488 const
void* data, UINT32* pSize)
490 WINPR_ASSERT(clipboard);
494 if ((clipboard->formatId != CF_DIB) && (clipboard->formatId != CF_DIBV5))
497 "Unuspported source format %s [0x%04" PRIx32
498 "], trying to convert to %s [0x%04" PRIx32
"]",
500 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
501 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
504 if (!format_is_image(clipboard, dstFormatId))
507 "Unuspported destination format %s [0x%04" PRIx32
508 "], trying to convert from %s [0x%04" PRIx32
"]",
510 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
511 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
515 const UINT32 format = image_format(clipboard, dstFormatId);
517 void* result =
nullptr;
519 wImage* img = winpr_image_new();
520 void* bmp = clipboard_synthesize_image_bmp(clipboard, clipboard->formatId, data, pSize);
521 const UINT32 SrcSize = *pSize;
527 if (winpr_image_read_buffer(img, bmp, SrcSize) <= 0)
530 result = winpr_image_write_buffer(img, format, &dsize);
533 if (dsize <= UINT32_MAX)
534 *pSize = (UINT32)dsize;
544 winpr_image_free(img, TRUE);
553WINPR_ATTR_MALLOC(free, 1)
554static
void* clipboard_synthesize_cf_dib(const
void* pvdata,
size_t dataLen, UINT32* pSize)
557 const BYTE* data = pvdata;
560 wStream sbuffer = WINPR_C_ARRAY_INIT;
561 wStream* s = Stream_StaticConstInit(&sbuffer, data, dataLen);
562 if (!readBitmapFileHeader(s, &pFileHeader))
566 BYTE* pDstData = (BYTE*)calloc(DstSize, 1);
572 memcpy(pDstData, src, DstSize);
573 *pSize = WINPR_ASSERTING_INT_CAST(UINT32, DstSize);
577WINPR_ATTR_MALLOC(free, 1)
578static
void* clipboard_synthesize_image_format_to_cf_dib(wClipboard* clipboard, UINT32 dstFormatId,
579 const
void* data, UINT32* pSize)
581 WINPR_ASSERT(clipboard);
585 if ((dstFormatId != CF_DIB) && (dstFormatId != CF_DIBV5))
588 "Unuspported destination format %s [0x%04" PRIx32
589 "], trying to convert from %s [0x%04" PRIx32
"]",
590 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId,
591 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
595 if (!format_is_image(clipboard, dstFormatId))
598 "Unuspported source format %s [0x%04" PRIx32
599 "], trying to convert to %s [0x%04" PRIx32
"]",
600 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
601 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
605 void* result =
nullptr;
607 const UINT32 SrcSize = *pSize;
609 wImage* image = winpr_image_new();
613 const int res = winpr_image_read_buffer(image, data, SrcSize);
617 dst = winpr_image_write_buffer(image, WINPR_IMAGE_BITMAP, &size);
621 result = clipboard_synthesize_cf_dib(dst, size, pSize);
624 winpr_image_free(image, TRUE);
634WINPR_ATTR_MALLOC(free, 1)
635static
void* clipboard_synthesize_image_format_to_format(wClipboard* clipboard, UINT32 dstFormatId,
636 const
void* data, UINT32* pSize)
638 WINPR_ASSERT(clipboard);
642 if (!format_is_image(clipboard, clipboard->formatId) ||
643 !format_is_image(clipboard, dstFormatId))
645 WLog_ERR(TAG,
"Unsupported conversion from %s [0x%04" PRIx32
"] to %s [0x%04" PRIx32
"]",
646 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
647 ClipboardGetFormatName(clipboard, dstFormatId), dstFormatId);
651 void* result =
nullptr;
653 const UINT32 SrcSize = *pSize;
656 wImage* image = winpr_image_new();
660 if (winpr_image_read_buffer(image, data, SrcSize) <= 0)
663 result = winpr_image_write_buffer(image, image_format(clipboard, dstFormatId), &size);
666 if (size <= UINT32_MAX)
667 *pSize = (UINT32)size;
676 winpr_image_free(image, TRUE);
685WINPR_ATTR_MALLOC(free, 1)
686static
void* clipboard_synthesize_ms_html_format(wClipboard* clipboard, UINT32 formatId,
687 const
void* pData, UINT32* pSize)
689 if (formatId != ClipboardGetFormatId(clipboard, mime_ms_html))
692 "Unuspported destination format %s [0x%04" PRIx32
693 "], trying to convert from %s [0x%04" PRIx32
"]",
694 ClipboardGetFormatName(clipboard, formatId), formatId,
695 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
698 if (clipboard->formatId != ClipboardGetFormatId(clipboard, mime_html))
701 "Unuspported destination format %s [0x%04" PRIx32
702 "], trying to convert from %s [0x%04" PRIx32
"]",
703 ClipboardGetFormatName(clipboard, formatId), formatId,
704 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
714 char* pDstData =
nullptr;
716 pSrcData.cpv =
nullptr;
718 WINPR_ASSERT(clipboard);
721 if (formatId == ClipboardGetFormatId(clipboard, mime_ms_html))
723 const size_t SrcSize = (size_t)*pSize;
724 const size_t DstSize = SrcSize + 200;
725 char* body =
nullptr;
726 char num[20] = WINPR_C_ARRAY_INIT;
729 pSrcData.pv = calloc(1, SrcSize + 1);
732 memcpy(pSrcData.pv, pData, SrcSize);
736 if (SrcSize > INT_MAX)
740 if ((pSrcData.cpb[0] == 0xFE) && (pSrcData.cpb[1] == 0xFF))
742 if (!ByteSwapUnicode(pSrcData.pv, (SrcSize / 2)))
747 if ((pSrcData.cpb[0] == 0xFF) && (pSrcData.cpb[1] == 0xFE))
749 char* utfString = ConvertWCharNToUtf8Alloc(&pSrcData.pv[1],
750 (SrcSize /
sizeof(WCHAR)) - 1,
nullptr);
752 pSrcData.cpc = utfString;
758 pDstData = (
char*)calloc(1, DstSize);
763 (void)sprintf_s(pDstData, DstSize,
765 "StartHTML:0000000000\r\n"
766 "EndHTML:0000000000\r\n"
767 "StartFragment:0000000000\r\n"
768 "EndFragment:0000000000\r\n");
769 body = strstr(pSrcData.cpc,
"<body");
772 body = strstr(pSrcData.cpc,
"<BODY");
775 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, DstSize));
776 CopyMemory(&pDstData[23], num, 10);
780 if (!winpr_str_append(
"<HTML><BODY>", pDstData, DstSize,
nullptr))
784 if (!winpr_str_append(
"<!--StartFragment-->", pDstData, DstSize,
nullptr))
788 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, SrcSize + 200));
789 CopyMemory(&pDstData[69], num, 10);
791 if (!winpr_str_append(pSrcData.cpc, pDstData, DstSize,
nullptr))
795 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, SrcSize + 200));
796 CopyMemory(&pDstData[93], num, 10);
798 if (!winpr_str_append(
"<!--EndFragment-->", pDstData, DstSize,
nullptr))
803 if (!winpr_str_append(
"</BODY></HTML>", pDstData, DstSize,
nullptr))
808 (void)sprintf_s(num,
sizeof(num),
"%010" PRIuz
"", strnlen(pDstData, DstSize));
809 CopyMemory(&pDstData[43], num, 10);
810 *pSize = (UINT32)strnlen(pDstData, DstSize) + 1;
817WINPR_ATTR_MALLOC(free, 1)
818static
char* html_pre_write(
wStream* s, const
char* what)
820 const size_t len = strlen(what);
821 Stream_Write(s, what, len);
822 char* startHTML = Stream_PointerAs(s,
char);
823 for (
size_t x = 0; x < 10; x++)
824 Stream_Write_INT8(s,
'0');
825 Stream_Write(s,
"\r\n", 2);
829static void html_fill_number(
char* pos,
size_t val)
831 char str[11] = WINPR_C_ARRAY_INIT;
832 (void)_snprintf(str,
sizeof(str),
"%010" PRIuz, val);
833 memcpy(pos, str, 10);
836WINPR_ATTR_MALLOC(free, 1)
837static
void* clipboard_wrap_html(const
char* mime, const
char* idata,
size_t ilength,
846 char* b64 = b64_encode((
const BYTE*)idata, ilength, &b64len);
850 const size_t mimelen = strlen(mime);
851 wStream* s = Stream_New(
nullptr, b64len + 225 + mimelen);
858 char* startHTML = html_pre_write(s,
"Version:0.9\r\nStartHTML:");
859 char* endHTML = html_pre_write(s,
"EndHTML:");
860 char* startFragment = html_pre_write(s,
"StartFragment:");
861 char* endFragment = html_pre_write(s,
"EndFragment:");
863 html_fill_number(startHTML, Stream_GetPosition(s));
864 const char html[] =
"<html><!--StartFragment-->";
865 Stream_Write(s, html, strnlen(html,
sizeof(html)));
867 html_fill_number(startFragment, Stream_GetPosition(s));
869 const char body[] =
"<body><img alt=\"FreeRDP clipboard image\" src=\"data:";
870 Stream_Write(s, body, strnlen(body,
sizeof(body)));
872 Stream_Write(s, mime, mimelen);
874 const char base64[] =
";base64,";
875 Stream_Write(s, base64, strnlen(base64,
sizeof(base64)));
876 Stream_Write(s, b64, b64len);
878 const char end[] =
"\"/></body>";
879 Stream_Write(s, end, strnlen(end,
sizeof(end)));
881 html_fill_number(endFragment, Stream_GetPosition(s));
883 const char fragend[] =
"<!--EndFragment--></html>";
884 Stream_Write(s, fragend, strnlen(fragend,
sizeof(fragend)));
885 html_fill_number(endHTML, Stream_GetPosition(s));
887 void* res = Stream_Buffer(s);
888 const size_t pos = Stream_GetPosition(s);
889 *plen = WINPR_ASSERTING_INT_CAST(uint32_t, pos);
890 Stream_Free(s, FALSE);
895WINPR_ATTR_MALLOC(free, 1)
896static
void* clipboard_wrap_format_to_html(uint32_t bmpFormat, const
char* idata,
size_t ilength,
900 wImage* img = winpr_image_new();
904 if (winpr_image_read_buffer(img, (
const BYTE*)idata, ilength) <= 0)
909 void* bmp = winpr_image_write_buffer(img, bmpFormat, &bmpsize);
913 res = clipboard_wrap_html(winpr_image_format_mime(bmpFormat), bmp, bmpsize, plen);
917 winpr_image_free(img, TRUE);
921WINPR_ATTR_MALLOC(free, 1)
922static
void* clipboard_wrap_bmp_to_ms_html(const
char* idata,
size_t ilength, uint32_t* plen)
924 const uint32_t formats[] = { WINPR_IMAGE_WEBP, WINPR_IMAGE_PNG, WINPR_IMAGE_JPEG };
926 for (
size_t x = 0; x < ARRAYSIZE(formats); x++)
928 const uint32_t format = formats[x];
929 if (winpr_image_format_is_supported(format))
931 return clipboard_wrap_format_to_html(format, idata, ilength, plen);
934 const uint32_t bmpFormat = WINPR_IMAGE_BITMAP;
935 return clipboard_wrap_html(winpr_image_format_mime(bmpFormat), idata, ilength, plen);
938WINPR_ATTR_MALLOC(free, 1)
939static
void* clipboard_synthesize_image_ms_html(WINPR_ATTR_UNUSED wClipboard* clipboard,
940 UINT32 formatId, const
void* data, UINT32* pSize)
942 if (formatId != ClipboardGetFormatId(clipboard, mime_ms_html))
945 "Unuspported destination format %s [0x%04" PRIx32
946 "], trying to convert from %s [0x%04" PRIx32
"]",
947 ClipboardGetFormatName(clipboard, formatId), formatId,
948 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
954 const size_t datalen = *pSize;
956 switch (clipboard->formatId)
959 return clipboard_wrap_html(mime_tiff, data, datalen, pSize);
963 uint32_t bmplen = *pSize;
965 clipboard_synthesize_image_bmp(clipboard, clipboard->formatId, data, &bmplen);
968 WLog_WARN(TAG,
"failed to convert formatId 0x%08" PRIx32
" [%s]",
970 ClipboardGetFormatName(clipboard, clipboard->formatId));
975 void* res = clipboard_wrap_bmp_to_ms_html(bmp, bmplen, pSize);
981#if defined(WINPR_UTILS_IMAGE_WEBP)
982 const uint32_t idWebp = ClipboardRegisterFormat(clipboard, mime_webp);
983 if (clipboard->formatId == idWebp)
984 return clipboard_wrap_html(mime_webp, data, datalen, pSize);
987#if defined(WINPR_UTILS_IMAGE_PNG)
988 const uint32_t idPng = ClipboardRegisterFormat(clipboard, mime_png);
989 if (clipboard->formatId == idPng)
990 return clipboard_wrap_html(mime_png, data, datalen, pSize);
992#if defined(WINPR_UTILS_IMAGE_JPEG)
993 const uint32_t idJpeg = ClipboardRegisterFormat(clipboard, mime_jpeg);
994 if (clipboard->formatId == idJpeg)
995 return clipboard_wrap_html(mime_jpeg, data, datalen, pSize);
998 const uint32_t idTiff = ClipboardRegisterFormat(clipboard, mime_tiff);
999 if (clipboard->formatId == idTiff)
1000 return clipboard_wrap_html(mime_tiff, data, datalen, pSize);
1002 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
1004 const char* mime = mime_bitmap[x];
1005 const uint32_t
id = ClipboardRegisterFormat(clipboard, mime);
1008 return clipboard_wrap_bmp_to_ms_html(data, datalen, pSize);
1011 WLog_WARN(TAG,
"Unsupported image format id 0x%08" PRIx32
" [%s]", clipboard->formatId,
1012 ClipboardGetFormatName(clipboard, clipboard->formatId));
1024WINPR_ATTR_MALLOC(free, 1)
1025static
void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 formatId,
1026 const
void* data, UINT32* pSize)
1028 if (formatId != ClipboardGetFormatId(clipboard, mime_html))
1031 "Unuspported destination format %s [0x%04" PRIx32
1032 "], trying to convert from %s [0x%04" PRIx32
"]",
1033 ClipboardGetFormatName(clipboard, formatId), formatId,
1034 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
1037 if (clipboard->formatId != ClipboardGetFormatId(clipboard, mime_ms_html))
1040 "Unuspported destination format %s [0x%04" PRIx32
1041 "], trying to convert from %s [0x%04" PRIx32
"]",
1042 ClipboardGetFormatName(clipboard, formatId), formatId,
1043 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId);
1047 char* pDstData =
nullptr;
1049 if (formatId == ClipboardGetFormatId(clipboard, mime_html))
1051 const char* str = (
const char*)data;
1052 const size_t SrcSize = *pSize;
1053 const char* begStr = strstr(str,
"StartHTML:");
1054 const char* endStr = strstr(str,
"EndHTML:");
1056 if (!begStr || !endStr)
1060 const long beg = strtol(&begStr[10],
nullptr, 10);
1065 const long end = strtol(&endStr[8],
nullptr, 10);
1067 if ((beg < 0) || (end < 0) || ((
size_t)beg > SrcSize) || ((
size_t)end > SrcSize) ||
1068 (beg >= end) || (errno != 0))
1071 const size_t DstSize = (size_t)(end - beg);
1072 pDstData = calloc(DstSize + 1,
sizeof(
char));
1077 CopyMemory(pDstData, &str[beg], DstSize);
1078 const size_t rc = ConvertLineEndingToLF(pDstData, DstSize);
1079 WINPR_ASSERT(rc <= UINT32_MAX);
1080 *pSize = (UINT32)rc;
1086BOOL ClipboardInitSynthesizers(wClipboard* clipboard)
1088 WINPR_ASSERT(clipboard);
1090 const UINT32 formatIdUtf = ClipboardRegisterFormat(clipboard, mime_text_utf8);
1091 const UINT32 formatIdUtf8String = ClipboardRegisterFormat(clipboard, mime_text_UTF8_STRING);
1092 const UINT32 formatIdPlain = ClipboardRegisterFormat(clipboard, mime_text_plain);
1093 const UINT32 textFormatIds[] = { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT,
1094 formatIdUtf, formatIdUtf8String, formatIdPlain };
1098 for (
size_t x = 0; x < ARRAYSIZE(textFormatIds); x++)
1100 const UINT32 formatId = textFormatIds[x];
1102 for (
size_t y = 0; y < ARRAYSIZE(textFormatIds); y++)
1104 const UINT32 dstFormatId = textFormatIds[y];
1105 if (formatId == dstFormatId)
1108 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, dstFormatId,
1109 clipboard_synthesize_string))
1113 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, CF_LOCALE,
1114 clipboard_synthesize_cf_locale))
1118 const uint32_t msHtmlFormat = ClipboardRegisterFormat(clipboard, mime_ms_html);
1123 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_TIFF, msHtmlFormat,
1124 clipboard_synthesize_image_ms_html))
1130 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, CF_DIB,
1131 clipboard_synthesize_image_dib_to_format))
1133 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIB, msHtmlFormat,
1134 clipboard_synthesize_image_ms_html))
1137#if defined(WINPR_UTILS_IMAGE_DIBv5)
1138 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, CF_DIB,
1139 clipboard_synthesize_image_dib_to_format))
1141 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, msHtmlFormat,
1142 clipboard_synthesize_image_ms_html))
1149 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
1151 const char* mime = mime_images[x];
1152 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime);
1153 if (altFormatId == 0)
1155 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIB, altFormatId,
1156 clipboard_synthesize_image_dib_to_format))
1158 if (!ClipboardRegisterSynthesizerEx(clipboard, altFormatId, CF_DIB,
1159 clipboard_synthesize_image_format_to_cf_dib))
1161 if (!ClipboardRegisterSynthesizerEx(clipboard, altFormatId, msHtmlFormat,
1162 clipboard_synthesize_image_ms_html))
1164#if defined(WINPR_UTILS_IMAGE_DIBv5)
1165 if (!ClipboardRegisterSynthesizerEx(clipboard, CF_DIBV5, altFormatId,
1166 clipboard_synthesize_image_dib_to_format))
1168 if (!ClipboardRegisterSynthesizerEx(clipboard, altFormatId, CF_DIBV5,
1169 clipboard_synthesize_image_format_to_cf_dib))
1174 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
1176 const char* mime = mime_images[x];
1177 if (strcmp(mime, mime_tiff) == 0)
1179 const UINT32 formatId = ClipboardRegisterFormat(clipboard, mime);
1180 for (
size_t y = 0; y < ARRAYSIZE(mime_images); y++)
1182 const char* altMime = mime_images[y];
1183 if ((x == y) || (strcmp(altMime, mime_tiff) == 0))
1185 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, altMime);
1186 if ((formatId == 0) || (altFormatId == 0))
1188 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, altFormatId,
1189 clipboard_synthesize_image_format_to_format))
1199 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_html);
1200 if (!ClipboardRegisterSynthesizerEx(clipboard, msHtmlFormat, altFormatId,
1201 clipboard_synthesize_html_format))
1209 UINT32 formatId = ClipboardRegisterFormat(clipboard, mime_html);
1213 const UINT32 altFormatId = ClipboardRegisterFormat(clipboard, mime_ms_html);
1214 if (!ClipboardRegisterSynthesizerEx(clipboard, formatId, altFormatId,
1215 clipboard_synthesize_ms_html_format))