Skip to content
Snippets Groups Projects
Commit 216b4e4a authored by Loïc's avatar Loïc
Browse files

inflate-rs: add first Rust module

parent 2bbef04b
No related branches found
No related tags found
No related merge requests found
Pipeline #276677 passed with stage
in 17 minutes and 44 seconds
......@@ -15,12 +15,27 @@ stream_filter_LTLIBRARIES += libdecomp_plugin.la
endif
endif
if HAVE_RUST
.PHONY: libinflate_rs.la # Cargo does the incremental compilation
libinflate_rs.la:
@$(LIBTOOL_CARGO) stream_filter/inflate-rs/ $@
libinflate_rs_plugin_la_SOURCES = $(NULL)
libinflate_rs_plugin_la_LIBADD = libinflate_rs.la
stream_filter_LTLIBRARIES += libinflate_rs_plugin.la
else
libinflate_plugin_la_SOURCES = stream_filter/inflate.c
libinflate_plugin_la_LIBADD = -lz
if HAVE_ZLIB
stream_filter_LTLIBRARIES += libinflate_plugin.la
endif
endif
libprefetch_plugin_la_SOURCES = stream_filter/prefetch.c
if !HAVE_WINSTORE
stream_filter_LTLIBRARIES += libprefetch_plugin.la
......
[package]
name = "inflate-rs"
version = "0.1.0"
edition = "2021"
[features]
capi = []
[dependencies]
flate2 = "1.0"
vlcrs-core = { path = "../../../src/vlcrs-core" }
vlcrs-core-macros = { path = "../../../src/vlcrs-core-macros" }
use std::io::Read;
use flate2::read::{GzDecoder, ZlibDecoder};
use vlcrs_core::error::CoreError;
use vlcrs_core::module::stream_filter::{Module, Registration, ThisModule};
use vlcrs_core::stream::{BorrowedStream, ReadStream, StreamControl};
use vlcrs_core_macros::module;
pub struct Inflate {
_gz: Option<Registration<GzStreamReader>>,
_zlib: Option<Registration<ZlibStreamReader>>,
}
struct GzStreamReader {
gz_decoder: GzDecoder<BorrowedStream>,
}
struct ZlibStreamReader {
zlib_decoder: ZlibDecoder<BorrowedStream>,
}
impl Module for Inflate {
fn init(
mut source: BorrowedStream,
this_module: ThisModule,
) -> vlcrs_core::error::Result<Self> {
let buf = source.peek(2)?;
match buf.buf() {
[0xF8, _] => {
let zlib = ZlibStreamReader::new(source);
Ok(Inflate {
_zlib: Some(Registration::new(this_module).register(zlib)?),
_gz: None,
})
}
[0x1F, 0x8B] => {
let gz = GzStreamReader::new(source);
Ok(Inflate {
_gz: Some(Registration::new(this_module).register(gz)?),
_zlib: None,
})
}
_ => Err(CoreError::Unknown),
}
}
}
impl GzStreamReader {
fn new(stream: BorrowedStream) -> GzStreamReader {
GzStreamReader {
gz_decoder: GzDecoder::new(stream),
}
}
}
impl ZlibStreamReader {
fn new(stream: BorrowedStream) -> ZlibStreamReader {
ZlibStreamReader {
zlib_decoder: ZlibDecoder::new(stream),
}
}
}
impl ReadStream for GzStreamReader {
fn read(
&mut self,
_stream: &mut BorrowedStream,
buf: &mut [u8],
) -> vlcrs_core::error::Result<usize> {
self.gz_decoder.read(buf).map_err(|_| CoreError::Unknown)
}
}
impl ReadStream for ZlibStreamReader {
fn read(
&mut self,
_stream: &mut BorrowedStream,
buf: &mut [u8],
) -> vlcrs_core::error::Result<usize> {
self.zlib_decoder.read(buf).map_err(|_| CoreError::Unknown)
}
}
impl StreamControl for GzStreamReader {
fn can_seek(&mut self, _stream: &mut BorrowedStream) -> bool {
false
}
fn can_fast_seek(&mut self, _stream: &mut BorrowedStream) -> bool {
false
}
}
impl StreamControl for ZlibStreamReader {
fn can_seek(&mut self, _stream: &mut BorrowedStream) -> bool {
false
}
fn can_fast_seek(&mut self, _stream: &mut BorrowedStream) -> bool {
false
}
}
module! {
type: Inflate,
capability: "stream_filter" @ 330,
category: SUBCAT_INPUT_STREAM_FILTER,
description: "Zlib decompression filter - Built in Rust",
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment