Coverage Report

Created: 2026-03-04 06:14

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