From 622771856409e7842e53a0aa8dcbab5df5c5a291 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Thu, 3 Feb 2022 01:57:01 -0500 Subject: [PATCH] Migrate to binrw; update cargo deny config; update deps --- .github/workflows/build.yaml | 8 ++ Cargo.toml | 10 +- deny.toml | 203 ++++++++++++++++++++++++++++++++++- src/disc/gcn.rs | 2 +- src/disc/mod.rs | 18 ++-- src/disc/wii.rs | 6 +- src/fst.rs | 10 +- src/io/iso.rs | 6 +- src/io/mod.rs | 3 +- src/io/nfs.rs | 4 +- src/lib.rs | 7 +- src/streams.rs | 6 +- 12 files changed, 244 insertions(+), 39 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 53ee00b..298d38e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -17,6 +17,14 @@ jobs: with: toolchain: ${{ matrix.toolchain }} override: true + - uses: actions-rs/cargo@v1 + with: + command: check + args: --all-features + - uses: actions-rs/cargo@v1 + with: + command: test + args: --all-features - uses: actions-rs/cargo@v1 with: command: build diff --git a/Cargo.toml b/Cargo.toml index 3dc3a1a..21a7715 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,12 +18,12 @@ name = "nodtool" path = "src/bin.rs" [dependencies] -aes = "0.7.4" +aes = "0.7.5" anyhow = "1.0.53" -binread = "2.1.1" +binrw = "0.8.4" block-modes = "0.8.1" -clap = "2.33.3" -encoding_rs = "0.8.28" +clap = "2.34.0" +encoding_rs = "0.8.30" file-size = "1.0.3" -sha-1 = "0.9.7" +sha-1 = "0.10.0" thiserror = "1.0.30" diff --git a/deny.toml b/deny.toml index ab17368..6279167 100644 --- a/deny.toml +++ b/deny.toml @@ -1,2 +1,203 @@ +# This template contains all of the possible sections and their default values + +# Note that all fields that take a lint level have these possible values: +# * deny - An error will be produced and the check will fail +# * warn - A warning will be produced, but the check will not fail +# * allow - No warning or error will be produced, though in some cases a note +# will be + +# The values provided in this template are the default values that will be used +# when any section or field is not specified in your own configuration + +# If 1 or more target triples (and optionally, target_features) are specified, +# only the specified targets will be checked when running `cargo deny check`. +# This means, if a particular package is only ever used as a target specific +# dependency, such as, for example, the `nix` crate only being used via the +# `target_family = "unix"` configuration, that only having windows targets in +# this list would mean the nix crate, as well as any of its exclusive +# dependencies not shared by any other crates, would be ignored, as the target +# list here is effectively saying which targets you are building for. +targets = [ + # The triple can be any string, but only the target triples built in to + # rustc (as of 1.40) can be checked against actual config expressions + #{ triple = "x86_64-unknown-linux-musl" }, + # You can also specify which target_features you promise are enabled for a + # particular target. target_features are currently not validated against + # the actual valid features supported by the target architecture. + #{ triple = "wasm32-unknown-unknown", features = ["atomics"] }, +] + +# This section is considered when running `cargo deny check advisories` +# More documentation for the advisories section can be found here: +# https://embarkstudios.github.io/cargo-deny/checks/advisories/cfg.html +[advisories] +# The path where the advisory database is cloned/fetched into +db-path = "~/.cargo/advisory-db" +# The url(s) of the advisory databases to use +db-urls = ["https://github.com/rustsec/advisory-db"] +# The lint level for security vulnerabilities +vulnerability = "deny" +# The lint level for unmaintained crates +unmaintained = "warn" +# The lint level for crates that have been yanked from their source registry +yanked = "warn" +# The lint level for crates with security notices. Note that as of +# 2019-12-17 there are no security notice advisories in +# https://github.com/rustsec/advisory-db +notice = "warn" +# A list of advisory IDs to ignore. Note that ignored advisories will still +# output a note when they are encountered. +ignore = [ + #"RUSTSEC-0000-0000", +] +# Threshold for security vulnerabilities, any vulnerability with a CVSS score +# lower than the range specified will be ignored. Note that ignored advisories +# will still output a note when they are encountered. +# * None - CVSS Score 0.0 +# * Low - CVSS Score 0.1 - 3.9 +# * Medium - CVSS Score 4.0 - 6.9 +# * High - CVSS Score 7.0 - 8.9 +# * Critical - CVSS Score 9.0 - 10.0 +#severity-threshold = + +# This section is considered when running `cargo deny check licenses` +# More documentation for the licenses section can be found here: +# https://embarkstudios.github.io/cargo-deny/checks/licenses/cfg.html [licenses] -allow = ["MIT"] +# The lint level for crates which do not have a detectable license +unlicensed = "deny" +# List of explictly allowed licenses +# See https://spdx.org/licenses/ for list of possible licenses +# [possible values: any SPDX 3.11 short identifier (+ optional exception)]. +allow = [ + "MIT", + "Apache-2.0", + "BSD-3-Clause", +] +# List of explictly disallowed licenses +# See https://spdx.org/licenses/ for list of possible licenses +# [possible values: any SPDX 3.11 short identifier (+ optional exception)]. +deny = [ + #"Nokia", +] +# Lint level for licenses considered copyleft +copyleft = "warn" +# Blanket approval or denial for OSI-approved or FSF Free/Libre licenses +# * both - The license will be approved if it is both OSI-approved *AND* FSF +# * either - The license will be approved if it is either OSI-approved *OR* FSF +# * osi-only - The license will be approved if is OSI-approved *AND NOT* FSF +# * fsf-only - The license will be approved if is FSF *AND NOT* OSI-approved +# * neither - This predicate is ignored and the default lint level is used +allow-osi-fsf-free = "neither" +# Lint level used when no other predicates are matched +# 1. License isn't in the allow or deny lists +# 2. License isn't copyleft +# 3. License isn't OSI/FSF, or allow-osi-fsf-free = "neither" +default = "deny" +# The confidence threshold for detecting a license from license text. +# The higher the value, the more closely the license text must be to the +# canonical license text of a valid SPDX license file. +# [possible values: any between 0.0 and 1.0]. +confidence-threshold = 0.8 +# Allow 1 or more licenses on a per-crate basis, so that particular licenses +# aren't accepted for every possible crate as with the normal allow list +exceptions = [ + # Each entry is the crate and version constraint, and its specific allow + # list + #{ allow = ["Zlib"], name = "adler32", version = "*" }, +] + +# Some crates don't have (easily) machine readable licensing information, +# adding a clarification entry for it allows you to manually specify the +# licensing information +[[licenses.clarify]] +# The name of the crate the clarification applies to +name = "encoding_rs" +# The optional version constraint for the crate +#version = "*" +# The SPDX expression for the license requirements of the crate +expression = "(Apache-2.0 OR MIT) AND BSD-3-Clause" +# One or more files in the crate's source used as the "source of truth" for +# the license expression. If the contents match, the clarification will be used +# when running the license check, otherwise the clarification will be ignored +# and the crate will be checked normally, which may produce warnings or errors +# depending on the rest of your configuration +license-files = [ + # Each entry is a crate relative path, and the (opaque) hash of its contents + { path = "COPYRIGHT", hash = 0x39f8ad31 } +] + +[licenses.private] +# If true, ignores workspace crates that aren't published, or are only +# published to private registries +ignore = false +# One or more private registries that you might publish crates to, if a crate +# is only published to private registries, and ignore is true, the crate will +# not have its license(s) checked +registries = [ + #"https://sekretz.com/registry +] + +# This section is considered when running `cargo deny check bans`. +# More documentation about the 'bans' section can be found here: +# https://embarkstudios.github.io/cargo-deny/checks/bans/cfg.html +[bans] +# Lint level for when multiple versions of the same crate are detected +multiple-versions = "warn" +# Lint level for when a crate version requirement is `*` +wildcards = "allow" +# The graph highlighting used when creating dotgraphs for crates +# with multiple versions +# * lowest-version - The path to the lowest versioned duplicate is highlighted +# * simplest-path - The path to the version with the fewest edges is highlighted +# * all - Both lowest-version and simplest-path are used +highlight = "all" +# List of crates that are allowed. Use with care! +allow = [ + #{ name = "ansi_term", version = "=0.11.0" }, +] +# List of crates to deny +deny = [ + # Each entry the name of a crate and a version range. If version is + # not specified, all versions will be matched. + #{ name = "ansi_term", version = "=0.11.0" }, + # + # Wrapper crates can optionally be specified to allow the crate when it + # is a direct dependency of the otherwise banned crate + #{ name = "ansi_term", version = "=0.11.0", wrappers = [] }, +] +# Certain crates/versions that will be skipped when doing duplicate detection. +skip = [ + #{ name = "ansi_term", version = "=0.11.0" }, +] +# Similarly to `skip` allows you to skip certain crates during duplicate +# detection. Unlike skip, it also includes the entire tree of transitive +# dependencies starting at the specified crate, up to a certain depth, which is +# by default infinite +skip-tree = [ + #{ name = "ansi_term", version = "=0.11.0", depth = 20 }, +] + +# This section is considered when running `cargo deny check sources`. +# More documentation about the 'sources' section can be found here: +# https://embarkstudios.github.io/cargo-deny/checks/sources/cfg.html +[sources] +# Lint level for what to happen when a crate from a crate registry that is not +# in the allow list is encountered +unknown-registry = "warn" +# Lint level for what to happen when a crate from a git repository that is not +# in the allow list is encountered +unknown-git = "warn" +# List of URLs for allowed crate registries. Defaults to the crates.io index +# if not specified. If it is specified but empty, no registries are allowed. +allow-registry = ["https://github.com/rust-lang/crates.io-index"] +# List of URLs for allowed Git repositories +allow-git = [] + +[sources.allow-org] +# 1 or more github.com organizations to allow git sources for +#github = [""] +# 1 or more gitlab.com organizations to allow git sources for +#gitlab = [""] +# 1 or more bitbucket.org organizations to allow git sources for +#bitbucket = [""] diff --git a/src/disc/gcn.rs b/src/disc/gcn.rs index a9c7f3f..fe01bea 100644 --- a/src/disc/gcn.rs +++ b/src/disc/gcn.rs @@ -3,7 +3,7 @@ use std::{ io::{Read, Seek, SeekFrom}, }; -use binread::prelude::*; +use binrw::{BinRead, BinReaderExt}; use crate::{ disc::{BI2Header, DiscBase, DiscIO, Header, PartHeader, PartReadStream, BUFFER_SIZE}, diff --git a/src/disc/mod.rs b/src/disc/mod.rs index a21c13b..0805e89 100644 --- a/src/disc/mod.rs +++ b/src/disc/mod.rs @@ -2,7 +2,7 @@ use std::{fmt::Debug, io}; -use binread::{prelude::*, BinReaderExt, NullString}; +use binrw::{BinRead, BinReaderExt, NullString}; use crate::{ disc::{gcn::DiscGCN, wii::DiscWii}, @@ -82,13 +82,14 @@ pub trait DiscBase: Send + Sync { /// # Examples /// /// Basic usage: - /// ``` + /// ```no_run /// use nod::disc::new_disc_base; /// use nod::io::new_disc_io; /// /// let mut disc_io = new_disc_io("path/to/file".as_ref())?; /// let disc_base = new_disc_base(disc_io.as_mut())?; /// let mut partition = disc_base.get_data_partition(disc_io.as_mut())?; + /// # Ok::<(), nod::Error>(()) /// ``` fn get_data_partition<'a>( &self, @@ -101,13 +102,14 @@ pub trait DiscBase: Send + Sync { /// # Examples /// /// Basic usage: -/// ``` +/// ```no_run /// use nod::io::new_disc_io; /// use nod::disc::new_disc_base; /// /// let mut disc_io = new_disc_io("path/to/file".as_ref())?; /// let disc_base = new_disc_base(disc_io.as_mut())?; /// disc_base.get_header(); +/// # Ok::<(), nod::Error>(()) /// ``` pub fn new_disc_base(disc_io: &mut dyn DiscIO) -> Result> { let mut stream = disc_io.begin_read_stream(0)?; @@ -129,7 +131,7 @@ pub trait PartReadStream: ReadStream { /// # Examples /// /// Basic usage: - /// ``` + /// ```no_run /// use nod::disc::{new_disc_base, PartHeader}; /// use nod::fst::NodeType; /// use nod::io::new_disc_io; @@ -142,8 +144,9 @@ pub trait PartReadStream: ReadStream { /// if let Some(NodeType::File(node)) = header.find_node("/MP3/Worlds.txt") { /// let mut s = String::new(); /// partition.begin_file_stream(node)?.read_to_string(&mut s); - /// println!(s); + /// println!("{}", s); /// } + /// # Ok::<(), nod::Error>(()) /// ``` fn begin_file_stream(&mut self, node: &Node) -> io::Result; @@ -166,7 +169,7 @@ pub trait PartHeader: Debug + Send + Sync { /// # Examples /// /// Basic usage: - /// ``` + /// ```no_run /// use nod::disc::{new_disc_base, PartHeader}; /// use nod::fst::NodeType; /// use nod::io::new_disc_io; @@ -176,11 +179,12 @@ pub trait PartHeader: Debug + Send + Sync { /// let mut partition = disc_base.get_data_partition(disc_io.as_mut())?; /// let header = partition.read_header()?; /// if let Some(NodeType::File(node)) = header.find_node("/MP1/Metroid1.pak") { - /// println!(node.name); + /// println!("{}", node.name); /// } /// if let Some(NodeType::Directory(node, children)) = header.find_node("/MP1") { /// println!("Number of files: {}", children.len()); /// } + /// # Ok::<(), nod::Error>(()) /// ``` fn find_node(&self, path: &str) -> Option<&NodeType>; } diff --git a/src/disc/wii.rs b/src/disc/wii.rs index 0ab370f..aa13821 100644 --- a/src/disc/wii.rs +++ b/src/disc/wii.rs @@ -4,7 +4,7 @@ use std::{ }; use aes::{Aes128, Block, NewBlockCipher}; -use binread::prelude::*; +use binrw::{BinRead, BinReaderExt}; use block_modes::{block_padding::NoPadding, BlockMode, Cbc}; use sha1::{digest, Digest, Sha1}; @@ -143,9 +143,7 @@ struct TMD { #[derive(Debug, PartialEq, BinRead)] struct Certificate { sig_type: SigType, - #[br(count = if sig_type == SigType::Rsa4096 { 512 } - else if sig_type == SigType::Rsa2048 { 256 } - else if sig_type == SigType::EllipticalCurve { 64 } else { 0 })] + #[br(count = if sig_type == SigType::Rsa4096 { 512 } else if sig_type == SigType::Rsa2048 { 256 } else if sig_type == SigType::EllipticalCurve { 64 } else { 0 })] sig: Vec, #[br(pad_before = 60, count = 64)] issuer: Vec, diff --git a/src/fst.rs b/src/fst.rs index fa47d70..37a6f13 100644 --- a/src/fst.rs +++ b/src/fst.rs @@ -2,7 +2,7 @@ use std::io::{Read, Seek, SeekFrom}; -use binread::{derive_binread, prelude::*, NullString, ReadOptions}; +use binrw::{binread, BinReaderExt, BinResult, NullString, ReadOptions}; use encoding_rs::SHIFT_JIS; /// File system node kind. @@ -13,7 +13,7 @@ pub enum NodeKind { } /// An individual file system node. -#[derive_binread] +#[binread] #[derive(Clone, Debug, PartialEq)] pub struct Node { #[br(temp)] @@ -50,7 +50,7 @@ pub enum NodeType { } fn read_node(reader: &mut R, ro: &ReadOptions, i: &mut u32) -> BinResult { - let node = reader.read_type::(ro.endian)?; + let node = reader.read_type::(ro.endian())?; *i += 1; BinResult::Ok(if node.kind == NodeKind::Directory { let mut children: Vec = Vec::new(); @@ -75,10 +75,10 @@ fn read_node_name( if !root { let offset = base + v.name_offset as u64; reader.seek(SeekFrom::Start(offset))?; - let null_string = reader.read_type::(ro.endian)?; + let null_string = reader.read_type::(ro.endian())?; let (res, _, errors) = SHIFT_JIS.decode(&*null_string.0); if errors { - return BinResult::Err(binread::Error::Custom { + return BinResult::Err(binrw::Error::Custom { pos: offset, err: Box::new("Failed to decode node name"), }); diff --git a/src/io/iso.rs b/src/io/iso.rs index a8b163a..742d480 100644 --- a/src/io/iso.rs +++ b/src/io/iso.rs @@ -5,11 +5,7 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{ - io::DiscIO, - streams::{ByteReadStream, ReadStream}, - Result, -}; +use crate::{io::DiscIO, streams::ReadStream, Result}; pub(crate) struct DiscIOISO { pub(crate) filename: PathBuf, diff --git a/src/io/mod.rs b/src/io/mod.rs index c92f1c4..ba47786 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -29,10 +29,11 @@ pub trait DiscIO: Send + Sync { /// # Examples /// /// Basic usage: -/// ``` +/// ```no_run /// use nod::io::new_disc_io; /// /// let mut disc_io = new_disc_io("path/to/file".as_ref())?; +/// # Ok::<(), nod::Error>(()) /// ``` pub fn new_disc_io(filename: &Path) -> Result> { let path_result = fs::canonicalize(filename); diff --git a/src/io/nfs.rs b/src/io/nfs.rs index 22463c2..36e32d7 100644 --- a/src/io/nfs.rs +++ b/src/io/nfs.rs @@ -6,7 +6,7 @@ use std::{ }; use aes::{Aes128, NewBlockCipher}; -use binread::{derive_binread, prelude::*}; +use binrw::{binread, BinRead, BinReaderExt}; use block_modes::{block_padding::NoPadding, BlockMode, Cbc}; use crate::{disc::BUFFER_SIZE, io::DiscIO, streams::ReadStream, Error, Result}; @@ -19,7 +19,7 @@ pub(crate) struct LBARange { pub(crate) num_blocks: u32, } -#[derive_binread] +#[binread] #[derive(Clone, Debug, PartialEq)] #[br(magic = b"EGGS", assert(end_magic == * b"SGGE"))] pub(crate) struct NFSHeader { diff --git a/src/lib.rs b/src/lib.rs index 97a41d4..1368948 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ //! # Examples //! //! Opening a disc image and reading a file: -//! ``` +//! ```no_run //! use nod::disc::{new_disc_base, PartHeader}; //! use nod::fst::NodeType; //! use nod::io::new_disc_io; @@ -23,8 +23,9 @@ //! if let Some(NodeType::File(node)) = header.find_node("/MP3/Worlds.txt") { //! let mut s = String::new(); //! partition.begin_file_stream(node)?.read_to_string(&mut s); -//! println!(s); +//! println!("{}", s); //! } +//! # Ok::<(), nod::Error>(()) //! ``` use thiserror::Error; @@ -36,7 +37,7 @@ pub mod streams; #[derive(Error, Debug)] pub enum Error { #[error("binary format")] - BinaryFormat(#[from] binread::Error), + BinaryFormat(#[from] binrw::Error), #[error("encryption")] Encryption(#[from] block_modes::BlockModeError), #[error("io error: `{0}`")] diff --git a/src/streams.rs b/src/streams.rs index c5b873c..9eadd34 100644 --- a/src/streams.rs +++ b/src/streams.rs @@ -59,11 +59,7 @@ impl ReadStream for File { // Try to restore position even if the above failed let seek_result = self.seek(SeekFrom::Start(before)); if seek_result.is_err() { - return if result.is_err() { - result - } else { - seek_result - } + return if result.is_err() { result } else { seek_result }; } result }