Coverage Report

Created: 2026-02-26 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/clear.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * ClearCodec Bitmap Compression
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016 Thincast Technologies GmbH
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <winpr/crt.h>
25
#include <winpr/print.h>
26
#include <winpr/bitstream.h>
27
28
#include <freerdp/codec/color.h>
29
#include <freerdp/codec/clear.h>
30
#include <freerdp/log.h>
31
32
#define TAG FREERDP_TAG("codec.clear")
33
34
24.4k
#define CLEARCODEC_FLAG_GLYPH_INDEX 0x01
35
23.5k
#define CLEARCODEC_FLAG_GLYPH_HIT 0x02
36
16.5k
#define CLEARCODEC_FLAG_CACHE_RESET 0x04
37
38
4.09M
#define CLEARCODEC_VBAR_SIZE 32768
39
3.85M
#define CLEARCODEC_VBAR_SHORT_SIZE 16384
40
41
typedef struct
42
{
43
  UINT32 size;
44
  UINT32 count;
45
  UINT32* pixels;
46
} CLEAR_GLYPH_ENTRY;
47
48
typedef struct
49
{
50
  UINT32 size;
51
  UINT32 count;
52
  BYTE* pixels;
53
} CLEAR_VBAR_ENTRY;
54
55
struct S_CLEAR_CONTEXT
56
{
57
  BOOL Compressor;
58
  NSC_CONTEXT* nsc;
59
  UINT32 seqNumber;
60
  BYTE* TempBuffer;
61
  size_t TempSize;
62
  UINT32 nTempStep;
63
  UINT32 TempFormat;
64
  UINT32 format;
65
  CLEAR_GLYPH_ENTRY GlyphCache[4000];
66
  UINT32 VBarStorageCursor;
67
  CLEAR_VBAR_ENTRY VBarStorage[CLEARCODEC_VBAR_SIZE];
68
  UINT32 ShortVBarStorageCursor;
69
  CLEAR_VBAR_ENTRY ShortVBarStorage[CLEARCODEC_VBAR_SHORT_SIZE];
70
};
71
72
static const UINT32 CLEAR_LOG2_FLOOR[256] = {
73
  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
74
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
75
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
76
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
77
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
78
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
79
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
80
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
81
};
82
83
static const BYTE CLEAR_8BIT_MASKS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
84
85
static void clear_reset_vbar_storage(CLEAR_CONTEXT* WINPR_RESTRICT clear, BOOL zero)
86
19.7k
{
87
19.7k
  if (zero)
88
16.5k
  {
89
541M
    for (size_t i = 0; i < ARRAYSIZE(clear->VBarStorage); i++)
90
541M
      winpr_aligned_free(clear->VBarStorage[i].pixels);
91
92
16.5k
    ZeroMemory(clear->VBarStorage, sizeof(clear->VBarStorage));
93
16.5k
  }
94
95
19.7k
  clear->VBarStorageCursor = 0;
96
97
19.7k
  if (zero)
98
16.5k
  {
99
270M
    for (size_t i = 0; i < ARRAYSIZE(clear->ShortVBarStorage); i++)
100
270M
      winpr_aligned_free(clear->ShortVBarStorage[i].pixels);
101
102
16.5k
    ZeroMemory(clear->ShortVBarStorage, sizeof(clear->ShortVBarStorage));
103
16.5k
  }
104
105
19.7k
  clear->ShortVBarStorageCursor = 0;
106
19.7k
}
107
108
static void clear_reset_glyph_cache(CLEAR_CONTEXT* WINPR_RESTRICT clear)
109
16.5k
{
110
66.1M
  for (size_t i = 0; i < ARRAYSIZE(clear->GlyphCache); i++)
111
66.1M
    winpr_aligned_free(clear->GlyphCache[i].pixels);
112
113
16.5k
  ZeroMemory(clear->GlyphCache, sizeof(clear->GlyphCache));
114
16.5k
}
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
913k
{
122
913k
  if (nWidth + nXDst > nDstWidth)
123
0
    nWidth = nDstWidth - nXDst;
124
125
913k
  if (nHeight + nYDst > nDstHeight)
126
0
    nHeight = nDstHeight - nYDst;
127
128
913k
  return freerdp_image_copy_no_overlap(dst, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
129
913k
                                       src, SrcFormat, nSrcStep, 0, 0, palette,
130
913k
                                       FREERDP_KEEP_DST_ALPHA);
131
913k
}
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
696
{
139
696
  BOOL rc = 0;
140
141
696
  if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
142
0
    return FALSE;
143
144
696
  rc = nsc_process_message(nsc, 32, width, height, Stream_Pointer(s), bitmapDataByteCount,
145
696
                           pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel, nDstWidth,
146
696
                           nDstHeight, FREERDP_FLIP_NONE);
147
696
  Stream_Seek(s, bitmapDataByteCount);
148
696
  return rc;
149
696
}
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
504
{
157
504
  UINT32 x = 0;
158
504
  UINT32 y = 0;
159
504
  UINT32 pixelCount = 0;
160
504
  UINT32 bitmapDataOffset = 0;
161
504
  size_t pixelIndex = 0;
162
504
  UINT32 numBits = 0;
163
504
  BYTE startIndex = 0;
164
504
  BYTE stopIndex = 0;
165
504
  BYTE suiteIndex = 0;
166
504
  BYTE suiteDepth = 0;
167
504
  BYTE paletteCount = 0;
168
504
  UINT32 palette[128] = WINPR_C_ARRAY_INIT;
169
170
504
  if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
171
0
    return FALSE;
172
173
504
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
174
6
    return FALSE;
175
498
  Stream_Read_UINT8(s, paletteCount);
176
498
  bitmapDataOffset = 1 + (paletteCount * 3);
177
178
498
  if ((paletteCount > 127) || (paletteCount < 1))
179
16
  {
180
16
    WLog_ERR(TAG, "paletteCount %" PRIu8 "", paletteCount);
181
16
    return FALSE;
182
16
  }
183
184
482
  if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, paletteCount, 3ull))
