3#include <winpr/print.h>
4#include <winpr/image.h>
5#include <winpr/clipboard.h>
6#include <winpr/stream.h>
9static BOOL test_dib_to_bmp(
const BYTE* dib,
size_t dibSize,
size_t expectedOffset)
13 wClipboard* clipboard = ClipboardCreate();
17 const UINT32 bmpId = ClipboardRegisterFormat(clipboard,
"image/bmp");
18 if ((bmpId == 0) || (dibSize > UINT32_MAX) ||
19 !ClipboardSetData(clipboard, CF_DIB, dib, (UINT32)dibSize))
23 BYTE* bmp = ClipboardGetData(clipboard, bmpId, &bmpSize);
27 wStream bmpBuffer = WINPR_C_ARRAY_INIT;
28 wStream* s = Stream_StaticConstInit(&bmpBuffer, bmp, bmpSize);
33 UINT32 bitmapOffset = 0;
34 Stream_Read_UINT32(s, bitmapOffset);
35 if ((bitmapOffset != expectedOffset) ||
41 wImage* image = winpr_image_new();
44 rc = (winpr_image_read_buffer(image, bmp, bmpSize) > 0);
45 winpr_image_free(image, TRUE);
50 ClipboardDestroy(clipboard);
54static void write_dib_info_header(
wStream* s, UINT32 size, UINT16 bpp, UINT32 compression)
58 Stream_Write_UINT32(s, size);
59 Stream_Write_INT32(s, 1);
60 Stream_Write_INT32(s, 1);
61 Stream_Write_UINT16(s, 1);
62 Stream_Write_UINT16(s, bpp);
63 Stream_Write_UINT32(s, compression);
64 Stream_Write_UINT32(s, 4);
68static BOOL test_dib_offsets(
void)
71 wStream sbuffer = WINPR_C_ARRAY_INIT;
72 wStream* s = Stream_StaticInit(&sbuffer, v4,
sizeof(v4));
75 write_dib_info_header(s,
sizeof(
BITMAPV4HEADER), 32, BI_BITFIELDS);
77 Stream_Write_UINT32(s, 0x00FF0000);
78 Stream_Write_UINT32(s, 0x0000FF00);
79 Stream_Write_UINT32(s, 0x000000FF);
80 Stream_Write_UINT32(s, 0xFF000000);
82 Stream_Write_UINT32(s, 0xFF123456);
86 BYTE bitfields[
sizeof(
BITMAPINFOHEADER) + 3 *
sizeof(DWORD) + 4] = WINPR_C_ARRAY_INIT;
87 s = Stream_StaticInit(&sbuffer, bitfields,
sizeof(bitfields));
91 Stream_Write_UINT32(s, 0x00FF0000);
92 Stream_Write_UINT32(s, 0x0000FF00);
93 Stream_Write_UINT32(s, 0x000000FF);
94 Stream_Write_UINT32(s, 0xFF123456);
95 if (!test_dib_to_bmp(bitfields,
sizeof(bitfields),
101 s = Stream_StaticInit(&sbuffer, paletted,
sizeof(paletted));
105 Stream_Zero(s, 256 *
sizeof(
RGBQUAD));
106 Stream_Write_UINT32(s, 0);
107 return test_dib_to_bmp(paletted,
sizeof(paletted),
112static BOOL test_text_leading_newline(
void)
114 const char* tests[][2] = { {
"\nfoo",
"\r\nfoo" }, {
"\n",
"\r\n" } };
117 wClipboard* clipboard = ClipboardCreate();
121 const UINT32 textId = ClipboardRegisterFormat(clipboard,
"text/plain");
122 for (
size_t x = 0; x < ARRAYSIZE(tests); x++)
125 if (!ClipboardSetData(clipboard, textId, tests[x][0], (UINT32)strlen(tests[x][0])))
129 WCHAR* wstr = ClipboardGetData(clipboard, CF_UNICODETEXT, &size);
130 char* str = ConvertWCharNToUtf8Alloc(wstr, size /
sizeof(WCHAR),
nullptr);
131 const BOOL match = str && (strcmp(str, tests[x][1]) == 0);
136 (void)fprintf(stderr,
"text/plain to CF_UNICODETEXT failed for case %" PRIuz
"\n", x);
143 ClipboardDestroy(clipboard);
147int TestClipboardFormats(
int argc,
char* argv[])
151 UINT32* pFormatIds =
nullptr;
152 const char* formatName =
nullptr;
153 wClipboard* clipboard =
nullptr;
154 UINT32 utf8StringFormatId = 0;
159 clipboard = ClipboardCreate();
162 if (!test_dib_offsets())
164 if (!test_text_leading_newline())
167 const char* mime_types[] = {
"text/html",
"text/html",
"image/bmp",
168 "image/png",
"image/webp",
"image/jpeg" };
169 for (
size_t x = 0; x < ARRAYSIZE(mime_types); x++)
171 const char* mime = mime_types[x];
172 UINT32
id = ClipboardRegisterFormat(clipboard, mime);
173 (void)fprintf(stderr,
"ClipboardRegisterFormat(%s) -> 0x%08" PRIx32
"\n", mime,
id);
178 utf8StringFormatId = ClipboardRegisterFormat(clipboard,
"UTF8_STRING");
179 pFormatIds =
nullptr;
180 count = ClipboardGetRegisteredFormatIds(clipboard, &pFormatIds);
182 for (UINT32 index = 0; index < count; index++)
184 UINT32 formatId = pFormatIds[index];
185 formatName = ClipboardGetFormatName(clipboard, formatId);
186 (void)fprintf(stderr,
"Format: 0x%08" PRIX32
" %s\n", formatId, formatName);
196 const char pSrcData[] =
"this is a test string";
197 char* pDstData =
nullptr;
199 SrcSize = (UINT32)(strnlen(pSrcData, ARRAYSIZE(pSrcData)) + 1);
200 bSuccess = ClipboardSetData(clipboard, utf8StringFormatId, pSrcData, SrcSize);
201 (void)fprintf(stderr,
"ClipboardSetData: %" PRId32
"\n", bSuccess);
203 pDstData = (
char*)ClipboardGetData(clipboard, utf8StringFormatId, &DstSize);
204 (void)fprintf(stderr,
"ClipboardGetData: %s\n", pDstData);
211 char* pSrcData =
nullptr;
212 WCHAR* pDstData =
nullptr;
214 pDstData = (WCHAR*)ClipboardGetData(clipboard, CF_UNICODETEXT, &DstSize);
215 pSrcData = ConvertWCharNToUtf8Alloc(pDstData, DstSize /
sizeof(WCHAR),
nullptr);
217 (void)fprintf(stderr,
"ClipboardGetData (synthetic): %s\n", pSrcData);
222 pFormatIds =
nullptr;
223 count = ClipboardGetFormatIds(clipboard, &pFormatIds);
225 for (UINT32 index = 0; index < count; index++)
227 UINT32 formatId = pFormatIds[index];
228 formatName = ClipboardGetFormatName(clipboard, formatId);
229 (void)fprintf(stderr,
"Format: 0x%08" PRIX32
" %s\n", formatId, formatName);
234 const char* name = TEST_CLIP_BMP;
235 BOOL bSuccess = FALSE;
236 UINT32 idBmp = ClipboardRegisterFormat(clipboard,
"image/bmp");
238 wImage* img = winpr_image_new();
242 if (winpr_image_read(img, name) <= 0)
244 winpr_image_free(img, TRUE);
249 void* data = winpr_image_write_buffer(img, WINPR_IMAGE_BITMAP, &bmpsize);
250 bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
251 (void)fprintf(stderr,
"ClipboardSetData: %" PRId32
"\n", bSuccess);
254 winpr_image_free(img, TRUE);
262 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
263 (void)fprintf(stderr,
"ClipboardGetData: [CF_DIB] %p [%" PRIu32
"]\n", pDstData,
267 bSuccess = ClipboardSetData(clipboard,
id, pDstData, DstSize);
273 const uint32_t
id = ClipboardGetFormatId(clipboard,
"HTML Format");
275 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
279 FILE* fp = fopen(
"test.html",
"w");
282 (void)fwrite(pDstData, 1, DstSize, fp);
289 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/bmp");
292 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
293 (void)fprintf(stderr,
"ClipboardGetData: [image/bmp] %p [%" PRIu32
"]\n", pDstData,
298 if (DstSize != bmpsize)
302#if defined(WINPR_UTILS_IMAGE_PNG)
304 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/png");
307 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
308 (void)fprintf(stderr,
"ClipboardGetData: [image/png] %p\n", pDstData);
314 const char* name = TEST_CLIP_PNG;
315 BOOL bSuccess = FALSE;
316 UINT32 idBmp = ClipboardRegisterFormat(clipboard,
"image/png");
318 wImage* img = winpr_image_new();
322 if (winpr_image_read(img, name) <= 0)
324 winpr_image_free(img, TRUE);
329 void* data = winpr_image_write_buffer(img, WINPR_IMAGE_PNG, &bmpsize);
330 bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
331 (void)fprintf(stderr,
"ClipboardSetData: %" PRId32
"\n", bSuccess);
334 winpr_image_free(img, TRUE);
342 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
343 (void)fprintf(stderr,
"ClipboardGetData: [CF_DIB] %p [%" PRIu32
"]\n", pDstData,
347 bSuccess = ClipboardSetData(clipboard,
id, pDstData, DstSize);
354#if defined(WINPR_UTILS_IMAGE_WEBP)
356 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/webp");
359 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
360 (void)fprintf(stderr,
"ClipboardGetData: [image/webp] %p\n", pDstData);
367#if defined(WINPR_UTILS_IMAGE_JPEG)
369 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/jpeg");
372 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
373 (void)fprintf(stderr,
"ClipboardGetData: [image/jpeg] %p\n", pDstData);
385 ClipboardDestroy(clipboard);