Skip to main content

ghast_protocol/
frame.rs

1//! Framing: length prefixes, zlib compression and the encryption hook.
2//!
3//! [`FrameDecoder`] turns received bytes into [`RawPacket`]s, [`FrameEncoder`] turns
4//! packets into bytes ready to send. Both are synchronous and transport-agnostic; the
5//! `tokio` feature wraps them in [`crate::io`].
6//!
7//! Wire format (vanilla): `VarInt length | data`, where without compression
8//! `data = VarInt id | body` and with compression
9//! `data = VarInt uncompressed_len | (id | body), zlib-compressed unless uncompressed_len == 0`.
10//! With encryption on, the whole stream is AES-128-CFB8 encrypted.
11//!
12//! Lazy decoding: the decoder applies a [`PacketFilter`]. Unwanted packets are dropped
13//! before anything is decoded; for compressed frames only the first few bytes (enough
14//! for the packet id) are inflated, so ignored packets cost almost nothing.
15
16use bytes::{Buf, BufMut, Bytes, BytesMut};
17#[cfg(not(feature = "libdeflate"))]
18use flate2::{Compress, Compression, FlushCompress, Status};
19use flate2::{Decompress, FlushDecompress};
20
21use crate::codec::{McWrite, Reader, read_varint_prefix, varint_len};
22use crate::crypto::{Cfb8Decryptor, Cfb8Encryptor};
23use crate::error::{DecodeError, EncodeError, FrameError};
24use crate::packet::{Packet, PacketFilter, PacketSet, RawPacket};
25
26/// Largest frame length expressible in the 3-byte length prefix vanilla accepts.
27pub const MAX_FRAME_LEN: usize = (1 << 21) - 1;
28
29/// Limits applied by the decoder.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct FrameLimits {
32    /// Maximum frame length (at most [`MAX_FRAME_LEN`]).
33    pub max_frame_len: usize,
34    /// Maximum declared uncompressed packet size. Vanilla 1.8 uses 2 MiB, later
35    /// versions 8 MiB; the default is 8 MiB.
36    pub max_uncompressed_len: usize,
37}
38
39impl Default for FrameLimits {
40    fn default() -> Self {
41        FrameLimits {
42            max_frame_len: MAX_FRAME_LEN,
43            max_uncompressed_len: 8 << 20,
44        }
45    }
46}
47
48/// Decoder counters (useful for profiling what a server sends).
49#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
50#[non_exhaustive]
51pub struct DecoderStats {
52    /// Frames split off the stream.
53    pub frames: u64,
54    /// Frames dropped by the filter.
55    pub skipped: u64,
56    /// Compressed frames dropped after inflating only the packet id.
57    pub skipped_compressed: u64,
58    /// Bytes produced by inflation.
59    pub inflated_bytes: u64,
60}
61
62/// Splits a byte stream into packets.
63pub struct FrameDecoder {
64    buf: BytesMut,
65    decryptor: Option<Cfb8Decryptor>,
66    compression: bool,
67    /// Streaming inflater: used to peek at packet ids (and for full inflation without
68    /// the `libdeflate` feature).
69    inflater: Decompress,
70    #[cfg(feature = "libdeflate")]
71    libdeflate: libdeflater::Decompressor,
72    filter: PacketFilter,
73    limits: FrameLimits,
74    stats: DecoderStats,
75}
76
77impl std::fmt::Debug for FrameDecoder {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        f.debug_struct("FrameDecoder")
80            .field("buffered", &self.buf.len())
81            .field("encrypted", &self.decryptor.is_some())
82            .field("compression", &self.compression)
83            .field("stats", &self.stats)
84            .finish_non_exhaustive()
85    }
86}
87
88impl Default for FrameDecoder {
89    fn default() -> Self {
90        Self::new()
91    }
92}
93
94impl FrameDecoder {
95    /// A decoder with no compression, no encryption, default limits and no filter.
96    pub fn new() -> Self {
97        FrameDecoder {
98            buf: BytesMut::with_capacity(16 * 1024),
99            decryptor: None,
100            compression: false,
101            inflater: Decompress::new(true),
102            #[cfg(feature = "libdeflate")]
103            libdeflate: libdeflater::Decompressor::new(),
104            filter: PacketFilter::all(),
105            limits: FrameLimits::default(),
106            stats: DecoderStats::default(),
107        }
108    }
109
110    /// Sets the limits.
111    pub fn set_limits(&mut self, limits: FrameLimits) {
112        self.limits = FrameLimits {
113            max_frame_len: limits.max_frame_len.min(MAX_FRAME_LEN),
114            ..limits
115        };
116    }
117
118    /// Turns compression on or off (from the server's Set Compression packet; a
119    /// negative threshold means off). Call it right after decoding that packet.
120    pub fn set_compression(&mut self, threshold: i32) {
121        self.compression = threshold >= 0;
122    }
123
124    /// Whether compression is on.
125    pub fn compression(&self) -> bool {
126        self.compression
127    }
128
129    /// Turns on decryption. Bytes already buffered but not yet split into frames are
130    /// decrypted too, since the server encrypts everything after its encryption request
131    /// was answered.
132    pub fn enable_encryption(&mut self, secret: &[u8; 16]) {
133        let mut d = Cfb8Decryptor::new(secret);
134        d.decrypt(&mut self.buf);
135        self.decryptor = Some(d);
136    }
137
138    /// Whether decryption is on.
139    pub fn is_encrypted(&self) -> bool {
140        self.decryptor.is_some()
141    }
142
143    /// Sets the per-id interest filter.
144    pub fn set_filter(&mut self, filter: PacketFilter) {
145        self.filter = filter;
146    }
147
148    /// The current filter.
149    pub fn filter(&self) -> &PacketFilter {
150        &self.filter
151    }
152
153    /// Counters.
154    pub fn stats(&self) -> DecoderStats {
155        self.stats
156    }
157
158    /// Number of buffered, not yet consumed bytes.
159    pub fn buffered(&self) -> usize {
160        self.buf.len()
161    }
162
163    /// Appends received bytes (decrypting them if encryption is on).
164    pub fn push(&mut self, data: &[u8]) {
165        let start = self.buf.len();
166        self.buf.extend_from_slice(data);
167        self.decrypt_from(start);
168    }
169
170    /// The receive buffer, for reading directly into it. After appending `n` bytes call
171    /// [`FrameDecoder::commit`] with the length before the read.
172    pub fn buffer_mut(&mut self) -> &mut BytesMut {
173        &mut self.buf
174    }
175
176    /// Decrypts bytes appended to [`FrameDecoder::buffer_mut`] since `old_len`.
177    pub fn commit(&mut self, old_len: usize) {
178        self.decrypt_from(old_len);
179    }
180
181    fn decrypt_from(&mut self, start: usize) {
182        if let Some(d) = &mut self.decryptor
183            && start < self.buf.len()
184        {
185            d.decrypt(&mut self.buf[start..]);
186        }
187    }
188
189    /// How many more bytes are needed to complete the next frame, if known.
190    pub fn needed(&self) -> Option<usize> {
191        match read_varint_prefix(&self.buf[..self.buf.len().min(3)]) {
192            Ok(Some((len, n))) if len >= 0 => (n + len as usize).checked_sub(self.buf.len()),
193            _ => None,
194        }
195    }
196
197    /// Splits off the next packet that passes the filter.
198    ///
199    /// Returns `Ok(None)` when more bytes are needed. Errors are fatal for the
200    /// connection (the stream position is lost).
201    pub fn next_packet(&mut self) -> Result<Option<RawPacket>, FrameError> {
202        loop {
203            let Some(frame) = self.next_frame()? else {
204                return Ok(None);
205            };
206            self.stats.frames += 1;
207            if let Some(p) = self.unpack(frame)? {
208                return Ok(Some(p));
209            }
210            self.stats.skipped += 1;
211        }
212    }
213
214    /// Splits off the next raw frame (length prefix removed).
215    fn next_frame(&mut self) -> Result<Option<Bytes>, FrameError> {
216        let head = &self.buf[..self.buf.len().min(3)];
217        let (len, n) = match read_varint_prefix(head) {
218            Ok(Some(v)) => v,
219            Ok(None) if head.len() < 3 => return Ok(None),
220            _ => return Err(FrameError::BadLength),
221        };
222        if len <= 0 {
223            return Err(FrameError::BadLength);
224        }
225        let len = len as usize;
226        if len > self.limits.max_frame_len {
227            return Err(FrameError::FrameTooLarge {
228                len,
229                max: self.limits.max_frame_len,
230            });
231        }
232        if self.buf.len() < n + len {
233            // Make room for the whole frame up front (one allocation).
234            self.buf.reserve(n + len - self.buf.len());
235            return Ok(None);
236        }
237        self.buf.advance(n);
238        Ok(Some(self.buf.split_to(len).freeze()))
239    }
240
241    /// Turns a frame into a packet, or `None` if the filter drops it.
242    fn unpack(&mut self, frame: Bytes) -> Result<Option<RawPacket>, FrameError> {
243        if !self.compression {
244            return self.split_id(frame);
245        }
246        let mut r = Reader::new(&frame);
247        let data_len = r.read_varint()?;
248        if data_len < 0 {
249            return Err(FrameError::Decode(DecodeError::NegativeLength(i64::from(
250                data_len,
251            ))));
252        }
253        if data_len == 0 {
254            let rest = frame.slice(r.position()..);
255            return self.split_id(rest);
256        }
257        let data_len = data_len as usize;
258        if data_len > self.limits.max_uncompressed_len {
259            return Err(FrameError::DecompressedTooLarge {
260                len: data_len,
261                max: self.limits.max_uncompressed_len,
262            });
263        }
264        let input = &frame[r.position()..];
265        if !self.filter.is_all() {
266            // Inflate just the packet id (at most 5 bytes) and decide.
267            self.inflater.reset(true);
268            let mut head = [0u8; 5];
269            let want = data_len.min(5);
270            self.inflater
271                .decompress(input, &mut head[..want], FlushDecompress::None)
272                .map_err(|_| FrameError::BadCompression("corrupt zlib stream"))?;
273            let got = self.inflater.total_out() as usize;
274            let id = match read_varint_prefix(&head[..got])? {
275                Some((id, _)) => id,
276                None if got < want => return Err(FrameError::BadCompression("stream ended early")),
277                None => return Err(FrameError::Decode(DecodeError::VarIntTooLong)),
278            };
279            self.stats.inflated_bytes += got as u64;
280            if !self.filter.contains(id) {
281                self.stats.skipped_compressed += 1;
282                return Ok(None);
283            }
284        }
285        let out = self.inflate(input, data_len)?;
286        self.stats.inflated_bytes += data_len as u64;
287        let data = Bytes::from(out);
288        let mut r = Reader::new(&data);
289        let id = r.read_varint()?;
290        let body = data.slice(r.position()..);
291        Ok(Some(RawPacket::new(id, body)))
292    }
293
294    /// Inflates a whole zlib stream that must produce exactly `data_len` bytes.
295    #[cfg(feature = "libdeflate")]
296    fn inflate(&mut self, input: &[u8], data_len: usize) -> Result<Vec<u8>, FrameError> {
297        let mut out = vec![0u8; data_len];
298        match self.libdeflate.zlib_decompress(input, &mut out) {
299            Ok(n) if n == data_len => Ok(out),
300            Ok(_) | Err(libdeflater::DecompressionError::InsufficientSpace) => Err(
301                FrameError::BadCompression("size differs from declared length"),
302            ),
303            Err(_) => Err(FrameError::BadCompression("corrupt zlib stream")),
304        }
305    }
306
307    /// Inflates a whole zlib stream that must produce exactly `data_len` bytes.
308    #[cfg(not(feature = "libdeflate"))]
309    fn inflate(&mut self, input: &[u8], data_len: usize) -> Result<Vec<u8>, FrameError> {
310        self.inflater.reset(true);
311        let mut out: Vec<u8> = Vec::with_capacity(data_len);
312        let status = self
313            .inflater
314            .decompress_vec(input, &mut out, FlushDecompress::Finish)
315            .map_err(|_| FrameError::BadCompression("corrupt zlib stream"))?;
316        if out.len() != data_len {
317            return Err(FrameError::BadCompression(
318                "size differs from declared length",
319            ));
320        }
321        if status != Status::StreamEnd {
322            // The output is full; the stream must end without producing more.
323            let consumed = self.inflater.total_in() as usize;
324            let mut extra = [0u8; 1];
325            let st = self
326                .inflater
327                .decompress(
328                    &input[consumed.min(input.len())..],
329                    &mut extra,
330                    FlushDecompress::Finish,
331                )
332                .map_err(|_| FrameError::BadCompression("corrupt zlib stream"))?;
333            if self.inflater.total_out() as usize != data_len || st != Status::StreamEnd {
334                return Err(FrameError::BadCompression(
335                    "size differs from declared length",
336                ));
337            }
338        }
339        Ok(out)
340    }
341
342    fn split_id(&self, data: Bytes) -> Result<Option<RawPacket>, FrameError> {
343        let mut r = Reader::new(&data);
344        let id = r.read_varint()?;
345        if !self.filter.contains(id) {
346            return Ok(None);
347        }
348        let body = data.slice(r.position()..);
349        Ok(Some(RawPacket::new(id, body)))
350    }
351}
352
353/// Turns packets into wire bytes. Packets are appended to an internal output buffer;
354/// take it with [`FrameEncoder::take`] (or let [`crate::io::FramedWriter`] flush it), so
355/// several packets of one tick go out in a single write.
356pub struct FrameEncoder {
357    out: BytesMut,
358    scratch: BytesMut,
359    compressed: Vec<u8>,
360    threshold: Option<usize>,
361    #[cfg(not(feature = "libdeflate"))]
362    deflater: Compress,
363    #[cfg(feature = "libdeflate")]
364    deflater: libdeflater::Compressor,
365    encryptor: Option<Cfb8Encryptor>,
366}
367
368impl std::fmt::Debug for FrameEncoder {
369    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
370        f.debug_struct("FrameEncoder")
371            .field("pending", &self.out.len())
372            .field("threshold", &self.threshold)
373            .field("encrypted", &self.encryptor.is_some())
374            .finish_non_exhaustive()
375    }
376}
377
378impl Default for FrameEncoder {
379    fn default() -> Self {
380        Self::new()
381    }
382}
383
384impl FrameEncoder {
385    /// An encoder with no compression and no encryption.
386    pub fn new() -> Self {
387        FrameEncoder {
388            out: BytesMut::with_capacity(4096),
389            scratch: BytesMut::with_capacity(1024),
390            compressed: Vec::new(),
391            threshold: None,
392            // Level 6 like Java's `Deflater` default (also gives the same `78 9c` header).
393            #[cfg(not(feature = "libdeflate"))]
394            deflater: Compress::new(Compression::default(), true),
395            #[cfg(feature = "libdeflate")]
396            deflater: libdeflater::Compressor::new(libdeflater::CompressionLvl::default()),
397            encryptor: None,
398        }
399    }
400
401    /// Sets the compression threshold (negative = off), as received from the server.
402    pub fn set_compression(&mut self, threshold: i32) {
403        self.threshold = usize::try_from(threshold).ok();
404    }
405
406    /// Turns on encryption for everything written from now on.
407    pub fn enable_encryption(&mut self, secret: &[u8; 16]) {
408        self.encryptor = Some(Cfb8Encryptor::new(secret));
409    }
410
411    /// Encodes and frames a typed packet.
412    pub fn write_packet<P: Packet>(&mut self, p: &P) -> Result<(), EncodeError> {
413        self.scratch.clear();
414        self.scratch.put_varint(P::ID);
415        p.encode(&mut self.scratch)?;
416        self.frame_scratch()
417    }
418
419    /// Encodes and frames a packet-set enum value.
420    pub fn write_set<S: PacketSet>(&mut self, p: &S) -> Result<(), EncodeError> {
421        self.scratch.clear();
422        self.scratch.put_varint(p.id());
423        p.encode_body(&mut self.scratch)?;
424        self.frame_scratch()
425    }
426
427    /// Frames a raw packet.
428    pub fn write_raw(&mut self, p: &RawPacket) -> Result<(), EncodeError> {
429        self.scratch.clear();
430        self.scratch.put_varint(p.id);
431        self.scratch.put_slice(&p.body);
432        self.frame_scratch()
433    }
434
435    fn frame_scratch(&mut self) -> Result<(), EncodeError> {
436        let start = self.out.len();
437        let len = self.scratch.len();
438        let too_big = |len| EncodeError::LengthOverflow {
439            len,
440            max: MAX_FRAME_LEN,
441        };
442        match self.threshold {
443            None => {
444                if len > MAX_FRAME_LEN {
445                    return Err(too_big(len));
446                }
447                self.out.put_varint(len as i32);
448                self.out.put_slice(&self.scratch);
449            }
450            Some(t) if len < t => {
451                if len + 1 > MAX_FRAME_LEN {
452                    return Err(too_big(len + 1));
453                }
454                self.out.put_varint(len as i32 + 1);
455                self.out.put_u8(0);
456                self.out.put_slice(&self.scratch);
457            }
458            Some(_) => {
459                let len_i = i32::try_from(len).map_err(|_| too_big(len))?;
460                self.deflate()?;
461                let frame_len = varint_len(len_i) + self.compressed.len();
462                if frame_len > MAX_FRAME_LEN {
463                    return Err(too_big(frame_len));
464                }
465                self.out.put_varint(frame_len as i32);
466                self.out.put_varint(len_i);
467                self.out.put_slice(&self.compressed);
468            }
469        }
470        if let Some(e) = &mut self.encryptor {
471            e.encrypt(&mut self.out[start..]);
472        }
473        Ok(())
474    }
475
476    /// Compresses `scratch` into `compressed`.
477    #[cfg(feature = "libdeflate")]
478    fn deflate(&mut self) -> Result<(), EncodeError> {
479        let bound = self.deflater.zlib_compress_bound(self.scratch.len());
480        self.compressed.clear();
481        self.compressed.resize(bound, 0);
482        let n = self
483            .deflater
484            .zlib_compress(&self.scratch, &mut self.compressed)
485            .map_err(|_| EncodeError::Invalid("deflate failed"))?;
486        self.compressed.truncate(n);
487        Ok(())
488    }
489
490    /// Compresses `scratch` into `compressed`.
491    #[cfg(not(feature = "libdeflate"))]
492    fn deflate(&mut self) -> Result<(), EncodeError> {
493        self.deflater.reset();
494        self.compressed.clear();
495        self.compressed
496            .reserve(self.scratch.len() + self.scratch.len() / 1000 + 64);
497        loop {
498            let consumed = self.deflater.total_in() as usize;
499            let st = self
500                .deflater
501                .compress_vec(
502                    &self.scratch[consumed..],
503                    &mut self.compressed,
504                    FlushCompress::Finish,
505                )
506                .map_err(|_| EncodeError::Invalid("deflate failed"))?;
507            if st == Status::StreamEnd {
508                return Ok(());
509            }
510            self.compressed.reserve(self.compressed.capacity().max(64));
511        }
512    }
513
514    /// Bytes waiting to be sent.
515    pub fn pending(&self) -> &[u8] {
516        &self.out
517    }
518
519    /// Takes the bytes waiting to be sent.
520    pub fn take(&mut self) -> BytesMut {
521        self.out.split()
522    }
523
524    /// The output buffer (for transports that consume it in place).
525    pub fn out_mut(&mut self) -> &mut BytesMut {
526        &mut self.out
527    }
528}
529
530#[cfg(test)]
531mod tests {
532    use super::*;
533    use flate2::{Compress, Compression, FlushCompress};
534
535    fn roundtrip(threshold: Option<i32>, secret: Option<[u8; 16]>, filter: PacketFilter) {
536        let mut enc = FrameEncoder::new();
537        let mut dec = FrameDecoder::new();
538        if let Some(t) = threshold {
539            enc.set_compression(t);
540            dec.set_compression(t);
541        }
542        if let Some(s) = secret {
543            enc.enable_encryption(&s);
544            dec.enable_encryption(&s);
545        }
546        dec.set_filter(filter);
547        let packets: Vec<RawPacket> = (0..50)
548            .map(|i| {
549                let body: Vec<u8> = (0..(i * 37) % 700).map(|j| (j * 7 + i) as u8).collect();
550                RawPacket::new(i % 9, Bytes::from(body))
551            })
552            .collect();
553        for p in &packets {
554            enc.write_raw(p).unwrap();
555        }
556        let wire = enc.take();
557        // Feed in odd-sized pieces.
558        let mut got = Vec::new();
559        for chunk in wire.chunks(13) {
560            dec.push(chunk);
561            while let Some(p) = dec.next_packet().unwrap() {
562                got.push(p);
563            }
564        }
565        let want: Vec<RawPacket> = packets
566            .into_iter()
567            .filter(|p| filter.contains(p.id))
568            .collect();
569        assert_eq!(got, want);
570        assert_eq!(dec.buffered(), 0);
571    }
572
573    #[test]
574    fn roundtrips() {
575        let key = [3u8; 16];
576        let f = PacketFilter::none().with(1).with(4);
577        for (t, s, filt) in [
578            (None, None, PacketFilter::all()),
579            (Some(256), None, PacketFilter::all()),
580            (Some(0), None, PacketFilter::all()),
581            (Some(64), Some(key), PacketFilter::all()),
582            (None, Some(key), f),
583            (Some(256), None, f),
584            (Some(1), Some(key), f),
585        ] {
586            roundtrip(t, s, filt);
587        }
588    }
589
590    #[test]
591    fn known_answer_frames() {
592        // Uncompressed: length 3, id 0x00, body [0x01, 0x02].
593        let mut enc = FrameEncoder::new();
594        enc.write_raw(&RawPacket::new(0, Bytes::from_static(&[1, 2])))
595            .unwrap();
596        assert_eq!(&enc.take()[..], &[0x03, 0x00, 0x01, 0x02]);
597        // Below the threshold: data length 0 marker.
598        enc.set_compression(256);
599        enc.write_raw(&RawPacket::new(0x21, Bytes::from_static(&[9])))
600            .unwrap();
601        assert_eq!(&enc.take()[..], &[0x03, 0x00, 0x21, 0x09]);
602        // A zlib frame produced by Java's Deflater (level 6) for id 0x26 + 300 zero bytes
603        // decodes back to that packet.
604        let mut body = vec![0x26];
605        body.extend_from_slice(&[0u8; 300]);
606        let mut z = Compress::new(Compression::default(), true);
607        let mut c = Vec::with_capacity(400);
608        z.compress_vec(&body, &mut c, FlushCompress::Finish)
609            .unwrap();
610        assert_eq!(&c[..2], &[0x78, 0x9c], "zlib header for the default level");
611        let mut frame = BytesMut::new();
612        frame.put_varint((varint_len(301) + c.len()) as i32);
613        frame.put_varint(301);
614        frame.put_slice(&c);
615        let mut dec = FrameDecoder::new();
616        dec.set_compression(256);
617        dec.push(&frame);
618        let p = dec.next_packet().unwrap().unwrap();
619        assert_eq!(p.id, 0x26);
620        assert_eq!(&p.body[..], &[0u8; 300][..]);
621    }
622
623    #[test]
624    fn rejects_malformed() {
625        let mut dec = FrameDecoder::new();
626        dec.push(&[0xff, 0xff, 0xff, 0x01]);
627        assert!(matches!(dec.next_packet(), Err(FrameError::BadLength)));
628        let mut dec = FrameDecoder::new();
629        dec.push(&[0x00]);
630        assert!(matches!(dec.next_packet(), Err(FrameError::BadLength)));
631        // Declared uncompressed size too large.
632        let mut dec = FrameDecoder::new();
633        dec.set_compression(0);
634        dec.push(&[0x05, 0x80, 0x80, 0x80, 0x10, 0x00]);
635        assert!(matches!(
636            dec.next_packet(),
637            Err(FrameError::DecompressedTooLarge { .. })
638        ));
639        // Garbage instead of zlib.
640        let mut dec = FrameDecoder::new();
641        dec.set_compression(0);
642        dec.push(&[0x04, 0x05, 0x01, 0x02, 0x03]);
643        assert!(matches!(
644            dec.next_packet(),
645            Err(FrameError::BadCompression(_))
646        ));
647        // Declared size larger than the actual inflated size.
648        let mut z = Compress::new(Compression::default(), true);
649        let mut c = Vec::with_capacity(64);
650        z.compress_vec(&[1, 2, 3], &mut c, FlushCompress::Finish)
651            .unwrap();
652        let mut frame = BytesMut::new();
653        frame.put_varint(1 + c.len() as i32);
654        frame.put_varint(4);
655        frame.put_slice(&c);
656        let mut dec = FrameDecoder::new();
657        dec.set_compression(0);
658        dec.push(&frame);
659        assert!(matches!(
660            dec.next_packet(),
661            Err(FrameError::BadCompression(_))
662        ));
663        // ... and smaller.
664        let mut frame = BytesMut::new();
665        frame.put_varint(1 + c.len() as i32);
666        frame.put_varint(2);
667        frame.put_slice(&c);
668        let mut dec = FrameDecoder::new();
669        dec.set_compression(0);
670        dec.push(&frame);
671        assert!(matches!(
672            dec.next_packet(),
673            Err(FrameError::BadCompression(_))
674        ));
675    }
676
677    #[test]
678    fn partial_inflation_skips_unwanted() {
679        let mut enc = FrameEncoder::new();
680        enc.set_compression(16);
681        let big = RawPacket::new(0x38, Bytes::from(vec![7u8; 100_000]));
682        enc.write_raw(&big).unwrap();
683        enc.write_raw(&RawPacket::new(0x00, Bytes::from(vec![1u8; 64])))
684            .unwrap();
685        let mut dec = FrameDecoder::new();
686        dec.set_compression(16);
687        dec.set_filter(PacketFilter::all().without(0x38));
688        dec.push(&enc.take());
689        let p = dec.next_packet().unwrap().unwrap();
690        assert_eq!(p.id, 0);
691        let s = dec.stats();
692        assert_eq!((s.frames, s.skipped, s.skipped_compressed), (2, 1, 1));
693        assert!(
694            s.inflated_bytes < 100,
695            "inflated {} bytes",
696            s.inflated_bytes
697        );
698    }
699
700    #[test]
701    fn encryption_enabled_mid_buffer() {
702        // Bytes that arrive before `enable_encryption` but belong after the switch are
703        // decrypted when it is enabled.
704        let key = [9u8; 16];
705        let mut plain = FrameEncoder::new();
706        plain
707            .write_raw(&RawPacket::new(1, Bytes::from_static(b"plain")))
708            .unwrap();
709        let mut crypt = FrameEncoder::new();
710        crypt.enable_encryption(&key);
711        crypt
712            .write_raw(&RawPacket::new(2, Bytes::from_static(b"secret")))
713            .unwrap();
714        let mut dec = FrameDecoder::new();
715        dec.push(&plain.take());
716        dec.push(&crypt.take());
717        assert_eq!(dec.next_packet().unwrap().unwrap().id, 1);
718        dec.enable_encryption(&key);
719        let p = dec.next_packet().unwrap().unwrap();
720        assert_eq!((p.id, &p.body[..]), (2, &b"secret"[..]));
721    }
722}