Skip to content

Commit 834edee

Browse files
committed
Use pure rust lz4_flex
HC support from PSeitz/lz4_flex#191
1 parent 628b4d4 commit 834edee

File tree

11 files changed

+39
-689
lines changed

11 files changed

+39
-689
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
[submodule "selinux"]
22
path = native/src/external/selinux
33
url = https://github.com/topjohnwu/selinux.git
4-
[submodule "lz4"]
5-
path = native/src/external/lz4
6-
url = https://github.com/lz4/lz4.git
74
[submodule "libcxx"]
85
path = native/src/external/libcxx
96
url = https://github.com/topjohnwu/libcxx.git

native/src/Android.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ include $(CLEAR_VARS)
7979
LOCAL_MODULE := magiskboot
8080
LOCAL_STATIC_LIBRARIES := \
8181
libbase \
82-
liblz4 \
8382
libboot-rs
8483

8584
LOCAL_SRC_FILES := \

native/src/Cargo.lock

Lines changed: 11 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/src/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ quick-protobuf = "0.8.1"
3434
flate2 = { version = "1.1.5", default-features = false }
3535
bzip2 = "0.6.1"
3636
zopfli = "0.8.3"
37-
lz4 = "1.28.1"
37+
lz4_flex = { git = "https://github.com/PSeitz/lz4_flex.git", rev = "1c08f39e1ab52f2824763e0a57f2aa925f392167", default-features = false, features = ["hc", "std", "frame"] }
3838
lzma-rust2 = { version = "0.15.4", default-features = false }
3939
nix = "0.30.1"
4040
bitflags = "2.10.0"
@@ -53,7 +53,6 @@ der = "0.8.0-rc.10"
5353
[patch.crates-io]
5454
pb-rs = { git = "https://github.com/topjohnwu/quick-protobuf.git" }
5555
quick-protobuf = { git = "https://github.com/topjohnwu/quick-protobuf.git" }
56-
lz4-sys = { path = "external/lz4-sys" }
5756

5857
[workspace.lints.clippy]
5958
unwrap_used = "deny"

native/src/boot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ bytemuck = { workspace = true, features = ["derive", "min_const_generics"] }
3434
num-traits = { workspace = true }
3535
flate2 = { workspace = true, features = ["zlib-rs"] }
3636
bzip2 = { workspace = true }
37-
lz4 = { workspace = true }
37+
lz4_flex = { workspace = true, features = ["hc", "std", "frame"]}
3838
lzma-rust2 = { workspace = true, features = ["xz", "std", "encoder", "optimization"] }
3939
zopfli = { workspace = true, features = ["gzip"] }

