ghast-protocol 0.1.0

Minecraft Java Edition network protocol for the ghast client library: generated per-version packets, framing, compression, encryption, NBT
Documentation
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
//! Named Binary Tag (NBT) as used on the network by 1.8 – 1.20.1.
//!
//! The protocol carries NBT as a *named root tag*: a tag-type byte, a `u16`-prefixed
//! name and the payload. A single `0` byte (TAG_End) means "no compound" — vanilla's
//! packet buffer reads both its plain and its optional NBT that way.
//!
//! Packets keep NBT as [`RawNbt`]: the exact bytes of the root tag, found by *skipping*
//! over the structure without allocating. Turning it into a tree ([`RawNbt::parse`]) is
//! opt-in, so a client that only needs item ids and counts from `window_items` never pays
//! for building NBT trees (on busy servers that NBT is most of the inbound bytes).
//!
//! Strings are Java "Modified UTF-8" (NUL as `C0 80`, supplementary characters as
//! surrogate pairs). Raw bytes are always preserved exactly; the parsed tree decodes
//! strings lossily (unpaired surrogates become U+FFFD).

use bytes::{BufMut, Bytes, BytesMut};

use crate::codec::{Decode, DecodeResult, Encode, EncodeResult, Reader};
use crate::error::{DecodeError, EncodeError};

/// Maximum nesting depth of compounds and lists (same as vanilla).
pub const MAX_DEPTH: usize = 512;

/// NBT tag type ids.
#[allow(missing_docs)]
pub mod tag {
    pub const END: u8 = 0;
    pub const BYTE: u8 = 1;
    pub const SHORT: u8 = 2;
    pub const INT: u8 = 3;
    pub const LONG: u8 = 4;
    pub const FLOAT: u8 = 5;
    pub const DOUBLE: u8 = 6;
    pub const BYTE_ARRAY: u8 = 7;
    pub const STRING: u8 = 8;
    pub const LIST: u8 = 9;
    pub const COMPOUND: u8 = 10;
    pub const INT_ARRAY: u8 = 11;
    pub const LONG_ARRAY: u8 = 12;
}

/// The undecoded bytes of one network NBT root tag (type byte, name and payload), or a
/// single `0` byte for "no compound".
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RawNbt(Bytes);

impl RawNbt {
    /// The "no compound" value (a single TAG_End byte).
    pub const END: RawNbt = RawNbt(Bytes::from_static(&[0]));

    /// The raw bytes, exactly as they appear on the wire.
    #[inline]
    pub fn as_bytes(&self) -> &Bytes {
        &self.0
    }

    /// Whether this is the single-byte "no compound" value.
    #[inline]
    pub fn is_end(&self) -> bool {
        self.0.first() == Some(&tag::END)
    }

    /// Validates `bytes` as exactly one root tag and wraps them.
    pub fn from_bytes(bytes: Bytes) -> DecodeResult<Self> {
        let mut r = Reader::new(&bytes);
        let v = Self::read(&mut r)?;
        r.finish()?;
        Ok(v)
    }

    /// Reads a root tag (or a single TAG_End byte) by skipping over its structure.
    pub fn read(r: &mut Reader<'_>) -> DecodeResult<Self> {
        let start = r.position();
        let ty = r.read_u8()?;
        if ty != tag::END {
            let name_len = r.read_u16()? as usize;
            r.skip(name_len)?;
            skip_payload(r, ty, 0)?;
        }
        Ok(RawNbt(r.bytes_since(start)))
    }

    /// Reads protodef `optionalNbt`: a `0` byte is `None`, anything else a root tag.
    pub fn read_optional(r: &mut Reader<'_>) -> DecodeResult<Option<Self>> {
        if r.peek_u8()? == tag::END {
            r.skip(1)?;
            Ok(None)
        } else {
            Self::read(r).map(Some)
        }
    }

    /// Writes the raw bytes.
    #[inline]
    pub fn write(&self, w: &mut BytesMut) {
        w.put_slice(&self.0);
    }

    /// Writes protodef `optionalNbt`.
    #[inline]
    pub fn write_optional(v: &Option<Self>, w: &mut BytesMut) {
        match v {
            Some(n) => n.write(w),
            None => w.put_u8(tag::END),
        }
    }

    /// Parses the tree. Returns `None` for the "no compound" value.
    pub fn parse(&self) -> DecodeResult<Option<NamedTag>> {
        let mut r = Reader::new(&self.0);
        let ty = r.read_u8()?;
        if ty == tag::END {
            return Ok(None);
        }
        let name = read_mutf8(&mut r)?;
        let tag = read_payload(&mut r, ty, 0)?;
        r.finish()?;
        Ok(Some(NamedTag { name, tag }))
    }

