riff-chunks 0.1.0

Borrowed and streaming readers plus a seekable writer for RIFF containers.
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
use core::fmt;
use core::iter::FusedIterator;

use crate::error::Failure;
use crate::{Error, Fourcc, Result};

/// Maximum number of list levels that a reader or writer can enter.
pub const MAX_DEPTH: usize = 32;

/// The container form and its byte order.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Form {
    /// The little-endian RIFF form.
    Riff,
    /// A big-endian RIFX container.
    Rifx,
    /// An RF64 container with 64-bit sizes in a `ds64` chunk.
    Rf64,
    /// A BW64 container with the RF64 layout.
    Bw64,
}

impl Form {
    /// Returns true for the big-endian RIFX form.
    pub const fn is_big_endian(self) -> bool {
        matches!(self, Self::Rifx)
    }

    /// Returns true when the form requires a leading `ds64` chunk.
    pub const fn has_ds64(self) -> bool {
        matches!(self, Self::Rf64 | Self::Bw64)
    }

    #[cfg(feature = "std")]
    pub(crate) const fn id(self) -> Fourcc {
        match self {
            Self::Riff => Fourcc::RIFF,
            Self::Rifx => Fourcc::RIFX,
            Self::Rf64 => Fourcc::RF64,
            Self::Bw64 => Fourcc::BW64,
        }
    }
}

/// The twelve-byte container header.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Header {
    /// Container form.
    pub form: Form,
    /// Declared size of the bytes after the size field.
    pub size: u32,
    /// Four-byte form type such as `WAVE`, `AVI `, or `WEBP`.
    pub form_type: Fourcc,
}

/// The resolved contents of an RF64 or BW64 size table.
#[derive(Clone, Copy, Debug)]
pub struct Ds64<'a> {
    /// Resolved container size.
    pub riff_size: u64,
    /// Resolved size for a `data` chunk with a sentinel size.
    pub data_size: u64,
    /// Informational sample count.
    pub sample_count: u64,
    pub(crate) table: &'a [u8],
}

impl<'a> Ds64<'a> {
    /// Iterates over the non-`data` size entries in file order.
    pub fn table(&self) -> impl Iterator<Item = (Fourcc, u64)> + 'a {
        self.table.chunks_exact(12).map(|entry| {
            let id = Fourcc([entry[0], entry[1], entry[2], entry[3]]);
            let size = read_u64_le(&entry[4..12]);
            (id, size)
        })
    }

    /// Resolves a sentinel size for `id`.
    pub fn size_of(&self, id: Fourcc) -> Option<u64> {
        if id == Fourcc(*b"data") {
            Some(self.data_size)
        } else {
            self.table()
                .find_map(|(entry_id, size)| (entry_id == id).then_some(size))
        }
    }
}

/// A resolved chunk header.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ChunkHeader {
    /// Chunk identifier.
    pub id: Fourcc,
    /// Resolved payload size without a pad byte.
    pub size: u64,
    /// Absolute offset of the eight-byte chunk header.
    pub offset: u64,
    /// List depth, with zero at the top level.
    pub depth: u8,
}

impl ChunkHeader {
    /// Returns true when this is a `LIST` chunk.
    pub fn is_list(&self) -> bool {
        self.id.0 == Fourcc::LIST.0
    }

    /// Returns the payload size including word-alignment padding.
    ///
    /// The result saturates only for `u64::MAX`, which cannot have a
    /// representable padded extent. Readers reject that size before slicing.
    pub const fn padded_size(&self) -> u64 {
        self.size.saturating_add(self.size & 1)
    }

    /// Returns the header, payload, and padding size.
    ///
    /// The result saturates when the mathematical extent exceeds `u64`.
    pub const fn extent(&self) -> u64 {
        8_u64.saturating_add(self.padded_size())
    }
}

/// A borrowed view of a complete container.
pub struct Riff<'a> {
    data: &'a [u8],
    header: Header,
    ds64: Option<Ds64<'a>>,
}