native/src/boot/compress.rs

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ use bzip2::write::BzEncoder;
77
use flate2::Compression as GzCompression;
88
use flate2::read::MultiGzDecoder;
99
use flate2::write::GzEncoder;
10-
use lz4::block::CompressionMode;
11-
use lz4::liblz4::BlockChecksum;
12-
use lz4::{
13-
BlockMode, BlockSize, ContentChecksum, Decoder as LZ4FrameDecoder, Encoder as LZ4FrameEncoder,
14-
EncoderBuilder as LZ4FrameEncoderBuilder,
10+
use lz4_flex::frame::{
11+
BlockSize as LZ4BlockSize, FrameDecoder as LZ4FrameDecoder, FrameEncoder as LZ4FrameEncoder,
12+
FrameInfo as LZ4FrameInfo,
1513
};
1614
use lzma_rust2::{CheckType, LzmaOptions, LzmaReader, LzmaWriter, XzOptions, XzReader, XzWriter};
1715
use std::cmp::min;
@@ -51,9 +49,7 @@ impl<W: Write> WriteFinish<W> for BufWriter<ZopFliEncoder<W>> {
5149

5250
impl<W: Write> WriteFinish<W> for LZ4FrameEncoder<W> {
5351
fn finish(self: Box<Self>) -> std::io::Result<W> {
54-
let (w, r) = Self::finish(*self);
55-
r?;
56-
Ok(w)
52+
Ok(Self::finish(*self)?)
5753
}
5854
}
5955

@@ -65,40 +61,31 @@ impl<W: Write> WriteFinish<W> for LZ4FrameEncoder<W> {
6561
// LZ4BlockEncoder
6662

6763
const LZ4_BLOCK_SIZE: usize = 0x800000;
68-
const LZ4HC_CLEVEL_MAX: i32 = 12;
64+
const LZ4HC_CLEVEL_MAX: u8 = 12;
6965
const LZ4_MAGIC: u32 = 0x184c2102;
7066

7167
struct LZ4BlockEncoder<W: Write> {
7268
write: W,
7369
chunker: Chunker,
74-
out_buf: Box<[u8]>,
7570
total: u32,
7671
is_lg: bool,
7772
}
7873

7974
impl<W: Write> LZ4BlockEncoder<W> {
8075
fn new(write: W, is_lg: bool) -> Self {
81-
let out_sz = lz4::block::compress_bound(LZ4_BLOCK_SIZE).unwrap_or(LZ4_BLOCK_SIZE);
8276
LZ4BlockEncoder {
8377
write,
8478
chunker: Chunker::new(LZ4_BLOCK_SIZE),
85-
// SAFETY: all bytes will be initialized before it is used
86-
out_buf: unsafe { Box::new_uninit_slice(out_sz).assume_init() },
8779
total: 0,
8880
is_lg,
8981
}
9082
}
9183

92-
fn encode_block(write: &mut W, out_buf: &mut [u8], chunk: &[u8]) -> std::io::Result<()> {
93-
let compressed_size = lz4::block::compress_to_buffer(
94-
chunk,
95-
Some(CompressionMode::HIGHCOMPRESSION(LZ4HC_CLEVEL_MAX)),
96-
false,
97-
out_buf,
98-
)?;
99-
let block_size = compressed_size as u32;
84+
fn encode_block(write: &mut W, chunk: &[u8]) -> std::io::Result<()> {
85+
let compressed = lz4_flex::block::compress_hc_to_vec(chunk, LZ4HC_CLEVEL_MAX);
86+
let block_size = compressed.len() as u32;
10087
write.write_pod(&block_size)?;
101-
write.write_all(&out_buf[..compressed_size])
88+
write.write_all(&compressed)
10289
}
10390
}
10491

@@ -123,7 +110,7 @@ impl<W: Write> Write for LZ4BlockEncoder<W> {
123110
let (b, chunk) = self.chunker.add_data(buf);
124111
buf = b;
125112
if let Some(chunk) = chunk {
126-
Self::encode_block(&mut self.write, &mut self.out_buf, chunk)?;
113+
Self::encode_block(&mut self.write, chunk)?;
127114
}
128115
}
129116
Ok(())
@@ -134,7 +121,7 @@ impl<W: Write> WriteFinish<W> for LZ4BlockEncoder<W> {
134121
fn finish(mut self: Box<Self>) -> std::io::Result<W> {
135122
let chunk = self.chunker.get_available();
136123
if !chunk.is_empty() {
137-
Self::encode_block(&mut self.write, &mut self.out_buf, chunk)?;
124+
Self::encode_block(&mut self.write, chunk)?;
138125
}
139126
if self.is_lg {
140127
self.write.write_pod(&self.total)?;
@@ -155,10 +142,12 @@ struct LZ4BlockDecoder<R: Read> {
155142

156143
impl<R: Read> LZ4BlockDecoder<R> {
157144
fn new(read: R) -> Self {
158-
let compressed_sz = lz4::block::compress_bound(LZ4_BLOCK_SIZE).unwrap_or(LZ4_BLOCK_SIZE);
159145
Self {
160146
read,
161-
in_buf: unsafe { Box::new_uninit_slice(compressed_sz).assume_init() },
147+
in_buf: unsafe {
148+
Box::new_uninit_slice(lz4_flex::block::get_maximum_output_size(LZ4_BLOCK_SIZE))
149+
.assume_init()
150+
},
162151
out_buf: unsafe { Box::new_uninit_slice(LZ4_BLOCK_SIZE).assume_init() },
163152
out_len: 0,
164153
out_pos: 0,
@@ -200,11 +189,8 @@ impl<R: Read> Read for LZ4BlockDecoder<R> {
200189
}
201190
}
202191

203-
self.out_len = lz4::block::decompress_to_buffer(
204-
compressed_block,
205-
Some(LZ4_BLOCK_SIZE as i32),
206-
&mut self.out_buf,
207-
)?;
192+
self.out_len = lz4_flex::block::decompress_into(compressed_block, &mut self.out_buf)
193+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
208194
self.out_pos = 0;
209195
}
210196
let copy_len = min(buf.len(), self.out_len - self.out_pos);
@@ -232,17 +218,14 @@ pub fn get_encoder<'a, W: Write + 'a>(
232218
None,
233219
)?),
234220
FileFormat::BZIP2 => Box::new(BzEncoder::new(w, BzCompression::best())),
235-
FileFormat::LZ4 => {
236-
let encoder = LZ4FrameEncoderBuilder::new()
237-
.block_size(BlockSize::Max4MB)
238-
.block_mode(BlockMode::Independent)
239-
.checksum(ContentChecksum::ChecksumEnabled)
240-
.block_checksum(BlockChecksum::BlockChecksumEnabled)
241-
.level(9)
242-
.auto_flush(true)
243-
.build(w)?;
244-
Box::new(encoder)
245-
}
221+
FileFormat::LZ4 => Box::new(LZ4FrameEncoder::with_compression_level(
222+
LZ4FrameInfo::new()
223+
.block_size(LZ4BlockSize::Max4MB)
224+
.block_checksums(true)
225+
.content_checksum(true),
226+
w,
227+
LZ4HC_CLEVEL_MAX,
228+
)),
246229
FileFormat::LZ4_LEGACY => Box::new(LZ4BlockEncoder::new(w, false)),
247230
FileFormat::LZ4_LG => Box::new(LZ4BlockEncoder::new(w, true)),
248231
FileFormat::ZOPFLI => {
@@ -267,7 +250,7 @@ pub fn get_decoder<'a, R: Read + 'a>(
267250
FileFormat::XZ => Box::new(XzReader::new(r, true)),
268251
FileFormat::LZMA => Box::new(LzmaReader::new_mem_limit(r, u32::MAX, None)?),
269252
FileFormat::BZIP2 => Box::new(BzDecoder::new(r)),
270-
FileFormat::LZ4 => Box::new(LZ4FrameDecoder::new(r)?),
253+
FileFormat::LZ4 => Box::new(LZ4FrameDecoder::new(r)),
271254
FileFormat::LZ4_LG | FileFormat::LZ4_LEGACY => Box::new(LZ4BlockDecoder::new(r)),
272255
FileFormat::ZOPFLI | FileFormat::GZIP => Box::new(MultiGzDecoder::new(r)),
273256
_ => unreachable!(),

native/src/external/Android.mk

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@ LOCAL_SRC_FILES := \
1111
xz-embedded/xz_dec_stream.c
1212
include $(BUILD_STATIC_LIBRARY)
1313

14-
# liblz4.a
15-
include $(CLEAR_VARS)
16-
LOCAL_MODULE := liblz4
17-
LOCAL_C_INCLUDES := $(LOCAL_PATH)/lz4/lib
18-
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
19-
LOCAL_SRC_FILES := \
20-
lz4/lib/lz4.c \
21-
lz4/lib/lz4frame.c \
22-
lz4/lib/lz4hc.c \
23-
lz4/lib/xxhash.c
24-
include $(BUILD_STATIC_LIBRARY)
25-
2614
SE_PATH := $(LOCAL_PATH)/selinux
2715

2816
# libsepol.a

native/src/external/lz4

Lines changed: 0 additions & 1 deletion
This file was deleted.

native/src/external/lz4-sys/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)