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
//! Structures that describes asar's header.
//!
//! Asar's header is represented using a single root [`Directory`], with tree
//! structures similar to what the file system looks like.

use serde::de::{Error, Unexpected};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Deref;
use tokio::io;

/// Entry of either a file or a directory.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Entry {
  /// A file.
  File(FileMetadata),

  /// A directory.
  Directory(Directory),
}

impl Entry {
  pub(crate) fn search_segments(&self, segments: &[&str]) -> Option<&Entry> {
    match self {
      _ if segments.is_empty() => Some(self),
      Self::File(_) => None,
      Self::Directory(dir) => dir.search_segments(segments),
    }
  }
}

/// Metadata of a file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileMetadata {
  /// Where the file is located.
  #[serde(flatten)]
  pub pos: FilePosition,

  /// The file's size.
  ///
  /// According to [official repository], this field should not be larger than
  /// `9007199254740991`, which is JavaScript's `Number.MAX_SAFE_INTEGER` and
  /// about 8PB in size.
  ///
  /// [official repository]: https://github.com/electron/asar#format
  pub size: u64,

  /// Whether the file is an executable.
  #[serde(default)]
  #[serde(skip_serializing_if = "bool::clone")]
  pub executable: bool,

  /// Optional integrity information of the file.
  pub integrity: Option<Integrity>,
}

impl FileMetadata {
  pub(crate) fn offset(&self) -> io::Result<u64> {
    if let FilePosition::Offset(x) = self.pos {
      Ok(x)
    } else {
      Err(io::Error::new(
        io::ErrorKind::Other,
        "unpacked file is currently not supported",
      ))
    }
  }
}

/// Whether the file is stored in the archive or is unpacked.
#[derive(Debug, Clone, Copy)]
pub enum FilePosition {
  /// Offset of the file in the archive, indicates the file is stored in it.
  Offset(u64),

  /// Indicates the file is stored outside the archive.
  Unpacked,
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum Helper<'a> {
  Offset {
    #[serde(skip_serializing_if = "Option::is_none")]
    unpacked: Option<bool>,
    offset: &'a str,
  },
  Unpacked {
    unpacked: bool,
  },
}

impl Serialize for FilePosition {
  fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
    let offset_string;
    let helper = match self {
      Self::Offset(offset) => {
        offset_string = offset.to_string();
        Helper::Offset {
          unpacked: None,
          offset: &offset_string,
        }
      }
      Self::Unpacked => Helper::Unpacked { unpacked: true },
    };

    helper.serialize(ser)
  }
}

impl<'de> Deserialize<'de> for FilePosition {
  fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
    match Helper::deserialize(de)? {
      Helper::Offset { unpacked, .. } if matches!(unpacked, Some(true)) => {
        Err(Error::custom("got both 'unpacked' and 'offset' field"))
      }
      Helper::Offset { offset, .. } => offset
        .parse()
        .map(Self::Offset)
        .map_err(|_| Error::invalid_value(Unexpected::Str(offset), &"valid u64 string")),
      Helper::Unpacked { unpacked: true } => Ok(Self::Unpacked),
      Helper::Unpacked { unpacked: false } => {
        Err(Error::invalid_value(Unexpected::Bool(false), &"true"))
      }
    }
  }
}

/// Integrity information of a file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Integrity {
  /// Hashing algorithm used.
  pub algorithm: Algorithm,

  /// The hash of the entire file.
  pub hash: Hash,

  /// Indicates the size of each block of the hashes in `blocks`.
  #[serde(rename = "blockSize")]
  pub block_size: u32,

  /// Hashes of blocks.
  pub blocks: Vec<Hash>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Hash(#[serde(with = "hex::serde")] pub(crate) Vec<u8>);

impl From<Vec<u8>> for Hash {
  fn from(x: Vec<u8>) -> Self {
    Self(x)
  }
}

impl From<Hash> for Vec<u8> {
  fn from(x: Hash) -> Self {
    x.0
  }
}

impl AsRef<[u8]> for Hash {
  fn as_ref(&self) -> &[u8] {
    &self.0
  }
}

impl Deref for Hash {
  type Target = [u8];

  fn deref(&self) -> &Self::Target {
    self.as_ref()
  }
}

impl Debug for Hash {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    <Self as Display>::fmt(self, f)
  }
}

impl Display for Hash {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    f.write_str(&hex::encode(&self.0))
  }
}

/// Hashing algorithm used to check files' integrity.
///
/// Currently only SHA256 is officially supported.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Algorithm {
  SHA256,
}

/// A directory, containing files.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Directory {
  pub files: HashMap<Box<str>, Entry>,
}

impl Directory {
  pub(crate) fn search_segments(&self, segments: &[&str]) -> Option<&Entry> {
    (self.files)
      .get(segments[0])
      .and_then(|x| x.search_segments(&segments[1..]))
  }
}