ironrdp_pdu/rdp/capability_sets/
bitmap_codecs.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#[cfg(test)]
mod tests;

use bitflags::bitflags;
use ironrdp_core::{
    cast_length, decode, ensure_fixed_part_size, ensure_size, invalid_field_err, other_err, Decode, DecodeResult,
    Encode, EncodeResult, ReadCursor, WriteCursor,
};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};

const RFX_ICAP_VERSION: u16 = 0x0100;
const RFX_ICAP_TILE_SIZE: u16 = 0x40;
const RFX_ICAP_COLOR_CONVERSION: u8 = 1;
const RFX_ICAP_TRANSFORM_BITS: u8 = 1;
const RFX_ICAP_LENGTH: usize = 8;

const RFX_CAPSET_BLOCK_TYPE: u16 = 0xcbc1;
const RFX_CAPSET_TYPE: u16 = 0xcfc0;
const RFX_CAPSET_STATIC_DATA_LENGTH: usize = 13;

const RFX_CAPS_BLOCK_TYPE: u16 = 0xcbc0;
const RFX_CAPS_BLOCK_LENGTH: u32 = 8;
const RFX_CAPS_NUM_CAPSETS: u16 = 1;
const RFX_CAPS_STATIC_DATA_LENGTH: usize = 8;

const RFX_CLIENT_CAPS_CONTAINER_STATIC_DATA_LENGTH: usize = 12;

const NSCODEC_LENGTH: usize = 3;
const CODEC_STATIC_DATA_LENGTH: usize = 19;

#[rustfmt::skip]
const GUID_NSCODEC: Guid = Guid(0xca8d_1bb9, 0x000f, 0x154f, 0x58, 0x9f, 0xae, 0x2d, 0x1a, 0x87, 0xe2, 0xd6);
#[rustfmt::skip]
const GUID_REMOTEFX: Guid = Guid(0x7677_2f12, 0xbd72, 0x4463, 0xaf, 0xb3, 0xb7, 0x3c, 0x9c, 0x6f, 0x78, 0x86);
#[rustfmt::skip]
const GUID_IMAGE_REMOTEFX: Guid = Guid(0x2744_ccd4, 0x9d8a, 0x4e74, 0x80, 0x3c, 0x0e, 0xcb, 0xee, 0xa1, 0x9c, 0x54);
#[rustfmt::skip]
const GUID_IGNORE: Guid = Guid(0x9c43_51a6, 0x3535, 0x42ae, 0x91, 0x0c, 0xcd, 0xfc, 0xe5, 0x76, 0x0b, 0x58);

#[derive(Debug, PartialEq, Eq)]
pub struct Guid(u32, u16, u16, u8, u8, u8, u8, u8, u8, u8, u8);

impl Guid {
    const NAME: &'static str = "Guid";

    const FIXED_PART_SIZE: usize = 16;
}

impl Encode for Guid {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_fixed_part_size!(in: dst);

        dst.write_u32(self.0);
        dst.write_u16(self.1);
        dst.write_u16(self.2);
        dst.write_u8(self.3);
        dst.write_u8(self.4);
        dst.write_u8(self.5);
        dst.write_u8(self.6);
        dst.write_u8(self.7);
        dst.write_u8(self.8);
        dst.write_u8(self.9);
        dst.write_u8(self.10);

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

impl<'de> Decode<'de> for Guid {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let guid1 = src.read_u32();
        let guid2 = src.read_u16();
        let guid3 = src.read_u16();
        let guid4 = src.read_u8();
        let guid5 = src.read_u8();
        let guid6 = src.read_u8();
        let guid7 = src.read_u8();
        let guid8 = src.read_u8();
        let guid9 = src.read_u8();
        let guid10 = src.read_u8();
        let guid11 = src.read_u8();

        Ok(Guid(
            guid1, guid2, guid3, guid4, guid5, guid6, guid7, guid8, guid9, guid10, guid11,
        ))
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BitmapCodecs(pub Vec<Codec>);

impl BitmapCodecs {
    const NAME: &'static str = "BitmapCodecs";

    const FIXED_PART_SIZE: usize = 1 /* len */;
}

impl Encode for BitmapCodecs {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());

        dst.write_u8(cast_length!("len", self.0.len())?);

        for codec in self.0.iter() {
            codec.encode(dst)?;
        }

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.0.iter().map(Encode::size).sum::<usize>()
    }
}

