polysmelt 0.19.0

Pure-Rust 3D asset interchange: glTF/GLB reading and writing, meshopt compression, and readers for common mesh formats, SketchUp included
Documentation
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Reading binary files: [`Bin`], a cursor that checks every read against the end, and reads at
//! offsets that give `None` past it.
//!
//! The byte order is a type: [`Le`] and [`Be`] cost nothing at run time, and [`Endian`] holds
//! one a file chooses for itself. Bulk reads ([`Bin::f32s`], [`Bin::u32s`], [`Bin::vec3s`]) check
//! the whole run once, before allocating, so a huge count in a small file is an error rather
//! than a huge allocation, and decode without a bounds check per value.

use crate::Result;

/// A byte order.
pub(crate) trait Order: Copy {
    fn u16(self, b: [u8; 2]) -> u16;
    fn u32(self, b: [u8; 4]) -> u32;
    fn u64(self, b: [u8; 8]) -> u64;
}

/// Little-endian, the order most formats use.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Le;

/// Big-endian.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Be;

/// The order a file says it uses: big-endian when `big`.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Endian {
    pub big: bool,
}

impl Order for Le {
    #[inline]
    fn u16(self, b: [u8; 2]) -> u16 {
        u16::from_le_bytes(b)
    }
    #[inline]
    fn u32(self, b: [u8; 4]) -> u32 {
        u32::from_le_bytes(b)
    }
    #[inline]
    fn u64(self, b: [u8; 8]) -> u64 {
        u64::from_le_bytes(b)
    }
}

impl Order for Be {
    #[inline]
    fn u16(self, b: [u8; 2]) -> u16 {
        u16::from_be_bytes(b)
    }
    #[inline]
    fn u32(self, b: [u8; 4]) -> u32 {
        u32::from_be_bytes(b)
    }
    #[inline]
    fn u64(self, b: [u8; 8]) -> u64 {
        u64::from_be_bytes(b)
    }
}

impl Order for Endian {
    #[inline]
    fn u16(self, b: [u8; 2]) -> u16 {
        if self.big { Be.u16(b) } else { Le.u16(b) }
    }
    #[inline]
    fn u32(self, b: [u8; 4]) -> u32 {
        if self.big { Be.u32(b) } else { Le.u32(b) }
    }
    #[inline]
    fn u64(self, b: [u8; 8]) -> u64 {
        if self.big { Be.u64(b) } else { Le.u64(b) }
    }
}

/// The `N` bytes at `at`, or `None` where they'd run past the end.
#[inline]
pub(crate) fn array_at<const N: usize>(d: &[u8], at: usize) -> Option<[u8; N]> {
    d.get(at..at.checked_add(N)?)?.try_into().ok()
}

/// The little-endian `u16` at `at`, or `None` past the end.
#[inline]
pub(crate) fn u16_at(d: &[u8], at: usize) -> Option<u16> {
    array_at(d, at).map(u16::from_le_bytes)
}

/// The little-endian `u32` at `at`, or `None` past the end.
#[inline]
pub(crate) fn u32_at(d: &[u8], at: usize) -> Option<u32> {
    array_at(d, at).map(u32::from_le_bytes)
}

/// The little-endian `u64` at `at`, or `None` past the end.
#[inline]
pub(crate) fn u64_at(d: &[u8], at: usize) -> Option<u64> {
    array_at(d, at).map(u64::from_le_bytes)
}

/// The little-endian `f32` at `at`, or `None` past the end.
#[inline]
pub(crate) fn f32_at(d: &[u8], at: usize) -> Option<f32> {
    array_at(d, at).map(f32::from_le_bytes)
}

/// The little-endian `f64` at `at`, or `None` past the end.
#[inline]
pub(crate) fn f64_at(d: &[u8], at: usize) -> Option<f64> {
    array_at(d, at).map(f64::from_le_bytes)
}

/// Up to `n` whole `N`-byte records from `at` on: as many as there are bytes for.
#[inline]
pub(crate) fn records<const N: usize>(d: &[u8], at: usize, n: usize) -> &[[u8; N]] {
    let all = d.get(at..).unwrap_or_default().as_chunks::<N>().0;
    all.get(..n).unwrap_or(all)
}

