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
use std::{borrow::Cow, io::Read, ops::Range};

use byteorder::{BigEndian, ReadBytesExt};

use crate::{
    error::{Error, Result},
    Tag,
};

mod private {
    // Only this crate can implement this trait. Other traits can inherit from
    // Sealed in order to prevent other crates from creating implementations.
    pub trait Sealed {}
}

fn try_size(size: i32, multiplier: usize) -> Result<usize> {
    let size: usize = size
        .try_into()
        .map_err(|_| Error::bespoke("size was negative".to_string()))?;

    size.checked_mul(multiplier)
        .ok_or_else(|| Error::bespoke("size too large".to_string()))
}
pub enum Reference<'b, 'c, T>
where
    T: ?Sized + 'static,
{
    Borrowed(&'b T),
    Copied(&'c T),
}

impl<'b, 'c> AsRef<[u8]> for Reference<'b, 'c, [u8]> {
    fn as_ref(&self) -> &[u8] {
        match self {
            Reference::Borrowed(bs) => bs,
            Reference::Copied(bs) => bs,
        }
    }
}

pub trait Input<'de>: private::Sealed {
    #[doc(hidden)]
    fn consume_byte(&mut self) -> Result<u8>;

    // #[doc(hidden)]
    // fn discard(&mut self);

    #[doc(hidden)]
    fn ignore_str(&mut self) -> Result<()>;

    #[doc(hidden)]
    fn ignore_bytes(&mut self, size: usize) -> Result<()>;

    fn consume_tag(&mut self) -> Result<Tag> {
        let tag = self.consume_byte()?;
        Tag::try_from(tag).map_err(|_| Error::invalid_tag(tag))
    }

    fn consume_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;

    fn consume_bytes<'s>(
        &'s mut self,
        n: usize,
        scratch: &'s mut Vec<u8>,
    ) -> Result<Reference<'de, 's, [u8]>>;

    fn consume_i16(&mut self) -> Result<i16>;
    fn consume_i32(&mut self) -> Result<i32>;
    fn consume_i64(&mut self) -> Result<i64>;
    fn consume_f32(&mut self) -> Result<f32>;
    fn consume_f64(&mut self) -> Result<f64>;

    fn ignore_value(&mut self, tag: Tag) -> Result<()> {
        match tag {
            Tag::Byte => {
                self.consume_byte()?;
            }
            Tag::Short => {
                self.consume_i16()?;
            }
            Tag::Int => {
                self.consume_i32()?;
            }
            Tag::Long => {
                self.consume_i64()?;
            }
            Tag::Float => {
                self.consume_f32()?;
            }
            Tag::Double => {
                self.consume_f64()?;
            }
            Tag::String => {
                self.ignore_str()?;
            }
            Tag::ByteArray => {
                let size = self.consume_i32()? as usize;
                self.ignore_bytes(size)?;
            }
            Tag::IntArray => {
                let size = self.consume_i32()?;
                self.ignore_bytes(try_size(size, std::mem::size_of::<i32>())?)?;
            }
            Tag::LongArray => {
                let size = self.consume_i32()?;
                self.ignore_bytes(try_size(size, std::mem::size_of::<i64>())?)?;
            }
            Tag::Compound => {
                // Need to loop and ignore each value until we reach an end tag.

                // we need to enter the compound, then ignore it's value.
                loop {
                    let tag = self.consume_tag()?;
                    if tag == Tag::End {
                        break;
                    }

                    // consume the name.
                    self.ignore_str()?;
                    self.ignore_value(tag)?;
                }
            }
            Tag::List => {
                let element_tag = self.consume_tag()?;
                let size = self.consume_i32()?;
                for _ in 0..size {
                    self.ignore_value(element_tag)?;
                }
            }
            Tag::End => {
                // If we are trying to ignore a list of empty compounds, that
                // list might be indicated by a series of End tags. If this
                // occurs then we should end the Compound branch of this match
                // statement, where the end tag will be consumed. So we should
                // never reach here.
                //
                // TODO: Write an explicit test for ignored list of compound.
                unreachable!()
            }
        }

        Ok(())
    }
}

pub(crate) struct Slice<'de> {
    pub data: &'de [u8],
}

impl<'de> private::Sealed for Slice<'de> {}
impl<'de> Slice<'de> {
    fn consume(&mut self, r: Range<usize>) -> Result<&'de [u8]> {
        if r.end <= self.data.len() {
            let ret = &self.data[r.start..r.end];
            self.data = &self.data[r.end..];
            Ok(ret)
        } else {
            Err(Error::unexpected_eof())
        }
    }
}