    /// Serialises a tree into raw network bytes.
    pub fn from_named(t: &NamedTag) -> Result<Self, EncodeError> {
        let mut w = BytesMut::new();
        t.encode(&mut w)?;
        Ok(RawNbt(w.freeze()))
    }
}

impl Decode for RawNbt {
    #[inline]
    fn decode(r: &mut Reader<'_>) -> DecodeResult<Self> {
        Self::read(r)
    }
}

impl Encode for RawNbt {
    #[inline]
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        self.write(w);
        Ok(())
    }
}

/// Skips the payload of a tag of type `ty`.
fn skip_payload(r: &mut Reader<'_>, ty: u8, depth: usize) -> DecodeResult<()> {
    match ty {
        tag::BYTE => r.skip(1),
        tag::SHORT => r.skip(2),
        tag::INT | tag::FLOAT => r.skip(4),
        tag::LONG | tag::DOUBLE => r.skip(8),
        tag::BYTE_ARRAY => {
            let n = read_array_len(r, 1)?;
            r.skip(n)
        }
        tag::INT_ARRAY => {
            let n = read_array_len(r, 4)?;
            r.skip(n * 4)
        }
        tag::LONG_ARRAY => {
            let n = read_array_len(r, 8)?;
            r.skip(n * 8)
        }
        tag::STRING => {
            let n = r.read_u16()? as usize;
            r.skip(n)
        }
        tag::LIST => {
            if depth >= MAX_DEPTH {
                return Err(DecodeError::NbtTooDeep(MAX_DEPTH));
            }
            let elem = r.read_u8()?;
            let n = read_list_len(r, elem)?;
            match elem {
                // Fixed-size elements: skip in one step.
                tag::BYTE => r.skip(n),
                tag::SHORT => r.skip(n * 2),
                tag::INT | tag::FLOAT => r.skip(n * 4),
                tag::LONG | tag::DOUBLE => r.skip(n * 8),
                _ => {
                    for _ in 0..n {
                        skip_payload(r, elem, depth + 1)?;
                    }
                    Ok(())
                }
            }
        }
        tag::COMPOUND => {
            if depth >= MAX_DEPTH {
                return Err(DecodeError::NbtTooDeep(MAX_DEPTH));
            }
            loop {
                let t = r.read_u8()?;
                if t == tag::END {
                    return Ok(());
                }
                let name_len = r.read_u16()? as usize;
                r.skip(name_len)?;
                skip_payload(r, t, depth + 1)?;
            }
        }
        other => Err(DecodeError::InvalidNbtTag(other)),
    }
}

/// Reads an `i32` array length and checks it against the remaining input.
fn read_array_len(r: &mut Reader<'_>, elem_size: usize) -> DecodeResult<usize> {
    let n = r.read_i32()?;
    r.check_count(i64::from(n), elem_size)
}

/// Smallest encoded size of one list element of type `elem`.
fn min_size(elem: u8) -> usize {
    match elem {
        tag::BYTE | tag::COMPOUND => 1,
        tag::SHORT | tag::STRING => 2,
        tag::INT | tag::FLOAT | tag::BYTE_ARRAY | tag::INT_ARRAY | tag::LONG_ARRAY => 4,
        tag::LONG | tag::DOUBLE => 8,
        tag::LIST => 5,
        _ => 1,
    }
}

fn read_list_len(r: &mut Reader<'_>, elem: u8) -> DecodeResult<usize> {
    let n = r.read_i32()?;
    if elem == tag::END {
        return if n <= 0 {
            Ok(0)
        } else {
            Err(DecodeError::NbtEndList)
        };
    }
    if elem > tag::LONG_ARRAY {
        return Err(DecodeError::InvalidNbtTag(elem));
    }
    r.check_count(i64::from(n), min_size(elem))
}

/// A named root tag.
#[derive(Debug, Clone, PartialEq)]
pub struct NamedTag {
    /// Root name (usually empty on the network).
    pub name: String,
    /// The root tag (usually a compound).
    pub tag: Tag,
}

/// An NBT tag.
#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub enum Tag {
    Byte(i8),
    Short(i16),
    Int(i32),
    Long(i64),
    Float(f32),
    Double(f64),
    ByteArray(Bytes),
    String(String),
    List(List),
    Compound(Compound),
    IntArray(Vec<i32>),
    LongArray(Vec<i64>),
}