185
8
    return FALSE;
186
187
2.91k
  for (UINT32 i = 0; i < paletteCount; i++)
188
2.43k
  {
189
2.43k
    BYTE r = 0;
190
2.43k
    BYTE g = 0;
191
2.43k
    BYTE b = 0;
192
2.43k
    Stream_Read_UINT8(s, b);
193
2.43k
    Stream_Read_UINT8(s, g);
194
2.43k
    Stream_Read_UINT8(s, r);
195
2.43k
    palette[i] = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
196
2.43k
  }
197
198
474
  pixelIndex = 0;
199
474
  pixelCount = width * height;
200
474
  numBits = CLEAR_LOG2_FLOOR[paletteCount - 1] + 1;
201
202
1.39k
  while (bitmapDataOffset < bitmapDataByteCount)
203
1.29k
  {
204
1.29k
    UINT32 tmp = 0;
205
1.29k
    UINT32 color = 0;
206
1.29k
    UINT32 runLengthFactor = 0;
207
208
1.29k
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
209
18
      return FALSE;
210
211
1.27k
    Stream_Read_UINT8(s, tmp);
212
1.27k
    Stream_Read_UINT8(s, runLengthFactor);
213
1.27k
    bitmapDataOffset += 2;
214
1.27k
    suiteDepth = (tmp >> numBits) & CLEAR_8BIT_MASKS[(8 - numBits)];
215
1.27k
    stopIndex = tmp & CLEAR_8BIT_MASKS[numBits];
216
1.27k
    startIndex = stopIndex - suiteDepth;
217
218
1.27k
    if (runLengthFactor >= 0xFF)
219
382
    {
220
382
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
221
9
        return FALSE;
222
223
373
      Stream_Read_UINT16(s, runLengthFactor);
224
373
      bitmapDataOffset += 2;
225
226
373
      if (runLengthFactor >= 0xFFFF)
227
209
      {
228
209
        if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
229
6
          return FALSE;
230
231
203
        Stream_Read_UINT32(s, runLengthFactor);
232
203
        bitmapDataOffset += 4;
233
203
      }
234
373
    }
235
236
1.25k
    if (startIndex >= paletteCount)
237
80
    {
238
80
      WLog_ERR(TAG, "startIndex %" PRIu8 " > paletteCount %" PRIu8 "]", startIndex,
239
80
               paletteCount);
240
80
      return FALSE;
241
80
    }
242
243
1.17k
    if (stopIndex >= paletteCount)
244
18
    {
245
18
      WLog_ERR(TAG, "stopIndex %" PRIu8 " > paletteCount %" PRIu8 "]", stopIndex,
246
18
               paletteCount);
247
18
      return FALSE;
248
18
    }
249
250
1.16k
    suiteIndex = startIndex;
251
252
1.16k
    if (suiteIndex > 127)
253
0
    {
254
0
      WLog_ERR(TAG, "suiteIndex %" PRIu8 " > 127]", suiteIndex);
255
0
      return FALSE;
256
0
    }
257
258
1.16k
    color = palette[suiteIndex];
259
260
1.16k
    if ((pixelIndex + runLengthFactor) > pixelCount)
261
215
    {
262
215
      WLog_ERR(TAG,
263
215
               "pixelIndex %" PRIuz " + runLengthFactor %" PRIu32 " > pixelCount %" PRIu32 "",
264
215
               pixelIndex, runLengthFactor, pixelCount);
265
215
      return FALSE;
266
215
    }
267
268
6.70k
    for (UINT32 i = 0; i < runLengthFactor; i++)
269
5.76k
    {
270
5.76k
      BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
271
5.76k
                                 (nYDstRel + y) * nDstStep];
272
273
5.76k
      if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
274
5.76k
        FreeRDPWriteColor(pTmpData, DstFormat, color);
275
276
5.76k
      if (++x >= width)
277
338
      {
278
338
        y++;
279
338
        x = 0;
280
338
      }
281
5.76k
    }
282
283
946
    pixelIndex += runLengthFactor;
284
285
946
    if ((pixelIndex + (suiteDepth + 1)) > pixelCount)
286
26
    {
287
26
      WLog_ERR(TAG,
288
26
               "pixelIndex %" PRIuz " + suiteDepth %" PRIu8 " + 1 > pixelCount %" PRIu32 "",
289
26
               pixelIndex, suiteDepth, pixelCount);
290
26
      return FALSE;
291
26
    }
292
293
2.23k
    for (UINT32 i = 0; i <= suiteDepth; i++)
