Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/codec/bulk.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Bulk Compression
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <math.h>
21
#include <winpr/assert.h>
22
23
#include <freerdp/config.h>
24
25
#include "../core/settings.h"
26
#include "bulk.h"
27
#include "../codec/mppc.h"
28
#include "../codec/ncrush.h"
29
#include "../codec/xcrush.h"
30
31
#include <freerdp/log.h>
32
#define TAG FREERDP_TAG("core")
33
34
//#define WITH_BULK_DEBUG 1
35
36
struct rdp_bulk
37
{
38
  ALIGN64 rdpContext* context;
39
  ALIGN64 UINT32 CompressionLevel;
40
  ALIGN64 UINT16 CompressionMaxSize;
41
  ALIGN64 MPPC_CONTEXT* mppcSend;
42
  ALIGN64 MPPC_CONTEXT* mppcRecv;
43
  ALIGN64 NCRUSH_CONTEXT* ncrushRecv;
44
  ALIGN64 NCRUSH_CONTEXT* ncrushSend;
45
  ALIGN64 XCRUSH_CONTEXT* xcrushRecv;
46
  ALIGN64 XCRUSH_CONTEXT* xcrushSend;
47
  ALIGN64 BYTE OutputBuffer[65536];
48
};
49
50
#if defined(WITH_BULK_DEBUG)
51
static INLINE const char* bulk_get_compression_flags_string(UINT32 flags)
52
{
53
  flags &= BULK_COMPRESSION_FLAGS_MASK;
54
55
  if (flags == 0)
56
    return "PACKET_UNCOMPRESSED";
57
  else if (flags == PACKET_COMPRESSED)
58
    return "PACKET_COMPRESSED";
59
  else if (flags == PACKET_AT_FRONT)
60
    return "PACKET_AT_FRONT";
61
  else if (flags == PACKET_FLUSHED)
62
    return "PACKET_FLUSHED";
63
  else if (flags == (PACKET_COMPRESSED | PACKET_AT_FRONT))
64
    return "PACKET_COMPRESSED | PACKET_AT_FRONT";
65
  else if (flags == (PACKET_COMPRESSED | PACKET_FLUSHED))
66
    return "PACKET_COMPRESSED | PACKET_FLUSHED";
67
  else if (flags == (PACKET_AT_FRONT | PACKET_FLUSHED))
68
    return "PACKET_AT_FRONT | PACKET_FLUSHED";
69
  else if (flags == (PACKET_COMPRESSED | PACKET_AT_FRONT | PACKET_FLUSHED))
70
    return "PACKET_COMPRESSED | PACKET_AT_FRONT | PACKET_FLUSHED";
71
72
  return "PACKET_UNKNOWN";
73
}
74
#endif
75
76
static UINT32 bulk_compression_level(rdpBulk* WINPR_RESTRICT bulk)
77
0
{
78
0
  rdpSettings* settings = NULL;
79
0
  WINPR_ASSERT(bulk);
80
0
  WINPR_ASSERT(bulk->context);
81
0
  settings = bulk->context->settings;
82
0
  WINPR_ASSERT(settings);
83
0
  bulk->CompressionLevel = (settings->CompressionLevel >= PACKET_COMPR_TYPE_RDP61)
84
0
                               ? PACKET_COMPR_TYPE_RDP61
85
0
                               : settings->CompressionLevel;
86
0
  WINPR_ASSERT(bulk->CompressionLevel <= UINT16_MAX);
87
0
  return bulk->CompressionLevel;
88
0
}
89
90
UINT16 bulk_compression_max_size(rdpBulk* WINPR_RESTRICT bulk)
91
0
{
92
0
  WINPR_ASSERT(bulk);
93
0
  (void)bulk_compression_level(bulk);
94
0
  bulk->CompressionMaxSize = (bulk->CompressionLevel < PACKET_COMPR_TYPE_64K) ? 8192 : UINT16_MAX;
95
0
  return bulk->CompressionMaxSize;
96
0
}
97
98
#if defined(WITH_BULK_DEBUG)
99
static INLINE int bulk_compress_validate(rdpBulk* bulk, const BYTE* pSrcData, UINT32 SrcSize,
100
                                         const BYTE* pDstData, UINT32 DstSize, UINT32 Flags)
