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
use std::borrow::Cow;
use std::hash::Hash;
use std::{fmt, mem};

use byteorder::{BigEndian, ReadBytesExt};

use crate::tag::Tag;
use crate::{Compound, Error, List, Result, Value};

/// Decodes uncompressed NBT binary data from the provided slice.
///
/// The string returned in the tuple is the name of the root compound
/// (typically the empty string).
pub fn from_binary<'de, S>(slice: &mut &'de [u8]) -> Result<(Compound<S>, S)>
where
    S: FromModifiedUtf8<'de> + Hash + Ord,
{
    let mut state = DecodeState { slice, depth: 0 };

    let root_tag = state.read_tag()?;

    if root_tag != Tag::Compound {
        return Err(Error::new_owned(format!(
            "expected root tag for compound (got {})",
            root_tag.name(),
        )));
    }

    let root_name = state.read_string::<S>()?;
    let root = state.read_compound()?;

    debug_assert_eq!(state.depth, 0);

    Ok((root, root_name))
}

/// Maximum recursion depth to prevent overflowing the call stack.
const MAX_DEPTH: usize = 512;

struct DecodeState<'a, 'de> {
    slice: &'a mut &'de [u8],
    /// Current recursion depth.
    depth: usize,
}

impl<'de> DecodeState<'_, 'de> {
    #[inline]
    fn check_depth<T>(&mut self, f: impl FnOnce(&mut Self) -> Result<T>) -> Result<T> {
        if self.depth >= MAX_DEPTH {
            return Err(Error::new_static("reached maximum recursion depth"));
        }

        self.depth += 1;
        let res = f(self);
        self.depth -= 1;
        res
    }

    fn read_tag(&mut self) -> Result<Tag> {
        match self.slice.read_u8()? {
            0 => Ok(Tag::End),
            1 => Ok(Tag::Byte),
            2 => Ok(Tag::Short),
            3 => Ok(Tag::Int),
            4 => Ok(Tag::Long),
            5 => Ok(Tag::Float),
            6 => Ok(Tag::Double),
            7 => Ok(Tag::ByteArray),
            8 => Ok(Tag::String),
            9 => Ok(Tag::List),
            10 => Ok(Tag::Compound),
            11 => Ok(Tag::IntArray),
            12 => Ok(Tag::LongArray),
            byte => Err(Error::new_owned(format!("invalid tag byte of {byte:#x}"))),
        }
    }

    fn read_value<S>(&mut self, tag: Tag) -> Result<Value<S>>
    where
        S: FromModifiedUtf8<'de> + Hash + Ord,
    {
        match tag {
            Tag::End => unreachable!("illegal TAG_End argument"),
            Tag::Byte => Ok(self.read_byte()?.into()),
            Tag::Short => Ok(self.read_short()?.into()),
            Tag::Int => Ok(self.read_int()?.into()),
            Tag::Long => Ok(self.read_long()?.into()),
            Tag::Float => Ok(self.read_float()?.into()),
            Tag::Double => Ok(self.read_double()?.into()),
            Tag::ByteArray => Ok(self.read_byte_array()?.into()),
            Tag::String => Ok(Value::String(self.read_string::<S>()?)),
            Tag::List => self.check_depth(|st| Ok(st.read_any_list::<S>()?.into())),
            Tag::Compound => self.check_depth(|st| Ok(st.read_compound::<S>()?.into())),
            Tag::IntArray => Ok(self.read_int_array()?.into()),
            Tag::LongArray => Ok(self.read_long_array()?.into()),
        }
    }

    fn read_byte(&mut self) -> Result<i8> {
        Ok(self.slice.read_i8()?)
    }

    fn read_short(&mut self) -> Result<i16> {
        Ok(self.slice.read_i16::<BigEndian>()?)
    }

    fn read_int(&mut self) -> Result<i32> {
        Ok(self.slice.read_i32::<BigEndian>()?)
    }

    fn read_long(&mut self) -> Result<i64> {
        Ok(self.slice.read_i64::<BigEndian>()?)
    }

    fn read_float(&mut self) -> Result<f32> {
        Ok(self.slice.read_f32::<BigEndian>()?)
    }

    fn read_double(&mut self) -> Result<f64> {
        Ok(self.slice.read_f64::<BigEndian>()?)
    }

    fn read_byte_array(&mut self) -> Result<Vec<i8>> {
        let len = self.slice.read_i32::<BigEndian>()?;

        if len.is_negative() {
            return Err(Error::new_owned(format!(
                "negative byte array length of {len}"
            )));
        }

        if len as usize > self.slice.len() {
            return Err(Error::new_owned(format!(
                "byte array length of {len} exceeds remainder of input"
            )));
        }

        let (left, right) = self.slice.split_at(len as usize);

        let array = left.iter().map(|b| *b as i8).collect();
        *self.slice = right;

        Ok(array)
    }

    fn read_string<S>(&mut self) -> Result<S>
    where
        S: FromModifiedUtf8<'de>,
    {
        let len = self.slice.read_u16::<BigEndian>()?.into();

        if len > self.slice.len() {
            return Err(Error::new_owned(format!(
                "string of length {len} exceeds remainder of input"
            )));
        }

        let (left, right) = self.slice.split_at(len);

        match S::from_modified_utf8(left) {
            Ok(str) => {
                *self.slice = right;
                Ok(str)
            }
            Err(_) => Err(Error::new_static("could not decode modified UTF-8 data")),
        }
    }

    fn read_any_list<S>(&mut self) -> Result<List<S>>
    where
        S: FromModifiedUtf8<'de> + Hash + Ord,
    {
        match self.read_tag()? {
            Tag::End => match self.read_int()? {
                0 => Ok(List::End),
                len => Err(Error::new_owned(format!(
                    "TAG_End list with nonzero length of {len}"
                ))),
            },
            Tag::Byte => Ok(self.read_list(Tag::Byte, 1, |st| st.read_byte())?.into()),
            Tag::Short => Ok(self.read_list(Tag::Short, 2, |st| st.read_short())?.into()),
            Tag::Int => Ok(self.read_list(Tag::Int, 4, |st| st.read_int())?.into()),
            Tag::Long => Ok(self.read_list(Tag::Long, 8, |st| st.read_long())?.into()),
            Tag::Float => Ok(self.read_list(Tag::Float, 4, |st| st.read_float())?.into()),
            Tag::Double => Ok(self
                .read_list(Tag::Double, 8, |st| st.read_double())?
                .into()),
            Tag::ByteArray => Ok(self
                .read_list(Tag::ByteArray, 0, |st| st.read_byte_array())?
                .into()),
            Tag::String => Ok(List::String(
                self.read_list(Tag::String, 0, |st| st.read_string::<S>())?,
            )),
            Tag::List => self.check_depth(|st| {
                Ok(st
                    .read_list(Tag::List, 0, |st| st.read_any_list::<S>())?
                    .into())
            }),
            Tag::Compound => self.check_depth(|st| {
                Ok(st
                    .read_list(Tag::Compound, 0, |st| st.read_compound::<S>())?
                    .into())
            }),
            Tag::IntArray => Ok(self
                .read_list(Tag::IntArray, 0, |st| st.read_int_array())?
                .into()),
            Tag::LongArray => Ok(self
                .read_list(Tag::LongArray, 0, |st| st.read_long_array())?
                .into()),
        }
    }

    /// Assumes the element tag has already been read.
    ///
    /// `min_elem_size` is the minimum size of the list element when encoded.
    #[inline]
    fn read_list<T, F>(
        &mut self,
        elem_type: Tag,
        elem_size: usize,
        mut read_elem: F,
    ) -> Result<Vec<T>>
    where
        F: FnMut(&mut Self) -> Result<T>,
    {
        let len = self.read_int()?;

        if len.is_negative() {
            return Err(Error::new_owned(format!(
                "negative {} list length of {len}",
                elem_type.name()
            )));
        }

        // Ensure we don't reserve more than the maximum amount of memory required given
        // the size of the remaining input.
        if len as u64 * elem_size as u64 > self.slice.len() as u64 {
            return Err(Error::new_owned(format!(
                "{} list of length {len} exceeds remainder of input",
                elem_type.name()
            )));
        }

        let mut list = Vec::with_capacity(if elem_size == 0 { 0 } else { len as usize });

        for _ in 0..len {
            list.push(read_elem(self)?);
        }

        Ok(list)
    }

    fn read_compound<S>(&mut self) -> Result<Compound<S>>
    where
        S: FromModifiedUtf8<'de> + Hash + Ord,
    {
        let mut compound = Compound::new();

        loop {
            let tag = self.read_tag()?;
            if tag == Tag::End {
                return Ok(compound);
            }

            compound.insert(self.read_string::<S>()?, self.read_value::<S>(tag)?);
        }
    }

    fn read_int_array(&mut self) -> Result<Vec<i32>> {
        let len = self.read_int()?;

        if len.is_negative() {
            return Err(Error::new_owned(format!(
                "negative int array length of {len}",
            )));
        }

        if len as u64 * mem::size_of::<i32>() as u64 > self.slice.len() as u64 {
            return Err(Error::new_owned(format!(
                "int array of length {len} exceeds remainder of input"
            )));
        }

        let mut array = Vec::with_capacity(len as usize);
        for _ in 0..len {
            array.push(self.read_int()?);
        }

        Ok(array)
    }

    fn read_long_array(&mut self) -> Result<Vec<i64>> {
        let len = self.read_int()?;

        if len.is_negative() {
            return Err(Error::new_owned(format!(
                "negative long array length of {len}",
            )));
        }

        if len as u64 * mem::size_of::<i64>() as u64 > self.slice.len() as u64 {
            return Err(Error::new_owned(format!(
                "long array of length {len} exceeds remainder of input"
            )));
        }

        let mut array = Vec::with_capacity(len as usize);
        for _ in 0..len {
            array.push(self.read_long()?);
        }

        Ok(array)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct FromModifiedUtf8Error;

impl fmt::Display for FromModifiedUtf8Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("could not decode modified UTF-8 data")
    }
}