294
1.31k
    {
295
1.31k
      BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
296
1.31k
                                 (nYDstRel + y) * nDstStep];
297
1.31k
      UINT32 ccolor = palette[suiteIndex];
298
299
1.31k
      if (suiteIndex > 127)
300
0
      {
301
0
        WLog_ERR(TAG, "suiteIndex %" PRIu8 " > 127", suiteIndex);
302
0
        return FALSE;
303
0
      }
304
305
1.31k
      suiteIndex++;
306
307
1.31k
      if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
308
1.31k
        FreeRDPWriteColor(pTmpData, DstFormat, ccolor);
309
310
1.31k
      if (++x >= width)
311
263
      {
312
263
        y++;
313
263
        x = 0;
314
263
      }
315
1.31k
    }
316
317
920
    pixelIndex += (suiteDepth + 1);
318
920
  }
319
320
102
  if (pixelIndex != pixelCount)
321
56
  {
322
56
    WLog_ERR(TAG, "pixelIndex %" PRIuz " != pixelCount %" PRIu32 "", pixelIndex, pixelCount);
323
56
    return FALSE;
324
56
  }
325
326
46
  return TRUE;
327
102
}
328
329
static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height)
330
932k
{
331
932k
  if (!clear)
332
0
    return FALSE;
333
334
932k
  const UINT64 size = 1ull * (width + 16ull) * (height + 16ull);
335
932k
  const size_t bpp = FreeRDPGetBytesPerPixel(clear->format);
336
932k
  if (size > UINT32_MAX / bpp)
337
0
    return FALSE;
338
339
932k
  if (size > clear->TempSize / bpp)
340
16.5k
  {
341
16.5k
    BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer,
342
16.5k
                                              WINPR_ASSERTING_INT_CAST(size_t, size), bpp, 32);
343
344
16.5k
    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
16.5k
    clear->TempSize = WINPR_ASSERTING_INT_CAST(size_t, size* bpp);
352
16.5k
    clear->TempBuffer = tmp;
353
16.5k
  }
354
355
932k
  return TRUE;
356
932k
}
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.33k
{
366
5.33k
  UINT32 nSrcStep = 0;
367
5.33k
  UINT32 suboffset = 0;
368
5.33k
  BYTE* dstBuffer = NULL;
369
5.33k
  UINT32 pixelIndex = 0;
370
5.33k
  UINT32 pixelCount = 0;
371
372
5.33k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, residualByteCount))
373
4.38k
    return FALSE;
374
375
948
  suboffset = 0;
376
948
  pixelIndex = 0;
377
948
  pixelCount = nWidth * nHeight;
378
379
948
  if (!clear_resize_buffer(clear, nWidth, nHeight))
380
0
    return FALSE;
381
382
948
  dstBuffer = clear->TempBuffer;
383
384
856k
  while (suboffset < residualByteCount)
385
856k
  {
386
856k
    BYTE r = 0;
387
856k
    BYTE g = 0;
388
856k
    BYTE b = 0;
389
856k
    UINT32 runLengthFactor = 0;
390
856k
    UINT32 color = 0;
391
392
856k
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
393
27
      return FALSE;
394
395
856k
    Stream_Read_UINT8(s, b);
396
856k
    Stream_Read_UINT8(s, g);
397
856k
    Stream_Read_UINT8(s, r);
398
856k
    Stream_Read_UINT8(s, runLengthFactor);
399
856k
    suboffset += 4;
400
856k
    color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
401
402
856k
    if (runLengthFactor >= 0xFF)
403
1.45k
    {
404
1.45k
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
405
14
        return FALSE;
406
407
1.43k
      Stream_Read_UINT16(s, runLengthFactor);
408
1.43k
      suboffset += 2;
409
410
1.43k
      if (runLengthFactor >= 0xFFFF)
411
808
      {
412
808
        if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
413
12
          return FALSE;
414
415
796
        Stream_Read_UINT32(s, runLengthFactor);
416
796
        suboffset += 4;
417
796
      }
418
1.43k
    }
419
420
856k
    if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex)))
421
595
    {
422
595
      WLog_ERR(TAG,
423
595
               "pixelIndex %" PRIu32 " + runLengthFactor %" PRIu32 " > pixelCount %" PRIu32
424
595
               "",
425
595
               pixelIndex, runLengthFactor, pixelCount);
426
595
      return FALSE;
427
595
    }
428
429
1.09M
    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
855k
    pixelIndex += runLengthFactor;
436
855k
  }
437
438
300
  nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
439
440
300
  if (pixelIndex != pixelCount)
441
280
  {
442
280
    WLog_ERR(TAG, "pixelIndex %" PRIu32 " != pixelCount %" PRIu32 "", pixelIndex, pixelCount);
443
280
    return FALSE;
444
280
  }
445
446
20
  return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
447
20
                       clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight,
448
20
                       palette);
449
300
}
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.32k
{
459
2.32k
  UINT32 suboffset = 0;
460
461
2.32k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, subcodecByteCount))
462
209
    return FALSE;
463
464
916k
  while (suboffset < subcodecByteCount)
465
915k
  {
466
915k
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 13))
467
17
      return FALSE;
468
469
915k
    const UINT16 xStart = Stream_Get_UINT16(s);
470
915k
    const UINT16 yStart = Stream_Get_UINT16(s);
