Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Rust rewrite #13

Closed
wants to merge 120 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
c6787ce
First working code to encrypt CB1 codes
mlafeldt May 27, 2015
f98ae89
Return encrypted code
mlafeldt May 27, 2015
b5d450c
Fix arithmetic overflow
mlafeldt May 27, 2015
53a5f08
Implement code decryption
mlafeldt May 27, 2015
66a200d
Use Cargo
mlafeldt Jun 11, 2015
a4bc829
Test Rust code on Travis
mlafeldt Jun 11, 2015
2f6bc4d
Add trailing commas
mlafeldt Jun 19, 2015
5afd9ff
Build a CLI tool
mlafeldt Jun 22, 2015
3a20740
Dedicated namespace for CB v1 scheme
mlafeldt Jun 22, 2015
a4b6a3e
Add skeleton for CB v7 scheme
mlafeldt Jun 22, 2015
5194895
Add v7 scheme constants
mlafeldt Jun 22, 2015
99f5f3e
Port mul_inverse() to Rust
mlafeldt Jun 23, 2015
340fb14
Port mul_encrypt/decrypt to Rust
mlafeldt Jun 23, 2015
7188673
Add missing constant
mlafeldt Jun 24, 2015
088c464
Extract mul_tests
mlafeldt Jun 24, 2015
4ed6940
Get rid of compiler warnings
mlafeldt Jun 24, 2015
edeb854
Working RC4 implementation
mlafeldt Jun 24, 2015
9f17406
rustfmt
mlafeldt Dec 1, 2015
1911a70
Add TODOs
mlafeldt Dec 1, 2015
2353482
Binaries should include Cargo.lock
mlafeldt Dec 1, 2015
ce29765
First working version of extract_cheats
mlafeldt Oct 23, 2016
48b5b89
Basic CB1 decryption support
mlafeldt Oct 24, 2016
c6f7bbb
Basic option handling
mlafeldt Oct 24, 2016
6f5c466
Run passing tests on Travis
mlafeldt Oct 24, 2016
1618f7e
Invoke C crypto functions from Rust
mlafeldt Oct 25, 2016
1d2dda6
Make cheats-extract-and-decrypt.t pass
mlafeldt Oct 25, 2016
682ce8e
Fix warning: value assigned to `fix_beef` is never read
mlafeldt Oct 25, 2016
59cdc85
Use clap for CLI
mlafeldt Oct 25, 2016
8a904a9
Avoid index out of bounds
mlafeldt Oct 25, 2016
d932f08
Restructure code
mlafeldt Oct 26, 2016
67cf2a9
rustfmt rfc
mlafeldt Jun 7, 2017
792c24a
cargo update
mlafeldt Jun 17, 2017
1d4907d
cargo update
mlafeldt Jan 23, 2019
55a9622
Switch to new cc crate
mlafeldt Jan 23, 2019
394a655
cargo fmt
mlafeldt Jan 23, 2019
df834e0
cargo update
mlafeldt Apr 30, 2020
902ae94
rustfmt
mlafeldt Apr 30, 2020
09bc9f3
Fix deprecation warnings
mlafeldt Apr 30, 2020
ab5d248
Merge remote-tracking branch 'origin/master' into rust
mlafeldt Apr 30, 2020
3ced6c7
Use super glob
mlafeldt Apr 30, 2020
73a7923
Run crypto tests
mlafeldt Apr 30, 2020
52351e9
Allow to call cb_encrypt_code and cb_set_common_v7 from Rust
mlafeldt May 1, 2020
3bf9eef
Merge crypto mod with codebreaker crate
mlafeldt May 1, 2020
d18e043
Don't run doc tests
mlafeldt May 1, 2020
48e5f49
Expose CB file functions
mlafeldt May 1, 2020
dbb2670
Expose CB V7 functions
mlafeldt May 1, 2020
03f6363
Revert me: export internal C variables for incremental porting
mlafeldt May 1, 2020
dd8c8ff
Let sharness show diff
mlafeldt May 1, 2020
3a57289
Port common code functions to (unsafe) Rust
mlafeldt May 1, 2020
46fe7ff
Use already ported CB v1 functions
mlafeldt May 1, 2020
a544fea
Make all hex lower case
mlafeldt May 1, 2020
5b2ff62
Let rc4.encrypt modify data directly
mlafeldt May 1, 2020
d338624
Revert me: expose my C stuff
mlafeldt May 1, 2020
864d426
Implement first working version of cb7::beefcode()
mlafeldt May 1, 2020
c3656c2
Introduce Vec<usize>
mlafeldt May 2, 2020
c24fab9
copy_from_slice() seems unnecessary
mlafeldt May 2, 2020
4f1439d
slice_to_u8_mut ftw
mlafeldt May 2, 2020
4602eaf
Add (decrypt|encrypt)_code_mut for convenience
mlafeldt May 2, 2020
06256dd
RC4 tweaks
mlafeldt May 2, 2020
4eb7366
Simplify tests
mlafeldt May 2, 2020
5667ce4
Revert "copy_from_slice() seems unnecessary"
mlafeldt May 2, 2020
aa928d8
Make all cb7 functions work
mlafeldt May 2, 2020
e96b88f
Remove superfluous super::
mlafeldt May 2, 2020
c4266b5
Rename constants
mlafeldt May 2, 2020
0641eef
Port cb7::decrypt_code to (almost) pure Rust
mlafeldt May 3, 2020
426c9f0
Add slice_to_u8()
mlafeldt May 3, 2020
2b0c600
Increase max width
mlafeldt May 3, 2020
3322178
Add RSA module
mlafeldt May 3, 2020
15ae0e9
Port cb7::encrypt_code to (almost) pure Rust
mlafeldt May 3, 2020
10f4716
Clean up imports
mlafeldt May 3, 2020
b70384c
Only use RSA code via FFI
mlafeldt May 3, 2020
b491602
Use Rust 2018 edition
mlafeldt May 3, 2020
f3f7313
Add Cargo descriptions
mlafeldt May 3, 2020
470c621
Remove superfluous oldkey which is always the same as key
mlafeldt May 3, 2020
c51f49f
Simplify beefcode()
mlafeldt May 3, 2020
e1902fe
Use Rust bool type
mlafeldt May 4, 2020
b8513c6
Simplify v7_init check
mlafeldt May 4, 2020
4f15612
Remove superfluous parentheses
mlafeldt May 4, 2020
72fcb64
Apply math
mlafeldt May 4, 2020
590ab48
Add is_beefcode()
mlafeldt May 4, 2020
296fec4
Port rsa_crypt to pure Rust
mlafeldt May 4, 2020
4f53d2a
Switch to num-bigint crate
mlafeldt May 4, 2020
d2a803f
Revert "Revert me: expose my C stuff"
mlafeldt May 4, 2020
7bda1fc
Revert "Revert me: export internal C variables for incremental porting"
mlafeldt May 4, 2020
14cc672
Merge remote-tracking branch 'origin/master' into rust
mlafeldt May 4, 2020
94e97da
to_u32_digits was a recent addition to num-bigint
mlafeldt May 4, 2020
b834d6d
Introduce Context structs and remove unsafe blocks
mlafeldt May 5, 2020
ff801c3
Rename root struct
mlafeldt May 5, 2020
7ee6fa1
Move slice functions to where they belong
mlafeldt May 5, 2020
e68f906
Add first unit tests for V7 scheme
mlafeldt May 6, 2020
21eafff
Move initialization where it belongs
mlafeldt May 6, 2020
a92771f
EncMode -> Scheme
mlafeldt May 6, 2020
5f2503b
decrypt_code2 -> auto_decrypt_code
mlafeldt May 6, 2020
a6be65c
Let cb7 module handle beefc0df
mlafeldt May 6, 2020
6894f01
Remove unused FFI code
mlafeldt May 6, 2020
05212c1
Tweak comment
mlafeldt May 6, 2020
0e59474
Tweak read16/32()
mlafeldt May 6, 2020
3e843fa
Add parse/format_code helpers
mlafeldt May 6, 2020
da29048
Add test
mlafeldt May 7, 2020
c83db0e
Context -> Cb7
mlafeldt May 7, 2020
68f4288
Move constants
mlafeldt May 7, 2020
7b27524
BigUint is only used in rsa_crypt()
mlafeldt May 7, 2020
fe85f2b
Make encrypt/decrypt interface consistent
mlafeldt May 7, 2020
d328cae
Make Clippy happier
mlafeldt May 7, 2020
c520769
Implement Default trait (Clippy)
mlafeldt May 8, 2020
1383e2a
Collapse else if block (Clippy)
mlafeldt May 8, 2020
8975e6e
Add ZERO_SEEDS constant
mlafeldt May 8, 2020
66c7a11
Add slice_to_u32()
mlafeldt May 8, 2020
d2cbec8
Make Clippy happy
mlafeldt May 8, 2020
9445100
Remove superfluous "extern create"
mlafeldt May 8, 2020
53b1070
Add Clippy to CI
mlafeldt May 8, 2020
cbfa0cd
Set edition in rustfmt config
mlafeldt May 8, 2020
92d1aed
Move is_beefcode()
mlafeldt May 8, 2020
430d751
Show full backtraces in CI
mlafeldt May 8, 2020
3627f1f
Let Cb7::default decrypt common V7 codes
mlafeldt May 8, 2020
3bbeadd
Add new_v7 constructor
mlafeldt May 8, 2020
5ff0bf4
Extract codebreaker crate
mlafeldt May 8, 2020
a711b66
Use local codebreaker crate during development
mlafeldt May 15, 2020
57fbcfc
cargo publish
mlafeldt May 15, 2020
fcaba11
Update codebreaker crate
mlafeldt Jul 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/build/
/cb2util
/release/
# Rust
/target
20 changes: 11 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
language: c
language: rust

