Skip to main content

simp_rexif/
lowlevel.rs

1use super::rational::{IRational, URational};
2use std::convert::TryInto;
3
4/// Read value from a stream of bytes
5#[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/// Read value from a stream of bytes
16#[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/// Read value from a stream of bytes
27#[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/// Read value from a stream of bytes
38#[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/// Read value from a stream of bytes
49#[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/// Read value from a stream of bytes
55#[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/// Read value from a stream of bytes
61#[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/// Read value from a stream of bytes
69#[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
90/// Read array from a stream of bytes. Caller must be sure of count and buffer size
91pub(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)]
96/// Read array from a stream of bytes. Caller must be sure of count and buffer size
97pub(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
101/// Read array from a stream of bytes. Caller must be sure of count and buffer size
102pub(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
106/// Read array from a stream of bytes. Caller must be sure of count and buffer size
107pub(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
111/// Read array from a stream of bytes. Caller must be sure of count and buffer size
112pub(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
116/// Read array from a stream of bytes. Caller must be sure of count and buffer size
117pub(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
121/// Read array from a stream of bytes. Caller must be sure of count and buffer size
122pub(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
126/// Read array from a stream of bytes. Caller must be sure of count and buffer size
127pub(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
131/// Read array from a stream of bytes. Caller must be sure of count and buffer size
132pub(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}