101
{
102
  int status;
103
  const BYTE* v_pSrcData = NULL;
104
  const BYTE* v_pDstData = NULL;
105
  UINT32 v_SrcSize = 0;
106
  UINT32 v_DstSize = 0;
107
  UINT32 v_Flags = 0;
108
109
  WINPR_ASSERT(bulk);
110
  WINPR_ASSERT(pSrcData);
111
  WINPR_ASSERT(pDstData);
112
113
  v_pSrcData = pDstData;
114
  v_SrcSize = DstSize;
115
  v_Flags = Flags | bulk->CompressionLevel;
116
  status = bulk_decompress(bulk, v_pSrcData, v_SrcSize, &v_pDstData, &v_DstSize, v_Flags);
117
118
  if (status < 0)
119
  {
120
    WLog_DBG(TAG, "compression/decompression failure");
121
    return status;
122
  }
123
124
  if (v_DstSize != SrcSize)
125
  {
126
    WLog_DBG(TAG,
127
             "compression/decompression size mismatch: Actual: %" PRIu32 ", Expected: %" PRIu32
128
             "",
129
             v_DstSize, SrcSize);
130
    return -1;
131
  }
132
133
  if (memcmp(v_pDstData, pSrcData, SrcSize) != 0)
134
  {
135
    WLog_DBG(TAG, "compression/decompression input/output mismatch! flags: 0x%08" PRIX32 "",
136
             v_Flags);
137
#if 1
138
    WLog_DBG(TAG, "Actual:");
139
    winpr_HexDump(TAG, WLOG_DEBUG, v_pDstData, SrcSize);
140
    WLog_DBG(TAG, "Expected:");
141
    winpr_HexDump(TAG, WLOG_DEBUG, pSrcData, SrcSize);
142
#endif
143
    return -1;
144
  }
145
146
  return status;
147
}
148
#endif
149
150
int bulk_decompress(rdpBulk* WINPR_RESTRICT bulk, const BYTE* WINPR_RESTRICT pSrcData,
151
                    UINT32 SrcSize, const BYTE** WINPR_RESTRICT ppDstData,
152
                    UINT32* WINPR_RESTRICT pDstSize, UINT32 flags)
