sparse/
reader.rs

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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::{deserialize_from, Chunk, Reader, SparseHeader};
use anyhow::{ensure, Context, Result};
use byteorder::{ByteOrder as _, LE};
use std::io::{Read, Seek, SeekFrom};

/// SparseReader is an implementation of std::io::Read which transparently unpacks the underlying
/// sparse image as it is read.
/// If random access reads are not required, it is more performant to use `unsparse` to completely
/// unpack a sparse image.
pub struct SparseReader {
    reader: Box<dyn Reader + Send + Sync>,
    // Offset into the logical (unsparsed) image.
    offset: u64,
    // Size of the logical (unsparsed) image.
    size: u64,
    // The second field is the offset into `reader` at which the payload of the chunk appears, for
    // Raw chunks.
    chunks: Vec<(Chunk, Option<u64>)>,
}

impl SparseReader {
    /// Attempts to create a SparseReader from the given image.  Returns failure if the image is
    /// malformed.
    pub fn new(mut reader: Box<dyn Reader + Send + Sync>) -> Result<Self> {
        let header: SparseHeader =
            deserialize_from(&mut reader).context("Failed to read header")?;
        ensure!(header.valid(), "Invalid header");
        let num_chunks = header.total_chunks as usize;

        let mut chunks = vec![];
        let mut offset = 0;
        for _ in 0..num_chunks {
            let chunk = Chunk::read_metadata(&mut reader, offset, header.blk_sz)?;
            let data_offset = if chunk.chunk_type() == crate::format::CHUNK_TYPE_RAW {
                let data_offset = reader.stream_position()?;
                // Skip past the data payload
                reader.seek(SeekFrom::Current(chunk.output_size() as i64))?;
                Some(data_offset)
            } else {
                None
            };
            offset += chunk.output_size() as u64;
            chunks.push((chunk, data_offset));
        }

        reader.seek(SeekFrom::Start(0)).context("Failed to rewind reader")?;
        Ok(Self { reader, offset: 0, size: offset, chunks })
    }

    /// Returns the index of the current chunk in `self.chunks`.
    fn current_chunk(&self) -> Option<usize> {
        let mut off = 0;
        let mut i = 0;
        for (chunk, _) in &self.chunks {
            let size = chunk.output_size() as u64;
            if self.offset >= off && self.offset < off + size {
                return Some(i);
            }
            off += size;
            i += 1;
        }
        None
    }
}

// It's assumed that `reader` already points at the right offset to read from the chunk, and `buf`
// won't read past the end of the chunk.
// `output_offset` is the logical position in the output stream.
fn read_from_chunk<R: Reader>(
    reader: &mut R,
    chunk: &Chunk,
    output_offset: u64,
    buf: &mut [u8],
) -> std::io::Result<usize> {
    match chunk {
        Chunk::Raw { .. } => reader.read(buf),
        Chunk::Fill { value, .. } => {
            let mut value_bytes = value.to_le_bytes();
            value_bytes.rotate_left(output_offset as usize % std::mem::size_of::<u32>());
            let value_rotated = LE::read_u32(&value_bytes);
            // Safety: `std::slice::align_to_mut` requires that everything in the dst slice is a
            // valid type, which is true when going from [u8; 4] to [u32; 1].
            let (prefix, wholes, suffix) = unsafe { buf.align_to_mut::<u32>() };
            prefix.copy_from_slice(&value_bytes[value_bytes.len() - prefix.len()..]);
            wholes.fill(value_rotated);
            suffix.copy_from_slice(&value_bytes[..suffix.len()]);
            Ok(buf.len())
        }
        Chunk::DontCare { .. } => {
            buf.fill(0);
            Ok(buf.len())
        }
        _ => unreachable!(),
    }
}

impl Read for SparseReader {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let mut bytes_read = 0;
        while bytes_read < buf.len() {
            let current_chunk_idx = match self.current_chunk() {
                Some(i) => i,
                None => return Ok(bytes_read),
            };
            let (current_chunk, chunk_start_offset) = &self.chunks[current_chunk_idx];
            let offset_in_chunk = self.offset - current_chunk.output_offset().unwrap();
            debug_assert!(offset_in_chunk < current_chunk.output_size() as u64);
            let to_read = std::cmp::min(
                buf.len() - bytes_read,
                current_chunk.output_size() - offset_in_chunk as usize,
            );
            if let Some(offset) = chunk_start_offset {
                self.reader.seek(SeekFrom::Start(*offset + offset_in_chunk))?;
            }
            let bytes_read_from_chunk = read_from_chunk(
                &mut self.reader,
                current_chunk,
                self.offset,
                &mut buf[bytes_read..bytes_read + to_read],
            )?;
            bytes_read += bytes_read_from_chunk;
            self.offset += bytes_read_from_chunk as u64;
        }
        Ok(bytes_read)
    }
}