471
915k
    const UINT16 width = Stream_Get_UINT16(s);
472
915k
    const UINT16 height = Stream_Get_UINT16(s);
473
915k
    const UINT32 bitmapDataByteCount = Stream_Get_UINT32(s);
474
915k
    const UINT8 subcodecId = Stream_Get_UINT8(s);
475
915k
    suboffset += 13;
476
477
915k
    if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
478
106
      return FALSE;
479
480
915k
    const UINT32 nXDstRel = nXDst + xStart;
481
915k
    const UINT32 nYDstRel = nYDst + yStart;
482
915k
    if (1ull * nXDstRel + width > nDstWidth)
483
557
    {
484
557
      WLog_ERR(TAG, "nXDstRel %" PRIu32 " + width %" PRIu16 " > nDstWidth %" PRIu32 "",
485
557
               nXDstRel, width, nDstWidth);
486
557
      return FALSE;
487
557
    }
488
915k
    if (1ull * nYDstRel + height > nDstHeight)
489
169
    {
490
169
      WLog_ERR(TAG, "nYDstRel %" PRIu32 " + height %" PRIu16 " > nDstHeight %" PRIu32 "",
491
169
               nYDstRel, height, nDstHeight);
492
169
      return FALSE;
493
169
    }
494
495
914k
    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
914k
    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
914k
    if (!clear_resize_buffer(clear, width, height))
509
0
      return FALSE;
510
511
914k
    switch (subcodecId)
512
914k
    {
513
913k
      case 0: /* Uncompressed */
514
913k
      {
515
913k
        const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24);
516
913k
        const size_t nSrcSize = 1ull * nSrcStep * height;
517
518
913k
        if (bitmapDataByteCount != nSrcSize)
519
31
        {
520
31
          WLog_ERR(TAG, "bitmapDataByteCount %" PRIu32 " != nSrcSize %" PRIuz "",
521
31
                   bitmapDataByteCount, nSrcSize);
522
31
          return FALSE;
523
31
        }
524
525
913k
        if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height,
526
913k
                           Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth,
527
913k
                           nDstHeight, palette))
528
0
          return FALSE;
529
530
913k
        Stream_Seek(s, bitmapDataByteCount);
531
913k
      }
532
0
      break;
533
534
696
      case 1: /* NSCodec */
535
696
        if (!clear_decompress_nscodec(clear->nsc, width, height, s, bitmapDataByteCount,
536
696
                                      pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel,
537
696
                                      nDstWidth, nDstHeight))
538
336
          return FALSE;
539
540
360
        break;
541
542
504
      case 2: /* CLEARCODEC_SUBCODEC_RLEX */
543
504
        if (!clear_decompress_subcode_rlex(s, bitmapDataByteCount, width, height, pDstData,
544
504
                                           DstFormat, nDstStep, nXDstRel, nYDstRel,
545
504
                                           nDstWidth, nDstHeight))
546
458
          return FALSE;
547
548
46
        break;
549
550
46
      default:
551
18
        WLog_ERR(TAG, "Unknown subcodec ID %" PRIu8 "", subcodecId);
552
18
        return FALSE;
553
914k
    }
554
555
914k
    suboffset += bitmapDataByteCount;
556
914k
  }
557
558
427
  return TRUE;
559
2.11k
}
560
561
static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear,
562
                              CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry)
563
3.98M
{
564
3.98M
  if (vBarEntry->count > vBarEntry->size)
565
1.90M
  {
566
1.90M
    const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
567
1.90M
    const UINT32 oldPos = vBarEntry->size * bpp;
568
1.90M
    const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp;
569
570
1.90M
    vBarEntry->size = vBarEntry->count;
571
1.90M
    BYTE* tmp =
572
1.90M
        (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32);
573
574
1.90M
    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
1.90M
    memset(&tmp[oldPos], 0, diffSize);
582
1.90M
    vBarEntry->pixels = tmp;
583
1.90M
  }
584
585
3.98M
  if (!vBarEntry->pixels && vBarEntry->size)
586
0
  {
587
0
    WLog_ERR(TAG, "vBarEntry->pixels is NULL but vBarEntry->size is %" PRIu32 "",
588
0
             vBarEntry->size);
589
0
    return FALSE;
590
0
  }
591
592
3.98M
  return TRUE;
593
3.98M
}
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
1.97k
{
602
1.97k
  UINT32 suboffset = 0;
603
604
1.97k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, bandsByteCount))
605
432
    return FALSE;
606
607
27.8k
  while (suboffset < bandsByteCount)
608
27.5k
  {
609
27.5k
    BYTE cr = 0;
610
27.5k
    BYTE cg = 0;
611
27.5k
    BYTE cb = 0;
612
27.5k
    UINT16 xStart = 0;
613
27.5k
    UINT16 xEnd = 0;
614
27.5k
    UINT16 yStart = 0;
615
27.5k
    UINT16 yEnd = 0;
616
27.5k
    UINT32 colorBkg = 0;
617
27.5k
    UINT16 vBarHeader = 0;
618
27.5k
    UINT16 vBarYOn = 0;
619
27.5k
    UINT16 vBarYOff = 0;
620
27.5k
    UINT32 vBarCount = 0;
621
27.5k
    UINT32 vBarPixelCount = 0;
622
27.5k
    UINT32 vBarShortPixelCount = 0;
623
624
27.5k
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 11))
625
11
      return FALSE;
