Coverage Report

Created: 2026-01-09 06:49

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