Coverage Report

Created: 2026-02-26 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/bulk.c
Line
Count
Source
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
WINPR_ATTR_NODISCARD
77
static UINT32 bulk_compression_level(rdpBulk* WINPR_RESTRICT bulk)
78
0
{
79
0
  WINPR_ASSERT(bulk);
80
0
  WINPR_ASSERT(bulk->context);
81
0
  rdpSettings* 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
static void bulk_update_compression_max_size(rdpBulk* WINPR_RESTRICT bulk)
91
0
{
92
0
  WINPR_ASSERT(bulk);
93
0
  const UINT32 CompressionLevel = bulk_compression_level(bulk);
94
0
  bulk->CompressionMaxSize = (CompressionLevel < PACKET_COMPR_TYPE_64K) ? 8192 : UINT16_MAX;
95
0
}
96
UINT16 bulk_compression_max_size(rdpBulk* WINPR_RESTRICT bulk)
97
0
{
98
0
  bulk_update_compression_max_size(bulk);
99
0
  return bulk->CompressionMaxSize;
100
0
}
101
102
#if defined(WITH_BULK_DEBUG)
103
static inline int bulk_compress_validate(rdpBulk* bulk, const BYTE* pSrcData, UINT32 SrcSize,
104
                                         const BYTE* pDstData, UINT32 DstSize, UINT32 Flags)
105
{
106
  int status;
107
  const BYTE* v_pSrcData = NULL;
108
  const BYTE* v_pDstData = NULL;
109
  UINT32 v_SrcSize = 0;
110
  UINT32 v_DstSize = 0;
111
  UINT32 v_Flags = 0;
112
113
  WINPR_ASSERT(bulk);
114
  WINPR_ASSERT(pSrcData);
115
  WINPR_ASSERT(pDstData);
116
117
  v_pSrcData = pDstData;
118
  v_SrcSize = DstSize;
119
  v_Flags = Flags | bulk->CompressionLevel;
120
  status = bulk_decompress(bulk, v_pSrcData, v_SrcSize, &v_pDstData, &v_DstSize, v_Flags);
121
122
  if (status < 0)
123
  {
124
    WLog_DBG(TAG, "compression/decompression failure");
125
    return status;
126
  }
127
128
  if (v_DstSize != SrcSize)
129
  {
130
    WLog_DBG(TAG,
131
             "compression/decompression size mismatch: Actual: %" PRIu32 ", Expected: %" PRIu32
132
             "",
133
             v_DstSize, SrcSize);
134
    return -1;
135
  }
136
137
  if (memcmp(v_pDstData, pSrcData, SrcSize) != 0)
138
  {
139
    WLog_DBG(TAG, "compression/decompression input/output mismatch! flags: 0x%08" PRIX32 "",
140
             v_Flags);
141
#if 1
142
    WLog_DBG(TAG, "Actual:");
143
    winpr_HexDump(TAG, WLOG_DEBUG, v_pDstData, SrcSize);
144
    WLog_DBG(TAG, "Expected:");
145
    winpr_HexDump(TAG, WLOG_DEBUG, pSrcData, SrcSize);
146
#endif
147
    return -1;
148
  }
149
150
  return status;
151
}
152
#endif
153
154
int bulk_decompress(rdpBulk* WINPR_RESTRICT bulk, const BYTE* WINPR_RESTRICT pSrcData,
155
                    UINT32 SrcSize, const BYTE** WINPR_RESTRICT ppDstData,
156
                    UINT32* WINPR_RESTRICT pDstSize, UINT32 flags)
157
0
{
158
0
  int status = -1;
159
160
0
  WINPR_ASSERT(bulk);
161
0
  WINPR_ASSERT(bulk->context);
162
0
  WINPR_ASSERT(pSrcData);
163
0
  WINPR_ASSERT(ppDstData);
164
0
  WINPR_ASSERT(pDstSize);
165
166
0
  rdpMetrics* metrics = bulk->context->metrics;
167
0
  WINPR_ASSERT(metrics);
168
169
0
  bulk_update_compression_max_size(bulk);
170
0
  const UINT32 type = flags & BULK_COMPRESSION_TYPE_MASK;
171
172
0
  if (flags & BULK_COMPRESSION_FLAGS_MASK)
173
0
  {
174
0
    switch (type)
175
0
    {
176
0
      case PACKET_COMPR_TYPE_8K:
177
0
        mppc_set_compression_level(bulk->mppcRecv, 0);
178
0
        status =
179
0
            mppc_decompress(bulk->mppcRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
180
0
        break;
181
182
0
      case PACKET_COMPR_TYPE_64K:
183
0
        mppc_set_compression_level(bulk->mppcRecv, 1);
184
0
        status =
185
0
            mppc_decompress(bulk->mppcRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
186
0
        break;
187
188
0
      case PACKET_COMPR_TYPE_RDP6:
189
0
        status = ncrush_decompress(bulk->ncrushRecv, pSrcData, SrcSize, ppDstData, pDstSize,
190
0
                                   flags);
191
0
        break;
192
193
0
      case PACKET_COMPR_TYPE_RDP61:
194
0
        status = xcrush_decompress(bulk->xcrushRecv, pSrcData, SrcSize, ppDstData, pDstSize,
195
0
                                   flags);
196
0
        break;
197
198
0
      case PACKET_COMPR_TYPE_RDP8:
199
0
        WLog_ERR(TAG, "Unsupported bulk compression type %08" PRIx32,
200
0
                 bulk->CompressionLevel);
201
0
        status = -1;
202
0
        break;
203
0
      default:
204
0
        WLog_ERR(TAG, "Unknown bulk compression type %08" PRIx32, bulk->CompressionLevel);
205
0
        status = -1;
206
0
        break;
207
0
    }
208
0
  }
209
0
  else
210
0
  {
211
0
    *ppDstData = pSrcData;
212
0
    *pDstSize = SrcSize;
213
0
    status = 0;
214
0
  }
215
216
0
  if (status >= 0)
217
0
  {
218
0
    const UINT32 CompressedBytes = SrcSize;
219
0
    const UINT32 UncompressedBytes = *pDstSize;
220
0
    const double CompressionRatio =
221
0
        metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes);
222
#ifdef WITH_BULK_DEBUG
223
    {
224
      WLog_DBG(TAG,
225
               "Decompress Type: %" PRIu32 " Flags: %s (0x%08" PRIX32
226
               ") Compression Ratio: %f (%" PRIu32 " / %" PRIu32 "), Total: %f (%" PRIu64
227
               " / %" PRIu64 ")",
228
               type, bulk_get_compression_flags_string(flags), flags, CompressionRatio,
229
               CompressedBytes, UncompressedBytes, metrics->TotalCompressionRatio,
230
               metrics->TotalCompressedBytes, metrics->TotalUncompressedBytes);
231
    }
232
#else
233
0
    WINPR_UNUSED(CompressionRatio);
234
0
#endif
235
0
  }
236
0
  else
237
0
  {
238
0
    WLog_ERR(TAG, "Decompression failure!");
239
0
  }
240
241
0
  return status;
242
0
}
243
244
int bulk_compress(rdpBulk* WINPR_RESTRICT bulk, const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
245
                  const BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize,
246
                  UINT32* WINPR_RESTRICT pFlags)
247
0
{
248
0
  int status = -1;
249
250
0
  WINPR_ASSERT(bulk);
251
0
  WINPR_ASSERT(bulk->context);
252
0
  WINPR_ASSERT(pSrcData);
253
0
  WINPR_ASSERT(ppDstData);
254
0
  WINPR_ASSERT(pDstSize);
255
256
0
  rdpMetrics* metrics = bulk->context->metrics;
257
0
  WINPR_ASSERT(metrics);
258
259
0
  if ((SrcSize <= 50) || (SrcSize >= 16384))
260
0
  {
261
0
    *ppDstData = pSrcData;
262
0
    *pDstSize = SrcSize;
263
0
    return 0;
264
0
  }
265
266
0
  *pDstSize = sizeof(bulk->OutputBuffer);
267
0
  const UINT32 CompressionLevel = bulk_compression_level(bulk);
268
0
  bulk_update_compression_max_size(bulk);
269
270
0
  switch (CompressionLevel)
271
0
  {
272
0
    case PACKET_COMPR_TYPE_8K:
273
0
    case PACKET_COMPR_TYPE_64K:
274
0
      mppc_set_compression_level(bulk->mppcSend, CompressionLevel);
275
0
      status = mppc_compress(bulk->mppcSend, pSrcData, SrcSize, bulk->OutputBuffer, ppDstData,
276
0
                             pDstSize, pFlags);
277
0
      break;
278
0
    case PACKET_COMPR_TYPE_RDP6:
279
0
      status = ncrush_compress(bulk->ncrushSend, pSrcData, SrcSize, bulk->OutputBuffer,
280
0
                               ppDstData, pDstSize, pFlags);
281
0
      break;
282
0
    case PACKET_COMPR_TYPE_RDP61:
283
0
      status = xcrush_compress(bulk->xcrushSend, pSrcData, SrcSize, bulk->OutputBuffer,
284
0
                               ppDstData, pDstSize, pFlags);
285
0
      break;
286
0
    case PACKET_COMPR_TYPE_RDP8:
287
0
      WLog_ERR(TAG, "Unsupported bulk compression type %08" PRIx32, CompressionLevel);
288
0
      status = -1;
289
0
      break;
290
0
    default:
291
0
      WLog_ERR(TAG, "Unknown bulk compression type %08" PRIx32, CompressionLevel);
292
0
      status = -1;
293
0
      break;
294
0
  }
295
296
0
  if (status >= 0)
297
0
  {
298
0
    const UINT32 CompressedBytes = *pDstSize;
299
0
    const UINT32 UncompressedBytes = SrcSize;
300
0
    const double CompressionRatio =
301
0
        metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes);
302
#ifdef WITH_BULK_DEBUG
303
    {
304
      WLog_DBG(TAG,
305
               "Compress Type: %" PRIu32 " Flags: %s (0x%08" PRIX32
306
               ") Compression Ratio: %f (%" PRIu32 " / %" PRIu32 "), Total: %f (%" PRIu64
307
               " / %" PRIu64 ")",
308
               bulk->CompressionLevel, bulk_get_compression_flags_string(*pFlags), *pFlags,
309
               CompressionRatio, CompressedBytes, UncompressedBytes,
310
               metrics->TotalCompressionRatio, metrics->TotalCompressedBytes,
311
               metrics->TotalUncompressedBytes);
312
    }
313
#else
314
0
    WINPR_UNUSED(CompressionRatio);
315
0
#endif
316
0
  }
317
318
#if defined(WITH_BULK_DEBUG)
319
320
  if (bulk_compress_validate(bulk, pSrcData, SrcSize, *ppDstData, *pDstSize, *pFlags) < 0)
321
    status = -1;
322
323
#endif
324
0
  return status;
325
0
}
326
327
void bulk_reset(rdpBulk* WINPR_RESTRICT bulk)
328
0
{
329
0
  WINPR_ASSERT(bulk);
330
331
0
  mppc_context_reset(bulk->mppcSend, FALSE);
332
0
  mppc_context_reset(bulk->mppcRecv, FALSE);
333
0
  ncrush_context_reset(bulk->ncrushRecv, FALSE);
334
0
  ncrush_context_reset(bulk->ncrushSend, FALSE);
335
0
  xcrush_context_reset(bulk->xcrushRecv, FALSE);
336
0
  xcrush_context_reset(bulk->xcrushSend, FALSE);
337
0
}
338
339
rdpBulk* bulk_new(rdpContext* context)
340
0
{
341
0
  rdpBulk* bulk = NULL;
342
0
  WINPR_ASSERT(context);
343
344
0
  bulk = (rdpBulk*)calloc(1, sizeof(rdpBulk));
345
346
0
  if (!bulk)
347
0
    goto fail;
348
349
0
  bulk->context = context;
350
0
  bulk->mppcSend = mppc_context_new(1, TRUE);
351
0
  if (!bulk->mppcSend)
352
0
    goto fail;
353
0
  bulk->mppcRecv = mppc_context_new(1, FALSE);
354
0
  if (!bulk->mppcRecv)
355
0
    goto fail;
356
0
  bulk->ncrushRecv = ncrush_context_new(FALSE);
357
0
  if (!bulk->ncrushRecv)
358
0
    goto fail;
359
0
  bulk->ncrushSend = ncrush_context_new(TRUE);
360
0
  if (!bulk->ncrushSend)
361
0
    goto fail;
362
0
  bulk->xcrushRecv = xcrush_context_new(FALSE);
363
0
  if (!bulk->xcrushRecv)
364
0
    goto fail;
365
0
  bulk->xcrushSend = xcrush_context_new(TRUE);
366
0
  if (!bulk->xcrushSend)
367
0
    goto fail;
368
0
  bulk->CompressionLevel = context->settings->CompressionLevel;
369
370
0
  return bulk;
371
0
fail:
372
0
  WINPR_PRAGMA_DIAG_PUSH
373
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
374
0
  bulk_free(bulk);
375
0
  WINPR_PRAGMA_DIAG_POP
376
0
  return NULL;
377
0
}
378
379
void bulk_free(rdpBulk* bulk)
380
0
{
381
0
  if (!bulk)
382
0
    return;
383
384
0
  mppc_context_free(bulk->mppcSend);
385
0
  mppc_context_free(bulk->mppcRecv);
386
0
  ncrush_context_free(bulk->ncrushRecv);
387
0
  ncrush_context_free(bulk->ncrushSend);
388
0
  xcrush_context_free(bulk->xcrushRecv);
389
0
  xcrush_context_free(bulk->xcrushSend);
390
0
  free(bulk);
391
0
}