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