compatible with v1 keystore

This commit is contained in:
Jimmy-Z 2023-08-20 01:08:57 +08:00
parent 971ed05b58
commit c6e44e9078

View file

@ -2,14 +2,14 @@ use std::{
borrow::Cow, borrow::Cow,
cmp::Ordering, cmp::Ordering,
fs::File, fs::File,
path::Path,
io::{Read, Seek}, io::{Read, Seek},
mem::size_of, mem::size_of,
path::Path,
str::from_utf8, str::from_utf8,
}; };
use crate::{ use crate::{
abi_utils::{TransmuteSafe, LE32, read_vec}, abi_utils::{read_vec, TransmuteSafe, LE32},
Error, Error,
}; };
@ -19,27 +19,42 @@ mod abi {
#[repr(C)] #[repr(C)]
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub(super) struct FileHeader { pub(super) struct FileHeader {
magic0: LE32, pub ver: LE32,
magic1: LE32, magic1: LE32,
pub words_offset: LE32, pub words_offset: LE32,
pub idx_offset: LE32, pub idx_offset: LE32,
pub next_offset: LE32, // Jimmy-Z: no idea what this is // Jimmy-Z: no idea what this is
// present on (not limited to) OALD10, SANKOKU8
pub next_offset: LE32,
magic5: LE32, magic5: LE32,
magic6: LE32, magic6: LE32,
magic7: LE32, magic7: LE32,
} }
impl FileHeader { impl FileHeader {
pub(super) fn validate(&self) -> Result<(), Error> { pub(super) fn from(r: &mut impl Read) -> Result<Self, Error> {
if self.magic0.read() == 0x20000 let mut h = FileHeader::default();
&& self.magic1.read() == 0 r.read_exact(&mut h.as_bytes_mut()[..0x10])?;
&& self.magic5.read() == 0 if h.ver.read() == 0x10000 && h.words_offset.read() == 0x10{
&& self.magic6.read() == 0 } else if h.ver.read() == 0x20000 && h.words_offset.read() == 0x20 {
&& self.magic7.read() == 0 r.read_exact(&mut h.as_bytes_mut()[0x10..])?;
&& self.words_offset.us() < self.idx_offset.us() } else {
&& (self.next_offset.read() == 0 || self.idx_offset.us() < self.next_offset.us()) return Err(Error::KeyFileHeaderValidate)
}
if h.ver.read() == 0x10000
&& h.magic1.read() == 0
&& h.words_offset.us() < h.idx_offset.us()
{ {
Ok(()) Ok(h)
} else if h.ver.read() == 0x20000
&& h.magic1.read() == 0
&& h.magic5.read() == 0
&& h.magic6.read() == 0
&& h.magic7.read() == 0
&& h.words_offset.us() < h.idx_offset.us()
&& (h.next_offset.read() == 0 || h.idx_offset.us() < h.next_offset.us())
{
Ok(h)
} else { } else {
Err(Error::KeyFileHeaderValidate) Err(Error::KeyFileHeaderValidate)
} }
@ -122,9 +137,7 @@ impl Keys {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Keys, Error> { pub fn new<P: AsRef<Path>>(path: P) -> Result<Keys, Error> {
let mut file = File::open(path)?; let mut file = File::open(path)?;
let file_size = file.metadata()?.len() as usize; let file_size = file.metadata()?.len() as usize;
let mut hdr = FileHeader::default(); let hdr = FileHeader::from(&mut file)?;
file.read_exact(hdr.as_bytes_mut())?;
hdr.validate()?;
file.seek(std::io::SeekFrom::Start(hdr.words_offset.read() as u64))?; file.seek(std::io::SeekFrom::Start(hdr.words_offset.read() as u64))?;
let words = read_vec(&mut file, hdr.words_offset.us(), hdr.idx_offset.us())?; let words = read_vec(&mut file, hdr.words_offset.us(), hdr.idx_offset.us())?;