-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added basic HSSP 1-3 functionality
- Loading branch information
Showing
12 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ name = 'corelib' | |
[dependencies] | ||
chrono = "0.4.38" | ||
crc32fast = "1.4.2" | ||
murmur3 = "0.5.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pub mod parser; | ||
|
||
#[derive(Debug)] | ||
pub struct HsspMetadata { | ||
pub version: u8, | ||
pub checksum: u32, | ||
pub encryption: Option<HsspEncryption>, | ||
pub files: Vec<HsspFileEntry>, | ||
pub has_main: bool, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct HsspEncryption { | ||
pub hash: [u8; 32], | ||
pub iv: [u8; 16], | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct HsspFileEntry { | ||
pub name: String, | ||
pub offset: u64, | ||
pub size: u64, | ||
pub is_main: bool, | ||
pub is_directory: bool, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::{file::FileReader, helpers::hash::murmur3}; | ||
|
||
use super::{HsspEncryption, HsspFileEntry, HsspMetadata}; | ||
|
||
pub fn metadata(file: &mut FileReader) -> HsspMetadata { | ||
let mut version = 2; | ||
let magic = file.read_utf8(&4); | ||
if magic == "SFA\0" { | ||
version = 1; | ||
} | ||
let checksum = file.read_u32le(); | ||
let file_count = file.read_u32le(); | ||
let pwd_hash: [u8; 32] = file.read_u8array(&32).try_into().unwrap(); | ||
let iv: [u8; 16] = file.read_u8array(&16).try_into().unwrap(); | ||
let main = file.read_u32le(); | ||
if version == 2 { | ||
let bytes = file.read_u128le(); | ||
if bytes == 0 { | ||
version = 3; | ||
} else { | ||
file.jump(&-16); | ||
} | ||
} | ||
|
||
let encrypted = !(pwd_hash == [0; 32] && iv == [0; 16]); // TODO: handle encryption | ||
|
||
let mut files = Vec::new(); | ||
|
||
for idx in 0..file_count { | ||
let size = file.read_u64le(); | ||
println!("size: {}", size); | ||
let name_len = file.read_u16le(); | ||
println!("name_len: {}", name_len); | ||
let mut name = file.read_utf8(&(name_len as u64)); | ||
let is_directory = name.starts_with("//"); | ||
if is_directory { | ||
name = name[2..].to_string(); | ||
} | ||
let offset = file.get_position(); | ||
files.push(HsspFileEntry { | ||
name, | ||
offset, | ||
size, | ||
is_main: idx + 1 == main, | ||
is_directory, | ||
}); | ||
file.jump(&(size as i128 + name_len as i128)); // that actually was a bug | ||
} | ||
|
||
HsspMetadata { | ||
version, | ||
checksum, | ||
encryption: if encrypted { | ||
Some(HsspEncryption { hash: pwd_hash, iv }) | ||
} else { | ||
None | ||
}, | ||
files, | ||
has_main: main != 0, | ||
} | ||
} | ||
|
||
pub fn check_integrity_all(file: &mut FileReader, metadata: &HsspMetadata) -> bool { | ||
let offset = if metadata.version > 2 { 128 } else { 64 }; | ||
if murmur3::hash(file, &offset, &(file.get_size() - offset), &822616071) == metadata.checksum { | ||
return true; | ||
} | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod crc32; | ||
pub mod murmur3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use crate::file::FileReader; | ||
use murmur3::murmur3_32; | ||
|
||
// Buffer size is fixed to 4 Bytes | ||
pub fn hash(file: &mut FileReader, offset: &u64, size: &u64, seed: &u32) -> u32 { | ||
let pos_before = file.get_position(); | ||
file.seek(offset); | ||
let mut reader = file.set_end(&(offset + size)); | ||
let result = murmur3_32(&mut reader, *seed).unwrap(); | ||
file.seek(&pos_before); | ||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use corelib::file::FileReader; | ||
|
||
#[test] | ||
fn sample_000() { | ||
let mut file = FileReader::new(&"tests/samples/hssp/000.hssp".to_string()); | ||
|
||
let metadata = corelib::formats::hssp::parser::metadata(&mut file); | ||
|
||
assert_eq!(metadata.version, 2); | ||
assert!(metadata.encryption.is_none()); | ||
assert_eq!(metadata.files.len(), 1); | ||
assert_eq!(metadata.files[0].name, "test.txt"); | ||
assert!(!metadata.files[0].is_main); | ||
assert!(!metadata.files[0].is_directory); | ||
assert!(!metadata.has_main); | ||
|
||
assert!(corelib::formats::hssp::parser::check_integrity_all( | ||
&mut file, &metadata | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.