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