sqsh-rs 0.2.1

A Rust wrapper around the libsqsh library
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
use crate::{error, Archive, InodeRef};
use bitflags::bitflags;
use sqsh_sys as ffi;
use std::fmt;
use std::ptr::NonNull;

/// The type of compression used in an archive.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Compression {
    id: ffi::SqshSuperblockCompressionId,
}

impl Compression {
    pub const GZIP: Self = Self {
        id: ffi::SqshSuperblockCompressionId::SQSH_COMPRESSION_GZIP,
    };

    pub const LZMA: Self = Self {
        id: ffi::SqshSuperblockCompressionId::SQSH_COMPRESSION_LZMA,
    };

    pub const LZO: Self = Self {
        id: ffi::SqshSuperblockCompressionId::SQSH_COMPRESSION_LZO,
    };

    pub const XZ: Self = Self {
        id: ffi::SqshSuperblockCompressionId::SQSH_COMPRESSION_XZ,
    };

    pub const LZ4: Self = Self {
        id: ffi::SqshSuperblockCompressionId::SQSH_COMPRESSION_LZ4,
    };

    pub const ZSTD: Self = Self {
        id: ffi::SqshSuperblockCompressionId::SQSH_COMPRESSION_ZSTD,
    };

    #[must_use]
    pub fn name(&self) -> Option<&'static str> {
        Some(match *self {
            Self::GZIP => "gzip",
            Self::LZMA => "lzma",
            Self::LZO => "lzo",
            Self::XZ => "xz",
            Self::LZ4 => "lz4",
            Self::ZSTD => "zstd",
            _ => return None,
        })
    }
}

impl fmt::Debug for Compression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = self.name();
        let value: &dyn fmt::Debug = match &name {
            Some(name) => name,
            None => &self.id,
        };
        f.debug_tuple("Compression").field(value).finish()
    }
}

/// Information about the compression options used in an archive.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum CompressionOptions {
    Gzip {
        compression_level: u32,
        window_size: u16,
        strategies: GzipStrategies,
    },
    Xz {
        dictionary_size: u32,
        filters: XzFilters,
    },
    Lz4 {
        version: u32,
        flags: Lz4Flags,
    },
    Zstd {
        compression_level: u32,
    },
    Lzo {
        algorithm: LzoAlgorithm,
        compression_level: u32,
    },
}

bitflags! {
    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    pub struct GzipStrategies: u16 {
        const DEFAULT = 1 << 0;
        const FILTERED = 1 << 1;
        const HUFFMAN_ONLY = 1 << 2;
        const RLE = 1 << 3;
        const FIXED = 1 << 4;

        const _ = !0;
    }

    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    pub struct XzFilters: u16 {
        const X86 = 1 << 0;
        const POWERPC = 1 << 1;
        const IA64 = 1 << 2;
        const ARM = 1 << 3;
        const ARMTHUMB = 1 << 4;
        const SPARC = 1 << 5;

        const _ = !0;
    }

    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    pub struct Lz4Flags: u32 {
        const DEFAULT = 1 << 0;
        const HC = 1 << 1;

        const _ = !0;
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct LzoAlgorithm(u32);

impl LzoAlgorithm {
    const LZO1X_1: Self = Self(ffi::SqshLzoAlgorithm::SQSH_LZO_ALGORITHM_LZO1X_1.0);
    const LZO1X_1_11: Self = Self(ffi::SqshLzoAlgorithm::SQSH_LZO_ALGORITHM_LZO1X_1_11.0);
    const LZO1X_1_12: Self = Self(ffi::SqshLzoAlgorithm::SQSH_LZO_ALGORITHM_LZO1X_1_12.0);
    const LZO1X_1_15: Self = Self(ffi::SqshLzoAlgorithm::SQSH_LZO_ALGORITHM_LZO1X_1_15.0);
    const LZO1X_999: Self = Self(ffi::SqshLzoAlgorithm::SQSH_LZO_ALGORITHM_LZO1X_999.0);

    fn as_str(&self) -> Option<&'static str> {
        match *self {
            Self::LZO1X_1 => Some("lzo1x_1"),
            Self::LZO1X_1_11 => Some("lzo1x_1_11"),
            Self::LZO1X_1_12 => Some("lzo1x_1_12"),
            Self::LZO1X_1_15 => Some("lzo1x_1_15"),
            Self::LZO1X_999 => Some("lzo1x_999"),
            _ => None,
        }
    }
}

impl Default for LzoAlgorithm {
    fn default() -> Self {
        Self::LZO1X_999
    }
}

impl fmt::Debug for LzoAlgorithm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = self.as_str();
        let value: &dyn fmt::Debug = match &name {
            Some(name) => name,
            None => &self.0,
        };
        f.debug_tuple("LzoAlgorithm").field(value).finish()
    }
}

