1use super::rational::{IRational, URational};
2use std::convert::TryInto;
3
4#[inline(always)]
6pub(crate) fn read_u16(le: bool, raw: &[u8]) -> Option<u16> {
7 let bytes = raw.get(..2)?.try_into().ok()?;
8 Some(if le {
9 u16::from_le_bytes(bytes)
10 } else {
11 u16::from_be_bytes(bytes)
12 })
13}
14
15#[inline(always)]
17pub(crate) fn read_i16(le: bool, raw: &[u8]) -> Option<i16> {
18 let bytes = raw.get(..2)?.try_into().ok()?;
19 Some(if le {
20 i16::from_le_bytes(bytes)
21 } else {
22 i16::from_be_bytes(bytes)
23 })
24}
25
26#[inline(always)]
28pub(crate) fn read_u32(le: bool, raw: &[u8]) -> Option<u32> {
29 let bytes = raw.get(..4)?.try_into().ok()?;
30 Some(if le {
31 u32::from_le_bytes(bytes)
32 } else {
33 u32::from_be_bytes(bytes)
34 })
35}
36
37#[inline(always)]
39pub(crate) fn read_i32(le: bool, raw: &[u8]) -> Option<i32> {
40 let bytes = raw.get(..4)?.try_into().ok()?;
41 Some(if le {
42 i32::from_le_bytes(bytes)
43 } else {
44 i32::from_be_bytes(bytes)
45 })
46}
47
48#[inline(always)]
50pub(crate) fn read_f32(raw: &[u8]) -> Option<f32> {
51 raw.get(..4)?.try_into().ok().map(f32::from_le_bytes)
52}
53
54#[inline(always)]
56pub(crate) fn read_f64(raw: &[u8]) -> Option<f64> {
57 raw.get(..8)?.try_into().ok().map(f64::from_le_bytes)
58}
59
60#[inline(always)]
62pub(crate) fn read_urational(le: bool, raw: &[u8]) -> Option<URational> {
63 let n = read_u32(le, &raw[0..4])?;
64 let d = read_u32(le, &raw[4..8])?;
65 Some(URational { numerator: n, denominator: d })
66}
67
68#[inline(always)]
70pub(crate) fn read_irational(le: bool, raw: &[u8]) -> Option<IRational> {
71 let n = read_i32(le, &raw[0..4])?;
72 let d = read_i32(le, &raw[4..8])?;
73 Some(IRational { numerator: n, denominator: d })
74}
75
76#[inline(always)]
77fn read_elements<T>(size: u8, count: u32, raw: &[u8], convert: impl Fn(&[u8]) -> T) -> Option<Vec<T>> {
78 let count = count as usize;
79 let size = size as usize;
80 let byte_size = size.checked_mul(count)?;
81 let bytes = raw.get(..byte_size)?;
82
83 let mut out = Vec::new();
84 out.try_reserve_exact(count).ok()?;
85 out.extend(bytes.chunks_exact(size).map(convert).take(count));
86
87 Some(out)
88}
89
90pub(crate) fn read_i8_array(count: u32, raw: &[u8]) -> Option<Vec<i8>> {
92 Some(raw.get(..count as usize)?.iter().map(|&i| i as i8).collect())
93}
94
95#[inline(never)]
96pub(crate) fn read_u16_array(le: bool, count: u32, raw: &[u8]) -> Option<Vec<u16>> {
98 read_elements(2, count, raw, move |ch| read_u16(le, ch).unwrap())
99}
100
101pub(crate) fn read_i16_array(le: bool, count: u32, raw: &[u8]) -> Option<Vec<i16>> {
103 read_elements(2, count, raw, move |ch| read_i16(le, ch).unwrap())
104}
105
106pub(crate) fn read_u32_array(le: bool, count: u32, raw: &[u8]) -> Option<Vec<u32>> {
108 read_elements(4, count, raw, move |ch| read_u32(le, ch).unwrap())
109}
110
111pub(crate) fn read_i32_array(le: bool, count: u32, raw: &[u8]) -> Option<Vec<i32>> {
113 read_elements(4, count, raw, move |ch| read_i32(le, ch).unwrap())
114}
115
116pub(crate) fn read_f32_array(count: u32, raw: &[u8]) -> Option<Vec<f32>> {
118 read_elements(4, count, raw, move |ch| read_f32(ch).unwrap())
119}
120
121pub(crate) fn read_f64_array(count: u32, raw: &[u8]) -> Option<Vec<f64>> {
123 read_elements(8, count, raw, move |ch| read_f64(ch).unwrap())
124}
125
126pub(crate) fn read_urational_array(le: bool, count: u32, raw: &[u8]) -> Option<Vec<URational>> {
128 read_elements(8, count, raw, move |ch| read_urational(le, ch).unwrap())
129}
130
131pub(crate) fn read_irational_array(le: bool, count: u32, raw: &[u8]) -> Option<Vec<IRational>> {
133 read_elements(8, count, raw, move |ch| read_irational(le, ch).unwrap())
134}