/// An NBT list: element type id and elements (all of that type).
#[derive(Debug, Clone, PartialEq, Default)]
pub struct List {
    /// Element tag type ([`tag::END`] for an empty untyped list).
    pub elem_type: u8,
    /// The elements; each must be of `elem_type`.
    pub items: Vec<Tag>,
}

/// An NBT compound: named tags in wire order.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Compound(pub Vec<(String, Tag)>);

impl Compound {
    /// Looks up an entry by name (linear scan; compounds are small).
    pub fn get(&self, name: &str) -> Option<&Tag> {
        self.0.iter().find(|(k, _)| k == name).map(|(_, v)| v)
    }
}

impl Tag {
    /// The tag type id.
    pub fn type_id(&self) -> u8 {
        match self {
            Tag::Byte(_) => tag::BYTE,
            Tag::Short(_) => tag::SHORT,
            Tag::Int(_) => tag::INT,
            Tag::Long(_) => tag::LONG,
            Tag::Float(_) => tag::FLOAT,
            Tag::Double(_) => tag::DOUBLE,
            Tag::ByteArray(_) => tag::BYTE_ARRAY,
            Tag::String(_) => tag::STRING,
            Tag::List(_) => tag::LIST,
            Tag::Compound(_) => tag::COMPOUND,
            Tag::IntArray(_) => tag::INT_ARRAY,
            Tag::LongArray(_) => tag::LONG_ARRAY,
        }
    }

    /// The compound, if this is one.
    pub fn as_compound(&self) -> Option<&Compound> {
        match self {
            Tag::Compound(c) => Some(c),
            _ => None,
        }
    }
}

fn read_payload(r: &mut Reader<'_>, ty: u8, depth: usize) -> DecodeResult<Tag> {
    Ok(match ty {
        tag::BYTE => Tag::Byte(r.read_i8()?),
        tag::SHORT => Tag::Short(r.read_i16()?),
        tag::INT => Tag::Int(r.read_i32()?),
        tag::LONG => Tag::Long(r.read_i64()?),
        tag::FLOAT => Tag::Float(r.read_f32()?),
        tag::DOUBLE => Tag::Double(r.read_f64()?),
        tag::BYTE_ARRAY => {
            let n = read_array_len(r, 1)?;
            Tag::ByteArray(r.read_bytes(n)?)
        }
        tag::INT_ARRAY => {
            let n = read_array_len(r, 4)?;
            let s = r.take(n * 4)?;
            Tag::IntArray(
                s.as_chunks::<4>()
                    .0
                    .iter()
                    .map(|c| i32::from_be_bytes(*c))
                    .collect(),
            )
        }
        tag::LONG_ARRAY => {
            let n = read_array_len(r, 8)?;
            let s = r.take(n * 8)?;
            Tag::LongArray(
                s.as_chunks::<8>()
                    .0
                    .iter()
                    .map(|c| i64::from_be_bytes(*c))
                    .collect(),
            )
        }
        tag::STRING => Tag::String(read_mutf8(r)?),
        tag::LIST => {
            if depth >= MAX_DEPTH {
                return Err(DecodeError::NbtTooDeep(MAX_DEPTH));
            }
            let elem = r.read_u8()?;
            let n = read_list_len(r, elem)?;
            let mut items = Vec::with_capacity(n);
            for _ in 0..n {
                items.push(read_payload(r, elem, depth + 1)?);
            }
            Tag::List(List {
                elem_type: elem,
                items,
            })
        }
        tag::COMPOUND => {
            if depth >= MAX_DEPTH {
                return Err(DecodeError::NbtTooDeep(MAX_DEPTH));
            }
            let mut entries = Vec::new();
            loop {
                let t = r.read_u8()?;
                if t == tag::END {
                    break;
                }
                let name = read_mutf8(r)?;
                entries.push((name, read_payload(r, t, depth + 1)?));
            }
            Tag::Compound(Compound(entries))
        }
        other => return Err(DecodeError::InvalidNbtTag(other)),
    })
}

impl Encode for NamedTag {
    fn encode(&self, w: &mut BytesMut) -> EncodeResult {
        w.put_u8(self.tag.type_id());
        write_mutf8(w, &self.name)?;
        write_payload(w, &self.tag, 0)
    }
}

fn i32_len(n: usize) -> Result<i32, EncodeError> {
    i32::try_from(n).map_err(|_| EncodeError::LengthOverflow {
        len: n,
        max: i32::MAX as usize,
    })
}

