xisf-core 0.5.2

Core engine for reading and writing XISF (Extensible Image Serialization Format) files
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Distributed XISF: a header file plus separate data blocks files.
//!
//! A monolithic `.xisf` carries its header and its blocks in one file. A
//! *distributed* unit splits them:
//!
//! - an **XISF header file** (`.xish`) holding only the XML header, with no
//!   signature or preamble -- the header is the whole file;
//! - zero or more **XISF data blocks files** (`.xisb`), which the header
//!   addresses with `location="path:..."` or `location="url:..."`.
//!
//! A blocks file is not a flat run of bytes. It is:
//!
//! ```text
//! "XISB0100"          8 bytes, signature
//! reserved            8 bytes, zero
//! block index         a singly linked list of nodes
//! [data blocks]       at the positions the index gives
//! [unused space]      blocks may sit anywhere, so there may be gaps
//! ```
//!
//! The index being a *linked list* rather than a table is the part worth
//! knowing: nodes can be appended anywhere in the file, so a writer can add
//! blocks to an existing file without rewriting it. It also means a reader
//! must not trust it blindly -- a cycle in the `next` pointers would loop
//! forever, and a corrupt file is exactly where that shows up, so the walk
//! below is bounded and refuses to revisit a node.

use alloc::collections::BTreeSet;

use crate::err;
use crate::error::Result;

/// The eight bytes a data blocks file starts with. Note `XISB`, not `XISF`.
pub const BLOCKS_SIGNATURE: &[u8; 8] = b"XISB0100";

/// Signature and reserved field, before the first index node.
pub const BLOCKS_PREAMBLE_LEN: usize = 16;

/// A block index element: 40 bytes describing one block.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct IndexElement {
    /// Unique within the file, and how the header names this block.
    pub id: u64,
    /// Byte position from the start of the file. Zero means a *free* element:
    /// a placeholder for a block not yet written.
    pub position: u64,
    /// Length as stored. Zero for a free element.
    pub length: u64,
    /// Length after decompression, or zero when the block is not compressed
    /// -- and also zero for a free element, so it is not a reliable signal on
    /// its own.
    pub uncompressed_length: u64,
}

impl IndexElement {
    /// Serialised size, fixed by the specification.
    pub const SIZE: usize = 40;

    /// Whether this is a placeholder rather than a real block.
    pub fn is_free(&self) -> bool {
        self.position == 0
    }

    fn parse(bytes: &[u8]) -> Self {
        let read = |offset: usize| {
            u64::from_le_bytes(bytes[offset..offset + 8].try_into().expect("checked length"))
        };
        IndexElement {
            id: read(0),
            position: read(8),
            length: read(16),
            uncompressed_length: read(24),
            // Bytes 32..40 are reserved and must be zero; nothing reads them.
        }
    }

    fn write(&self, out: &mut Vec<u8>) {
        out.extend_from_slice(&self.id.to_le_bytes());
        out.extend_from_slice(&self.position.to_le_bytes());
        out.extend_from_slice(&self.length.to_le_bytes());
        out.extend_from_slice(&self.uncompressed_length.to_le_bytes());
        out.extend_from_slice(&[0u8; 8]);
    }
}

/// A parsed data blocks file: its index, flattened.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct BlocksIndex {
    /// Every element from every node, in the order the list walks them.
    pub elements: Vec<IndexElement>,
}

impl BlocksIndex {
    /// The element with a given identifier, if the file has one.
    pub fn get(&self, id: u64) -> Option<&IndexElement> {
        self.elements.iter().find(|e| e.id == id)
    }

    /// Elements that actually point at data, skipping free placeholders.
    pub fn occupied(&self) -> impl Iterator<Item = &IndexElement> {
        self.elements.iter().filter(|e| !e.is_free())
    }
}

/// How many index nodes a walk will follow before giving up.
///
/// The list is caller-controlled data, so a bound is not optional. A file with
/// this many nodes is already unreasonable, and one with more is either
/// hostile or broken.
const MAX_NODES: usize = 100_000;

