Coverage Report

Created: 2026-01-17 07:16

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