Skip to content

Commit e615665

Browse files
committed
add an example for compression (for benchmarking)
1 parent aecdb53 commit e615665

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

fuzz/Cargo.lock

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

fuzz/fuzz_targets/compress.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fuzz_target!(|data: String| {
1212
&mut length,
1313
data.as_ptr().cast(),
1414
data.len() as _,
15+
9,
1516
)
1617
};
1718

fuzz/fuzz_targets/decompress.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fuzz_target!(|data: String| {
3232
&mut length,
3333
data.as_ptr().cast(),
3434
data.len() as _,
35+
9,
3536
)
3637
};
3738

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use core::ffi::c_uint;
2+
3+
use test_libbz2_rs_sys::{compress_c, compress_rs};
4+
5+
fn main() {
6+
let mut it = std::env::args();
7+
8+
let _ = it.next().unwrap();
9+
10+
match it.next().unwrap().as_str() {
11+
"c" => {
12+
let level: i32 = it.next().unwrap().parse().unwrap();
13+
14+
let path = it.next().unwrap();
15+
let input = std::fs::read(&path).unwrap();
16+
17+
let mut dest_vec = vec![0u8; 1 << 28];
18+
19+
let mut dest_len = dest_vec.len() as c_uint;
20+
let dest = dest_vec.as_mut_ptr();
21+
22+
let source = input.as_ptr();
23+
let source_len = input.len() as _;
24+
25+
let err = unsafe { compress_c(dest, &mut dest_len, source, source_len, level) };
26+
27+
if err != 0 {
28+
panic!("error {err}");
29+
}
30+
31+
dest_vec.truncate(dest_len as usize);
32+
33+
drop(dest_vec)
34+
}
35+
"rs" => {
36+
let level: i32 = it.next().unwrap().parse().unwrap();
37+
38+
let path = it.next().unwrap();
39+
let input = std::fs::read(&path).unwrap();
40+
41+
let mut dest_vec = vec![0u8; 1 << 28];
42+
43+
let mut dest_len = dest_vec.len() as std::ffi::c_uint;
44+
let dest = dest_vec.as_mut_ptr();
45+
46+
let source = input.as_ptr();
47+
let source_len = input.len() as _;
48+
49+
let err = unsafe { compress_rs(dest, &mut dest_len, source, source_len, level) };
50+
51+
if err != 0 {
52+
panic!("error {err}");
53+
}
54+
55+
dest_vec.truncate(dest_len as usize);
56+
57+
drop(dest_vec)
58+
}
59+
other => panic!("invalid option '{other}', expected one of 'c' or 'rs'"),
60+
}
61+
}

test-libbz2-rs-sys/src/chunked.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ fn compress_chunked_input() {
141141
&mut dest_len,
142142
SAMPLE1_REF.as_ptr(),
143143
SAMPLE1_REF.len() as _,
144+
9,
144145
)
145146
};
146147
assert_eq!(err, 0);
@@ -293,6 +294,7 @@ fn compress_chunked_output() {
293294
&mut dest_len,
294295
SAMPLE1_REF.as_ptr(),
295296
SAMPLE1_REF.len() as _,
297+
9,
296298
)
297299
};
298300
assert_eq!(err, 0);

test-libbz2-rs-sys/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ macro_rules! assert_eq_compress {
8383
&mut dest_len,
8484
input.as_ptr(),
8585
input.len() as core::ffi::c_uint,
86+
9,
8687
);
8788

8889
dest.truncate(dest_len as usize);
@@ -375,6 +376,7 @@ pub unsafe fn compress_c(
375376
dest_len: *mut libc::c_uint,
376377
source: *const u8,
377378
source_len: libc::c_uint,
379+
blockSize100k: i32,
378380
) -> i32 {
379381
use bzip2_sys::*;
380382

@@ -400,7 +402,7 @@ pub unsafe fn compress_c(
400402
strm.bzfree = None;
401403
strm.opaque = std::ptr::null_mut::<libc::c_void>();
402404
unsafe {
403-
ret = BZ2_bzCompressInit(&mut strm, 9, 0, 30);
405+
ret = BZ2_bzCompressInit(&mut strm, blockSize100k, 0, 30);
404406
if ret != 0 as libc::c_int {
405407
return ret;
406408
}
@@ -428,6 +430,7 @@ pub unsafe fn compress_rs(
428430
dest_len: *mut libc::c_uint,
429431
source: *const u8,
430432
source_len: libc::c_uint,
433+
blockSize100k: i32,
431434
) -> i32 {
432435
use libbz2_rs_sys::*;
433436

@@ -453,7 +456,7 @@ pub unsafe fn compress_rs(
453456
strm.bzfree = None;
454457
strm.opaque = std::ptr::null_mut::<libc::c_void>();
455458
unsafe {
456-
ret = BZ2_bzCompressInit(&mut strm, 9, 0, 30);
459+
ret = BZ2_bzCompressInit(&mut strm, blockSize100k, 0, 30);
457460
if ret != 0 as libc::c_int {
458461
return ret;
459462
}
@@ -1307,6 +1310,7 @@ mod high_level_interface {
13071310
&mut expected_len,
13081311
SAMPLE1_BZ2.as_ptr(),
13091312
SAMPLE1_BZ2.len() as _,
1313+
9,
13101314
)
13111315
};
13121316
assert_eq!(err, 0);

0 commit comments

Comments
 (0)