fn write_payload(w: &mut BytesMut, t: &Tag, depth: usize) -> EncodeResult {
    if depth > MAX_DEPTH {
        return Err(EncodeError::Invalid("NBT nesting too deep"));
    }
    match t {
        Tag::Byte(v) => w.put_i8(*v),
        Tag::Short(v) => w.put_i16(*v),
        Tag::Int(v) => w.put_i32(*v),
        Tag::Long(v) => w.put_i64(*v),
        Tag::Float(v) => w.put_f32(*v),
        Tag::Double(v) => w.put_f64(*v),
        Tag::ByteArray(b) => {
            w.put_i32(i32_len(b.len())?);
            w.put_slice(b);
        }
        Tag::String(s) => write_mutf8(w, s)?,
        Tag::List(l) => {
            w.put_u8(l.elem_type);
            w.put_i32(i32_len(l.items.len())?);
            if l.elem_type == tag::END && !l.items.is_empty() {
                return Err(EncodeError::Invalid("NBT list of TAG_End with elements"));
            }
            for item in &l.items {
                if item.type_id() != l.elem_type {
                    return Err(EncodeError::Invalid("NBT list element of the wrong type"));
                }
                write_payload(w, item, depth + 1)?;
            }
        }
        Tag::Compound(c) => {
            for (k, v) in &c.0 {
                w.put_u8(v.type_id());
                write_mutf8(w, k)?;
                write_payload(w, v, depth + 1)?;
            }
            w.put_u8(tag::END);
        }
        Tag::IntArray(a) => {
            w.put_i32(i32_len(a.len())?);
            for v in a {
                w.put_i32(*v);
            }
        }
        Tag::LongArray(a) => {
            w.put_i32(i32_len(a.len())?);
            for v in a {
                w.put_i64(*v);
            }
        }
    }
    Ok(())
}

/// Reads a `u16`-prefixed Modified UTF-8 string.
fn read_mutf8(r: &mut Reader<'_>) -> DecodeResult<String> {
    let n = r.read_u16()? as usize;
    let b = r.take(n)?;
    decode_mutf8(b)
}

/// Decodes Java Modified UTF-8.
pub fn decode_mutf8(b: &[u8]) -> DecodeResult<String> {
    // Fast path: plain ASCII without NUL (by far the common case) or UTF-8 that cannot
    // contain MUTF-8 specials (`C0 80` for NUL, `ED` lead bytes for surrogates).
    if b.iter().all(|&c| c != 0 && c < 0x80) {
        return std::str::from_utf8(b)
            .map(str::to_owned)
            .map_err(|_| DecodeError::InvalidUtf8);
    }
    if !b.iter().any(|&c| c == 0 || c == 0xc0 || c == 0xed)
        && let Ok(s) = std::str::from_utf8(b)
    {
        return Ok(s.to_owned());
    }
    let mut units: Vec<u16> = Vec::with_capacity(b.len());
    let mut i = 0;
    while i < b.len() {
        let c = b[i];
        if c < 0x80 {
            units.push(u16::from(c));
            i += 1;
        } else if c & 0xe0 == 0xc0 {
            let c2 = *b.get(i + 1).ok_or(DecodeError::InvalidUtf8)?;
            if c2 & 0xc0 != 0x80 {
                return Err(DecodeError::InvalidUtf8);
            }
            units.push((u16::from(c & 0x1f) << 6) | u16::from(c2 & 0x3f));
            i += 2;
        } else if c & 0xf0 == 0xe0 {
            let c2 = *b.get(i + 1).ok_or(DecodeError::InvalidUtf8)?;
            let c3 = *b.get(i + 2).ok_or(DecodeError::InvalidUtf8)?;
            if c2 & 0xc0 != 0x80 || c3 & 0xc0 != 0x80 {
                return Err(DecodeError::InvalidUtf8);
            }
            units.push(
                (u16::from(c & 0x0f) << 12) | (u16::from(c2 & 0x3f) << 6) | u16::from(c3 & 0x3f),
            );
            i += 3;
        } else {
            return Err(DecodeError::InvalidUtf8);
        }
    }
    Ok(String::from_utf16_lossy(&units))
}