/// Read a data blocks file's index.
pub fn parse_blocks_file(bytes: &[u8]) -> Result<BlocksIndex> {
    if bytes.len() < BLOCKS_PREAMBLE_LEN {
        return Err(err!(
            Truncated,
            "a data blocks file needs at least {BLOCKS_PREAMBLE_LEN} bytes, this has {}",
            bytes.len()
        ));
    }
    if &bytes[..8] != BLOCKS_SIGNATURE {
        return Err(err!(
            NotXisf,
            "expected the signature {:?}, found {:?}",
            String::from_utf8_lossy(BLOCKS_SIGNATURE),
            String::from_utf8_lossy(&bytes[..8])
        ));
    }
    if bytes[8..16] != [0; 8] {
        return Err(err!(BadHeader, "the reserved field must be zero"));
    }

    // Nodes may not repeat a position, but nothing stops them overlapping,
    // so a node's declared elements can be counted again by the next node
    // sixteen bytes along. Bounding each node against the end of the file is
    // therefore not enough: a hundred thousand overlapping nodes multiply
    // into an index far larger than the file describing it. Every element
    // occupies forty bytes on disk, so no honest file can describe more
    // blocks than it has room for, and that is the bound.
    let max_elements = bytes.len() / IndexElement::SIZE;

    let mut elements = Vec::new();
    // A set rather than a list: this is searched once per node, so a linear
    // scan makes the walk quadratic and hands back the cost the MAX_NODES
    // bound was meant to cap -- a hundred thousand chained nodes fit in one
    // and a half megabytes and took the better part of a second to refuse.
    let mut seen: BTreeSet<u64> = BTreeSet::new();
    let mut position = BLOCKS_PREAMBLE_LEN as u64;

    for _ in 0..MAX_NODES {
        // A node position of zero ends the list.
        if position == 0 {
            return Ok(BlocksIndex { elements });
        }
        // A cycle would otherwise loop until the bound, which is a slow way
        // to say "corrupt". Naming it is better than timing out.
        if !seen.insert(position) {
            return Err(err!(BadHeader, "the block index loops back to position {position}"));
        }

        let start = usize::try_from(position)
            .map_err(|_| err!(Truncated, "an index node lies beyond this platform's range"))?;
        // Length, reserved, next: 16 bytes of node header.
        let header_end = start
            .checked_add(16)
            .ok_or_else(|| err!(Truncated, "an index node position overflows"))?;
        if header_end > bytes.len() {
            return Err(err!(Truncated, "an index node at {start} runs past the end of the file"));
        }

        let count = u32::from_le_bytes(bytes[start..start + 4].try_into().expect("4 bytes"));
        if bytes[start + 4..start + 8] != [0; 4] {
            return Err(err!(BadHeader, "an index node's reserved field is not zero"));
        }
        let next = u64::from_le_bytes(bytes[start + 8..start + 16].try_into().expect("8 bytes"));

        let count = count as usize;
        let elements_end = count
            .checked_mul(IndexElement::SIZE)
            .and_then(|n| header_end.checked_add(n))
            .ok_or_else(|| err!(Truncated, "an index node declares too many elements"))?;
        if elements_end > bytes.len() {
            return Err(err!(
                Truncated,
                "an index node at {start} declares {count} elements, which run past the end"
            ));
        }

        if elements.len() + count > max_elements {
            return Err(err!(
                BadHeader,
                "the block index declares more than {max_elements} elements, \
                 which is more than a file of {} bytes can hold",
                bytes.len()
            ));
        }
        for i in 0..count {
            let at = header_end + i * IndexElement::SIZE;
            elements.push(IndexElement::parse(&bytes[at..at + IndexElement::SIZE]));
        }
        position = next;
    }

    Err(err!(BadHeader, "the block index has more than {MAX_NODES} nodes"))
}