impl Archive<'_> {
    #[must_use]
    pub fn superblock(&self) -> Superblock<'_> {
        unsafe { Superblock::new(ffi::sqsh_archive_superblock(self.inner.as_ptr())) }
    }

    pub fn compression_options(&self) -> error::Result<Option<CompressionOptions>> {
        struct RawCompressionOptions(NonNull<ffi::SqshCompressionOptions>);
        impl Drop for RawCompressionOptions {
            fn drop(&mut self) {
                unsafe { ffi::sqsh_compression_options_free(self.0.as_ptr()) };
            }
        }

        let superblock = self.superblock();
        if !superblock.has_compression_options() {
            return Ok(None);
        }
        let compression_options = unsafe {
            let mut err = 0;
            let raw = ffi::sqsh_compression_options_new(self.inner.as_ptr(), &mut err);
            let raw = match NonNull::new(raw) {
                Some(raw) => raw,
                None => return Err(error::new(err)),
            };
            RawCompressionOptions(raw)
        };

        Ok(Some(match superblock.compression_type() {
            Compression::GZIP => CompressionOptions::Gzip {
                compression_level: unsafe {
                    ffi::sqsh_compression_options_gzip_compression_level(
                        compression_options.0.as_ptr(),
                    )
                },
                window_size: unsafe {
                    ffi::sqsh_compression_options_gzip_window_size(compression_options.0.as_ptr())
                },
                strategies: GzipStrategies::from_bits_retain(unsafe {
                    ffi::sqsh_compression_options_gzip_strategies(compression_options.0.as_ptr()).0
                        as _
                }),
            },
            Compression::XZ => CompressionOptions::Xz {
                dictionary_size: unsafe {
                    ffi::sqsh_compression_options_xz_dictionary_size(compression_options.0.as_ptr())
                },
                filters: XzFilters::from_bits_retain(unsafe {
                    ffi::sqsh_compression_options_xz_filters(compression_options.0.as_ptr()).0 as _
                }),
            },
            Compression::LZ4 => CompressionOptions::Lz4 {
                version: unsafe {
                    ffi::sqsh_compression_options_lz4_version(compression_options.0.as_ptr())
                },
                flags: Lz4Flags::from_bits_retain(unsafe {
                    ffi::sqsh_compression_options_lz4_flags(compression_options.0.as_ptr())
                } as _),
            },
            Compression::ZSTD => CompressionOptions::Zstd {
                compression_level: unsafe {
                    ffi::sqsh_compression_options_zstd_compression_level(
                        compression_options.0.as_ptr(),
                    )
                },
            },
            Compression::LZO => CompressionOptions::Lzo {
                algorithm: LzoAlgorithm(unsafe {
                    ffi::sqsh_compression_options_lzo_algorithm(compression_options.0.as_ptr()).0
                        as _
                }),
                compression_level: unsafe {
                    ffi::sqsh_compression_options_lzo_compression_level(
                        compression_options.0.as_ptr(),
                    )
                },
            },
            _ => return Ok(None),
        }))
    }
}

/// Information about the layout and statistics about an archive.
#[derive(Copy, Clone)]
pub struct Superblock<'archive> {
    inner: &'archive ffi::SqshSuperblock,
}

impl<'archive> Superblock<'archive> {
    pub(crate) unsafe fn new(inner: *const ffi::SqshSuperblock) -> Self {
        let inner = inner.as_ref().expect("null superblock pointer");
        Self { inner }
    }

    #[must_use]
    pub fn compression_type(&self) -> Compression {
        let compression_id = unsafe { ffi::sqsh_superblock_compression_id(self.inner) };
        Compression { id: compression_id }
    }

    /// Retrieves the number of inodes in an archive.
    #[must_use]
    pub fn inode_count(&self) -> u32 {
        unsafe { ffi::sqsh_superblock_inode_count(self.inner) }
    }

    /// Retrieves the number of ids in an archive.
    #[must_use]
    pub fn id_count(&self) -> u16 {
        unsafe { ffi::sqsh_superblock_id_count(self.inner) }
    }

    /// Retrieves the number of fragment entries in an archive.
    #[must_use]
    pub fn fragment_entry_count(&self) -> u32 {
        unsafe { ffi::sqsh_superblock_fragment_entry_count(self.inner) }
    }

    /// Retrieves the start offset of the inode table in a superblock context.
    #[must_use]
    pub fn inode_table_start(&self) -> u64 {
        unsafe { ffi::sqsh_superblock_inode_table_start(self.inner) }
    }