153
0
{
154
0
  int status = -1;
155
156
0
  WINPR_ASSERT(bulk);
157
0
  WINPR_ASSERT(bulk->context);
158
0
  WINPR_ASSERT(pSrcData);
159
0
  WINPR_ASSERT(ppDstData);
160
0
  WINPR_ASSERT(pDstSize);
161
162
0
  rdpMetrics* metrics = bulk->context->metrics;
163
0
  WINPR_ASSERT(metrics);
164
165
0
  (void)bulk_compression_max_size(bulk);
166
0
  const UINT32 type = flags & BULK_COMPRESSION_TYPE_MASK;
167
168
0
  if (flags & BULK_COMPRESSION_FLAGS_MASK)
169
0
  {
170
0
    switch (type)
171
0
    {
172
0
      case PACKET_COMPR_TYPE_8K:
173
0
        mppc_set_compression_level(bulk->mppcRecv, 0);
174
0
        status =
175
0
            mppc_decompress(bulk->mppcRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
176
0
        break;
177
178
0
      case PACKET_COMPR_TYPE_64K:
179
0
        mppc_set_compression_level(bulk->mppcRecv, 1);
180
0
        status =
181
0
            mppc_decompress(bulk->mppcRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
182
0
        break;
183
184
0
      case PACKET_COMPR_TYPE_RDP6:
185
0
        status = ncrush_decompress(bulk->ncrushRecv, pSrcData, SrcSize, ppDstData, pDstSize,
186
0
                                   flags);
187
0
        break;
188
189
0
      case PACKET_COMPR_TYPE_RDP61:
190
0
        status = xcrush_decompress(bulk->xcrushRecv, pSrcData, SrcSize, ppDstData, pDstSize,
191
0
                                   flags);
192
0
        break;
193
194
0
      case PACKET_COMPR_TYPE_RDP8:
195
0
        WLog_ERR(TAG, "Unsupported bulk compression type %08" PRIx32,
196
0
                 bulk->CompressionLevel);
197
0
        status = -1;
198
0
        break;
199
0
      default:
200
0
        WLog_ERR(TAG, "Unknown bulk compression type %08" PRIx32, bulk->CompressionLevel);
201
0
        status = -1;
202
0
        break;
203
0
    }
204
0
  }
205
0
  else
206
0
  {
207
0
    *ppDstData = pSrcData;
208
0
    *pDstSize = SrcSize;
209
0
    status = 0;
210
0
  }
211
212
0
  if (status >= 0)
213
0
  {
214
0
    const UINT32 CompressedBytes = SrcSize;
215
0
    const UINT32 UncompressedBytes = *pDstSize;
216
0
    const double CompressionRatio =
217
0
        metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes);
218
#ifdef WITH_BULK_DEBUG
219
    {
220
      WLog_DBG(TAG,
221
               "Decompress Type: %" PRIu32 " Flags: %s (0x%08" PRIX32
222
               ") Compression Ratio: %f (%" PRIu32 " / %" PRIu32 "), Total: %f (%" PRIu64
223
               " / %" PRIu64 ")",
224
               type, bulk_get_compression_flags_string(flags), flags, CompressionRatio,
225
               CompressedBytes, UncompressedBytes, metrics->TotalCompressionRatio,
226
               metrics->TotalCompressedBytes, metrics->TotalUncompressedBytes);
227
    }
228
#else
229
0
    WINPR_UNUSED(CompressionRatio);
230
0
#endif
231
0
  }
232
0
  else
233
0
  {
234
0
    WLog_ERR(TAG, "Decompression failure!");
235
0
  }
236
237
0
  return status;
238
0
}
239
240
int bulk_compress(rdpBulk* WINPR_RESTRICT bulk, const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
241
                  const BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize,
242
                  UINT32* WINPR_RESTRICT pFlags)
243
0
{
244
0
  int status = -1;
245
246
0
  WINPR_ASSERT(bulk);
247
0
  WINPR_ASSERT(bulk->context);
248
0
  WINPR_ASSERT(pSrcData);
249
0
  WINPR_ASSERT(ppDstData);
250
0
  WINPR_ASSERT(pDstSize);
251
252
0
  rdpMetrics* metrics = bulk->context->metrics;
253
0
  WINPR_ASSERT(metrics);
254
255
0
  if ((SrcSize <= 50) || (SrcSize >= 16384))
256
0
  {
257
0
    *ppDstData = pSrcData;
258
0
    *pDstSize = SrcSize;
259
0
    return 0;
260
0
  }
261
262
0
  *pDstSize = sizeof(bulk->OutputBuffer);
263
0
  (void)bulk_compression_level(bulk);
264
0
  (void)bulk_compression_max_size(bulk);
265
266
0
  switch (bulk->CompressionLevel)
267
0
  {
268
0
    case PACKET_COMPR_TYPE_8K:
269
0
    case PACKET_COMPR_TYPE_64K:
270
0
      mppc_set_compression_level(bulk->mppcSend, bulk->CompressionLevel);
271
0
      status = mppc_compress(bulk->mppcSend, pSrcData, SrcSize, bulk->OutputBuffer, ppDstData,
272
0
                             pDstSize, pFlags);
273
0
      break;
274
0
    case PACKET_COMPR_TYPE_RDP6:
275
0
      status = ncrush_compress(bulk->ncrushSend, pSrcData, SrcSize, bulk->OutputBuffer,
276
0
                               ppDstData, pDstSize, pFlags);
277
0
      break;
278
0
    case PACKET_COMPR_TYPE_RDP61:
279
0
      status = xcrush_compress(bulk->xcrushSend, pSrcData, SrcSize, bulk->OutputBuffer,
280
0
                               ppDstData, pDstSize, pFlags);
281
0
      break;
282
0
    case PACKET_COMPR_TYPE_RDP8:
283
0
      WLog_ERR(TAG, "Unsupported bulk compression type %08" PRIx32, bulk->CompressionLevel);
284
0
      status = -1;
285
0
      break;
286
0
    default:
287
0
      WLog_ERR(TAG, "Unknown bulk compression type %08" PRIx32, bulk->CompressionLevel);
288
0
      status = -1;
289
0
      break;
290
0
  }
291
292
0
  if (status >= 0)
293
0
  {
294
0
    const UINT32 CompressedBytes = *pDstSize;
295
0
    const UINT32 UncompressedBytes = SrcSize;
296
0
    const double CompressionRatio =
297
0
        metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes);
298
#ifdef WITH_BULK_DEBUG
299
    {
300
      WLog_DBG(TAG,
301
               "Compress Type: %" PRIu32 " Flags: %s (0x%08" PRIX32
302
               ") Compression Ratio: %f (%" PRIu32 " / %" PRIu32 "), Total: %f (%" PRIu64
303
               " / %" PRIu64 ")",
304
               bulk->CompressionLevel, bulk_get_compression_flags_string(*pFlags), *pFlags,
305
               CompressionRatio, CompressedBytes, UncompressedBytes,
306
               metrics->TotalCompressionRatio, metrics->TotalCompressedBytes,
307
               metrics->TotalUncompressedBytes);
308
    }
309
#else
310
0
    WINPR_UNUSED(CompressionRatio);
311
0
#endif
312
0
  }
313
314
#if defined(WITH_BULK_DEBUG)
315
316
  if (bulk_compress_validate(bulk, pSrcData, SrcSize, *ppDstData, *pDstSize, *pFlags) < 0)
317
    status = -1;
318
319
#endif
320
0
  return status;
321
0
}
322
323
void bulk_reset(rdpBulk* WINPR_RESTRICT bulk)
324
0
{
325
0
  WINPR_ASSERT(bulk);
326
327
0
  mppc_context_reset(bulk->mppcSend, FALSE);
328
0
  mppc_context_reset(bulk->mppcRecv, FALSE);
329
0
  ncrush_context_reset(bulk->ncrushRecv, FALSE);
330
0
  ncrush_context_reset(bulk->ncrushSend, FALSE);
331
0
  xcrush_context_reset(bulk->xcrushRecv, FALSE);
332
0
  xcrush_context_reset(bulk->xcrushSend, FALSE);
333
0
}
334
335
rdpBulk* bulk_new(rdpContext* context)
336
0
{
337
0
  rdpBulk* bulk = NULL;
338
0
  WINPR_ASSERT(context);
339
340
0
  bulk = (rdpBulk*)calloc(1, sizeof(rdpBulk));
341
342
0
  if (!bulk)
343
0
    goto fail;
344
345
0
  bulk->context = context;
346
0
  bulk->mppcSend = mppc_context_new(1, TRUE);
347
0
  if (!bulk->mppcSend)
348
0
    goto fail;
349
0
  bulk->mppcRecv = mppc_context_new(1, FALSE);
350
0
  if (!bulk->mppcRecv)
351
0
    goto fail;
352
0
  bulk->ncrushRecv = ncrush_context_new(FALSE);
353
0
  if (!bulk->ncrushRecv)
354
0
    goto fail;
355
0
  bulk->ncrushSend = ncrush_context_new(TRUE);
356
0
  if (!bulk->ncrushSend)
357
0
    goto fail;
358
0
  bulk->xcrushRecv = xcrush_context_new(FALSE);
359
0
  if (!bulk->xcrushRecv)
360
0
    goto fail;
361
0
  bulk->xcrushSend = xcrush_context_new(TRUE);
362
0
  if (!bulk->xcrushSend)
363
0
    goto fail;
364
0
  bulk->CompressionLevel = context->settings->CompressionLevel;
365
366
0
  return bulk;
367
0
fail:
368
0
  WINPR_PRAGMA_DIAG_PUSH
369
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
370
0
  bulk_free(bulk);
371
0
  WINPR_PRAGMA_DIAG_POP
372
0
  return NULL;
373
0
}
374
375
void bulk_free(rdpBulk* bulk)
376
0
{
377
0
  if (!bulk)
378
0
    return;
379
380
0
  mppc_context_free(bulk->mppcSend);
381
0
  mppc_context_free(bulk->mppcRecv);
382
0
  ncrush_context_free(bulk->ncrushRecv);
383
0
  ncrush_context_free(bulk->ncrushSend);
384
0
  xcrush_context_free(bulk->xcrushRecv);
385
0
  xcrush_context_free(bulk->xcrushSend);
386
0
  free(bulk);
387
0
}