FreeRDP
Loading...
Searching...
No Matches
clear.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/print.h>
26#include <winpr/bitstream.h>
27
28#include <freerdp/codec/color.h>
29#include <freerdp/codec/clear.h>
30#include <freerdp/log.h>
31
32#define TAG FREERDP_TAG("codec.clear")
33
34#define CLEARCODEC_FLAG_GLYPH_INDEX 0x01
35#define CLEARCODEC_FLAG_GLYPH_HIT 0x02
36#define CLEARCODEC_FLAG_CACHE_RESET 0x04
37
38#define CLEARCODEC_VBAR_SIZE 32768
39#define CLEARCODEC_VBAR_SHORT_SIZE 16384
40
41typedef struct
42{
43 UINT32 size;
44 UINT32 count;
45 UINT32* pixels;
46} CLEAR_GLYPH_ENTRY;
47
48typedef struct
49{
50 UINT32 size;
51 UINT32 count;
52 BYTE* pixels;
53} CLEAR_VBAR_ENTRY;
54
55struct S_CLEAR_CONTEXT
56{
57 BOOL Compressor;
58 NSC_CONTEXT* nsc;
59 UINT32 seqNumber;
60 BYTE* TempBuffer;
61 size_t TempSize;
62 UINT32 nTempStep;
63 UINT32 TempFormat;
64 UINT32 format;
65 CLEAR_GLYPH_ENTRY GlyphCache[4000];
66 UINT32 VBarStorageCursor;
67 CLEAR_VBAR_ENTRY VBarStorage[CLEARCODEC_VBAR_SIZE];
68 UINT32 ShortVBarStorageCursor;
69 CLEAR_VBAR_ENTRY ShortVBarStorage[CLEARCODEC_VBAR_SHORT_SIZE];
70 wLog* log;
71};
72
73static const UINT32 CLEAR_LOG2_FLOOR[256] = {
74 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
75 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
76 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
77 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
78 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
79 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
80 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
81 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
82};
83
84static const BYTE CLEAR_8BIT_MASKS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
85
86static void clear_reset_vbar_storage(CLEAR_CONTEXT* WINPR_RESTRICT clear, BOOL zero)
87{
88 if (zero)
89 {
90 for (size_t i = 0; i < ARRAYSIZE(clear->VBarStorage); i++)
91 winpr_aligned_free(clear->VBarStorage[i].pixels);
92
93 ZeroMemory(clear->VBarStorage, sizeof(clear->VBarStorage));
94 }
95
96 clear->VBarStorageCursor = 0;
97
98 if (zero)
99 {
100 for (size_t i = 0; i < ARRAYSIZE(clear->ShortVBarStorage); i++)
101 winpr_aligned_free(clear->ShortVBarStorage[i].pixels);
102
103 ZeroMemory(clear->ShortVBarStorage, sizeof(clear->ShortVBarStorage));
104 }
105
106 clear->ShortVBarStorageCursor = 0;
107}
108
109static void clear_reset_glyph_cache(CLEAR_CONTEXT* WINPR_RESTRICT clear)
110{
111 for (size_t i = 0; i < ARRAYSIZE(clear->GlyphCache); i++)
112 winpr_aligned_free(clear->GlyphCache[i].pixels);
113
114 ZeroMemory(clear->GlyphCache, sizeof(clear->GlyphCache));
115}
116
117static BOOL convert_color(BYTE* WINPR_RESTRICT dst, UINT32 nDstStep, UINT32 DstFormat, UINT32 nXDst,
118 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
119 const BYTE* WINPR_RESTRICT src, UINT32 nSrcStep, UINT32 SrcFormat,
120 UINT32 nDstWidth, UINT32 nDstHeight,
121 const gdiPalette* WINPR_RESTRICT palette)
122{
123 if (nWidth + nXDst > nDstWidth)
124 nWidth = nDstWidth - nXDst;
125
126 if (nHeight + nYDst > nDstHeight)
127 nHeight = nDstHeight - nYDst;
128
129 return freerdp_image_copy_no_overlap(dst, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
130 src, SrcFormat, nSrcStep, 0, 0, palette,
131 FREERDP_KEEP_DST_ALPHA);
132}
133
134static BOOL clear_decompress_nscodec(wLog* log, NSC_CONTEXT* WINPR_RESTRICT nsc, UINT32 width,
135 UINT32 height, wStream* WINPR_RESTRICT s,
136 UINT32 bitmapDataByteCount, BYTE* WINPR_RESTRICT pDstData,
137 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDstRel,
138 UINT32 nYDstRel, UINT32 nDstWidth, UINT32 nDstHeight)
139{
140 BOOL rc = 0;
141
142 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, bitmapDataByteCount))
143 return FALSE;
144
145 rc = nsc_process_message(nsc, 32, width, height, Stream_Pointer(s), bitmapDataByteCount,
146 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel, nDstWidth,
147 nDstHeight, FREERDP_FLIP_NONE);
148 Stream_Seek(s, bitmapDataByteCount);
149 return rc;
150}
151
152static BOOL clear_decompress_subcode_rlex(wLog* log, wStream* WINPR_RESTRICT s,
153 UINT32 bitmapDataByteCount, UINT32 width, UINT32 height,
154 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
155 UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel,
156 UINT32 nDstWidth, UINT32 nDstHeight)
157{
158 UINT32 x = 0;
159 UINT32 y = 0;
160 UINT32 pixelCount = 0;
161 UINT32 bitmapDataOffset = 0;
162 size_t pixelIndex = 0;
163 UINT32 numBits = 0;
164 BYTE startIndex = 0;
165 BYTE stopIndex = 0;
166 BYTE suiteIndex = 0;
167 BYTE suiteDepth = 0;
168 BYTE paletteCount = 0;
169 UINT32 palette[128] = WINPR_C_ARRAY_INIT;
170
171 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, bitmapDataByteCount))
172 return FALSE;
173
174 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 1))
175 return FALSE;
176 Stream_Read_UINT8(s, paletteCount);
177 bitmapDataOffset = 1 + (paletteCount * 3);
178
179 if ((paletteCount > 127) || (paletteCount < 1))
180 {
181 WLog_Print(log, WLOG_ERROR, "paletteCount %" PRIu8 "", paletteCount);
182 return FALSE;
183 }
184
185 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, paletteCount, 3ull))
186 return FALSE;
187
188 for (UINT32 i = 0; i < paletteCount; i++)
189 {
190 BYTE r = 0;
191 BYTE g = 0;
192 BYTE b = 0;
193 Stream_Read_UINT8(s, b);
194 Stream_Read_UINT8(s, g);
195 Stream_Read_UINT8(s, r);
196 palette[i] = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
197 }
198
199 pixelIndex = 0;
200 pixelCount = width * height;
201 numBits = CLEAR_LOG2_FLOOR[paletteCount - 1] + 1;
202
203 while (bitmapDataOffset < bitmapDataByteCount)
204 {
205 UINT32 tmp = 0;
206 UINT32 color = 0;
207 UINT32 runLengthFactor = 0;
208
209 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 2))
210 return FALSE;
211
212 Stream_Read_UINT8(s, tmp);
213 Stream_Read_UINT8(s, runLengthFactor);
214 bitmapDataOffset += 2;
215 suiteDepth = (tmp >> numBits) & CLEAR_8BIT_MASKS[(8 - numBits)];
216 stopIndex = tmp & CLEAR_8BIT_MASKS[numBits];
217 startIndex = stopIndex - suiteDepth;
218
219 if (runLengthFactor >= 0xFF)
220 {
221 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 2))
222 return FALSE;
223
224 Stream_Read_UINT16(s, runLengthFactor);
225 bitmapDataOffset += 2;
226
227 if (runLengthFactor >= 0xFFFF)
228 {
229 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
230 return FALSE;
231
232 Stream_Read_UINT32(s, runLengthFactor);
233 bitmapDataOffset += 4;
234 }
235 }
236
237 if (startIndex >= paletteCount)
238 {
239 WLog_Print(log, WLOG_ERROR, "startIndex %" PRIu8 " > paletteCount %" PRIu8 "]",
240 startIndex, paletteCount);
241 return FALSE;
242 }
243
244 if (stopIndex >= paletteCount)
245 {
246 WLog_Print(log, WLOG_ERROR, "stopIndex %" PRIu8 " > paletteCount %" PRIu8 "]",
247 stopIndex, paletteCount);
248 return FALSE;
249 }
250
251 suiteIndex = startIndex;
252
253 if (suiteIndex > 127)
254 {
255 WLog_Print(log, WLOG_ERROR, "suiteIndex %" PRIu8 " > 127]", suiteIndex);
256 return FALSE;
257 }
258
259 color = palette[suiteIndex];
260
261 if ((pixelIndex + runLengthFactor) > pixelCount)
262 {
263 WLog_Print(log, WLOG_ERROR,
264 "pixelIndex %" PRIuz " + runLengthFactor %" PRIu32 " > pixelCount %" PRIu32
265 "",
266 pixelIndex, runLengthFactor, pixelCount);
267 return FALSE;
268 }
269
270 for (UINT32 i = 0; i < runLengthFactor; i++)
271 {
272 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
273 (nYDstRel + y) * nDstStep];
274
275 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
276 FreeRDPWriteColor(pTmpData, DstFormat, color);
277
278 if (++x >= width)
279 {
280 y++;
281 x = 0;
282 }
283 }
284
285 pixelIndex += runLengthFactor;
286
287 if ((pixelIndex + (suiteDepth + 1)) > pixelCount)
288 {
289 WLog_Print(log, WLOG_ERROR,
290 "pixelIndex %" PRIuz " + suiteDepth %" PRIu8 " + 1 > pixelCount %" PRIu32 "",
291 pixelIndex, suiteDepth, pixelCount);
292 return FALSE;
293 }
294
295 for (UINT32 i = 0; i <= suiteDepth; i++)
296 {
297 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
298 (nYDstRel + y) * nDstStep];
299 UINT32 ccolor = palette[suiteIndex];
300
301 if (suiteIndex > 127)
302 {
303 WLog_Print(log, WLOG_ERROR, "suiteIndex %" PRIu8 " > 127", suiteIndex);
304 return FALSE;
305 }
306
307 suiteIndex++;
308
309 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
310 FreeRDPWriteColor(pTmpData, DstFormat, ccolor);
311
312 if (++x >= width)
313 {
314 y++;
315 x = 0;
316 }
317 }
318
319 pixelIndex += (suiteDepth + 1);
320 }
321
322 if (pixelIndex != pixelCount)
323 {
324 WLog_Print(log, WLOG_ERROR, "pixelIndex %" PRIuz " != pixelCount %" PRIu32 "", pixelIndex,
325 pixelCount);
326 return FALSE;
327 }
328
329 return TRUE;
330}
331
332static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height)
333{
334 if (!clear)
335 return FALSE;
336
337 const UINT64 size = 1ull * (width + 16ull) * (height + 16ull);
338 const size_t bpp = FreeRDPGetBytesPerPixel(clear->format);
339 if (size > UINT32_MAX / bpp)
340 return FALSE;
341
342 if (size > clear->TempSize / bpp)
343 {
344 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer,
345 WINPR_ASSERTING_INT_CAST(size_t, size), bpp, 32);
346
347 if (!tmp)
348 {
349 WLog_Print(clear->log, WLOG_ERROR,
350 "clear->TempBuffer winpr_aligned_recalloc failed for %" PRIu64 " bytes",
351 size);
352 return FALSE;
353 }
354
355 clear->TempSize = WINPR_ASSERTING_INT_CAST(size_t, size* bpp);
356 clear->TempBuffer = tmp;
357 }
358
359 return TRUE;
360}
361
362static BOOL clear_decompress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
363 wStream* WINPR_RESTRICT s, UINT32 residualByteCount,
364 UINT32 nWidth, UINT32 nHeight,
365 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
366 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
367 UINT32 nDstWidth, UINT32 nDstHeight,
368 const gdiPalette* WINPR_RESTRICT palette)
369{
370 UINT32 nSrcStep = 0;
371 UINT32 suboffset = 0;
372 BYTE* dstBuffer = nullptr;
373 UINT32 pixelIndex = 0;
374 UINT32 pixelCount = 0;
375
376 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, residualByteCount))
377 return FALSE;
378
379 suboffset = 0;
380 pixelIndex = 0;
381 pixelCount = nWidth * nHeight;
382
383 if (!clear_resize_buffer(clear, nWidth, nHeight))
384 return FALSE;
385
386 dstBuffer = clear->TempBuffer;
387
388 while (suboffset < residualByteCount)
389 {
390 BYTE r = 0;
391 BYTE g = 0;
392 BYTE b = 0;
393 UINT32 runLengthFactor = 0;
394 UINT32 color = 0;
395
396 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 4))
397 return FALSE;
398
399 Stream_Read_UINT8(s, b);
400 Stream_Read_UINT8(s, g);
401 Stream_Read_UINT8(s, r);
402 Stream_Read_UINT8(s, runLengthFactor);
403 suboffset += 4;
404 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
405
406 if (runLengthFactor >= 0xFF)
407 {
408 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
409 return FALSE;
410
411 Stream_Read_UINT16(s, runLengthFactor);
412 suboffset += 2;
413
414 if (runLengthFactor >= 0xFFFF)
415 {
416 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 4))
417 return FALSE;
418
419 Stream_Read_UINT32(s, runLengthFactor);
420 suboffset += 4;
421 }
422 }
423
424 if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex)))
425 {
426 WLog_Print(clear->log, WLOG_ERROR,
427 "pixelIndex %" PRIu32 " + runLengthFactor %" PRIu32 " > pixelCount %" PRIu32
428 "",
429 pixelIndex, runLengthFactor, pixelCount);
430 return FALSE;
431 }
432
433 for (UINT32 i = 0; i < runLengthFactor; i++)
434 {
435 FreeRDPWriteColor(dstBuffer, clear->format, color);
436 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
437 }
438
439 pixelIndex += runLengthFactor;
440 }
441
442 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
443
444 if (pixelIndex != pixelCount)
445 {
446 WLog_Print(clear->log, WLOG_ERROR, "pixelIndex %" PRIu32 " != pixelCount %" PRIu32 "",
447 pixelIndex, pixelCount);
448 return FALSE;
449 }
450
451 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
452 clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight,
453 palette);
454}
455
456static BOOL clear_decompress_subcodecs_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
457 wStream* WINPR_RESTRICT s, UINT32 subcodecByteCount,
458 UINT32 nWidth, UINT32 nHeight,
459 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
460 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
461 UINT32 nDstWidth, UINT32 nDstHeight,
462 const gdiPalette* WINPR_RESTRICT palette)
463{
464 UINT32 suboffset = 0;
465
466 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, subcodecByteCount))
467 return FALSE;
468
469 while (suboffset < subcodecByteCount)
470 {
471 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 13))
472 return FALSE;
473
474 const UINT16 xStart = Stream_Get_UINT16(s);
475 const UINT16 yStart = Stream_Get_UINT16(s);
476 const UINT16 width = Stream_Get_UINT16(s);
477 const UINT16 height = Stream_Get_UINT16(s);
478 const UINT32 bitmapDataByteCount = Stream_Get_UINT32(s);
479 const UINT8 subcodecId = Stream_Get_UINT8(s);
480 suboffset += 13;
481
482 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, bitmapDataByteCount))
483 return FALSE;
484
485 const UINT32 nXDstRel = nXDst + xStart;
486 const UINT32 nYDstRel = nYDst + yStart;
487 if (1ull * nXDstRel + width > nDstWidth)
488 {
489 WLog_Print(clear->log, WLOG_ERROR,
490 "nXDstRel %" PRIu32 " + width %" PRIu16 " > nDstWidth %" PRIu32 "", nXDstRel,
491 width, nDstWidth);
492 return FALSE;
493 }
494 if (1ull * nYDstRel + height > nDstHeight)
495 {
496 WLog_Print(clear->log, WLOG_ERROR,
497 "nYDstRel %" PRIu32 " + height %" PRIu16 " > nDstHeight %" PRIu32 "",
498 nYDstRel, height, nDstHeight);
499 return FALSE;
500 }
501
502 if (1ull * xStart + width > nWidth)
503 {
504 WLog_Print(clear->log, WLOG_ERROR,
505 "xStart %" PRIu16 " + width %" PRIu16 " > nWidth %" PRIu32 "", xStart, width,
506 nWidth);
507 return FALSE;
508 }
509 if (1ull * yStart + height > nHeight)
510 {
511 WLog_Print(clear->log, WLOG_ERROR,
512 "yStart %" PRIu16 " + height %" PRIu16 " > nHeight %" PRIu32 "", yStart,
513 height, nHeight);
514 return FALSE;
515 }
516
517 if (!clear_resize_buffer(clear, width, height))
518 return FALSE;
519
520 switch (subcodecId)
521 {
522 case 0: /* Uncompressed */
523 {
524 const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24);
525 const size_t nSrcSize = 1ull * nSrcStep * height;
526
527 if (bitmapDataByteCount != nSrcSize)
528 {
529 WLog_Print(clear->log, WLOG_ERROR,
530 "bitmapDataByteCount %" PRIu32 " != nSrcSize %" PRIuz "",
531 bitmapDataByteCount, nSrcSize);
532 return FALSE;
533 }
534
535 if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height,
536 Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth,
537 nDstHeight, palette))
538 return FALSE;
539
540 Stream_Seek(s, bitmapDataByteCount);
541 }
542 break;
543
544 case 1: /* NSCodec */
545 if (!clear_decompress_nscodec(clear->log, clear->nsc, width, height, s,
546 bitmapDataByteCount, pDstData, DstFormat, nDstStep,
547 nXDstRel, nYDstRel, nDstWidth, nDstHeight))
548 return FALSE;
549
550 break;
551
552 case 2: /* CLEARCODEC_SUBCODEC_RLEX */
553 if (!clear_decompress_subcode_rlex(clear->log, s, bitmapDataByteCount, width,
554 height, pDstData, DstFormat, nDstStep, nXDstRel,
555 nYDstRel, nDstWidth, nDstHeight))
556 return FALSE;
557
558 break;
559
560 default:
561 WLog_Print(clear->log, WLOG_ERROR, "Unknown subcodec ID %" PRIu8 "", subcodecId);
562 return FALSE;
563 }
564
565 suboffset += bitmapDataByteCount;
566 }
567
568 return TRUE;
569}
570
571static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear,
572 CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry)
573{
574 if (vBarEntry->count > vBarEntry->size)
575 {
576 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
577 const UINT32 oldPos = vBarEntry->size * bpp;
578 const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp;
579
580 BYTE* tmp =
581 (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32);
582
583 if (!tmp)
584 {
585 WLog_Print(clear->log, WLOG_ERROR,
586 "vBarEntry->pixels winpr_aligned_recalloc %" PRIu32 " failed",
587 vBarEntry->count * bpp);
588 return FALSE;
589 }
590
591 memset(&tmp[oldPos], 0, diffSize);
592 vBarEntry->pixels = tmp;
593 vBarEntry->size = vBarEntry->count;
594 }
595
596 if (!vBarEntry->pixels && vBarEntry->size)
597 {
598 WLog_Print(clear->log, WLOG_ERROR,
599 "vBarEntry->pixels is nullptr but vBarEntry->size is %" PRIu32 "",
600 vBarEntry->size);
601 return FALSE;
602 }
603
604 return TRUE;
605}
606
607static BOOL clear_decompress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
608 wStream* WINPR_RESTRICT s, UINT32 bandsByteCount,
609 UINT32 nWidth, UINT32 nHeight,
610 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
611 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
612 UINT32 nDstWidth, UINT32 nDstHeight)
613{
614 UINT32 suboffset = 0;
615
616 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, bandsByteCount))
617 return FALSE;
618
619 while (suboffset < bandsByteCount)
620 {
621 BYTE cr = 0;
622 BYTE cg = 0;
623 BYTE cb = 0;
624 UINT16 xStart = 0;
625 UINT16 xEnd = 0;
626 UINT16 yStart = 0;
627 UINT16 yEnd = 0;
628 UINT32 colorBkg = 0;
629 UINT16 vBarHeader = 0;
630 UINT16 vBarYOn = 0;
631 UINT16 vBarYOff = 0;
632 UINT32 vBarCount = 0;
633 UINT32 vBarPixelCount = 0;
634 UINT32 vBarShortPixelCount = 0;
635
636 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 11))
637 return FALSE;
638
639 Stream_Read_UINT16(s, xStart);
640 Stream_Read_UINT16(s, xEnd);
641 Stream_Read_UINT16(s, yStart);
642 Stream_Read_UINT16(s, yEnd);
643 Stream_Read_UINT8(s, cb);
644 Stream_Read_UINT8(s, cg);
645 Stream_Read_UINT8(s, cr);
646 suboffset += 11;
647 colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF);
648
649 if (xEnd < xStart)
650 {
651 WLog_Print(clear->log, WLOG_ERROR, "xEnd %" PRIu16 " < xStart %" PRIu16 "", xEnd,
652 xStart);
653 return FALSE;
654 }
655
656 if (yEnd < yStart)
657 {
658 WLog_Print(clear->log, WLOG_ERROR, "yEnd %" PRIu16 " < yStart %" PRIu16 "", yEnd,
659 yStart);
660 return FALSE;
661 }
662
663 vBarCount = (xEnd - xStart) + 1;
664
665 for (UINT32 i = 0; i < vBarCount; i++)
666 {
667 UINT32 vBarHeight = 0;
668 CLEAR_VBAR_ENTRY* vBarEntry = nullptr;
669 CLEAR_VBAR_ENTRY* vBarShortEntry = nullptr;
670 BOOL vBarUpdate = FALSE;
671 const BYTE* cpSrcPixel = nullptr;
672
673 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
674 return FALSE;
675
676 Stream_Read_UINT16(s, vBarHeader);
677 suboffset += 2;
678 vBarHeight = (yEnd - yStart + 1);
679
680 if (vBarHeight > 52)
681 {
682 WLog_Print(clear->log, WLOG_ERROR, "vBarHeight (%" PRIu32 ") > 52", vBarHeight);
683 return FALSE;
684 }
685
686 if ((vBarHeader & 0xC000) == 0x4000) /* SHORT_VBAR_CACHE_HIT */
687 {
688 const UINT16 vBarIndex = (vBarHeader & 0x3FFF);
689 vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]);
690
691 if (!vBarShortEntry)
692 {
693 WLog_Print(clear->log, WLOG_ERROR, "missing vBarShortEntry %" PRIu16 "",
694 vBarIndex);
695 return FALSE;
696 }
697
698 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 1))
699 return FALSE;
700
701 Stream_Read_UINT8(s, vBarYOn);
702 suboffset += 1;
703 vBarShortPixelCount = vBarShortEntry->count;
704 vBarUpdate = TRUE;
705 }
706 else if ((vBarHeader & 0xC000) == 0x0000) /* SHORT_VBAR_CACHE_MISS */
707 {
708 vBarYOn = (vBarHeader & 0xFF);
709 vBarYOff = ((vBarHeader >> 8) & 0x3F);
710
711 if (vBarYOff < vBarYOn)
712 {
713 WLog_Print(clear->log, WLOG_ERROR, "vBarYOff %" PRIu16 " < vBarYOn %" PRIu16 "",
714 vBarYOff, vBarYOn);
715 return FALSE;
716 }
717
718 vBarShortPixelCount = (vBarYOff - vBarYOn);
719
720 if (vBarShortPixelCount > 52)
721 {
722 WLog_Print(clear->log, WLOG_ERROR, "vBarShortPixelCount %" PRIu32 " > 52",
723 vBarShortPixelCount);
724 return FALSE;
725 }
726
727 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(clear->log, s, vBarShortPixelCount,
728 3ull))
729 return FALSE;
730
731 if (clear->ShortVBarStorageCursor >= CLEARCODEC_VBAR_SHORT_SIZE)
732 {
733 WLog_Print(clear->log, WLOG_ERROR,
734 "clear->ShortVBarStorageCursor %" PRIu32
735 " >= CLEARCODEC_VBAR_SHORT_SIZE (%" PRId32 ")",
736 clear->ShortVBarStorageCursor, CLEARCODEC_VBAR_SHORT_SIZE);
737 return FALSE;
738 }
739
740 vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]);
741 vBarShortEntry->count = vBarShortPixelCount;
742
743 if (!resize_vbar_entry(clear, vBarShortEntry))
744 return FALSE;
745
746 for (size_t y = 0; y < vBarShortPixelCount; y++)
747 {
748 BYTE r = 0;
749 BYTE g = 0;
750 BYTE b = 0;
751 BYTE* dstBuffer =
752 &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)];
753 UINT32 color = 0;
754 Stream_Read_UINT8(s, b);
755 Stream_Read_UINT8(s, g);
756 Stream_Read_UINT8(s, r);
757 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
758
759 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
760 return FALSE;
761 }
762
763 suboffset += (vBarShortPixelCount * 3);
764 clear->ShortVBarStorageCursor =
765 (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE;
766 vBarUpdate = TRUE;
767 }
768 else if ((vBarHeader & 0x8000) == 0x8000) /* VBAR_CACHE_HIT */
769 {
770 const UINT16 vBarIndex = (vBarHeader & 0x7FFF);
771 vBarEntry = &(clear->VBarStorage[vBarIndex]);
772
773 /* If the cache was reset we need to fill in some dummy data. */
774 if (vBarEntry->size == 0)
775 {
776 WLog_Print(clear->log, WLOG_WARN,
777 "Empty cache index %" PRIu16 ", filling dummy data", vBarIndex);
778 vBarEntry->count = vBarHeight;
779
780 if (!resize_vbar_entry(clear, vBarEntry))
781 return FALSE;
782 }
783 }
784 else
785 {
786 WLog_Print(clear->log, WLOG_ERROR, "invalid vBarHeader 0x%04" PRIX16 "",
787 vBarHeader);
788 return FALSE; /* invalid vBarHeader */
789 }
790
791 if (vBarUpdate)
792 {
793 BYTE* pSrcPixel = nullptr;
794 BYTE* dstBuffer = nullptr;
795
796 if (clear->VBarStorageCursor >= CLEARCODEC_VBAR_SIZE)
797 {
798 WLog_Print(clear->log, WLOG_ERROR,
799 "clear->VBarStorageCursor %" PRIu32
800 " >= CLEARCODEC_VBAR_SIZE %" PRId32 "",
801 clear->VBarStorageCursor, CLEARCODEC_VBAR_SIZE);
802 return FALSE;
803 }
804
805 vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]);
806 vBarPixelCount = vBarHeight;
807 vBarEntry->count = vBarPixelCount;
808
809 if (!resize_vbar_entry(clear, vBarEntry))
810 return FALSE;
811
812 dstBuffer = vBarEntry->pixels;
813 /* if (y < vBarYOn), use colorBkg */
814 UINT32 y = 0;
815 UINT32 count = vBarYOn;
816
817 if ((y + count) > vBarPixelCount)
818 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
819
820 if (count > 0)
821 {
822 while (count--)
823 {
824 FreeRDPWriteColor(dstBuffer, clear->format, colorBkg);
825 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
826 }
827 }
828
829 /*
830 * if ((y >= vBarYOn) && (y < (vBarYOn + vBarShortPixelCount))),
831 * use vBarShortPixels at index (y - shortVBarYOn)
832 */
833 y = vBarYOn;
834 count = vBarShortPixelCount;
835
836 if ((y + count) > vBarPixelCount)
837 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
838
839 if (count > 0)
840 {
841 const size_t offset =
842 (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format);
843 pSrcPixel = &vBarShortEntry->pixels[offset];
844 if (offset + count > vBarShortEntry->count)
845 {
846 WLog_Print(clear->log, WLOG_ERROR,
847 "offset + count > vBarShortEntry->count");
848 return FALSE;
849 }
850 }
851 for (size_t x = 0; x < count; x++)
852 {
853 UINT32 color = 0;
854 color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)],
855 clear->format);
856
857 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
858 return FALSE;
859
860 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
861 }
862
863 /* if (y >= (vBarYOn + vBarShortPixelCount)), use colorBkg */
864 y = vBarYOn + vBarShortPixelCount;
865 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
866
867 if (count > 0)
868 {
869 while (count--)
870 {
871 if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg))
872 return FALSE;
873
874 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
875 }
876 }
877
878 vBarEntry->count = vBarPixelCount;
879 clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
880 }
881
882 if (vBarEntry->count != vBarHeight)
883 {
884 WLog_Print(clear->log, WLOG_ERROR,
885 "vBarEntry->count %" PRIu32 " != vBarHeight %" PRIu32 "",
886 vBarEntry->count, vBarHeight);
887 vBarEntry->count = vBarHeight;
888
889 if (!resize_vbar_entry(clear, vBarEntry))
890 return FALSE;
891 }
892
893 const UINT32 nXDstRel = nXDst + xStart;
894 const UINT32 nYDstRel = nYDst + yStart;
895 cpSrcPixel = vBarEntry->pixels;
896
897 if (i < nWidth)
898 {
899 UINT32 count = vBarEntry->count;
900
901 if (count > nHeight)
902 count = nHeight;
903
904 if (nXDstRel + i >= nDstWidth)
905 return FALSE;
906
907 for (UINT32 y = 0; y < count; y++)
908 {
909 if (nYDstRel + y >= nDstHeight)
910 return FALSE;
911
912 BYTE* pDstPixel8 =
913 &pDstData[((nYDstRel + y) * nDstStep) +
914 ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))];
915 UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format);
916 color = FreeRDPConvertColor(color, clear->format, DstFormat, nullptr);
917
918 if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color))
919 return FALSE;
920
921 cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format);
922 }
923 }
924 }
925 }
926
927 return TRUE;
928}
929
930static BOOL clear_decompress_glyph_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
931 wStream* WINPR_RESTRICT s, UINT32 glyphFlags, UINT32 nWidth,
932 UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
933 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
934 UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
935 const gdiPalette* WINPR_RESTRICT palette,
936 BYTE** WINPR_RESTRICT ppGlyphData)
937{
938 UINT16 glyphIndex = 0;
939
940 if (ppGlyphData)
941 *ppGlyphData = nullptr;
942
943 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX))
944 {
945 WLog_Print(clear->log, WLOG_ERROR, "Invalid glyph flags %08" PRIX32 "", glyphFlags);
946 return FALSE;
947 }
948
949 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0)
950 return TRUE;
951
952 if ((nWidth * nHeight) > (1024 * 1024))
953 {
954 WLog_Print(clear->log, WLOG_ERROR, "glyph too large: %" PRIu32 "x%" PRIu32 "", nWidth,
955 nHeight);
956 return FALSE;
957 }
958
959 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
960 return FALSE;
961
962 Stream_Read_UINT16(s, glyphIndex);
963
964 if (glyphIndex >= 4000)
965 {
966 WLog_Print(clear->log, WLOG_ERROR, "Invalid glyphIndex %" PRIu16 "", glyphIndex);
967 return FALSE;
968 }
969
970 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT)
971 {
972 UINT32 nSrcStep = 0;
973 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
974 BYTE* glyphData = nullptr;
975
976 if (!glyphEntry)
977 {
978 WLog_Print(clear->log, WLOG_ERROR, "clear->GlyphCache[%" PRIu16 "]=nullptr",
979 glyphIndex);
980 return FALSE;
981 }
982
983 glyphData = (BYTE*)glyphEntry->pixels;
984
985 if (!glyphData)
986 {
987 WLog_Print(clear->log, WLOG_ERROR, "clear->GlyphCache[%" PRIu16 "]->pixels=nullptr",
988 glyphIndex);
989 return FALSE;
990 }
991
992 if ((nWidth * nHeight) > glyphEntry->count)
993 {
994 WLog_Print(clear->log, WLOG_ERROR,
995 "(nWidth %" PRIu32 " * nHeight %" PRIu32 ") > glyphEntry->count %" PRIu32 "",
996 nWidth, nHeight, glyphEntry->count);
997 return FALSE;
998 }
999
1000 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
1001 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
1002 glyphData, nSrcStep, clear->format, nDstWidth, nDstHeight, palette);
1003 }
1004
1005 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)
1006 {
1007 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
1008 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
1009 const size_t count = 1ull * nWidth * nHeight;
1010 const size_t hlimit = SIZE_MAX / ((nWidth > 0) ? nWidth : 1);
1011 if ((nWidth == 0) || (nHeight == 0) || (hlimit < nHeight))
1012 {
1013 const char* exceeded = (hlimit < nHeight) ? "within" : "outside";
1014 WLog_Print(clear->log, WLOG_ERROR,
1015 "CLEARCODEC_FLAG_GLYPH_INDEX: nWidth=%" PRIu32 ", nHeight=%" PRIu32
1016 ", nWidth * nHeight is %s allowed range",
1017 nWidth, nHeight, exceeded);
1018 return FALSE;
1019 }
1020
1021 if (count > glyphEntry->size)
1022 {
1023 BYTE* tmp = winpr_aligned_recalloc(glyphEntry->pixels, count, 1ull * bpp, 32);
1024
1025 if (!tmp)
1026 {
1027 WLog_Print(clear->log, WLOG_ERROR,
1028 "glyphEntry->pixels winpr_aligned_recalloc %" PRIuz " failed!",
1029 count * bpp);
1030 return FALSE;
1031 }
1032
1033 glyphEntry->count = WINPR_ASSERTING_INT_CAST(UINT32, count);
1034 glyphEntry->size = glyphEntry->count;
1035 glyphEntry->pixels = (UINT32*)tmp;
1036 }
1037
1038 if (!glyphEntry->pixels)
1039 {
1040 WLog_Print(clear->log, WLOG_ERROR, "glyphEntry->pixels=nullptr");
1041 return FALSE;
1042 }
1043
1044 if (ppGlyphData)
1045 *ppGlyphData = (BYTE*)glyphEntry->pixels;
1046
1047 return TRUE;
1048 }
1049
1050 return TRUE;
1051}
1052
1053static inline BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat)
1054{
1055 if (!clear || !clear->nsc)
1056 return FALSE;
1057
1058 clear->format = DstFormat;
1059 return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat);
1060}
1061
1062INT32 clear_decompress(CLEAR_CONTEXT* WINPR_RESTRICT clear, const BYTE* WINPR_RESTRICT pSrcData,
1063 UINT32 SrcSize, UINT32 nWidth, UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
1064 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
1065 UINT32 nDstWidth, UINT32 nDstHeight,
1066 const gdiPalette* WINPR_RESTRICT palette)
1067{
1068 INT32 rc = -1;
1069 BYTE seqNumber = 0;
1070 BYTE glyphFlags = 0;
1071 UINT32 residualByteCount = 0;
1072 UINT32 bandsByteCount = 0;
1073 UINT32 subcodecByteCount = 0;
1074 wStream sbuffer = WINPR_C_ARRAY_INIT;
1075 wStream* s = nullptr;
1076 BYTE* glyphData = nullptr;
1077
1078 if (!pDstData)
1079 return -1002;
1080
1081 if ((nDstWidth == 0) || (nDstHeight == 0))
1082 return -1022;
1083
1084 if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1085 return -1004;
1086
1087 if (nXDst > nDstWidth)
1088 {
1089 WLog_Print(clear->log, WLOG_WARN, "nXDst %" PRIu32 " > nDstWidth %" PRIu32, nXDst,
1090 nDstWidth);
1091 return -1005;
1092 }
1093
1094 if (nYDst > nDstHeight)
1095 {
1096 WLog_Print(clear->log, WLOG_WARN, "nYDst %" PRIu32 " > nDstHeight %" PRIu32, nYDst,
1097 nDstHeight);
1098 return -1006;
1099 }
1100
1101 s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
1102
1103 if (!s)
1104 return -2005;
1105
1106 if (!Stream_CheckAndLogRequiredLengthWLog(clear->log, s, 2))
1107 goto fail;
1108
1109 if (!updateContextFormat(clear, DstFormat))
1110 goto fail;
1111
1112 Stream_Read_UINT8(s, glyphFlags);
1113 Stream_Read_UINT8(s, seqNumber);
1114
1115 if (!clear->seqNumber && seqNumber)
1116 clear->seqNumber = seqNumber;
1117
1118 if (seqNumber != clear->seqNumber)
1119 {
1120 WLog_Print(clear->log, WLOG_ERROR, "Sequence number unexpected %" PRIu8 " - %" PRIu32 "",
1121 seqNumber, clear->seqNumber);
1122 WLog_Print(clear->log, WLOG_ERROR, "seqNumber %" PRIu8 " != clear->seqNumber %" PRIu32 "",
1123 seqNumber, clear->seqNumber);
1124 goto fail;
1125 }
1126
1127 clear->seqNumber = (seqNumber + 1) % 256;
1128
1129 if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET)
1130 {
1131 clear_reset_vbar_storage(clear, FALSE);
1132 }
1133
1134 if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat,
1135 nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette,
1136 &glyphData))
1137 {
1138 WLog_Print(clear->log, WLOG_ERROR, "clear_decompress_glyph_data failed!");
1139 goto fail;
1140 }
1141
1142 /* Read composition payload header parameters */
1143 if (Stream_GetRemainingLength(s) < 12)
1144 {
1145 const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX);
1146
1147 if ((glyphFlags & mask) == mask)
1148 goto finish;
1149
1150 WLog_Print(clear->log, WLOG_ERROR,
1151 "invalid glyphFlags, missing flags: 0x%02" PRIx8 " & 0x%02" PRIx32
1152 " == 0x%02" PRIx32,
1153 glyphFlags, mask, glyphFlags & mask);
1154 goto fail;
1155 }
1156
1157 Stream_Read_UINT32(s, residualByteCount);
1158 Stream_Read_UINT32(s, bandsByteCount);
1159 Stream_Read_UINT32(s, subcodecByteCount);
1160
1161 if (residualByteCount > 0)
1162 {
1163 if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData,
1164 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1165 nDstHeight, palette))
1166 {
1167 WLog_Print(clear->log, WLOG_ERROR, "clear_decompress_residual_data failed!");
1168 goto fail;
1169 }
1170 }
1171
1172 if (bandsByteCount > 0)
1173 {
1174 if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData,
1175 DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight))
1176 {
1177 WLog_Print(clear->log, WLOG_ERROR, "clear_decompress_bands_data failed!");
1178 goto fail;
1179 }
1180 }
1181
1182 if (subcodecByteCount > 0)
1183 {
1184 if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData,
1185 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1186 nDstHeight, palette))
1187 {
1188 WLog_Print(clear->log, WLOG_ERROR, "clear_decompress_subcodecs_data failed!");
1189 goto fail;
1190 }
1191 }
1192
1193 if (glyphData)
1194 {
1195 uint32_t w = MIN(nWidth, nDstWidth);
1196 if (nXDst > nDstWidth)
1197 {
1198 WLog_Print(clear->log, WLOG_WARN,
1199 "glyphData copy area x exceeds destination: x=%" PRIu32 " > %" PRIu32, nXDst,
1200 nDstWidth);
1201 w = 0;
1202 }
1203 else if (nXDst + w > nDstWidth)
1204 {
1205 WLog_Print(clear->log, WLOG_WARN,
1206 "glyphData copy area x + width exceeds destination: x=%" PRIu32 " + %" PRIu32
1207 " > %" PRIu32,
1208 nXDst, w, nDstWidth);
1209 w = nDstWidth - nXDst;
1210 }
1211
1212 if (w != nWidth)
1213 {
1214 WLog_Print(clear->log, WLOG_WARN,
1215 "glyphData copy area width truncated: requested=%" PRIu32
1216 ", truncated to %" PRIu32,
1217 nWidth, w);
1218 }
1219
1220 uint32_t h = MIN(nHeight, nDstHeight);
1221 if (nYDst > nDstHeight)
1222 {
1223 WLog_Print(clear->log, WLOG_WARN,
1224 "glyphData copy area y exceeds destination: y=%" PRIu32 " > %" PRIu32, nYDst,
1225 nDstHeight);
1226 h = 0;
1227 }
1228 else if (nYDst + h > nDstHeight)
1229 {
1230 WLog_Print(clear->log, WLOG_WARN,
1231 "glyphData copy area y + height exceeds destination: x=%" PRIu32
1232 " + %" PRIu32 " > %" PRIu32,
1233 nYDst, h, nDstHeight);
1234 h = nDstHeight - nYDst;
1235 }
1236
1237 if (h != nHeight)
1238 {
1239 WLog_Print(clear->log, WLOG_WARN,
1240 "glyphData copy area height truncated: requested=%" PRIu32
1241 ", truncated to %" PRIu32,
1242 nHeight, h);
1243 }
1244
1245 if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, w, h, pDstData,
1246 DstFormat, nDstStep, nXDst, nYDst, palette,
1247 FREERDP_KEEP_DST_ALPHA))
1248 goto fail;
1249 }
1250
1251finish:
1252 rc = 0;
1253fail:
1254 return rc;
1255}
1256
1257int clear_compress(WINPR_ATTR_UNUSED CLEAR_CONTEXT* WINPR_RESTRICT clear,
1258 WINPR_ATTR_UNUSED const BYTE* WINPR_RESTRICT pSrcData,
1259 WINPR_ATTR_UNUSED UINT32 SrcSize,
1260 WINPR_ATTR_UNUSED BYTE** WINPR_RESTRICT ppDstData,
1261 WINPR_ATTR_UNUSED UINT32* WINPR_RESTRICT pDstSize)
1262{
1263 WLog_Print(clear->log, WLOG_ERROR, "TODO: not implemented!");
1264 return 1;
1265}
1266
1267BOOL clear_context_reset(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1268{
1269 if (!clear)
1270 return FALSE;
1271
1276 clear->seqNumber = 0;
1277 return TRUE;
1278}
1279
1280CLEAR_CONTEXT* clear_context_new(BOOL Compressor)
1281{
1282 CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(CLEAR_CONTEXT), 32);
1283
1284 if (!clear)
1285 return nullptr;
1286 clear->log = WLog_Get(TAG);
1287 clear->Compressor = Compressor;
1288 clear->nsc = nsc_context_new();
1289
1290 if (!clear->nsc)
1291 goto error_nsc;
1292
1293 if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32))
1294 goto error_nsc;
1295
1296 if (!clear_resize_buffer(clear, 512, 512))
1297 goto error_nsc;
1298
1299 if (!clear->TempBuffer)
1300 goto error_nsc;
1301
1302 if (!clear_context_reset(clear))
1303 goto error_nsc;
1304
1305 return clear;
1306error_nsc:
1307 WINPR_PRAGMA_DIAG_PUSH
1308 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1309 clear_context_free(clear);
1310 WINPR_PRAGMA_DIAG_POP
1311 return nullptr;
1312}
1313
1314void clear_context_free(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1315{
1316 if (!clear)
1317 return;
1318
1319 nsc_context_free(clear->nsc);
1320 winpr_aligned_free(clear->TempBuffer);
1321
1322 clear_reset_vbar_storage(clear, TRUE);
1323 clear_reset_glyph_cache(clear);
1324
1325 winpr_aligned_free(clear);
1326}