626
627
27.5k
    Stream_Read_UINT16(s, xStart);
628
27.5k
    Stream_Read_UINT16(s, xEnd);
629
27.5k
    Stream_Read_UINT16(s, yStart);
630
27.5k
    Stream_Read_UINT16(s, yEnd);
631
27.5k
    Stream_Read_UINT8(s, cb);
632
27.5k
    Stream_Read_UINT8(s, cg);
633
27.5k
    Stream_Read_UINT8(s, cr);
634
27.5k
    suboffset += 11;
635
27.5k
    colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF);
636
637
27.5k
    if (xEnd < xStart)
638
35
    {
639
35
      WLog_ERR(TAG, "xEnd %" PRIu16 " < xStart %" PRIu16 "", xEnd, xStart);
640
35
      return FALSE;
641
35
    }
642
643
27.5k
    if (yEnd < yStart)
644
32
    {
645
32
      WLog_ERR(TAG, "yEnd %" PRIu16 " < yStart %" PRIu16 "", yEnd, yStart);
646
32
      return FALSE;
647
32
    }
648
649
27.4k
    vBarCount = (xEnd - xStart) + 1;
650
651
2.09M
    for (UINT32 i = 0; i < vBarCount; i++)
652
2.06M
    {
653
2.06M
      UINT32 vBarHeight = 0;
654
2.06M
      CLEAR_VBAR_ENTRY* vBarEntry = NULL;
655
2.06M
      CLEAR_VBAR_ENTRY* vBarShortEntry = NULL;
656
2.06M
      BOOL vBarUpdate = FALSE;
657
2.06M
      const BYTE* cpSrcPixel = NULL;
658
659
2.06M
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
660
492
        return FALSE;
661
662
2.06M
      Stream_Read_UINT16(s, vBarHeader);
663
2.06M
      suboffset += 2;
664
2.06M
      vBarHeight = (yEnd - yStart + 1);
665
666
2.06M
      if (vBarHeight > 52)
667
60
      {
668
60
        WLog_ERR(TAG, "vBarHeight (%" PRIu32 ") > 52", vBarHeight);
669
60
        return FALSE;
670
60
      }
671
672
2.06M
      if ((vBarHeader & 0xC000) == 0x4000) /* SHORT_VBAR_CACHE_HIT */
673
120k
      {
674
120k
        const UINT16 vBarIndex = (vBarHeader & 0x3FFF);
675
120k
        vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]);
676
677
120k
        if (!vBarShortEntry)
678
0
        {
679
0
          WLog_ERR(TAG, "missing vBarShortEntry %" PRIu16 "", vBarIndex);
680
0
          return FALSE;
681
0
        }
682
683
120k
        if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
684
13
          return FALSE;
685
686
120k
        Stream_Read_UINT8(s, vBarYOn);
687
120k
        suboffset += 1;
688
120k
        vBarShortPixelCount = vBarShortEntry->count;
689
120k
        vBarUpdate = TRUE;
690
120k
      }
691
1.94M
      else if ((vBarHeader & 0xC000) == 0x0000) /* SHORT_VBAR_CACHE_MISS */
692
1.93M
      {
693
1.93M
        vBarYOn = (vBarHeader & 0xFF);
694
1.93M
        vBarYOff = ((vBarHeader >> 8) & 0x3F);
695
696
1.93M
        if (vBarYOff < vBarYOn)
697
279
        {
698
279
          WLog_ERR(TAG, "vBarYOff %" PRIu16 " < vBarYOn %" PRIu16 "", vBarYOff, vBarYOn);
699
279
          return FALSE;
700
279
        }
701
702
1.92M
        vBarShortPixelCount = (vBarYOff - vBarYOn);
703
704
1.92M
        if (vBarShortPixelCount > 52)
705
9
        {
706
9
          WLog_ERR(TAG, "vBarShortPixelCount %" PRIu32 " > 52", vBarShortPixelCount);
707
9
          return FALSE;
708
9
        }
709
710
1.92M
        if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, vBarShortPixelCount, 3ull))
711
43
          return FALSE;
712
713
1.92M
        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
1.92M
        vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]);
723
1.92M
        vBarShortEntry->count = vBarShortPixelCount;
724
725
1.92M
        if (!resize_vbar_entry(clear, vBarShortEntry))
726
0
          return FALSE;
727
728
1.94M
        for (size_t y = 0; y < vBarShortPixelCount; y++)
729
14.4k
        {
730
14.4k
          BYTE r = 0;
731
14.4k
          BYTE g = 0;
732
14.4k
          BYTE b = 0;
733
14.4k
          BYTE* dstBuffer =
734
14.4k
              &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)];
735
14.4k
          UINT32 color = 0;
736
14.4k
          Stream_Read_UINT8(s, b);
737
14.4k
          Stream_Read_UINT8(s, g);
738
14.4k
          Stream_Read_UINT8(s, r);
739
14.4k
          color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
740
741
14.4k
          if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
742
0
            return FALSE;
743
14.4k
        }
744
745
1.92M
        suboffset += (vBarShortPixelCount * 3);
746
1.92M
        clear->ShortVBarStorageCursor =