/// The `f32`s in `b`, in order `o`.
#[inline]
pub(crate) fn floats<O: Order, const B: usize, const N: usize>(o: O, b: [u8; B]) -> [f32; N] {
    let (words, _) = b.as_chunks::<4>();
    std::array::from_fn(|k| f32::from_bits(o.u32(words[k])))
}

/// The bytes before the first NUL, or all of them.
#[inline]
pub(crate) fn until_nul(b: &[u8]) -> &[u8] {
    b.iter().position(|&c| c == 0).map_or(b, |end| &b[..end])
}

/// A reader over a file's bytes that checks every read against the end. A read past it fails
/// with a [`Truncated`](crate::ErrorKind::Truncated) error that names the format and leaves the
/// cursor where it was.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Bin<'a, O: Order = Le> {
    pub data: &'a [u8],
    /// Where the next read starts; never past the end of `data`.
    pub at: usize,
    /// The format's name, for errors.
    pub what: &'static str,
    pub order: O,
}

impl<'a> Bin<'a, Le> {
    /// A little-endian reader from the start of `data`.
    pub fn new(data: &'a [u8], what: &'static str) -> Self {
        Self::with_order(data, what, Le)
    }
}

impl<'a> Bin<'a, Be> {
    /// A big-endian reader from the start of `data`.
    pub fn be(data: &'a [u8], what: &'static str) -> Self {
        Self::with_order(data, what, Be)
    }
}

/// `n` values of `size` bytes each, read in bulk: one check for the run, then no bounds check
/// per value.
macro_rules! bulk {
    ($(#[$doc:meta] $name:ident -> $t:ty, $size:literal, |$o:ident, $c:ident| $conv:expr;)*) => {$(
        #[$doc]
        #[inline]
        pub fn $name(&mut self, n: usize) -> Result<Vec<$t>> {
            let $o = self.order;
            let b = self.run(n, $size)?;
            Ok(b.as_chunks::<$size>().0.iter().map(|&$c| $conv).collect())
        }
    )*};
}

impl<'a, O: Order> Bin<'a, O> {
    /// A reader from the start of `data` in byte order `order`.
    pub fn with_order(data: &'a [u8], what: &'static str, order: O) -> Self {
        Self {
            data,
            at: 0,
            what,
            order,
        }
    }

    /// Where the next read starts.
    #[inline]
    pub fn pos(&self) -> usize {
        self.at
    }

    /// Bytes left.
    #[inline]
    pub fn left(&self) -> usize {
        self.data.len().saturating_sub(self.at)
    }