/// Read one block out of a data blocks file, without reading the whole file.
///
/// A distributed unit exists so that bulk data lives outside the header, and
/// such a file can be far larger than the block wanted from it. Reading it
/// whole to take one image out of it defeats the arrangement, and turns a
/// two-hundred-byte header into a demand for however many gigabytes happen to
/// sit beside it. The index is walked by seeking, and only the block itself
/// is read.
pub fn read_block_from<R: std::io::Read + std::io::Seek>(
    source: &mut R,
    file_len: u64,
    id: u64,
) -> Result<Option<Vec<u8>>> {
    use std::io::SeekFrom;

    let mut preamble = [0u8; BLOCKS_PREAMBLE_LEN];
    source.seek(SeekFrom::Start(0))?;
    source.read_exact(&mut preamble).map_err(|_| {
        err!(Truncated, "a data blocks file needs at least {BLOCKS_PREAMBLE_LEN} bytes")
    })?;
    if &preamble[..8] != BLOCKS_SIGNATURE {
        return Err(err!(NotXisf, "not a data blocks file"));
    }
    if preamble[8..16] != [0; 8] {
        return Err(err!(BadHeader, "the reserved field must be zero"));
    }

    // The same bounds as the in-memory walk, for the same reasons: a cycle
    // loops forever, and overlapping nodes multiply into an index larger than
    // the file describing it.
    let max_elements = file_len / IndexElement::SIZE as u64;
    let mut counted = 0u64;
    let mut seen: BTreeSet<u64> = BTreeSet::new();
    let mut position = BLOCKS_PREAMBLE_LEN as u64;

    for _ in 0..MAX_NODES {
        if position == 0 {
            return Ok(None);
        }
        if !seen.insert(position) {
            return Err(err!(BadHeader, "the block index loops back to position {position}"));
        }

        if position.checked_add(16).is_none_or(|end| end > file_len) {
            return Err(err!(Truncated, "an index node at {position} runs past the end"));
        }
        let mut node = [0u8; 16];
        source.seek(SeekFrom::Start(position))?;
        source.read_exact(&mut node)?;

        let count = u32::from_le_bytes(node[..4].try_into().expect("4 bytes")) as u64;
        if node[4..8] != [0; 4] {
            return Err(err!(BadHeader, "an index node's reserved field is not zero"));
        }
        let next = u64::from_le_bytes(node[8..16].try_into().expect("8 bytes"));

        let elements_end = count
            .checked_mul(IndexElement::SIZE as u64)
            .and_then(|n| n.checked_add(position + 16))
            .ok_or_else(|| err!(Truncated, "an index node declares too many elements"))?;
        if elements_end > file_len {
            return Err(err!(
                Truncated,
                "an index node at {position} declares {count} elements, which run past the end"
            ));
        }
        counted += count;
        if counted > max_elements {
            return Err(err!(
                BadHeader,
                "the block index declares more than {max_elements} elements, \
                 which is more than a file of {file_len} bytes can hold"
            ));
        }

        // Elements are read a node at a time rather than all at once, so a
        // large index costs one node's worth of memory rather than all of it.
        let mut buffer = vec![0u8; IndexElement::SIZE];
        for i in 0..count {
            source.seek(SeekFrom::Start(position + 16 + i * IndexElement::SIZE as u64))?;
            source.read_exact(&mut buffer)?;
            let element = IndexElement::parse(&buffer);
            if element.id != id || element.is_free() {
                continue;
            }

            let end = element
                .position
                .checked_add(element.length)
                .ok_or_else(|| err!(Truncated, "block {id}'s position and length overflow"))?;
            if end > file_len {
                return Err(err!(
                    Truncated,
                    "block {id} runs {} bytes past the end of the file",
                    end - file_len
                ));
            }
            let length = usize::try_from(element.length)
                .map_err(|_| err!(Unsupported, "block {id} is too large for this platform"))?;

            let mut data = vec![0u8; length];
            source.seek(SeekFrom::Start(element.position))?;
            source.read_exact(&mut data)?;
            return Ok(Some(data));
        }
        position = next;
    }

    Err(err!(BadHeader, "the block index has more than {MAX_NODES} nodes"))
}

/// Build a data blocks file from a set of blocks.
///
/// Writes a single index node listing every block, then the blocks in order.
/// The specification allows several nodes and gaps, both of which exist so a
/// file can be extended in place; a fresh file needs neither.
/// One block to place in a data blocks file.
///
/// The uncompressed length is carried alongside the bytes because the index
/// element records it: "If the data block pointed to by the block index
/// element has been compressed, the value of this item must be the length in
/// bytes of the uncompressed data block. For free block index elements and
/// index elements pointing to uncompressed blocks, the uncompressed block
/// length must be zero." A blocks file writer cannot work that out from the
/// stored bytes alone, so it has to be told.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PendingBlock {
    pub id: u64,
    pub bytes: Vec<u8>,
    /// The size before compression, or `None` for a block stored as it is.
    pub uncompressed_length: Option<u64>,
}