747
1.92M
            (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE;
748
1.92M
        vBarUpdate = TRUE;
749
1.92M
      }
750
15.7k
      else if ((vBarHeader & 0x8000) == 0x8000) /* VBAR_CACHE_HIT */
751
15.7k
      {
752
15.7k
        const UINT16 vBarIndex = (vBarHeader & 0x7FFF);
753
15.7k
        vBarEntry = &(clear->VBarStorage[vBarIndex]);
754
755
        /* If the cache was reset we need to fill in some dummy data. */
756
15.7k
        if (vBarEntry->size == 0)
757
6.33k
        {
758
6.33k
          WLog_WARN(TAG, "Empty cache index %" PRIu16 ", filling dummy data", vBarIndex);
759
6.33k
          vBarEntry->count = vBarHeight;
760
761
6.33k
          if (!resize_vbar_entry(clear, vBarEntry))
762
0
            return FALSE;
763
6.33k
        }
764
15.7k
      }
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.06M
      if (vBarUpdate)
772
2.04M
      {
773
2.04M
        BYTE* pSrcPixel = NULL;
774
2.04M
        BYTE* dstBuffer = NULL;
775
776
2.04M
        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.04M
        vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]);
786
2.04M
        vBarPixelCount = vBarHeight;
787
2.04M
        vBarEntry->count = vBarPixelCount;
788
789
2.04M
        if (!resize_vbar_entry(clear, vBarEntry))
790
0
          return FALSE;
791
792
2.04M
        dstBuffer = vBarEntry->pixels;
793
        /* if (y < vBarYOn), use colorBkg */
794
2.04M
        UINT32 y = 0;
795
2.04M
        UINT32 count = vBarYOn;
796
797
2.04M
        if ((y + count) > vBarPixelCount)
798
122k
          count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
799
800
2.04M
        if (count > 0)
801
122k
        {
802
761k
          while (count--)
803
638k
          {
804
638k
            FreeRDPWriteColor(dstBuffer, clear->format, colorBkg);
805
638k
            dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
806
638k
          }
807
122k
        }
808
809
        /*
810
         * if ((y >= vBarYOn) && (y < (vBarYOn + vBarShortPixelCount))),
811
         * use vBarShortPixels at index (y - shortVBarYOn)
812
         */
813
2.04M
        y = vBarYOn;
814
2.04M
        count = vBarShortPixelCount;
815
816
2.04M
        if ((y + count) > vBarPixelCount)
817
122k
          count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
818
819
2.04M
        if (count > 0)
820
1.13k
        {
821
1.13k
          const size_t offset =
822
1.13k
              (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format);
823
1.13k
          pSrcPixel = &vBarShortEntry->pixels[offset];
824
1.13k
          if (offset + count > vBarShortEntry->count)
825
0
          {
826
0
            WLog_ERR(TAG, "offset + count > vBarShortEntry->count");
827
0
            return FALSE;
828
0
          }
829
1.13k
        }
830
2.05M
        for (size_t x = 0; x < count; x++)
831
4.37k
        {
832
4.37k
          UINT32 color = 0;
833
4.37k
          color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)],
834
4.37k
                                   clear->format);
835
836
4.37k
          if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
837
0
            return FALSE;
838
839
4.37k
          dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
840
4.37k
        }
841
842
        /* if (y >= (vBarYOn + vBarShortPixelCount)), use colorBkg */
843
2.04M
        y = vBarYOn + vBarShortPixelCount;
844
2.04M
        count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
845
846
2.04M
        if (count > 0)
847
1.92M
        {
848
11.8M
          while (count--)
849
9.96M
          {
850
9.96M
            if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg))
851
0
              return FALSE;
852
853
9.96M
            dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
854
9.96M
          }
855
1.92M
        }
856
857
2.04M
        vBarEntry->count = vBarPixelCount;
858
2.04M
        clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
859
2.04M
      }
860
861
2.06M
      if (vBarEntry->count != vBarHeight)
862
154
      {
863
154
        WLog_ERR(TAG, "vBarEntry->count %" PRIu32 " != vBarHeight %" PRIu32 "",
864
154
                 vBarEntry->count, vBarHeight);
865
154
        vBarEntry->count = vBarHeight;
866
867
154
        if (!resize_vbar_entry(clear, vBarEntry))
868
0
          return FALSE;
869
154
      }
870
871
2.06M
      const UINT32 nXDstRel = nXDst + xStart;
872
2.06M
      const UINT32 nYDstRel = nYDst + yStart;
873
2.06M
      cpSrcPixel = vBarEntry->pixels;
874
875
2.06M
      if (i < nWidth)
876
50.7k
      {
877
50.7k
        UINT32 count = vBarEntry->count;
878
879
50.7k
        if (count > nHeight)
880
5.68k
          count = nHeight;
881
882
50.7k
        if (nXDstRel + i >= nDstWidth)
883
142
          return FALSE;
884
885
245k
        for (UINT32 y = 0; y < count; y++)
886
194k
        {
887
194k
          if (nYDstRel + y >= nDstHeight)
888
142
            return FALSE;
889
890
194k
          BYTE* pDstPixel8 =
891
194k
              &pDstData[((nYDstRel + y) * nDstStep) +
892
194k
                        ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))];
893
194k
          UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format);
894
194k
          color = FreeRDPConvertColor(color, clear->format, DstFormat, NULL);
