apple_xar/
format.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use {
6    scroll::{IOread, IOwrite, Pread, SizeWith},
7    std::fmt::{Display, Formatter},
8};
9
10/// A XAR archive header.
11///
12/// The header effectively defines a table of contents, which
13/// holds information about the content of the archive.
14#[derive(Clone, Copy, Debug, IOread, IOwrite, Pread, SizeWith)]
15pub struct XarHeader {
16    /// File magic. `xar!`.
17    pub magic: u32,
18
19    /// Size of this header + magic.
20    pub size: u16,
21
22    /// Format version number.
23    pub version: u16,
24
25    /// Size in bytes of zlib compressed table of contents.
26    pub toc_length_compressed: u64,
27
28    /// Size in bytes of uncompressed table of contents.
29    pub toc_length_uncompressed: u64,
30
31    /// Checksum algorithm used.
32    pub checksum_algorithm_id: u32,
33}
34
35/// Checksum format used in file.
36pub enum XarChecksum {
37    None,
38    Sha1,
39    Md5,
40    Sha256,
41    Sha512,
42    Other(u32),
43}
44
45impl From<u32> for XarChecksum {
46    fn from(i: u32) -> Self {
47        match i {
48            0 => Self::None,
49            1 => Self::Sha1,
50            2 => Self::Md5,
51            3 => Self::Sha256,
52            4 => Self::Sha512,
53            _ => Self::Other(i),
54        }
55    }
56}
57
58impl From<XarChecksum> for u32 {
59    fn from(checksum: XarChecksum) -> u32 {
60        match checksum {
61            XarChecksum::None => 0,
62            XarChecksum::Sha1 => 1,
63            XarChecksum::Md5 => 2,
64            XarChecksum::Sha256 => 3,
65            XarChecksum::Sha512 => 4,
66            XarChecksum::Other(v) => v,
67        }
68    }
69}
70
71impl Display for XarChecksum {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            XarChecksum::None => f.write_str("none"),
75            XarChecksum::Sha1 => f.write_str("SHA-1"),
76            XarChecksum::Md5 => f.write_str("MD5"),
77            XarChecksum::Sha256 => f.write_str("SHA-256"),
78            XarChecksum::Sha512 => f.write_str("SHA-512"),
79            XarChecksum::Other(v) => f.write_fmt(format_args!("unknown ({v})")),
80        }
81    }
82}