sudo: false

compiler:
- gcc
- clang

install: make CC=$CC install
rust: stable

env:
- TEST_OPTS=-v
- DEFAULT_TEST_TARGET=prove
- RUST_BACKTRACE=full

before_script:
- rustup component add clippy

script: make test
script:
- cargo build
- cargo clippy -- -D warnings
- cargo test --verbose
- make -C test SHARNESS_BUILD_DIRECTORY="$PWD/target/debug" T="cheats-extract*" TEST_OPTS=-v

branches:
only:
Expand Down
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "cb2util"
version = "0.0.1"
description = "CodeBreaker PS2 File Utility"
authors = ["Mathias Lafeldt <mathias.lafeldt@gmail.com>"]
license = "MIT"
repository = "https://github.com/mlafeldt/cb2util"
documentation = "https://github.com/mlafeldt/cb2util"
edition = "2018"
include = ["src/**/*"]

[dependencies]
clap = "2"
flate2 = "0.2"
codebreaker = { path = "../codebreaker-rs", version = "0.2" }
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 120
edition = "2018"
119 changes: 119 additions & 0 deletions src/cli/cheats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use std::fs::File;
use std::io::Read;
use std::path::Path;

use clap::ArgMatches;
use flate2::read::ZlibDecoder;

use codebreaker::{cb7, Codebreaker};

