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
//! `FileIndex` implementations

use std::collections::{BTreeMap, HashMap};
use std::io::{Read, Result, Seek, SeekFrom};
use std::iter::{self, Extend};
use std::ops::Range;

use cfg_if::cfg_if;

use super::{Entry, Section};

/// The storage used to store file indices.
///
/// If you open the phar only to view the stub, phar metadata, etc.,
/// use `index::NoIndex`.
///
/// To sequentially access all files in the archive,
/// use `index::Iterable` implementations.
/// If random file access is not required,
/// use `index::OffsetOnly`.
///
/// To access specific files only,
/// use `index::RandomAccess` implementations.
/// Prefer using `NameMap` if individual entry metadata is not required.
/// To also access their metadata,
/// use `MetadataMap`.
/// There are some type aliases for the respective HashMap/BTreeMap implementations.
pub trait FileIndex: Default {
    /// Whether file metadata should be scanned on loading.
    fn scan_files() -> bool {
        true
    }

    /// Whether `Entry` should force `name` to use `Section::Cached`.
    fn requires_name() -> bool {
        false
    }

    /// Whether `Entry` should force `metadata` to use `Section::Cached`.
    fn requires_metadata() -> bool {
        false
    }

    /// Adds an `Entry` to the index.
    fn feed_entry(&mut self, offset: u64, entry: Entry) -> Result<()>;

    /// Marks the end of header
    fn end_of_header(&mut self, _offset: u64) {}
}

/// A subfamily of file indices for iterable files.
///
/// The iteration order may not be the order in the phar archive,
/// and may not even be stable.
pub trait Iterable: FileIndex {
    /// Iterates over the files in this index.
    fn for_each_file<'t, R, F>(&self, read: R, f: F) -> Result<()>
    where
        R: Read + Seek + 't,
        F: FnMut(&[u8], &mut (dyn Read)) -> Result<()>,
    {
        self.for_each_file_fold(read, f, |_, ()| ()).map(|_| ())
    }

    /// Iterates over the files in this index and fold return values.
    fn for_each_file_fold<'t, R, F, G, T, U>(&self, read: R, f: F, fold: G) -> Result<Option<T>>
    where
        R: Read + Seek + 't,
        F: FnMut(&[u8], &mut (dyn Read)) -> Result<U>,
        G: FnMut(Option<T>, U) -> T;
}

/// A subfamily of file indices for random access of files by name.
pub trait RandomAccess: FileIndex {
    /// Returns the file contents range of the file of the required name.
    ///
    /// Returns `None` if there are no files with the specified name.
    fn read_file(&self, name: &[u8]) -> Option<Range<u64>>;
}

/// Indicates that the phar should not index phar files at all.
///
/// This should only be used if phar files are not going to be accessed,
/// or allocating `O(num_files)` memory is considered a security vulnerability.
#[derive(Debug, Default)]
pub struct NoIndex(());

impl FileIndex for NoIndex {
    fn scan_files() -> bool {
        false
    }

    fn feed_entry(&mut self, _: u64, _: Entry) -> Result<()> {
        unreachable!()
    }
}

/// Stores files by position.
///
/// Uses only `O(nm)` memory,
/// where `n` is the number of files,
/// and `m` is either `1` or the length of filenames
/// depending on whether files are cached.
#[derive(Debug, Default)]
pub struct OffsetOnly {
    content_offset: u64,
    entries: Vec<OffsetOnlyEntry>,
}

#[derive(Debug)]
struct OffsetOnlyEntry {
    name: Section,
    flags: u32,
    end_offset_from_co: u64,
}

impl FileIndex for OffsetOnly {
    fn feed_entry(&mut self, _: u64, entry: Entry) -> Result<()> {
        let prev = match self.entries.last() {
            Some(ooe) => ooe.end_offset_from_co,
            None => 0,
        };
        let size: u64 = entry.compressed_file_size.into();
        self.entries.push(OffsetOnlyEntry {
            name: entry.name,
            flags: entry.flags,
            end_offset_from_co: prev + size,
        });
        Ok(())
    }

    fn end_of_header(&mut self, offset: u64) {
        self.content_offset = offset;
    }
}

impl Iterable for OffsetOnly {
    fn for_each_file_fold<'t, R, F, G, T, U>(
        &self,
        mut read: R,
        mut f: F,
        mut fold: G,
    ) -> Result<Option<T>>
    where
        R: Read + Seek + 't,
        F: FnMut(&[u8], &mut (dyn Read)) -> Result<U>,
        G: FnMut(Option<T>, U) -> T,
    {
        let mut start_offset = self.content_offset;
        let mut reduced = None;

        for OffsetOnlyEntry {
            name,
            flags,
            end_offset_from_co,
        } in &self.entries
        {
            let name = name.as_memory(&mut read)?;
            let name = name.as_ref();
            let end_offset = *end_offset_from_co + self.content_offset;

            let _ = read.seek(SeekFrom::Start(start_offset))?;
            let mut decompressed =
                adapted_reader(*flags, (&mut read).take(end_offset - start_offset))?;
            let mapped = f(name, &mut decompressed)?;
            reduced = Some(fold(reduced, mapped));

            start_offset = end_offset;
        }

        Ok(reduced)
    }
}

/// Indexes files by name for random access.
#[derive(Debug, Default)]
pub struct NameMap<M> {
    map: M,
    last_offset: u64,
    content_offset: u64,
}

