21 #include <winpr/assert.h>
23 #include <freerdp/config.h>
25 #include "../core/settings.h"
27 #include "../codec/mppc.h"
28 #include "../codec/ncrush.h"
29 #include "../codec/xcrush.h"
31 #include <freerdp/log.h>
32 #define TAG FREERDP_TAG("core")
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];
50 #if defined(WITH_BULK_DEBUG)
51 static INLINE
const char* bulk_get_compression_flags_string(UINT32 flags)
53 flags &= BULK_COMPRESSION_FLAGS_MASK;
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";
72 return "PACKET_UNKNOWN";
76 static UINT32 bulk_compression_level(rdpBulk* WINPR_RESTRICT bulk)
78 rdpSettings* settings = NULL;
80 WINPR_ASSERT(bulk->context);
81 settings = bulk->context->settings;
82 WINPR_ASSERT(settings);
83 bulk->CompressionLevel = (settings->CompressionLevel >= PACKET_COMPR_TYPE_RDP61)
84 ? PACKET_COMPR_TYPE_RDP61
85 : settings->CompressionLevel;
86 WINPR_ASSERT(bulk->CompressionLevel <= UINT16_MAX);
87 return bulk->CompressionLevel;
90 UINT16 bulk_compression_max_size(rdpBulk* WINPR_RESTRICT bulk)
93 (void)bulk_compression_level(bulk);
94 bulk->CompressionMaxSize = (bulk->CompressionLevel < PACKET_COMPR_TYPE_64K) ? 8192 : UINT16_MAX;
95 return bulk->CompressionMaxSize;
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)
103 const BYTE* v_pSrcData = NULL;
104 const BYTE* v_pDstData = NULL;
105 UINT32 v_SrcSize = 0;
106 UINT32 v_DstSize = 0;
110 WINPR_ASSERT(pSrcData);
111 WINPR_ASSERT(pDstData);
113 v_pSrcData = pDstData;
115 v_Flags = Flags | bulk->CompressionLevel;
116 status = bulk_decompress(bulk, v_pSrcData, v_SrcSize, &v_pDstData, &v_DstSize, v_Flags);
120 WLog_DBG(TAG,
"compression/decompression failure");
124 if (v_DstSize != SrcSize)
127 "compression/decompression size mismatch: Actual: %" PRIu32
", Expected: %" PRIu32
133 if (memcmp(v_pDstData, pSrcData, SrcSize) != 0)
135 WLog_DBG(TAG,
"compression/decompression input/output mismatch! flags: 0x%08" PRIX32
"",
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);
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)
157 WINPR_ASSERT(bulk->context);
158 WINPR_ASSERT(pSrcData);
159 WINPR_ASSERT(ppDstData);
160 WINPR_ASSERT(pDstSize);
162 rdpMetrics* metrics = bulk->context->metrics;
163 WINPR_ASSERT(metrics);
165 (void)bulk_compression_max_size(bulk);
166 const UINT32 type = flags & BULK_COMPRESSION_TYPE_MASK;
168 if (flags & BULK_COMPRESSION_FLAGS_MASK)
172 case PACKET_COMPR_TYPE_8K:
173 mppc_set_compression_level(bulk->mppcRecv, 0);
175 mppc_decompress(bulk->mppcRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
178 case PACKET_COMPR_TYPE_64K:
179 mppc_set_compression_level(bulk->mppcRecv, 1);
181 mppc_decompress(bulk->mppcRecv, pSrcData, SrcSize, ppDstData, pDstSize, flags);
184 case PACKET_COMPR_TYPE_RDP6:
185 status = ncrush_decompress(bulk->ncrushRecv, pSrcData, SrcSize, ppDstData, pDstSize,
189 case PACKET_COMPR_TYPE_RDP61:
190 status = xcrush_decompress(bulk->xcrushRecv, pSrcData, SrcSize, ppDstData, pDstSize,
194 case PACKET_COMPR_TYPE_RDP8:
195 WLog_ERR(TAG,
"Unsupported bulk compression type %08" PRIx32,
196 bulk->CompressionLevel);
200 WLog_ERR(TAG,
"Unknown bulk compression type %08" PRIx32, bulk->CompressionLevel);
207 *ppDstData = pSrcData;
214 const UINT32 CompressedBytes = SrcSize;
215 const UINT32 UncompressedBytes = *pDstSize;
216 const double CompressionRatio =
217 metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes);
218 #ifdef WITH_BULK_DEBUG
221 "Decompress Type: %" PRIu32
" Flags: %s (0x%08" PRIX32
222 ") Compression Ratio: %f (%" PRIu32
" / %" PRIu32
"), Total: %f (%" PRIu64
224 type, bulk_get_compression_flags_string(flags), flags, CompressionRatio,
225 CompressedBytes, UncompressedBytes, metrics->TotalCompressionRatio,
226 metrics->TotalCompressedBytes, metrics->TotalUncompressedBytes);
229 WINPR_UNUSED(CompressionRatio);
234 WLog_ERR(TAG,
"Decompression failure!");
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)
247 WINPR_ASSERT(bulk->context);
248 WINPR_ASSERT(pSrcData);
249 WINPR_ASSERT(ppDstData);
250 WINPR_ASSERT(pDstSize);
252 rdpMetrics* metrics = bulk->context->metrics;
253 WINPR_ASSERT(metrics);
255 if ((SrcSize <= 50) || (SrcSize >= 16384))
257 *ppDstData = pSrcData;
262 *pDstSize =
sizeof(bulk->OutputBuffer);
263 (void)bulk_compression_level(bulk);
264 (void)bulk_compression_max_size(bulk);
266 switch (bulk->CompressionLevel)
268 case PACKET_COMPR_TYPE_8K:
269 case PACKET_COMPR_TYPE_64K:
270 mppc_set_compression_level(bulk->mppcSend, bulk->CompressionLevel);
271 status = mppc_compress(bulk->mppcSend, pSrcData, SrcSize, bulk->OutputBuffer, ppDstData,
274 case PACKET_COMPR_TYPE_RDP6:
275 status = ncrush_compress(bulk->ncrushSend, pSrcData, SrcSize, bulk->OutputBuffer,
276 ppDstData, pDstSize, pFlags);
278 case PACKET_COMPR_TYPE_RDP61:
279 status = xcrush_compress(bulk->xcrushSend, pSrcData, SrcSize, bulk->OutputBuffer,
280 ppDstData, pDstSize, pFlags);
282 case PACKET_COMPR_TYPE_RDP8:
283 WLog_ERR(TAG,
"Unsupported bulk compression type %08" PRIx32, bulk->CompressionLevel);
287 WLog_ERR(TAG,
"Unknown bulk compression type %08" PRIx32, bulk->CompressionLevel);
294 const UINT32 CompressedBytes = *pDstSize;
295 const UINT32 UncompressedBytes = SrcSize;
296 const double CompressionRatio =
297 metrics_write_bytes(metrics, UncompressedBytes, CompressedBytes);
298 #ifdef WITH_BULK_DEBUG
301 "Compress Type: %" PRIu32
" Flags: %s (0x%08" PRIX32
302 ") Compression Ratio: %f (%" PRIu32
" / %" PRIu32
"), Total: %f (%" PRIu64
304 bulk->CompressionLevel, bulk_get_compression_flags_string(*pFlags), *pFlags,
305 CompressionRatio, CompressedBytes, UncompressedBytes,
306 metrics->TotalCompressionRatio, metrics->TotalCompressedBytes,
307 metrics->TotalUncompressedBytes);
310 WINPR_UNUSED(CompressionRatio);
314 #if defined(WITH_BULK_DEBUG)
316 if (bulk_compress_validate(bulk, pSrcData, SrcSize, *ppDstData, *pDstSize, *pFlags) < 0)
323 void bulk_reset(rdpBulk* WINPR_RESTRICT bulk)
327 mppc_context_reset(bulk->mppcSend, FALSE);
328 mppc_context_reset(bulk->mppcRecv, FALSE);
329 ncrush_context_reset(bulk->ncrushRecv, FALSE);
330 ncrush_context_reset(bulk->ncrushSend, FALSE);
331 xcrush_context_reset(bulk->xcrushRecv, FALSE);
332 xcrush_context_reset(bulk->xcrushSend, FALSE);
335 rdpBulk* bulk_new(rdpContext* context)
337 rdpBulk* bulk = NULL;
338 WINPR_ASSERT(context);
340 bulk = (rdpBulk*)calloc(1,
sizeof(rdpBulk));
345 bulk->context = context;
346 bulk->mppcSend = mppc_context_new(1, TRUE);
349 bulk->mppcRecv = mppc_context_new(1, FALSE);
352 bulk->ncrushRecv = ncrush_context_new(FALSE);
353 if (!bulk->ncrushRecv)
355 bulk->ncrushSend = ncrush_context_new(TRUE);
356 if (!bulk->ncrushSend)
358 bulk->xcrushRecv = xcrush_context_new(FALSE);
359 if (!bulk->xcrushRecv)
361 bulk->xcrushSend = xcrush_context_new(TRUE);
362 if (!bulk->xcrushSend)
364 bulk->CompressionLevel = context->settings->CompressionLevel;
368 WINPR_PRAGMA_DIAG_PUSH
369 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
371 WINPR_PRAGMA_DIAG_POP
375 void bulk_free(rdpBulk* bulk)
380 mppc_context_free(bulk->mppcSend);
381 mppc_context_free(bulk->mppcRecv);
382 ncrush_context_free(bulk->ncrushRecv);
383 ncrush_context_free(bulk->ncrushSend);
384 xcrush_context_free(bulk->xcrushRecv);
385 xcrush_context_free(bulk->xcrushSend);