Replace byte-by-byte history buffer copies with copy_within (memmove)
for non-overlapping LZ77 matches in MPPC, NCRUSH, and XCRUSH
decompression. Use 8-byte u64 XOR chunk comparison in NCRUSH and XCRUSH
match-length finding instead of per-byte comparison.
Both optimizations are safe Rust, WASM-compatible, and scale with match
length. Add criterion benchmarks for all four compression algorithms.
Co-authored-by: Cursor <cursoragent@cursor.com>
The FastPath processor previously only read one update from each
FastPath output PDU, but the RDP spec allows multiple updates per PDU.
When the server uses bulk compression, it packs multiple updates into a
single PDU, causing fragment continuations (Next/Last) and subsequent
updates to be silently dropped. This led to bitmap decode failures and
decompressor state desynchronization.
Extract the per-update logic into process_single_update() and loop over
all updates in the PDU payload.
Co-authored-by: Cursor <cursoragent@cursor.com>
- Add --compression CLI argument (rdp4/rdp5/rdp6/rdp61) to ironrdp-client
- Add compression_type field to connector::Config and ConnectionResult
- Set INFO_COMPRESSION flag and compression type in Client Info PDU
- Add ironrdp-bulk dependency to ironrdp-session
- Integrate BulkCompressor into FastPath processor for server-to-client
decompression with per-PDU compression flag handling
- Add compression metrics (total_compressed_bytes, total_uncompressed_bytes,
compression_ratio) to BulkCompressor, mirroring FreeRDP's rdpMetrics
- Add tracing logs at negotiation, initialization, and per-PDU decompression
- Replace .expect() calls with non-panicking patterns in xcrush
- Add bounds safety to compressed_data() in bulk.rs
- Fix cargo fmt across all crates
- Update all call sites (ffi, web, testsuite, examples) for new fields
Co-authored-by: Cursor <cursoragent@cursor.com>
Fixed no_std+alloc support by adding missing Vec/vec! imports from
alloc crate in bulk.rs and ncrush/mod.rs. All three build configs
now succeed:
- wasm32-unknown-unknown (default features / std)
- wasm32-unknown-unknown (no_std + alloc)
- native (all 130 tests pass)
Zero platform-specific dependencies confirmed.
Co-authored-by: Cursor <cursoragent@cursor.com>
All 130 tests pass in both debug and release mode.
37 byte-exact and roundtrip tests confirm no output differences
after the safety refactoring in TASK-033 and TASK-034.
Phase 10 (Memory Safety Refactoring) complete.
Co-authored-by: Cursor <cursoragent@cursor.com>
Replace ~60 widening `as` casts with idiomatic `From`/`into()`:
- u8→usize, u8→u32, u16→usize, u16→u32, u16→i32 conversions
- Byte extraction via `to_be_bytes()`/`to_le_bytes()` instead of
manual shift-and-mask patterns
All remaining narrowing casts are documented with
`#[expect(clippy::as_conversions)]` and reason strings explaining
why each is bounded. Zero `as_conversions` clippy warnings remain.
All 130 tests pass with byte-exact output preserved.
Co-authored-by: Cursor <cursoragent@cursor.com>
Comprehensive safety audit of the ironrdp-bulk crate found:
- Zero unsafe blocks in the entire crate
- Zero raw pointer operations (*const, *mut, as_ptr, as_mut_ptr)
- Zero transmute or unchecked operations
- Zero safety lint suppressions
Added #![forbid(unsafe_code)] to lib.rs to enforce this invariant at
compile time. Documented the audit findings in a crate-level doc comment
including the inventory of ~161 `as` casts (tracked for TASK-034) and
2 infallible unwrap_or_else(unreachable) calls.
Co-authored-by: Cursor <cursoragent@cursor.com>
Add compress() and decompress() routing methods to BulkCompressor:
- decompress(): extracts compression type from flags (low 4 bits),
routes to MPPC (level 0/1), NCRUSH, or XCRUSH. Returns source data
unchanged if no compression flags are set.
- compress(): routes to the configured algorithm based on
compression_level. Skips compression for sizes <=50 or >=16384.
Compressed output goes into the internal 64KB output buffer.
- compressed_data(): accessor to retrieve compressed bytes from the
internal buffer after a successful compress() call.
All 117 tests pass.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port the rdp_bulk coordinator struct from FreeRDP. BulkCompressor holds
send/receive context pairs for all three compression algorithms (MPPC,
NCRUSH, XCRUSH) plus a 64KB output buffer for compressed data.
- BulkCompressor::new(compression_level) creates all 6 contexts
- compression_level() returns the configured outbound algorithm
- should_skip_compression() checks size bounds (<=50 or >=16384)
- reset() resets all 6 contexts
- Accessor methods for each send/receive context
- Exported BulkCompressor from lib.rs
Added 9 unit tests. All 117 tests pass.
Co-authored-by: Cursor <cursoragent@cursor.com>
Add 5 round-trip tests that compress then decompress and verify the
output matches the original input byte-for-byte:
- test_ncrush_roundtrip_bells: FreeRDP "bells" test data
- test_ncrush_roundtrip_repetitive: "ABCABC..." pattern
- test_ncrush_roundtrip_prose: longer text with repeated phrases
- test_ncrush_roundtrip_binary: all 256 byte values repeated twice
- test_ncrush_roundtrip_sequential: multiple messages on the same
compressor/decompressor context pair (tests history state carryover)
All 108 tests pass. This completes Phase 8 (NCRUSH Compression & Tests).
Co-authored-by: Cursor <cursoragent@cursor.com>
Add byte-exact compression test ported from FreeRDP's
test_NCrushCompressBells. Compresses the "bells" test string with
a fresh compressor context and verifies the output matches FreeRDP's
expected 44-byte compressed output exactly.
Test passes on first try, confirming the full NCRUSH compression
pipeline (hash chains, LRU offset cache, Huffman encoding, EOS)
produces bit-identical output to FreeRDP.
Also fixed unused variable and unused mut warnings. All 103 tests pass.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port the full NCRUSH compression pipeline from FreeRDP's ncrush_compress:
- Window management: flush detection and encoder window sliding at 32KB
- Hash chain population and source data copy into history buffer
- Main compression loop with literal and match encoding paths
- LRU offset cache management (4-entry cache with swap logic)
- Offset cache hit vs miss encoding using TASK-026 helpers
- Trailing literal encoding for last 0-2 bytes
- EOS marker and output finalization
- Output size validation (compressed must not exceed source)
Fixed critical bug: self.history_offset must be set to end of valid
data (history_offset + src_size) before the compression loop, since
find_best_match reads it as the limit for find_match_length.
Added 5 unit tests for compress: basic, repeats, empty, history
offset update, and offset cache update. All 102 tests pass.
Co-authored-by: Cursor <cursoragent@cursor.com>
Add NCrushBitWriter (LSB-first bit accumulator) and Huffman encoding
helpers for NCRUSH compression: encode_literal, encode_copy_offset,
encode_offset_cache_hit, encode_length_of_match, encode_eos, and
get_lec_code. These port the encoding patterns from FreeRDP's
NCrushWriteStart/WriteBits/WriteFinish and the LEC/LOM Huffman
table lookups used in ncrush_compress.
Includes 12 unit tests covering bit writer mechanics, literal
encoding, EOS marker, offset cache hits, LOM encoding, and
copy offset encoding. All 97 tests pass.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port hash_table_add, find_match_length, find_best_match, and
move_encoder_windows from FreeRDP. Uses 2-byte direct hash into
65536-entry hash table with match table chain traversal (up to
4 candidates with 6-step unrolled quick-filter). Add 9 unit tests
for hash insertion, chain creation, match finding, and window
sliding. 86 tests passing.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port ncrush_decompress from FreeRDP with LSB-first bit reading,
Huffman LEC/LOM decoding, CopyOffset with extra bits, LRU offset
cache rotation, and history buffer copy with overlap/wrap handling.
Includes fetch_bits helper and 7 unit tests (76 total passing).
Co-authored-by: Cursor <cursoragent@cursor.com>
Port xcrush_compress_l1 and xcrush_compress from FreeRDP:
- compress_l1: copies src to history, runs chunk computation → match
finding → optimization → output generation pipeline; falls back to
L1_NO_COMPRESSION when no matches found or input ≤ 50 bytes
- compress: two-level pipeline — L1 chunk matching then L2 MPPC
compression with proper flag header, fallback handling, and
compression flags carry-over between calls
Co-authored-by: Cursor <cursoragent@cursor.com>
Port rolling hash chunk computation from FreeRDP's xcrush_compute_chunks:
- update_hash: djb2-style hash over first 32 bytes of a chunk
- append_chunk: records chunk signature if size >= 15
- compute_signatures: 32-byte rolling hash window with boundary when
accumulator & 0x7F == 0, producing variable-size chunk signatures
Co-authored-by: Cursor <cursoragent@cursor.com>
Port xcrush_decompress_l1 and xcrush_decompress from FreeRDP:
- Level-1 decompression: parses RDP 6.1 match details header, interleaves
literal copies with history match copies using copy_within for overlap safety
- Full decompression: handles all flag combinations — L1-only, L1+L2 (MPPC),
no-compression passthrough, and PACKET_FLUSHED history reset
- 8 new unit tests covering L1 decompression, full decompression, error cases
Co-authored-by: Cursor <cursoragent@cursor.com>
Define all XCRUSH data types ported from FreeRDP's xcrush.c:
- XCrushContext: 2MB history buffer, chunk tables, signature arrays,
match arrays, and inner MPPC context
- XCrushMatchInfo, XCrushChunk, XCrushSignature: helper structs
- Rdp61MatchDetails, Rdp61CompressedData: RDP 6.1 wire format types
Large buffers allocated on the heap via vec! to avoid stack overflow
(2MB history + 512KB chunks + 128KB next_chunks).
Co-authored-by: Cursor <cursoragent@cursor.com>
Add 6 round-trip tests that compress then decompress data and verify
the output matches the original input:
- Small text (49B "bells") with RDP4 and RDP5
- Medium text (386B "island") with RDP5
- Large binary buffer (6.5KB) with RDP5
- 16KB repeating pattern with RDP4 and RDP5
Completes Phase 4 (MPPC Compression): all 3 tasks done, 38 total
tests passing.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port all four compression tests from FreeRDP's TestFreeRDPCodecMppc.c:
- test_mppc_compress_bells_rdp4 (RDP4 "bells" text)
- test_mppc_compress_bells_rdp5 (RDP5 "bells" text)
- test_mppc_compress_island_rdp5 (RDP5 John Donne "island" excerpt)
- test_mppc_compress_buffer_rdp5 (RDP5 large binary buffer)
All tests produce byte-exact output matching FreeRDP's reference
implementation. Added TEST_ISLAND_DATA (386 bytes) and
TEST_ISLAND_DATA_RDP5 (297 bytes) test vectors to test_data.rs.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port mppc_compress from FreeRDP's libfreerdp/codec/mppc.c to Rust.
Uses LZ77 with 3-byte hash matching (MPPC_MATCH_INDEX) for finding
repeated sequences in the history buffer. Encodes literals, copy-offset
and length-of-match using BitStreamWriter with different encoding
tables for RDP4 (8K) and RDP5 (64K) compression levels. Falls back to
PACKET_FLUSHED when compressed output exceeds source size.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port three decompression test vectors from FreeRDP's TestFreeRDPCodecMppc.c:
- test_mppc_decompress_bells_rdp4: RDP4 (8K) decompression of
"for.whom.the.bell.tolls,.the.bell.tolls.for.thee!" (33 bytes → 49 bytes)
- test_mppc_decompress_bells_rdp5: RDP5 (64K) decompression of the same
string (34 bytes → 49 bytes)
- test_mppc_decompress_buffer_rdp5: Large binary buffer RDP5 decompression
(2835 bytes → 6496 bytes)
All three tests produce byte-for-byte identical output to FreeRDP.
New file: test_data.rs contains all test vectors extracted from the C source.
Phase 3 (MPPC Decompression) is now complete.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port FreeRDP's mppc_decompress() to Rust with full support for both
RDP4 (8K history) and RDP5 (64K history) compression levels. Implements:
- PACKET_FLUSHED and PACKET_AT_FRONT flag handling
- Literal decoding (< 0x80 with 0-prefix, >= 0x80 with 10-prefix)
- CopyOffset decoding with RDP4/RDP5 encoding variants
- Unary-coded LengthOfMatch decoding (ranges 3 to 65535)
- History buffer copy with wrapping via mask (0x1FFF/0xFFFF)
- Zero-copy passthrough for uncompressed packets via shared lifetime
Co-authored-by: Cursor <cursoragent@cursor.com>
Add MppcContext with Box-allocated history buffer (64K) and match
buffer (32K), supporting both RDP4 (8K) and RDP5 (64K) modes.
Includes new(), reset(flush), set_compression_level(), and
accessor methods for compression_level, history_buffer_size,
history_mask, and is_compressor.
Co-authored-by: Cursor <cursoragent@cursor.com>
Port FreeRDP's wBitStream write operations to Rust. The writer uses a
32-bit accumulator that flushes big-endian to the output buffer when
full, matching the exact semantics of BitStream_Write_Bits and
BitStream_Flush.
API: new(), write_bits(), flush(), bits_written(), byte_length().
Co-authored-by: Cursor <cursoragent@cursor.com>
Port FreeRDP's wBitStream read operations to Rust. The reader uses a
32-bit accumulator with big-endian prefetch, matching the exact
semantics of BitStream_Fetch, BitStream_Prefetch, and BitStream_Shift.
API: new(), read_bits(), peek_bits(), shift(), accumulator(),
remaining_bits(), bits_consumed(), shift32().
Co-authored-by: Cursor <cursoragent@cursor.com>
Create the ironrdp-bulk crate under crates/ironrdp-bulk/ with:
- Cargo.toml with no_std/alloc/std feature flags
- Minimal lib.rs with crate attributes following IronRDP conventions
The crate is automatically included in the workspace via the
crates/* glob in the root Cargo.toml.
Co-authored-by: Cursor <cursoragent@cursor.com>
Add structured project plan for porting FreeRDP's bulk compressor
support (MPPC, XCRUSH, NCRUSH) to IronRDP. The plan covers 37 tasks
across 11 phases, from crate setup through memory safety refactoring
and WebAssembly compatibility.
Co-authored-by: Cursor <cursoragent@cursor.com>
Adds `SendFileContentsResponse` to `ClipboardMessage` enum, enabling
clipboard
backends to signal when file data is ready to send via
`submit_file_contents()`.
This provides the message-based interface pattern used consistently by
server
implementations for clipboard operations.
Co-authored-by: lamco-office <office@lamco.io>
Per [MS-RDPECLIP section 2.2.5.3][file-contents-spec], the Local
Clipboard Owner
sends File Contents Request PDU to retrieve file data from the Shared
Clipboard
Owner during paste operations.
This enables server implementations to request file contents from
clients,
completing the bidirectional file transfer capability.
[file-contents-spec]:
https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpeclip/cbc851d3-4e68-45f4-9292-26872a9209f2
Co-authored-by: lamco-office <office@lamco.io>