pub fn run(m: &ArgMatches) {
let path = Path::new(m.value_of("INPUT").unwrap());
let mut file = match File::open(path) {
Err(why) => abort!("couldn't open {}: {}", path.display(), why),
Ok(file) => file,
};

let mut buf: Vec<u8> = Vec::new();
file.read_to_end(&mut buf).unwrap();

assert!(buf.len() > CHEATS_HEADER_SIZE);
assert!(&buf[0..4] == CHEATS_FILE_ID);

let mut decoder = ZlibDecoder::new(&buf[CHEATS_HEADER_SIZE..]);
let mut unpack: Vec<u8> = Vec::new();
decoder.read_to_end(&mut unpack).unwrap();

let decrypt = m.is_present("decrypt");
extract_cheats(&unpack[..], decrypt)
}

fn extract_cheats(buf: &[u8], decrypt: bool) {
let mut i = 0;
while i < buf.len() - 2 {
// Check for end marker
if read16(&buf[i..i + 2]) == 0xffff {
break;
}

// Reset code encryption
let mut cb = Codebreaker::new();
let mut beefcodf = false;
let mut fix_beef = 0;

if i > 0 {
println!();
}

// Process game title
// Example: "007 Agent Under Fire\0"
let title = cstring(&buf[i..]);
println!("\"{}\"", title);
i += title.len() + 1;

let numdesc = read16(&buf[i..i + 2]);
i += 2;

// Process code description(s)
// Example: "Infinite Ammo\0"
for _ in 0..numdesc {
let desc = cstring(&buf[i..]);
println!("{}", desc);
i += desc.len() + 2;

let numlines = read16(&buf[i..i + 2]);
i += 2;

// Process code line(s)
// Example: 1A3EDED4 000003E7
for _ in 0..numlines {
let mut addr = read32(&buf[i..i + 4]);
let mut val = read32(&buf[i + 4..i + 8]);
i += 8;

// Decrypt code
if decrypt {
cb.auto_decrypt_code_mut(&mut addr, &mut val);
}

// Discard beefcode and other junk
if beefcodf {
beefcodf = false;
continue;
}
if fix_beef != 0 && (addr == 0x000f_fffe || addr == 0x000f_ffff) {
fix_beef -= 1;
continue;
}
if cb7::is_beefcode(addr) {
beefcodf = addr & 1 != 0;
fix_beef = 2;
continue;
} else {
beefcodf = false;
fix_beef = 0;
}

println!("{:08X} {:08X}", addr, val);
}
}
}
}

fn cstring(b: &[u8]) -> String {
let eos = b.iter().position(|&x| x == 0).unwrap();
String::from_utf8_lossy(&b[..eos]).into_owned()
}

fn read32(b: &[u8]) -> u32 {
assert!(b.len() == 4);
u32::from(b[0]) | (u32::from(b[1]) << 8) | (u32::from(b[2]) << 16) | (u32::from(b[3]) << 24)
}

fn read16(b: &[u8]) -> u16 {
assert!(b.len() == 2);
u16::from(b[0]) | (u16::from(b[1]) << 8)
}

const CHEATS_HEADER_SIZE: usize = 8;
const CHEATS_FILE_ID: &[u8; 4] = b"CFU\0";
12 changes: 12 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Based on http://is.gd/8TYNHp
macro_rules! abort {
($($arg:tt)*) => (
{
use ::std::io::prelude::*;
write!(&mut ::std::io::stderr(), "error: {}\n", format_args!($($arg)*)).unwrap();
::std::process::exit(1);
}
)
}

pub mod cheats;
Loading