pub fn write_blocks_file(blocks: &[PendingBlock]) -> Result<Vec<u8>> {
    let node_size = 16 + blocks.len() * IndexElement::SIZE;
    let first_block = BLOCKS_PREAMBLE_LEN
        .checked_add(node_size)
        .ok_or_else(|| err!(Unsupported, "too many blocks for this platform"))?;

    let mut seen: BTreeSet<u64> = BTreeSet::new();
    let mut elements = Vec::with_capacity(blocks.len());
    let mut position = first_block as u64;
    for block in blocks {
        let id = block.id;
        // The identifier is how the header names a block, so a duplicate
        // makes the file ambiguous rather than merely odd.
        if !seen.insert(id) {
            return Err(err!(InvalidArgument, "two blocks share the identifier {id}"));
        }
        // A zero position marks a free element, so a block of no bytes would
        // be indistinguishable from a placeholder -- and two of them would
        // share a position, since the cursor would not advance. The only
        // zero-length block the format allows is an inline one, which never
        // reaches a blocks file.
        if block.bytes.is_empty() {
            return Err(err!(
                InvalidArgument,
                "block {id} is empty; a data blocks file holds blocks of one or more bytes"
            ));
        }

        elements.push(IndexElement {
            id,
            position,
            length: block.bytes.len() as u64,
            uncompressed_length: block.uncompressed_length.unwrap_or(0),
        });
        position += block.bytes.len() as u64;
    }

    let mut out =
        Vec::with_capacity(first_block + blocks.iter().map(|b| b.bytes.len()).sum::<usize>());
    out.extend_from_slice(BLOCKS_SIGNATURE);
    out.extend_from_slice(&[0u8; 8]);

    out.extend_from_slice(&(blocks.len() as u32).to_le_bytes());
    out.extend_from_slice(&[0u8; 4]);
    // One node, so there is no next.
    out.extend_from_slice(&0u64.to_le_bytes());
    for element in &elements {
        element.write(&mut out);
    }

    for block in blocks {
        out.extend_from_slice(&block.bytes);
    }
    Ok(out)
}

/// Read the header of a distributed unit, which is the whole `.xish` file.
///
/// Unlike a monolithic file there is no signature to check, so a wrong file
/// here fails as an XML error rather than a signature mismatch. The root
/// element check in [`crate::header::parse`] is what catches it.
pub fn parse_header_file(bytes: &[u8]) -> Result<crate::header::Header> {
    let text = core::str::from_utf8(bytes)
        .map_err(|e| err!(BadHeader, "an XISF header file must be UTF-8: {e}"))?;
    crate::header::parse(text)
}

