diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index bc09afc..da26cf4 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -6,6 +6,9 @@ jobs: run: name: Run runs-on: ubuntu-latest + strategy: + matrix: + features: [--all-features, --no-default-features] steps: - name: Install Rust stable uses: actions-rs/toolchain@v1 @@ -22,16 +25,16 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --workspace + args: --workspace ${{ matrix.features }} - name: Lint uses: actions-rs/cargo@v1 with: command: clippy - args: --workspace -- -D warnings + args: --workspace ${{ matrix.features }} -- -D warnings - name: Test uses: actions-rs/cargo@v1 with: command: test - args: --workspace + args: --workspace ${{ matrix.features }} diff --git a/Cargo.toml b/Cargo.toml index 7fcefd9..edd76eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,11 +9,15 @@ repository = "https://github.com/mlafeldt/codebreaker-rs" documentation = "https://docs.rs/codebreaker/" homepage = "https://crates.io/crates/codebreaker" keywords = ["codebreaker", "ps2", "gamehacking", "homebrew"] -categories = ["algorithms", "cryptography"] +categories = ["algorithms", "cryptography", "no-std"] edition = "2018" [lib] name = "codebreaker" [dependencies] -num-bigint = "^0.2.4" +num-bigint = "0.3" + +[features] +default = ["std"] +std = ["num-bigint/std"] diff --git a/src/cb1.rs b/src/cb1.rs index cd2794d..7dcf243 100644 --- a/src/cb1.rs +++ b/src/cb1.rs @@ -98,6 +98,7 @@ const SEEDS: [[u32; 16]; 3] = [ mod tests { use super::*; use crate::code; + use crate::std_alloc::Vec; struct Test { decrypted: &'static str, diff --git a/src/cb7.rs b/src/cb7.rs index 1621972..36c888f 100644 --- a/src/cb7.rs +++ b/src/cb7.rs @@ -1,10 +1,11 @@ //! Encrypt and decrypt cheat codes for CodeBreaker PS2 v7+. use crate::rc4::Rc4; +use crate::std_alloc::Vec; -use std::fmt; -use std::mem::size_of; -use std::slice; +use core::fmt; +use core::mem::size_of; +use core::slice; /// A processor for CB v7+ codes. #[derive(Clone, Copy)] @@ -466,6 +467,7 @@ const SEEDS: [[u8; 256]; 5] = [ mod tests { use super::*; use crate::code; + use crate::std_alloc::Vec; fn mul_tests() -> Vec<(u32, u32, u32)> { vec![ diff --git a/src/lib.rs b/src/lib.rs index 619915a..05f0951 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,30 @@ #![deny(clippy::all, clippy::nursery)] #![deny(nonstandard_style, rust_2018_idioms)] #![deny(missing_docs, missing_debug_implementations)] +#![no_std] + +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[cfg(feature = "std")] +mod std_alloc { + #[cfg(test)] + pub use std::string::String; + pub use std::vec::Vec; +} + +#[cfg(not(feature = "std"))] +#[allow(unused_imports)] +#[macro_use] +extern crate alloc; + +#[cfg(not(feature = "std"))] +mod std_alloc { + #[cfg(test)] + pub use alloc::string::String; + pub use alloc::vec::Vec; +} pub mod cb1; pub mod cb7; @@ -286,6 +310,7 @@ fn num_code_lines(addr: u32) -> usize { #[cfg(test)] mod tests { use super::*; + use crate::std_alloc::Vec; struct Test { cb: Codebreaker, @@ -491,7 +516,9 @@ mod tests { #[cfg(test)] mod code { - pub(crate) fn parse(line: &str) -> (u32, u32) { + use crate::std_alloc::{String, Vec}; + + pub fn parse(line: &str) -> (u32, u32) { let code: Vec = line .splitn(2, ' ') .map(|v| u32::from_str_radix(v, 16).unwrap()) @@ -499,7 +526,7 @@ mod code { (code[0], code[1]) } - pub(crate) fn format(code: (u32, u32)) -> String { + pub fn format(code: (u32, u32)) -> String { format!("{:08X} {:08X}", code.0, code.1) } } diff --git a/src/rc4.rs b/src/rc4.rs index 09bb8e7..15fd827 100644 --- a/src/rc4.rs +++ b/src/rc4.rs @@ -38,6 +38,7 @@ impl Rc4 { #[cfg(test)] mod tests { use super::*; + use crate::std_alloc::Vec; struct Test { key: &'static str,