impl<'de> Decode<'de> for BitmapCodecs {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let codecs_count = src.read_u8();

        let mut codecs = Vec::with_capacity(codecs_count as usize);
        for _ in 0..codecs_count {
            codecs.push(Codec::decode(src)?);
        }

        Ok(BitmapCodecs(codecs))
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Codec {
    pub id: u8,
    pub property: CodecProperty,
}

impl Codec {
    const NAME: &'static str = "Codec";

    const FIXED_PART_SIZE: usize = CODEC_STATIC_DATA_LENGTH;
}

impl Encode for Codec {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());

        let guid = match &self.property {
            CodecProperty::NsCodec(_) => GUID_NSCODEC,
            CodecProperty::RemoteFx(_) => GUID_REMOTEFX,
            CodecProperty::ImageRemoteFx(_) => GUID_IMAGE_REMOTEFX,
            CodecProperty::Ignore => GUID_IGNORE,
            _ => return Err(other_err!("invalid codec")),
        };
        guid.encode(dst)?;

        dst.write_u8(self.id);

        match &self.property {
            CodecProperty::NsCodec(p) => {
                dst.write_u16(cast_length!("len", p.size())?);
                p.encode(dst)?;
            }
            CodecProperty::RemoteFx(p) => {
                match p {
                    RemoteFxContainer::ClientContainer(container) => {
                        dst.write_u16(cast_length!("len", container.size())?);
                        container.encode(dst)?;
                    }
                    RemoteFxContainer::ServerContainer(size) => {
                        dst.write_u16(cast_length!("len", *size)?);
                        let buff = vec![0u8; *size];
                        dst.write_slice(&buff);
                    }
                };
            }
            CodecProperty::ImageRemoteFx(p) => {
                match p {
                    RemoteFxContainer::ClientContainer(container) => {
                        dst.write_u16(cast_length!("len", container.size())?);
                        container.encode(dst)?;
                    }
                    RemoteFxContainer::ServerContainer(size) => {
                        dst.write_u16(cast_length!("len", *size)?);
                        let buff = vec![0u8; *size];
                        dst.write_slice(&buff);
                    }
                };
            }
            CodecProperty::Ignore => dst.write_u16(0),
            CodecProperty::None => dst.write_u16(0),
        };

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
            + match &self.property {
                CodecProperty::NsCodec(p) => p.size(),
                CodecProperty::RemoteFx(p) => match p {
                    RemoteFxContainer::ClientContainer(container) => container.size(),
                    RemoteFxContainer::ServerContainer(size) => *size,
                },
                CodecProperty::ImageRemoteFx(p) => match p {
                    RemoteFxContainer::ClientContainer(container) => container.size(),
                    RemoteFxContainer::ServerContainer(size) => *size,
                },
                CodecProperty::Ignore => 0,
                CodecProperty::None => 0,
            }
    }
}

impl<'de> Decode<'de> for Codec {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let guid = Guid::decode(src)?;

        let id = src.read_u8();
        let codec_properties_len = usize::from(src.read_u16());

        let property = if codec_properties_len != 0 {
            ensure_size!(in: src, size: codec_properties_len);
            let property_buffer = src.read_slice(codec_properties_len);

            match guid {
                GUID_NSCODEC => CodecProperty::NsCodec(decode(property_buffer)?),
                GUID_REMOTEFX | GUID_IMAGE_REMOTEFX => {
                    let property = if property_buffer[0] == 0 {
                        RemoteFxContainer::ServerContainer(codec_properties_len)
                    } else {
                        RemoteFxContainer::ClientContainer(decode(property_buffer)?)
                    };

                    match guid {
                        GUID_REMOTEFX => CodecProperty::RemoteFx(property),
                        GUID_IMAGE_REMOTEFX => CodecProperty::ImageRemoteFx(property),
                        _ => unreachable!(),
                    }
                }
                GUID_IGNORE => CodecProperty::Ignore,
                _ => CodecProperty::None,
            }
        } else {
            match guid {
                GUID_NSCODEC | GUID_REMOTEFX | GUID_IMAGE_REMOTEFX => {
                    return Err(invalid_field_err!(
                        "codecPropertiesLen",
                        "invalid codec property length"
                    ));
                }
                GUID_IGNORE => CodecProperty::Ignore,
                _ => CodecProperty::None,
            }
        };