impl fmt::Debug for Riff<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Riff")
            .field("header", &self.header)
            .field("ds64", &self.ds64)
            .field("len", &self.data.len())
            .finish()
    }
}

impl<'a> Riff<'a> {
    /// Parses the file header and any required 64-bit size table.
    ///
    /// # Examples
    ///
    /// ```
    /// use riff_chunks::{Fourcc, Riff};
    ///
    /// let bytes = b"RIFF\x10\x00\x00\x00TESTdata\x04\x00\x00\x00abcd";
    /// let file = Riff::parse(bytes)?;
    /// let chunk = file.chunks().find(Fourcc::from_str("data"))?.unwrap();
    ///
    /// assert_eq!(chunk.payload, b"abcd");
    /// # Ok::<(), riff_chunks::Error>(())
    /// ```
    pub fn parse(data: &'a [u8]) -> Result<Self> {
        let header = parse_header(data)?;
        let ds64 = if header.form.has_ds64() {
            Some(parse_borrowed_ds64(data)?)
        } else {
            None
        };
        Ok(Self { data, header, ds64 })
    }

    /// Returns the parsed file header.
    pub const fn header(&self) -> Header {
        self.header
    }

    /// Returns the RF64 or BW64 size table when present.
    pub const fn ds64(&self) -> Option<Ds64<'a>> {
        self.ds64
    }

    /// Creates an iterator over top-level chunks.
    pub fn chunks(&self) -> Chunks<'a> {
        Chunks::new(
            &self.data[12..],
            12,
            0,
            self.header.form.is_big_endian(),
            self.ds64,
            false,
        )
    }

    /// Returns all bytes after the twelve-byte file header.
    pub fn body(&self) -> &'a [u8] {
        &self.data[12..]
    }
}

/// A chunk whose payload borrows from the input.
#[derive(Clone, Copy, Debug)]
pub struct Chunk<'a> {
    /// Resolved chunk header.
    pub header: ChunkHeader,
    /// Exact payload bytes without a pad byte.
    pub payload: &'a [u8],
    big_endian: bool,
    ds64: Option<Ds64<'a>>,
}

impl<'a> Chunk<'a> {
    /// Returns the first four payload bytes as the list type.
    pub fn list_type(&self) -> Result<Fourcc> {
        if !self.header.is_list() {
            return Err(Error::NotAList { id: self.header.id });
        }
        if self.payload.len() < 4 {
            return Err(Error::ListTooShort {
                id: self.header.id,
                size: self.header.size,
            });
        }
        Ok(Fourcc([
            self.payload[0],
            self.payload[1],
            self.payload[2],
            self.payload[3],
        ]))
    }

    /// Creates an iterator over this list's direct children.
    pub fn children(&self) -> Result<Chunks<'a>> {
        self.list_type()?;
        if usize::from(self.header.depth) >= MAX_DEPTH {
            return Err(Error::TooDeep { max: MAX_DEPTH });
        }
        Ok(Chunks::new(
            &self.payload[4..],
            self.header.offset.saturating_add(12),
            self.header.depth + 1,
            self.big_endian,
            self.ds64,
            true,
        ))
    }
}

#[derive(Clone, Copy, Debug)]
enum IterState {
    Running,
    Pending(Failure),
    Done,
}

/// An iterator over one chunk nesting level.
pub struct Chunks<'a> {
    data: &'a [u8],
    pos: usize,
    base: u64,
    depth: u8,
    big_endian: bool,
    ds64: Option<Ds64<'a>>,
    parent_bound: bool,
    state: IterState,
}

impl fmt::Debug for Chunks<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Chunks")
            .field("pos", &self.pos)
            .field("base", &self.base)
            .field("depth", &self.depth)
            .field("state", &self.state)
            .finish_non_exhaustive()
    }
}

impl<'a> Chunks<'a> {
    fn new(
        data: &'a [u8],
        base: u64,
        depth: u8,
        big_endian: bool,
        ds64: Option<Ds64<'a>>,
        parent_bound: bool,
    ) -> Self {
        Self {
            data,
            pos: 0,
            base,
            depth,
            big_endian,
            ds64,
            parent_bound,
            state: IterState::Running,
        }
    }

