Coverage Report

Created: 2026-04-12 06:59

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