impl<M: Default + Extend<(Vec<u8>, (u32, Range<u64>))>> FileIndex for NameMap<M>
where
    for<'t> &'t M: IntoIterator<Item = (&'t Vec<u8>, &'t (u32, Range<u64>))>,
{
    fn requires_name() -> bool {
        true
    }

    fn end_of_header(&mut self, offset: u64) {
        self.content_offset = offset;
    }

    fn feed_entry(&mut self, _: u64, entry: Entry) -> Result<()> {
        let len: u64 = entry.compressed_file_size.into();

        let name = match entry.name {
            Section::Cached(cache) => cache,
            _ => unreachable!("requires_name is set to true"),
        };
        let start = self.last_offset;
        let end = start + len;
        self.last_offset = end;
        self.map
            .extend(iter::once((name, (entry.flags, start..end))));
        Ok(())
    }
}

impl<M: Default + Extend<(Vec<u8>, (u32, Range<u64>))>> Iterable for NameMap<M>
where
    for<'t> &'t M: IntoIterator<Item = (&'t Vec<u8>, &'t (u32, Range<u64>))>,
{
    fn for_each_file_fold<'t, R, F, G, T, U>(
        &self,
        mut read: R,
        mut f: F,
        mut fold: G,
    ) -> Result<Option<T>>
    where
        R: Read + Seek + 't,
        F: FnMut(&[u8], &mut (dyn Read)) -> Result<U>,
        G: FnMut(Option<T>, U) -> T,
    {
        let mut reduced = None;

        for (name, (flags, Range { start, end })) in &self.map {
            let _ = read.seek(SeekFrom::Start(*start + self.content_offset))?;
            let mut decompressed = adapted_reader(*flags, (&mut read).take(end - start))?;
            let mapped = f(name, &mut decompressed)?;
            reduced = Some(fold(reduced, mapped));
        }

        Ok(reduced)
    }
}

/// Indexes files by name with a HashMap.
pub type NameHashMap = NameMap<HashMap<Vec<u8>, (u32, Range<u64>)>>;
/// Indexes files by name with a BTreeMap.
pub type NameBTreeMap = NameMap<BTreeMap<Vec<u8>, (u32, Range<u64>)>>;

/// Indexes files by name for random access, and stores file metadata.
#[derive(Debug, Default)]
pub struct MetadataMap<M> {
    pub(crate) map: M,
    last_offset: u64,
    content_offset: u64,
}

impl<M: Default + Extend<(Vec<u8>, (Entry, Range<u64>))>> FileIndex for MetadataMap<M>
where
    for<'t> &'t M: IntoIterator<Item = (&'t Vec<u8>, &'t (Entry, Range<u64>))>,
{
    fn requires_name() -> bool {
        true
    }

    fn requires_metadata() -> bool {
        true
    }

    fn end_of_header(&mut self, offset: u64) {
        self.content_offset = offset;
    }

    fn feed_entry(&mut self, _: u64, entry: Entry) -> Result<()> {
        let name = match &entry.name {
            Section::Cached(cache) => cache,
            _ => unreachable!("requires_name is set to true"),
        };
        let start = self.last_offset;
        let len: u64 = entry.compressed_file_size.into();
        let end = start + len;
        self.last_offset = end;
        self.map
            .extend(iter::once((name.clone(), (entry, start..end))));
        Ok(())
    }
}

impl<M: Default + Extend<(Vec<u8>, (Entry, Range<u64>))>> Iterable for MetadataMap<M>
where
    for<'t> &'t M: IntoIterator<Item = (&'t Vec<u8>, &'t (Entry, Range<u64>))>,
{
    fn for_each_file_fold<'t, R, F, G, T, U>(
        &self,
        mut read: R,
        mut f: F,
        mut fold: G,
    ) -> Result<Option<T>>
    where
        R: Read + Seek + 't,
        F: FnMut(&[u8], &mut (dyn Read)) -> Result<U>,
        G: FnMut(Option<T>, U) -> T,
    {
        let mut reduced = None;

        for (name, (entry, Range { start, end })) in &self.map {
            let _ = read.seek(SeekFrom::Start(*start + self.content_offset))?;
            let mut decompressed = adapted_reader(entry.flags, (&mut read).take(end - start))?;
            let mapped = f(name, &mut decompressed)?;
            reduced = Some(fold(reduced, mapped));
        }

        Ok(reduced)
    }
}

/// Indexes files by name with a HashMap, and stores file metadata.
pub type MetadataHashMap = MetadataMap<HashMap<Vec<u8>, (Entry, Range<u64>)>>;

/// Indexes files by name with a BTreeMap, and stores file metadata.
pub type MetadataBTreeMap = MetadataMap<BTreeMap<Vec<u8>, (Entry, Range<u64>)>>;

#[allow(clippy::unnecessary_wraps)]
fn adapted_reader<'t>(flag: u32, r: impl Read + 't) -> Result<Box<(dyn Read + 't)>> {
    if (flag & 0x1000) > 0 {
        cfg_if! {
            if #[cfg(feature = "comp-zlib")] {
                Ok(Box::new(flate2::read::ZlibDecoder::new(r)))
            } else {
                Err(Error::new(ErrorKind::Other, "Compile the phar crate with comp-zlib feature to use zlib-compressed files"))
            }
        }
    } else if (flag & 0x2000) > 0 {
        cfg_if! {
            if #[cfg(feature = "comp-bzip")] {
                Ok(Box::new(bzip2::read::BzDecoder::new(r)))
            } else {
                Err(Error::new(ErrorKind::Other, "Compile the phar crate with comp-bzip feature to use bzip-compressed files"))
            }
        }
    } else {
        Ok(Box::new(r))
    }
}