impl Seek for SparseReader {
    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
        self.offset = match pos {
            SeekFrom::Start(pos) => pos,
            SeekFrom::Current(delta) => self
                .offset
                .checked_add_signed(delta)
                .ok_or_else(|| std::io::Error::from(std::io::ErrorKind::InvalidInput))?,
            SeekFrom::End(delta) => self
                .size
                .checked_add_signed(delta)
                .ok_or_else(|| std::io::Error::from(std::io::ErrorKind::InvalidInput))?,
        };
        Ok(self.offset)
    }
}

#[cfg(test)]
mod test {
    use crate::builder::{DataSource, SparseImageBuilder};
    use crate::reader::SparseReader;
    use rand::rngs::SmallRng;
    use rand::{RngCore, SeedableRng};
    use std::io::{Read as _, Seek as _, SeekFrom, Write as _};
    use tempfile::{NamedTempFile, TempDir};

    #[test]
    fn empty_reader() {
        let tmpdir = TempDir::new().unwrap();

        let mut sparse_file = NamedTempFile::new_in(&tmpdir).unwrap().into_file();
        SparseImageBuilder::new().build(&mut sparse_file).expect("Build sparse image failed");
        sparse_file.seek(SeekFrom::Start(0)).unwrap();

        let mut reader =
            SparseReader::new(Box::new(sparse_file)).expect("Failed to create SparseReader");

        let mut unsparsed_bytes = vec![];
        reader.read_to_end(&mut unsparsed_bytes).expect("Failed to read unsparsed image");
        assert_eq!(unsparsed_bytes.len(), 0);
    }

    #[test]
    fn seek() {
        let tmpdir = TempDir::new().unwrap();

        let data = {
            let mut data = Box::new([0u8; 8192]);
            let mut i: u8 = 0;
            for d in data.as_mut() {
                *d = i;
                i = i.wrapping_add(1);
            }
            data
        };

        let mut sparse_file = NamedTempFile::new_in(&tmpdir).unwrap().into_file();
        SparseImageBuilder::new()
            .add_chunk(DataSource::Buffer(data))
            .build(&mut sparse_file)
            .expect("Build sparse image failed");
        sparse_file.seek(SeekFrom::Start(0)).unwrap();
        let mut reader =
            SparseReader::new(Box::new(sparse_file)).expect("Failed to create SparseReader");

        let mut buf = [0u8; 1];
        assert_eq!(0, reader.seek(SeekFrom::Start(0)).unwrap());
        assert_eq!(1, reader.read(&mut buf).unwrap());
        assert_eq!(buf[0], 0u8);

        assert_eq!(100, reader.seek(SeekFrom::Start(100)).unwrap());
        assert_eq!(1, reader.read(&mut buf).unwrap());
        assert_eq!(buf[0], 100u8);

        assert_eq!(99, reader.seek(SeekFrom::Current(-2)).unwrap());
        assert_eq!(1, reader.read(&mut buf).unwrap());
        assert_eq!(buf[0], 99u8);

        assert_eq!(100, reader.seek(SeekFrom::Current(0)).unwrap());
        assert_eq!(1, reader.read(&mut buf).unwrap());
        assert_eq!(buf[0], 100u8);

        assert_eq!(102, reader.seek(SeekFrom::Current(1)).unwrap());
        assert_eq!(1, reader.read(&mut buf).unwrap());
        assert_eq!(buf[0], 102u8);

        assert_eq!(8191, reader.seek(SeekFrom::End(-1)).unwrap());
        assert_eq!(1, reader.read(&mut buf).unwrap());
        assert_eq!(buf[0], 255u8);

        assert_eq!(8192, reader.seek(SeekFrom::End(0)).unwrap());
        assert_eq!(0, reader.read(&mut buf).unwrap());

        assert_eq!(8193, reader.seek(SeekFrom::End(1)).unwrap());
        assert_eq!(0, reader.read(&mut buf).unwrap());
    }

    #[test]
    fn read_past_eof() {
        let tmpdir = TempDir::new().unwrap();

        let mut sparse_file = NamedTempFile::new_in(&tmpdir).unwrap().into_file();
        SparseImageBuilder::new()
            .add_chunk(DataSource::Buffer(Box::new([0xffu8; 8192])))
            .build(&mut sparse_file)
            .expect("Build sparse image failed");
        sparse_file.seek(SeekFrom::Start(0)).unwrap();

        let mut reader =
            SparseReader::new(Box::new(sparse_file)).expect("Failed to create SparseReader");

        let mut buf = [0u8; 2];

        reader.seek(SeekFrom::Start(8191)).expect("Seek failed");
        assert_eq!(reader.read(&mut buf).expect("Failed to read"), 1);

        reader.seek(SeekFrom::Start(8192)).expect("Seek failed");
        assert_eq!(reader.read(&mut buf).expect("Failed to read"), 0);
    }