        Ok(Self { id, property })
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum RemoteFxContainer {
    ClientContainer(RfxClientCapsContainer),
    ServerContainer(usize),
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum CodecProperty {
    NsCodec(NsCodec),
    RemoteFx(RemoteFxContainer),
    ImageRemoteFx(RemoteFxContainer),
    Ignore,
    None,
}

/// The NsCodec structure advertises properties of the NSCodec Bitmap Codec.
///
/// # Fields
///
/// * `is_dynamic_fidelity_allowed` - indicates support for lossy bitmap compression by reducing color fidelity
/// * `is_subsampling_allowed` - indicates support for chroma subsampling
/// * `color_loss_level` - indicates the maximum supported Color Loss Level
///
/// If received Color Loss Level value is lesser than 1 or greater than 7, it assigns to 1 or 7 respectively. This was made for compatibility with FreeRDP server.
///
/// # MSDN
///
/// * [NSCodec Capability Set](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpnsc/0eac0ba8-7bdd-4300-ab8d-9bc784c0a669)
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct NsCodec {
    pub is_dynamic_fidelity_allowed: bool,
    pub is_subsampling_allowed: bool,
    pub color_loss_level: u8,
}

impl NsCodec {
    const NAME: &'static str = "NsCodec";

    const FIXED_PART_SIZE: usize = NSCODEC_LENGTH;
}

impl Encode for NsCodec {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_fixed_part_size!(in: dst);

        dst.write_u8(u8::from(self.is_dynamic_fidelity_allowed));
        dst.write_u8(u8::from(self.is_subsampling_allowed));
        dst.write_u8(self.color_loss_level);

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

impl<'de> Decode<'de> for NsCodec {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let is_dynamic_fidelity_allowed = src.read_u8() != 0;
        let is_subsampling_allowed = src.read_u8() != 0;

        let color_loss_level = src.read_u8().clamp(1, 7);

        Ok(Self {
            is_dynamic_fidelity_allowed,
            is_subsampling_allowed,
            color_loss_level,
        })
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RfxClientCapsContainer {
    pub capture_flags: CaptureFlags,
    pub caps_data: RfxCaps,
}

impl RfxClientCapsContainer {
    const NAME: &'static str = "RfxClientCapsContainer";

    const FIXED_PART_SIZE: usize = RFX_CLIENT_CAPS_CONTAINER_STATIC_DATA_LENGTH;
}

impl Encode for RfxClientCapsContainer {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());

        dst.write_u32(cast_length!("len", self.size())?);
        dst.write_u32(self.capture_flags.bits());
        dst.write_u32(cast_length!("capsLen", self.caps_data.size())?);
        self.caps_data.encode(dst)?;

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.caps_data.size()
    }
}

impl<'de> Decode<'de> for RfxClientCapsContainer {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        let _length = src.read_u32();
        let capture_flags = CaptureFlags::from_bits_truncate(src.read_u32());
        let _caps_length = src.read_u32();
        let caps_data = RfxCaps::decode(src)?;

        Ok(Self {
            capture_flags,
            caps_data,
        })
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RfxCaps(pub RfxCapset);

impl RfxCaps {
    const NAME: &'static str = "RfxCaps";

    const FIXED_PART_SIZE: usize = RFX_CAPS_STATIC_DATA_LENGTH;
}

impl Encode for RfxCaps {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());

        dst.write_u16(RFX_CAPS_BLOCK_TYPE);
        dst.write_u32(RFX_CAPS_BLOCK_LENGTH);
        dst.write_u16(RFX_CAPS_NUM_CAPSETS);
        self.0.encode(dst)?; // capsets data

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.0.size()
    }
}

impl<'de> Decode<'de> for RfxCaps {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let block_type = src.read_u16();
        if block_type != RFX_CAPS_BLOCK_TYPE {
            return Err(invalid_field_err!("blockType", "invalid rfx caps block type"));
        }

        let block_len = src.read_u32();
        if block_len != RFX_CAPS_BLOCK_LENGTH {
            return Err(invalid_field_err!("blockLen", "invalid rfx caps block length"));
        }

        let num_capsets = src.read_u16();
        if num_capsets != RFX_CAPS_NUM_CAPSETS {
            return Err(invalid_field_err!("numCapsets", "invalid rfx caps num capsets"));
        }

