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
use super::Segment;
use crate::{
    error::{Error, Result},
    marker::Integer,
    Endidness,
};
use core::convert::TryFrom;
/// An alias for a segment that deals with binary data.
pub type DataSegment<'s> = Segment<'s, u8>;

macro_rules! make_num_method {
    ($type:ty, $name:ident, $method:ident, $($doc:literal),+) => {
        $(#[doc = $doc])+
        pub fn $name(&self) -> Result<$type> {
            self.$method::<$type>()
        }
    };
}
macro_rules! make_num_method_with_offset {
    ($type:ty, $name:ident, $method:ident, $($doc:literal),+) => {
        $(#[doc = $doc])+
        pub fn $name(&self, offset: usize) -> Result<$type> {
            self.$method::<$type>(offset)
        }
    };
}

impl<'s> DataSegment<'s> {
    /// Creates a new [`Segment`] using the provided endidness.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    #[inline]
    pub fn with_endidness(data: &'s [u8], endidness: Endidness) -> Self {
        Self::new_full(data, 0, 0, endidness)
    }

    /// Creates a new [`Segment`] using the provided endidness and initial offset.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    #[inline]
    pub fn with_offset_and_endidness(
        data: &'s [u8],
        initial_offset: usize,
        endidness: Endidness,
    ) -> Self {
        Self::new_full(data, initial_offset, 0, endidness)
    }

    #[inline]
    /// The endidness of the [`Segment`].
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn endidness(&self) -> Endidness {
        self.endidness
    }

    /// Fills the provided buffer with the next n bytes, where n is the length of the buffer. This
    /// then advances the [`Segment::current_offset`] by n.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn next_bytes(&self, buf: &mut [u8]) -> Result<()> {
        //FIXME not async/threadsafe
        for i in 0..buf.len() {
            buf[i] = self.next_u8()?;
        }
        Ok(())
    }

    fn int_at_pos<N: Integer>(&self, pos: usize) -> Result<N> {
        self.validate_pos(pos, N::WIDTH - 1)?;
        Ok(N::with_endidness(
            &self.data[pos..pos + N::WIDTH],
            self.endidness,
        ))
    }

    /// Gets an integer of the provided type (e.g. `u8`, `i8`, `u16`, `i16`, etcetera) at the given
    /// offset without altering the [`Segment::current_offset`]. In most cases, you should use
    /// methods like [`Segment::u8_at`] instead.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn int_at<N: Integer>(&self, offset: usize) -> Result<N> {
        self.validate_offset(offset, N::WIDTH - 1)?;
        Ok(N::with_endidness(
            &self[offset..offset + N::WIDTH],
            self.endidness,
        ))
    }

    #[inline]
    /// See the documentation for [`Segment::int_at`].
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn u8_at(&self, offset: usize) -> Result<u8> {
        self.item_at(offset)
    }
    make_num_method_with_offset! {u16, u16_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {u32, u32_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {u64, u64_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {u128, u128_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {i8, i8_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {i16, i16_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {i32, i32_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {i64, i64_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method_with_offset! {i128, i128_at, int_at,
    "See the documentation for [`Segment::int_at`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    /// Gets an integer of the provided type (e.g. `u8`, `i8`, `u16`, `i16`, etcetera) starting at
    /// the at the [`Segment::current_offset`] without altering it. In most cases, you should use
    /// methods like [`Segment::current_u8`] instead.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    #[inline]
    pub fn current_int<N: Integer>(&self) -> Result<N> {
        self.int_at(self.current_offset())
    }

    make_num_method! {u8, current_u8, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u16, current_u16, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u32, current_u32, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u64, current_u64, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u128, current_u128, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i8, current_i8, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i16, current_i16, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i32, current_i32, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i64, current_i64, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i128, current_i128, current_int,
    "See the documentation for [`Segment::current_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    /// Gets an integer of the provided type (e.g. `u8`, `i8`, `u16`, `i16`, etcetera) starting at
    /// the at the [`Segment::current_offset`] but without advancing the
    /// [`Segment::current_offset`]. In most
    /// cases, you should use methods like [`Segment::peek_u8`] instead.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn peek_int<N: Integer>(&self) -> Result<N> {
        let pos = self.adj_pos(N::WIDTH as i128)?;
        self.int_at(self.pos_to_offset(pos))
    }

    make_num_method! {u8, peek_u8, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u16, peek_u16, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u32, peek_u32, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u64, peek_u64, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u128, peek_u128, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i8, peek_i8, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i16, peek_i16, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i32, peek_i32, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i64, peek_i64, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i128, peek_i128, peek_int,
    "See the documentation for [`Segment::peek_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    /// Gets an integer of the provided type (e.g. `u8`, `i8`, `u16`, `i16`, etcetera) starting at
    /// the at the [`Segment::current_offset`] and then advances the [`Segment::current_offset`] by
    /// n, where n is the number of bytes required to create the requested integer type. In most
    /// cases, you should use methods like [`Segment::next_u8`] instead.
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn next_int<N: Integer>(&self) -> Result<N> {
        let pos = self.adj_pos(N::WIDTH as i128)?;
        self.int_at(self.pos_to_offset(pos))
    }

    #[inline]
    /// See the documentation for [`Segment::next_int`].
    ///
    /// Note: Only available if the [`Segment`]'s I is `u8`.
    pub fn next_u8(&self) -> Result<u8> {
        self.next_item()
    }

    make_num_method! {u16, next_u16, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u32, next_u32, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u64, next_u64, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {u128, next_u128, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i8, next_i8, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i16, next_i16, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i32, next_i32, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i64, next_i64, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}

    make_num_method! {i128, next_i128, next_int,
    "See the documentation for [`Segment::next_int`].\n\n",
    "Note: Only available if the [`Segment`]'s I is `u8`."}
}
impl<'s> TryFrom<&DataSegment<'s>> for () {
    type Error = Error;
    fn try_from(_: &Segment<'s, u8>) -> Result<Self> {
        Ok(())
    }
}
impl<'s> TryFrom<&Segment<'s, u8>> for u8 {
    type Error = Error;
    fn try_from(segment: &Segment<'s, u8>) -> Result<Self> {
        segment.next_item()
    }
}

macro_rules! impl_try_from {
    ($type:ty) => {
        impl<'s> TryFrom<&Segment<'s, u8>> for $type {
            type Error = Error;
            fn try_from(segment: &Segment<'s, u8>) -> Result<Self> {
                segment.next_int()
            }
        }
        impl<'s, const N: usize> TryFrom<&Segment<'s, u8>> for [$type; N] {
            type Error = Error;
            fn try_from(segment: &Segment<'s, u8>) -> Result<Self> {
                let pos = segment.adj_pos((<$type>::WIDTH * N) as i128)?;
                let mut array = [0; N];
                for i in 0..N {
                    array[i] = segment.int_at_pos(pos + (i * <$type>::WIDTH))?
                }
                Ok(array)
            }
        }
    };
}

impl_try_from! { u16 }
impl_try_from! { u32 }
impl_try_from! { u64 }
impl_try_from! { u128 }

impl_try_from! { i8 }
impl_try_from! { i16 }
impl_try_from! { i32 }
impl_try_from! { i64 }
impl_try_from! { i128 }