895
896
194k
          if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color))
897
0
            return FALSE;
898
899
194k
          cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format);
900
194k
        }
901
50.6k
      }
902
2.06M
    }
903
27.4k
  }
904
905
280
  return TRUE;
906
1.53k
}
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
16.5k
{
916
16.5k
  UINT16 glyphIndex = 0;
917
918
16.5k
  if (ppGlyphData)
919
16.5k
    *ppGlyphData = NULL;
920
921
16.5k
  if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX))
922
1.25k
  {
923
1.25k
    WLog_ERR(TAG, "Invalid glyph flags %08" PRIX32 "", glyphFlags);
924
1.25k
    return FALSE;
925
1.25k
  }
926
927
15.2k
  if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0)
928
8.64k
    return TRUE;
929
930
6.63k
  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.63k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
937
0
    return FALSE;
938
939
6.63k
  Stream_Read_UINT16(s, glyphIndex);
940
941
6.63k
  if (glyphIndex >= 4000)
942
2.36k
  {
943
2.36k
    WLog_ERR(TAG, "Invalid glyphIndex %" PRIu16 "", glyphIndex);
944
2.36k
    return FALSE;
945
2.36k
  }
946
947
4.26k
  if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT)
948
708
  {
949
708
    UINT32 nSrcStep = 0;
950
708
    CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
951
708
    BYTE* glyphData = NULL;
952
953
708
    if (!glyphEntry)
954
0
    {
955
0
      WLog_ERR(TAG, "clear->GlyphCache[%" PRIu16 "]=NULL", glyphIndex);
956
0
      return FALSE;
957
0
    }
958
959
708
    glyphData = (BYTE*)glyphEntry->pixels;
960
961
708
    if (!glyphData)
962
708
    {
963
708
      WLog_ERR(TAG, "clear->GlyphCache[%" PRIu16 "]->pixels=NULL", glyphIndex);
964
708
      return FALSE;
965
708
    }
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.56k
  if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)
981
3.56k
  {
982
3.56k
    const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
983
3.56k
    CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
984
3.56k
    glyphEntry->count = nWidth * nHeight;
985
986
3.56k
    if (glyphEntry->count > glyphEntry->size)
987
3.56k
    {
988
3.56k
      BYTE* tmp =
989
3.56k
          winpr_aligned_recalloc(glyphEntry->pixels, glyphEntry->count, 1ull * bpp, 32);
990
991
3.56k
      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.56k
      glyphEntry->size = glyphEntry->count;
999
3.56k
      glyphEntry->pixels = (UINT32*)tmp;
1000
3.56k
    }
1001
1002
3.56k
    if (!glyphEntry->pixels)
1003
0
    {
1004
0
      WLog_ERR(TAG, "glyphEntry->pixels=NULL");
1005
0
      return FALSE;
1006
0
    }
1007
1008
3.56k
    if (ppGlyphData)
1009
3.56k
      *ppGlyphData = (BYTE*)glyphEntry->pixels;
1010
1011
3.56k
    return TRUE;
1012
3.56k
  }
1013
1014
0
  return TRUE;
1015
3.56k
}
1016
1017
static inline BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat)
1018
33.0k
{
1019
33.0k
  if (!clear || !clear->nsc)
1020
0
    return FALSE;
1021
1022
33.0k
  clear->format = DstFormat;
1023
33.0k
  return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat);
1024
33.0k
}
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
16.5k
{
1032
16.5k
  INT32 rc = -1;
1033
16.5k
  BYTE seqNumber = 0;
1034
16.5k
  BYTE glyphFlags = 0;
1035
16.5k
  UINT32 residualByteCount = 0;
1036
16.5k
  UINT32 bandsByteCount = 0;
1037
16.5k
  UINT32 subcodecByteCount = 0;
1038
16.5k
  wStream sbuffer = WINPR_C_ARRAY_INIT;
1039
16.5k
  wStream* s = NULL;
1040
16.5k
  BYTE* glyphData = NULL;
1041
1042
16.5k
  if (!pDstData)
1043
0
    return -1002;
1044
1045
16.5k
  if ((nDstWidth == 0) || (nDstHeight == 0))
1046
0
    return -1022;
1047
1048
16.5k
  if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1049
0
    return -1004;
1050
1051
16.5k
  if (nXDst > nDstWidth)
1052
0
  {
1053
0
    WLog_WARN(TAG, "nXDst %" PRIu32 " > nDstWidth %" PRIu32, nXDst, nDstWidth);
1054
0
    return -1005;
1055
0
  }
1056
1057
16.5k
  if (nYDst > nDstHeight)
1058
0
  {
1059
0
    WLog_WARN(TAG, "nYDst %" PRIu32 " > nDstHeight %" PRIu32, nYDst, nDstHeight);
1060
0
    return -1006;
1061
0
  }
1062
1063
16.5k
  s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
1064
1065
16.5k
  if (!s)
1066
0
    return -2005;
1067
1068
16.5k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1069
0
    goto fail;
1070
1071
16.5k
  if (!updateContextFormat(clear, DstFormat))
1072
0
    goto fail;
1073
1074
16.5k
  Stream_Read_UINT8(s, glyphFlags);
1075
16.5k
  Stream_Read_UINT8(s, seqNumber);
1076
1077
16.5k
  if (!clear->seqNumber && seqNumber)
1078
13.8k
    clear->seqNumber = seqNumber;
1079
1080
16.5k
  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
16.5k
  clear->seqNumber = (seqNumber + 1) % 256;
1090
1091
16.5k
  if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET)