    /// Finds the next chunk with `id` at this nesting level.
    pub fn find(&mut self, id: Fourcc) -> Result<Option<Chunk<'a>>> {
        for item in self.by_ref() {
            let chunk = item?;
            if chunk.header.id == id {
                return Ok(Some(chunk));
            }
        }
        Ok(None)
    }

    fn fail(&mut self, failure: Failure) -> Option<Result<Chunk<'a>>> {
        self.state = IterState::Done;
        Some(Err(failure.into_error()))
    }
}

impl<'a> Iterator for Chunks<'a> {
    type Item = Result<Chunk<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.state {
            IterState::Done => return None,
            IterState::Pending(failure) => return self.fail(failure),
            IterState::Running => {}
        }

        let remaining = self.data.len().saturating_sub(self.pos);
        if remaining == 0 {
            self.state = IterState::Done;
            return None;
        }

        let offset = self
            .base
            .saturating_add(u64::try_from(self.pos).unwrap_or(u64::MAX));
        if remaining < 8 {
            return self.fail(Failure::TruncatedHeader(
                offset,
                u64::try_from(remaining).unwrap_or(u64::MAX),
            ));
        }

        let header_bytes = &self.data[self.pos..self.pos + 8];
        let id = Fourcc([
            header_bytes[0],
            header_bytes[1],
            header_bytes[2],
            header_bytes[3],
        ]);
        let size32 = read_u32(&header_bytes[4..8], self.big_endian);
        let size = match resolve_size(size32, id, self.ds64) {
            Ok(size) => size,
            Err(error) => {
                self.state = IterState::Done;
                return Some(Err(error));
            }
        };

        if id == Fourcc::LIST && size < 4 {
            return self.fail(Failure::ListTooShort(id, size));
        }

        let payload_end = match 8_u64.checked_add(size) {
            Some(end) => end,
            None => {
                return self.fail(Failure::Truncated(
                    id,
                    size,
                    u64::try_from(remaining - 8).unwrap_or(u64::MAX),
                ));
            }
        };
        let extent = match payload_end.checked_add(size & 1) {
            Some(end) => end,
            None => {
                return self.fail(Failure::Truncated(
                    id,
                    size,
                    u64::try_from(remaining - 8).unwrap_or(u64::MAX),
                ));
            }
        };
        let remaining_u64 = u64::try_from(remaining).unwrap_or(u64::MAX);

        if self.parent_bound && extent > remaining_u64 {
            return self.fail(Failure::ChildExceedsParent(id, size, remaining_u64));
        }
        if payload_end > remaining_u64 {
            return self.fail(Failure::Truncated(
                id,
                size,
                remaining_u64.saturating_sub(8),
            ));
        }

        let payload_size = match usize::try_from(size) {
            Ok(size) => size,
            Err(_) => {
                return self.fail(Failure::Truncated(
                    id,
                    size,
                    remaining_u64.saturating_sub(8),
                ));
            }
        };
        let payload_start = self.pos + 8;
        let payload_stop = match payload_start.checked_add(payload_size) {
            Some(stop) => stop,
            None => {
                return self.fail(Failure::Truncated(
                    id,
                    size,
                    remaining_u64.saturating_sub(8),
                ));
            }
        };
        let payload = &self.data[payload_start..payload_stop];
        let missing_pad = extent > remaining_u64;
        self.pos = if missing_pad {
            payload_stop
        } else {
            match usize::try_from(extent) {
                Ok(extent) => self.pos + extent,
                Err(_) => self.data.len(),
            }
        };

        if missing_pad {
            self.state = IterState::Pending(Failure::MissingPad(
                id,
                offset.saturating_add(8).saturating_add(size),
            ));
        }

        Some(Ok(Chunk {
            header: ChunkHeader {
                id,
                size,
                offset,
                depth: self.depth,
            },
            payload,
            big_endian: self.big_endian,
            ds64: self.ds64,
        }))
    }
}

