mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-05 11:30:47 -05:00
magiskboot: migrate check_fmt to Rust
Migrate check_fmt and guess_lzma to Rust in format.rs. Assisted-by: Gemini 3.6 Flash
This commit is contained in:
@@ -102,3 +102,73 @@ pub fn fmt_compressed(fmt: FileFormat) -> bool {
|
||||
pub fn fmt_compressed_any(fmt: FileFormat) -> bool {
|
||||
fmt.is_compressed() || matches!(fmt, FileFormat::LZOP)
|
||||
}
|
||||
|
||||
const CHROMEOS_MAGIC: &[u8] = b"CHROMEOS";
|
||||
const BOOT_MAGIC: &[u8] = b"ANDROID!";
|
||||
const VENDOR_BOOT_MAGIC: &[u8] = b"VNDRBOOT";
|
||||
const GZIP1_MAGIC: &[u8] = b"\x1f\x8b";
|
||||
const GZIP2_MAGIC: &[u8] = b"\x1f\x9e";
|
||||
const LZOP_MAGIC: &[u8] = b"\x89LZO";
|
||||
const XZ_MAGIC: &[u8] = b"\xfd7zXZ";
|
||||
const BZIP_MAGIC: &[u8] = b"BZh";
|
||||
const LZ41_MAGIC: &[u8] = b"\x03\x21\x4c\x18";
|
||||
const LZ42_MAGIC: &[u8] = b"\x04\x22\x4d\x18";
|
||||
const LZ4_LEG_MAGIC: &[u8] = b"\x02\x21\x4c\x18";
|
||||
const MTK_MAGIC: &[u8] = b"\x88\x16\x88\x58";
|
||||
const DTB_MAGIC: &[u8] = b"\xd0\x0d\xfe\xed";
|
||||
const DHTB_MAGIC: &[u8] = b"\x44\x48\x54\x42\x01\x00\x00\x00";
|
||||
const TEGRABLOB_MAGIC: &[u8] = b"-SIGNED-BY-SIGNBLOB-";
|
||||
const ZIMAGE_MAGIC: &[u8] = b"\x18\x28\x6f\x01";
|
||||
|
||||
fn guess_lzma(buf: &[u8]) -> bool {
|
||||
// 0 : (pb * 5 + lp) * 9 + lc
|
||||
// 1 - 4 : dict size, must be 2^n
|
||||
// 5 - 12: all 0xFF
|
||||
if buf.len() <= 13 || buf[0] != 0x5d {
|
||||
return false;
|
||||
}
|
||||
let dict_sz = match buf[1..5].try_into() {
|
||||
Ok(bytes) => u32::from_le_bytes(bytes),
|
||||
Err(_) => return false,
|
||||
};
|
||||
if dict_sz == 0 || (dict_sz & (dict_sz - 1)) != 0 {
|
||||
return false;
|
||||
}
|
||||
buf[5..13] == [0xff; 8]
|
||||
}
|
||||
|
||||
pub fn check_fmt(buf: &[u8]) -> FileFormat {
|
||||
if buf.starts_with(CHROMEOS_MAGIC) {
|
||||
FileFormat::CHROMEOS
|
||||
} else if buf.starts_with(BOOT_MAGIC) {
|
||||
FileFormat::AOSP
|
||||
} else if buf.starts_with(VENDOR_BOOT_MAGIC) {
|
||||
FileFormat::AOSP_VENDOR
|
||||
} else if buf.starts_with(GZIP1_MAGIC) || buf.starts_with(GZIP2_MAGIC) {
|
||||
FileFormat::GZIP
|
||||
} else if buf.starts_with(LZOP_MAGIC) {
|
||||
FileFormat::LZOP
|
||||
} else if buf.starts_with(XZ_MAGIC) {
|
||||
FileFormat::XZ
|
||||
} else if guess_lzma(buf) {
|
||||
FileFormat::LZMA
|
||||
} else if buf.starts_with(BZIP_MAGIC) {
|
||||
FileFormat::BZIP2
|
||||
} else if buf.starts_with(LZ41_MAGIC) || buf.starts_with(LZ42_MAGIC) {
|
||||
FileFormat::LZ4
|
||||
} else if buf.starts_with(LZ4_LEG_MAGIC) {
|
||||
FileFormat::LZ4_LEGACY
|
||||
} else if buf.starts_with(MTK_MAGIC) {
|
||||
FileFormat::MTK
|
||||
} else if buf.starts_with(DTB_MAGIC) {
|
||||
FileFormat::DTB
|
||||
} else if buf.starts_with(DHTB_MAGIC) {
|
||||
FileFormat::DHTB
|
||||
} else if buf.starts_with(TEGRABLOB_MAGIC) {
|
||||
FileFormat::BLOB
|
||||
} else if buf.len() >= 0x28 && &buf[0x24..0x28] == ZIMAGE_MAGIC {
|
||||
FileFormat::ZIMAGE
|
||||
} else {
|
||||
FileFormat::UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user