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
// SPDX-License-Identifier: Apache-2.0 OR MIT
use crate::{
	error::{Error, Result},
	header::{FileIntegrity, FileLocation, Header},
};
use std::{
	borrow::Cow,
	collections::BTreeMap,
	path::{Path, PathBuf},
};

/// An AsarReader is a struct that takes an asar [`Header`] and its offset,
/// and reads the files specified in the header from the given byte buffer.
///
/// The lifetime of the [`AsarReader`] is tied to the lifetime of the byte
/// buffer that it reads from.
///
/// If the `check-integrity-on-read` feature is enabled, then the [`AsarReader`]
/// will check file integrity when reading an archive, and error out if any
/// integrity check fails.
///
/// ## Example
///
/// ```rust,no_run
/// use asar::{AsarReader, Header, Result};
/// use std::fs;
///
/// fn main() -> Result<()> {
/// 	let asar_file = fs::read("archive.asar")?;
/// 	let reader = AsarReader::new(&asar_file, None)?;
///
/// 	println!("There are {} files in archive.asar", reader.files().len());
/// 	Ok(())
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct AsarReader<'a> {
	header: Header,
	directories: BTreeMap<PathBuf, Vec<PathBuf>>,
	files: BTreeMap<PathBuf, AsarFile<'a>>,
	symlinks: BTreeMap<PathBuf, PathBuf>,
	asar_path: Option<PathBuf>,
}

impl<'a> AsarReader<'a> {
	/// Parse and read an asar archive from a byte buffer.
	///
	/// If you care about unpacked files, pass a `asar_path` containing the path
	/// to the asar archive.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// use asar::{AsarReader, Header};
	/// use std::{fs, path::PathBuf};
	///
	/// let asar_file = fs::read("archive.asar")?;
	/// let asar = AsarReader::new(&asar_file, PathBuf::from("./archive.asar"))?;
	/// # Ok::<(), asar::Error>(())
	/// ```
	pub fn new(data: &'a [u8], asar_path: impl Into<Option<PathBuf>>) -> Result<Self> {
		let (header, offset) = Header::read(&mut &data[..])?;
		Self::new_from_header(header, offset, data, asar_path)
	}

	/// Read an asar archive from a byte buffer, using the given header and
	/// offset.
	///
	/// If you care about unpacked files, pass a `asar_path` containing the path
	/// to the asar archive.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// use asar::{AsarReader, Header};
	/// use std::fs;
	///
	/// let asar_file = fs::read("archive.asar")?;
	/// let (header, offset) = Header::read(&mut &asar_file[..])?;
	/// let asar = AsarReader::new_from_header(header, offset, &asar_file, None)?;
	/// # Ok::<(), asar::Error>(())
	/// ```
	pub fn new_from_header(
		header: Header,
		offset: usize,
		data: &'a [u8],
		asar_path: impl Into<Option<PathBuf>>,
	) -> Result<Self> {
		let mut files = BTreeMap::new();
		let mut directories = BTreeMap::new();
		let mut symlinks = BTreeMap::new();
		let asar_path = asar_path.into();
		recursive_read(
			PathBuf::new(),
			&mut files,
			&mut directories,
			&mut symlinks,
			&header,
			offset,
			data,
			asar_path.as_deref(),
		)?;
		Ok(Self {
			header,
			files,
			directories,
			symlinks,
			asar_path,
		})
	}

	/// Gets all files in the asar.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// for (path, file_info) in asar.files() {
	/// 	println!("file {}", path.display());
	/// 	println!("\t{} bytes", file_info.data().len());
	/// 	println!(
	/// 		"\thash: {}",
	/// 		hex::encode(file_info.integrity().unwrap().hash())
	/// 	);
	/// }
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub const fn files(&self) -> &BTreeMap<PathBuf, AsarFile<'a>> {
		&self.files
	}

	/// Gets all directories in the asar.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// for (path, contents) in asar.directories() {
	/// 	println!("dir {}", path.display());
	/// 	for file in contents {
	/// 		println!("\tfile {}", file.display());
	/// 	}
	/// }
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub const fn directories(&self) -> &BTreeMap<PathBuf, Vec<PathBuf>> {
		&self.directories
	}

	/// Gets all symbolic links in the asar.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// for (path, link) in asar.symlinks() {
	/// 	println!("file {}", path.display());
	/// 	println!("\tlink {}", link.display());
	/// }
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub const fn symlinks(&self) -> &BTreeMap<PathBuf, PathBuf> {
		&self.symlinks
	}

	/// Gets information about a file.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	/// use std::path::Path;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// let file_info = asar.read(Path::new("hello.txt")).unwrap();
	/// println!("hello.txt is {} bytes", file_info.data().len());
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub fn read(&self, path: &Path) -> Option<&AsarFile> {
		if let Some(link) = self.symlinks.get(path) {
			return self.files.get(link);
		}
		self.files.get(path)
	}

	/// Gets the contents of a directory.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	/// use std::path::Path;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// let contents = asar.read_dir(Path::new("dir a/dir b")).unwrap();
	/// for file in contents {
	/// 	println!("file {}", file.display());
	/// }
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub fn read_dir(&self, path: &Path) -> Option<&[PathBuf]> {
		self.directories.get(path).map(|paths| paths.as_slice())
	}
}