    /// Retrieves the start offset of the directory table in a superblock context.
    #[must_use]
    pub fn directory_table_start(&self) -> u64 {
        unsafe { ffi::sqsh_superblock_directory_table_start(self.inner) }
    }

    /// Retrieves the start offset of the fragment table in a superblock context.
    #[must_use]
    pub fn fragment_table_start(&self) -> Option<u64> {
        self.has_fragments()
            .then(|| unsafe { ffi::sqsh_superblock_fragment_table_start(self.inner) })
    }

    /// Retrieves the start offset of the export table in a superblock context.
    #[must_use]
    pub fn export_table_start(&self) -> Option<u64> {
        self.has_export_table()
            .then(|| unsafe { ffi::sqsh_superblock_export_table_start(self.inner) })
    }

    /// Retrieves the start offset of the id table in a superblock context.
    #[must_use]
    pub fn id_table_start(&self) -> u64 {
        unsafe { ffi::sqsh_superblock_id_table_start(self.inner) }
    }

    /// Retrieves the start offset of the xattr id table in a superblock context.
    #[must_use]
    pub fn xattr_id_table_start(&self) -> Option<u64> {
        self.has_xattr_table()
            .then(|| unsafe { ffi::sqsh_superblock_xattr_id_table_start(self.inner) })
    }

    /// Retrieves the reference of the root inode in a superblock context.
    #[must_use]
    pub fn root_inode_ref(&self) -> InodeRef {
        let inode_ref = unsafe { ffi::sqsh_superblock_inode_root_ref(self.inner) };
        InodeRef(inode_ref)
    }

    /// Checks if a superblock context has fragment table.
    #[must_use]
    pub fn has_fragments(&self) -> bool {
        unsafe { ffi::sqsh_superblock_has_fragments(self.inner) }
    }

    /// Checks if a superblock context has an export table.
    #[must_use]
    pub fn has_export_table(&self) -> bool {
        unsafe { ffi::sqsh_superblock_has_export_table(self.inner) }
    }

    /// Checks if a superblock context has an xattr table.
    #[must_use]
    pub fn has_xattr_table(&self) -> bool {
        unsafe { ffi::sqsh_superblock_has_xattr_table(self.inner) }
    }

    /// Checks if a superblock context has compression options.
    #[must_use]
    pub fn has_compression_options(&self) -> bool {
        unsafe { ffi::sqsh_superblock_has_compression_options(self.inner) }
    }

    /// Retrieves the major version of an archive.
    #[must_use]
    pub fn version_major(&self) -> u16 {
        unsafe { ffi::sqsh_superblock_version_major(self.inner) }
    }

    /// Retrieves the minor version of an archive.
    #[must_use]
    pub fn version_minor(&self) -> u16 {
        unsafe { ffi::sqsh_superblock_version_minor(self.inner) }
    }

    /// Retrieves the block size of an archive.
    #[must_use]
    pub fn block_size(&self) -> u32 {
        unsafe { ffi::sqsh_superblock_block_size(self.inner) }
    }

    /// Retrieves the modification time of an archive, as seconds since the Unix epoch.
    #[must_use]
    pub fn modification_time(&self) -> u32 {
        unsafe { ffi::sqsh_superblock_modification_time(self.inner) }
    }

    /// Retrieves the number of bytes used in an archive.
    #[must_use]
    pub fn bytes_used(&self) -> u64 {
        unsafe { ffi::sqsh_superblock_bytes_used(self.inner) }
    }
}

impl<'archive> fmt::Debug for Superblock<'archive> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Superblock")
            .field("compression_type", &self.compression_type())
            .field("inode_count", &self.inode_count())
            .field("id_count", &self.id_count())
            .field("fragment_entry_count", &self.fragment_entry_count())
            .field("inode_table_start", &self.inode_table_start())
            .field("directory_table_start", &self.directory_table_start())
            .field("fragment_table_start", &self.fragment_table_start())
            .field("export_table_start", &self.export_table_start())
            .field("id_table_start", &self.id_table_start())
            .field("xattr_id_table_start", &self.xattr_id_table_start())
            .field("root_inode_ref", &self.root_inode_ref())
            .field("has_fragments", &self.has_fragments())
            .field("has_export_table", &self.has_export_table())
            .field("has_xattr_table", &self.has_xattr_table())
            .field("has_compression_options", &self.has_compression_options())
            .field("version_major", &self.version_major())
            .field("version_minor", &self.version_minor())
            .field("block_size", &self.block_size())
            .field("modification_time", &self.modification_time())
            .field("bytes_used", &self.bytes_used())
            .finish()
    }
}