impl FusedIterator for Chunks<'_> {}

pub(crate) fn parse_header(data: &[u8]) -> Result<Header> {
    if data.len() < 12 {
        return Err(Error::TruncatedHeader {
            offset: 0,
            available: u64::try_from(data.len()).unwrap_or(u64::MAX),
        });
    }
    let found = [data[0], data[1], data[2], data[3]];
    let form = form_from_id(found)?;
    Ok(Header {
        form,
        size: read_u32(&data[4..8], form.is_big_endian()),
        form_type: Fourcc([data[8], data[9], data[10], data[11]]),
    })
}

pub(crate) fn form_from_id(found: [u8; 4]) -> Result<Form> {
    match Fourcc(found) {
        Fourcc::RIFF => Ok(Form::Riff),
        Fourcc::RIFX => Ok(Form::Rifx),
        Fourcc::RF64 => Ok(Form::Rf64),
        Fourcc::BW64 => Ok(Form::Bw64),
        _ => Err(Error::NotRiff { found }),
    }
}

pub(crate) fn read_u32(bytes: &[u8], big_endian: bool) -> u32 {
    let value = [bytes[0], bytes[1], bytes[2], bytes[3]];
    if big_endian {
        u32::from_be_bytes(value)
    } else {
        u32::from_le_bytes(value)
    }
}

pub(crate) fn read_u64_le(bytes: &[u8]) -> u64 {
    u64::from_le_bytes([
        bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
    ])
}

pub(crate) fn parse_ds64_payload(payload: &[u8], declared: u64) -> Result<Ds64<'_>> {
    if declared < 28 || payload.len() < 28 {
        return Err(Error::BadDs64 { size: declared });
    }
    let table_len = read_u32(&payload[24..28], false);
    let table_bytes = match u64::from(table_len).checked_mul(12) {
        Some(bytes) => bytes,
        None => return Err(Error::BadDs64 { size: declared }),
    };
    let required = match 28_u64.checked_add(table_bytes) {
        Some(required) => required,
        None => return Err(Error::BadDs64 { size: declared }),
    };
    if required > declared {
        return Err(Error::BadDs64 { size: declared });
    }
    let required_usize = match usize::try_from(required) {
        Ok(required) if required <= payload.len() => required,
        _ => return Err(Error::BadDs64 { size: declared }),
    };
    Ok(Ds64 {
        riff_size: read_u64_le(&payload[0..8]),
        data_size: read_u64_le(&payload[8..16]),
        sample_count: read_u64_le(&payload[16..24]),
        table: &payload[28..required_usize],
    })
}

fn parse_borrowed_ds64(data: &[u8]) -> Result<Ds64<'_>> {
    if data.len() < 20 {
        return Err(Error::TruncatedHeader {
            offset: 12,
            available: u64::try_from(data.len().saturating_sub(12)).unwrap_or(u64::MAX),
        });
    }
    let id = Fourcc([data[12], data[13], data[14], data[15]]);
    if id != Fourcc::DS64 {
        return Err(Error::MissingDs64 { found: id });
    }
    let size = u64::from(read_u32(&data[16..20], false));
    if size < 28 {
        return Err(Error::BadDs64 { size });
    }
    let available = data.len().saturating_sub(20);
    let size_usize = match usize::try_from(size) {
        Ok(size) if size <= available => size,
        _ => {
            return Err(Error::Truncated {
                id,
                declared: size,
                available: u64::try_from(available).unwrap_or(u64::MAX),
            });
        }
    };
    parse_ds64_payload(&data[20..20 + size_usize], size)
}

pub(crate) fn resolve_size(size32: u32, id: Fourcc, ds64: Option<Ds64<'_>>) -> Result<u64> {
    if size32 != u32::MAX {
        return Ok(u64::from(size32));
    }
    match ds64 {
        Some(ds64) => ds64.size_of(id).ok_or(Error::UnresolvedSize { id }),
        None => Ok(u64::from(size32)),
    }
}