        let capsets_data = RfxCapset::decode(src)?;

        Ok(RfxCaps(capsets_data))
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RfxCapset(pub Vec<RfxICap>);

impl RfxCapset {
    const NAME: &'static str = "RfxCapset";

    const FIXED_PART_SIZE: usize = RFX_CAPSET_STATIC_DATA_LENGTH;
}

impl Encode for RfxCapset {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());

        dst.write_u16(RFX_CAPSET_BLOCK_TYPE);
        dst.write_u32(cast_length!(
            "len",
            RFX_CAPSET_STATIC_DATA_LENGTH + self.0.len() * RFX_ICAP_LENGTH
        )?);
        dst.write_u8(1); // codec id
        dst.write_u16(RFX_CAPSET_TYPE);
        dst.write_u16(cast_length!("len", self.0.len())?);
        dst.write_u16(cast_length!("len", RFX_ICAP_LENGTH)?);

        for rfx in self.0.iter() {
            rfx.encode(dst)?;
        }

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.0.len() * RFX_ICAP_LENGTH
    }
}

impl<'de> Decode<'de> for RfxCapset {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        let block_type = src.read_u16();
        if block_type != RFX_CAPSET_BLOCK_TYPE {
            return Err(invalid_field_err!("blockType", "invalid rfx capset block type"));
        }

        let _block_len = src.read_u32();

        let codec_id = src.read_u8();
        if codec_id != 1 {
            return Err(invalid_field_err!("codecId", "invalid rfx codec ID"));
        }

        let capset_type = src.read_u16();
        if capset_type != RFX_CAPSET_TYPE {
            return Err(invalid_field_err!("capsetType", "invalid rfx capset type"));
        }

        let num_icaps = src.read_u16();
        let _icaps_len = src.read_u16();

        let mut icaps_data = Vec::with_capacity(num_icaps as usize);
        for _ in 0..num_icaps {
            icaps_data.push(RfxICap::decode(src)?);
        }

        Ok(RfxCapset(icaps_data))
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RfxICap {
    pub flags: RfxICapFlags,
    pub entropy_bits: EntropyBits,
}

impl RfxICap {
    const NAME: &'static str = "RfxICap";

    const FIXED_PART_SIZE: usize = RFX_ICAP_LENGTH;
}

impl Encode for RfxICap {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_fixed_part_size!(in: dst);

        dst.write_u16(RFX_ICAP_VERSION);
        dst.write_u16(RFX_ICAP_TILE_SIZE);
        dst.write_u8(self.flags.bits());
        dst.write_u8(RFX_ICAP_COLOR_CONVERSION);
        dst.write_u8(RFX_ICAP_TRANSFORM_BITS);
        dst.write_u8(self.entropy_bits.to_u8().unwrap());

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

impl<'de> Decode<'de> for RfxICap {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let version = src.read_u16();
        if version != RFX_ICAP_VERSION {
            return Err(invalid_field_err!("version", "invalid rfx icap version"));
        }

        let tile_size = src.read_u16();
        if tile_size != RFX_ICAP_TILE_SIZE {
            return Err(invalid_field_err!("tileSize", "invalid rfx icap tile size"));
        }

        let flags = RfxICapFlags::from_bits_truncate(src.read_u8());

        let color_conversion = src.read_u8();
        if color_conversion != RFX_ICAP_COLOR_CONVERSION {
            return Err(invalid_field_err!("colorConv", "invalid rfx color conversion bits"));
        }

        let transform_bits = src.read_u8();
        if transform_bits != RFX_ICAP_TRANSFORM_BITS {
            return Err(invalid_field_err!("transformBits", "invalid rfx transform bits"));
        }

        let entropy_bits = EntropyBits::from_u8(src.read_u8())
            .ok_or_else(|| invalid_field_err!("entropyBits", "invalid rfx entropy bits"))?;

        Ok(RfxICap { flags, entropy_bits })
    }
}

#[derive(PartialEq, Eq, Debug, FromPrimitive, ToPrimitive, Copy, Clone)]
pub enum EntropyBits {
    Rlgr1 = 1,
    Rlgr3 = 4,
}

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct CaptureFlags: u32 {
        const CARDP_CAPS_CAPTURE_NON_CAC = 1;
    }
}

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct RfxICapFlags: u8 {
        const CODEC_MODE = 2;
    }
}