1092
3.20k
  {
1093
3.20k
    clear_reset_vbar_storage(clear, FALSE);
1094
3.20k
  }
1095
1096
16.5k
  if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat,
1097
16.5k
                                   nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette,
1098
16.5k
                                   &glyphData))
1099
4.32k
  {
1100
4.32k
    WLog_ERR(TAG, "clear_decompress_glyph_data failed!");
1101
4.32k
    goto fail;
1102
4.32k
  }
1103
1104
  /* Read composition payload header parameters */
1105
12.2k
  if (Stream_GetRemainingLength(s) < 12)
1106
2.77k
  {
1107
2.77k
    const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX);
1108
1109
2.77k
    if ((glyphFlags & mask) == mask)
1110
0
      goto finish;
1111
1112
2.77k
    WLog_ERR(TAG,
1113
2.77k
             "invalid glyphFlags, missing flags: 0x%02" PRIx8 " & 0x%02" PRIx32
1114
2.77k
             " == 0x%02" PRIx32,
1115
2.77k
             glyphFlags, mask, glyphFlags & mask);
1116
2.77k
    goto fail;
1117
2.77k
  }
1118
1119
9.43k
  Stream_Read_UINT32(s, residualByteCount);
1120
9.43k
  Stream_Read_UINT32(s, bandsByteCount);
1121
9.43k
  Stream_Read_UINT32(s, subcodecByteCount);
1122
1123
9.43k
  if (residualByteCount > 0)
1124
5.33k
  {
1125
5.33k
    if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData,
1126
5.33k
                                        DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1127
5.33k
                                        nDstHeight, palette))
1128
5.31k
    {
1129
5.31k
      WLog_ERR(TAG, "clear_decompress_residual_data failed!");
1130
5.31k
      goto fail;
1131
5.31k
    }
1132
5.33k
  }
1133
1134
4.12k
  if (bandsByteCount > 0)
1135
1.97k
  {
1136
1.97k
    if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData,
1137
1.97k
                                     DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight))
1138
1.69k
    {
1139
1.69k
      WLog_ERR(TAG, "clear_decompress_bands_data failed!");
1140
1.69k
      goto fail;
1141
1.69k
    }
1142
1.97k
  }
1143
1144
2.43k
  if (subcodecByteCount > 0)
1145
2.32k
  {
1146
2.32k
    if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData,
1147
2.32k
                                         DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1148
2.32k
                                         nDstHeight, palette))
1149
1.90k
    {
1150
1.90k
      WLog_ERR(TAG, "clear_decompress_subcodecs_data failed!");
1151
1.90k
      goto fail;
1152
1.90k
    }
1153
2.32k
  }
1154
1155
533
  if (glyphData)
1156
161
  {
1157
161
    uint32_t w = MIN(nWidth, nDstWidth);
1158
161
    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
161
    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
161
    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
161
    uint32_t h = MIN(nHeight, nDstHeight);
1182
161
    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
161
    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
161
    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
161
    if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, w, h, pDstData,
1206
161
                                       DstFormat, nDstStep, nXDst, nYDst, palette,
1207
161
                                       FREERDP_KEEP_DST_ALPHA))
1208
0
      goto fail;
1209
161
  }
1210
1211
533
finish:
1212
533
  rc = 0;
1213
16.5k
fail:
1214
16.5k
  return rc;
1215
533
}
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
16.5k
{
1229
16.5k
  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
16.5k
  clear->seqNumber = 0;
1237
16.5k
  return TRUE;
1238
16.5k
}
1239
1240
CLEAR_CONTEXT* clear_context_new(BOOL Compressor)
1241
16.5k
{
1242
16.5k
  CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(CLEAR_CONTEXT), 32);
1243
1244
16.5k
  if (!clear)
1245
0
    return NULL;
1246
1247
16.5k
  clear->Compressor = Compressor;
1248
16.5k
  clear->nsc = nsc_context_new();
1249
1250
16.5k
  if (!clear->nsc)
1251
0
    goto error_nsc;
1252
1253
16.5k
  if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32))
1254
0
    goto error_nsc;
1255
1256
16.5k
  if (!clear_resize_buffer(clear, 512, 512))
1257
0
    goto error_nsc;
1258
1259
16.5k
  if (!clear->TempBuffer)
1260
0
    goto error_nsc;
1261
1262
16.5k
  if (!clear_context_reset(clear))
1263
0
    goto error_nsc;
1264
1265
16.5k
  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 NULL;
1272
16.5k
}
1273
1274
void clear_context_free(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1275
16.5k
{
1276
16.5k
  if (!clear)
1277
0
    return;
1278
1279
16.5k
  nsc_context_free(clear->nsc);
1280
16.5k
  winpr_aligned_free(clear->TempBuffer);
1281
1282
16.5k
  clear_reset_vbar_storage(clear, TRUE);
1283
16.5k
  clear_reset_glyph_cache(clear);
1284
1285
16.5k
  winpr_aligned_free(clear);
1286
16.5k
}