    #[test]
    fn full_read() {
        let tmpdir = TempDir::new().unwrap();

        // Generate a large temporary file
        let (mut file, _temp_path) = NamedTempFile::new_in(&tmpdir).unwrap().into_parts();
        let mut rng = SmallRng::from_entropy();
        let mut data = Vec::<u8>::new();
        data.resize(100 * 4096, 0);
        rng.fill_bytes(&mut data);
        file.write_all(&data).unwrap();
        file.flush().unwrap();
        file.seek(SeekFrom::Start(0)).unwrap();
        let content_size = data.len();

        let mut sparse_file = NamedTempFile::new_in(&tmpdir).unwrap().into_file();
        SparseImageBuilder::new()
            .add_chunk(DataSource::Buffer(Box::new([0xffu8; 8192])))
            .add_chunk(DataSource::Reader(Box::new(file)))
            .add_chunk(DataSource::Skip(16384))
            .add_chunk(DataSource::Fill(0xaaaa_aaaau32, 1024))
            .add_chunk(DataSource::Skip(4096))
            .build(&mut sparse_file)
            .expect("Build sparse image failed");
        sparse_file.seek(SeekFrom::Start(0)).unwrap();

        let mut reader =
            SparseReader::new(Box::new(sparse_file)).expect("Failed to create SparseReader");

        let mut unsparsed_bytes = vec![];
        reader.read_to_end(&mut unsparsed_bytes).expect("Failed to read unsparsed image");
        assert_eq!(unsparsed_bytes.len(), 8192 + content_size + 16384 + 4096 + 4096);
        assert_eq!(&unsparsed_bytes[..8192], &[0xffu8; 8192]);
        assert_eq!(&unsparsed_bytes[8192..8192 + content_size], &data[..]);
        assert_eq!(
            &unsparsed_bytes[8192 + content_size..8192 + content_size + 16384],
            &[0u8; 16384]
        );
        assert_eq!(
            &unsparsed_bytes[8192 + content_size + 16384..8192 + content_size + 16384 + 4096],
            &[0xaau8; 4096]
        );
        assert_eq!(&unsparsed_bytes[8192 + content_size + 16384 + 4096..], &[0u8; 4096]);
    }

    #[test]
    fn unaligned_reads() {
        let tmpdir = TempDir::new().unwrap();

        // Generate a large temporary file
        let (mut file, _temp_path) = NamedTempFile::new_in(&tmpdir).unwrap().into_parts();
        let mut rng = SmallRng::from_entropy();
        let mut data = Vec::<u8>::new();
        data.resize(100 * 4096, 0);
        rng.fill_bytes(&mut data);
        file.write_all(&data).unwrap();
        file.flush().unwrap();
        file.seek(SeekFrom::Start(0)).unwrap();
        let content_size = data.len();

        let mut sparse_file = NamedTempFile::new_in(&tmpdir).unwrap().into_file();
        SparseImageBuilder::new()
            .add_chunk(DataSource::Buffer(Box::new([0xffu8; 8192])))
            .add_chunk(DataSource::Reader(Box::new(file)))
            .add_chunk(DataSource::Skip(16384))
            .add_chunk(DataSource::Fill(0x0102_0304u32, 1024))
            .add_chunk(DataSource::Skip(4096))
            .build(&mut sparse_file)
            .expect("Build sparse image failed");
        sparse_file.seek(SeekFrom::Start(0)).unwrap();

        let mut reader =
            SparseReader::new(Box::new(sparse_file)).expect("Failed to create SparseReader");

        let mut buffer = [0u8; 4096];

        // Do an unaligned read from each section

        // DataSource::Buffer
        reader.seek(SeekFrom::Start(10)).expect("Failed to seek");
        let _ = reader.read(&mut buffer[..20]).expect("Failed to read");
        assert_eq!(&buffer[..20], &[0xffu8; 20]);

        // DataSource::File
        reader.seek(SeekFrom::Start(8192 + 4095)).expect("Failed to seek");
        let _ = reader.read(&mut buffer[..2]).expect("Failed to read");
        assert_eq!(&buffer[..2], &data[4095..4097]);

        // DataSource::Skip
        reader.seek(SeekFrom::Start(8192 + content_size as u64 + 4090)).expect("Failed to seek");
        let _ = reader.read(&mut buffer[..6]).expect("Failed to read");
        assert_eq!(&buffer[..6], &[0u8; 6]);

        // DataSource::Fill
        reader
            .seek(SeekFrom::Start(8192 + content_size as u64 + 16384 + 3))
            .expect("Failed to seek");
        let _ = reader.read(&mut buffer[..9]).expect("Failed to read");
        // Bear in mind the byte ordering is LE, so 0x01020304 == [0x04, 0x03, 0x02, 0x01]
        assert_eq!(&buffer[..9], &[0x01, 0x04, 0x03, 0x02, 0x01, 0x04, 0x03, 0x02, 0x01]);

        // DataSource::Skip
        reader
            .seek(SeekFrom::Start(8192 + content_size as u64 + 16384 + 4096 + 1))
            .expect("Failed to seek");
        let _ = reader.read(&mut buffer[..4095]).expect("Failed to read");
        assert_eq!(&buffer[..4095], &[0u8; 4095]);

        // Do an unaligned read spanning two sections (the last Fill and Skip)
        reader
            .seek(SeekFrom::Start(8192 + content_size as u64 + 16384 + 4090))
            .expect("Failed to seek");
        let _ = reader.read(&mut buffer[..9]).expect("Failed to read");
        assert_eq!(&buffer[..9], &[0x02, 0x01, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00]);
    }
}