    /// The bytes left, without moving past them.
    #[inline]
    pub fn rest(&self) -> &'a [u8] {
        self.data.get(self.at..).unwrap_or_default()
    }

    /// The next `n` bytes.
    #[inline]
    pub fn bytes(&mut self, n: usize) -> Result<&'a [u8]> {
        match self.at.checked_add(n).filter(|&end| end <= self.data.len()) {
            Some(end) => {
                let s = &self.data[self.at..end];
                self.at = end;
                Ok(s)
            }
            None => Err(self.cut_short()),
        }
    }

    #[cold]
    #[inline(never)]
    fn cut_short(&self) -> crate::Error {
        err!(Truncated, "{} file is cut short", self.what)
    }

    /// Moves past `n` bytes.
    #[inline]
    pub fn skip(&mut self, n: usize) -> Result<()> {
        self.bytes(n).map(|_| ())
    }

    /// Moves to `off` from the start.
    pub fn seek(&mut self, off: usize) -> Result<()> {
        ensure!(
            off <= self.data.len(),
            Truncated,
            "{} file has an offset that runs past the end of the file",
            self.what
        );
        self.at = off;
        Ok(())
    }

    /// A reader over the next `n` bytes, which this one moves past: a chunk's body, whose reads
    /// can't run into what follows it.
    #[inline]
    pub fn sub(&mut self, n: usize) -> Result<Self> {
        let data = self.bytes(n)?;
        Ok(Self { data, at: 0, ..*self })
    }

    /// The next `N` bytes as an array.
    #[inline]
    pub fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
        let mut out = [0; N];
        out.copy_from_slice(self.bytes(N)?);
        Ok(out)
    }

    #[inline]
    pub fn u8(&mut self) -> Result<u8> {
        Ok(self.array::<1>()?[0])
    }

    #[inline]
    pub fn i8(&mut self) -> Result<i8> {
        Ok(self.u8()?.cast_signed())
    }

    /// A byte, true unless 0.
    #[inline]
    pub fn bool(&mut self) -> Result<bool> {
        Ok(self.u8()? != 0)
    }

    #[inline]
    pub fn u16(&mut self) -> Result<u16> {
        let b = self.array()?;
        Ok(self.order.u16(b))
    }

    #[inline]
    pub fn i16(&mut self) -> Result<i16> {
        Ok(self.u16()?.cast_signed())
    }

    #[inline]
    pub fn u32(&mut self) -> Result<u32> {
        let b = self.array()?;
        Ok(self.order.u32(b))
    }

    #[inline]
    pub fn i32(&mut self) -> Result<i32> {
        Ok(self.u32()?.cast_signed())
    }

    #[inline]
    pub fn u64(&mut self) -> Result<u64> {
        let b = self.array()?;
        Ok(self.order.u64(b))
    }

    #[inline]
    pub fn i64(&mut self) -> Result<i64> {
        Ok(self.u64()?.cast_signed())
    }

    #[inline]
    pub fn f32(&mut self) -> Result<f32> {
        self.u32().map(f32::from_bits)
    }

    #[inline]
    pub fn f64(&mut self) -> Result<f64> {
        self.u64().map(f64::from_bits)
    }

    #[inline]
    pub fn vec2(&mut self) -> Result<[f32; 2]> {
        let b = self.array::<8>()?;
        Ok(floats(self.order, b))
    }

    #[inline]
    pub fn vec3(&mut self) -> Result<[f32; 3]> {
        let b = self.array::<12>()?;
        Ok(floats(self.order, b))
    }

    #[inline]
    pub fn vec4(&mut self) -> Result<[f32; 4]> {
        let b = self.array::<16>()?;
        Ok(floats(self.order, b))
    }

    /// A count of items of `size` bytes each that must fit in what's left: refused otherwise,
    /// so nothing is allocated for a count the file can't hold.
    pub fn count(&self, n: u64, size: usize) -> Result<usize> {
        self.fits(crate::num::usize_of(n), size)
    }

    /// `n`, when `n` items of `size` bytes each fit in what's left.
    #[inline]
    fn fits(&self, n: usize, size: usize) -> Result<usize> {
        match n.checked_mul(size.max(1)) {
            Some(b) if b <= self.left() => Ok(n),
            _ => bail!(
                Truncated,
                "{} file is cut short: it claims {n} items, more than it holds",
                self.what
            ),
        }
    }

    /// The bytes of `n` items of `size` bytes each, checked as [`count`](Self::count) checks
    /// them.
    #[inline]
    pub fn run(&mut self, n: usize, size: usize) -> Result<&'a [u8]> {
        let n = self.fits(n, size)?;
        self.bytes(n * size)
    }

    bulk! {
        /// `n` bytes as signed values.
        i8s -> i8, 1, |_o, c| c[0].cast_signed();
        /// `n` 16-bit values.
        u16s -> u16, 2, |o, c| o.u16(c);
        /// `n` signed 16-bit values.
        i16s -> i16, 2, |o, c| o.u16(c).cast_signed();
        /// `n` 32-bit values.
        u32s -> u32, 4, |o, c| o.u32(c);
        /// `n` signed 32-bit values.
        i32s -> i32, 4, |o, c| o.u32(c).cast_signed();
        /// `n` floats.
        f32s -> f32, 4, |o, c| f32::from_bits(o.u32(c));
        /// `n` doubles.
        f64s -> f64, 8, |o, c| f64::from_bits(o.u64(c));
        /// `n` pairs of floats.
        vec2s -> [f32; 2], 8, |o, c| {
            let (w, _) = c.as_chunks::<4>();
            [0, 1].map(|k| f32::from_bits(o.u32(w[k])))
        };
        /// `n` vectors of three floats.
        vec3s -> [f32; 3], 12, |o, c| {
            let (w, _) = c.as_chunks::<4>();
            [0, 1, 2].map(|k| f32::from_bits(o.u32(w[k])))
        };
        /// `n` vectors of four floats.
        vec4s -> [f32; 4], 16, |o, c| {
            let (w, _) = c.as_chunks::<4>();
            [0, 1, 2, 3].map(|k| f32::from_bits(o.u32(w[k])))
        };
    }

    /// `n` indices of `size` bytes each (1, 2 or 4), widened to 32 bits.
    pub fn indices(&mut self, n: usize, size: usize) -> Result<Vec<u32>> {
        Ok(match size {
            1 => self.run(n, 1)?.iter().map(|&b| u32::from(b)).collect(),
            2 => {
                let o = self.order;
                let b = self.run(n, 2)?;
                b.as_chunks::<2>().0.iter().map(|&c| u32::from(o.u16(c))).collect()
            }
            _ => self.u32s(n)?,
        })
    }

    /// A fixed-size string field: up to its first NUL.
    #[inline]
    pub fn fixed_str(&mut self, n: usize) -> Result<&'a [u8]> {
        self.bytes(n).map(until_nul)
    }

    /// A NUL-terminated string, without its NUL; one the file ends first is refused.
    pub fn cstr(&mut self) -> Result<&'a [u8]> {
        let rest = self.rest();
        match rest.iter().position(|&c| c == 0) {
            Some(end) => {
                self.at += end + 1;
                Ok(&rest[..end])
            }
            None => Err(self.cut_short()),
        }
    }

    /// The bytes up to `stop` (or the end), moving past `stop` too.
    pub fn until(&mut self, stop: u8) -> &'a [u8] {
        let rest = self.rest();
        let end = rest.iter().position(|&c| c == stop);
        self.at += end.map_or(rest.len(), |e| e + 1);
        &rest[..end.unwrap_or(rest.len())]
    }

    /// The `u16` at `at` from the start, in this reader's order, without moving; `None` past
    /// the end.
    #[inline]
    pub fn u16_at(&self, at: usize) -> Option<u16> {
        array_at(self.data, at).map(|b| self.order.u16(b))
    }

    /// The `u32` at `at` from the start, in this reader's order, without moving; `None` past
    /// the end.
    #[inline]
    pub fn u32_at(&self, at: usize) -> Option<u32> {
        array_at(self.data, at).map(|b| self.order.u32(b))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ErrorKind;

    #[test]
    fn reads_in_either_order() {
        let d = [1, 2, 3, 4, 5, 6, 7, 8];
        let mut le = Bin::new(&d, "test");
        assert_eq!(le.u16().unwrap(), 0x0201);
        assert_eq!(le.u32().unwrap(), 0x0605_0403);
        let mut be = Bin::be(&d, "test");
        assert_eq!(be.u16().unwrap(), 0x0102);
        assert_eq!(be.u32().unwrap(), 0x0304_0506);
        assert_eq!(be.u16().unwrap(), 0x0708);
        let mut e = Bin::with_order(&d, "test", Endian { big: true });
        assert_eq!(e.u64().unwrap(), 0x0102_0304_0506_0708);
        let mut e = Bin::with_order(&d, "test", Endian { big: false });
        assert_eq!(e.u64().unwrap(), 0x0807_0605_0403_0201);
        assert_eq!(Bin::be(&d, "t").u32s(2).unwrap(), [0x0102_0304, 0x0506_0708]);
        assert_eq!(Bin::new(&d, "t").u16s(4).unwrap(), [0x0201, 0x0403, 0x0605, 0x0807]);
        assert_eq!(Bin::new(&[0xff, 0x80], "t").i8s(2).unwrap(), [-1, -128]);
    }

    #[test]
    fn floats_and_vectors() {
        let mut d = Vec::new();
        for v in [1.0f32, -2.0, 0.5, 8.0] {
            d.extend(v.to_be_bytes());
        }
        assert_eq!(Bin::be(&d, "t").vec4().unwrap(), [1.0, -2.0, 0.5, 8.0]);
        assert_eq!(Bin::be(&d, "t").vec2s(2).unwrap(), [[1.0, -2.0], [0.5, 8.0]]);
        let mut b = Bin::be(&d, "t");
        assert_eq!(b.vec3s(1).unwrap(), [[1.0, -2.0, 0.5]]);
        assert_eq!(b.f32().unwrap(), 8.0);
        let le: Vec<u8> = [3.0f64, -1.5].iter().flat_map(|v| v.to_le_bytes()).collect();
        assert_eq!(Bin::new(&le, "t").f64s(2).unwrap(), [3.0, -1.5]);
        assert_eq!(f64_at(&le, 8), Some(-1.5));
        assert_eq!(f64_at(&le, 9), None);
    }

    #[test]
    fn a_short_read_is_truncated_and_moves_nothing() {
        let mut b = Bin::new(&[1, 2, 3], "XYZ");
        assert_eq!(b.u16().unwrap(), 0x0201);
        let e = b.u32().unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Truncated);
        assert_eq!(e.to_string(), "XYZ file is cut short");
        assert_eq!(b.pos(), 2);
        assert_eq!(b.u8().unwrap(), 3);
        assert_eq!(b.left(), 0);
        assert!(b.u8().is_err());
        assert!(b.skip(usize::MAX).is_err());
        assert_eq!(b.seek(4).unwrap_err().kind(), ErrorKind::Truncated);
        b.seek(1).unwrap();
        assert_eq!(b.rest(), [2, 3]);
    }

    #[test]
    fn a_huge_count_in_a_tiny_file_fails_before_allocating() {
        let d = [0u8; 16];
        for n in [9, 1 << 40, usize::MAX / 2, usize::MAX] {
            let mut b = Bin::new(&d, "T");
            assert_eq!(b.f32s(n).unwrap_err().kind(), ErrorKind::Truncated);
            assert_eq!(b.vec3s(n).unwrap_err().kind(), ErrorKind::Truncated);
            assert_eq!(b.u32s(n).unwrap_err().kind(), ErrorKind::Truncated);
            assert_eq!(b.indices(n, 2).unwrap_err().kind(), ErrorKind::Truncated);
            assert_eq!(b.pos(), 0);
        }
        assert_eq!(
            Bin::new(&d, "T").count(u64::MAX, 4).unwrap_err().kind(),
            ErrorKind::Truncated
        );
        assert_eq!(Bin::new(&d, "T").count(4, 4).unwrap(), 4);
        assert_eq!(Bin::new(&d, "T").f32s(4).unwrap().len(), 4);
        assert_eq!(Bin::new(&d, "T").f32s(0).unwrap(), Vec::<f32>::new());
    }

    #[test]
    fn sub_readers_end_with_their_chunk() {
        let d = [1, 0, 2, 0, 3, 0];
        let mut b = Bin::new(&d, "T");
        let mut c = b.sub(4).unwrap();
        assert_eq!(b.pos(), 4);
        assert_eq!(c.u16().unwrap(), 1);
        assert_eq!(c.u16().unwrap(), 2);
        assert!(c.u16().is_err());
        assert_eq!(b.u16().unwrap(), 3);
        assert!(b.sub(1).is_err());
    }

    #[test]
    fn strings() {
        let d = b"ab\0cd\0\0\0ef";
        let mut b = Bin::new(d, "T");
        assert_eq!(b.cstr().unwrap(), b"ab");
        assert_eq!(b.fixed_str(5).unwrap(), b"cd");
        assert_eq!(b.until(b'\n'), b"ef");
        assert_eq!(b.left(), 0);
        assert!(Bin::new(b"no end", "T").cstr().is_err());
        assert_eq!(until_nul(b"x\0y"), b"x");
    }

    #[test]
    fn reads_at_offsets_stop_at_the_end() {
        let d = [1, 0, 0, 0];
        assert_eq!(u32_at(&d, 0), Some(1));
        assert_eq!(u16_at(&d, 2), Some(0));
        assert_eq!(u16_at(&d, 3), None);
        assert_eq!(u32_at(&d, usize::MAX), None);
        assert_eq!(u64_at(&d, 0), None);
        assert_eq!(Bin::be(&d, "T").u16_at(0), Some(0x0100));
        assert_eq!(Bin::be(&d, "T").u32_at(1), None);
    }
}