impl<'de> Input<'de> for Slice<'de> {
    fn consume_byte(&mut self) -> Result<u8> {
        Ok(self.consume(0..1)?[0])
    }

    fn ignore_str(&mut self) -> Result<()> {
        let len = self.consume(0..2)?.read_u16::<BigEndian>()? as usize;
        self.consume(0..len).map(|_| ())
    }

    fn consume_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
        let len = self.consume(0..2)?.read_u16::<BigEndian>()? as usize;
        let str = self.consume(0..len)?;
        let str = cesu8::from_java_cesu8(str).map_err(|_| Error::nonunicode_string(str))?;

        Ok(match str {
            Cow::Borrowed(str) => Reference::Borrowed(str),
            Cow::Owned(str) => {
                *scratch = str.into_bytes();
                // we just converted scratch into the bytes of a string, so it
                // definitely utf8.
                Reference::Copied(unsafe { std::str::from_utf8_unchecked(scratch) })
            }
        })
    }

    fn consume_bytes<'s>(
        &'s mut self,
        n: usize,
        _scratch: &'s mut Vec<u8>,
    ) -> Result<Reference<'de, 's, [u8]>> {
        let bs = self.consume(0..n)?;
        Ok(Reference::Borrowed(bs))
    }

    fn consume_i16(&mut self) -> Result<i16> {
        let mut bs = self.consume(0..std::mem::size_of::<i16>())?;
        Ok(bs.read_i16::<BigEndian>()?)
    }

    fn consume_i32(&mut self) -> Result<i32> {
        let mut bs = self.consume(0..std::mem::size_of::<i32>())?;
        Ok(bs.read_i32::<BigEndian>()?)
    }

    fn consume_i64(&mut self) -> Result<i64> {
        let mut bs = self.consume(0..std::mem::size_of::<i64>())?;
        Ok(bs.read_i64::<BigEndian>()?)
    }

    fn consume_f32(&mut self) -> Result<f32> {
        let mut bs = self.consume(0..std::mem::size_of::<f32>())?;
        Ok(bs.read_f32::<BigEndian>()?)
    }

    fn consume_f64(&mut self) -> Result<f64> {
        let mut bs = self.consume(0..std::mem::size_of::<f64>())?;
        Ok(bs.read_f64::<BigEndian>()?)
    }

    fn ignore_bytes(&mut self, size: usize) -> Result<()> {
        self.consume(0..size)?;
        Ok(())
    }
}

pub(crate) struct Reader<R: Read> {
    pub reader: R,
}

impl<R: Read> private::Sealed for Reader<R> {}

impl<'de, R: Read> Input<'de> for Reader<R> {
    fn consume_byte(&mut self) -> Result<u8> {
        Ok(self.reader.read_u8()?)
    }

    fn ignore_str(&mut self) -> Result<()> {
        let len = self.reader.read_u16::<BigEndian>()? as usize;
        let mut buf = vec![0; len]; // TODO: try a scratch space to reduce allocs?
        Ok(self.reader.read_exact(&mut buf)?)
    }

    fn consume_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
        let len = self.reader.read_u16::<BigEndian>()? as usize;
        scratch.clear();
        scratch.resize(len, 0);
        self.reader.read_exact(scratch)?;

        let str = cesu8::from_java_cesu8(scratch).map_err(|_| Error::nonunicode_string(scratch))?;

        Ok(match str {
            Cow::Borrowed(_) => {
                Reference::Copied(unsafe { std::str::from_utf8_unchecked(scratch) })
            }
            Cow::Owned(s) => {
                *scratch = s.into_bytes();
                Reference::Copied(unsafe { std::str::from_utf8_unchecked(scratch) })
            }
        })
    }

    fn consume_bytes<'s>(
        &'s mut self,
        n: usize,
        scratch: &'s mut Vec<u8>,
    ) -> Result<Reference<'de, 's, [u8]>> {
        scratch.clear();
        scratch.resize(n, 0);
        self.reader.read_exact(scratch.as_mut_slice())?;

        Ok(Reference::Copied(scratch.as_slice()))
    }

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

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

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

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

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

    fn ignore_bytes(&mut self, size: usize) -> Result<()> {
        let mut buf = vec![0; size]; // TODO: try a scratch space to reduce allocs?
        self.reader.read_exact(&mut buf)?;
        Ok(())
    }
}