impl std::error::Error for FromModifiedUtf8Error {}

/// A string type which can be decoded from Java's [modified UTF-8](https://docs.oracle.com/javase/8/docs/api/java/io/DataInput.html#modified-utf-8).
pub trait FromModifiedUtf8<'de>: Sized {
    fn from_modified_utf8(
        modified_utf8: &'de [u8],
    ) -> std::result::Result<Self, FromModifiedUtf8Error>;
}

impl<'de> FromModifiedUtf8<'de> for Cow<'de, str> {
    fn from_modified_utf8(
        modified_utf8: &'de [u8],
    ) -> std::result::Result<Self, FromModifiedUtf8Error> {
        cesu8::from_java_cesu8(modified_utf8).map_err(move |_| FromModifiedUtf8Error)
    }
}

impl<'de> FromModifiedUtf8<'de> for String {
    fn from_modified_utf8(
        modified_utf8: &'de [u8],
    ) -> std::result::Result<Self, FromModifiedUtf8Error> {
        match cesu8::from_java_cesu8(modified_utf8) {
            Ok(str) => Ok(str.into_owned()),
            Err(_) => Err(FromModifiedUtf8Error),
        }
    }
}

#[cfg(feature = "java_string")]
impl<'de> FromModifiedUtf8<'de> for Cow<'de, java_string::JavaStr> {
    fn from_modified_utf8(
        modified_utf8: &'de [u8],
    ) -> std::result::Result<Self, FromModifiedUtf8Error> {
        java_string::JavaStr::from_modified_utf8(modified_utf8).map_err(|_| FromModifiedUtf8Error)
    }
}

#[cfg(feature = "java_string")]
impl<'de> FromModifiedUtf8<'de> for java_string::JavaString {
    fn from_modified_utf8(
        modified_utf8: &'de [u8],
    ) -> std::result::Result<Self, FromModifiedUtf8Error> {
        match java_string::JavaStr::from_modified_utf8(modified_utf8) {
            Ok(str) => Ok(str.into_owned()),
            Err(_) => Err(FromModifiedUtf8Error),
        }
    }
}