/// A block stored as it is, for tests that do not care about compression.
#[cfg(test)]
fn plain_block(id: u64, bytes: &[u8]) -> PendingBlock {
    PendingBlock { id, bytes: bytes.to_vec(), uncompressed_length: None }
}

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

    fn blocks_file(blocks: &[(u64, &[u8])]) -> Vec<u8> {
        let owned: Vec<PendingBlock> = blocks
            .iter()
            .map(|(id, d)| PendingBlock { id: *id, bytes: d.to_vec(), uncompressed_length: None })
            .collect();
        write_blocks_file(&owned).expect("write")
    }

    /// "For free block index elements and index elements pointing to
    /// uncompressed blocks, the uncompressed block length must be zero" --
    /// and for a compressed one it must be the size before compression, which
    /// a writer cannot work out from the stored bytes and has to be told.
    #[test]
    fn an_index_element_records_the_size_before_compression() {
        let bytes = write_blocks_file(&[
            PendingBlock { id: 1, bytes: vec![0xAA; 40], uncompressed_length: Some(4096) },
            PendingBlock { id: 2, bytes: vec![0xBB; 40], uncompressed_length: None },
        ])
        .expect("write");
        let index = parse_blocks_file(&bytes).expect("parse");
        assert_eq!(index.get(1).unwrap().uncompressed_length, 4096, "compressed block");
        assert_eq!(index.get(2).unwrap().uncompressed_length, 0, "uncompressed block");
    }

    /// A zero position marks a free element, so a zero-length block would be
    /// indistinguishable from a placeholder -- and two of them would share a
    /// position, since the write cursor would not advance.
    #[test]
    fn an_empty_block_cannot_be_placed_in_a_blocks_file() {
        let err = write_blocks_file(&[plain_block(1, b"")]).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::InvalidArgument);
        assert!(err.message().contains("empty"), "{}", err.message());
    }

    #[test]
    fn a_blocks_file_round_trips() {
        let bytes = blocks_file(&[(1, b"first"), (7, b"second block"), (99, b"third")]);
        let index = parse_blocks_file(&bytes).expect("parse");

        assert_eq!(index.elements.len(), 3);
        assert_eq!(index.get(7).unwrap().length, 12);
        assert_eq!(index.get(99).unwrap().length, 5);
        assert!(index.get(1234).is_none());

        // And the positions must actually address the data.
        for (id, expected) in [(1u64, &b"first"[..]), (7, b"second block"), (99, b"third")] {
            let element = index.get(id).unwrap();
            let start = element.position as usize;
            let end = start + element.length as usize;
            assert_eq!(&bytes[start..end], expected, "block {id} is not where the index says");
        }
    }

    #[test]
    fn the_signature_is_xisb_not_xisf() {
        let mut bytes = blocks_file(&[(1, b"x")]);
        bytes[3] = b'F'; // XISB -> XISF
        assert_eq!(parse_blocks_file(&bytes).unwrap_err().kind(), ErrorKind::NotXisf);
    }

    #[test]
    fn free_elements_are_recognised_and_skipped() {
        // Built by hand: a free element has position and length zero.
        let mut bytes = Vec::new();
        bytes.extend_from_slice(BLOCKS_SIGNATURE);
        bytes.extend_from_slice(&[0u8; 8]);
        bytes.extend_from_slice(&2u32.to_le_bytes());
        bytes.extend_from_slice(&[0u8; 4]);
        bytes.extend_from_slice(&0u64.to_le_bytes());
        IndexElement { id: 1, position: 0, length: 0, uncompressed_length: 0 }.write(&mut bytes);
        let data_at = (16 + 16 + 2 * IndexElement::SIZE) as u64;
        IndexElement { id: 2, position: data_at, length: 3, uncompressed_length: 0 }
            .write(&mut bytes);
        bytes.extend_from_slice(b"abc");

        let index = parse_blocks_file(&bytes).expect("parse");
        assert_eq!(index.elements.len(), 2);
        assert!(index.get(1).unwrap().is_free());
        assert_eq!(index.occupied().count(), 1, "the free element should be skipped");
    }

    /// The index is a linked list of caller-controlled positions, so a cycle
    /// has to be caught rather than followed.
    #[test]
    fn a_cyclic_index_is_refused_rather_than_followed() {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(BLOCKS_SIGNATURE);
        bytes.extend_from_slice(&[0u8; 8]);
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&[0u8; 4]);
        // Next points back at this very node.
        bytes.extend_from_slice(&(BLOCKS_PREAMBLE_LEN as u64).to_le_bytes());

        let err = parse_blocks_file(&bytes).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::BadHeader);
        assert!(err.message().contains("loops"), "{}", err.message());
    }

    #[test]
    fn a_node_pointing_past_the_end_is_refused() {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(BLOCKS_SIGNATURE);
        bytes.extend_from_slice(&[0u8; 8]);
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&[0u8; 4]);
        bytes.extend_from_slice(&1_000_000u64.to_le_bytes());
        assert_eq!(parse_blocks_file(&bytes).unwrap_err().kind(), ErrorKind::Truncated);
    }

    /// A declared element count that runs past the file must not be trusted:
    /// it is the obvious way to make a reader read out of bounds.
    #[test]
    fn an_absurd_element_count_is_refused() {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(BLOCKS_SIGNATURE);
        bytes.extend_from_slice(&[0u8; 8]);
        bytes.extend_from_slice(&u32::MAX.to_le_bytes());
        bytes.extend_from_slice(&[0u8; 4]);
        bytes.extend_from_slice(&0u64.to_le_bytes());
        assert_eq!(parse_blocks_file(&bytes).unwrap_err().kind(), ErrorKind::Truncated);
    }

    #[test]
    fn truncation_at_every_length_is_an_error_not_a_panic() {
        let bytes = blocks_file(&[(1, b"first"), (2, b"second")]);
        for n in 0..bytes.len() {
            let _ = parse_blocks_file(&bytes[..n]);
        }
    }

    #[test]
    fn corruption_of_any_single_byte_never_panics() {
        let original = blocks_file(&[(1, b"first"), (2, b"second")]);
        for i in 0..original.len().min(200) {
            let mut bytes = original.clone();
            bytes[i] ^= 0xff;
            let _ = parse_blocks_file(&bytes);
        }
    }

    #[test]
    fn duplicate_identifiers_are_refused_when_writing() {
        let err = write_blocks_file(&[plain_block(1, b"a"), plain_block(1, b"b")]).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::InvalidArgument);
    }

    #[test]
    fn an_empty_blocks_file_is_valid() {
        let bytes = write_blocks_file(&[]).expect("write");
        let index = parse_blocks_file(&bytes).expect("parse");
        assert!(index.elements.is_empty());
    }

    #[test]
    fn a_header_file_is_the_header_alone() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<xisf version="1.0"><Image geometry="4:4:1" sampleFormat="UInt8"
      location="path(data.xisb)"/></xisf>"#;
        let header = parse_header_file(xml.as_bytes()).expect("parse");
        assert_eq!(header.images().len(), 1);

        // A monolithic file is not a header file: it starts with a signature,
        // which is not XML.
        let mut monolithic = Vec::from(crate::layout::SIGNATURE);
        monolithic.extend_from_slice(&[0u8; 8]);
        assert!(parse_header_file(&monolithic).is_err());
    }
}