/// Writes a `u16`-prefixed Modified UTF-8 string.
fn write_mutf8(w: &mut BytesMut, s: &str) -> EncodeResult {
    let too_long = |len| EncodeError::LengthOverflow {
        len,
        max: u16::MAX as usize,
    };
    // Fast path: no NUL and no supplementary characters means MUTF-8 == UTF-8.
    if s.bytes().all(|b| b != 0 && b < 0xf0) {
        let n = u16::try_from(s.len()).map_err(|_| too_long(s.len()))?;
        w.put_u16(n);
        w.put_slice(s.as_bytes());
        return Ok(());
    }
    let mut out = Vec::with_capacity(s.len() + 8);
    for u in s.encode_utf16() {
        match u {
            0x0001..=0x007f => out.push(u as u8),
            0x0000 | 0x0080..=0x07ff => {
                out.push(0xc0 | (u >> 6) as u8);
                out.push(0x80 | (u & 0x3f) as u8);
            }
            _ => {
                out.push(0xe0 | (u >> 12) as u8);
                out.push(0x80 | ((u >> 6) & 0x3f) as u8);
                out.push(0x80 | (u & 0x3f) as u8);
            }
        }
    }
    let n = u16::try_from(out.len()).map_err(|_| too_long(out.len()))?;
    w.put_u16(n);
    w.put_slice(&out);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The classic `hello_world.nbt` test file (uncompressed).
    const HELLO_WORLD: &[u8] = &[
        0x0a, 0x00, 0x0b, b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd', 0x08,
        0x00, 0x04, b'n', b'a', b'm', b'e', 0x00, 0x09, b'B', b'a', b'n', b'a', b'n', b'r', b'a',
        b'm', b'a', 0x00,
    ];

    #[test]
    fn hello_world() {
        let b = Bytes::from_static(HELLO_WORLD);
        let raw = RawNbt::from_bytes(b.clone()).unwrap();
        let t = raw.parse().unwrap().unwrap();
        assert_eq!(t.name, "hello world");
        let c = t.tag.as_compound().unwrap();
        assert_eq!(c.get("name"), Some(&Tag::String("Bananrama".into())));
        assert_eq!(RawNbt::from_named(&t).unwrap().as_bytes(), &b);
    }

    #[test]
    fn optional_and_end() {
        let b = Bytes::from_static(&[0x00, 0x01]);
        let mut r = Reader::new(&b);
        assert_eq!(RawNbt::read_optional(&mut r), Ok(None));
        assert_eq!(r.remaining(), 1);
        let b = Bytes::from_static(&[0x00]);
        let raw = RawNbt::from_bytes(b).unwrap();
        assert!(raw.is_end());
        assert_eq!(raw.parse(), Ok(None));
    }

    #[test]
    fn rejects_bad_input() {
        // Unknown tag type inside a compound.
        let b = Bytes::from_static(&[0x0a, 0x00, 0x00, 0x0d, 0x00, 0x00]);
        assert_eq!(RawNbt::from_bytes(b), Err(DecodeError::InvalidNbtTag(13)));
        // List of TAG_End with a positive length.
        let b = Bytes::from_static(&[0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05]);
        assert_eq!(RawNbt::from_bytes(b), Err(DecodeError::NbtEndList));
        // Huge int array length is bounded by the input instead of allocating.
        let b = Bytes::from_static(&[0x0b, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff]);
        assert!(matches!(
            RawNbt::from_bytes(b),
            Err(DecodeError::LengthTooLarge { .. })
        ));
        // Deep nesting is limited.
        let mut v = vec![0x09, 0x00, 0x00];
        for _ in 0..600 {
            v.extend_from_slice(&[0x09, 0x00, 0x00, 0x00, 0x01]);
        }
        let b = Bytes::from(v);
        assert_eq!(
            RawNbt::from_bytes(b),
            Err(DecodeError::NbtTooDeep(MAX_DEPTH))
        );
    }

    #[test]
    fn modified_utf8() {
        let s = "a\0b\u{e9}\u{1f600}";
        let mut w = BytesMut::new();
        write_mutf8(&mut w, s).unwrap();
        // NUL -> C0 80, U+1F600 -> surrogate pair, each 3 bytes.
        assert_eq!(
            &w[..],
            &[
                0x00, 0x0c, b'a', 0xc0, 0x80, b'b', 0xc3, 0xa9, 0xed, 0xa0, 0xbd, 0xed, 0xb8, 0x80
            ]
        );
        assert_eq!(decode_mutf8(&w[2..]).unwrap(), s);
        assert_eq!(decode_mutf8(&[0xc3]), Err(DecodeError::InvalidUtf8));
        // Standard 4-byte UTF-8 (as written by some non-Java encoders) is accepted.
        assert_eq!(
            decode_mutf8(&[0xf0, 0x9f, 0x98, 0x80]).unwrap(),
            "\u{1f600}"
        );
        assert_eq!(decode_mutf8(&[0xf8, 0x80]), Err(DecodeError::InvalidUtf8));
    }
}