/// This represents a file in an asar archive, with a byte slice referencing the
/// contents, and the integrity details containing file hashes.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AsarFile<'a> {
	data: Cow<'a, [u8]>,
	integrity: Option<FileIntegrity>,
}

impl<'a> AsarFile<'a> {
	/// The data of the file.
	///
	/// ## Example
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	/// use std::path::Path;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// let file_info = asar.read(Path::new("hello.txt")).unwrap();
	/// assert_eq!(file_info.data(), b"Hello, World!");
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub fn data(&self) -> &[u8] {
		self.data.as_ref()
	}

	/// Integrity details of the file, such as hashes.
	///
	/// ## Example
	/// ```rust,no_run
	/// # use std::fs;
	/// use asar::AsarReader;
	/// use std::path::Path;
	///
	/// # let asar_file = fs::read("archive.asar")?;
	/// # let asar = AsarReader::new(&asar_file, None)?;
	/// let file_info = asar.read(Path::new("hello.txt")).unwrap();
	/// let integrity = file_info.integrity().unwrap();
	/// assert_eq!(
	/// 	integrity.hash(),
	/// 	b"\xf6\x95\x2d\x6e\xef\x55\x5d\xdd\x87\xac\xa6\x6e\x56\xb9\x15\x30\x22\
	/// x2d\x6e\x31\x84\x14\x81\x6f\x3b\xa7\xcf\x5b\xf6\x94\xbf\x0f"
	/// );
	/// # Ok::<(), asar::Error>(())
	/// ```
	#[inline]
	pub const fn integrity(&self) -> Option<&FileIntegrity> {
		self.integrity.as_ref()
	}
}

fn recursive_read<'a>(
	path: PathBuf,
	file_map: &mut BTreeMap<PathBuf, AsarFile<'a>>,
	dir_map: &mut BTreeMap<PathBuf, Vec<PathBuf>>,
	symlink_map: &mut BTreeMap<PathBuf, PathBuf>,
	header: &Header,
	begin_offset: usize,
	data: &'a [u8],
	asar_path: Option<&Path>,
) -> Result<()> {
	match header {
		Header::File(file) => {
			let data = match file.location() {
				FileLocation::Offset { offset } => {
					let start = begin_offset + offset;
					let end = start + file.size();
					if data.len() < end {
						println!(
							"file truncated path='{}', data_len={}, start={}, size={}, end={}",
							path.display(),
							data.len(),
							start,
							file.size(),
							end
						);
						return Err(Error::Truncated);
					}
					Cow::Borrowed(&data[start..end])
				}
				FileLocation::Unpacked { .. } => match asar_path {
					Some(asar_path) => {
						std::fs::read(asar_path.with_extension("asar.unpacked").join(&path))
							.map(Cow::Owned)
							.map_err(|err| Error::UnpackedIoError {
								path: path.clone(),
								err,
							})?
					}
					None => Cow::Borrowed(&[] as &[u8]),
				},
			};
			#[cfg(feature = "check-integrity-on-read")]
			{
				let integrity = file.integrity();
				let algorithm = integrity.algorithm();
				let block_size = integrity.block_size();
				let blocks = integrity.blocks();
				if block_size > 0 && !blocks.is_empty() {
					for (idx, (block, expected_hash)) in
						data.chunks(block_size).zip(blocks.iter()).enumerate()
					{
						let hash = algorithm.hash(block);
						if hash != *expected_hash {
							return Err(Error::HashMismatch {
								file: path,
								block: Some(idx + 1),
								expected: expected_hash.to_owned(),
								actual: hash,
							});
						}
					}
				}
				let hash = algorithm.hash(data);
				if hash != integrity.hash() {
					return Err(Error::HashMismatch {
						file: path,
						block: None,
						expected: integrity.hash().to_owned(),
						actual: hash,
					});
				}
			}
			file_map.insert(path, AsarFile {
				data,
				integrity: file.integrity().cloned(),
			});
		}
		Header::Directory { files } => {
			for (name, header) in files {
				let file_path = path.join(name);
				dir_map
					.entry(path.clone())
					.or_default()
					.push(file_path.clone());
				recursive_read(
					file_path,
					file_map,
					dir_map,
					symlink_map,
					header,
					begin_offset,
					data,
					asar_path,
				)?;
			}
		}
		Header::Link { link } => {
			symlink_map.insert(path, link.clone());
		}
	}
	Ok(())
}

#[cfg(test)]
pub mod test {
	use super::AsarReader;
	use crate::header::TEST_ASAR;
	use include_dir::{include_dir, Dir};

	static ASAR_CONTENTS: Dir = include_dir!("$CARGO_MANIFEST_DIR/data/contents");

	#[test]
	fn test_reading() {
		let reader = AsarReader::new(TEST_ASAR, None).expect("failed to read asar");
		for (path, file) in reader.files() {
			let real_file = ASAR_CONTENTS
				.get_file(path)
				.unwrap_or_else(|| panic!("test.asar contains invalid file {}", path.display()));
			let real_contents = real_file.contents();
			let asar_contents = file.data();
			assert_eq!(real_contents, asar_contents);
		}
	}
}