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
use std::ops::{Deref, DerefMut};

use byteorder::{BigEndian, ReadBytesExt};
use serde::{de::Visitor, Deserialize, Serialize};
use serde_bytes::ByteBuf;

pub(crate) const BYTE_ARRAY_TOKEN: &str = "__fastnbt_byte_array";
pub(crate) const INT_ARRAY_TOKEN: &str = "__fastnbt_int_array";
pub(crate) const LONG_ARRAY_TOKEN: &str = "__fastnbt_long_array";

/// NBT ByteArray that owns its data. This type preserves the exact NBT type
/// when (de)serializing. This dereferences into a i8 slice, so should be usable
/// basically anywhere a slice should be.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "arbitrary1", derive(arbitrary::Arbitrary))]
pub struct ByteArray {
    data: Vec<i8>,
}

impl Serialize for ByteArray {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[derive(Serialize)]
        struct Inner {
            __fastnbt_byte_array: ByteBuf,
        }

        Inner {
            __fastnbt_byte_array: ByteBuf::from(self.to_bytes()),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ByteArray {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct InnerVisitor;
        impl<'de> Visitor<'de> for InnerVisitor {
            type Value = ByteArray;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("byte array")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                let token = map.next_key::<&str>()?.ok_or_else(|| {
                    serde::de::Error::custom("expected NBT byte array token, but got empty map")
                })?;
                let data = map.next_value::<ByteBuf>()?;

                if token == BYTE_ARRAY_TOKEN {
                    Ok(ByteArray::from_buf(data.into_vec()))
                } else {
                    Err(serde::de::Error::custom("expected NBT byte array token"))
                }
            }
        }
        deserializer.deserialize_map(InnerVisitor)
    }
}

impl ByteArray {
    /// Create a new ByteArray from the given data.
    pub fn new(data: Vec<i8>) -> Self {
        Self { data }
    }

    /// Move the raw data out of this ByteArray.
    pub fn into_inner(self) -> Vec<i8> {
        self.data
    }

    /// Produce a ByteArray from raw data.
    pub(crate) fn from_bytes(data: &[u8]) -> Self {
        // Safe to treat [u8] as [i8].
        let data = unsafe { &*(data as *const [u8] as *const [i8]) };
        ByteArray {
            data: data.to_owned(),
        }
    }

    /// Produce a ByteArray from raw data.
    pub(crate) fn from_buf(data: Vec<u8>) -> Self {
        // TODO: Remove copy.
        let data = data.as_slice();

        // Safe to treat [u8] as [i8].
        let data = unsafe { &*(data as *const [u8] as *const [i8]) };
        ByteArray {
            data: data.to_owned(),
        }
    }

    pub(crate) fn to_bytes(&self) -> Vec<u8> {
        self.data.iter().flat_map(|i| i.to_be_bytes()).collect()
    }
}

impl Deref for ByteArray {
    type Target = [i8];

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for ByteArray {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

/// NBT IntArray that owns its data. This type preserves the exact NBT type
/// when (de)serializing. This dereferences into a i32 slice, so should be usable
/// basically anywhere a slice should be.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "arbitrary1", derive(arbitrary::Arbitrary))]
pub struct IntArray {
    data: Vec<i32>,
}

impl IntArray {
    /// Create a new IntArray from the given data.
    pub fn new(data: Vec<i32>) -> Self {
        Self { data }
    }

    /// Move the raw data out of this IntArray.
    pub fn into_inner(self) -> Vec<i32> {
        self.data
    }

    /// Produce a IntArray from raw data. This data should be big endian!
    pub(crate) fn from_bytes(data: &[u8]) -> std::io::Result<Self> {
        let data = data
            .chunks_exact(4)
            .map(|mut bs| bs.read_i32::<BigEndian>())
            .collect::<std::io::Result<Vec<i32>>>()?;

        Ok(IntArray { data })
    }

    pub(crate) fn to_bytes(&self) -> Vec<u8> {
        self.data.iter().flat_map(|i| i.to_be_bytes()).collect()
    }
}

impl Serialize for IntArray {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[derive(Serialize)]
        struct Inner {
            __fastnbt_int_array: ByteBuf,
        }

        Inner {
            __fastnbt_int_array: ByteBuf::from(self.to_bytes()),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for IntArray {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct InnerVisitor;
        impl<'de> Visitor<'de> for InnerVisitor {
            type Value = IntArray;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("int array")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                let token = map.next_key::<&str>()?.ok_or_else(|| {
                    serde::de::Error::custom("expected NBT int array token, but got empty map")
                })?;
                let data = map.next_value::<ByteBuf>()?;

                match token {
                    INT_ARRAY_TOKEN => IntArray::from_bytes(&data)
                        .map_err(|_| serde::de::Error::custom("could not read i32 for int array")),
                    _ => Err(serde::de::Error::custom("expected NBT int array token")),
                }
            }
        }
        deserializer.deserialize_map(InnerVisitor)
    }
}

impl Deref for IntArray {
    type Target = [i32];

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for IntArray {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

/// NBT LongArray that owns its data. This type preserves the exact NBT type
/// when (de)serializing. This dereferences into a i64 slice, so should be usable
/// basically anywhere a slice should be.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "arbitrary1", derive(arbitrary::Arbitrary))]
pub struct LongArray {
    data: Vec<i64>,
}

impl LongArray {
    /// Create a new LongArray from the given data.
    pub fn new(data: Vec<i64>) -> Self {
        Self { data }
    }

    /// Move the raw data out of this LongArray.
    pub fn into_inner(self) -> Vec<i64> {
        self.data
    }

    pub(crate) fn from_bytes(data: &[u8]) -> std::io::Result<Self> {
        let data = data
            .chunks_exact(8)
            .map(|mut bs| bs.read_i64::<BigEndian>())
            .collect::<std::io::Result<Vec<i64>>>()?;

        Ok(LongArray { data })
    }

    pub(crate) fn to_bytes(&self) -> Vec<u8> {
        self.data.iter().flat_map(|i| i.to_be_bytes()).collect()
    }
}

impl Serialize for LongArray {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[derive(Serialize)]
        struct Inner {
            __fastnbt_long_array: ByteBuf,
        }

        Inner {
            __fastnbt_long_array: ByteBuf::from(self.to_bytes()),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for LongArray {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct InnerVisitor;
        impl<'de> Visitor<'de> for InnerVisitor {
            type Value = LongArray;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("long array")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                let token = map.next_key::<&str>()?.ok_or_else(|| {
                    serde::de::Error::custom("expected NBT long array token, but got empty map")
                })?;
                let data = map.next_value::<ByteBuf>()?;

                match token {
                    LONG_ARRAY_TOKEN => LongArray::from_bytes(&data)
                        .map_err(|_| serde::de::Error::custom("could not read i64 for long array")),
                    _ => Err(serde::de::Error::custom("expected NBT long array token")),
                }
            }
        }
        deserializer.deserialize_map(InnerVisitor)
    }
}

impl Deref for LongArray {
    type Target = [i64];

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for LongArray {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}