Coverage Report

Created: 2025-07-01 06:46

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