-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from soulsmods/feat/better-dcx
DCX parser rework
- Loading branch information
Showing
10 changed files
with
328 additions
and
146 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
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 was deleted.
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::io::{self, Read}; | ||
|
||
use flate2::read::ZlibDecoder; | ||
|
||
pub struct DcxDecoderDeflate<'a>(ZlibDecoder<&'a [u8]>); | ||
|
||
impl<'a> DcxDecoderDeflate<'a> { | ||
pub fn from_buffer(buf: &'a [u8]) -> Self { | ||
Self(ZlibDecoder::new(buf)) | ||
} | ||
} | ||
|
||
impl<'a> Read for DcxDecoderDeflate<'a> { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
self.0.read(buf) | ||
} | ||
} |
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,65 @@ | ||
use std::io::{Cursor, Error, ErrorKind, Read, Result}; | ||
|
||
use byteorder::BE; | ||
use oodle_sys::{ | ||
OodleLZ_Decode_ThreadPhase_OodleLZ_Decode_ThreadPhaseAll, OodleLZ_Decompress, OODLELZ_FAILED, | ||
}; | ||
use zerocopy::U32; | ||
|
||
pub struct DcxDecoderKraken<'a> { | ||
compressed: &'a [u8], | ||
uncompressed_size: U32<BE>, | ||
inner_cursor: Option<Cursor<Vec<u8>>>, | ||
} | ||
|
||
impl<'a> DcxDecoderKraken<'a> { | ||
pub fn from_buffer(buf: &'a [u8], uncompressed_size: U32<BE>) -> Self { | ||
Self { | ||
compressed: buf, | ||
uncompressed_size, | ||
inner_cursor: None, | ||
} | ||
} | ||
} | ||
impl<'a> Read for DcxDecoderKraken<'a> { | ||
// TODO: implement somewhat incremental reading by working with oodle's | ||
// blocks per example in docs. | ||
// It currently just decompresses the entire input one go and then | ||
// operates a Cursor wrapping the decompressed bytes. | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
if self.inner_cursor.is_none() { | ||
let mut inner_buffer = vec![0u8; self.uncompressed_size.get() as usize]; | ||
let compressed_len = | ||
isize::try_from(inner_buffer.len()).map_err(|e| Error::new(ErrorKind::Other, e))?; | ||
let inner_buffer_len = | ||
isize::try_from(inner_buffer.len()).map_err(|e| Error::new(ErrorKind::Other, e))?; | ||
|
||
let result = unsafe { | ||
OodleLZ_Decompress( | ||
self.compressed.as_ptr() as *const _, | ||
compressed_len, | ||
inner_buffer.as_mut_ptr() as *mut _, | ||
inner_buffer_len, | ||
oodle_sys::OodleLZ_FuzzSafe_OodleLZ_FuzzSafe_Yes, | ||
0, | ||
0, | ||
std::ptr::null_mut(), | ||
0, | ||
None, | ||
std::ptr::null_mut(), | ||
std::ptr::null_mut(), | ||
0, | ||
OodleLZ_Decode_ThreadPhase_OodleLZ_Decode_ThreadPhaseAll, | ||
) as usize | ||
}; | ||
|
||
if result == OODLELZ_FAILED as usize { | ||
return Err(Error::from(ErrorKind::Other)); | ||
} | ||
|
||
self.inner_cursor = Some(Cursor::new(inner_buffer)); | ||
} | ||
|
||
self.inner_cursor.as_mut().unwrap().read(buf) | ||
} | ||
} |
Oops, something went wrong.